﻿function validate_option1()
{
    var alertText = "To calculate your emissions you must:\r\n";
    var option1IsValid = true;
    if (document.getElementById)
    {
        // validate Fuel litres for required & numeric values
        var numericExpression = /^[0-9]+$/;
        var txtFuelLitres = document.getElementById('ctl00_cphPrimary_txtFuelLitres');
        if (!txtFuelLitres.value.match(numericExpression)) {
            option1IsValid = false;
            alertText += " - Enter amount of litres in numeric format (e.g. \"50\")\r\n";
        }
        
        // validate city driving drop down for required value
        var ddlOpt1CityDriving = document.getElementById('ctl00_cphPrimary_ddlOpt1CityDriving');
        var cityDrivingVal = ddlOpt1CityDriving.value;
        if (cityDrivingVal == "Select..." || cityDrivingVal == "") {
            option1IsValid = false;
            alertText += " - Select amount of city driving you do\r\n";
        } else if (cityDrivingVal == "Loading data...") {
            option1IsValid = false;
            alert("There was an error loading data required for this page. Please refresh your browser and try again.");
            return false;
        }
        
        // 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 emissions you must:\r\n";
    var option2IsValid = true;
    if (document.getElementById)
    {                
        // validate engine size drop down for required value
        var ddlOpt2EngineSize = document.getElementById('ctl00_cphPrimary_ddlOpt2EngineSize');
        if (ddlOpt2EngineSize.value == "" || ddlOpt2EngineSize.value == "Select...") {
            option2IsValid = false;
            alertText += " - Select your motorcycle's engine size\r\n";
        } else if (ddlOpt2EngineSize.value == "Loading data...") {
            option2IsValid = false;
            alert("There was an error loading data required for this page. Please refresh your browser and try again.");
            return false;
        }
        
        // validate mileage per annum for required & numeric values
        var numericExpression = /^[0-9]+$/;
        var txtOpt2Mileage = document.getElementById('ctl00_cphPrimary_txtOpt2Mileage');
        var txtOpt2Kilometres = document.getElementById('ctl00_cphPrimary_txtOpt2Kilometres');
        if ((!txtOpt2Mileage.value.match(numericExpression))&& (!txtOpt2Kilometres.value.match(numericExpression))) {
            option2IsValid = false;
            alertText += " - Enter miles OR kilometres per annum in numeric format (e.g. \"12000\")\r\n";
        } else if ((txtOpt2Mileage.value.match(numericExpression))&& (txtOpt2Kilometres.value.match(numericExpression))) {
            option2IsValid = false;
            alertText += " - Enter miles OR kilometres per annum in numeric format (e.g. \"12000\")\r\n";
        }
        
        // validate city driving drop down for required value
        var ddlOpt2CityDriving = document.getElementById('ctl00_cphPrimary_ddlOpt2CityDriving');
        if (ddlOpt2CityDriving.value == "Select..." || ddlOpt2CityDriving.value == "") {
            option2IsValid = false;
            alertText += " - Select amount of city driving you do\r\n";
        } else if (ddlOpt2CityDriving.value == "Loading data...") {
            option2IsValid = false;
            alert("There was an error loading data required for this page. Please refresh your browser and try again.");
            return false;
        }
        
        // return the option1IsValid, alert user if option1IsValid == false with alertText
        if (option2IsValid) {
            return true;
        } else {
            alert(alertText);
            return false;
        }
    }
}
