document.onkeypress=enterkeypressed;
				
function popup(mylink, windowname)
{
	if (! window.focus)return true;
		var href;
	if (typeof(mylink) == 'string')
		href=mylink;
	else
		href=mylink.href;
		window.open(href, windowname, 'width=400,height=400,scrollbars=yes');
		return false;
}
		

//Determine browser
function browsertype(){
//function browsertype(e){
		if (navigator.appName.toUpperCase().match(/MICROSOFT INTERNET EXPLORER/) != null)
			//ieLastKeyUp(e);
			return("IE");
		if (navigator.appName.toUpperCase().match(/NETSCAPE/) != null)
		{
			//document.onkeypress=otherLastKeyUp;
			return("NETSCAPE");
		}
	}
	
//	On search field change set hidden field value to form id
function fieldvaluechanged(form) {
				switch(browsertype())
				{
				case "IE":
				document.getElementById("LastFormUpdated").value = form;
				break;
				
				case "NETSCAPE":
				//document.forms.all.item('LastFormUpdated').value = form;
				document.getElementById("LastFormUpdated").value = form; 
				break;
				}			
		}
		
function stvaluechg(field) {
				
			switch(browsertype())
				{
				case "IE":
				SearchNameForm
				document.getElementById("LastFormUpdated").value = "SearchNameForm";
				document.forms['SearchNameForm'].st.value = field.value;
				document.forms['SearchNameForm'].st_nm.value = field.options[field.selectedIndex].text;
				break;
				
				case "NETSCAPE":
				document.getElementById("LastFormUpdated").value = "SearchNameForm";
				document.forms['SearchNameForm'].st.value = field.value;
				document.forms['SearchNameForm'].st_nm.value = field.value;
				break;
				}			
		}
		
		
function enterkeypressed(e)
{
	switch(browsertype())
			{
				
				case "IE":
				var ieKeyid = window.event;
				
				if(ieKeyid.keyCode == 13)
				{
					
					var j = document.forms['SearchNameForm'];
					var name = j['nm'];
					
					var k = document.forms['SearchZipForm'];
					var zip = k['zip'];
					
					var l = document.forms['searchstatecity'];
					var st_nm = l['st_nm'];
					
										
					if (name.value.length < 1 && zip.value.length < 1 && st_nm.value.length < 1)
					{
						
						MissingSearchCriteria();
						document.getElementById("namediv").style.backgroundColor="#75a3ee";
						name.focus();
						window.event.keycode = 0;
						return false;
					}
					else if (document.getElementById("LastFormUpdated").value == "SearchNameForm")
					{
						var f = document.forms['SearchNameForm'];
						var name = f['nm'];
						if(name.value.length<3)
						{
							HideErrorMessages();
							//document.getElementById("NameError2").style.visibility='visible';
							document.getElementById("NameError2").style.display='inline';
							name.focus();
							return false;
						}
						else
						{
							f.submit();
						}
					}
					else if (document.getElementById("LastFormUpdated").value == "SearchZipForm")
					{
						
						if (!isNumeric(zip.value) | zip.value.length < 5)
						{
							
							HideErrorMessages();
							document.getElementById("ZipCodeError").style.visibility='visible';
							document.getElementById("ZipCodeError").style.display='inline';
							document.getElementById("zipdiv").style.backgroundColor="#75a3ee";
							zip.focus();
							ieKeyid.keyCode = 0;
							return false;
						}
						else
						{
							submitZipForm(document.getElementById("Select1").value);
							//var i = document.forms[document.getElementById("LastFormUpdated").value];
							//i['dist'].value = document.getElementById("Select1").value;
							//i.submit();
							return false;
						}
					}
					else
					{
						var i = document.forms[document.getElementById("LastFormUpdated").value];
						i.submit();
						return false;
					}
				}
					
					break;
	
				
				case "NETSCAPE":
				var otherkeyid = e.keyCode;
				if (otherkeyid == 13)
        		{
					
					var j = document.forms['SearchNameForm'];
					var name = j['nm'];
					
					var k = document.forms['SearchZipForm'];
					var zip = k['zip'];
					
					var l = document.forms['searchstatecity'];
					var st_nm = l['st'];
										
					if (name.value.length < 1 && zip.value.length < 1 && st_nm.value.length < 1)
					{
						MissingSearchCriteria();
						document.getElementById("namediv").style.backgroundColor="#75a3ee";
						name.focus();
						otherkeyid.keycode = 0;
						return false;
					}					
					else if (document.getElementById("LastFormUpdated").value == "SearchNameForm")
					{
						var f = document.forms['SearchNameForm'];
						var name = f['nm'];
						if(name.value.length<3)
						{
							HideErrorMessages();
							document.getElementById("NameError2").style.visibility='visible';
							document.getElementById("NameError2").style.display='inline';
							name.focus();
							return false;
						}
						else
						{
							f.submit();
						}
					}				
					else if (document.getElementById("LastFormUpdated").value == "SearchZipForm")
					{
						if (!isNumeric(zip.value) | zip.value.length < 5)
						{
							HideErrorMessages();
							document.getElementById("ZipCodeError").style.visibility='visible';
							document.getElementById("ZipCodeError").style.display='inline';
							document.getElementById("zipdiv").style.backgroundColor="#75a3ee";
							zip.focus();
							otherkeyid.keycode = 0;
							return false;
						}
						else
						{
							var i = document.forms[document.getElementById("LastFormUpdated").value];
							i.submit();
							return false;
						}
					}
				else
					{
						var i = document.forms[document.getElementById("LastFormUpdated").value];
						i.submit();
						return false;
					}
					
				}
								
				
			}	

}


