var xmlhttp;

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

function ProceedPay(){
	if(document.getElementById("proceed_hidden").value="1"){
	document.proceed.submit();
	}
	}
function copyCountry(country){document.getElementById("country_hidden_div").value=country;}
function copyLeg1Start(value){document.getElementById("plane_hidden_div_1_start").value=value;}
function copyLeg1End(value){document.getElementById("plane_hidden_div_1_end").value=value;}
function copyLeg2Start(value){document.getElementById("plane_hidden_div_2_start").value=value;}
function copyLeg2End(value){document.getElementById("plane_hidden_div_2_end").value=value;}
function copyLeg3Start(value){document.getElementById("plane_hidden_div_3_start").value=value;}
function copyLeg3End(value){document.getElementById("plane_hidden_div_3_end").value=value;}
function copyPlaneLegs(value){document.getElementById("plane_hidden_div_legs").value=value;}
function copyPlaneSeat(value){document.getElementById("plane_hidden_div_seat").value=value;}
function custom_eff_copy(eff){document.getElementById("hidden-car").value=eff;}
function use_car_type(type){document.getElementById("use_car_type").value=type;}
function removeCommas(aNum) {
	aNum=aNum.replace(/,/g,"");
	aNum=aNum.replace(/\s/g,"");
	return aNum;
}
function submit_form(){document.theform.submit();}
function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
	var x = Math.round(num * Math.pow(10,dec));
	if (x >= 0) n1=n2='';
	var y = (''+Math.abs(x)).split('');var z = y.length - dec;
	if (z<0) z--;
	for(var i = z; i < 0; i++) y.unshift('0');
	if (z<0) z = 1;
	y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0');
	while (z > 3) {
		z-=3;
		y.splice(z,0,thou);
	}
	var r = curr1+n1+y.join('')+n2+curr2;
	return r;
}
function updateTotal(){
	var car = 0;
	var plane = 0;
	var electricity = 0;
	var gas = 0;
	
	if (document.getElementById("car_hidden_div").value){
	car         = parseFloat(removeCommas(document.getElementById("car_hidden_div").value));
	}
	if(document.getElementById("plane_hidden_div").value){
	plane       = parseFloat(removeCommas(document.getElementById("plane_hidden_div").value));
	}
	if(document.getElementById("electricity_hidden_div").value){
	electricity = parseFloat(removeCommas(document.getElementById("electricity_hidden_div").value));
	}
	if(document.getElementById("gas_hidden_div").value){
	gas         = parseFloat(removeCommas(document.getElementById("gas_hidden_div").value));
	}
	var total       = (car + plane + electricity + gas);
	if (document.getElementById("language").value=="zh"){
	document.getElementById("total_calculation_div").innerHTML =  '&#24635;&#37327;: '+formatNumber(total,2,',','.','',' &#21544;','',''); }
	else { document.getElementById("total_calculation_div").innerHTML =  'Total: '+formatNumber(total,2,',','.','',' tons','',''); }
	document.getElementById("total_hidden_div").value = total;
}

function changeUnits(num) {
	if (num=='standard') {
		document.getElementById("unit1").innerHTML='(mpg)';
		document.getElementById("unit2").innerHTML='miles';
		document.getElementById("unit3").innerHTML='kWh';
		document.getElementById("unit4").innerHTML='Liters';
		}
	if (num=='metric') {
		document.getElementById("unit1").innerHTML='(liters/100km)';
		document.getElementById("unit2").innerHTML='kilometers';
		document.getElementById("unit3").innerHTML='kWh';
		document.getElementById("unit4").innerHTML='Liters';
		}
	}
	
	
	
	
	
	
	
