
var scrollPosX,scrollPosY;
var CFWI={};
	
	// create custom defaultButtons object
	CFWI.DefaultButtons={
		registerButton:function(cId,bId){
			var oC=document.getElementById(cId);
			var oB=document.getElementById(bId);
			if(CFWI.IsDefined(oC)&&CFWI.IsDefined(oB)){
				attachEventListener(oC,'keypress',function(e){
					CFWI.DefaultButtons.Click(e,oB);
				},true);
			}
		},
		Click:function(e,lbtn){
			if(!e.which&&!e.keyCode){
				return;
			}else{
				var k=(e.which||e.keyCode);
				var t=(e.target||e.srcElement);
				try{
					if(k===13){
						if(t.type==='text'){if(e.preventDefault){e.preventDefault();}e.returnValue=false;}
						if(t.type!=='textarea'){
							if(lbtn.getAttribute('onclick')){
								if(!(document.all)){
									s=lbtn.getAttribute('onclick').toString();
								}else{
									s=lbtn.attributes["onclick"].value;
								}
								if(s.indexOf('return ')>-1){
									s=s.replace('return ','');
								}
								if(eval(s)){
									eval(lbtn.href);
								}else{
									if(e.preventDefault){e.preventDefault();}
									e.returnValue = false;
									return false;
								}
							}else{
								eval(lbtn.href);
								return false;
							}
						}
					}
				}
				catch(err){
					//alert(err);
					return false;
				}
			}
		}
	};
	// Example: CFWI.DefaultButtons.registerButton('containerID','buttonID');
	
	// Check to see if an object is defined (HTML Object, Javascript Object, Variable)
	CFWI.IsDefined=function(what){
		if(typeof(what)!=='undefined'&&what!=='null'&&what!==null){
			return true;
		}else{
			return false;
		}
	}
	
	// Custom Object to handle various Tabs Tasks
	CFWI.Tabs={
		LoadTabUrl:function(fName,url){
			if(CFWI.IsDefined(fName)&&CFWI.IsDefined(url)){
				var f=parent.frames[fName]?parent.frames[fName]:window.frames[fName];
				if(CFWI.IsDefined(f)){
					if(!(f.location.href.toLowerCase().indexOf(url.toLowerCase())>-1)){
						f.location.replace(url);
					}
				}
			}
		},
		SetTabPageHeight:function(args){
			if(!(document.all)){
				var obj;
				if(CFWI.IsDefined(args)){
					if(args.length>0){
						for(var i=0;i<args.length;i+=1){
							obj=$get(args[i]);
							if(CFWI.IsDefined(obj)){
								obj.style.height='auto';
								if(obj.offsetParent){
									obj.offsetParent.style.height='100px';
									obj.offsetParent.style.height='auto';
								}
							}
						}
					}
				}
			}
		}
	}
	
	// Custom Object to Force Reload of Framed Page
	CFWI.Frame={
		Reload:function(fName){
			if(CFWI.IsDefined(fName)){
				var f=parent.frames[fName]?parent.frames[fName]:window.frames[fName];
				if(CFWI.IsDefined(f)){
					f.location.replace(f.location.href);
				}
			}
		},
		ReloadSubFrame:function(obj){
			if(CFWI.IsDefined(obj)){
				obj.location.replace(obj.location.href);
			}
		}
	}
	
	CFWI.Effects={
		getOpacity:function(id){
			var o=getObj(id);
			if(typeof(o)!=='undefined'){
				if(typeof(o.style.opacity)!=='undefined'){
					if(o.style.opacity.toString().length>0){
						return (o.style.opacity*100);
					}else{
						return '';
					}
				}else if(typeof(o.style.MozOpacity)!=='undefined'){
					if(o.style.MozOpacity.toString().length>0){
						return (o.style.MozOpacity*100);
					}else{
						return '';
					}
				}else if(typeof(o.style.KhtmlOpacity)!=='undefined'){
					if(o.style.KhtmlOpacity.toString().length>0){
						return (o.style.KhtmlOpacity*100);
					}else{
						return '';
					}
				}else if(typeof(o.style.filter)!=='undefined'){
					if(o.style.filter.toString().length>0){
						return parseInt(o.style.filter.toString());
					}else{
						return '';
					}
				}
			}
		},
		setOpacity:function(id,v){
			var o=getObj(id),x=(v/100);
			if(typeof(o)!=='undefined'){
				o.style.opacity=(x);
				o.style.MozOpacity=(x);
				o.style.KhtmlOpacity=(x);
				o.style.filter="alpha(opacity="+v+")";
			}
		},
		fade:function(id,which,ms,display){
			var timer=0,i,speed,v,o;
			if(typeof(ms)==='undefined'){ms=500;}
			if(typeof(display)==='undefined'){display='block';}
			speed=Math.round(ms/100);
			if(typeof(id)!=='undefined'&&typeof(which)!=='undefined'){
				o=getObj(id);
				switch(which){
					case 'out':
						v=CFWI.Effects.getOpacity(id);
						if(v.toString().length===0||v.toString().length>1){
							for(i=100;i>=0;i--){
								setTimeout("CFWI.Effects.setOpacity('"+id+"',"+i+");",(timer*speed));
								timer++;
							}
							setTimeout(function(){
								o.style.display='none';
							},ms);
						}
						break;
					case 'in':
						v=CFWI.Effects.getOpacity(id);
						if(v.toString().length>0&&v.toString().length<3){
							getObj(id).style.display=display;
							for(i=0;i<=100;i++){
								setTimeout("CFWI.Effects.setOpacity('"+id+"',"+i+");",(timer*speed));
								timer++;
							}
						}
						break;
					default:
						break;
				}
			}
		}
	}
	

