﻿/****** REGION: Constants ******/
var xmlDataDoc = "";
var selectIdFlightCountries = "";
/****** END REGION: Constants ******/

function loadXmlDataDoc()
{
    feed_id = 0;
    feed_total = 0;
    feed_file = individualCarXML;
    
    if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
        xmlDataDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDataDoc.async = "true";
        xmlDataDoc.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDoc.load(individualCarXML);
    } else if (window.XMLHttpRequest) {
        xmlDataDoc = new XMLHttpRequest();
        xmlDataDoc.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDoc.open("GET", individualCarXML, true);
        xmlDataDoc.send(null);
    }
    return true;
}
function verifyXmlDataLoaded()
{
    //alert('xmlDoc readyState: ' + xmlDataDoc.readyState);
    if (xmlDataDoc.readyState == 4)
    {
        loadOption1FuelTypes();
        loadOption1CityDriving();
        loadOption2FuelTypes();
        loadOption2CityDriving();
    };
}


/****** REGION: Option 1 Methods ******/
function loadOption1FuelTypes()
{
    var selectID = 'ctl00_cphPrimary_ddlOpt1FuelType';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlDataDoc.responseXML) 
        {
            xmlDataDoc = xmlDataDoc.responseXML;
        }
        if (xmlDataDoc.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');            
            
            var options = xmlDataDoc.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('FuelType');
                    // 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 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 (xmlDataDoc.responseXML) 
        {
            xmlDataDoc = xmlDataDoc.responseXML;
        }
        if (xmlDataDoc.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');            
            
            var options = xmlDataDoc.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 ddlFuelType = document.getElementById('ctl00_cphPrimary_ddlOpt1FuelType');
        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 = ddlFuelType.options[ddlFuelType.selectedIndex].value;
        result = result * txtLitres.value;
        result = result * ddlFqy.options[ddlFqy.selectedIndex].value;
        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;        
        var roundedCost = (Math.round((roundedResult * costPerTonne)*100)/100).toFixed(2);
                
        document.getElementById('ctl00_cphPrimary_hfOpt1Result').value = roundedResult;
        if(document.all)
        {
            // show the tonnes        
            document.getElementById('divOp1Result').innerText = roundedResult;
                       
            // show the cost of this offset
            document.getElementById('divOp1Cost').innerText = roundedCost;
        }
        else
        {
            document.getElementById('divOp1Result').textContent = roundedResult;
            
            document.getElementById('divOp1Cost').textContent = roundedCost;
        }
    }
}

/****** END REGION: Option 1 Methods ******/


/****** REGION: Option 2 Methods ******/
function loadOption2FuelTypes()
{
    var selectID = 'ctl00_cphPrimary_ddlOpt2FuelType';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlDataDoc.responseXML) 
        {
            xmlDataDoc = xmlDataDoc.responseXML;
        }
        if (xmlDataDoc.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');            
            
            var options = xmlDataDoc.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('FuelType');
                    // add the data items to the list
                    for (var iFt = 0; iFt < fuelTypes.length; iFt++)
                    {
                        AddListOption(selectID, fuelTypes.item(iFt).getAttribute('Name'), 
                            fuelTypes.item(iFt).getAttribute('Name'));
                    }
                }
            };
        }
        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 (xmlDataDoc.responseXML) 
        {
            xmlDataDoc = xmlDataDoc.responseXML;
        }
        if (xmlDataDoc.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');            
            
            var options = xmlDataDoc.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 loadOption2EngineSizes(selectFuelType)
{
    // set selectID of engine <select>
    var selectID = 'ctl00_cphPrimary_ddlOpt2EngineSize';

    // get the selected value from the calling select
    var selectedValue = selectFuelType.options[selectFuelType.selectedIndex].value;
    
    // populate the target select with data
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the country's airports
        if (xmlDataDoc.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');            
            
            var options = xmlDataDoc.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('FuelType');
                    
                    // find the matching FuelType to selectedValue
                    for (var iFt = 0; iFt < fuelTypes.length; iFt++)
                    {
                        if (fuelTypes.item(iFt).getAttribute('Name') == selectedValue)
                        {
                            // this is the matching fuel type, get the EngineSizes and bind them to the select control
                            var engineSizes = fuelTypes.item(iFt).getElementsByTagName('EngineSize');
                            for (var iEs = 0; iEs < engineSizes.length; iEs++)
                            {
                                 AddListOption(selectID, engineSizes.item(iEs).firstChild.nodeValue, 
                                    engineSizes.item(iEs).getAttribute('EmissionFactor'));
                            }
                        }
                    }
                }
            };
        }
        else {
            AddListOption(selectID, 'Error loading data', '');
        };
    };
}

function calculateOption2()
{
    if (document.getElementById)
    {
        var ddlFuelType = document.getElementById('ctl00_cphPrimary_ddlOpt2FuelType');
        var ddlEngineSize = document.getElementById('ctl00_cphPrimary_ddlOpt2EngineSize');
        var txtMileage = document.getElementById('ctl00_cphPrimary_txtOpt2Mileage');
        var blnUseKilometres = false;
        if(txtMileage.value == '')
        {
            //grab the kilometres one
            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 ******/