function makeNewFlight(){	
	flightNumber = document.getElementById("hidden_plane_flight_total").value;
	flightNumber++;
	document.getElementById("hidden_plane_flight_total").value = flightNumber;
	legText = document.getElementById("hidden_plane_text_leg").value;
	startText = document.getElementById("hidden_plane_text_start").value;
	endText = document.getElementById("hidden_plane_text_end").value;
	containerContents = document.getElementById("plane_container").innerHTML;
    containerNewContents  = containerContents + "             <div id=\"div_plane_" + flightNumber + "\">"
	+ "             <b>" + legText + " " + flightNumber + "</b>"
    + "             <table>"
    + "             <tbody>"
    + "             <tr>"
    + "             <td width=\"100\">" + startText + "</td><td>"
    + "                 <div id=\"leg_" + flightNumber + "_start_AutoComplete\">"
    + "                 <input class=\"ui-corner-all inputbox\" id=\"leg_" + flightNumber + "_start\" name=\"leg_" + flightNumber + "_start\" type=\"text\" onblur=\"calculatePlane(this.value,document.getElementById('leg_" + flightNumber + "_end').value,document.getElementById('seat_type').value);copyLeg" + flightNumber + "Start(this.value);\" />"
    + "                 <div id=\"leg_" + flightNumber + "_start_container\" style=\"width:180px;\"></div>"
    + "                 </div>"
    + "             </td>"
    + "             </tr>"
    + "             <tr>"
    + "             <td>" + endText + "</td><td>"
    + "                 <div id=\"leg_" + flightNumber + "_end_AutoComplete\">"
    + "                 <input class=\"ui-corner-all inputbox\" id=\"leg_" + flightNumber + "_end\" name=\"leg_" + flightNumber + "_end\" type=\"text\" onblur=\"calculatePlane(document.getElementById('leg_" + flightNumber + "_start').value,this.value,document.getElementById('seat_type').value," + flightNumber + ");copy_plane_leg_" + flightNumber + "(this.value);copyLeg" + flightNumber + "End(this.value);\" />"
    + "                 <div id=\"leg_" + flightNumber + "_end_container\" style=\"width:180px;\"></div>"
    + "                 </div>"
    + "             </td>"
    + "             </tr>"
    + "             </tbody>"
    + "             </table>"
    + "             </div>";
	document.getElementById("plane_container").innerHTML = containerNewContents;
	}
	
	
	
	
	
function plane_legs(num) {
	if (num==0) {
		plane_close_2();
		plane_close_3();
	}
	if (num==1) {
		plane_open_2();
		plane_close_3();
	}
	if (num==2) {
		plane_open_2();
		plane_open_3();
	}
}

function plane_open_2() { document.getElementById('div_plane_2').style.display = 'block'; }
function plane_open_3() { document.getElementById('div_plane_3').style.display = 'block'; }
function plane_close_2() { document.getElementById('div_plane_2').style.display = 'none'; }
function plane_close_3() { document.getElementById('div_plane_3').style.display = 'none'; }
function copy_plane_leg_1(text)
        {
		document.getElementById('leg_2_start').value = text;
		}
function copy_plane_leg_2(text)
        {
		document.getElementById('leg_3_start').value = text;
		}
	
	
	
	
	
	
	
	
	
	
