//----------------------------------------------------------------------------------------
//var xmlHttp = null;
//var isBusy ;

/// This function has been used to display/ hide the zip, city and state search criteria
function DisplaySearchCriteriaDiv(bDisplay)
{
	if (bDisplay){
		doLinks.DeactivateLinks(); 
		initializeSearchOprions();
		divSearchCriteria.style.display = "inline";
		txtZipcode.focus();
	}else{
		divSearchCriteria.style.display = "none";
		doLinks.ActivateLinks(); 
		//xmlHttp.abort(); 
	}
}

function ShowHideLoading(bVisible, message){
	txtLoadingMessage.innerHTML  = message;
	if (bVisible){
		errorMsg.style.display = "none";
		loadImg.style.display = "inline";
	}else{
		loadImg.style.display = "none";
	}
	
}

//----------------------------------------------------------------------------------------
function WRUCodeKeyPress(event)
{
	if (event.keyCode==13 && btnSearch.disabled==false)
		{
			SearchServices(txtSelectedCountryId.value, txtSelectedZipId.value);
		}
	
	if (event.keyCode==27)
		{
			DisplaySearchCriteriaDiv(false);
		}
}

//----------------------------------------------------------------------------------------

// Initialize search option page
function initializeSearchOprions()
{
	enableSearch(false);
	populateSateCodes();
	ResetStateCityInfo();
	ResetZipCodeInfo();
	txtErrorMessage.style.visibility = "hidden";
	SetError("");
	
	//ToDo: popup near the Country link with an transparant arrow
	divSearchCriteria.style.top = (document.body.clientHeight - 100)/ 2;
	divSearchCriteria.style.left = (document.body.clientWidth - 450 )/ 2;
}

//----------------------------------------------------------------------------------------

function ResetStateCityInfo()
{
	cboStates.value = "";
	clearDropDown(cboCities);
}

//----------------------------------------------------------------------------------------

function ResetZipCodeInfo()
{
	txtZipcode.value = "";
}

//----------------------------------------------------------------------------------------

function SetError(errorString)
{
	txtErrorMessage.innerHTML  = errorString;
	if (errorString.length > 0){
		errorMsg.style.display = "inline";
	}else{
		errorMsg.style.display = "none";
	}
	
}

//----------------------------------------------------------------------------------------

/// This function has been used to display the services of the selected country
/// If the country has zipCodes or States available, this method will show the Search Criteria Entering Page
function SearchForServices(nCountryId, bHasZipcodes, bHasStateCodes)
{
	txtSelectedCountryId.value = nCountryId;
	if ((bHasZipcodes > 0)|| (bHasStateCodes > 0))
	{
		DisplaySearchCriteriaDiv(true);
	}else{
		SearchServices(	nCountryId, -1);
	}
}
	
//----------------------------------------------------------------------------------------	
	
// follwing function has been used to endable disable the links of the page
var doLinks={DeactivateLinks:function(){return;}, ActivateLinks:function(){return;}};
	
if(document.createElement)
doLinks=
{
	coverDiv:document.createElement('div'),
	DeactivateLinks:function()
	{
		var coverDivStyleObj=this.coverDiv.style;
		if(coverDivStyleObj.position!='absolute')
		{
			coverDivStyleObj.position			= 'absolute';
			coverDivStyleObj.top				= coverDivStyleObj.left=0;
			coverDivStyleObj.zIndex				= 9;
			coverDivStyleObj.backgroundColor	= 'gray';
					
			if(typeof coverDivStyleObj.opacity=='string')
				coverDivStyleObj.opacity		= '0.01';
			else if(typeof coverDivStyleObj.MozOpacity=='string')
				coverDivStyleObj.MozOpacity		='0.01';
			else if(typeof coverDivStyleObj.KhtmlOpacity=='string')
				coverDivStyleObj.KhtmlOpacity	='0.01';
			else if (document.documentElement.filters)
			{
				coverDivStyleObj.filter			='alpha(opacity=1)';
				coverDivStyleObj.backgroundColor='transparent';
			}
		else
			coverDivStyleObj.backgroundColor	= 'transparent';
			coverDivStyleObj.height				= Math.max(document.body.offsetHeight, document.documentElement.offsetHeight)+'px';
			coverDivStyleObj.width				= Math.max(document.body.offsetWidth, document.documentElement.offsetWidth)+'px';
		}
				
		document.body.appendChild(this.coverDiv);
			
		if (document.documentElement.filters&&coverDivStyleObj.backgroundColor!='gray')
			coverDivStyleObj.backgroundColor='gray';
	},
	ActivateLinks:function()
	{
		document.body.removeChild(this.coverDiv);
	}
};

//----------------------------------------------------------------------------------------	
	
