
// if want to disblay text boxes with load gif - not so recomanded if user is realy fast he can cach it while loading and auto complete doesnt show there for hide it while loading ajax see below
//document.searchForm.CodeLoad.className = "ac_loading";
//document.searchForm.CityLocation.className = "ac_loading";
//document.searchForm.RegAreaLocation.className = "ac_loading";
  
$(function(){
			$(".BTN").click(function(){
		     var id=$(this).attr("id");
			 if(id=="showLoc"){
				 showLoc();
				 $(this).css("backgroundImage","url(/images/leftms.jpg)");
				 $("#showPos").css("backgroundImage","url(/images/right.jpg)");
			 }else{
				 showPos();
				 $(this).css("backgroundImage","url(/images/rightms.jpg)");
				 $("#showLoc").css("backgroundImage","url(/images/left.jpg)");
			 }
		 });
 });
		function showLoc(){
	$("#postalsearchdiv").hide();
	$("#searchIntro").hide();
	$("#locationSearch").show();
}
function showPos(){
	$("#postalsearchdiv").show();
	$("#searchIntro").hide();
	$("#locationSearch").hide();
}
function showInt(){
	$("#postalsearchdiv").hide();
	$("#searchIntro").show();
	$("#locationSearch").hide();
}
document.getElementById('globalClear').value = "0";

function findCity(li) {
if( li == null ) 
{
	if (document.getElementById('globalClear').value == "1")
	{
		document.getElementById("CityLocation").value = "";// not found clear user value
	}
	return; //alert("No match, city was not found !"); 	
}
// if coming from an AJAX call, let's use the CityId as the value	
if( !!li.extra ) 
	{
		var sValue = li.extra[0]; 	
	}
// otherwise, let's just display the value in the text box	
else 
	{	var sValue = li.selectValue; 	
		
	}
	//alert("findCity: " + sValue);
	if (document.getElementById('globalClear').value == "0")
	{
		LoadRegArea(sValue);
	}
}

//function formatItem(row) {	return row[0] + " (id: " + row[1] + ")";}
/*
function lookupLocal(){	
	var oSuggest = $("#CityLocation")[0].autocompleter; 	
	oSuggest.findValue();	
	
	return false;
}
*/
function selectCity(li) {	findCity(li); }

////////////////////////////////////////////////////////////////////////////////
function findRegArea(li) {
if( li == null ) 
{
	if (document.getElementById('globalClear').value == "1") 
	{
		document.getElementById("RegAreaLocation").value = "";// not found clear user value
	}
	return; //alert("No match, RegArea was not found !"); 	
}
// if coming from an AJAX call, let's use the CityId as the value	
if( !!li.extra ) 
	{
		var sValue = li.extra[0]; 	
	}
// otherwise, let's just display the value in the text box	
else 
	{	var sValue = li.selectValue; 	
		
	}
	//alert("findRegArea: " + sValue);
	
}

function selectRegArea(li) {	findRegArea(li); }
//////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
function findCode(li) {
if( li == null ) 
{
	if (document.getElementById('globalClear').value == "1") 
	{
		document.getElementById("CodeLocation").value = "";// not found clear user value
	}
	return; //alert("No match, RegArea was not found !"); 	
}
// if coming from an AJAX call, let's use the CityId as the value	
if( !!li.extra ) 
	{
		var sValue = li.extra[0]; 	
	}
// otherwise, let's just display the value in the text box	
else 
	{	var sValue = li.selectValue; 	
		
	}
	//alert("findRegArea: " + sValue);
	
}

function selectCode(li) {	findCode(li); }
//////////////////////////////////////////////////////////////////////////////////


$(document).ready(function() {
		
	LoadCode();
	
	LoadCities();
	
	LoadRegArea('');
});


///////////////////////////////////////////////////////////////////////////////////////
// getCode.asp
function LoadCode()
{
	$.get("/JS_AUTOCOM/getCode.asp", function(data){
  		//alert("Data Loaded: " + data);
   		$("#CodeLocation").autocompleteArray(		data.split(","),		{
													   delay:10,
													   minChars:1,
													   matchSubset:1,
													   cacheLength:1,
													   onItemSelect:selectCode,
													   onFindValue:findCode,
													   autoFill:false,
													   selectFirst:true
													   }	//maxItemsToShow:100  
		);
		document.searchForm.CodeLocation.style.display = "block";
		document.getElementById('CodeLoad').style.display = "none";
	});
}

