// JavaScript Document

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


// FORM FUNCTIONS
//===========================================================

function check_form()
{
	var	frm = document.forms[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';
	}

	if(frm.zip_1.value == ''  || frm.zip_1.value == 'Zip') { 
	err = true;
	errMsg = errMsg + 'Starting Address Zip\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(frm.zip_2.value == ''  || frm.zip_2.value == 'Zip') { 
	err = true;
	errMsg = errMsg + 'Destination Address Zip\n';
	}

	if(err == true) { alert(errMsg); } 
	else {
	// 	document.forms[0].submit();
	// CALL GOOGLE FUNCTIONS
	
		// format to / from address
		var from_address = frm.street_1.value +' '+frm.city_1.value+',CA '+frm.zip_1.value;
		var dest_address = frm.street_2.value +' '+frm.city_2.value+',CA '+frm.zip_2.value;
		
		// call google setDirections()  
		setDirections(from_address, dest_address);
		
	}


}


// sets the address info for each airport
function UpdateForm1(val)
{
var	frm = document.forms[0];

	if(val == 'SF'){
		frm.street_1.value = '503 North McDonnell Road';
		frm.city_1.value   = 'San Francisco';
		frm.zip_1.value   = '94728';	
	}
	
	
	else if(val == 'SJ'){
		frm.street_1.value = '1661 Airport Blvd';
		frm.city_1.value   = 'San Jose';
		frm.zip_1.value   = '95110';	
	}
	
		else if(val == 'OA'){
		frm.street_1.value = '52 Airport Drive';
		frm.city_1.value   = 'Oakland';
		frm.zip_1.value   = '94621';	
	}
	else { 
	frm.street_1.value = '';
		frm.city_1.value   = '';
		frm.zip_1.value   = '';	
	
	}
}


// sets the address info for each airport
function UpdateForm(val)
{
var	frm = document.forms[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 = '52 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   = '';	
	
	}



}




// Google functions
//===========================================================
    function load() {
			
      if (GBrowserIsCompatible()) {      
       // map = new GMap2(document.getElementById("map"));
		

        gdir = new GDirections();
        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.forms[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('input:'+gDistance.replace(/&nbsp;/,'|'));
		//alert(gDistance.replace(/&nbsp;/,'|')+'|'+HighDrop);
		agent.call("","ajax_calcPrice","ajax_callback_calcPrice",gDistance.replace(/&nbsp;/,'|')+'|'+HighDrop);	
	}
	
	
	function ajax_callback_calcPrice(Prange)
	{
		//alert('callback'+Prange);
		//estTd
		// estSmallText
		//alert(Prange);
		document.getElementById("estTd").style.backgroundColor='#3399ff';
		document.getElementById("estTd").innerHTML = 'Estimated Fare: '+Prange+'<br />(Actual fare may vary)';
		setTimeout('document.getElementById("estTd").style.backgroundColor=\'#3399ff\'', 1500);
		
	}

