


// Google var setup
var map;
var gdir;
var geocoder = null;
var addressMarker;






function check_form()
{
	var	frm = document.getElementsByName("estimator_form").item(0);
	var err = false;
	var errMsg = 'The following fields are incomplete:\n';
	
	if(frm.street_1.value == '' || frm.street_1.value == 'Address') { 
	err = true;
	errMsg = errMsg + 'Starting Address Street\n';
	}
	
	if(frm.city_1.value == ''  || frm.city_1.value == 'City') { 
	err = true;
	errMsg = errMsg + 'Starting Address City\n';
	}
	// begin dest checking
	
	if(frm.street_2.value == ''  || frm.street_2.value == 'Address') { 
	err = true;
	errMsg = errMsg + 'Destination Address Street\n';
	}
	
	if(frm.city_2.value == ''  || frm.city_2.value == 'City') { 
	err = true;
	errMsg = errMsg + 'Destination Address City\n';
	}
	if(err == true) { alert(errMsg); } 
	else {
		var from_address = frm.street_1.value +' '+frm.city_1.value+',CA';
		var dest_address = frm.street_2.value +' '+frm.city_2.value+',CA';
		// call google setDirections()  
		setDirections(from_address, dest_address);
	}


}






// Google functions
//===========================================================
    function gload()
	{
		if(GBrowserIsCompatible())
		{
			map = new GMap2(document.getElementById("map"));
			gdir = new GDirections();
			GEvent.addListener(gdir, "load", onGDirectionsLoad);
			GEvent.addListener(gdir, "error", handleErrors);
		}
	}
    
    function setDirections(fromAddress, toAddress) {
      gdir.load("from: " + fromAddress + " to: " + toAddress);
    }

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}

	function onGDirectionsLoad(){ 
	   
	   var frm = document.getElementsByName("estimator_form").item(0);
	   HighDrop = false;
	   if(frm.city_1.value.toUpperCase() == "SUNNYVALE" || 
		   	frm.city_2.value.toUpperCase() == "SUNNYVALE") { 
	   HighDrop = true;}
	   
	   ajax_calcPrice(gdir.getSummaryHtml(), HighDrop);
	   
	   // update screen
	   
	   // done
	}
	

	function ajax_calcPrice(gDistance, HighDrop)
	{
		// alert("http://www.yellowcheckercab.com/go/calculator_ajax.php?miles="+gDistance.replace(/&nbsp;/,'|')+'|'+HighDrop);
	
		$.get("http://yellowcheckercab.com/calculator_ajax.php?miles="+gDistance.replace(/&nbsp;/,'|')+'|'+HighDrop, function(data){
			$('#total_cost').val(data);
			//alert("Data Loaded: " + data);
		});
	}




// sets the address info for each airport
function UpdateForm(val)
{
var	frm = document.getElementsByName("estimator_form").item(0);

	if(val == 'SF'){
		frm.street_2.value = '503 North McDonnell Road';
		frm.city_2.value   = 'San Francisco';
		frm.zip_2.value   = '94728';	
	}
	
	
	else if(val == 'SJ'){
		frm.street_2.value = '1661 Airport Blvd';
		frm.city_2.value   = 'San Jose';
		frm.zip_2.value   = '95110';	
	}
	
		else if(val == 'OA'){
		frm.street_2.value = '1 Airport Drive';
		frm.city_2.value   = 'Oakland';
		frm.zip_2.value   = '94621';	
	}
	else { 
	frm.street_2.value = '';
		frm.city_2.value   = '';
		frm.zip_2.value   = '';	
	
	}



}




	function updatelink() 
	{
		var src='';
		var dest='';
		
		if ($('#street_1').val()=='' && $('#street_1').val()=='') {
			$('#book_your_taxi').attr('href','http://www.ridecharge.com/s/ca-sacramento/taxi/branded/yellow_cab_sacramento');
		} else {
			if ($('#street_1').val()!='' && $('#street_1').val()!='Street Address') {
				src=$('#street_1').val()+'+'+$('#city_1 :selected').val()+'+'+'CA';
			}
			if ($('#street_2').val()!='' && $('#street_2').val()!='Street Address') {
				dest=$('#street_2').val()+'+'+$('#city_2 :selected').val()+'+'+'CA';
			}

			$('#book_your_taxi').attr('href','http://www.ridecharge.com/s/ca-sacramento/taxi/branded/yellow_cab_sacramento?pickup_location='+src+'&dropoff_location='+dest);
		}
		
	}

