var ajax = new sack();



Number.prototype.toRad = function() {  // convert degrees to radians
  return this * Math.PI / 180;
}

Number.prototype.toDeg = function() {  // convert radians to degrees (signed)
  return this * 180 / Math.PI;
}

Number.prototype.toBrng = function() {  // convert radians to degrees (as bearing: 0...360)
  return (this.toDeg()+360) % 360;
}

function dostuff()
{
	var response = new String(ajax.response);	// Executing the response from Ajax as Javascript code
	var values = response.split(',');

	lat_home = 51.55;
	lon_home = 0.717;
	
	lat1 = values[0]; //51.537;
	lon1 = values[1]; //53.063;

	lat2 = values[2]; //0.733;
	lon2 = values[3]; //-3.035;
	
	if(lat1 == "NF") { 
		alert('Could not find your departure location!'); 
	}
	else if(lat2 == "NF") { 
		alert('Could not find your destination location!'); 
	}
	else
	{
		lat1 = parseFloat(lat1);
		lon1 = parseFloat(lon1);
		lat2 = parseFloat(lat2);
		lon2 = parseFloat(lon2);
		
		
	// work out to source
	
	var R = 6371; // km
	var dLat = (lat1-lat_home).toRad();
	var dLon = (lon1-lon_home).toRad(); 
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
	        Math.cos(lat1.toRad()) * Math.cos(lat_home.toRad()) * 
	        Math.sin(dLon/2) * Math.sin(dLon/2); 
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	var d = R * c;
	var miles1 = d*0.621371192;
	if(miles1>100) { 
		miles1 = miles1 + 50;
	}
	else if(miles1>150)
	{
		miles1 = miles1 + 100;
	}
	else if(miles1>250)
	{
		miles1 = miles1 + 150;
	}
	
	//work source to dest	
	
	var R = 6371; // km
	var dLat = (lat2-lat1).toRad();
	var dLon = (lon2-lon1).toRad(); 
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
	        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
	        Math.sin(dLon/2) * Math.sin(dLon/2); 
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	var d = R * c;
	var miles2 = d*0.621371192;
	if(miles2>100) { 
		miles2 = miles2 + 50;
	}
	else if(miles2>150)
	{
		miles2 = miles2 + 100;
	}
	else if(miles2>250)
	{
		miles2 = miles2 + 150;
	}
	
	// work source to home
	
	var R = 6371; // km
	var dLat = (lat_home-lat2).toRad();
	var dLon = (lon_home-lon2).toRad(); 
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
	        Math.cos(lat1.toRad()) * Math.cos(lat_home.toRad()) * 
	        Math.sin(dLon/2) * Math.sin(dLon/2); 
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	var d = R * c;
	var miles3 = d*0.621371192;
	if(miles3>100) { 
		miles3 = miles3 + 50;
	}
	else if(miles3>150)
	{
		miles3 = miles3 + 100;
	}
	else if(miles3>250)
	{
		miles3 = miles3 + 150;
	}
	
	miles = miles1+miles2+miles3;
	
	
	cost = (miles * 0.5);
	if(miles < 10)
	{
		document.getElementById('results').innerHTML = 'Total distance is less than 10 miles! Please call us for a quote on 01702 306460 or 07841133206';
	}
	else 
	{
		document.getElementById('results').innerHTML = 'Approximate distance from our base to your location: ' + Math.round(miles1) + ' miles<br /> Approximate distance between your location and destination: ' + Math.round(miles2) + ' miles<br />	Approximate distance between your destination and our base: ' + Math.round(miles3) + ' miles<br />	<hr />	Total approximate distance to travel: ' + Math.round(miles) + ' miles<br />	Approximate total cost: &pound;' + Math.round(cost) + '<br /><br /><strong>Please note this is an estimation, not a quotation. For an exact quotation please contact us using the form to the right or by calling us on 01702 306460 or 07841133206';
	}
	}
}

function getLatLon(postcode1,postcode2)
{
	if(postcode1.length >= 2 && postcode1.length <= 4)
	{
		if(postcode2.length >= 2 && postcode2.length <= 4)
		{
			ajax.onCompletion = dostuff; // Specify function that will be executed after file has been found
			ajax.requestFile = '/c.php?p1='+postcode1+'&p2='+postcode2;
			ajax.runAJAX();		// Execute AJAX function
		}
		else
		{
			alert("Destination postcode must be minimum 2 chars, maximum 4 chars")
		}
	}
	else
	{
		alert("Departure postcode must be minimum 2 chars, maximum 4 chars")
	}
}
