// JavaScript Document
<!--
//******************************
// Determines the age of the person on Jan 1 in the year of DOV
function yearsOld(dob,theDov){
   birthday = new Date (dob.getYear(), dob.getMonth(), dob.getDate());
   dov = new Date (dov.getYear(), 0,1);
   years = dov.getFullYear() - birthday.getFullYear();
   birthday = new Date (dov.getYear(), dob.getMonth(), dob.getDate());
   // If your birthday hasn't occurred yet this year, subtract 1.
   if(dov < birthday){
      years-- ;
   }
   return (years)
}

   
//******************************
function checkForm(theForm) {
    var msg = "The following errors were found in the input form: \n\n";
    var inputsNotOk = false;
		var todaysDate = new Date();

    if (!dateIsValid(theForm.DobYear.value, theForm.DobMonth.selectedIndex+1, theForm.DobDay.value))
    {
        msg += "Date of Birth: Please enter a valid date (e.g. January 23 1998).\n\n";
        inputsNotOk = true;
    }

    if (!dateIsValid(theForm.DovYear.value, theForm.DovMonth.selectedIndex+1, theForm.DovDay.value))
        {
            msg += "Date of Valuation: Please enter a valid date (e.g. January 23 1998).\n\n";
            inputsNotOk = true;
    }

    if (theForm.annualDeficit.value == "")
    {
        msg += "Annual Deficit: Please enter a number!\n\n";
        inputsNotOk = true;
    }

    if (isNaN(theForm.annualDeficit.value))
    {
        msg += "Annual Deficit: Please enter a number!\n\n";
        inputsNotOk = true;
    }

    if (theForm.endAge.value == "")
    {
        msg += "Ending Age: Please enter a number!\n\n";
        inputsNotOk = true;
    }

    if (isNaN(theForm.endAge.value) && !theForm.useWorkingLifeExpectancy[0].checked)
    {
        msg += "Ending Age: Please enter a number!\n\n";
        inputsNotOk = true;
    }
    
    if (!isNaN(theForm.endAge.value) && theForm.includeMortalityAdjustment[0].checked)
    {
        if (Number(theForm.endAge.value) > 108)
        {
        	msg += "Ending Age: Mortality rates are only available to age 108. Either reduce the ending age or exclude the mortality adjustment.\n\n";
        	inputsNotOk = true;
        }
    }    

    discountRate = Number(theForm.discountRate.value);
    if (isNaN(discountRate)) {
        msg += "Discount Rate: Please enter a percentage!  The percentage should be entered without the % sign.\n\n";
        inputsNotOk = true;
    }
    else if (discountRate < 0) {
        msg += "Discount Rate: The discount rate cannot be a negative number.\n\n";
        inputsNotOk = true;
    }

    growthRate = Number(theForm.growthRate.value);
    if (isNaN(growthRate)) {
        msg += "Growth Rate: Please enter a percentage!  The percentage should be entered without the % sign.\n\n";
        inputsNotOk = true;
    }
    else if (growthRate < 0) {
        msg += "Growth Rate: Must be greater than 0.\n\n";
        inputsNotOk = true;
    }

    dob = new Date(theForm.DobYear.value,
                  parseInt(theForm.DobMonth.selectedIndex),
                  theForm.DobDay.value);
    dov = new Date(theForm.DovYear.value,
                  parseInt(theForm.DovMonth.selectedIndex),
                  theForm.DovDay.value);

    if (dov < dob) {
        msg += "Date of Valuation: It must be later than the date of birth.\n\n";
        inputsNotOk = true;
    }

    if (dob > todaysDate) {
        msg += "Date of Birth: It must be earlier than today's date.\n\n";
        inputsNotOk = true;
    }    

    if (!isNaN(theForm.endAge.value)) {
      if(yearsOld(dob, dov) >= parseInt(theForm.endAge.value)) {
        msg += "Ending Age: The 'Ending Age' is less than the age the individual will be at the 'Date of Valuation'.\nPlease adjust the 'Date of Valuation' or the 'Ending Age' accordingly.\n\n";
        inputsNotOk = true;
      }
    }        
    
    if (inputsNotOk) {
        alert(msg);
        return false;
    }

    // Re-enable the disabled controls before submit, otherwise, the values
    // of the disabled controls are not submitted at all.
    theForm.eduLevel.disabled = false;
    theForm.endAge.disabled = false;
}




function useWorkingLifeExpectancy_onclick() {
    var theForm = document.formPresentValueInput;
    if (theForm.useWorkingLifeExpectancy[0].checked) {
        theForm.eduLevel.disabled = false;
        theForm.endAge.disabled = true;
        theForm.endAge.value = "65"
    } else {
        theForm.eduLevel.disabled = true;
        theForm.endAge.disabled = false;
    }
}

// -->
