﻿function validate_option1()
{
    var alertText = "To calculate your emissions you must:\r\n";
    var option1IsValid = true;
    if (document.getElementById)
    {
        // validate departure airport for required value
        if (document.getElementById('ddlOpt1DptrApt').value == "") {
            option1IsValid = false;
            alertText += " - Select the departure airport for this flight\r\n";
        }
        
        // validate destination airport for required value
        if (document.getElementById('ddlOpt1DestApt').value == "") {
            option1IsValid = false;
            alertText += " - Select the destination airport for this flight\r\n";
        }
        
        // validate departure & destination airports are not the same
        if (document.getElementById('ddlOpt1DptrApt').value == document.getElementById('ddlOpt1DestApt').value)
        {
            option1IsValid = false;
            alertText += " - Departure and destination airports cannot be the same\r\n";
        }
        
        // validate number of passengers for required value
        if (document.getElementById('ddlOpt1Psgr').value == "") {
            option1IsValid = false;
            alertText += " - Select the number of passengers for this flight\r\n";
        }
        
        // validate type of flight for required value
        if (document.getElementById('ddlOpt1JnyTyp').value == "") {
            option1IsValid = false;
            alertText += " - Select type of flight; one-way or return\r\n";
        }
        
        // validate flight regularity for required value
        if (document.getElementById('ddlOpt1RegFlt').value == "") {
            option1IsValid = false;
            alertText += " - Select flight regularity; regular or charter\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 emissions you must:\r\n";
    var option2IsValid = true;
    if (document.getElementById)
    {
        // validate number of passengers for required field
        if (document.getElementById('ddlOpt2Psgr').value == "") {
            option2IsValid = false;
            alertText += " - Select number of passengers to calculate for\r\n";
        }
        
        // validate journey type for required field - not needed, all regular flights are 2-way
//                if (document.getElementById('ddlOpt2JnyTyp').value == "") {
//                    option2IsValid = false;
//                    alertText += " - Select journey type; one-way or return\r\n";
//                }
        
        // set variables to use for numeric & flight count validation
        var flightCountValid = false;
        var numericExpression = /^[0-9]+$/;
        
        // validate number of domestic flights per annum for required and numeric value
        if (document.getElementById('txtDomFltsPA').value.length > 0)
        {
            flightCountValid = true;
            if (!document.getElementById('txtDomFltsPA').value.match(numericExpression)) {
                option2IsValid = false;
                alertText += " - Enter the number of domestic flights in numeric format\r\n";
            }
        }
        
        // validate number of short haul flights per annum for required and numeric value
        if (document.getElementById('txtSHaulFltsPA').value.length > 0)
        {
            flightCountValid = true;
            if (!document.getElementById('txtSHaulFltsPA').value.match(numericExpression)) {
                option2IsValid = false;
                alertText += " - Enter the number of short haul flights in numeric format\r\n";
            }
        }
        
        // validate number of long haul flights per annum for required and numeric value
        if (document.getElementById('txtLHaulFltsPA').value.length > 0)
        {
            flightCountValid = true;
            if (!document.getElementById('txtLHaulFltsPA').value.match(numericExpression)) {
                option2IsValid = false;
                alertText += " - Enter the number of long haul flights in numeric format\r\n";
            }
        }
        
        // check that at least one type of flight has been entered
        if (!flightCountValid) {
            option2IsValid = false;
            alertText += " - Enter number of flights for domestic, short haul or long haul\r\n";
        }
        
        // return the option2IsValid, alert user if option2IsValid == false with alertText
        if (option2IsValid) {
            return true;
        } else {
            alert(alertText);
            return false;
        }
    }
}
