﻿/* JavaScript developed by errumm ltd : www.errumm.co.uk

 * 

 * (C) Copyright 2007, Down with Carbon ltd. 

 * 

*/



    function CalculateFlightCO2(ouputId, outputId2)

    {

        var depId = document.getElementById("ddlDeparture").value;

        var viaId = document.getElementById("ddlVia").value;

        var arrId = document.getElementById("ddlArrival").value;

        var rtn = document.getElementById("chkReturn").checked;

        var nPassengers = document.getElementById("ddlNumberOfPassengers").value;

        

        var AviationCalc = new AviationCalculator(depId, viaId, arrId, rtn, nPassengers);

        

        var CO2Tonnes = Math.round(DecimalValue(AviationCalc.CalculateC02()/ 1000)*10)/10 ;

         

        if(isNaN(CO2Tonnes))

            SetDocText(ouputId, "We could not calculate your cabonr dioxide for your journey, please try again.");

        else

        {

            SetDocText(ouputId, "Equivalent total tonnes carbon dioxide for actual journey to nearest 0.1 tonne : <h1>" + CO2Tonnes + " Tonnes </h1>");

            document.getElementById(outputId2).value = CO2Tonnes;

        }

    }



    function AviationCalculator(depId, viaId, arrId, isReturn, passengerCount)

    {

        this.DepartureId, this.ViaId, this.ArrivalId, this.ReturnFlight; this.PassengerCount;

        

        this.DepartureId = depId;

        this.ViaId = viaId;

        this.ArrivalId = arrId;

        this.ReturnFlight = isReturn;

        this.pi = Math.PI;

 		this.PassengerCount = passengerCount;

        

        //

        this.DepLatRad = this.RadiusCalculation(LAT[this.DepartureId]);

        this.ViaLatRad = this.RadiusCalculation(LAT[this.ViaId]);

        this.ArrLatRad = this.RadiusCalculation(LAT[this.ArrivalId]);

        

        //

        this.DepLonRad = this.RadiusCalculation(LON[this.DepartureId]);

        this.ViaLonRad = this.RadiusCalculation(LON[this.ViaId]);

        this.ArrLonRad = this.RadiusCalculation(LON[this.ArrivalId]);



        //

        this.DepLatRad1 = this.DepLatRad;

        this.ViaLatRad1 = this.ViaLatRad;

        this.ArrLatRad1 = this.DepLatRad;



        //

        this.DepLatRad2 = this.ViaLatRad;

        this.ViaLatRad2 = this.ArrLatRad;

        this.ArrLatRad2 = this.ArrLatRad;

        

        //

        this.DepLonRad1 = this.DepLonRad;

        this.ViaLonRad1 = this.ViaLonRad;

        this.ArrLonRad1 = this.DepLonRad;



        //

        this.DepLonRad2 = this.ViaLonRad;

        this.ViaLonRad2 = this.ArrLonRad;

        this.ArrLonRad2 = this.ArrLonRad;

        

        //

        this.DepdLatRad = this.ViaLatRad - this.DepLatRad;

        this.ViadLatRad = this.ArrLatRad - this.ViaLatRad;

        this.ArrdLatRad = this.DepLatRad - this.ArrLatRad;

        

        //

        this.DepdLonRad = this.ViaLonRad - this.DepLonRad;

        this.ViadLonRad = this.ArrLonRad - this.ViaLonRad;

        this.ArrdLonRad = this.DepLonRad - this.ArrLonRad;        

        

        //

        this.DepAglRad = this.CalculateAngle(this.DepdLatRad, this.DepLatRad1, this.DepLatRad2, this.DepdLonRad);

        this.ViaAglRad = this.CalculateAngle(this.ViadLatRad, this.ViaLatRad1, this.ViaLatRad2, this.ViadLonRad);

        this.ArrAglRad = this.CalculateAngle(this.ArrdLatRad, this.ArrLatRad1, this.ArrLatRad2, this.ArrdLonRad);

        

        //

        this.DepDisKM = this.CalculateDistance(this.DepAglRad, false);        

        this.ViaDisKM = this.CalculateDistance(this.ViaAglRad, false);

        this.ArrDisKM = this.CalculateDistance(this.ArrAglRad, false);

        

        //

        this.DepHaulType = this.HaulType(this.DepDisKM);

        this.ViaHaulType = this.HaulType(this.ViaDisKM);

        this.ArrHaulType = this.HaulType(this.ArrDisKM);

        

        //

        this.DepCO2OutwardJourney = this.CalculateHaulCO2(this.DepHaulType, this.DepDisKM);

        this.ViaCO2OutwardJourney = this.CalculateHaulCO2(this.ViaHaulType, this.ViaDisKM);

        this.ArrCO2OutwardJourney = this.CalculateHaulCO2(this.ArrHaulType, this.ArrDisKM);



        //

        this.DepCO2TotalJourney = this.CalculateTotalJourneyCO2(this.ReturnFlight, this.DepCO2OutwardJourney);

        this.ViaCO2TotalJourney = this.CalculateTotalJourneyCO2(this.ReturnFlight, this.ViaCO2OutwardJourney);

        this.ArrCO2TotalJourney = this.CalculateTotalJourneyCO2(this.ReturnFlight, this.ArrCO2OutwardJourney);



    }

    

    AviationCalculator.prototype.CalculateC02 = function()

    {

        if(this.ViaId == -1)

            this.TotalC02 = this.ArrCO2TotalJourney * this.PassengerCount;

        else

            this.TotalC02 = (this.ViaCO2TotalJourney + this.DepCO2TotalJourney) * this.PassengerCount;

          

  

		return this.TotalC02;

    }

    

    

    AviationCalculator.prototype.RadiusCalculation = function(LongtitudeDegrees)

    {

        try

        {    

            return (LongtitudeDegrees * this.pi / 180);

        }

        catch(err)

        {

            alert(err + ": " + RadiusCalculation + ": LongtitudeDegrees = " + LongtitudeDegrees);

        }

    }

    

    AviationCalculator.prototype.CalculateAngle = function(difLatRad, latRad1, latRad2, difLonRad)

    {

        try

        {

            return 2 * Math.asin(Math.sqrt((Math.pow(Math.sin(difLatRad/2),2))+Math.cos(latRad1)*Math.cos(latRad2)*(Math.pow(Math.sin(difLonRad/2),2))));

        }

        catch(err)

        {

            alert(err + ":: difLatRad = " + difLatRad + ": latRad1 = " + latRad1 + ": LatRad2 = " + LatRad2 + " : = difLonRad" + difLonRad);

        }

    }

    

    AviationCalculator.prototype.CalculateDistance = function(angleRad , Miles)

    {

        if(!Miles)

            return angleRad * 6367;

        else

            return (this.CalculateDistance(angleRad, true) * 5/8);

    }

    

    AviationCalculator.prototype.HaulType = function(distanceKM)

    {

        if(distanceKM <= 3000)

            return "SHORT";

        else if(distanceKM <= 6500)

            return "MEDIUM";

        else

            return "LONG";                        

    }

    

    AviationCalculator.prototype.CalculateHaulCO2 = function(haulType, distanceKM)

    {

        switch(haulType)

        {

            case "SHORT":

                return (28.91 + 0.1183 * distanceKM - 0.00000301 * Math.pow(distanceKM , 2));

            break;

            

            case "MEDIUM":

                return (48.36 + 0.1836 * distanceKM - 0.000001276 * Math.pow(distanceKM , 2));

            break;

            

            case "LONG":

                return (43.52 + 0.1652 * distanceKM - 0.000001148 * Math.pow(distanceKM , 2));

            break;

            

            default:

                alert("couldn't ascertain which haul type to use.");

                        

        }

    }

    

    AviationCalculator.prototype.CalculateTotalJourneyCO2 = function(returnFlight, CO2)

    {

        if(returnFlight)        

            return (CO2 * 2);

        else

            return CO2;

    }

    





