var autoDisableFilters=true;
var tfFormName='lqForm';
var tfAnimate = true;
var elToolBar = false; //new elEffect('qbContent');
window.onbeforeunload=tfUnloading;
addLoadEvent(tfLoaded);

function tfButtonClick(closeTitle, openTitle){
	strID= "qbContent";
	if (document.getElementById) el = window.document.getElementById(strID);
	else if (document.all) el = window.document.all[strID];
	if (!closeTitle) var closeTitle = 'Close';
	if (!openTitle) var openTitle = 'Search';

	 
	btnText = xbGetInnerHTML(window,'qbButton')
	elBbtn   =xbGetElement(window,'qbButton') // Info container
	btnText = elBbtn.value;
	//alert(el.style.height);
	if (!  elToolBar ) elToolBar = new elExpand('qbContent');
	
	if (btnText==closeTitle){	// CLOSE
		elBbtn.value = openTitle;
		if (tfAnimate) divClose = elToolBar.close(); //new DivSizer(strID, el.fullHeight, 0 );
		else xbElClose(el);
	}
	else{	// OPEN
		elBbtn.value = closeTitle;
		if (tfAnimate) elToolBar.open(); //divOpen = new DivSizer(strID, 0, el.fullHeight );
		else xbElOpen(el);
		
	}
	elBbtn.blur();
}
 

function tfUnloading(){
      // scrolling offset calculation via www.quirksmode.org
    if (self.pageYOffset) vScroll = self.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop) vScroll = document.documentElement.scrollTop; 
    else if (document.body) vScroll = document.body.scrollTop; 
    //alert(document.documentElement.scrollTop);
	var theDate = new Date();
	var expiryDate = new Date( theDate.getTime() + 20000 );
	var expires = expiryDate.toGMTString();
	var cookie = 'scrollTop=' + vScroll + ';expires=' + expires;
	document.cookie = cookie ;

}
function tfGetCookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function tfLoaded(){
	var url = String(document.location);
	var isFave = (url.indexOf('action=fav') >0 );
	vScroll = tfGetCookie('scrollTop');
	var browser=navigator.appName;
	if (isFave && vScroll>0){
		 
		if (browser=="Microsoft Internet Explorer"){
			document.documentElement.scrollTop= vScroll  ;
		}
		else window.scrollTo(0, vScroll);
	}
}

 
 
function tfGetForm(){
	var tfForm = (document.getElementById(tfFormName));
 
	return( tfForm );
} 
function pcFocus(){ //postcode box has focus
	var tfForm = tfGetForm();
	var elCountrySelect = tfForm.country;
	if (elCountrySelect){
		var countryVal = elCountrySelect.options[elCountrySelect.selectedIndex].value;
		if(countryVal != 'United Kingdom') {
			setSelect(elCountrySelect, 'United Kingdom');    // select UK
			countryChanged();
		}
	}
}
function setSelect(el, value) {
	for(index = 0;  index < el.length; index++) {
		if(el[index].value == value) el.selectedIndex = index;
   }
}
function countryChanged( ){	// Also called on form load to set filters
     var arGeoRegions;
	var tfForm = tfGetForm();
     var elCountrySelect = tfForm.country;
	var elRegionSelect = tfForm.region;
	var elCountySelect = tfForm.county;
     var elGeoRegionSelect = tfForm.geoRegion;
	var elPostCode = tfForm.ukPostCode;
	var countryVal = elCountrySelect.options[elCountrySelect.selectedIndex].value;
 
	// Clear county select
	elCountySelect.options.length=0
	// Get new county array

	if(countryVal == '*'){     // All countries
		 if (elGeoRegionSelect){
			 elGeoRegionSelect.selectedIndex = 0;    // Set region = All regions
			 elGeoRegionSelect.disabled = true;      // Disable Regions
		 }
		 if (elRegionSelect){
			 elRegionSelect.selectedIndex = 0;    // Set region = All regions
			 elRegionSelect.disabled = true;      // Disable Regions
		 }
		 if (elCountySelect){
		 	elCountySelect.selectedIndex = 0;    // Disable Counties
		 	elCountySelect.disabled = true;      // Set county = All Counties
		 }
		 if (elPostCode){
			elPostCode.value='';
			elPostCode.disabled=true;
		}
 	}
	else if(countryVal == 'United Kingdom'){     // UK
       	if (elGeoRegionSelect){
       		var arGeoRegions =  arAllGeoRegions[countryVal].split(",");
			elGeoRegionSelect.disabled = false;	// Enable regions
			elGeoRegionSelect.options.length=0;    // Clear region select
		}

		if (elRegionSelect){
       		if(arAllRegions[countryVal]) arRegions =  arAllRegions[countryVal].split(",");
			elRegionSelect.disabled = false;	// Enable regions
			elRegionSelect.options.length=0;    // Clear region select
		}
		if (elCountySelect){
       		var arCounties =  arAllCounties[countryVal].split(",");
			elCountySelect.disabled = false;	// Enable regions
			elCountySelect.options.length=0;    // Clear region select
		}
		if (elPostCode){
			elPostCode.disabled=false;
			elPostCode.value='';
		}
 	}
	else{          // Non-UK
       	if (elGeoRegionSelect){
       		var arGeoRegions =  arAllGeoRegions[countryVal].split(",");
			elGeoRegionSelect.disabled = false;	// Enable regions
			elGeoRegionSelect.options.length=0;    // Clear region select

		}
		if (elRegionSelect){
       		var arRegions =  arAllRegions[countryVal].split(",");
			elRegionSelect.disabled = false;	// Enable regions
			elRegionSelect.options.length=0;    // Clear region select
		}
		if (elCountySelect){
	          var arCounties =  arAllCounties[countryVal].split(",");
			elCountySelect.options.length=0;    // Clear region select
			elCountySelect.disabled = true;	// Disable  regions
		}
		if (elPostCode){
			elPostCode.value='';
			elPostCode.disabled=true;
		}

	}
	if (elGeoRegionSelect){
	  	for(i=0;i<arGeoRegions.length;i++) {
	       key = (i==0)? '*' : arGeoRegions[i];
	       val=arGeoRegions[i];
	       if (val) elGeoRegionSelect.options[i]=new Option(val, key, true, false)
	  	}
	}

	if(arRegions){
	     for(i=0;i<arRegions.length;i++) {
	       key = (i==0)? '*' : arRegions[i];
	       val=arRegions[i];
	       if (val) elRegionSelect.options[i]=new Option(val, key, true, false)
	  	}
 	}

	if (arCounties){
	  	for(i=0;i<arCounties.length;i++) {
	       key = (i==0)? '*' : arCounties[i];
	       val=arCounties[i];
	       if (val) elCountySelect.options[i]=new Option(val, key, true, false)
	  	}
 	}

}
function geoRegionChanged( ){
  	// If region is selected disable counties
	var tfForm = tfGetForm();
  	var elRegionSelect = tfForm.region;
	var elGeoRegionSelect = tfForm.geoRegion;
	var elCountySelect = tfForm.county;

	var geoRegionVal = elGeoRegionSelect.options[elGeoRegionSelect.selectedIndex].value;

	if(geoRegionVal=='*'){     // All Regions
		 elCountySelect.selectedIndex = 0;    // Select All Counties
		 if (autoDisableFilters) elCountySelect.disabled = false;      // Enable counties
		 elRegionSelect.selectedIndex = 0;    // Select All  Regions
           if (autoDisableFilters) elRegionSelect.disabled = false;      // Enable Regions
 	}
	else{
		elCountySelect.selectedIndex=0    // Select All Counties
		if (autoDisableFilters) elCountySelect.disabled = true;	// Disable counties
		elRegionSelect.selectedIndex = 0;    // Select All  Regions
		if (autoDisableFilters) elRegionSelect.disabled = true;      // Disable Regions
	}
}

