﻿/****** REGION: Constants ******/
var xmlDataDocIndRail = "";
/****** END REGION: Constants ******/


function loadXmlDataDoc()
{
    feed_id = 0;
    feed_total = 0;
    feed_file = individualFlightXML;
    
    if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
        xmlDataDocIndRail = new ActiveXObject("Microsoft.XMLDOM");
        xmlDataDocIndRail.async = "true";
        xmlDataDocIndRail.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDocIndRail.load(individualRailXML);
    } else if (window.XMLHttpRequest) {
        xmlDataDocIndRail = new XMLHttpRequest();
        xmlDataDocIndRail.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDocIndRail.open("GET", individualRailXML, true);
        xmlDataDocIndRail.send(null);
    }
    return true;
}
function verifyXmlDataLoaded()
{
    if (xmlDataDocIndRail.readyState == 4)
    {
        loadOpt1JrnyLength();
    };
}

/****** REGION: Option 1 Methods ******/
function loadOpt1JrnyLength()
{
    var selectID = 'ddlOpt1JrnyLength';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting data
        if (xmlDataDocIndRail.responseXML) {
            xmlDataDocIndRail = xmlDataDocIndRail.responseXML;
        }
        if (xmlDataDocIndRail.getElementsByTagName('Option').length > 0) {
            // add a default "Select..." option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndRail.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 Commuter')
                { // this is the data item we want
                    var listItems = options.item(i).getElementsByTagName('JourneyLength');
                    // add the data items to the list
                    for (var iLi = 0; iLi < listItems.length; iLi++)
                    {
                        AddListOption(selectID, listItems.item(iLi).firstChild.nodeValue, 
                            listItems.item(iLi).getAttribute('NumericValue'));
                    }
                }
            };
        }
    }
}
function calculateOption1()
{
    if (document.getElementById)
    {
        var txtOpt1NumCommuters = document.getElementById('txtOpt1NumCommuters');
        var ddlOpt1JrnyLength = document.getElementById('ddlOpt1JrnyLength');
        var ddlOpt1DaysPerWeek = document.getElementById('ddlOpt1DaysPerWeek');
        
        // make the calculation based on the user's selections        
        var result = 0.06; // commuting emission factor
        result = result * txtOpt1NumCommuters.value * 52.1; // 52.1 = weeks per year
        result = result * ddlOpt1DaysPerWeek.value * ddlOpt1JrnyLength.value * 35; // 35 = 35kph
        
        // convert from kilo's to tonnes
        result = result / 1000;
        
        debugger;
        // round to two decimal places
        var roundedResult = Math.round(result*100)/100;
        document.getElementById('ctl00_cphPrimary_hfOpt1Result').value = roundedResult;
        
        // show the cost of this offset
        var roundedCost = (Math.round((roundedResult * costPerTonne)*100)/100).toFixed(2);
        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;
        }
        
    }
}
/****** END REGION: Option 1 Methods ******/

/****** REGION: Option 2 Methods ******/
function calculateOption2() {
    if (document.getElementById){
        var txtOpt2Journeys = document.getElementById('txtOpt2Journeys');
        var txtOpt2Distance = document.getElementById('txtOpt2DistanceMileage');
        
        var blnUseKilometres = false;
        if(txtOpt2Distance.value == '')
        {
            blnUseKilometres = true;
            
            txtOpt2Distance = document.getElementById('txtOpt2DistanceKilometres')
        }
        
        // convert miles to kilometers
        var distanceInKm = txtOpt2Distance.value * (blnUseKilometres ? 1 : KilometresToMilesConversionFactor);
        
        // make the calculation
        result = 0.25; // 0.25 = kgs of emissions per kilometre
        result = result * 12; // 12 = 12 months
        result = result * txtOpt2Journeys.value * distanceInKm;
        
        // convert from kilo's to tonnes
        result = result / 1000;
        
        // round to two decimal places
        var roundedResult = Math.round(result*100)/100;
        var roundedCost = (Math.round((roundedResult * costPerTonne)*100)/100).toFixed(2);
        
        document.getElementById('ctl00_cphPrimary_hfOpt2Result').value = roundedResult;
        
        if(document.all)
        {
            document.getElementById('divOpt2Result').innerHTML = roundedResult.toString();
            document.getElementById('divOpt2Cost').innerText = roundedCost;
        }
        else
        {
            document.getElementById('divOpt2Result').textContent = roundedResult.toString();
            document.getElementById('divOpt2Cost').textContent = roundedCost;
        }
    }
}
/****** REGION: Option 2 Methods ******/
