	var geocoder, location1, location2, gDir;

/*
The code sample was found from 
http://briancray.com/2009/06/23/calculate-driving-distance-google-maps-api/
I modified these functions a bit, and did few changes on the php-file feferring to this. 
-Juho
*/


	function initialize() {
	

	
		geocoder = new GClientGeocoder();
		gDir = new GDirections();
		GEvent.addListener(gDir, "load", function() {
			var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
		if (gDir.getDistance().meters!=null && gDir.getDistance().meters!="")
		{
			document.getElementById('miles_p').innerHTML = toInteger(drivingDistanceMiles) + " miles";
            document.getElementById('price_p').innerHTML = "$" +(30+ (toInteger(drivingDistanceMiles)*2.5));
		}
		else
		{
			alert("Problems locating the Pick-Up or Drop off-locations, this might be due to dublicate addresses on same area. Please try again using more accurate criteria.");
		}
		});
	}

function toInteger(number){
  return Math.round(
    Number(number)
  );
};

function validate_zip(field)
{

var valid = "0123456789";
var hyphencount = 0;

	if (field.length!=5) 
	{
		return false;
	}
	
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		
			if (valid.indexOf(temp) == "-1") 
			{
				
				return false;
			}
	}
return true;

}

function validate_string(name){
var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -.";

for (var i=0; i < name.length; i++) 
	{
		temp = "" + name.substring(i, i+1);
		
			if (valid.indexOf(temp) == "-1") 
			{	
				return false;
			}
	}
return true;
}


function alertselected(select,location){

if(location =="from")
	{

		if(select.selectedIndex == 0)
		{
			document.forms[0].from_zip.value="";
			
			document.forms[0].from_zip.disabled =false;
			
			document.forms[0].from_adress.disabled =false;
			
			document.forms[0].from_city.disabled =false;
			
			document.forms[0].from_state.disabled =false;
		}
		
		else if(select.selectedIndex == 1)
		{
			document.forms[0].from_zip.value="";
			
			document.forms[0].from_adress.value="";
			
			document.forms[0].from_zip.disabled =true;
			
			document.forms[0].from_adress.disabled =true;
			
			document.forms[0].from_city.disabled =false;
			
			document.forms[0].from_state.disabled =false;
		}
		
		else if(select.selectedIndex == 2)
		{
			document.forms[0].from_adress.value="";
			
			document.forms[0].from_adress.disabled =true;
			
			document.forms[0].from_city.value="";
			
			document.forms[0].from_city.disabled =true;
			
			document.forms[0].from_state.value="";
			
			document.forms[0].from_state.disabled =true;
			
			document.forms[0].from_zip.disabled =false;
		}
	
	}
	
else if(location =="to")
	{

		if(select.selectedIndex == 0)
		{
			document.forms[0].to_zip.value="";
			
			document.forms[0].to_zip.disabled =false;
			
			document.forms[0].to_adress.disabled =false;
			
			document.forms[0].to_city.disabled =false;
			
			document.forms[0].to_state.disabled =false;
		}
		
		else if(select.selectedIndex == 1)
		{
			document.forms[0].to_zip.value="";
			
			document.forms[0].to_adress.value="";
			
			document.forms[0].to_zip.disabled =true;
			
			document.forms[0].to_adress.disabled =true;
			
			document.forms[0].to_city.disabled =false;
			
			document.forms[0].to_state.disabled =false;
		}
		
		else if(select.selectedIndex == 2)
		{
			document.forms[0].to_adress.value="";
			
			document.forms[0].to_adress.disabled =true;
			
			document.forms[0].to_city.value="";
			
			document.forms[0].to_city.disabled =true;
			
			document.forms[0].to_state.value="";
			
			document.forms[0].to_state.disabled =true;
			
			document.forms[0].to_zip.disabled =false;
		}		
	}


}