function regionChanged( ){

  	// If region is selected disable counties
	var tfForm = tfGetForm();
	var elRegionSelect = tfForm.region;
	var elCountySelect = tfForm.county;
     var elGeoRegionSelect = tfForm.geoRegion;
	var regionVal = elRegionSelect.options[elRegionSelect.selectedIndex].value;

	if(regionVal=='*'){     // All Regions

		 if (autoDisableFilters) elCountySelect.disabled = false;      // Enable counties
		 elCountySelect.selectedIndex = 0;    // Select All Counties
		 if (elGeoRegionSelect){
			 elGeoRegionSelect.selectedIndex = 0;    // Select All Geo Regions
			 if (autoDisableFilters) elGeoRegionSelect.disabled = false;      // Enable Geo Regions
   }
 	}
	else{

		elCountySelect.selectedIndex=0    // Select All Counties
		if (autoDisableFilters) elCountySelect.disabled = true;	// Disable counties
		if (elGeoRegionSelect){
			elGeoRegionSelect.selectedIndex = 0;    // Select All Geo Regions
			if (autoDisableFilters) elGeoRegionSelect.disabled = true;      // Disable Geo Regions
  		}
	}
}
function countyChanged( ){
  	// If region is selected disable counties
	var tfForm = tfGetForm();
	elRegionSelect = tfForm.region;
	elCountySelect = tfForm.county;
     elGeoRegionSelect = tfForm.geoRegion;
	var countyVal = elCountySelect.options[elCountySelect.selectedIndex].value;

	if(countyVal=='*'){     // All Regions
		 if (autoDisableFilters) elRegionSelect.disabled = false;      // Enable counties
		 elRegionSelect.selectedIndex = 0;    // Select All Counties
	      if (elGeoRegionSelect){
			 if (autoDisableFilters) elGeoRegionSelect.disabled = false;      // Enable Geo Regions
			 elGeoRegionSelect.selectedIndex = 0;    // Select All Geo Regions
 	 	   }
	 }
	else{
		elRegionSelect.selectedIndex=0    // Select All Counties
		if (autoDisableFilters) elRegionSelect.disabled = true;	// Disable counties
		if (elGeoRegionSelect){
			if (autoDisableFilters) elGeoRegionSelect.disabled = true;      // Disable Geo Regions
			 elGeoRegionSelect.selectedIndex = 0;    // Select All Geo Regions
		}
	}
}

function checkBoxClick(el){
    	offElName =   el.name + '_off';
    	offEl =  xbGetElement(window, offElName);
	if (offEl){	
       	if ( ! el.checked) offEl.value =1;
  		else offEl.value =0;
 	} 	
}
 function tfLocalMapSBClick(ptrText){
 	alert('GoogleMap Pointer Text...\n' + ptrText);
	return(false);
 }