﻿/****** 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)
    {
        loadOpt1JrnyTyp();
        loadOpt1TravelMethod();
    };
}

/****** 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 (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 == '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 loadOpt1TravelMethod()
{
    var selectID = 'ddlOpt1TravelMethod';
    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 == 'Non-Regular Commuter')
                { // this is the data item we want
                    var listItems = options.item(i).getElementsByTagName('TravelMethod');
                    // 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('EmissionFactor'));
                    }
                }
            };
        }
    }
}
function calculateOption1()
{
    if (document.getElementById)
    {
        var ddlOpt1JnyTyp = document.getElementById('ddlOpt1JnyTyp');
        var ddlOpt1TravelMethod = document.getElementById('ddlOpt1TravelMethod');
        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 * ddlOpt1TravelMethod.value;
        
        // 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 ******/
