﻿function validate_option1()
{
    var alertText = "To calculate your household fuel emissions you must:\r\n";
    var option1IsValid = true;
    if (document.getElementById)
    {
    
        var numericExpression = /^[0-9]+$/;
        // validate electricity for numeric value
        if (!document.getElementById('txtElectricity').value.match(numericExpression)) {
            option1IsValid = false;
            alertText += " - Enter electricity usage in numeric format\r\n";
        }
        
        // validate green tarrif electricity for numeric value
        if (!document.getElementById('txtGreenTarrifElec').value.match(numericExpression)) {
            option1IsValid = false;
            alertText += " - Enter green taffic electricity usage in numeric format\r\n";
        }
        
        // validate gas for numeric value
        if (!document.getElementById('txtGas').value.match(numericExpression)) {
            option1IsValid = false;
            alertText += " - Enter gas usage in numeric format\r\n";
        }
        
        // validate bottled gas for numeric value
        var txtOption1GasVolume = document.getElementById('ctl00_cphPrimary_txtBottledGasVolume');
        var txtOption1GasEnergy = document.getElementById('ctl00_cphPrimary_txtBottledGasEnergy');
        if ((!txtOption1GasVolume.value.match(numericExpression)) && (!txtOption1GasEnergy.value.match(numericExpression))) {
            option1IsValid = false;
            alertText += " - Enter bottled gas usage, litres OR kWh, in numeric format\r\n";
        } else if ((txtOption1GasVolume.value.match(numericExpression)) && (txtOption1GasEnergy.value.match(numericExpression))) {
            option1IsValid = false;
            alertText += " - Enter bottled gas usage, litres OR kWh, in numeric format\r\n";
        }
        
        // validate heating oil for numeric value
        if ((!document.getElementById('ctl00_cphPrimary_txtHeatingOilVolume').value.match(numericExpression)) && (!document.getElementById('ctl00_cphPrimary_txtHeatingOilEnergy').value.match(numericExpression))){
            option1IsValid = false;
            alertText += " - Enter heating oil usage, litres OR kWh, in numeric format\r\n";
        } else if ((document.getElementById('ctl00_cphPrimary_txtHeatingOilVolume').value.match(numericExpression)) && (document.getElementById('ctl00_cphPrimary_txtHeatingOilEnergy').value.match(numericExpression))){
            option1IsValid = false;
            alertText += " - Enter heating oil usage, litres OR kWh, in numeric format\r\n";
        }
        
        // validate coal for numeric value
        if (!document.getElementById('txtCoal').value.match(numericExpression)) {
            option1IsValid = false;
            alertText += " - Enter coal usage in numeric format\r\n";
        }
        
        // validate wood for numeric value
        if (!document.getElementById('txtWood').value.match(numericExpression)) {
            option1IsValid = false;
            alertText += " - Enter wood usage in numeric format\r\n";
        }
        
        // validate at least one entry exists
        var enteredValues = parseInt(document.getElementById('txtElectricity').value.length + 
            document.getElementById('txtGreenTarrifElec').value.length + 
            document.getElementById('txtGas').value.length + 
            document.getElementById('ctl00_cphPrimary_txtBottledGasVolume').value.length + 
            document.getElementById('ctl00_cphPrimary_txtBottledGasEnergy').value.length + 
            document.getElementById('ctl00_cphPrimary_txtHeatingOilVolume').value.length + 
            document.getElementById('ctl00_cphPrimary_txtHeatingOilEnergy').value.length + 
            document.getElementById('txtCoal').value.length + 
            document.getElementById('txtWood').value.length);
        if (enteredValues < 1) {
            option1IsValid = false;
            alertText += " - Enter a usage amount for at least 1 fuel type\r\n";
        }
        
        // validate number of years for required value
        if (document.getElementById('ddlOpt1Yrs').value == "") {
            option1IsValid = false;
            alertText += " - Select how many years to offset\r\n";
        }
        
        // return the option1IsValid, alert user if option1IsValid == false with alertText
        if (option1IsValid) {
            return true;
        } else {
            alert(alertText);
            return false;
        }
    }
}
function validate_option2()
{
    var alertText = "To calculate your household emissions by type and size you must:\r\n";
    var option2IsValid = true;
    if (document.getElementById)
    {   	        
        var numericExpression = /^[0-9]+$/;
        // validate type of abode for required value
        if (document.getElementById('ddlOpt2AbodeType').value == "") {
            option2IsValid = false;
            alertText += " - Select house or flat\r\n";
        }
        
        // validate number of people for required value
        if (document.getElementById('ddlOpt2People').value == "") {
            option2IsValid = false;
            alertText += " - Select number of people in the residence\r\n";
        }
        
        // validate number of rooms for numeric value
        if (!document.getElementById('txtOpt2Bedrooms').value.match(numericExpression)) {
            option2IsValid = false;
            alertText += " - Enter number of rooms in the residence in numeric format\r\n";
        }
        
        // validate central heating for required value
        if (document.getElementById('ddlOpt2CentralHeating').value == "") {
            option2IsValid = false;
            alertText += " - Indicate whether central heating is used in the residence\r\n";
        }
        
        // validate energy saving for required value
        if (document.getElementById('ddlOpt2EnergySaving').value == "") {
            option2IsValid = false;
            alertText += " - Indicate whether energy saving measures are used in the residence\r\n";
        }
        
        // validate energy supply for required value
        if (document.getElementById('ddlOpt2EnergySupply').value == "") {
            option2IsValid = false;
            alertText += " - Select the energy supply used in the residence\r\n";
        }
        
        // validate years to offset for required value
        if (document.getElementById('ddlOpt2Yrs').value == "") {
            option2IsValid = false;
            alertText += " - Select how many years to offset\r\n";
        }
        
        
        // return the option1IsValid, alert user if option1IsValid == false with alertText
        if (option2IsValid) {
            return true;
        } else {
            alert(alertText);
            return false;
        }
    }
}
