// JavaScript Document

// Car Calculations
function carCalculation(){
	
	
	
	var theForm = document.getElementById("car_calculation");
	var cAmount = theForm.elements['amount'].value;
	var cPrice = theForm.elements['price'].value;
	var cSymbol = theForm.elements['car_curr'].value;
	var fErrors = new Array();
	
	
	
	if(!cAmount){
		fErrors.push("- Petrol Bill");
		var fProcess = false;
	}
	
	if(!cPrice){
		fErrors.push("- Average Price");
		var fProcess = false;
	}
	
	// Loop through and display the errors
	if(fProcess == false){
	 
	 for(i=0; i<fErrors.length; i++){
		 if(i==0){
			var theErrors = fErrors[i] + "\n";
		 }else{
		 theErrors += fErrors[i] + "\n";
		 }
	 }
	 alert("Please enter\n\n" + theErrors);
	 return;

	}
	
	// Calcluation period
	if(theForm.elements['period'][0].checked){
	var cPeriod = 52;
	}else{
		var cPeriod = 12;
	}
	
	// Fuel type
	switch(theForm.elements['fuel'].selectedIndex){
	
	 case 0:
	 var cFuel = 2.4;
	 break;
	 
	 case 1:
	 var cFuel = 2.7;
	 break;
	 
	 case 2:
	 var cFuel = 1.6;
	 break;
		
	}
	
	// The calculation
	var subtotal = cAmount * cPeriod;
	subtotal = subtotal / cPrice;
	var totalTonne = (subtotal * cFuel) / 1000;
	
	var totalPrice =  totalTonne * 20;
	totalPrice = totalPrice * theForm.elements['car_fx'].value
	
	// Set form variables
	
	theForm.elements['car_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['car_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_car").innerText = totalTonne.toFixed(1) + "t"
		document.getElementById("result_price_car").innerText = cSymbol + totalPrice.toFixed(0);
	}else{
		document.getElementById("result_tonnes_car").textContent = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_car").textContent = cSymbol + totalPrice.toFixed(0) ;
	}
}

// Car Presets
function carPreset(){
	
	var theForm = document.getElementById("car_calculation");
	var cSymbol = theForm.elements['car_curr'].value;
	
	// Check for selected preset
	if(theForm.elements['preset'].selectedIndex!=0){
	
		switch (theForm.elements['preset'].value){
		
		case "motorcycle":
		var totalTonne = 2;
		break;
		
		case "small car":
		var totalTonne = 2.7;
		break;
		
		case "med car":
		var totalTonne = 3.6;
		break;
		
		case "large car":
		var totalTonne = 4.2;
		break;
		
		case "4x4":
		var totalTonne = 5.2;
		break;
		
		case "avg  diesel":
		var totalTonne = 4.1;
		break;
		
		case "avg lpg":
		var totalTonne = 2.4;
		break;
		
		
		}
	
	}
	
	var totalPrice =  totalTonne * 20;
	totalPrice = totalPrice * theForm.elements['car_fx'].value
	
	// Set form variables
	
	theForm.elements['car_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['car_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_car").innerText = totalTonne.toFixed(1) + "t"
		document.getElementById("result_price_car").innerText = cSymbol + totalPrice.toFixed(0);
	}else{
		document.getElementById("result_tonnes_car").textContent = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_car").textContent = cSymbol + totalPrice.toFixed(0) ;
	}
	
}