function enableSearch(lEnable)
{
	btnSearch.disabled = !lEnable;
}

//----------------------------------------------------------------------------------------	

// this has been done to fis the fast key press issue
var count = 0;	
function ValidateZipcodeOnkeyUp(str)
{   count = count+1;  
	setTimeout("ValidateZipcodeOnTimer("+count+",'" + str + "')",250);
}


function ValidateZipcodeOnTimer(currCount, str){
   if(currCount == count)   {
         count = 0;      
         ValidateZipcode(str);   
   }
 } 

function ValidateZipcode(str)
{
	ResetStateCityInfo();
	enableSearch(false);
	
	if (str.length < 5)
	{
		SetError("");
		txtErrorMessage.style.visibility = "hidden";
		return ; 
	}
	
	ShowHideLoading(true, txtValidateZipMessage.value);
	var url= "/common/servicelocator/validateZip.asp?CountryId=" + txtSelectedCountryId.value + "&ZipCode=" + str  ;
	new Ajax.Request(url, {     
		method:'get',     
		onSuccess: function(transport){       
				var response = transport.responseText || txtinvalidZipErrorMessage.value;       
				var validationString = "svcLocatorValidZipCode:";
				var validZipCodes = "svcLocatorValidZipCodes:";
				var inValidZipCode = "svcLocatorInValidZipCode:";

				// Check whether the ZipCode is valid
				if (response.indexOf(validationString) > -1){
					// remove the Meta Data info http://support.microsoft.com/kb/297995
					var responseText = response.substring(response.indexOf(validationString) + validationString.length);
					SetError("");
					txtSelectedZipId.value = responseText;
					enableSearch(true);
				} 
				else if (response.indexOf(validZipCodes) > -1)
				{
					SetError("");
					enableSearch(false);
				}else if (response.indexOf(inValidZipCode) > -1){
					// This value has been taken from the property file and set at the server side to a variable
					SetError(txtinvalidZipErrorMessage.value); 
					enableSearch(false);
				}else{
					SetError(txtinvalidZipErrorMessage.value); 
					enableSearch(false);
				}
				txtErrorMessage.style.visibility = "";
				ShowHideLoading(false, "");
		},     
		onFailure: function(){ 
			ShowHideLoading(false, "");
			SetError(""); 
			enableSearch(false);
		}   
	}); 
}

//----------------------------------------------------------------------------------------	

/*
function ValidateZipcodeOld(str)
{
	ResetStateCityInfo();
	enableSearch(false);
	
	if (str.length < 5)
	{
		SetError("");
		txtErrorMessage.style.display = "hidden";
		return ; 
	}
		
	if (str.length >=5){
		txtErrorMessage.style.display = "visible";
		
		var url= "/common/servicelocator/validateZip.asp?CountryId=" + txtSelectedCountryId.value + "&ZipCode=" + str  ;
		var validationString = "svcLocatorValidZipCode:";
		var validZipCodes = "svcLocatorValidZipCodes:";
		var inValidZipCode = "svcLocatorInValidZipCode:";
		xmlHttp = getHttpObject();
		
		xmlHttp.open("GET",url,true);
		isBusy = true;
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				SetError(xmlHttp.responseText);
				// Check whether the ZipCode is valid
				if (xmlHttp.responseText.indexOf(validationString) > -1){
					// remove the Meta Data info http://support.microsoft.com/kb/297995
					var responseText = xmlHttp.responseText.substring(xmlHttp.responseText.indexOf(validationString) + validationString.length);
					//SetError("");
					txtSelectedZipId.value = responseText;
					enableSearch(true);
				} 
				else if (xmlHttp.responseText.indexOf(validZipCodes) > -1)
				{
					//SetError("");
					enableSearch(false);
				}else if (xmlHttp.responseText.indexOf(inValidZipCode) > -1){
					// This value has been taken from the property file and set at the server side to a variable
					SetError(txtinvalidZipErrorMessage.value); 
					enableSearch(false);
				}else{
					SetError(txtinvalidZipErrorMessage.value); 
					enableSearch(false);
				}
				isBusy = false;
			}
		}
		xmlHttp.send(null);
		
	}  
}
*/

//----------------------------------------------------------------------------------------

/*
function getHttpObject()
{
	if (xmlHttp != null)
	{
		// Stop the current request;
		if (isBusy){
			xmlHttp.abort(); 
			xmlHttp.onreadystatechange = function () {}
		}
		
		return xmlHttp;
	}
	
	try
	{  
		//Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
		return xmlHttp; 
	}
	catch (e)
	{  
		// Internet Explorer  
		try
		{    
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");  
			return xmlHttp;  
		}
		catch (e)
		{    
			try
			{      
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
				return xmlHttp;     
			}
			catch (e)
			{		
				alert("Your browser does not support AJAX!");      
				return false;      
			}    
		}  
	}
}
*/
  
