﻿//  This set of script functions allows for client side validation  of a date --->

function checkdateValue(sDate) {
    //Returns true if value is a date format or is NULL
    //otherwise returns false	
    var errMsg = 'Please enter the date in the correct format of MM/DD/YYYY';
    var element_value = sDate;

    if (element_value.length == 0)
        return true;

    //Returns true if value is a date in the mm/dd/yyyy format
    isplit = element_value.indexOf('/');

    if (isplit == -1 || isplit == element_value.length) {
        return (false);
    }

    sMonth = element_value.substring(0, isplit);
    isplit = element_value.indexOf('/', isplit + 1);

    if (isplit == -1 || (isplit + 1) == element_value.length) {
        return (false);
    }

    sDay = element_value.substring((sMonth.length + 1), isplit);

    sYear = element_value.substring(isplit + 1);
    if (sYear.length == 2) {
        sYear = '20' + sYear;
    }

    if (!checkinteger(sMonth)) {
        //check month 
        return (false);
    } else if (!checkrange(sMonth, 1, 12)) {
        return (false);
    } else if (!checkinteger(sYear)) {
        //check year
        return false;
    } else if (!checkrange(sYear, 1000, 9999)) {
        return (false);
    } else if (!checkinteger(sDay)) {
        //check day
        return (false);
    } else if (!checkday(sYear, sMonth, sDay)) {
        return (false);
    } else {
        return true;
    }
}

function checkdate(pForm, psElement) {
    //Returns true if value is a date format or is NULL
    //otherwise returns false	

    var errMsg = 'Please enter the date in the correct format of MM/DD/YYYY';
    var element = eval("document." + pForm + "." + psElement);
    var element_value = eval("document." + pForm + "." + psElement + ".value");

    if (element_value.length == 0)
        return true;

    //Returns true if value is a date in the mm/dd/yyyy format
    isplit = element_value.indexOf('/');

    if (isplit == -1 || isplit == element_value.length) {
        alert(errMsg);
        element.focus();
        return (false);
    }

    sMonth = element_value.substring(0, isplit);
    isplit = element_value.indexOf('/', isplit + 1);

    if (isplit == -1 || (isplit + 1) == element_value.length) {
        alert(errMsg);
        element.focus();
        return (false);
    }

    sDay = element_value.substring((sMonth.length + 1), isplit);

    sYear = element_value.substring(isplit + 1);

    if (!checkinteger(sMonth)) {
        //check month 
        alert(errMsg);
        element.focus();
        return (false);
    } else if (!checkrange(sMonth, 1, 12)) {
        alert(errMsg);
        element.focus();
        return (false);
    } else if (!checkinteger(sYear)) {
        //check year
        alert(errMsg);
        element.focus();
        return false;
    } else if (!checkrange(sYear, 1000, 9999)) {
        alert(errMsg);
        element.focus();
        return (false);
    } else if (!checkinteger(sDay)) {
        //check day
        alert(errMsg);
        element.focus();
        return (false);
    } else if (!checkday(sYear, sMonth, sDay)) {
        alert(errMsg);
        element.focus();
        return (false);
    } else {
        return true;
    } 
}

