﻿function CheckDropDownListDateValid(source, arguments) {

    regex = /(.*)DateFormatCustomValidator/;

    matches = regex.exec(source.id)

    DateControlsID = matches[1];

    DayDropDownList = document.getElementById(DateControlsID + "DayDropDownList");

    SelectedDay = DayDropDownList.options[DayDropDownList.selectedIndex].value;

    MonthDropDownList = document.getElementById(DateControlsID + "MonthDropDownList");

    SelectedMonth = MonthDropDownList.options[MonthDropDownList.selectedIndex].value;

    YearDropDownList = document.getElementById(DateControlsID + "YearDropDownList");

    SelectedYear = YearDropDownList.options[YearDropDownList.selectedIndex].value;

    if (SelectedDay == '' && SelectedMonth == '' && SelectedYear == '') {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = isDate(SelectedDay, SelectedMonth, SelectedYear);
    }
}


function CheckTextBoxDateValid(source, arguments) 
{
    regex = /(.*)DateFormatCustomValidator/;

    matches = regex.exec(source.id)

    DateControlsID = matches[1];

    DayTextBox = document.getElementById(DateControlsID + "DayTextBox");

    SelectedDay = DayTextBox.value;

    MonthTextBox = document.getElementById(DateControlsID + "MonthTextBox");

    SelectedMonth = MonthTextBox.value;

    YearTextBox = document.getElementById(DateControlsID + "YearTextBox");

    SelectedYear = YearTextBox.value;

    if (SelectedDay == '' && SelectedMonth == '' && SelectedYear == '') {
        arguments.IsValid = true;
    }
    else if (SelectedDay == 'DD' && SelectedMonth == 'MM' && SelectedYear == 'YYYY') {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = isDate(SelectedDay, SelectedMonth, SelectedYear);
    }
}

function isDate(day, month, year) {
    var mSeconds;
    var objDate;

    month = month - 1; //Javascript months are 0-11

    mSeconds = (new Date(year, month, day)).getTime();

    objDate = new Date();
    objDate.setTime(mSeconds);

    // compare input parameter date and created Date() object  
    // if difference exists then date isn't valid  
    if (objDate.getFullYear() != year) return false;
    if (objDate.getMonth() != month) return false;
    if (objDate.getDate() != day) return false;

    // otherwise return true  
    return true;
} 