///////////////////////////////////////////////////////////////////////////////////////
// getCities.asp
function LoadCities()
{
	$.get("/JS_AUTOCOM/getCities.asp", function(data){
  		//alert("Data Loaded: " + data);
   		$("#CityLocation").autocompleteArray(		data.split(","),		{
													   delay:10,
													   minChars:1,
													   matchSubset:1,
													   cacheLength:1,
													   onItemSelect:selectCity,
													   onFindValue:findCity,
													   autoFill:false,
													   selectFirst:true
													   }	//maxItemsToShow:100  
		);
		document.searchForm.CityLocation.style.display = "block";
		document.getElementById('CityLoad').style.display = "none";
	});
}
///////////////////////////////////////////////////////////////////////////////////////
// will be also called on findCity()
function LoadRegArea(city_)
{
	//if (city_!="")
	//{
		// prevent caching ! caching - cuases duplication values and bugs !
		var oSuggest = $("#RegAreaLocation")[0].autocompleter; 
		if (oSuggest)
		{
			oSuggest.flushCache();
			document.searchForm.RegAreaLocation.value="";
		}
	//}
	
	document.searchForm.RegAreaLocation.style.display = "none";
	document.getElementById('RegAreaLoad').style.display = "block";
	$.get("/JS_AUTOCOM/getRegArea.asp?city="+city_, function(data){
  		//alert("Data Loaded: " + data);
   		$("#RegAreaLocation").autocompleteArray(		data.split(","),		{
													   delay:10,
													   minChars:1,
													   matchSubset:1,
													   cacheLength:1,
													   autoFill:false,
													   onItemSelect:selectRegArea,
													   onFindValue:findRegArea,
													   selectFirst:true
													   }	//maxItemsToShow:100  
		);
		document.searchForm.RegAreaLocation.style.display = "block";
		document.getElementById('RegAreaLoad').style.display = "none";
	});
}


//////////////////////////////////////////////////////////////////////////
function goSearchCode()
{
	
	//setTimeout(alert("ok"),1);
	
	document.getElementById('globalClear').value = "1";
	checkCode();
	
	setTimeout(function(){
						
		document.getElementById('globalClear').value = "0";
						
		code_  = document.searchForm.CodeLocation.value;
		
		if (code_!="")
		{
			if(document.getElementById('serviceSelector1').value !="0"){
				url_ = document.getElementById('serviceSelector1').value+"?code=" + code_ ;
			}else{
			url_ =  UR+"?code=" + code_ ;
			}
			document.location = url_;
			//alert(url_)
		}
		else
		{
			alert("Please select code value,\nfrom auto complete values")
		}
						},200);
}

//////////////////////////////////////////////////////////////////////////
function goSearch2()
{
	
	//setTimeout(alert("ok"),1);
	document.getElementById('globalClear').value = "1";
	checkCity();
	checkRegArea();
	
	setTimeout(function(){
						
			document.getElementById('globalClear').value = "0";
			
			city_  = document.searchForm.CityLocation.value;
			regArea_ = document.searchForm.RegAreaLocation.value;
			if (city_!="" || regArea_ !="")
			{//alert(document.getElementById('serviceSelector2').value);
				
				url_ = document.getElementById('serviceSelector2').value+"?city=" + city_ + "&regArea=" + regArea_;	
				document.location = url_;
				
				
				
				
			}
			else
			{
				alert("Please select City OR Region/Area values,\nfrom auto complete values")
			}
				},200);
}

/////////////////////////////////////////////////////////////////////////
function checkCity() // enter "" if not in list
{
	//alert('checkCity');
	$("#CityLocation")[0].autocompleter.findValue();
	// becuase findValue() is an event use this setTimeout to wait for sure for
	//document.getElementById("CityLocation").value
	
	// if user deleted value or chose no mach value - load again regions...
	if (document.getElementById('globalClear').value == "0") // if not search clicked
	{
		setTimeout(function(){ if (document.searchForm.CityLocation.value == "")
		{
			// prevent caching ! caching - cuases duplication values and bugs !
			$("#RegAreaLocation")[0].autocompleter.flushCache();
			LoadRegArea(''); // TO LOAD ALL REGION AGAIN IF USER HAS DELETED THE WANTED CITY AND WANTS TO GET ALL REGIONS & AREAS
		}},200);
	}
			
}
/////////////////////////////////////////////////////////////////////////
function checkRegArea() // enter "" if not in list
{
	//alert('checkRegArea'); 
	$("#RegAreaLocation")[0].autocompleter.findValue();	
		
}
/////////////////////////////////////////////////////////////////////////
function checkCode() // enter "" if not in list
{
	//alert('checkCity');
	$("#CodeLocation")[0].autocompleter.findValue();	
			
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
