﻿var ApplicantDayControl = "dd_day";
var ApplicantMonControl = "dd_month";
var ApplicantYearControl = "dd_year";

function cv_ApplicantDateofBirth_ClientValidate(source, arguments)
{
    var dateValid = true;
    dateValid = (checkValidDropDown('0',document.getElementById(source.controltovalidate.replace(ApplicantYearControl,ApplicantDayControl)))) ? dateValid : false ;
    dateValid = (checkValidDropDown('0',document.getElementById(source.controltovalidate.replace(ApplicantYearControl,ApplicantMonControl)))) ? dateValid : false ;
    dateValid = (checkValidDropDown('0',document.getElementById(source.controltovalidate))) ? dateValid : false;
    
    if(dateValid == true)
    {
        dateValid = IsValidDate(document.getElementById(source.controltovalidate.replace(ApplicantYearControl,ApplicantDayControl)).value,
                                document.getElementById(source.controltovalidate.replace(ApplicantYearControl,ApplicantMonControl)).value,
                                document.getElementById(source.controltovalidate).value);
    }                        
    
    arguments.IsValid = dateValid;
}       


function IsValidDate(day,month,year){

    var eStatus = true;
    //Determine if the given year is a leap year i.e. Feb=29
    var LeapYear = IsLeapYear(year);
    
    //if leap year then Feb can be no more than 29 days
    if(LeapYear) {
        if(month == 'Feb') {
            if(day > 29) {
                eStatus = false;
            }
        }
    }
    //if not leap year then Feb can be no more than 28 days
    if(!LeapYear) {
         if(month == 'Feb') {
            if(day > 28) {
                eStatus = false;
            }
        }
    }
    
    //Check for Apr,Jun,Sep,Nov are no more than 30
    if((month == 'Apr') || (month == 'Jun') || (month == 'Sep') || (month == 'Nov')) {
        if(day > 30) {
            eStatus = false;
        }
    }
    return eStatus;
}

function IsLeapYear(Year) {
    if ((Year % 4 == 0) || (Year % 100 == 0) || (Year % 400 == 0)) {
        return true;
    }
    else {
        return false;
    }
}

function checkValidDropDown(initialValue,obj)
{
    if(obj.value == initialValue) {
        return false;
    }
    else {
        return true;
    }
}
