﻿/****** REGION: Constants ******/
var xmlDataDocIndBus = "";
/****** END REGION: Constants ******/


function loadXmlDataDoc()
{
    feed_id = 0;
    feed_total = 0;
    feed_file = individualFlightXML;
    
    if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
        xmlDataDocIndBus = new ActiveXObject("Microsoft.XMLDOM");
        xmlDataDocIndBus.async = "true";
        xmlDataDocIndBus.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDocIndBus.load(individualBusXML);
    } else if (window.XMLHttpRequest) {
        xmlDataDocIndBus = new XMLHttpRequest();
        xmlDataDocIndBus.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDocIndBus.open("GET", individualBusXML, true);
        xmlDataDocIndBus.send(null);
    }
    return true;
}
function verifyXmlDataLoaded()
{
    if (xmlDataDocIndBus.readyState == 4)
    {
        loadOpt1JrnyTyp();
        loadOpt2JrnyLength();
    };
}

/****** REGION: Option 1 Methods ******/
function loadOpt1JrnyTyp()
{
    var selectID = 'ddlOpt1JnyTyp';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting data
        if (xmlDataDocIndBus.responseXML) {
            xmlDataDocIndBus = xmlDataDocIndBus.responseXML;
        }
        if (xmlDataDocIndBus.getElementsByTagName('Option').length > 0) {
            // add a default "Select..." option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndBus.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 Commuter')
                { // this is the data item we want
                    var listItems = options.item(i).getElementsByTagName('JourneyType');
                    // 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('UpliftFactor'));
                    }
                }
            };
        }
    }
}
function calculateOption1()
{
    if (document.getElementById)
    {
        var ddlOpt1JnyTyp = document.getElementById('ddlOpt1JnyTyp');
        var txtOpt1JrnyDist = document.getElementById('txtOpt1JrnyDistMileage');
        
        var blnUseKilometres = false;
        if(txtOpt1JrnyDist.value == '')
        {
            blnUseKilometres = true;
            txtOpt1JrnyDist = document.getElementById('txtOpt1JrnyDistKilometres')
        }
        // make the calculation based on the user's selections        
        var result = (txtOpt1JrnyDist.value * (blnUseKilometres ? 1 : KilometresToMilesConversionFactor));
        result = result * ddlOpt1JnyTyp.value;
        result = result * busCommuterEmissionsFactor;
        
        // 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;
        }
    }
}
/****** END REGION: Option 1 Methods ******/


/****** REGION: Option 2 Methods ******/
function loadOpt2JrnyLength()
{
    var selectID = 'ddlOpt2JrnyLength';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting data
        if (xmlDataDocIndBus.responseXML) {
            xmlDataDocIndBus = xmlDataDocIndBus.responseXML;
        }
        if (xmlDataDocIndBus.getElementsByTagName('Option').length > 0) {
            // add a default "Select..." option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndBus.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 calculateOption2()
{
    if (document.getElementById)
    {
        var ddlOpt2JrnyLength = document.getElementById('ddlOpt2JrnyLength');
        var ddlOpt2DaysPerWeek = document.getElementById('ddlOpt2DaysPerWeek');
        
        // make the calculation based on the user's selections
        var result = parseFloat(busCommuterEmissionsFactor); // emission factor for rail
        result = result * 52.1; // weeks per annum
        result = result * parseFloat(ddlOpt2JrnyLength.value); // average journey length
        result = result * ddlOpt2DaysPerWeek.value; // number of days travelled per week
        result = result * 35; // travelling an estimated 35 kilometres per hour
        
        // 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').innerHTML = roundedResult.toString();
            document.getElementById('divOpt2Cost').innerText = roundedCost;
        }
        else
        {
            document.getElementById('divOpt2Result').textContent = roundedResult.toString();
            document.getElementById('divOpt2Cost').textContent = roundedCost;
        }
    }
}
/****** END REGION: Option 2 Methods ******/