// House Calculations
function houseCalculation(){
	
	var theForm = document.getElementById("house_calculation");
	var cAmount = theForm.elements['amount'].value;
	var cSymbol = theForm.elements['house_curr'].value;
	var fErrors = new Array();
	
	// Calcluation period
	switch(theForm.elements['period'].selectedIndex){
	
	 case 0:
	 var cPeriod = 365;
	 break;
	 
	 case 1:
	 var cPeriod = 12;
	 break;
	 
	 case 2:
	 var cPeriod = 1;
	 break;
	 
	}
	
	// location
	switch(theForm.elements['location'].value){
	
	 case "NSW":
	 var cLocation = 0.92;
	 break;
	 
	 case "ACT":
	 var cLocation = 0.92;
	 break;
	 
	 case "NT":
	 var cLocation = 0.69;
	 break;
	 
	 case "QLD":
	 var cLocation = 1.02;
	 break;
	 
	 case "SA":
	 var cLocation = 0.95;
	 break;
	 
	 case "VIC":
	 var cLocation = 1.34;
	 break;
	 
	 case "WA":
	 var cLocation = 1.10;
	 break;
	 
	 case "TAS":
	 var cLocation = 0;
	 break;
	 
	 case "Australian Average":
	 var cLocation = 1;
	 break;
		
	}
	
	// Green power
	if(theForm.elements['greenpower'][0].checked){
	var cGreen = 0.1;
	}else if(theForm.elements['greenpower'][1].checked){
		var cGreen = 0.5;
	}else{
		var cGreen = 1;
	}
	
	if(!cAmount){
		fErrors.push("- Average Usage");
		var fProcess = false;
	}
	
	
	// Loop through and display the errors
	if(fProcess == false){
	 
	 for(i=0; i<fErrors.length; i++){
		 if(i==0){
			var theErrors = fErrors[i] + "\n";
		 }else{
		 theErrors += fErrors[i] + "\n";
		 }
	 }
	 alert("Please enter\n\n" + theErrors);
	 return;

	}
	
	// The calculation
	var totalTonne = (cLocation * cAmount * cPeriod * cGreen) / 1000;
	var totalPrice = totalTonne * 50;
	totalPrice = totalPrice * theForm.elements['house_fx'].value
	
	// Set form variables
	
	theForm.elements['house_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['house_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_house").innerText = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_house").innerText = cSymbol + totalPrice.toFixed(0);
	}else{
		document.getElementById("result_tonnes_house").textContent = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_house").textContent = cSymbol + totalPrice.toFixed(0);
	}
	
}

// Exact Calculations
function exactCalculation(){
	
	var theForm = document.getElementById("exact_calculation");
	var cAmount = theForm.elements['amount'].value;
	var cPrice = theForm.elements['price'].value;
	var cSymbol = theForm.elements['exact_curr'].value;
	var fProcess;
	
	if(!cAmount && !cPrice && theForm.elements['capita'].selectedIndex==0){
		fProcess = false;
	}
	
	// Calcluation period
	switch(theForm.elements['capita'].value){
	 
	 case "Australia":
	 var cCapita = 25.9;
	 break;
	 
	 case "Australia Domestic":
	 var cCapita = 13;
	 break;
	 
	 case "New Zealand":
	 var cCapita = 19.3;
	 break;
	 
	 case "USA":
	 var cCapita = 22.9;
	 break;
	 
	 case "Canada":
	 var cCapita = 24.3;
	 break;
	 
	 case "United Kingdom":
	 var cCapita = 11;
	 break;
	 
	 case "Ireland":
	 var cCapita = 16.8;
	 break;
	 
	 case "Indonesia":
	 var cCapita = 14.9;
	 break;
	 
	 case "Brazil":
	 var cCapita = 12.8;
	 break;
	 
	 case "Asia":
	 var cCapita = 4.5;
	 break;
	 
	 case "Europe":
	 var cCapita = 10.6;
	 break;
	 
	 case "Middle East":
	 var cCapita = 5.7;
	 break;
	 
	 case "Africa":
	 var cCapita = 4.5;
	 break;
	 
	 case "North America":
	 var cCapita = 23.1;
	 break;
	 
	 case "Cental America":
	 var cCapita = 6.3;
	 break;
	 
	 case "South America":
	 var cCapita = 11.1;
	 break;
	 
	 case "Oceania":
	 var cCapita = 24.2;
	 break;
	 
	 case "World":
	 var cCapita = 6.8;
	 break;
	 
	 
	}
	
	// Loop through and display the errors
	if(fProcess == false){
	 
	 alert("Please enter either the amount in tonnes, dollars or select per capita");
	 return;

	}
	
	// The calculation
	
	if(cAmount){
	 var totalTonne = cAmount;
	 var totalPrice = totalTonne * 20;
	}else if(cPrice){
	 var totalPrice = cPrice;
	 var totalTonne = cPrice / 20;
	}else if(cCapita){
	 var totalTonne = cCapita;
	 var totalPrice = totalTonne * 20;
	}
	
	totalPrice = totalPrice * 1;
	totalPrice = totalPrice * theForm.elements['exact_fx'].value
	totalTonne = totalTonne * 1;
	
	// Set form variables
	
	theForm.elements['exact_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['exact_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_exact").innerText = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_exact").innerText = cSymbol + totalPrice.toFixed(0);
		
	}else{
		document.getElementById("result_tonnes_exact").textContent = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_exact").textContent = cSymbol + totalPrice.toFixed(0);
	}
	
}

