﻿function validate_option1()
{
    var alertText = "To calculate staff offset you must:\r\n";
    var option1IsValid = true;
    if (document.getElementById)
    {
        var numericExpression = /^[0-9]*$/;
        var numericExpressionReq = /^[0-9]+$/; // one or more numeric value
        
        // validate number of members of staff for numeric value > 0
        if (!document.getElementById("txtOpt1StaffNumber").value.match(numericExpressionReq)) {
            option1IsValid = false;
            alertText += " - Enter number of members of staff (at least 1)\r\n";
        }
        
        // validate number of years for numeric value < 26
        if (!document.getElementById('txtOpt1Yrs').value.match(numericExpression)) {
            option1IsValid = false;
            alertText += " - Enter number of years to offset in numeric format\r\n";
        }
        else {
            if (parseInt(document.getElementById('txtOpt1Yrs').value) < 1 || parseInt(document.getElementById('txtOpt1Yrs').value) > 25) {
                option1IsValid = false;
                alertText += " - Enter number of years in numeric format between 1 and 25\r\n";
            }
        }
        
        // validate age for numeric value < 100
        if (!document.getElementById('txtOpt1Age1').value.match(numericExpression)) {
            option1IsValid = false;
            alertText += " - Enter the age value in numeric format\r\n";
        }
        else {
            if (parseInt(document.getElementById('txtOpt1Age1').value) < 18 || parseInt(document.getElementById('txtOpt1Age1').value) > 100) {
                option1IsValid = false;
                alertText += " - Enter the age in numeric format between 16 and 100\r\n";
            }
        }
        
        // validate the emission estimate for required value
        if (document.getElementById('ddlOpt1EstEmis').value == "") {
            option1IsValid = false;
            alertText += " - Select the estimated emission rate\r\n";
        }
        
        // validate either age or number of years has value
        var valueLength = parseInt(document.getElementById('txtOpt1Yrs').value.length) + 
            parseInt(document.getElementById('txtOpt1Age1').value.length);
        if (valueLength > 0) {
            // validate only age or number of years is present
            if (document.getElementById('txtOpt1Yrs').value.length > 0 && document.getElementById('txtOpt1Age1').value.length > 0) 
            {
                option1IsValid = false;
                alertText += " - Remove either age or number of years - cannot have both\r\n";
            }
        }
        else {
            option1IsValid = false;
            alertText += " - Enter either how many years to offset, or their age (average if more than one)\r\n";
        }
        
        
        // return the option1IsValid, alert user if option1IsValid == false with alertText
        if (option1IsValid) {
            return true;
        } else {
            alert(alertText);
            return false;
        }
    }
}
