﻿/****** REGION: Constants ******/
var xmlDataDocIndFlight = "";
var xmlAirportDataDoc = "";
/****** END REGION: Constants ******/


function loadXmlDataDoc()
{
    feed_id = 0;
    feed_total = 0;
    feed_file = individualFlightXML;
    
    if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
        xmlDataDocIndFlight = new ActiveXObject("Microsoft.XMLDOM");
        xmlDataDocIndFlight.async = "true";
        xmlDataDocIndFlight.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDocIndFlight.load(individualFlightXML);
    } else if (window.XMLHttpRequest) {
        xmlDataDocIndFlight = new XMLHttpRequest();
        xmlDataDocIndFlight.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDocIndFlight.open("GET", individualFlightXML, true);
        xmlDataDocIndFlight.send(null);
    }
    return true;
}
function verifyXmlDataLoaded()
{
    if (xmlDataDocIndFlight.readyState == 4)
    {
        loadOption1Psgr();
        loadOption1JnyTyp();
        loadOption1RegFlt();
        loadOption2Psgr();
        loadOption2JnyTyp();
    };
}


function loadXmlAirportDataDoc()
{
    feed_id = 0;
    feed_total = 0;
    feed_file = individualFlightXML;
    
    if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
        xmlAirportDataDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlAirportDataDoc.async = "true";
        xmlAirportDataDoc.onreadystatechange = verifyXmlAirportDataLoaded;
        xmlAirportDataDoc.load(airportXML);
    } else if (window.XMLHttpRequest) {
        xmlAirportDataDoc = new XMLHttpRequest();
        xmlAirportDataDoc.onreadystatechange = verifyXmlAirportDataLoaded;
        xmlAirportDataDoc.open("GET", airportXML, true);
        xmlAirportDataDoc.send(null);
    }
    return true;
}
function verifyXmlAirportDataLoaded()
{
    if (xmlAirportDataDoc.readyState == 4)
    {
        loadOption1DptrApt();
        loadOption1DstApt();
    };
}