// Attaches an Event Listener to a valid document event type
	function attachEventListener(target, eventType, functionRef, capture){if(typeof target.addEventListener!="undefined"){target.addEventListener(eventType,functionRef,capture);}else if(typeof target.attachEvent!="undefined"){target.attachEvent("on"+eventType,functionRef);window.attachEvent("onunload",function(e){removeListeners(target,eventType,functionRef,capture);});}else{return false;}return true;}
	function removeListeners(target, eventType, functionRef, capture){if(typeof target.removeEventListener!="undefined"){target.removeEventListener(eventType,functionRef,capture);}else if(typeof target.detachEvent!="undefined"){target.detachEvent("on"+eventType,functionRef);}else{return false;}return true;}

// Generic GetHTML Object Functions
	function getObj(oId){var d=document,i,el;el=d.getElementById?d.getElementById(oId):d.all?d.all[oId]?d[oId]:d[oId]:null;if(!el){if(d.forms.length>0){for(i=0; !el && i<d.forms.length; i++){el=d.forms[i][oId];}}}return el;}
	function get_HtmlObjWH(obj){
		if(typeof(obj)!=='undefined'&&obj!==null){
			if(obj.offsetWidth&&obj.offsetHeight){
				return [obj.offsetWidth,obj.offsetHeight];
			}else{
				return [0,0];
			}
		}
	}
	function CFWI_getXYofElement(obj){var x=y=0;if(obj.offsetParent){x=obj.offsetLeft;y=obj.offsetTop;while(obj=obj.offsetParent){x+=obj.offsetLeft;y+=obj.offsetTop;}}return [x,y];}

// Get Viewport Dimensions & Scroll Distances
	function CFWI_getWHofViewport(){var w=window.innerWidth?window.innerWidth-21:document.documentElement?document.documentElement.clientWidth:document.body.clientWidth?document.body.clientWidth:null;var h=window.innerHeight?window.innerHeight-21:document.documentElement?document.documentElement.clientHeight:document.body.clientHeight?document.body.clientHeight:null;if(w&&h){return [w,h];}}
	function CFWI_getXYofViewportScroll(){var x=window.innerWidth?window.pageXOffset:document.documentElement?document.documentElement.scrollLeft:document.body.scrollLeft?document.body.scrollLeft:null;var y=window.innerHeight?window.pageYOffset:document.documentElement?document.documentElement.scrollTop:document.body.scrollTop?document.body.scrollTop:null;return [x,y];}
	function CFWI_setInitialXYofViewportScroll(){scrollPosX=CFWI_getXYofViewportScroll()[0],scrollPosY=CFWI_getXYofViewportScroll()[1];}
	attachEventListener(window,'load',CFWI_setInitialXYofViewportScroll,false);


// Generic Append / Replace Css ClassName functions
	function CFWI_appendCssClass(obj,n){obj.className+=n;}
	function CFWI_restoreCssClass(obj,n){obj.className=obj.className.replace(n,'');}
// Generic Focus Event Handler for IE Forms 
	function _ieFocus(obj,which){var cssclass=' has-focus';if(document.all){if(which.toLowerCase()=='focus'){CFWI_appendCssClass(obj,cssclass);}else if(which.toLowerCase()=='blur'){CFWI_restoreCssClass(obj,cssclass);}}else{return true;}}
	
