//<![CDATA[
var oGoogle_Map_API_Timer;
var bValid_Google_Address_Found = false;
var bWaiting_For_Google = false;
var bChecking_Address1 = false;    

var sDealer_Name;
var sAddress2;
var sCity_State_Country_Zip;

// Function timer started in Check_Single_Address function		
function Is_API_Finished()
{					
	
	//Google has sent response back		
	if (bWaiting_For_Google == false)
	{							
		//Stop timer											
		clearInterval(oGoogle_Map_API_Timer);				
					
		if (bValid_Google_Address_Found == false)
		{				
			if (bChecking_Address1 == true)
			{
				if (sAddress2 != null && sAddress2 != '')					
				{						
					//check address 2					
					Check_Single_Address(sDealer_Name, sAddress2, sCity_State_Country_Zip);
				}
			}
		}
	}
}


function Check_Addresses(sInDealer_Name, sInAddress1, sInAddress2, sInCity_State_Country_Zip, nPCID, sGuid)
{
	sDealer_Name = sInDealer_Name;
	sAddress2 = sInAddress2;
	sCity_State_Country_Zip = sInCity_State_Country_Zip;
	
	//Check address 1
	if (sInAddress1 != '')
	{
		bChecking_Address1 = true;
		Check_Single_Address(sDealer_Name, sInAddress1, sCity_State_Country_Zip, nPCID, sGuid);
	}
	else
	{
		if (sInAddress2 != '')
		{
			Check_Single_Address(sDealer_Name, sAddress2, sCity_State_Country_Zip, nPCID, sGuid);
		}
	}						

}


function Check_Single_Address(sDealer_Name,sAddress, sCity_State_Country_Zip, nPCID, sGuid) 
{				
	var geocoder = null;
	var sDealer_Longitude_Latitude = null;
	var nLongitude = null;
	var nLatitude = null;
	var nComma_Idx = null;
	var nClosing_Parenthesis_Idx = null;
	var Google_Map_Div = null;
	var Google_Map_Link = null;

	geocoder = new GClientGeocoder();		
	
	if (geocoder) 
	{			
					
		bWaiting_For_Google = true;

		if (oGoogle_Map_API_Timer != null)
		{
			clearInterval(oGoogle_Map_API_Timer);
		}
		
		oGoogle_Map_API_Timer = setInterval('Is_API_Finished()',10);			
			
		geocoder.getLatLng(
		sAddress.replace(/\'/g,'')+' '+sCity_State_Country_Zip.replace(/\'/g,''), function(point)
			{
				if (!point) 
				{						
					//Could NOT find address						
							
					//Find Google Map Div
					Google_Map_Div = document.getElementById('dvGoogleMapLink');
					
					//Hide Google Map Div
					Google_Map_Div.style.visibility = "hidden";
					Google_Map_Div.style.display="none";
					
					bWaiting_For_Google = false;											
					
				} 
				else 
				{		
					bValid_Google_Address_Found = true;
																		
					sDealer_Longitude_Latitude = point.toString();														
												
					//Point should be returned in the following format ([Latitude],[Longitude]).  Ex. (40.867993,-96.721618)
					
					nComma_Idx = sDealer_Longitude_Latitude.indexOf(',');
					
					//start at position 1 to get rid of the beginning "(".  
					nLatitude = sDealer_Longitude_Latitude.substring(1,nComma_Idx);
					
					//Get final parenthesis
					nClosing_Parenthesis_Idx = sDealer_Longitude_Latitude.indexOf(')');
					
					//Get Longitude.  Add one to nComma_Idx so we do NOT include the comma.
					nLongitude = sDealer_Longitude_Latitude.substring(nComma_Idx+1,nClosing_Parenthesis_Idx);
					
					//Find Google Map Div
					Google_Map_Div = document.getElementById('dvGoogleMapLink');
					
					//Hide Google Map Div
					Google_Map_Div.style.visibility = "";
					Google_Map_Div.style.display="";						
											
					//Find Google Map Link
					Google_Map_Link = document.getElementById('hlGoogleMapLink');
										
					//Set link value
					Google_Map_Link.href= '/Dealers/Map.aspx?Lat=' + nLatitude + '&Lng=' + nLongitude + '&PCID=' + nPCID + '&guid=' + sGuid;

					bWaiting_For_Google = false;		
				}
            
			} 			
		);	//End of geocoder call
    }
    
}

	//]]>   
    