function runRegister(firstname,lastname,email,password,country,company,phone,units,language){
	
document.getElementById("user-box-content").innerHTML='<b>Registering...</b>';
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="js/register-user.php";
url=url+"?f="+firstname+"&l="+lastname+"&e="+email+"&p="+password+"&c="+country+"&y="+company+"&z="+phone+"&u="+units+"&x="+language;
//document.getElementById("user-box-content").innerHTML=url;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChangedRegUser;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function showManufacturer(year,manufacturer,model)
{
document.getElementById("car_manufacturer_div").innerHTML='Finding Manufacturers...';
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="js/getcar.php";
url=url+"?type=manufacturer"+"&year="+year;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChangedCarManu;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function showModel(year,manufacturer,model)
{
document.getElementById("car_model_div").innerHTML='Finding Models...';
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="js/getcar.php";
url=url+"?type=model&manufacturer="+manufacturer+"&year="+year;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChangedCarModel;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function showMPG(year,manufacturer,model)
{
document.getElementById("hidden-car").value="Finding MPG...";
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="js/getcar.php";
url=url+"?type=mpg&manufacturer="+manufacturer+"&model="+model+"&year="+year;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChangedCarMPG;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function calculatePlane(startLeg,endLeg,seatType,leg)
	{
	if (startLeg!="" && endLeg!="") 
		{
		if (document.getElementById("language").value=="zh"){
			document.getElementById("plane_calculation_div").innerHTML='<b>&#35745;&#31639;...</b>';
		} else { document.getElementById("plane_calculation_div").innerHTML='<b>Calculating...</b>'; }
		xmlhttp=GetXmlHttpObject();
		if (xmlhttp==null)
		  {
		  alert ("Browser does not support HTTP Request");
		  return;
		}
		var patt1=new RegExp("\\([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]\\)");
		var startNew = patt1.exec(startLeg);
		var endNew = patt1.exec(endLeg);
		var url="js/calculate-plane.php";
		url=url+"?s="+startNew+"&e="+endNew+"&t="+seatType;
		url=url+"&sid="+Math.random();
		if(leg==1){xmlhttp.onreadystatechange=stateChangedCalcPlane1;}
		if(leg==2){xmlhttp.onreadystatechange=stateChangedCalcPlane2;}
		if(leg==3){xmlhttp.onreadystatechange=stateChangedCalcPlane3;}
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}

function calculateGas(amount,type)
{
		if (document.getElementById("language").value=="zh"){
			document.getElementById("gas_calculation_div").innerHTML='<b>&#35745;&#31639;...</b>';
		} else { document.getElementById("gas_calculation_div").innerHTML='<b>Calculating...</b>'; }
document.getElementById("gas_amount_hidden_div").value=amount;
document.getElementById("gas_type_hidden_div").value=type;
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="js/calculate-gas.php";
url=url+"?a="+amount+"&t="+type;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChangedCalcGas;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function calculateElectricity(amount,country)
{
		if (document.getElementById("language").value=="zh"){
			document.getElementById("electricity_calculation_div").innerHTML='<b>&#35745;&#31639;...</b>';
		} else { document.getElementById("electricity_calculation_div").innerHTML='<b>Calculating...</b>'; }
document.getElementById("electricity_amount_hidden_div").value=amount;
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="js/calculate-electricity.php";
url=url+"?a="+amount+"&c="+country;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChangedCalcElectricity;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function calculateCar(efficiency,distance,type)
{
		if (document.getElementById("language").value=="zh"){
			document.getElementById("car_calculation_div").innerHTML='<b>&#35745;&#31639;...</b>'; }
		else { document.getElementById("car_calculation_div").innerHTML='<b>Calculating...</b>'; }
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="js/calculate-car.php";
url=url+"?e="+efficiency+"&d="+distance+"&t="+type;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChangedCalcCar;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}






function stateChangedCarManu()
{
if (xmlhttp.readyState==4)
{
document.getElementById("car_manufacturer_div").innerHTML=xmlhttp.responseText;
}
}

function stateChangedCarModel()
{
if (xmlhttp.readyState==4)
{
document.getElementById("car_model_div").innerHTML=xmlhttp.responseText;
document.getElementById("car6").style.display='block';
}
}

function stateChangedCarMPG()
{
if (xmlhttp.readyState==4)
{
document.getElementById("hidden-car").value=xmlhttp.responseText;
document.getElementById("car6").style.display='block';
}
}

function stateChangedRegUser()
{
if (xmlhttp.readyState==4)
{
var output = xmlhttp.responseText;
var output_array = output.split("******");
document.getElementById("user-box").innerHTML=output_array[0];
document.getElementById("country_hidden_div").value=output_array[1];
document.getElementById("id_hidden_div").value=output_array[2];
document.getElementById("email_hidden_div").value=output_array[3];
document.getElementById("firstname_hidden_div").value=output_array[4];
updateTotal();
}
}

function stateChangedCalcPlane1()
{
if (xmlhttp.readyState==4)
{
		document.getElementById("plane_hidden_div_1").value=removeCommas(xmlhttp.responseText);
		var leg1 = 0;
		var leg2 = 0;
		var leg3 = 0;
		var total = 0;
		leg1=parseFloat(removeCommas(xmlhttp.responseText));
		if(document.getElementById("plane_hidden_div_2").value){leg2=parseFloat(removeCommas(document.getElementById("plane_hidden_div_2").value));}
		if(document.getElementById("plane_hidden_div_3").value){leg3=parseFloat(removeCommas(document.getElementById("plane_hidden_div_3").value));}
		total = Math.round(( leg1 + leg2 + leg3 )*100)/100;
		if (document.getElementById("language").value=="zh"){
		document.getElementById("plane_calculation_div").innerHTML='<a><b>&#39134;&#26426;</b><br />'+total+' &#21544;</a>'; }
		else { document.getElementById("plane_calculation_div").innerHTML='<a><b>Plane</b><br />'+total+' tons</a>'; }
	if (xmlhttp.responseText!=0){
		document.getElementById("plane_calculation_div").className="color_pie";
	}else{
		document.getElementById("plane_calculation_div").className="";
		}
document.getElementById("plane_hidden_div").value=total;
updateTotal();
}
}
function stateChangedCalcPlane2()
{
if (xmlhttp.readyState==4)
{
		document.getElementById("plane_hidden_div_2").value=removeCommas(xmlhttp.responseText);
		var leg1 = 0;
		var leg2 = 0;
		var leg3 = 0;
		var total = 0;
		if(document.getElementById("plane_hidden_div_1").value){leg1=parseFloat(removeCommas(document.getElementById("plane_hidden_div_1").value));}
		leg2=parseFloat(removeCommas(xmlhttp.responseText));
		if(document.getElementById("plane_hidden_div_3").value){leg3=parseFloat(removeCommas(document.getElementById("plane_hidden_div_3").value));}
		total =  Math.round(( leg1 + leg2 + leg3 )*100)/100;
		if (document.getElementById("language").value=="zh"){
		document.getElementById("plane_calculation_div").innerHTML='<a><b>&#39134;&#26426;</b><br />'+total+' &#21544;</a>'; }
		else { document.getElementById("plane_calculation_div").innerHTML='<a><b>Plane</b><br />'+total+' tons</a>'; }
	if (xmlhttp.responseText!=0){
		document.getElementById("plane_calculation_div").className="color_pie";
	}else{
		document.getElementById("plane_calculation_div").className="";
		}
document.getElementById("plane_hidden_div").value=total;
updateTotal();
}
}
function stateChangedCalcPlane3()
{
if (xmlhttp.readyState==4)
{
		document.getElementById("plane_hidden_div_3").value=removeCommas(xmlhttp.responseText);
		var leg1 = 0;
		var leg2 = 0;
		var leg3 = 0;
		var total = 0;
		if(document.getElementById("plane_hidden_div_1").value){leg1=parseFloat(removeCommas(document.getElementById("plane_hidden_div_1").value));}
		if(document.getElementById("plane_hidden_div_2").value){leg2=parseFloat(removeCommas(document.getElementById("plane_hidden_div_2").value));}
		leg3=parseFloat(removeCommas(xmlhttp.responseText));
		total =  Math.round(( leg1 + leg2 + leg3 )*100)/100;
		if (document.getElementById("language").value=="zh"){
		document.getElementById("plane_calculation_div").innerHTML='<a><b>&#39134;&#26426;</b><br />'+total+' &#21544;</a>'; }
		else { document.getElementById("plane_calculation_div").innerHTML='<a><b>Plane</b><br />'+total+' tons</a>'; }
	if (xmlhttp.responseText!=0){
		document.getElementById("plane_calculation_div").className="color_pie";
	}else{
		document.getElementById("plane_calculation_div").className="";
		}
document.getElementById("plane_hidden_div").value=total;
updateTotal();
}
}

function stateChangedCalcGas()
{
if (xmlhttp.readyState==4)
{
	if (xmlhttp.responseText==0){
		document.getElementById("gas_calculation_div").innerHTML='';
		document.getElementById("gas_calculation_div").className="";
	}else{
		if (document.getElementById("language").value=="zh"){
			document.getElementById("gas_calculation_div").innerHTML='<a><b>&#29123;&#27668;</b><br />'+xmlhttp.responseText+' &#21544;</a>'; }
			else { document.getElementById("gas_calculation_div").innerHTML='<a><b>Gas</b><br />'+xmlhttp.responseText+' tons</a>'; }
		document.getElementById("gas_calculation_div").className="color_pie";
	}
	document.getElementById("gas_hidden_div").value=xmlhttp.responseText;
	updateTotal();
}
}


function stateChangedCalcElectricity()
{
if (xmlhttp.readyState==4)
{
	if (xmlhttp.responseText==0){
		document.getElementById("electricity_calculation_div").innerHTML='';
		document.getElementById("electricity_calculation_div").className="";
	}else{
		if (document.getElementById("language").value=="zh"){
		document.getElementById("electricity_calculation_div").innerHTML='<a><b>&#30005;</b><br />'+xmlhttp.responseText+' &#21544;</a>';}
		else { document.getElementById("electricity_calculation_div").innerHTML='<a><b>Electricity</b><br />'+xmlhttp.responseText+' tons</a>'; } 
		document.getElementById("electricity_calculation_div").className="color_pie";
	}
	document.getElementById("electricity_hidden_div").value=xmlhttp.responseText;
	updateTotal();
}
}

function stateChangedCalcCar()
{
if (xmlhttp.readyState==4)
{
	if (xmlhttp.responseText==0){
		document.getElementById("car_calculation_div").innerHTML='';
		document.getElementById("car_calculation_div").className="";
	}else{
		if (document.getElementById("language").value=="zh"){
		document.getElementById("car_calculation_div").innerHTML='<a><b>&#27773;&#36710;</b><br />'+xmlhttp.responseText+' &#21544;</a>'; }
		else { document.getElementById("car_calculation_div").innerHTML='<a><b>Vehicle</b><br />'+xmlhttp.responseText+' tons</a>'; }
		document.getElementById("car_calculation_div").className="color_pie";
	}
document.getElementById("car_hidden_div").value=xmlhttp.responseText;
updateTotal();
}
}

function updateCalculator(){
if (document.getElementById("language").value=="zh"){
	if(document.getElementById("car_hidden_div").value>0) {
		document.getElementById("car_calculation_div").innerHTML='<a><b>&#27773;&#36710;</b><br />'+document.getElementById("car_hidden_div").value+' &#21544;</a>';
		document.getElementById("car_calculation_div").className="color_pie";
		}
	if(document.getElementById("electricity_hidden_div").value>0){
		document.getElementById("electricity_calculation_div").innerHTML='<a><b>&#30005;</b><br />'+document.getElementById("electricity_hidden_div").value+' &#21544;</a>';
		document.getElementById("electricity_calculation_div").className="color_pie";
		}
	if(document.getElementById("gas_hidden_div").value>0){
		document.getElementById("gas_calculation_div").innerHTML='<a><b>&#29123;&#27668;</b><br />'+document.getElementById("gas_hidden_div").value+' &#21544;</a>';
		document.getElementById("gas_calculation_div").className="color_pie";
		}
	if(document.getElementById("plane_hidden_div").value>0){
		document.getElementById("plane_calculation_div").innerHTML='<a><b>&#39134;&#26426;</b><br />'+document.getElementById("plane_hidden_div").value+' &#21544;</a>';
		document.getElementById("plane_calculation_div").className="color_pie";
		}
	} else {
		if(document.getElementById("car_hidden_div").value>0) {
		document.getElementById("car_calculation_div").innerHTML='<a><b>Vehicle</b><br />'+document.getElementById("car_hidden_div").value+' tons</a>';
		document.getElementById("car_calculation_div").className="color_pie";
		}
	if(document.getElementById("electricity_hidden_div").value>0){
		document.getElementById("electricity_calculation_div").innerHTML='<a><b>Electricity</b><br />'+document.getElementById("electricity_hidden_div").value+' tons</a>';
		document.getElementById("electricity_calculation_div").className="color_pie";
		}
	if(document.getElementById("gas_hidden_div").value>0){
		document.getElementById("gas_calculation_div").innerHTML='<a><b>Gas</b><br />'+document.getElementById("gas_hidden_div").value+' tons</a>';
		document.getElementById("gas_calculation_div").className="color_pie";
		}
	if(document.getElementById("plane_hidden_div").value>0){
		document.getElementById("plane_calculation_div").innerHTML='<a><b>Plane</b><br />'+document.getElementById("plane_hidden_div").value+' tons</a>';
		document.getElementById("plane_calculation_div").className="color_pie";
		}
	}
}