function housePreset(){
	
	var theForm = document.getElementById("house_calculation");
	var cSymbol = theForm.elements['house_curr'].value;
	
	// Check for selected preset
	if(theForm.elements['preset'].selectedIndex!=0){
	
		switch (theForm.elements['preset'].value){
		
		case "1 bed apartment":
		var cKWH = 10;
		break;
		
		case "2 bed house":
		var cKWH = 15;
		break;
		
		case "3 bed house":
		var cKWH = 22;
		break;
		
		case "4 bed house":
		var cKWH = 28;
		break;
		
		case "5 bed house":
		var cKWH = 34;
		break
		}
	// Set form variables
	theForm.elements['amount'].value = cKWH;
	}else{
		theForm.elements['amount'].value = '';
	}
}
	
// exact preset
function exactPreset(){
	
	var theForm = document.getElementById("exact_calculation");
	var cSymbol = theForm.elements['exact_curr'].value;
	
	// exact preset
	if(theForm.elements['preset'].selectedIndex!=0){
	
		switch (theForm.elements['preset'].selectedIndex){
		
		case 1:
		var totalTonne = 4;
		break;
		
		case 2:
		var totalTonne = 7;
		break;
		
		case 3:
		var totalTonne = 10;
		break;
		
		}
		
	}
	
	
	// The calculation
	var totalPrice = totalTonne * 20;
	totalPrice = totalPrice * theForm.elements['exact_fx'].value
	
	// Set form variables
	theForm.elements['exact_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['exact_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_exact").innerText = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_exact").innerText = cSymbol + totalPrice.toFixed(0);
	}else{
		document.getElementById("result_tonnes_exact").textContent = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_exact").textContent = cSymbol + totalPrice.toFixed(0);
	}
	
}

// Flight Calculations
function flightCalculation(){
	
	var theForm = document.getElementById("flight_calculation");
	var cDistance = theForm.elements['flight_distance'].value;
	var cSymbol = theForm.elements['flight_curr'].value;
	var cPassengers = theForm.elements['passengers'].value
	
	var fErrors = new Array();
	

	
	if(theForm.elements['class'][0].checked ){
		if(cDistance <= 4000){
			var cClass = 0.583;
		}else{
			var cClass = 0.349;
		}
	}else if(theForm.elements['class'][1].checked){
		if(cDistance <= 4000){
			var cClass = 0.426;
		}else{
			var cClass = 0.256;
		}
	}
	
	if(theForm.elements['trip'][0].checked){
	var cTrip = 1;
	}else if(theForm.elements['trip'][1].checked){
		var cTrip = 2;
	}
	
	// Errors
	
	if(!cPassengers){
		fErrors.push("- Passengers");
		var fProcess = false;
	}
	
	if(!theForm.elements['class'][0].checked && !theForm.elements['class'][1].checked){
		fErrors.push("- Business or Economy Class");
		var fProcess = false;
	}
	
	if(!theForm.elements['trip'][0].checked & !theForm.elements['trip'][1].checked){
		fErrors.push("- One Way or Return");
		var fProcess = false;
	}
	
	
	// Loop through and display the errors
	if(fProcess == false){
	 
	 for(i=0; i<fErrors.length; i++){
		 if(i==0){
			var theErrors = fErrors[i] + "\n";
		 }else{
		 theErrors += fErrors[i] + "\n";
		 }
	 }
	 alert("Please enter\n\n" + theErrors);
	 return;

	}
	
	// The calculation
	if(cDistance > 4000){
		var totalTonne = (cDistance * cClass * cTrip * cPassengers) / 1000;
	}else{
	    var totalTonne = (cDistance * cClass * cTrip * cPassengers) / 1000;
	}
	
	var totalPrice = totalTonne * 20;
	
	// Set form variables
	
	theForm.elements['flight_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['flight_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_flight").innerText = totalTonne.toFixed(1)  + "t";
		document.getElementById("result_price_flight").innerText = cSymbol + totalPrice.toFixed(0);
	}else{
		document.getElementById("result_tonnes_flight").textContent = totalTonne.toFixed(1)  + "t";
		document.getElementById("result_price_flight").textContent = cSymbol + totalPrice.toFixed(0);
	}
	
}