//
//	This function will check the value in the 
//
function checkDateFormat(eItem) {
    sDate = eItem.value;
    var FullPattern = /\d{1,2}\/\d{1,2}\/\d{2,4}/; //Checks for patterd NN/NN/NN[NN]
    var PartialPattern = /\d{1,2}\/\d{0,6}/;
    var PartialPattern2 = /\d{1,2}\/\d{1,2}\/\d{0,4}/;
    //alert( sSSN.length);
    if (sDate.length > 2) {
        if (FullPattern.test(sDate)) {
            return true;
        } else if (sDate.length < 6) {
            if (!PartialPattern.test(sDate)) {
                //				alert(sSSN + ":" + sSSN.substr(0,3) + ":" + sSSN.substr(3) + ":")
                eItem.value = sDate.substr(0, 2) + "/" + sDate.substr(2);
                return true;
            }
            return false;
        } else if (sDate.length < 11) {
            if (PartialPattern2.test(sDate)) {
                eItem.value = sDate.substr(0, 6) + sDate.substr(6);
            } else if (PartialPattern.test(sDate)) {
                eItem.value = sDate.substr(0, 5) + "/" + sDate.substr(5);
            } else {
                eItem.value = sDate.substr(0, 2) + "/" + sDate.substr(2, 2) + "/" + sDate.substr(4);
            }
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}

function checkday(checkYear, checkMonth, checkDay) {

    maxDay = 31;

    if (checkMonth == 4 || checkMonth == 6 ||
			checkMonth == 9 || checkMonth == 11)
        maxDay = 30;
    else
        if (checkMonth == 2) {
        if (checkYear % 4 > 0)
            maxDay = 28;
        else
            if (checkYear % 100 == 0 && checkYear % 400 > 0)
            maxDay = 28;
        else
            maxDay = 29;
    }

    return checkrange(checkDay, 1, maxDay); //check day
}

function checkinteger(object_value) {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
    var decimal_format = ".";
    var check_char;

    //The first character can be + -  blank or a digit.
    check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
        return checknumber(object_value);
    else
        return false;
}

function checknumber(object_value) {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
    var start_format = " .+-0123456789";
    var number_format = " .0123456789";
    var check_char;
    var decimal = false;
    var trailing_blank = false;
    var digits = false;

    //The first character can be + - .  blank or a digit.
    check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
    if (check_char == 1)
        decimal = true;
    else if (check_char < 1)
        return false;

    //Remaining characters can be only . or a digit, but only one decimal.
    for (var i = 1; i < object_value.length; i++) {
        check_char = number_format.indexOf(object_value.charAt(i))
        if (check_char < 0)
            return false;
        else if (check_char == 1) {
            if (decimal)		// Second decimal.
                return false;
            else
                decimal = true;
        }
        else if (check_char == 0) {
            if (decimal || digits)
                trailing_blank = true;
            // ignore leading blanks

        }
        else if (trailing_blank)
            return false;
        else
            digits = true;
    }
    //All tests passed, so...
    return true
}

function checkrange(object_value, min_value, max_value) {
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;


    if (!checknumber(object_value)) {
        return false;
    }
    else {
        return (numberrange((eval(object_value)), min_value, max_value));
    }

    //All tests passed, so...
    return true;
}

function numberrange(object_value, min_value, max_value) {
    // check minimum
    if (min_value != null) {
        if (object_value < min_value)
            return false;
    }

    // check maximum
    if (max_value != null) {
        if (object_value > max_value)
            return false;
    }

    //All tests passed, so...
    return true;
}


function isFutureDate(pForm, psElement, psMonth, psDay, psYear) {
    /*
    *	This function checks that a date occurs in the future of the date specified
    */

    var errMsg = 'The date entered must occur in the future.';
    var element = eval("document." + pForm + "." + psElement);
    var element_value = eval("document." + pForm + "." + psElement + ".value");
    if (arguments.length == 2) {
        todaysdate = new Date();
        psDay = todaysdate.getDay() + 1;
        psMonth = todaysdate.getMonth() + 1;
        yy = todaysdate.getYear();
        year = (yy < 1000) ? yy + 1900 : yy;
        year2 = 2000 - year; year2 = (year2 < 10) ? "0" + year2 : year2;
        psYear = year2;
    }
    if (element_value.length == 0) {
        /*
        *	The element is empty so return true
        */
        return (true);
    } else {
        /*
        *	Break out the month, day, and year pieces entered into the textbox.
        */
        var month = element_value.substr(0, element_value.indexOf("/"));
        var sTempDate = element_value.substr(element_value.indexOf("/") + 1, element_value.length - element_value.indexOf("/"));
        var day = sTempDate.substr(0, sTempDate.indexOf("/"));
        var sTempDate = sTempDate.substr(sTempDate.indexOf("/") + 1, sTempDate.length);
        var year = sTempDate;


        /*
        *  Make sure the month format is MM. If there is only one character
        *	then we will need to add a zero in front. Do the same for the days.
        */
        if (month.length == 1) {
            // add a preceding 0
            month = '0' + month;
        }
        if (day.length == 1) {
            // add a preceding 0
            day = '0' + day;
        }

        /*
        *	First check the year, then the month and finally the day.
        *	Check if the entered value is less than or greater than
        *	the actual date. If it is not less than or greater than, then
        *	they must be equal so check the next smaller measurement of
        *	time.
        */
        if (psYear < year) {
            return (true);
        } else if (psYear > year) {
            alert(errMsg);
            element.focus();
            return (false);
        } else if (psMonth < month) {
            return (true);
        } else if (psMonth > month) {
            alert(errMsg);
            element.focus();
            return (false);
        } else if (psDay <= day) {
            return (true);
        } else {
            alert(errMsg);
            element.focus();
            return (false);
        }
    }
}

// Function to check the validity of the URL and correct it if possible
function checkURL(FormField, URLField) {
    bStartHTTP = false;
    sHTTPString = "http://";

    if (arguments.length > 1) {
        FieldObject = eval("document." + FormField + "." + URLField);
    } else {
        FieldObject = FormField;
    }
    URLString = FieldObject.value;
    if (URLString.length == 0) {
        return true();
    }
    //Strip off http:// for consistency
    if (URLString.substring(0, 7) == "http://") {
        URLString = URLString.substring(7);
        sHTTPString = "http://"
        bStartHTTP = true;
    } else if (URLString.substring(0, 8) == "https://") {
        URLString = URLString.substring(8);
        sHTTPString = "https://"
        bStartHTTP = true;
    }

    //Look for "/" to find the main host
    slashOne = URLString.indexOf("/")
    if (slashOne > 0) {
        coreURL = URLString.substring(0, slashOne);
        tailURL = URLString.substring(slashOne);
    } else {
        coreURL = URLString;
        tailURL = "";
    }

    //Search for "." in core
    dotOne = coreURL.indexOf(".")
    if (dotOne == -1) {
        alert("This is an invalid URL");
        return false;
    }
    //Add "www." if there is only 1 "." in the core.  If there is more than 1, we can't assume anything.
    if (coreURL.lastIndexOf(".") == dotOne && !bStartHTTP) {
        newURL = sHTTPString + "www." + coreURL + tailURL;
    } else {
        newURL = sHTTPString + coreURL + tailURL;
    }
    //		alert(newURL);
    FieldObject.value = newURL;
}

// Function to check the validity of an email address
function checkEmail(FormField, EmailField) {
    if (arguments.length > 1) {
        EmailString = eval("document." + FormField + "." + EmailField + ".value");
        EmailField = eval("document." + FormField + "." + EmailField);
    } else {
        EmailString = FormField.value;
        EmailField = FormField;
    }

    if (EmailString.length == 0) {
        return true;
    }
    //Look for "@" to find the main host
    atOne = EmailString.indexOf("@")
    if (atOne > 0) {
        emailUser = EmailString.substring(0, atOne);
        emailHost = EmailString.substring((atOne + 1));
    } else {
        alert("This is an invalid email address");
        EmailField.focus();
        return false;
    }
    //Search for "." in host
    dotOne = emailHost.indexOf(".")
    if (dotOne == -1) {
        alert("This is an invalid email address");
        EmailField.focus();
        return false;
    }
    return true;
}

//Function to check the validity of a phone number
function checkPhone(FormField, PhoneField) {
    if (arguments.length > 1) {
        fieldString = eval("document." + FormField + "." + PhoneField);
    } else {
        fieldString = FormField;
    }

    checkString = fieldString.value;
    if (checkString.length < 14) {
        if (checkString.length == 10) {
            //if formated as 1234567890
            phoneString = "(" + checkString.substring(0, 3) + ") " + checkString.substring(3, 6) + "-" + checkString.substring(6, 10);
        } else if (checkString.length == 12) {
            // if formated as 123-456-7890
            phoneString = "(" + checkString.substring(0, 3) + ") " + checkString.substring(4, 7) + "-" + checkString.substring(8, 12);
        } else if (checkString.length == 13) {
            if (checkString.substring(0, 1) == "(") {
                // if formated as (123)456-7890
                phoneString = checkString.substring(0, 5) + " " + checkString.substring(5, 13);
            } else {
                // if formated as ???
                phoneString = "(" + checkString.substring(0, 3) + ") " + checkString.substring(4, 7) + "-" + checkString.substring(9, 13);
            }
        } else {
            phoneString = checkString;
        }
    } else {
        phoneString = checkString;
    }
    fieldString.value = phoneString;
}

//
//	Function to check the given field to see if it is equal to or less than the given length
//
function checkLength(sFormName, sFieldName, nMaxLength) {
    rField = eval("document." + sFormName + "." + sFieldName);
    if (rField.value.length > nMaxLength) {
        alert("Your description is too long.  The max length is " + nMaxLength + " characters.  Your entry is " + rField.value.length + " characters and will be shortened.");
        rField.value = rField.value.substring(0, 500);
        return false;
    }
    return true;
}

//
//	Function that will loop over the supplied fields and see if there is anything in them
//
function checkRequiredFields(sFieldsList, sMessageList, sFormName) {
    docform = eval("document." + sFormName);
    arRequiredFields = sFieldsList.split(",");
    arRequiredFieldsMessages = sMessageList.split(",");
    Message = ""
    for (element in arRequiredFields) {
        eItem = eval("docform." + arRequiredFields[element]);

        if (eItem.value.length == 0) {
            Message = Message + arRequiredFieldsMessages[element] + " can not be blank\n";

        }
    }
    if (Message.length > 0) {
        alert(Message);
        return false;
    } else {
        return true;
    }
}

//
//	Function that will loop over the supplied fields and see if all are blank 
//	which probaly means the user doesn't want to submit it.
//
function checkForAllBlank(sFieldsList, sFormName) {
    docform = eval("document." + sFormName);
    arRequiredFields = sFieldsList.split(",");
    bAllBlank = true
    for (element in arRequiredFields) {
        eItem = eval("docform." + arRequiredFields[element]);

        if (eItem.value.length > 0) {
            bAllBlank = false
        }
    }
    return bAllBlank;
}
