	
	//=========================================================================
	// SmartLead - Zip Code Finder
	//=========================================================================

window.finderURL = 'finder.php';

function test_web_browser()
{
  if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
 	var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	 if (ffversion>=3)
	  return "FF3";
	 else if (ffversion>=2)
	  return "FF2";
	 else if (ffversion>=1)
	  return "FF1";
	}
	else
 	{
	 if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	 if (ieversion>=8)
	  return "IE8";
	 else if (ieversion>=7)
	  return "IE7";
	 else if (ieversion>=6)
	  return "IE6";
	 else if (ieversion>=5)
	  return "IE5";
	}
 }
}

function RequestZipCodesAPI(sRequestCode)
{
	var browser = test_web_browser();
	var bValidZipCode = (IsValidZipCode(window.document.getElementById('txt' + sRequestCode + 'ZipCode').value, false).ReturnCode == 0);
	var bValidCityState = (IsEmpty(window.document.getElementById('txt' + sRequestCode + 'City').value).ReturnCode == 0 && window.document.getElementById('cbo' + sRequestCode + 'State').selectedIndex > 0);
	
	
	
	if (bValidZipCode || bValidCityState) {			
	    if (bValidZipCode) 
	    {	        
			var url = window.finderURL + "?zip=" + window.document.getElementById('txt' + sRequestCode + 'ZipCode').value;	       			
			$.get(url, function(data) {	
				try
				{
					
					if (browser=="IE8" || browser=="IE7" || browser=="IE6" || browser=="IE5")
					{
						var city = $(data)[10].nodeValue;
						var state = $(data)[13].nodeValue
					}else if (browser=="FF2" || browser=="FF1")
					{
						var city =  $(data).children(0).prevObject[3].innerHTML;
						var state = $(data).children(0).prevObject[4].innerHTML;;										
					}
					else
					{
						var city =  $(data).find("City:eq(0)").text();										
						var state = $(data).find("State:eq(0)").text();										
					}
				}catch(err)
				{

				}
	            var hUserError;
				
	            //Clear the error messages and the marked fields.
	            ResetErrorDisplay();
	            if (city != "" && state != "") {
	               
				    document.getElementById('txt' + sRequestCode + 'City').value = city;										
	                document.getElementById('cbo' + sRequestCode + 'State').value = state;
	            }
	            else {
	                hUserError = new UserError('txt' + sRequestCode + 'ZipCode', 'The zip code you entered was not found.', sRequestCode == 'From' ? 3 : 4, 2);
	                hUserError.DisplayError();
	            }
	        }, "xml");
	    }
	    else {
	        // first we get the long/lat
	        var url = window.finderURL + "?city=" + window.document.getElementById('txt' + sRequestCode + 'City').value + "&state=" + window.document.getElementById('cbo' + sRequestCode + 'State').value;	        			
			$.get(url, function(data) {								
	            var zip = $(data).find("Zip:eq(0)").text();
				//var latitude = $(data).find("Latitude:eq(0)").text();
	            //var longitude = $(data).find("Longitude:eq(0)").text();
	            //var url = "finder.php?lat=" + latitude + "&long=" + longitude;
	            //$.get(url, function(data2) {
	             //   var zip = $(data2).find("PostalCodeNumber:eq(0)").text();
	                document.getElementById('txt' + sRequestCode + 'ZipCode').value = zip;
	            //}, "xml");
	        }, "xml");
	    }
	}
	else 
	{
		alert('In order to find a zip code enter a city name, select a state and then click the Find button.\nIf you want to validate your zip code enter it and click the find button.');
	}
}
	function RequestZipCodes(sRequestCode) 
	{
	var bValidZipCode = (IsValidZipCode(window.document.getElementById('txt' + sRequestCode + 'ZipCode').value, false).ReturnCode == 0);
	var bValidCityState = (IsEmpty(window.document.getElementById('txt' + sRequestCode + 'City').value).ReturnCode == 0 && window.document.getElementById('cbo' + sRequestCode + 'State').selectedIndex > 0);		
	var theform = 
		(document.getElementById('fraGetZipCode').document == null) ?
			document.getElementById('fraGetZipCode').contentDocument.getElementById('frmGetZipCode') :
			document.frames['fraGetZipCode'].document.getElementById('frmGetZipCode');

		if (bValidZipCode || bValidCityState) {
			theform.txtRequestCode.value = sRequestCode; 

			if (bValidZipCode) {
				theform.txtZipCode.value = window.document.getElementById('txt' + sRequestCode + 'ZipCode').value;
				theform.txtCity.value = '';
				theform.txtStateCode.value = '';
			}
			else {
				theform.txtZipCode.value = '';
				theform.txtCity.value = window.document.getElementById('txt' + sRequestCode + 'City').value;
				theform.txtStateCode.value = window.document.getElementById('cbo' + sRequestCode + 'State').value;
			}
					
			theform.submit();
		}
		else 
			alert('In order to find a zip code enter a city name, select a state and then click the Find button.\nIf you want to validate your zip code enter it and click the find button.');
	}


	
	function ReceiveZipCodes(sRequestCode, iResultCount) 
	{
	var hUserError;
	
		//Clear the error messages and the marked fields.
		ResetErrorDisplay(); 

		if (iResultCount > 0) {
			SelectZipCode(0, sRequestCode);
			window.document.getElementById('cmd' + sRequestCode + 'ZipList').disabled = !(iResultCount > 1);
		}
		else {
			hUserError =  new UserError('txt' + sRequestCode + 'ZipCode', 'The zip code you entered was not found.', sRequestCode=='From'?3:4, 2);
			hUserError.DisplayError();
			}  
	}



	//function DisplayZipHelp() 
	//{
	//var iLeftPos = event.srcElement.id=='HelpFromZip'?0:300;
	//
	//	window.winPopupHelpZip.style.left = event.clientX + window.document.body.scrollLeft - iLeftPos;
	//	window.winPopupHelpZip.style.top =  event.clientY + window.document.body.scrollTop - 145;
	//	window.winPopupHelpZip.style.display = 'inline';
	//}



	function ShowZipList(e) 
	{
	var src;
		if (e.srcElement) src = e.srcElement;
		else if (e.target) src = e.target;	
	var iLeftPos = src.id=='cmdFromZipList'?0:300;
 	var thedoc = 
		(document.getElementById('fraGetZipCode').document == null) ?
			document.getElementById('fraGetZipCode').contentDocument :
			document.frames['fraGetZipCode'].document;

		document.getElementById('ZipListBlock').innerHTML = thedoc.getElementById('ZipListBlock').innerHTML;
		document.getElementById('txtRequest').value = src.name.indexOf('From')>0?'From':'To';
			
		document.getElementById('winPopupZipList').style.left = e.clientX + window.document.body.scrollLeft - iLeftPos;
		document.getElementById('winPopupZipList').style.top =  e.clientY + window.document.body.scrollTop - 145;
		document.getElementById('winPopupZipList').style.display = 'inline';
	}



	function GetCityState() 
	{
	var src;
		if (e.srcElement) src = e.srcElement;
		else if (e.target) src = e.target;
			
	var sRequestCode = src.name.indexOf('From')>0?'From':'To';
	var theform = 
		(document.getElementById('fraGetZipCode').document == null) ?
			document.getElementById('fraGetZipCode').contentDocument.getElementById('frmGetZipCode') :
			frames['fraGetZipCode'].document.getElementById('frmGetZipCode');
	
		if (IsValidZipCode(src.value, false).ReturnCode == 0) {
			theform.txtRequestCode.value = sRequestCode; 
			theform.txtZipCode.value = window.document.getElementById('txt' + sRequestCode + 'ZipCode').value;
			theform.submit();
		}
	}


	
	function SelectZipCode(iIndex, sRequestCode) 
	{
	var thedoc = 
		(document.getElementById('fraGetZipCode').document == null) ?
			document.getElementById('fraGetZipCode').contentDocument :
			document.frames['fraGetZipCode'].document;
			
	var sValue = thedoc.getElementById('lstZipCodes').options[iIndex].text;
			
		document.getElementById('winPopupZipList').style.display = 'none'; 	

		document.getElementById('txt' + sRequestCode + 'ZipCode').value = sValue.slice(0,5);
		document.getElementById('txt' + sRequestCode + 'City').value = sValue.slice(8, sValue.length - 4);
		document.getElementById('cbo' + sRequestCode + 'State').value = sValue.slice(sValue.length - 2);
	}