// Flight preset
function flightPreset(){
	
	var theForm = document.getElementById("flight_calculation");
	var cSymbol = theForm.elements['flight_curr'].value;
	
	// exact preset
	if(theForm.elements['preset'].selectedIndex!=0){
	
		switch (theForm.elements['preset'].value){
		
		case "short domestic return":
		var totalTonne = 0.5;
		break;
		
		case "long domestic return":
		var totalTonne = 1;
		break;
		
		case "Aus to Bali return":
		var totalTonne = 3.1;
		break;
		
		case "Aus to London Return":
		var totalTonne = 10.2;
		break;
		
		case "Aus to LA Return":
		var totalTonne = 8.7;
		break;
		
		case "short domestic single":
		var totalTonne = 0.3;
		break;
		
		case "long domestic single":
		var totalTonne = 0.5;
		break;
		
		}
		
	}
	
	
	// The calculation
	var totalPrice = totalTonne * 20  ;
	totalPrice = totalPrice * theForm.elements['flight_fx'].value
	
	// Set form variables
	theForm.elements['flight_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['flight_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_flight").innerText = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_flight").innerText = cSymbol + totalPrice.toFixed(0);
	}else{
		document.getElementById("result_tonnes_flight").textContent = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_flight").textContent = cSymbol + totalPrice.toFixed(0);
	}
	
}



// Event Calculations
function eventCalculation(){
	
	var theForm = document.getElementById("event_calculation");
	var cSymbol = theForm.elements['event_curr'].value;
	var cTripShort = theForm.elements['trips_short'].value;
	var cTripMed = theForm.elements['trips_med'].value;
	var cTripLong = theForm.elements['trips_long'].value;
	
	var cCars = theForm.elements['cars'].value;
	var cDistance = theForm.elements['distance'].value;
	var cRooms = theForm.elements['rooms'].value;
	var fErrors = new Array();
	var fProcess;
	
	// Loop through and display the errors
	if(fProcess == false){
	 
	 for(i=0; i<fErrors.length; i++){
		 if(i==0){
			var theErrors = fErrors[i] + "\n";
		 }else{
		 theErrors += fErrors[i] + "\n";
		 }
	 }
	 alert("Please enter\n\n" + theErrors);
	 return;

	}
	
	// The calculation
	cDistance = (cDistance / 9.5 ) * 2.4;
	cRooms = cRooms * 15;
	var totalTonne = ((cTripShort * 300) + (cTripMed * 1000) + (cTripLong * 2500) + (cCars * cDistance) + cRooms + 100) / 1000;
	var totalPrice = totalTonne * 20;
	totalPrice = totalPrice * theForm.elements['event_fx'].value

	// Set form variables
	
	theForm.elements['event_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['event_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_event").innerText = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_event").innerText = cSymbol + totalPrice.toFixed(0);
	}else{
		document.getElementById("result_tonnes_event").textContent = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_event").textContent = cSymbol + totalPrice.toFixed(0);
	}
	
}

// Event Presets
function eventPreset(){
	
	var theForm = document.getElementById("event_calculation");
	var cSymbol = theForm.elements['event_curr'].value;
	
	// Check for selected preset
	if(theForm.elements['preset'].selectedIndex!=0){
	
		switch (theForm.elements['preset'].selectedIndex){
		
		case 1:
		var totalTonne = 12.7;
		break;
		
		case 2:
		var totalTonne = 23.6;
		break;
		
		case 3:
		var totalTonne = 34.6;
		break;
		
		case 4:
		var totalTonne = 26.2;
		break;
		
		case 5:
		var totalTonne = 3.5;
		break;
		
		
		}
	
	}
	
	var totalPrice =  totalTonne * 20;
	totalPrice = totalPrice * theForm.elements['event_fx'].value
	
	// Set form variables
	theForm.elements['event_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['event_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_event").innerText = totalTonne.toFixed(1) + "t"
		document.getElementById("result_price_event").innerText = cSymbol + totalPrice.toFixed(0);
	}else{
		document.getElementById("result_tonnes_event").textContent = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_event").textContent = cSymbol + totalPrice.toFixed(0) ;
	}
	
}

