﻿/****** REGION: Constants ******/
var xmlDataDocIndHousehold = "";
/****** END REGION: Constants ******/

function loadIndHouseHold()
{
    loadOpt1Yrs();
    loadOpt2Yrs();
    loadXmlDataDoc();
}
function loadXmlDataDoc()
{
    feed_id = 0;
    feed_total = 0;
    feed_file = individualFlightXML;
    
    if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
        xmlDataDocIndHousehold = new ActiveXObject("Microsoft.XMLDOM");
        xmlDataDocIndHousehold.async = "true";
        xmlDataDocIndHousehold.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDocIndHousehold.load(individualHouseholdXML);
    } else if (window.XMLHttpRequest) {
        xmlDataDocIndHousehold = new XMLHttpRequest();
        xmlDataDocIndHousehold.onreadystatechange = verifyXmlDataLoaded;
        xmlDataDocIndHousehold.open("GET", individualHouseholdXML, true);
        xmlDataDocIndHousehold.send(null);
    }
    return true;
}
function verifyXmlDataLoaded()
{
    if (xmlDataDocIndHousehold.readyState == 4)
    {
        loadOpt2AbodeType();
        loadOpt2CentralHeating();
        loadOpt2EnergySaving();
        loadOpt2EnergySupply();
    };
}

/****** REGION: Option 1 methods ******/
function loadOpt1Yrs()
{
    for (i = 1; i <= 30; i++) {
        AddListOption('ddlOpt1Yrs', i.toString(), i.toString());
    }
}
function calculateOption1()
{
    var txtElectricity = document.getElementById('txtElectricity');
    var txtGreenTarrifElec = document.getElementById('txtGreenTarrifElec');
    var txtGas = document.getElementById('txtGas');
    var txtBottledGas = document.getElementById('ctl00_cphPrimary_txtBottledGasVolume');
    var blnUseGasEnergy = false;
    if(txtBottledGas.value == '')
    {
        blnUseGasEnergy = true;
        txtBottledGas = document.getElementById('ctl00_cphPrimary_txtBottledGasEnergy');
    }
    var txtHeatingOil = document.getElementById('ctl00_cphPrimary_txtHeatingOilVolume');
    var blnUseHeatingOilEnergy = false;
    if(txtHeatingOil.value == '')
    {
        blnUseHeatingOilEnergy = true;
        txtHeatingOil = document.getElementById('ctl00_cphPrimary_txtHeatingOilEnergy');
        
    }
    var txtCoal = document.getElementById('txtCoal');
    var txtWood = document.getElementById('txtWood');
    var ddlOpt1Yrs = document.getElementById('ddlOpt1Yrs');
    
    var result = txtElectricity.value * householdEmissionElectricityEmissionFactor;
    result = parseFloat(result + (txtGreenTarrifElec.value * householdEmissionGreenTarrifEmissionFactor));
    result = parseFloat(result + (txtGas.value * householdEmissionGasEmissionFactor));
    result = parseFloat(result + (txtBottledGas.value * (blnUseGasEnergy ? householdEmissionBottledGasEmissionFactorForVolume : householdEmissionBottledGasEmissionFactor)));
    result = parseFloat(result + (txtHeatingOil.value * (blnUseHeatingOilEnergy ? householdEmissionHeatingOilEmissionFactorForVolume : householdEmissionHeatingOilEmissionFactor)));
    result = parseFloat(result + (txtCoal.value * householdEmissionCoalEmissionFactor));
    result = parseFloat(result + (txtWood.value *householdEmissionWoodEmissionFactor));
    result = parseFloat(result * ddlOpt1Yrs.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 ******/

/****** REGION: Option 2 methods ******/
function loadOpt2Yrs()
{
    for (i = 1; i <= 30; i++) {
        AddListOption('ddlOpt2Yrs', i.toString(), i.toString());
    }
}
function loadOpt2AbodeType()
{
    var selectID = 'ddlOpt2AbodeType';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlDataDocIndHousehold.responseXML) 
        {
            xmlDataDocIndHousehold = xmlDataDocIndHousehold.responseXML;
        }
        if (xmlDataDocIndHousehold.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndHousehold.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 == 'Household Size And Type')
                { // this is the data item we want
                    var listItems = options.item(i).getElementsByTagName('AbodeType');
                    // 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'));
                    }
                }
            };
        }
        else {
            AddListOption(selectID, 'Error loading data', '');
        };
    };
}
function loadOpt2CentralHeating()
{
    var selectID = 'ddlOpt2CentralHeating';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlDataDocIndHousehold.responseXML) 
        {
            xmlDataDocIndHousehold = xmlDataDocIndHousehold.responseXML;
        }
        if (xmlDataDocIndHousehold.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndHousehold.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 == 'Household Size And Type')
                { // this is the data item we want
                    var listItems = options.item(i).getElementsByTagName('CentralHeatingFactor');
                    // 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'));
                    }
                }
            };
        }
        else {
            AddListOption(selectID, 'Error loading data', '');
        };
    };
}
function loadOpt2EnergySaving()
{
    var selectID = 'ddlOpt2EnergySaving';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlDataDocIndHousehold.responseXML) 
        {
            xmlDataDocIndHousehold = xmlDataDocIndHousehold.responseXML;
        }
        if (xmlDataDocIndHousehold.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndHousehold.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 == 'Household Size And Type')
                { // this is the data item we want
                    var listItems = options.item(i).getElementsByTagName('EnergySavingFactor');
                    // 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'));
                    }
                }
            };
        }
        else {
            AddListOption(selectID, 'Error loading data', '');
        };
    };
}
function loadOpt2EnergySupply()
{
    var selectID = 'ddlOpt2EnergySupply';
    if (document.getElementById(selectID))
    {
        // clear previous values out first.
        RemoveListOptions(selectID);
        
        // iterate through the xml document getting the data
        if (xmlDataDocIndHousehold.responseXML) 
        {
            xmlDataDocIndHousehold = xmlDataDocIndHousehold.responseXML;
        }
        if (xmlDataDocIndHousehold.getElementsByTagName('Option').length > 0) {
            // add a default "Select" option
            AddListOption(selectID, 'Select...', '');
            
            var options = xmlDataDocIndHousehold.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 == 'Household Size And Type')
                { // this is the data item we want
                    var listItems = options.item(i).getElementsByTagName('EnergySupplyFactor');
                    // 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'));
                    }
                }
            };
        }
        else {
            AddListOption(selectID, 'Error loading data', '');
        };
    };
}
function calculateOption2() {
    var ddlOpt2AbodeType = document.getElementById('ddlOpt2AbodeType');
    var ddlOpt2People = document.getElementById('ddlOpt2People');
    var txtOpt2Bedrooms = document.getElementById('txtOpt2Bedrooms');
    var ddlOpt2CentralHeating = document.getElementById('ddlOpt2CentralHeating');
    var ddlOpt2EnergySaving = document.getElementById('ddlOpt2EnergySaving');
    var ddlOpt2EnergySupply = document.getElementById('ddlOpt2EnergySupply');
    var ddlOpt2Yrs = document.getElementById('ddlOpt2Yrs');
    
    // calculate the emissions with all the values from controls, and 
    var nPeoplePlusBedrooms = parseInt(ddlOpt2People.value) + parseInt(txtOpt2Bedrooms.value);
    var result = ddlOpt2AbodeType.value * ddlOpt2CentralHeating.value * ddlOpt2EnergySaving.value * ddlOpt2EnergySupply.value * nPeoplePlusBedrooms * ddlOpt2Yrs.value * householdTypeAndSizeEmissionFactor;
        
        // 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 ******/