function distancechanged()
{
		document.getElementById("dist").value = document.getElementById("Select1").value;
}
	

function SetFocus()
	{
	
	//used to populate state list in the event back button on broser is used
	var st_frm = document.forms['searchstatecity'];
	var stid = st_frm['st'].value;
	if (stid != "-1")
	{
		//ddstatelist2.options[ddstatelist2.selectedIndex].value = stid;
		SearchQCD1.getCitiesByStateToArray(stid,LoadCities_CallBack);
		var CityList = document.getElementById("ddcitylist");
		CityList.options[0] = new Option("                              ","-1");
		CityList.options[0].selected = true;
	}		

	// used to set width of dropdown list for state/city selection on pageload
	var CityList = document.getElementById("ddcitylist");
	var qstr = window.location.search;
	CityList.options[0] = new Option("                              ","-1");
	CityList.options[0].selected = true;
	if (qstr.indexOf("?nm=") > -1)
	{	
		
		//document.getElementById("initSearchHeader1").style.display='none';
		//document.getElementById("initSearchHeader2").style.display='none';
		document.getElementById("namediv").style.backgroundColor="#75a3ee";
		document.SearchNameForm.nm.focus();
		return;
	}
		
	if (qstr.indexOf("zip") > -1)
	{
		
		//document.getElementById("initSearchHeader1").style.display='none';
		//document.getElementById("initSearchHeader2").style.display='none';
		document.getElementById("zipdiv").style.backgroundColor="#75a3ee";
		document.SearchZipForm.zip.focus();
		return;
	}
		
	//Check querystring for State search and set focus
	if (qstr.indexOf("st_cd") > -1)
	{
				
		document.getElementById("statediv").style.backgroundColor="#75a3ee";
				
		//document.getElementById("initSearchHeader1").style.display='none';
		//document.getElementById("initSearchHeader2").style.display='none';
		var pairs = qstr.split("&");
	
			for (i=0;i<pairs.length-1;i++)
			{
				var pos = pairs[i].indexOf('=');
				if (pos >= 0)
				{
					var argname = pairs[i].substring(0,pos);
					var value = pairs[i].substring(pos+1);
					
					switch(argname)
					{
						case "st_cd":
						document.getElementById("st_cd").value = value;
						break;
				
						case "st":
						var st_frm = document.forms['searchstatecity'];
						st_frm['st'].value = value;
						SearchQCD1.getCitiesByStateToArray(value,LoadCities_CallBack);
						var CityList = document.getElementById("ddcitylist");
						CityList.options[0] = new Option("                              ","-1");
						CityList.options[0].selected = true;
						break;
				
						case "st_nm":
						var st_frm = document.forms['searchstatecity'];
						st_frm['st_nm'].value = value;
						break;
						
						case "cty_id":
						document.getElementById("cty_id").value = "-1";
						break;
						
						case "cty_nm":
						document.getElementById("cty_nm").value = "";
						break;
					}
				}
				
			}
						
		return;
	}	
	
document.getElementById('nm').focus();
	
}