//--> START populate the dropdowns

var oddlDeparture = document.getElementById("ddlDeparture");

var oddlVia = document.getElementById("ddlVia");

var oddlArrival = document.getElementById("ddlArrival");





if(oddlDeparture != null)

    for (var i=0; i < lsz;++i){

        addOption(oddlDeparture, LOCATION[i], i);

    }

else

    alert('oddlDeparture == null')    ;



if(oddlVia != null)

    for (var i=0; i < lsz;++i){

        addOption(oddlVia, LOCATION[i], i);

    }

else

    alert('oddlVia == null')    ;

    

if(oddlArrival != null)

    for (var i=0; i < lsz;++i){

        addOption(oddlArrival, LOCATION[i], i);

    }

else

    alert('oddlArrival == null')    ;



function addOption(selectbox,text,value )

{

    try{

        var optn = document.createElement("OPTION");

        optn.text = text;

        optn.value = value;

        selectbox.options.add(optn);

    }

    catch(err)

    {

        alert(err + "selectbox : " + selectbox + ", text: " + text + ", value:" + value);

    }

}



//--> END populate the dropdowns



function DecimalValue(value)

{

    return Math.round(value*100)/100  

}



function DecimalAsString(value)

{

    var i = parseFloat(value);

    if(isNaN(i)) { i = 0.00; }

    var minus = '';

    if(i < 0) { minus = '-'; }

    i = Math.abs(i);

    i = parseInt((i + .005) * 100);

    i = i / 100;

    s = new String(i);

    if(s.indexOf('.') < 0) { s += '.00'; }

    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }

    s = minus + s;

    return s;

}



function SetDocText(id, value){

    document.getElementById(id).innerHTML = value;

}