/****** REGION: Option 1 Methods ******/
function loadOption1DptrApt()
{
    var selectID = 'ddlOpt1DptrApt';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlAirportDataDoc.responseXML) 
        {
            xmlAirportDataDoc = xmlAirportDataDoc.responseXML;
        }
        if (xmlAirportDataDoc.getElementsByTagName('airport').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');            
            
            var airports = xmlAirportDataDoc.getElementsByTagName('airport');
            for (var i = 0; i < airports.length; i++)
            {
                var airportName = airports.item(i).getAttribute('city') + " - " + airports.item(i).childNodes[0].nodeValue;
                var airportLat = convertLatToMetric(airports.item(i).getAttribute('latDeg'), airports.item(i).getAttribute('latMin'), 
                    airports.item(i).getAttribute('latSec'), airports.item(i).getAttribute('latDirection'));
                var airportLong = convertLongToMetric(airports.item(i).getAttribute('longDeg'), airports.item(i).getAttribute('longMin'), 
                    airports.item(i).getAttribute('longSec'), airports.item(i).getAttribute('longDirection'));
                var airportLatLon = airportLat + "," + airportLong;
                AddListOption(selectID, airportName, airportLatLon);
            };
        }
        else {
            AddListOption(selectID, 'Error loading data', '');
        };
    };
}
function loadOption1DstApt()
{
    var selectID = 'ddlOpt1DestApt';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlAirportDataDoc.responseXML) 
        {
            xmlAirportDataDoc = xmlAirportDataDoc.responseXML;
        }
        if (xmlAirportDataDoc.getElementsByTagName('airport').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');            
            
            var airports = xmlAirportDataDoc.getElementsByTagName('airport');
            for (var i = 0; i < airports.length; i++)
            {
                var airportName = airports.item(i).getAttribute('city') + " - " + airports.item(i).childNodes[0].nodeValue;
                var airportLat = convertLatToMetric(airports.item(i).getAttribute('latDeg'), airports.item(i).getAttribute('latMin'), 
                    airports.item(i).getAttribute('latSec'), airports.item(i).getAttribute('latDirection'));
                var airportLong = convertLongToMetric(airports.item(i).getAttribute('longDeg'),airports.item(i).getAttribute('longMin'), 
                    airports.item(i).getAttribute('longSec'), airports.item(i).getAttribute('longDirection'));
                var airportLatLon = airportLat + "," + airportLong;
                AddListOption(selectID, airportName, airportLatLon);
            };
        }
        else {
            AddListOption(selectID, 'Error loading data', '');
        };
    };
}
function loadOption1Psgr()
{
    var selectID = 'ddlOpt1Psgr';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting data
        if (xmlDataDocIndFlight.responseXML) {
            xmlDataDocIndFlight = xmlDataDocIndFlight.responseXML;
        }
        if (xmlDataDocIndFlight.getElementsByTagName('Option').length > 0) {
            // add a default "Select..." option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndFlight.getElementsByTagName('Option');
            for (var i = 0; i < options.length; i++)
            {
                // check the "Name" attribute of the data item
                var optionName = options.item(i).getAttribute('Name');
                if (optionName == 'Non-Regular Flyer')
                { // this is the data item we want
                    var psgrCnts = options.item(i).getElementsByTagName('PassengerCount');
                    // add the data items to the list
                    for (var iPCs = 0; iPCs < psgrCnts.length; iPCs++)
                    {
                        AddListOption(selectID, psgrCnts.item(iPCs).firstChild.nodeValue, 
                            psgrCnts.item(iPCs).firstChild.nodeValue);
                    }
                }
            };
        }
    }
}
function loadOption1JnyTyp()
{
    var selectID = 'ddlOpt1JnyTyp';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting data
        if (xmlDataDocIndFlight.responseXML) {
            xmlDataDocIndFlight = xmlDataDocIndFlight.responseXML;
        }
        if (xmlDataDocIndFlight.getElementsByTagName('Option').length > 0) {
            // add a default "Select..." option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndFlight.getElementsByTagName('Option');
            for (var i = 0; i < options.length; i++)
            {
                // check the "Name" attribute of the data item
                var optionName = options.item(i).getAttribute('Name');
                if (optionName == 'Non-Regular Flyer')
                { // this is the data item we want
                    var jnyTyps = options.item(i).getElementsByTagName('JourneyType');
                    // add the data items to the list
                    for (var iPCs = 0; iPCs < jnyTyps.length; iPCs++)
                    {
                        AddListOption(selectID, jnyTyps.item(iPCs).firstChild.nodeValue, 
                            jnyTyps.item(iPCs).getAttribute('UpliftFactor'));
                    }
                }
            };
        }
    }
}
function loadOption1RegFlt()
{
    var selectID = 'ddlOpt1RegFlt';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting data
        if (xmlDataDocIndFlight.responseXML) {
            xmlDataDocIndFlight = xmlDataDocIndFlight.responseXML;
        }
        if (xmlDataDocIndFlight.getElementsByTagName('Option').length > 0) {
            // add a default "Select..." option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndFlight.getElementsByTagName('Option');
            for (var i = 0; i < options.length; i++)
            {
                // check the "Name" attribute of the data item
                var optionName = options.item(i).getAttribute('Name');
                if (optionName == 'Non-Regular Flyer')
                { // this is the data item we want
                    var fltRegs = options.item(i).getElementsByTagName('FlightRegularity');
                    // add the data items to the list
                    for (var iPCs = 0; iPCs < fltRegs.length; iPCs++)
                    {
                        AddListOption(selectID, fltRegs.item(iPCs).firstChild.nodeValue, 
                            fltRegs.item(iPCs).getAttribute('UpliftFactor'));
                    }
                }
            };
        }
    }
}
function calculateOption1()
{
    if (document.getElementById)
    {
        var ddlOpt1DptrApt = document.getElementById('ddlOpt1DptrApt');
        var ddlOpt1DestApt = document.getElementById('ddlOpt1DestApt');
        var ddlOpt1Psgr = document.getElementById('ddlOpt1Psgr');
        var ddlOpt1JnyTyp = document.getElementById('ddlOpt1JnyTyp');
        var ddlOpt1RegFlt = document.getElementById('ddlOpt1RegFlt');
        
        // make the calculation based on the user's selections
        debugger;
        var airportDistance = calculateLatLongDistance(ddlOpt1DptrApt.value.split(',')[0], ddlOpt1DptrApt.value.split(',')[1],
            ddlOpt1DestApt.value.split(',')[0], ddlOpt1DestApt.value.split(',')[1]);
        
        var result = airportDistance;
        result = result * ddlOpt1Psgr.value;
        result = result * ddlOpt1JnyTyp.value;
        result = result * ddlOpt1RegFlt.value;
        result = result * nonRegularFlightKgCo2PerKmPerPassenger;
        
        // convert from kilo's to tonnes
        result = result / 1000;
        
        // round to two decimal places
        var roundedResult = Math.round(result*100)/100;
        // show the cost of this offset
        var roundedCost = (Math.round((roundedResult * costPerTonne)*100)/100).toFixed(2);
        document.getElementById('ctl00_cphPrimary_hfOpt1Result').value = roundedResult;
        
        
        if(document.all)
        {
            document.getElementById('divOpt1Result').innerHTML = roundedResult.toString();
            document.getElementById('divOpt1Cost').innerText = roundedCost;
        }
        else
        {
            document.getElementById('divOpt1Result').textContent = roundedResult.toString();
            document.getElementById('divOpt1Cost').textContent = roundedCost;
        }
    }
}