// Generic Append to URL function
	// usage: 
	// srcId = Name of Cookie or Textbox that contains the value to be appended / replaced in the url
	// targetName = Name assigned to URL to be updated via the name attribute (e.g. <a name="mylink" href="">some text</a>
	// paramName = Name of Variable to find / append (e.g. find "key" in savedListing.aspx?format=print&key=guid&otherparams=something)
	function _appendToUrl(srcId,targetName,paramName){try{var strToAppend='',strToReplace='',newStr='',iStart,iStop,arr=document.getElementsByName(targetName),url='',val=getCookie(srcId);if(val==null||val=='null'){val=getObj(srcId);if(val&&val.type=='text'){val=val.value;}else{val='';}}if(arr&&arr.length>0){for(var i=0;i<arr.length;i++){url=arr[i].href;if(url.indexOf(paramName)==-1){strToAppend="?"+paramName+"="+val;if(url.indexOf("?")>-1){url=url.replace("?",(strToAppend+"&"));if(url.lastIndexOf("&")==url.length-1){url=url.substring(0,url.length-1);}}else{url=url+strToAppend;}}else if(url.indexOf(paramName)>-1){startindex=url.indexOf(paramName);stopindex=url.indexOf("&",startindex);newStr=paramName+"="+val;if(stopindex==-1){stopindex=url.length;}strToReplace=url.substring(startindex,stopindex);url=url.replace(strToReplace,newStr);}arr[i].href=url;}}}catch(err){}}

// Uses location.replace() method to set the href of the targeted iframe to prevent iframe url's from being added to history object.
	function CFWI_loadUrl(sender){try{if(sender.target){var theIframe=parent.frames[sender.target.toString()];if(theIframe){theIframe.location.replace(sender.href);}}else{if(sender.href){location.replace(sender.href);}}}catch(err){}}

// Generic Toggle Display Functions
	function CFWI_toggleDisplay(id,which){var obj=getObj(id);if(typeof obj=='undefined'){return}else{obj.style.display=which;return;};}

// Used to set the iframe width/height within the BrokerIDX UI Popup Window
	CFWI.ResizeDialog={
		IsInitialized:false,
		Title:'',
		Width:'',
		Height:'',
		MinWidth:250,
		MinHeight:100,
		oParent:'',
		oParentFrame:'',
		oParentTitle:'',
		oContent:'',
		Init:function(){
			CFWI.ResizeDialog.oParent=parent.getObj('cfwi_dialog');
			CFWI.ResizeDialog.oParentFrame=parent.document.getElementById('CFWI_Dialog_Iframe');
			CFWI.ResizeDialog.oContent=getObj('iframe-page-content');
			CFWI.ResizeDialog.oParentTitle=parent.getObj('CFWI_Dialog_Title');
			if(CFWI.ResizeDialog.Width.length===0){
				CFWI.ResizeDialog.Width=CFWI.ResizeDialog.GetWH()[0]+30;
			}
			if(CFWI.ResizeDialog.Height.length===0){
				CFWI.ResizeDialog.Height=CFWI.ResizeDialog.GetWH()[1]+30;
			}
			if(CFWI.ResizeDialog.Title.length!==0){
				CFWI.ResizeDialog.oParentTitle.innerHTML=CFWI.ResizeDialog.Title;
			}
		},
		GetWH:function(){
			var w,h,arr=get_HtmlObjWH(CFWI.ResizeDialog.oContent);
			if(arr[0]<CFWI.ResizeDialog.MinWidth){
				w=CFWI.ResizeDialog.MinWidth;
			}else{
				w=arr[0];
			}
			if(arr[1]<CFWI.ResizeDialog.MinHeight){
				h=CFWI.ResizeDialog.MinHeight;
			}else{
				h=arr[1];
			}
			return [w,h];
		},
		SetWH:function(arr){
			var aW=0,aH=0;
			if(typeof(arr)!=='undefined'){
				if(arr.length>0){
					aW=arr[0];
					aH=arr[1];
				}else{
					aW=aH=arr;
				}
			}
			CFWI.ResizeDialog.Width=CFWI.ResizeDialog.GetWH()[0]+30+aW;
			CFWI.ResizeDialog.Height=CFWI.ResizeDialog.GetWH()[1]+30+aH;
		},
		Resize:function(){
			if(!(CFWI.ResizeDialog.IsInitialized)){CFWI.ResizeDialog.Init();}
			CFWI.ResizeDialog.oParent.style.width=CFWI.ResizeDialog.Width+'px';
			CFWI.ResizeDialog.oParentFrame.style.height=CFWI.ResizeDialog.Height-20+'px';
			if(typeof(parent.oDialog)!=='undefined'){
				parent.oDialog.style.width=CFWI.ResizeDialog.Width+'px';
				parent.oDialog.style.height=CFWI.ResizeDialog.Height+'px';
				parent.CFWI_snapDialogToElement();
			}
		},
		UpdateTitle:function(s){
			if(CFWI.IsDefined(s)){
				if(s.length>0){
					CFWI.ResizeDialog.oParentTitle.innerHTML=s;
					return true;
				}else{
					CFWI.ResizeDialog.oParentTitle.innerHTML=CFWI.ResizeDialog.Title;
					return true;
				}
			}
		},
		Exec:function(){
			attachEventListener(window,'load',CFWI.ResizeDialog.Resize,false);
		}
	};