//----------------------------------------------------------------------------------------	
		
function clearDropDown(objDropDown){
     /*
     for (var i = (objDropDown.options.length -1); i >= 0; i--){
         objDropDown.options[i]=null;
     }
     */
     objDropDown.innerHTML="";
}

//----------------------------------------------------------------------------------------	


function populateSateCodes()
{
	var url= "/common/servicelocator/GetStateCodes.asp?CountryId=" + txtSelectedCountryId.value ;
	
	new Ajax.Request(url, 
	{     
		method:'get',     
		onSuccess: function(transport)
		{ 
			var validationString = "svcLocatorValidStates:";
			if (transport.responseText.indexOf(validationString) > -1){
					
				// remove the Meta Data info http://support.microsoft.com/kb/297995
				var responseText = transport.responseText.substring(transport.responseText.indexOf(validationString) + validationString.length  );

				// This is to Fix the IE Error
				if(document.all)
				{
					responseText = "<option>truncatethis</option>" + responseText;
				}
				
				cboStates.innerHTML=responseText;
				cboStates.outerHTML=cboStates.outerHTML;

			}else{
				cboStates.innerHTML="";
			}
		},     
		onFailure: function(){ 
			cboStates.innerHTML="";
		}   
	}); 
}

/*
// populate the StateCodea
function populateSateCodesOld()
{
	xmlHttp = getHttpObject();
	var url= "/common/servicelocator/GetStateCodes.asp?CountryId=" + txtSelectedCountryId.value ;
	var validationString = "svcLocatorValidStates:";
	
	xmlHttp.open("GET",url,true);
	isBusy = true;
	
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			if (xmlHttp.responseText.indexOf(validationString) > -1){
					
				// remove the Meta Data info http://support.microsoft.com/kb/297995
				var responseText = xmlHttp.responseText.substring(xmlHttp.responseText.indexOf(validationString) + validationString.length  );

				// This is to Fix the IE Error
				if(document.all)
				{
					responseText = "<option>truncatethis</option>" + responseText;
				}

				
				cboStates.innerHTML=responseText;
				cboStates.outerHTML=cboStates.outerHTML;

			}else{
				cboStates.innerHTML="";
			}
		}
	}
	xmlHttp.send(null);
}
*/
//----------------------------------------------------------------------------------------	

// Populate the Cities dropdown via Ajax
function populateCities(state)
{
	if (state == ""){
		clearDropDown(cboCities);
	}else{
		cboCities.disabled = true;
		ResetZipCodeInfo();
		SetError("");
		enableSearch(false);
		ShowHideLoading(true,txtLoadingCitiesMessage.value );
		
		var url= "/common/servicelocator/getCities.asp?CountryId=" + txtSelectedCountryId.value + "&stateCode="+ state ;
		new Ajax.Request(url, 
		{     
			method:'get',     
			onSuccess: function(transport)
			{ 
				var validationString = "svcLocatorValidCities:";
				if (transport.responseText.indexOf(validationString) > -1){
					
					// remove the Meta Data info http://support.microsoft.com/kb/297995
					var responseText = transport.responseText.substring(transport.responseText.indexOf(validationString) + validationString.length );
				
			
					// This is to Fix the IE Error
					if(document.all)
					{
						responseText = "<option>truncatethis</option>" + responseText;
					}

					cboCities.innerHTML=responseText;
					cboCities.outerHTML=cboCities.outerHTML;
					cboCities.disabled = false;
					
				}else{
					cboCities.innerHTML="";
					cboCities.disabled = false;
				}
				ShowHideLoading(false, "");
			},     
			onFailure: function()
			{ 
			cboCities.innerHTML="";
			cboCities.disabled = false;
			ShowHideLoading(false, "");
			}   
		}); 
	}
}

/*		
// Populate the Cities dropdown via Ajax
function populateCitiesOld(state)
{
	if (state == ""){
		clearDropDown(cboCities);
	}else{
		cboCities.disabled = true;
		ResetZipCodeInfo();
		SetError("");
		enableSearch(false);
	
		xmlHttp = getHttpObject();
		var url= "/common/servicelocator/getCities.asp?CountryId=" + txtSelectedCountryId.value + "&stateCode="+ state ;
		var validationString = "svcLocatorValidCities:";
		
		xmlHttp.open("GET",url,true);
		isBusy = true;
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				
				if (xmlHttp.responseText.indexOf(validationString) > -1){
					
					// remove the Meta Data info http://support.microsoft.com/kb/297995
					var responseText = xmlHttp.responseText.substring(xmlHttp.responseText.indexOf(validationString) + validationString.length );
				
			
					// This is to Fix the IE Error
					if(document.all)
					{
						responseText = "<option>truncatethis</option>" + responseText;
					}

					cboCities.innerHTML=responseText;
					cboCities.outerHTML=cboCities.outerHTML;
					cboCities.disabled = false;
					
				}else{
					cboCities.innerHTML="";
					cboCities.disabled = false;
				}
			}
		}
	
		xmlHttp.send(null);
	}
}
*/
//----------------------------------------------------------------------------------------	
		
