﻿/****** REGION: Constants ******/
var xmlDataDocMotorCycle = "";
var selectIdFlightCountries = "";
/****** END REGION: Constants ******/

function loadXmlDataDoc()
{
    feed_id = 0;
    feed_total = 0;
    feed_file = individualMotorcycleXML;
    
    if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
        xmlDataDocMotorCycle = new ActiveXObject("Microsoft.XMLDOM");
        xmlDataDocMotorCycle.async = "true";
        xmlDataDocMotorCycle.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDocMotorCycle.load(individualMotorcycleXML);
    } else if (window.XMLHttpRequest) {
        xmlDataDocMotorCycle = new XMLHttpRequest();
        xmlDataDocMotorCycle.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDocMotorCycle.open("GET", individualMotorcycleXML, true);
        xmlDataDocMotorCycle.send(null);
    }
    return true;
}
function verifyXmlDataLoaded()
{
    //alert('xmlDoc readyState: ' + xmlDataDocMotorCycle.readyState);
    if (xmlDataDocMotorCycle.readyState == 4)
    {
        loadOption1CityDriving();
        loadOption2CityDriving();
        loadOption2EngineSizes();
    };
}


/****** REGION: Option 1 Methods ******/
function loadOption1CityDriving()
{
    var selectID = 'ctl00_cphPrimary_ddlOpt1CityDriving';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlDataDocMotorCycle.responseXML) 
        {
            xmlDataDocMotorCycle = xmlDataDocMotorCycle.responseXML;
        }
        if (xmlDataDocMotorCycle.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');            
            
            var options = xmlDataDocMotorCycle.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 == 'Fuel Usage Per Litre')
                { // this is the data item we want
                    var fuelTypes = options.item(i).getElementsByTagName('CityDrivingOption');
                    // add the data items to the list
                    for (var iFt = 0; iFt < fuelTypes.length; iFt++)
                    {
                        AddListOption(selectID, fuelTypes.item(iFt).firstChild.nodeValue, 
                            fuelTypes.item(iFt).getAttribute('MultiplicationFactor'));
                    }
                }
            };
        }
        else {
            AddListOption(selectID, 'Error loading data', '');
        };
    };
}
function calculateOption1()
{
    if (document.getElementById)
    {
        var txtLitres = document.getElementById('ctl00_cphPrimary_txtFuelLitres');
        var ddlFqy = document.getElementById('ctl00_cphPrimary_ddlOpt1Fqcy');
        var ddlCityDriving = document.getElementById('ctl00_cphPrimary_ddlOpt1CityDriving');
        
        // make the calculation based on the user's selections
        var result = 2.32 * txtLitres.value;
        result = result * ddlFqy.value;
        result = result * ddlCityDriving.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('divOp1Result').innerHTML = roundedResult.toString();
            document.getElementById('divOpt1Cost').innerText = roundedCost;
        }
        else
        {
            document.getElementById('divOp1Result').textContent = roundedResult.toString();
            document.getElementById('divOpt1Cost').textContent = roundedCost;
        }
        
    }
}
/****** END REGION: Option 1 Methods ******/


/****** REGION: Option 2 Methods ******/
function loadOption2EngineSizes()
{
    var selectID = 'ctl00_cphPrimary_ddlOpt2EngineSize';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlDataDocMotorCycle.responseXML) 
        {
            xmlDataDocMotorCycle = xmlDataDocMotorCycle.responseXML;
        }
        if (xmlDataDocMotorCycle.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');            
            
            var options = xmlDataDocMotorCycle.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 == 'Mileage Per Annum')
                { // this is the data item we want
                    var fuelTypes = options.item(i).getElementsByTagName('EngineSize');
                    // add the data items to the list
                    for (var iFt = 0; iFt < fuelTypes.length; iFt++)
                    {
                        AddListOption(selectID, fuelTypes.item(iFt).firstChild.nodeValue, 
                            fuelTypes.item(iFt).getAttribute('EmissionFactor'));
                    }
                }
            };
        }
        else {
            AddListOption(selectID, 'Error loading data', '');
        };
    };
}
function loadOption2CityDriving()
{
    var selectID = 'ctl00_cphPrimary_ddlOpt2CityDriving';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlDataDocMotorCycle.responseXML) 
        {
            xmlDataDocMotorCycle = xmlDataDocMotorCycle.responseXML;
        }
        if (xmlDataDocMotorCycle.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');            
            
            var options = xmlDataDocMotorCycle.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 == 'Mileage Per Annum')
                { // this is the data item we want
                    var fuelTypes = options.item(i).getElementsByTagName('CityDrivingOption');
                    // add the data items to the list
                    for (var iFt = 0; iFt < fuelTypes.length; iFt++)
                    {
                        AddListOption(selectID, fuelTypes.item(iFt).firstChild.nodeValue, 
                            fuelTypes.item(iFt).getAttribute('MultiplicationFactor'));
                    }
                }
            };
        }
        else {
            AddListOption(selectID, 'Error loading data', '');
        };
    };
}
function calculateOption2()
{
    if (document.getElementById)
    {
        var ddlEngineSize = document.getElementById('ctl00_cphPrimary_ddlOpt2EngineSize');
        var txtMileage = document.getElementById('ctl00_cphPrimary_txtOpt2Mileage');
        
        var blnUseKilometres = false;
        if(txtMileage.value == '')
        {
            txtMileage = document.getElementById('ctl00_cphPrimary_txtOpt2Kilometres');
            blnUseKilometres = true;
        }
        
        var ddlCityDriving = document.getElementById('ctl00_cphPrimary_ddlOpt2CityDriving');
        
        // make the calculation based on the user's selections
        var result = ddlEngineSize.options[ddlEngineSize.selectedIndex].value;
        result = result *  (txtMileage.value * (blnUseKilometres ? 1 : KilometresToMilesConversionFactor));
        result = result * ddlCityDriving.options[ddlCityDriving.selectedIndex].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_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 ******/