function calculateLatLongDistance(lat1, long1, lat2, long2)
{
    var R = 6371; // km
    var dLat = (lat2-lat1) * Math.PI / 180;
    var dLon = (long2-long1) * Math.PI / 180; 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * 
            Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c;
    return d;
}
function convertLatToMetric(degree, minutes, seconds, compassDirection)
{
    degree = parseFloat(degree);
    minutes = parseFloat(minutes / 60);
    seconds = parseFloat(seconds / 3600);
    var newLat = parseFloat(degree + minutes + seconds);
    if (compassDirection.toLowerCase() == "s") newLat = "-" + newLat;    
    return newLat;
}
function convertLongToMetric(degree, minutes, seconds, compassDirection)
{
    degree = parseFloat(degree);
    minutes = parseFloat(minutes / 60);
    seconds = parseFloat(seconds / 3600);
    var newLon = parseFloat(degree + minutes + seconds);
    if (compassDirection.toLowerCase() == "w") newLon = "-" + newLon;
    return newLon;
}
/****** END REGION: Option 1 Methods ******/


/****** REGION: Option 2 Methods ******/
function loadOption2Psgr()
{
    var selectID = 'ddlOpt2Psgr';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting data
        if (xmlDataDocIndFlight.responseXML) {
            xmlDataDocIndFlight = xmlDataDocIndFlight.responseXML;
        }
        if (xmlDataDocIndFlight.getElementsByTagName('Option').length > 0) {
            // add a default "Select..." option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndFlight.getElementsByTagName('Option');
            for (var i = 0; i < options.length; i++)
            {
                // check the "Name" attribute of the data item
                var optionName = options.item(i).getAttribute('Name');
                if (optionName == 'Regular Flyer')
                { // this is the data item we want
                    var psgrCnts = options.item(i).getElementsByTagName('PassengerCount');
                    // add the data items to the list
                    for (var iPCs = 0; iPCs < psgrCnts.length; iPCs++)
                    {
                        AddListOption(selectID, psgrCnts.item(iPCs).firstChild.nodeValue, 
                            psgrCnts.item(iPCs).firstChild.nodeValue);
                    }
                }
            };
        }
    }
}
function loadOption2JnyTyp()
{
    var selectID = 'ddlOpt2JnyTyp';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting data
        if (xmlDataDocIndFlight.responseXML) {
            xmlDataDocIndFlight = xmlDataDocIndFlight.responseXML;
        }
        if (xmlDataDocIndFlight.getElementsByTagName('Option').length > 0) {
            // add a default "Select..." option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndFlight.getElementsByTagName('Option');
            for (var i = 0; i < options.length; i++)
            {
                // check the "Name" attribute of the data item
                var optionName = options.item(i).getAttribute('Name');
                if (optionName == 'Regular Flyer')
                { // this is the data item we want
                    var jnyTyps = options.item(i).getElementsByTagName('JourneyType');
                    // add the data items to the list
                    for (var iPCs = 0; iPCs < jnyTyps.length; iPCs++)
                    {
                        AddListOption(selectID, jnyTyps.item(iPCs).firstChild.nodeValue, 
                            jnyTyps.item(iPCs).getAttribute('UpliftFactor'));
                    }
                }
            };
        }
    }
}
function calculateOption2()
{
    if (document.getElementById)
    {
        var ddlOpt2Psgr = document.getElementById('ddlOpt2Psgr');
        //var ddlOpt2JnyTyp = document.getElementById('ddlOpt2JnyTyp');
        var txtDomFltsPA = document.getElementById('txtDomFltsPA');
        var txtSHaulFltsPA = document.getElementById('txtSHaulFltsPA');
        var txtLHaulFltsPA = document.getElementById('txtLHaulFltsPA');
        
        // make the calculation based on the user's selections
        var result = ddlOpt2Psgr.value;
        //result = result * ddlOpt2JnyTyp.value;
        //all regular flights are considered 2-way
        result *= 2;
        
        
        var kgsCO2 = 0;
        if (txtDomFltsPA.value.length > 0) {
            kgsCO2 = txtDomFltsPA.value * domesticFlightDistance * domesticFlightKgCo2PerKmPerPassenger;
        }
        
        if (txtLHaulFltsPA.value.length > 0) {
            kgsCO2 = kgsCO2 + (txtSHaulFltsPA.value * shortHaulFlightDistance * shortHaulFlightKgCo2PerKmPerPassenger);
        }
        
        if (txtLHaulFltsPA.value.length > 0) {
            kgsCO2 = kgsCO2 + (txtLHaulFltsPA.value * longHaulFlightDistance * longHaulFlightKgCo2PerKmPerPassenger);
        }
        
        result = result * kgsCO2;
        
        // convert from kilo's to tonnes
        result = result / 1000;
        
        // round to two decimal places
        var roundedResult = Math.round(result*100)/100;
        // show the cost of this offset
        var roundedCost = (Math.round((roundedResult * costPerTonne)*100)/100).toFixed(2);
        document.getElementById('ctl00_cphPrimary_hfOpt2Result').value = roundedResult;
        
        if(document.all)
        {        
            document.getElementById('divOpt2Result').innerText = roundedResult;        
            document.getElementById('divOpt2Cost').innerText = roundedCost;
        }
        else
        {
            document.getElementById('divOpt2Result').textContent = roundedResult;        
            document.getElementById('divOpt2Cost').textContent = roundedCost;
        }
    }
}
/****** END REGION: Option 2 Methods ******/