// Close the Dialog Window
	function CFWI_Dialog_Close(){
		if(typeof(parent.Dialog)!=='undefined'){
			parent.Dialog.Close();
		}
	}

// Determine how to handle, then process the parent window.  Used via Dialog Content iFrame.
	// Refresh, Change, Post Parent Window Functions
	function CFWI_Dialog_ReloadParent(){parent.document.location.replace(parent.document.location.href);}
	function CFWI_Dialog_ChangeParent(url){parent.document.location.replace(url);}
	function CFWI_Dialog_PostParent(){parent.document.forms[0].submit();}
	// Prep, then Handle / Process Parent Window
	function CFWI_PrepParentWindow(action,url){if(typeof action=='undefined'){return false;}else if(typeof url=='undefined'){return false;}else{try{var v=action+'|'+url;writeCookie("HandleParentWindow",v);return true;}catch(err){}}}
	function CFWI_HandleParentWindow(){if(getCookie("HandleParentWindow")&&getCookie("HandleParentWindow").length>0){var action=getCookie("HandleParentWindow").split("|")[0];var url=''+getCookie("HandleParentWindow").split("|")[1];writeCookie("HandleParentWindow",'');CFWI_ProcessParentWindow(action,url);}}
	function CFWI_ProcessParentWindow(action,url){if(typeof action=='undefined'){return false;}else{try{window.setTimeout('CFWI_Dialog_Close()',250);switch(action.toLowerCase()){case "changeparent":CFWI_Dialog_ChangeParent(url);break;case "reloadparent":CFWI_Dialog_ReloadParent();break;case "postparent":CFWI_Dialog_PostParent();break;case "none":break;default:break;}}catch(err){}}}


	
