﻿//custom validation!
function ValidateRequiredText(oSrc, args) {
    var isValid = false;
    if (args.Value.trim() == "") {
        //salmon background color        
        document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "#ffcccc";
        if (document.getElementById(oSrc.controltovalidate).type == "text") {
            //border around textbox
            document.getElementById(oSrc.controltovalidate).className = "required-input"; 
        }
        isValid = false;
    } else {
        //remove salmon background color
        document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "Transparent";
        if (document.getElementById(oSrc.controltovalidate).type == "text") {
            //remove border around textbox
            document.getElementById(oSrc.controltovalidate).className = "employment-textbox";
        }
        isValid = true;
    }
    //alert("isValid: " + isValid);
    args.IsValid = isValid;
}

//custom validation!
function ValidateRequiredCalendar(oSrc, args) {
    var isValid = false;
    if (args.Value.trim() == "") {
        //salmon background color        
        document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "#ffcccc";
        document.getElementById(oSrc.controltovalidate).className = "Calendar-required";
        isValid = false;
    } else {
        //remove salmon background color
        document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "Transparent";
        document.getElementById(oSrc.controltovalidate).className = "Calendar-enabled";
        isValid = true;
    }
    //alert("isValid: " + isValid);
    //alert("Required: " + document.getElementById(oSrc.controltovalidate).className);
    args.IsValid = isValid;
}

//custom validation!
function ValidateRequiredCheckboxList(oSrc, args) {
    var ctrl = oSrc.ValidationControl;
    var isValid = false;
    var theForm = document.forms['aspnetForm'];
    if (!theForm) {
        theForm = document.aspnetForm;
    }
    for(i=0; i<=theForm.length-1; i++) //loop through all of the form elements
        if (theForm.elements[i].name.indexOf(ctrl) != -1) //see if the ValidationControl is in the element name
            if (theForm.elements[i].checked == true) //if the checkbox is checked
                isValid = true; //the form is valid
    
    if (isValid) {
        //remove salmon background color
        document.getElementById(oSrc.id).parentNode.style.backgroundColor = "Transparent";
    } else {
        //border
        document.getElementById(oSrc.id).parentNode.style.backgroundColor = "#ffcccc";
    }
    //alert("isValid: " + isValid);
    args.IsValid = isValid;
}

function ValidatePattern(oSrc, args) {
    var re = oSrc.ValidationExpression;
    var isValid = false;
    if (args.IsValid)
        if (args.Value.match(re)) {
            //remove background color
            document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "Transparent";
            //remove border around textbox
            document.getElementById(oSrc.controltovalidate).className = "employment-textbox";
            isValid = true;
        } else {
            //border
            document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "#ffcccc";
            //border around textbox
            document.getElementById(oSrc.controltovalidate).className = "required-input";
            isValid = false;
        }
    args.IsValid = isValid;
}

function ValidatePatternCalendar(oSrc, args) {
    var re = oSrc.ValidationExpression;
    var isValid = false;
    if (args.IsValid)
        if (args.Value.match(re)) {
            //remove background color
            document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "Transparent";
            //remove border around textbox
            document.getElementById(oSrc.controltovalidate).className = "Calendar-enabled";
            isValid = true;
        } else {
            //border
            document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "#ffcccc";
            //border around textbox
            document.getElementById(oSrc.controltovalidate).className = "Calendar-required";
            isValid = false;
        }
    args.IsValid = isValid;
}

function ValidateDate(oSrc, args) {
    var isValid = false;
    //check for radio buttons
    if (args.IsValid)
        if (IsDate(args.Value) == true) {
            //parentNode - TD
            document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "Transparent";
            document.getElementById(oSrc.controltovalidate).className = "Calendar-enabled";
            isValid = true;
        } else {
            document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "#ffcccc";
            document.getElementById(oSrc.controltovalidate).className = "Calendar-required";
            isValid = false;
        }
    //alert("Date: " & document.getElementById(oSrc.controltovalidate).className);
    args.IsValid = isValid;
}

function ValidateFutureDate(oSrc, args) {
    var isValid = false;
    //check for radio buttons
    if (args.IsValid)
        if (new Date() >= new Date(args.Value)) {
            document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "#ffcccc";
            document.getElementById(oSrc.controltovalidate).className = "Calendar-required";
            isValid = false;
        } else {
            //parentNode - TD
            document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "Transparent";
            document.getElementById(oSrc.controltovalidate).className = "Calendar-enabled";
            isValid = true;
        }
    //alert("Future Date: " + document.getElementById(oSrc.controltovalidate).className);
    args.IsValid = isValid;
}

function ValidatePastDate(oSrc, args) {
    var isValid = false;
    //check for radio buttons
    if (args.IsValid)
        if (AddDays(new Date(),-1) <= new Date(args.Value)) {
            //parentNode - TD
            document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "#ffcccc";
            document.getElementById(oSrc.controltovalidate).className = "Calendar-required";
            isValid = false;
        } else {
            document.getElementById(oSrc.controltovalidate).parentNode.style.backgroundColor = "Transparent";
            document.getElementById(oSrc.controltovalidate).className = "Calendar-enabled";
            isValid = true;
        }
    //alert("Past Date: " + document.getElementById(oSrc.controltovalidate).className);
    args.IsValid = isValid;
}

function IsDate(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("Please enter date in one of the following formats:\nmm/dd/yy, mm/dd/yyyy, mm-dd-yyyy, or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[4];

    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("Month "+month+" doesn't have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            //alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}

function AddDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}