function checkfields() {

from_adress=document.forms[0].from_adress.value;

from_city=document.forms[0].from_city.value;

from_state=document.forms[0].from_state.value;

from_zip=document.forms[0].from_zip.value;

to_adress=document.forms[0].to_adress.value;

to_city=document.forms[0].to_city.value;

to_state=document.forms[0].to_state.value;

to_zip=document.forms[0].to_zip.value;
		
		
if(document.forms[0].fromselect.selectedIndex == 0)
	{
var cnt = 0;	
		//Pick Up adress field
		if (from_adress === "" || from_adress === null || from_adress.charAt(0) === ' ')
		{
			cnt++;
			
		}

		//Pick Up city field
		if (from_city === "" || from_city === null || from_city.charAt(0) === ' ')
		{
			cnt++;
		}
		else if(!validate_string(from_city))
		{
		alert("Please check the format of your pick up city.")
		return false;
		}
		 
		//Pick Up state field
		if (from_state === "" || from_state === null || from_state.charAt(0) === ' ')
		{
			cnt++;
		}
			else if(!validate_string(from_state))
		{
		alert("Please check the format of your pick up state.")
		return false;
		}
			if (from_zip === "" || from_zip === null || from_zip.charAt(0) === ' ')
		{
			cnt++;
		}
			else if(!validate_zip(from_zip))
		{
		alert("Please check the format of your pick up zip code.")
		return false;
		}
		
		if(cnt>2)
		{
		alert("Please provide pick up location information to at least 2 active fields.")
		return false;
		}
		
		
		

	}
	
	
	
else if(document.forms[0].fromselect.selectedIndex == 1)
	{ 
		
		//Pick Up city field
		if (from_city === "" || from_city === null || from_city.charAt(0) === ' ')
		{
			alert("Pick up city is a mandatory field.\nPlease amend and retry.")
			return false;
		}
		
		//Pick Up state field
		if (from_state === "" || from_state === null || from_state.charAt(0) === ' ')
		{
			alert("Pick up state is a mandatory field.\nPlease amend and retry.")
			return false;
		}
			if(!validate_string(from_city))
		{
		alert("Please check the format of your pick up city.")
		return false;
		}
			if(!validate_string(from_state))
		{
		alert("Please check the format of your pick up state.")
		return false;
		}
	}
	
else if (document.forms[0].fromselect.selectedIndex == 2)
	{

		if (from_zip === "" || from_zip === null || from_zip.charAt(0) === ' ')
		{
			alert("Pick up zip code is a mandatory field.\nPlease amend and retry.")
			return false;
		}
		else if(!validate_zip(from_zip)){
		alert("Invalid zip code for pick up location. Zip code can only contain 5 numbers, and no other characters");
		return false;
		}

	}
	

if(document.forms[0].toselect.selectedIndex == 0)
	{ 
		var cnt = 0
		if (to_adress === "" || to_adress === null || to_adress.charAt(0) === ' ')
		{
			cnt++;
		}

		//Drop off city field
		if (to_city === "" || to_city === null || to_city.charAt(0) === ' ')
		{
		cnt++;
		}
				else if(!validate_string(to_city))
		{
		alert("Please check the format of your drop off city.")
		return false;
		}
		
		//Drop off state field
		if (to_state === "" || to_state === null || to_state.charAt(0) === ' ')
		{
		cnt++;
		}
		else if(!validate_string(to_state))
		{
		alert("Please check the format of your drop off state.")
		return false;
		}
		
		
				if (to_zip === "" || to_zip === null || to_zip.charAt(0) === ' ')
		{
			cnt++;
		}
			else if(!validate_zip(to_zip))
		{
		alert("Please check the format of your drop off zip code.")
		return false;
		}
		
			if(cnt>2){
		alert("Please provide drop off location information to at least 2 active fields.")
		return false;
		}
	
		
	}
	
	if(document.forms[0].toselect.selectedIndex == 1)
	{ 
		//Drop off city field
		if (to_city === "" || to_city === null || to_city.charAt(0) === ' ')
		{
			alert("Drop off city is a mandatory field.\nPlease amend and retry.");
			return false;
		}
		
		//Drop off state field
		if (to_state === "" || to_state === null || to_state.charAt(0) === ' ')
		{
			alert("Drop off state is a mandatory field.\nPlease amend and retry.");
			return false;
		}
				if(!validate_string(to_city))
		{
		alert("Please check the format of your drop off city.")
		return false;
		}
			if(!validate_string(to_state))
		{
		alert("Please check the format of your drop off state.")
		return false;
		}
	}
	

else if (document.forms[0].toselect.selectedIndex == 2)
	{
		
		if (to_zip === "" || to_zip === null || to_zip.charAt(0) === ' ')
		{
			alert("Drop off zip code is a mandatory field.\nPlease amend and retry.");
			return false;
		}
		
			else if(!validate_zip(to_zip)){
		alert("Invalid zip code for drop location. Zip code can only contain 5 numbers, and no other characters");
		return false;
		}
	}
	
    return true;
	
}

function reset_fields(){

	document.forms[0].to_adress.disabled =false;
	
	document.forms[0].to_city.disabled =false;
	
	document.forms[0].to_state.disabled =false;
	
	document.forms[0].to_zip.disabled =true;
	
	document.forms[0].from_adress.disabled =false;
	
	document.forms[0].from_city.disabled =false;
	
	document.forms[0].from_state.disabled =false;
	
	document.forms[0].from_zip.disabled =true;
	
	document.getElementById('miles_p').innerHTML = "";
	
    document.getElementById('price_p').innerHTML = "";

}



	function showLocation() 
	{

	if(checkfields())
	{
		 var from = document.forms[0].from_adress.value +" " +document.forms[0].from_city.value;
		 from = from + " " +document.forms[0].from_state.value +" " + document.forms[0].from_zip.value;

		 var to = document.forms[0].to_adress.value +" " +document.forms[0].to_city.value;
		 to =to + " " +document.forms[0].to_state.value +" " + document.forms[0].to_zip.value;

			geocoder.getLocations(from, function (response) 
			{
				if (response.Status.code!=602)
					{
				
					if (!response || response.Status.code != 200)
						{
							document.getElementById('miles_p').innerHTML = "";
							document.getElementById('price_p').innerHTML = "";
							alert("Sorry, we were unable to find the pick-up location");
						}
						
						
					else
						{
							location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
							
							geocoder.getLocations(to, function (response) 
							{
							
							if (!response || response.Status.code != 200)
							{
								document.getElementById('miles_p').innerHTML = "";
								
								document.getElementById('price_p').innerHTML = "";
								
								alert("Sorry, we were unable to find the drop-off location");
							}
							
							else
							{
								location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
								
								gDir.load('from: ' + location1.address + ' to: ' + location2.address);
							}
							
							});
						}
					}
				else
					{
					alert("2 or more locations was found for pick up or drop off locations, please try again with more accurate criteria.");
					}
					
			});
	}
	}