// knots
rates = new Array();
rates["King Air 200"] = 270;
rates["King Air 90"] = 220;

function CalcTime(from_airport, dest_airport, knots) {
	var Orig=new Array();
	var Dest=new Array();
	var nm=0; // naurical miles
	var tc=0; // degrees
	
	if (from_airport=="" || dest_airport=="") {
		alert("Please enter both an Originating and Destination Airport.");
		return "";
	}
	else if (from_airport == dest_airport) {
		alert("Please choose different Originating and Destination Airports.");
		return "";
	} 
	else if (!knots) {
		alert("Please choose an aircraft type.");
		return "";
	}
	else {
		Orig=from_airport.split("/");
		Dest=dest_airport.split("/");
		d=Math.acos(Math.sin(Orig[2])
			*Math.sin(Dest[2])
			+Math.cos(Orig[2])
			*Math.cos(Dest[2])
			*Math.cos(Orig[4]-Dest[4]));
		nm=Math.round(3437.747*d);
		if (Math.sin(Dest[4]-Orig[4]) < 0) {
			tc=Math.acos((Math.sin(Dest[2])
				-Math.sin(Orig[2])*Math.cos(d))
				/(Math.sin(d)*Math.cos(Orig[2])));
		}
		else {
			tc=2*Math.PI
				-Math.acos((Math.sin(Dest[2])
				-Math.sin(Orig[2])
				*Math.cos(d))/(Math.sin(d)
				*Math.cos(Orig[2])));
		}
		tc=Math.round(tc*(180/Math.PI));
		//nm *= 1.15; // magical fudge factor
		//alert(nm);
		return ( (nm/knots).toString().substr(0,4) + " hours");
	}
}

function CalcPrice(from_airport, dest_airport, baseCost, dollarsPerMile) {
	var Orig=new Array();
	var Dest=new Array();
	var nm=0; // naurical miles
	var tc=0; // degrees
	
	if (from_airport=="" || dest_airport=="") {
		alert("Please enter both an Originating and Destination Airport.");
		return "";
	}
	else if (from_airport == dest_airport) {
		alert("Please choose different Originating and Destination Airports.");
		return "";
	} 
	else if (!dollarsPerMile) {
		alert("Please choose an aircraft type.");
		return "";
	}
	else {
		Orig=from_airport.split("/");
		Dest=dest_airport.split("/");
		d=Math.acos(Math.sin(Orig[2])
			*Math.sin(Dest[2])
			+Math.cos(Orig[2])
			*Math.cos(Dest[2])
			*Math.cos(Orig[4]-Dest[4]));
		nm=Math.round(3437.747*d);
		if (Math.sin(Dest[4]-Orig[4]) < 0) {
			tc=Math.acos((Math.sin(Dest[2])
				-Math.sin(Orig[2])*Math.cos(d))
				/(Math.sin(d)*Math.cos(Orig[2])));
		}
		else {
			tc=2*Math.PI
				-Math.acos((Math.sin(Dest[2])
				-Math.sin(Orig[2])
				*Math.cos(d))/(Math.sin(d)
				*Math.cos(Orig[2])));
		}
		tc=Math.round(tc*(180/Math.PI));
		//message=(Orig[0] + " to " + Dest[0] + "\n\n");
		//message+=("Degrees:  " +tc+ "\n");
		//message+=("Nautical Miles: "+nm+"\n");
		//message+=("Statute Miles: "+Math.round(nm*1.13636)+"\n");
		//message+=("Kilometers: "+Math.round(nm*1.6094));
		//alert(message);
		return "$" + toCurrency(baseCost + dollarsPerMile * nm);
	}
}

function toCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}