// Event Calculations
function lifeCalculation(){
	
	var theForm = document.getElementById("life_calculation");
	var cSymbol = theForm.elements['life_curr'].value;
	var cAdults = theForm.elements['adults'].value;
	var cMeals = theForm.elements['meals'].value;
	var cRubbish = theForm.elements['rubbish'].value;

	// recycle period
	if(theForm.elements['recycle'][0].checked){
	var cRecycle = 0.8;
	}else{
		var cRecycle = 1;
	}
	
	// rate
	if(theForm.elements['rate'][0].checked){
	var cRate = 15000;
	}else if(theForm.elements['rate'][1].checked){
		var cRate = 10000;
	}else{
		var cRate = 5000;
	}
	
	var fErrors = new Array();
	var fProcess;
	
	// Loop through and display the errors
	if(fProcess == false){
	 
	 for(i=0; i<fErrors.length; i++){
		 if(i==0){
			var theErrors = fErrors[i] + "\n";
		 }else{
		 theErrors += fErrors[i] + "\n";
		 }
	 }
	 alert("Please enter\n\n" + theErrors);
	 return;

	}
	
	// The calculation
	var subtotalA = cMeals * 1.5 * 52 * 1.28;
	var subtotalB = (((cRubbish/100) * 240) / 8) * 52
	var totalTonne = (cAdults * (subtotalA + subtotalB + cRate) * cRecycle) / 1000;
	var totalPrice = totalTonne * 20;
	totalPrice = totalPrice * theForm.elements['life_fx'].value

	// Set form variables
	theForm.elements['life_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['life_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_life").innerText = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_life").innerText = cSymbol + totalPrice.toFixed(0);
	}else{
		document.getElementById("result_tonnes_life").textContent = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_life").textContent = cSymbol + totalPrice.toFixed(0);
	}
	
}

// Life Presets
function lifePreset(){
	
	var theForm = document.getElementById("life_calculation");
	var cSymbol = theForm.elements['life_curr'].value;
	
	// Check for selected preset
	if(theForm.elements['preset'].selectedIndex!=0){
	
		switch (theForm.elements['preset'].value){
		
		case "Single - Low":
		var totalTonne = 7;
		break;
		
		case "Single - Average":
		var totalTonne = 13;
		break;
		
		case "Single - High":
		var totalTonne = 23;
		break;
		
		case "3 Person Family - Low":
		var totalTonne = 16;
		break;
		
		case "3 Person Family - Avg":
		var totalTonne = 29;
		break;
		
		case "3 Person Family - High":
		var totalTonne = 48;
		break;
		
		case "Couple - Low":
		var totalTonne = 12;
		break;
		
		case "Couple - Avg":
		var totalTonne = 22;
		break;
		
		case "Couple - High":
		var totalTonne = 40;
		break;
		
		}
	
	}
	
	var totalPrice =  totalTonne * 20;
	totalPrice = totalPrice * theForm.elements['life_fx'].value
	
	// Set form variables
	theForm.elements['life_tonnes'].value = totalTonne.toFixed(1);
	theForm.elements['life_price'].value = totalPrice.toFixed(0);
	
	if(document.all){
		document.getElementById("result_tonnes_life").innerText = totalTonne.toFixed(1) + "t"
		document.getElementById("result_price_life").innerText = cSymbol + totalPrice.toFixed(0);
	}else{
		document.getElementById("result_tonnes_life").textContent = totalTonne.toFixed(1) + "t";
		document.getElementById("result_price_life").textContent = cSymbol + totalPrice.toFixed(0) ;
	}
	
}

function exactAmount(){
	var theForm = document.getElementById("exact_calculation");
	theForm.elements['price'].value = '';
	theForm.elements['capita'].selectedIndex = 0;
}

function exactPrice(){
	var theForm = document.getElementById("exact_calculation");
	theForm.elements['amount'].value = '';
	theForm.elements['capita'].selectedIndex = 0;
}

function exactCapita(){
	var theForm = document.getElementById("exact_calculation");
	theForm.elements['price'].value = '';
	theForm.elements['amount'].value = '';
}

/* Flight Calc Stuff */

function setFlightDistance(){
	var theForm = document.getElementById("flight_calculation");
	var to = theForm.elements['to_airport'].value;
	var from = theForm.elements['from_airport'].value;
	
	if(theForm.elements['to_airport'].selectedIndex!=0){
	var refpage = document.location
	var newpage = "/flightdistance.php?to=" + to + "& from=" + from + "&ref=" + refpage;
	refpage.href = newpage;
	}
	
}