//Search result page	
function SearchServices(SelectedCountryId, zipId){

	var url = "";
	if 	(zipId == ""){
		GetValidZipIdForCity();	
	}else{
	
		url= "services.asp?CountryId=" + SelectedCountryId + "&zip_Id=" + zipId + "&ctry=" + txtsvcLocCtry.value + "&lang=" + txtsvcLocLang.value + "&site=" + txtsvcLocSite.value
		location.href = url;  
	}
}
	
//----------------------------------------------------------------------------------------	
	
//Endbles the search when the City is Selected
function CitySelected(SelectedCity){
	if (SelectedCity.length > 0){
		//txtSelectedZipId.value = SelectedCity
		enableSearch(true);
	}else{
		enableSearch(false);
	}
}
		
//----------------------------------------------------------------------------------------	

function GetValidZipIdForCity()
{
	var url= "/common/servicelocator/getzipidforcity.asp?CountryId=" + txtSelectedCountryId.value + "&stateCode=" + cboStates.value + "&city=" + cboCities.value ;
	new Ajax.Request(url, 
	{     
		method:'get',     
		onSuccess: function(transport)
		{ 
			var validationString = "svcLocatorValidZipID:";
			
			if (transport.responseText.indexOf(validationString) > -1){
					
				// remove the Meta Data info http://support.microsoft.com/kb/297995
				var responseText = transport.responseText.substring(transport.responseText.indexOf(validationString) + validationString.length );
				
					
				// Check whether the ZipCode is valid
				if (transport.responseText.indexOf(validationString) > -1){
					// remove the Meta Data info http://support.microsoft.com/kb/297995
					var responseText = transport.responseText.substring(transport.responseText.indexOf(validationString) + validationString.length);
					txtSelectedZipId.value = responseText;
						
							
					var url2= "services.asp?CountryId=" + txtSelectedCountryId.value + "&zip_Id=" + responseText + "&site=" + txtsvcLocSite.value;
					location.href = url2;  
					return;
					
				}
			}
		},     
		onFailure: function()
		{ 
		}   
	}); 
}


// Populate the Cities dropdown via Ajax
/*
function GetValidZipIdForCity()
{
		//var xmlHttp;
		xmlHttp = getHttpObject();
		var url= "/common/servicelocator/getzipidforcity.asp?CountryId=" + txtSelectedCountryId.value + "&stateCode=" + cboStates.value + "&city=" + cboCities.value ;
		var validationString = "svcLocatorValidZipID:";
		
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
			
				if (xmlHttp.responseText.indexOf(validationString) > -1){
					
					// remove the Meta Data info http://support.microsoft.com/kb/297995
					var responseText = xmlHttp.responseText.substring(xmlHttp.responseText.indexOf(validationString) + validationString.length );
				
					
					// Check whether the ZipCode is valid
					if (xmlHttp.responseText.indexOf(validationString) > -1){
						// remove the Meta Data info http://support.microsoft.com/kb/297995
						var responseText = xmlHttp.responseText.substring(xmlHttp.responseText.indexOf(validationString) + validationString.length);
						txtSelectedZipId.value = responseText;
						
							
						var url2= "services.asp?CountryId=" + txtSelectedCountryId.value + "&zip_Id=" + responseText;
						location.href = url2;  
						return;
					
					}
				}
			}
		}
	
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
*/

//----------------------------------------------------------------------------------------	

function $(v) { return(document.getElementById(v)); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function xy(e,v) { return(v?(agent('msie')?event.clientY+document.body.scrollTop:e.pageY):(agent('msie')?event.clientX+document.body.scrollTop:e.pageX)); }

function dragOBJ(d,e) {
    function drag(e) { 
		if(!stop) {
			d.style.top=(tX=xy(e,1)+oY-eY+'px'); 
			d.style.left=(tY=xy(e)+oX-eX+'px'); 
		} 
	}

    var oX=parseInt(d.style.left),oY=parseInt(d.style.top),eX=xy(e),eY=xy(e,1),tX,tY,stop;
    document.onmousemove=drag; 
    
    document.onmouseup=function(){ 
		stop=1; 
		document.onmousemove=''; 
		document.onmouseup=''; 
		
	};
}

//----------------------------------------------------------------------------------------	