function HideErrorMessages()
{
		if (document.getElementById("NameError"))
		{
			document.getElementById("NameError").style.display='none';
		}
		
		if (document.getElementById("NameError2"))
		{
			document.getElementById("NameError2").style.display='none';
		}
		
		if (document.getElementById("ZipCodeErrorPanel"))
		{
			document.getElementById("ZipCodeErrorPanel").style.display='none';
		}
		
		if (document.getElementById("ZipCodeError"))
		{
			document.getElementById("ZipCodeError").style.display='none';
		}
		
		if (document.getElementById("StateCityError"))
		{
			document.getElementById("StateCityError").style.display='none';
		}
		
		if (document.getElementById("ErrorPanel"))
		{
			document.getElementById("ErrorPanel").style.display='none';
		}
		
		if (document.getElementById("SearchCriteriaMissingErrorMsg"))
		{
			document.getElementById("SearchCriteriaMissingErrorMsg").style.display='none';
		}
}
	//Display missing search citeria message
	function MissingSearchCriteria(){
									
										
		if (document.getElementById("NameError"))
		{
			document.getElementById("NameError").style.display='none';
		}
		
		if (document.getElementById("ZipCodeErrorPanel"))
		{
			document.getElementById("ZipCodeErrorPanel").style.display='none';
		}
		
		if (document.getElementById("ZipCodeError"))
		{
			document.getElementById("ZipCodeError").style.display='none';
		}
		
		if (document.getElementById("StateCityError"))
		{
			document.getElementById("StateCityError").style.display='none';
		}
		
		if (document.getElementById("ErrorPanel"))
		{
			document.getElementById("ErrorPanel").style.display='none';
		}

		document.getElementById("SearchCriteriaMissingErrorMsg").style.visibility='visible';
		document.getElementById("SearchCriteriaMissingErrorMsg").style.display='inline';
	}
	
	//Change background color on Focus
	function focus1(s){
	
	switch(s)
				{
				case "namediv":
				document.getElementById("namediv").style.backgroundColor="#75a3ee";
				document.getElementById("zipdiv").style.backgroundColor="#dde6fd";
				document.getElementById("statediv").style.backgroundColor="#dde6fd";
				document.getElementById('SrchOption1Img').src = "images/btn_search_darker1.gif";
				document.getElementById('SrchOption2Img').src = "images/btn_search.gif";
				document.getElementById('SrchOption3Img').src = "images/btn_search.gif";
				break;
				
				case "zipdiv":
				document.getElementById("namediv").style.backgroundColor="#dde6fd";
				document.getElementById("zipdiv").style.backgroundColor="#75a3ee";
				document.getElementById("statediv").style.backgroundColor="#dde6fd";
				document.getElementById('SrchOption1Img').src = "images/btn_search.gif";
				document.getElementById('SrchOption2Img').src = "images/btn_search_darker1.gif";
				document.getElementById('SrchOption3Img').src = "images/btn_search.gif";
				break;
				
				case "statediv":
				document.getElementById("namediv").style.backgroundColor="#dde6fd";
				document.getElementById("zipdiv").style.backgroundColor="#dde6fd";
				document.getElementById("statediv").style.backgroundColor="#75a3ee";
				document.getElementById('SrchOption1Img').src = "images/btn_search.gif";
				document.getElementById('SrchOption2Img').src = "images/btn_search.gif";
				document.getElementById('SrchOption3Img').src = "images/btn_search_darker1.gif";
				break;
				}	
		}
	//Change background color on Blur
	function blur1(s){
			document.getElementById(s).style.backgroundColor="#dde6fd";
			switch(s)
				{
				case "namediv":
				document.getElementById('SrchOption1Img').src = "images/btn_search.gif";
				break;
				
				case "zipdiv":
				document.getElementById('SrchOption2Img').src = "images/btn_search.gif";
				break;
				
				case "statediv":
				document.getElementById('SrchOption3Img').src = "images/btn_search.gif";
				break;
				}
	}
	
	
	//Submit Name search form
	function submitNameForm(){
		var f = document.forms['SearchNameForm'];
		var name = f['nm'];
		if(name.value.length<1)
		{
			HideErrorMessages();
			MissingSearchCriteria()
			name.focus();
		}
		else if(name.value.length<3)
		{
			HideErrorMessages();
			//document.getElementById("NameError2").style.visibility='visible';
			document.getElementById("NameError2").style.display='inline';
			name.focus;
			return false;
		}
		else
		{
			f.submit();
		}
	}
	
				
	//Submit zip code form
	function submitZipForm(s){
		
		var f = document.forms['SearchZipForm'];
		var zip = f['zip'];
		if (zip.value.length<1)
		{
		HideErrorMessages();
		MissingSearchCriteria();
		zip.focus();
		
		}
		else if(zip.value.length<5 || !isNumeric(zip.value) || zip.value.length>5)
		{
			if (document.getElementById("SearchCriteriaMissingErrorMsg"))
			{
				document.getElementById("SearchCriteriaMissingErrorMsg").style.display='none';
			}
			HideErrorMessages();
			document.getElementById("ZipCodeError").style.visibility='visible';
			document.getElementById("ZipCodeError").style.display='inline';
			zip.focus();
			
		}
		else
		{
			f['dist'].value = document.getElementById("Select1").value;
			f.submit();
		}
	}
				
	
	// Check to see if zip code numeric value
	function isNumeric(s)
	{
		var ValidChars = "0123456789";
		var IsNumber=true;
		var Char;

		for (i = 0; i < s.length && IsNumber == true; i++) 
			{ 
			Char = s.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
				{
				IsNumber = false;
				}
			}
		return IsNumber;
	}
		
		   		        
	
	//Submit searcb by state and city
	function submitStateCityForm(s){
		var st_frm = document.forms['searchstatecity'];
		var name = st_frm['st_nm'];
		if (name.value.length < 1)
		{
		HideErrorMessages();
		MissingSearchCriteria();
		name.focus;
		}
		else
		{
		st_frm.submit();	
		}
					
	}
	
	function chgStateCityForm(field){
		  	
	switch(browsertype())
				{
				case "IE":
				document.forms['searchstatecity'].cty_nm.value = field.options[field.selectedIndex].text;
				//alert(field.options[field.selectedIndex].value);
				//alert(field.options[field.selectedIndex].text);
				document.forms['searchstatecity'].cty_id.value = field.options[field.selectedIndex].value;
				document.getElementById("LastFormUpdated").value = "searchstatecity";
				break;
				
				case "NETSCAPE":
				document.forms['searchstatecity'].cty_nm.value = field.options[field.selectedIndex].text;
				document.forms['searchstatecity'].cty_id.value = field.options[field.selectedIndex].value;
				document.getElementById("LastFormUpdated").value = "searchstatecity";
				break;
				}		
		
	}
	
	//AJAX function to load cities from search dropdown list
		    
	function LoadCities(ddstatelist2){

		fieldvaluechanged("searchstatecity");
		document.getElementById("LastFormUpdated").value = "searchstatecity";
		var st_frm = document.forms['searchstatecity'];
		st_frm['st'].value  = ddstatelist2.options[ddstatelist2.selectedIndex].value;
		st_frm['st_nm'].value  = ddstatelist2.options[ddstatelist2.selectedIndex].text;
		//SearchQCR1.getCitiesByStateToArray(st_frm['st'].value,LoadCities_CallBack);
		var CityList = document.getElementById("ddcitylist");
		CityList.options[0].text = "retrieving cities...";
		CityList.options[0].selected = true;

		SearchQCR1.getCitiesSelectList(st_frm['st'].value,LoadCities_CallBack);

	}
	
	//AJAX function 
	function LoadCities_CallBack(response){
	if (response.error != null)
	{
		alert(response.error);
		return;
	}
		var cities = response.value;
		var CityDIV = document.getElementById("CityDIV");
		CityDIV.innerHTML = cities;
	}
	
	function RTrim(VALUE)
	{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
		if(v_length < 0)
		{
			return "";
		}
		var iTemp = v_length -1;
		while(iTemp > -1){
			if(VALUE.charAt(iTemp) == w_space)
			{
			}
			else
			{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
			}
		iTemp = iTemp-1;

		} //End While
	return strTemp;

	} //End Function