// ### Last Selected Tab Code ### //

    // Control Tabstrip Tab Select event on individual tabs to use location.replace() method to prevent tab selections from being
    // remembered in history.  This function works both separately and in conjuction with the tabstrip-wide CFWI_onTabSelect functions.
    function CFWI_TabstripTabSelect(sender){var iframe=parent.frames[sender.Target];if(!iframe){iframe=frames[sender.Target];}if(iframe){try{iframe.location.replace(sender.NavigateUrl);return false;}catch(err){}}}
    
    // Function to select the default tab index for the sidebar tabstrip control
	function CFWI_SelectDefault(){
		if(typeof tsSidebar!=='undefined'){
			var isVisible = tsSidebar.getSelectedTab().get_visible();
			if(isVisible!==true||isVisible!=='true'||isVisible!==1||isVisible!=='1'){
				CFWI_SelectedTab('tsSidebar',0);
			}
		}
	}
	
	// TabStrip-wide event handler that Selects, then stores the last selected TabStrip Tab
    function CFWI_onTabSelect(sender, eventArgs){
	    var oTabStripId=sender.get_id();
	    var oSelectedTabId=eventArgs.get_tab().get_id();
	    var selectedTabsList='';
	    var strFn=oTabStripId+'.findTabById(\''+oSelectedTabId+'\')';
	    var found=false;
	    
    	
	    if(getCookie("SelectedTabs")){
		    selectedTabsList=getCookie("SelectedTabs");
		    var arr=selectedTabsList.split("|");
		    for(var i=0;i<arr.length;i++){
			    if(arr[i].indexOf(oTabStripId)>-1){
				    selectedTabsList=selectedTabsList.replace(arr[i],strFn);
				    found=true;
				    break;
			    }
		    }
		    if(!found){
			    if(selectedTabsList.length>0){
				    selectedTabsList+='|'+strFn;
			    }else{
				    selectedTabsList=strFn;
			    }
		    }
	    }else{
		    selectedTabsList=strFn;
	    }
	    writeCookie("SelectedTabs",selectedTabsList);
    	if(eval(strFn+'.isSelected()')){
    		return false;
    	}
    }

    function CFWI_setLastSelectedTab(){
	    if(getCookie("SelectedTabs")){
		    var arr=getCookie("SelectedTabs").split("|"),updateTabsList=getCookie("SelectedTabs"),fn='',isVisible='',tn='';
		    for(var i=0;i<arr.length;i++){
			    try {
					tn=eval(arr[i]+'.get_id()');
					isVisible=arr[i]+'.get_visible()';
					fn=arr[i]+'.select()';
					if(eval(isVisible)===true||eval(isVisible)==='true'||eval(isVisible)===1||eval(isVisible==='1')){
						eval(fn);
					}else{
						throw("Tab " + tn + " Not Found");
					}					
			    } 
			    catch(err){
					//The tab doesn't exist on this page...so we clear this path from the cookie.
				    if(updateTabsList.length>0){
					    if(updateTabsList.indexOf('|'+arr[i])>-1){
						    updateTabsList=updateTabsList.replace('|'+arr[i],'');
					    }else if(updateTabsList.indexOf(arr[i]+'|')>-1){
						    updateTabsList=updateTabsList.replace(arr[i]+'|','');
					    }else{
						    updateTabsList=updateTabsList.replace(arr[i],'');
					    }
				    }
				    //If the Tab Strip in question is the Sidebar, and the previously selected tab is no longer visible,
				    //then we default its view to the first tab (index 0);
				    CFWI_SelectDefault();
			    }
		    }
		    writeCookie("SelectedTabs",updateTabsList);
	    }
    }

    // Usage: CFWI_setSelectedTab('<%=TabStrip.ClientID %>',index);
    // Index value of -1 exits the function before it runs
    function CFWI_setSelectedTab(tabStripId, index){
	    try {
		    if(index>-1){
			    var updateTabsList='',itemToReplace='';
			    var strFn=tabStripId+'.get_tabs().getTab('+index+')';
			    if(getCookie("SelectedTabs")){
				    var arr=getCookie("SelectedTabs").split("|"),updateTabsList=getCookie("SelectedTabs");
				    for(var i=0;i<arr.length;i++){
					    if(arr[i].indexOf(tabStripId)>-1){
						    itemToReplace=arr[i];
						    break;
					    }
				    }
				    if(itemToReplace.length>0 && updateTabsList.length>0){
					    updateTabsList=updateTabsList.replace(itemToReplace,strFn);
				    }else{
					    if(updateTabsList.length>0){
						    updateTabsList+='|'+strFn;
					    }else{
						    updateTabsList=strFn;
					    }
				    }
			    }else{
				    updateTabsList=strFn;
			    }
			    writeCookie("SelectedTabs",updateTabsList);
		    }
		    return;
	    }
	    catch(err){
			return;
	    }
    }

    function CFWI_SelectedTab(tabStripId,index){
	    var strFn=tabStripId+'.get_tabs().getTab('+index+').select()';
	    eval(strFn);
    }

/* ### Cookies 
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
*/
function setCookie(name, value, expires, path, domain, secure){
	var arrKeys=value.split("|"),c=arrKeys.length,newValue=value,tempValue='';
	var msg= 'A maximum of 50 listings may be selected for comparison at one tithis.\n';
		msg+='You have selected '+ c +' listings for comparison.\n';
		if((c-50)==1){
			msg+='The last listing will be de-selected automatically from your list.';
		} else {
			msg+='The last '+ (c-50) +' listings will be de-selected automatically from your list.';
		}
	if(arrKeys.length>50){
		for(var i=0;i<50;i++){
			if(tempValue==''){
				tempValue=arrKeys[i];
			} else {
				tempValue+='|'+arrKeys[i];
			}
		}
		newValue=tempValue;
		alert(msg);
	}
	writeCookie(name, newValue, expires, path, domain, secure);
} 

function writeCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ("; path=/") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/*
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
*/
function getCookie(name){
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1){
        begin = dc.indexOf(prefix);
        if (begin == -1) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ("; path=/") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}