﻿function validate_option1()
{
    var alertText = "To calculate a wedding gift offset you must:\r\n";
    var option1IsValid = true;
    if (document.getElementById)
    {
        var numericExpression = /^[0-9]+$/;
        // validate celebration for required value
        if (document.getElementById('ddlOpt1Celeb').value == "") 
        {
            option1IsValid = false;
            alertText += " - Indicate whether to include offset for celebrations\r\n";
        }
        

        // validate number of years for numeric value < 26
        if ((document.getElementById('txtOpt1Yrs').value != '') && (!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) > 0)
        {
            //check it's in range 1 - 25
            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";
            }
        }        
        
        //need to check if all 3 boxes are filled, should one exclusive
        if ((document.getElementById('txtOpt1Age1').value != '') || (document.getElementById('txtOpt1Age2').value != ''))
        {                        	        
            // validate first age for numeric value < 100
            if (!document.getElementById('txtOpt1Age1').value.match(numericExpression)) 
            {
                option1IsValid = false;
                alertText += " - Enter the first age in numberic format\r\n";
            }
            else 
            {
                if ((parseInt(document.getElementById('txtOpt1Age1').value) < 18 || parseInt(document.getElementById('txtOpt1Age1').value) > 100)) 
                {
                    option1IsValid = false;
                    alertText += " - Enter the first age in numeric format between 18 and 100\r\n";
                }
            }
	        
            // validate second age for numeric value < 100
            if (!document.getElementById('txtOpt1Age2').value.match(numericExpression)) 
            {
                option1IsValid = false;
                alertText += " - Enter the second age in numberic format\r\n";
            }
            else
            {
                if ((parseInt(document.getElementById('txtOpt1Age2').value) < 18) || (parseInt(document.getElementById('txtOpt1Age2').value) > 100)) {
                    option1IsValid = false;
                    alertText += " - Enter the second age in numeric format between 18 and 100\r\n";
                }
            }        
        }
        
        // validate the emission estimate for required value
        if (document.getElementById('ddlOpt1EstEmis').value == "") 
        {
            option1IsValid = false;
            alertText += " - Select the couples estimated emissions\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 a birthday gift you must:\r\n";
    var option2IsValid = true;
    if (document.getElementById)
    {
        var numericExpression = /^[0-9]+$/;
        // validate celebration for required value
        if (document.getElementById('ddlOpt2Celeb').value == "") {
            option2IsValid = false;
            alertText += " - Indicate whether to include offset for celebrations\r\n";
        }
        
        if(parseInt(document.getElementById('txtOpt2Yrs').value) > 0)
        {
            // validate number of years for numeric value < 26
            if (!document.getElementById('txtOpt2Yrs').value.match(numericExpression)) 
            {
                option2IsValid = false;
                alertText += " - Enter number of years to offset in numeric format\r\n";
            }
            else 
            {
                if ((parseInt(document.getElementById('txtOpt2Yrs').value) < 1) || (parseInt(document.getElementById('txtOpt2Yrs').value) > 25)) 
                {
                    option2IsValid = false;
                    alertText += " - Enter number of years in numeric format between 1 and 25\r\n";
                }
            }                       
        }
        else
        {    	        
            // validate age for numeric value < 100
            if (!document.getElementById('txtOpt2Age').value.match(numericExpression)) {
                option2IsValid = false;
                alertText += " - Enter their first age in numberic format\r\n";
            }
            else {
                if (parseInt(document.getElementById('txtOpt2Age').value) > 100) {
                    option2IsValid = false;
                    alertText += " - Enter their age in numeric format less than 100\r\n";
                }
            }
        }
        
        // validate the emission estimate for required value
        if (document.getElementById('ddlOpt2EstEmis').value == "") 
        {
            option2IsValid = false;
            alertText += " - Select the person's estimated emissions\r\n";
        }
        
        // validate either age or number of years has value
        if ((parseInt(document.getElementById('txtOpt2Yrs').value) > 0) && (parseInt(document.getElementById('txtOpt2Age').value) > 0))
        {
            option2IsValid = false;
            alertText += " - Remove either how many years to offset, or the person's age. Can't have both\r\n";
        }        
        
        // return the option2IsValid, alert user if option2IsValid == false with alertText
        if (option2IsValid) {
            return true;
        } else {
            alert(alertText);
            return false;
        }
    }
}
function validate_option3()
{
    var alertText = "To calculate a new baby gift you must:\r\n";
    var option3IsValid = true;
    if (document.getElementById)
    {
        var numericExpression = /^[0-9]+$/;
        
        if(document.getElementById('txtOpt3YrsBaby').value != '')
        {
            // validate baby's years for numeric value < 26
            if (!document.getElementById('txtOpt3YrsBaby').value.match(numericExpression)) 
            {
                option3IsValid = false;
                alertText += " - Enter number of years to offset for the baby in numeric format\r\n";
            }
            else 
            {
                if(parseInt(document.getElementById('txtOpt3YrsBaby').value) > 0)
                {
                    if ((parseInt(document.getElementById('txtOpt3YrsBaby').value) < 1) || (parseInt(document.getElementById('txtOpt3YrsBaby').value) > 25))
                    {
                        option3IsValid = false;
                        alertText += " - Enter number of years for the baby in numeric format between 1 and 25\r\n";
                    }
                }
            }
        }
  
        if(document.getElementById('txtOpt3YrsParents').value != '')
        {
            // validate age for numeric value < 100
            if (!document.getElementById('txtOpt3YrsParents').value.match(numericExpression))
            {
                option3IsValid = false;
                alertText += " - Enter number of years to offset for the parents in numberic format\r\n";
            }
            else 
            {
                if (parseInt(document.getElementById('txtOpt3YrsParents').value) > 0)
                {
                    if ((parseInt(document.getElementById('txtOpt3YrsParents').value) < 1) || (parseInt(document.getElementById('txtOpt3YrsParents').value) > 25))
                    {
                        option3IsValid = false;
                        alertText += " - Enter number of years for the parents in numeric format between 1 and 25\r\n";
                    }
                }
            }
        }

        // validate the emission estimate for required value if txtOpt3YrsBaby has a value
        if (((parseInt(document.getElementById('txtOpt3YrsParents').value) > 0) || (parseInt(document.getElementById('txtOpt3YrsBaby').value) > 0)) && (parseInt(document.getElementById('ddlOpt3EstEmis').value) == 0))
        {
            option3IsValid = false;
            alertText += " - Select the parent's estimated emissions\r\n";
        }
        
        // validate either age or number of years has value
        var valueLength = parseInt(document.getElementById('txtOpt3YrsBaby').value) + parseInt(document.getElementById('txtOpt3YrsParents').value);
        if (valueLength <= 0) {
            option3IsValid = false;
            alertText += " - Enter how many years to offset for either the baby or parents\r\n";
        }
        
        
        // return the option3IsValid, alert user if option3IsValid == false with alertText
        if (option3IsValid) {
            return true;
        } else {
            alert(alertText);
            return false;
        }
    }
}
function validate_option4()
{

    var alertText = "To calculate a gift you must:\r\n";
    var option4IsValid = true;
    if (document.getElementById)
    {
        var numericExpression = /^[0-9]+$/;
        
        
        if ((document.getElementById('txtOpt4Yrs').value != '') && (parseInt(document.getElementById('txtOpt4Yrs').value) > 0))
        {
            // validate number of years for numeric value < 26
            if (!document.getElementById('txtOpt4Yrs').value.match(numericExpression)) 
            {
                option4IsValid = false;
                alertText += " - Enter number of years to offset in numeric format\r\n";
            }
            else {
                if (parseInt(document.getElementById('txtOpt4Yrs').value) < 1 || parseInt(document.getElementById('txtOpt4Yrs').value) > 25) {
                    option4IsValid = false;
                    alertText += " - Enter number of years in numeric format between 1 and 25\r\n";
                }
            }               
        }
        else
        {
            // validate age for numeric value < 100
            if (!document.getElementById('txtOpt4Age').value.match(numericExpression)) {
                option4IsValid = false;
                alertText += " - Enter their age in numberic format\r\n";
            }
            else {
                if (parseInt(document.getElementById('txtOpt4Age').value) > 100) {
                    option4IsValid = false;
                    alertText += " - Enter their age in numeric format less than 100\r\n";
                }
            }                   
        }
        
        if((parseInt(document.getElementById('txtOpt4Yrs').value) > 0) && (parseInt(document.getElementById('txtOpt4Age').value) > 0))
        {
            //exclusive options, one or the other
            option4IsValid = false;
            alertText += " - Remove either how many years to offset, or the person's age. Can't have both\r\n";
        }
        else if((parseInt(document.getElementById('txtOpt4Yrs').value) == 0) && (parseInt(document.getElementById('txtOpt4Age').value) == 0))
        {
            option4IsValid = false;
            alertText += " - Enter either how many years to offset, or the person's age\r\n";
        }
        
        // validate the emission estimate for required value
        if (document.getElementById('ddlOpt4EstEmis').value == "") {
            option4IsValid = false;
            alertText += " - Select the person's estimated emissions\r\n";
        } 
              
        // return the option4IsValid, alert user if option4IsValid == false with alertText
        if (option4IsValid) {
            return true;
        } else {
            alert(alertText);
            return false;
        }
    }
}
