﻿// -------------------------------------------------------------------
// CREMEWORKZ - FOR ALL parseInt, Included Radix of base 10 to parse it correctly
// -------------------------------------------------------------------

/***********************/
// variable declarations

var reWhitespace = /^\s+$/

var reAlphabetic = /^[a-zA-Z]+$/

var reAlphanumeric = /^[a-zA-Z0-9]+$/

var reNumberLetter = /^\d+$/

var reLetterOrDigit = /^([a-zA-Z]|\d)$/

var reSignedInteger = /^(\+|\-)?\d+$/

var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/

var reInteger = /^\d+$/

//USZipCode = "^(\\d{5})((\\-)?(\\d{4}))?$"
USZipCode = /^(\d{5})((\-)?(\d{4}))?$/

var ZIPCodeDelimiters = "-";

// Cho phep nhap 1231231234, 123-1231234, 123-123-1234, 123123-1234, (123)1231234, (123)-1231234, (123)123-1234,(123)-123-1234
//USPhone = "^(\\d{3}|(\\()\\d{3}\\))(\\d{3}|(\\-)?(\\d{3})(\\-)?|(\\ )?(\\d{3})(\\-)?)(\\d{4})$"
USPhone = /^(\d{3}|(\()\d{3}\))(\d{3}|(\-)?(\d{3})(\-)?|(\ )?(\d{3})(\-)?)(\d{4})$/

var phoneNumberDelimiters = "()- ";

//SSN = "^(\\d{3})(\\-)?\\d{2}(\\-)?\\d{4}$"
SSN = /^(\d{3})(\-)?\d{2}(\-)?\d{4}$/

var SSNDelimiters = "-";

/************************
Simple email
*************************/
// check email's format
function CheckEmailFormat(str) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)+$/.test(str)) {
        return (true);
    }
    return (false);
}

function CheckName(str) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)+$/.test(str)) {
        return (true);
    }
    return (false);
}

function simplecompareDate(d1, d2) {
    var i = 0;
    var mm1 = "", dd1 = "", yy1 = "";
    while (i < d1.length && d1.charAt(i) != "/") dd1 += d1.charAt(i++); i++;
    while (i < d1.length && d1.charAt(i) != "/") mm1 += d1.charAt(i++); i++;
    while (i < d1.length && d1.charAt(i) != "/") yy1 += d1.charAt(i++);
    i = 0;
    var mm2 = "", dd2 = "", yy2 = "";
    while (i < d2.length && d2.charAt(i) != "/") dd2 += d2.charAt(i++); i++;
    while (i < d2.length && d2.charAt(i) != "/") mm2 += d2.charAt(i++); i++;
    while (i < d2.length && d2.charAt(i) != "/") yy2 += d2.charAt(i++);
    if (dd1.charAt(0) == '0') {
        dd1 = "" + dd1.charAt(1);
    }
    if (mm1.charAt(0) == '0') {
        mm1 = "" + mm1.charAt(1);
    }
    if (dd2.charAt(0) == '0') {
        dd2 = "" + dd2.charAt(1);
    }
    if (mm1.charAt(0) == '0') {
        mm2 = "" + mm2.charAt(1);
    }

    if (parseInt(yy1, 10) < parseInt(yy2, 10)) return -1;
    else if (parseInt(yy1) > parseInt(yy2, 10)) return 1;
    else if (parseInt(mm1, 10) < parseInt(mm2, 10)) return -1;
    else if (parseInt(mm1, 10) > parseInt(mm2, 10)) return 1;
    else if (parseInt(dd1, 10) < parseInt(dd2, 10)) return -1;
    else if (parseInt(dd1) > parseInt(dd2, 10)) return 1;
    else return 0;
}


/***********************
String
***********************/
function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}

function isAlphabetic(s) {
    if (isEmpty(s) == false)
        return reAlphabetic.test(s);
}

function isAlphaNumeric(s) {
    if (isEmpty(s) == false)
        return reAlphanumeric.test(s);
}

function isNumberLetter(s) {
    if (isEmpty(s) == false)
        return reNumberLetter.test(s);
}

function isValidChar(s) {
    if (isEmpty(s) == false) {
        var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
        var temp;

        for (var i = 0; i < s.length; i++) {
            temp = "" + s.substring(i, i + 1);
            if (valid.indexOf(temp) == "-1") return false;
        }
        return true;
    }
}

function CheckLen(s, len, required) {
    if ((required == true) && isEmpty(s) == true)
        return false;

    if (s.length > len)
        return false;

    return true;
}

function isCompanyName(s, len, required) {
    if (CheckLen(s, len, required) == false)
        return false;
    else {
        var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_ ";
        var temp;

        for (var i = 0; i < s.length; i++) {
            temp = "" + s.substring(i, i + 1);
            if (valid.indexOf(temp) == "-1") return false;
        }
    }
    return true;
}

function isName(s, len, required) {
    if (CheckLen(s, len, required) == false)
        return false;
    else {
        if (isEmpty(s) == false) {
            var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
            var temp;
            for (var i = 0; i < s.length; i++) {
                temp = "" + s.substring(i, i + 1);
                if (valid.indexOf(temp) == "-1")
                    return false;
            }
        }
    }

    return true;
}

function isUserName(s, len, required) {
    if (CheckLen(s, len, required) == false)
        return false;
    else
        if (isValidChar(s) == false || isAlphabetic(Left(s, 3)) == false)
        return false;

    return true;
}

function Left(s, n) {
    if (n <= 0)
        return "";
    else if (n > String(s).length)
        return s;
    else
        return String(s).substring(0, n);
}

function Right(s, n) {
    if (n <= 0)
        return "";
    else if (n > String(s).length)
        return s;
    else {
        var iLen = String(s).length;
        return String(s).substring(iLen, iLen - n);
    }
}

function LTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);

    if (whitespace.indexOf(s.charAt(0)) != -1) {
        var j = 0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
            j++;
        s = s.substring(j, i);
    }

    return s;
}

function RTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);

    if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;
        s = s.substring(0, i + 1);
    }

    return s;
}

function Trim(s) {
    return RTrim(LTrim(s));
}

function RemoveBlank(s) {
    s.value = Trim(s.value);
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function stripCharsNotInBag(s, bag) {
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1)
            returnString += c;
    }
    return returnString;
}

function reformat(s) {
    var arg;
    var sPos = 0;
    var resultString = "";
    for (var i = 1; i < reformat.arguments.length; i++) {
        arg = reformat.arguments[i];
        if (i % 2 == 1) resultString += arg;
        else {
            resultString += s.substring(sPos, sPos + arg);
            sPos += arg;
        }
    }
    return resultString;
}

/***********************
Numeric
***********************/
function issimpleNumeric(s) {
    if (isEmpty(s) == false)
        if (isNaN(s))
        return false;
    else
        return true;
}

function isPosFloat(s, required, equal) {
    if (required == true && isEmpty(s) == true)
        return false;

    if (isEmpty(s) == false) {
        var bFloat;
        bFloat = isNaN(s);
        if (bFloat == true)
            return false;
        else {
            if (equal == 1) {//lon hon 0
                //alert(s);
                if (parseFloat(s) <= 0)
                    return false;
            }
            else { //cho phep bang 0
                if (parseFloat(s) < 0)
                    return false;
            }
        }
    }

    return true;
}

/*
function isInteger(s){
if (Left(s, 1) == "+")
s = Right(s, s.length - 1);
	
if (isEmpty(s) == false)
return reInteger.test(s);
}
*/

function isPosInteger(s, required, equal) {
    if (required == true && isEmpty(s) == true)
        return false;

    if (isEmpty(s) == false) {
        var bInteger;

        if (Left(s, 1) == "+")
            s = Right(s, s.length - 1);

        bInteger = reInteger.test(s);

        if (bInteger == false)
            return false;
        else
            if (equal == 1)//lon hon 0
            if (parseInt(s, 10) <= 0) return false;
        else //cho phep bang 0
            if (parseInt(s, 10) < 0) return false;
    }

    return true;
}

function FormatNumber(num, decimalNum, bolLeadingZero, bolParens, bolCommas) {

    if (isPosFloat(num, false) == false) return num;

    var tmpNum = num;
    var iSign = num < 0 ? -1 : 1; 	// Get sign of number

    // Adjust number so only the specified number of numbers after
    // the decimal point are shown.
    tmpNum *= Math.pow(10, decimalNum);
    tmpNum = Math.round(Math.abs(tmpNum))
    tmpNum /= Math.pow(10, decimalNum);
    tmpNum *= iSign; 				// Readjust for sign

    // Create a string object to do our formatting on
    var tmpNumStr = new String(tmpNum);

    // See if we need to strip out the leading zero or not.
    if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
        if (num > 0)
        tmpNumStr = tmpNumStr.substring(1, tmpNumStr.length);
    else
        tmpNumStr = "-" + tmpNumStr.substring(2, tmpNumStr.length);

    // See if we need to put in the commas
    if (bolCommas && (num >= 1000 || num <= -1000)) {
        var iStart = tmpNumStr.indexOf(".");
        if (iStart < 0)
            iStart = tmpNumStr.length;

        iStart -= 3;
        while (iStart > 1) {
            tmpNumStr = tmpNumStr.substring(0, iStart) + "," + tmpNumStr.substring(iStart, tmpNumStr.length)
            iStart -= 3;
        }
    }

    // See if we need to use parenthesis
    if (bolParens && num < 0)
        tmpNumStr = "(" + tmpNumStr.substring(1, tmpNumStr.length) + ")";

    return tmpNumStr; 	// Return our formatted string!
}

/***********************
Date & Time
***********************/

function isDate(s, type, separate, required) {
    if (required == true && isEmpty(s) == true) {
        return 0;
    }

    if (type > 2)
        type = 1;

    if (type < 0)
        type = 0;
    //type:  1: French format: dd/mm/yyyy
    //       0: US format: mm/dd/yyyy

    var i = 0;
    var d, m, y;

    if (!type) {
        m = s.substr(0, s.indexOf(separate));
        s = s.substr(s.indexOf(separate) + 1);
        d = s.substr(0, s.indexOf(separate));
        y = s.substr(s.indexOf(separate) + 1);
    }
    else {
        d = s.substr(0, s.indexOf(separate));
        s = s.substr(s.indexOf(separate) + 1);
        m = s.substr(0, s.indexOf(separate));
        y = s.substr(s.indexOf(separate) + 1);
    }
    if (d.length != 2) {
        return 1;
    }
    if (m.length != 2) {
        return 1;
    }


    if (isNumberLetter(y) == true && isNumberLetter(m) == true && isNumberLetter(d) == true) {
        if (y.length != 4) {
            return 1;
        }

        iDay = parseInt(d, 10);
        iMon = parseInt(m, 10);
        iYear = parseInt(y, 10);

        if (iYear < 1900) {
            return 2;
        }

        if ((iDay > 31) || (iDay < 1)) {
            return 3;
        }

        if ((iMon > 12) || (iMon < 1)) {
            return 4;
        }

        if (((iMon == 4) || (iMon == 6) || (iMon == 9) || (iMon == 11)) && (iDay > 30)) {
            return 5;
        }

        if (iMon == 2) {
            if (iYear % 4 == 0) {
                if ((iYear % 100 != 0) || (iYear % 400 == 0)) {
                    if (iDay > 29) {
                        return 6;
                    }
                }
                else if (iDay > 28) {
                    return 7;
                }
            }
            else {
                if (iDay > 28) {
                    return 7;
                }
            }
        }
    }
    else {
        return 8;
    }

    return -1;
}

function compareDate(s1, s2, type, separate) {
    if (type > 2)
        type = 1;

    if (type < 0)
        type = 0;
    //type:  1: French format: dd/mm/yyyy
    //       0: US format: mm/dd/yyyy

    var i = 0;
    var d1, m1, y1;
    var d2, m2, y2;

    if (!type) {
        m1 = s1.substr(0, s1.indexOf(separate))
        s1 = s1.substr(s1.indexOf(separate) + 1)
        d1 = parseInt(s1.substr(0, s1.indexOf(separate)), 10)
        y1 = parseInt(s1.substr(s1.indexOf(separate) + 1), 10)

        m2 = parseInt(s2.substr(0, s2.indexOf(separate)), 10)
        s2 = s2.substr(s2.indexOf(separate) + 1)
        d2 = parseInt(s2.substr(0, s2.indexOf(separate)), 10)
        y2 = parseInt(s2.substr(s2.indexOf(separate) + 1), 10)
    }
    else {
        d1 = parseInt(s1.substr(0, s1.indexOf(separate)), 10)
        s1 = s1.substr(s1.indexOf(separate) + 1)
        m1 = parseInt(s1.substr(0, s1.indexOf(separate)), 10)
        y1 = parseInt(s1.substr(s1.indexOf(separate) + 1), 10)

        d2 = parseInt(s2.substr(0, s2.indexOf(separate)), 10)
        s2 = s2.substr(s2.indexOf(separate) + 1)
        m2 = parseInt(s2.substr(0, s2.indexOf(separate)), 10)
        y2 = parseInt(s2.substr(s2.indexOf(separate) + 1), 10)

    }

    if (y1 > y2)
        return -1
    else if (y1 < y2)
        return 1
    else {
        if (m1 > m2)
            return -1
        else if (m1 < m2)
            return 1
        else {
            if (d1 > d2)
                return -1
            else if (d1 < d2)
                return 1
            else
                return 0
        }
    }
}


/***********************/
//Email

//function CheckEmail (emailStr) {
function isEmail(emailStr, len, required) {

    emailStr = emailStr.toLowerCase();

    if (CheckLen(emailStr, len, required) == false) {
        return false;
    }

    if (isEmpty(emailStr) == false) {
        /* The following variable tells the rest of the function whether or not
        to verify that the address ends in a two-letter country or well-known
        TLD. 1 means check it, 0 means don't. */

        var checkTLD = 1;

        /* The following is the list of known TLDs that an e-mail address must end with. */
        var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

        /* The following pattern is used to check if the entered e-mail address
        fits the user@domain format.  It also is used to separate the username
        from the domain. */

        var emailPat = /^(.+)@(.+)$/;

        /* The following string represents the pattern for matching all special
        characters. We don't want to allow special characters in the address. 
        These characters include ( ) < > @ , ; : \ " . [ ] */

        //var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
        var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]\\^!%*&=+/#\\'|`~";

        /* The following string represents the range of characters allowed in a 
        username or domainname.  It really states which chars aren't allowed.*/

        var validChars = "\[^\\s" + specialChars + "\]";

        /* The following pattern applies if the "user" is a quoted string (in
        which case, there are no rules about which characters are allowed
        and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
        is a legal e-mail address. */

        var quotedUser = "(\"[^\"]*\")";

        /* The following pattern applies for domains that are IP addresses,
        rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
        e-mail address. NOTE: The square brackets are required. */

        var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

        /* The following string represents an atom (basically a series of non-special characters.) */

        var atom = validChars + '+';

        /* The following string represents one word in the typical username.
        For example, in john.doe@somewhere.com, john and doe are words.
        Basically, a word is either an atom or quoted string. */

        var word = "(" + atom + "|" + quotedUser + ")";

        // The following pattern describes the structure of the user

        var userPat = new RegExp("^" + word + "(\\." + word + ")*$");

        /* The following pattern describes the structure of a normal symbolic
        domain, as opposed to ipDomainPat, shown above. */

        var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");

        /* Finally, let's start trying to figure out if the supplied address is valid. */

        /* Begin with the coarse pattern to simply break up user@domain into
        different pieces that are easy to analyze. */

        var matchArray = emailStr.match(emailPat);

        if (matchArray == null) {
            /* Too many/few @'s or something; basically, this address doesn't
            even fit the general mould of a valid e-mail address. */

            //alert("Email address seems incorrect (check @ and .'s)");
            return false;
        }

        var user = matchArray[1];
        var domain = matchArray[2];

        // Start by checking that only basic ASCII characters are in the strings (0-127).

        for (i = 0; i < user.length; i++) {
            if (user.charCodeAt(i) > 127) {
                //alert("Ths username contains invalid characters.");
                return false;
            }
        }

        for (i = 0; i < domain.length; i++) {
            if (domain.charCodeAt(i) > 127) {
                //alert("Ths domain name contains invalid characters.");
                return false;
            }
        }

        // See if "user" is valid 

        if (user.match(userPat) == null) {
            // user is not valid

            //alert("The username doesn't seem to be valid.");
            return false;
        }

        /* if the e-mail address is at an IP address (as opposed to a symbolic
        host name) make sure the IP address is valid. */

        var IPArray = domain.match(ipDomainPat);

        if (IPArray != null) {
            // this is an IP address

            for (var i = 1; i <= 4; i++) {
                if (IPArray[i] > 255) {
                    //alert("Destination IP address is invalid!");
                    return false;
                }
            }
            return true;
        }

        // Domain is symbolic name.  Check if it's valid.

        var atomPat = new RegExp("^" + atom + "$");
        var domArr = domain.split(".");
        var len = domArr.length;
        for (i = 0; i < len; i++) {
            if (domArr[i].search(atomPat) == -1) {
                //alert("The domain name does not seem to be valid.");
                return false;
            }
        }

        /* domain name seems valid, but now make sure that it ends in a
        known top-level domain (like com, edu, gov) or a two-letter word,
        representing country (uk, nl), and that there's a hostname preceding 
        the domain or country. */

        if (checkTLD && domArr[domArr.length - 1].length != 2 &&
				domArr[domArr.length - 1].search(knownDomsPat) == -1) {
            //alert("The address must end in a well-known domain or two letter " + "country.");
            return false;
        }

        // Make sure there's a host name preceding the domain.

        if (len < 2) {
            //alert("This address is missing a hostname!");
            return false;
        }

        // If we've gotten this far, everything's valid!
        //return true;
    }

    return true;
}

/***********************
US ZipCode, Phone, Fax, Social Security Number
***********************/

function isUSZipCode(s, required) {
    if (required == true && isEmpty(s) == true) {
        return false;
    }

    if (isEmpty(s) == false) {
        //var regex = new RegExp(USZipCode);
        //var bFlag = regex.test(s);
        var bFlag = USZipCode.test(s);
        if (bFlag == false) return false;
    }

    return true;
}

function FormatUSZipCode(s) {
    var Zip;

    Zip = s.value;

    if (isEmpty(s.value) == false && s.value.length != 5) {
        if (isUSZipCode(Zip, false) == true)
            Zip = reformat(stripCharsInBag(Zip, ZIPCodeDelimiters), "", 5, "-", 4);
    }

    s.value = Zip;
    return true;
}

function isUSPhone(s, required) {
    if (required == true && isEmpty(s) == true) {
        return false;
    }

    if (isEmpty(s) == false) {
        //var regex = new RegExp(USPhone);
        //return reAlphabetic.test(s);
        var bFlag = USPhone.test(s);
        if (bFlag == false) return false;
    }

    return true;
}

function FormatUSPhone(s) {
    var Phone;
    Phone = s.value;
    if (isEmpty(Phone) == false) {
        if (isUSPhone(Phone, false) == true)
            Phone = reformat(stripCharsInBag(Phone, phoneNumberDelimiters), "(", 3, ") ", 3, "-", 4);
    }

    s.value = Phone;
    return true;
}

function isSSN(s, required) {
    if (required == true && isEmpty(s) == true) {
        return 0;
    }

    if (isEmpty(s) == false) {
        //var regex = new RegExp(SSN);
        //var bFlag = regex.test(s);
        var bFlag = SSN.test(s);

        if (bFlag == false) {
            return 1;
        }
        else
            if (Left(s, 3) == "000") {
            return 2;
        }
    }

    return -1;
}

function FormatSSN(s) {
    var str;
    str = s.value;

    if (isEmpty(str) == false) {
        if (isSSN(str, false) == -1)
            str = reformat(stripCharsInBag(str, SSNDelimiters), "", 3, "-", 2, "-", 4)
    }

    s.value = str;
    return true;
}

/***********************
Form
***********************/

function GetRadioButtonValue(param) {
    if (param == null)
        return;
    if (param.length >= 2)
        for (i = 0; i < param.length; i++) {
        if (param[i].checked)
            return param[i].value;
    }
    else {
        if (param.checked)
            return param.value;
    }

    return "";
}

/*******************************
Credit Card
*******************************/
/*  ================================================================
FUNCTION:  CheckCard(st)
INPUT:     st - a string representing a credit card number
RETURNS:  true, if the credit card number passes the Luhn Mod-10
test.
false, otherwise
================================================================ */
function CheckCard(st) {
    if (st.length > 19)
        return (false);
    sum = 0; mul = 1; l = st.length;
    for (i = 0; i < l; i++) {
        digit = st.substring(l - i - 1, l - i);
        tproduct = parseInt(digit, 10) * mul;
        if (tproduct >= 10)
            sum += (tproduct % 10) + 1;
        else
            sum += tproduct;
        if (mul == 1)
            mul++;
        else
            mul--;
    }
    // Uncomment the following line to help create credit card numbers
    // 1. Create a dummy number with a 0 as the last digit
    // 2. Examine the sum written out
    // 3. Replace the last digit with the difference between the sum and the next multiple of 10.
    //  document.writeln("<BR>Sum      = ",sum,"<BR>");
    //  alert("Sum      = " + sum);
    if ((sum % 10) == 0)
        return (true);
    else
        return (false);
}

function isCreditCard(s, type) {
    switch (type) {
        case 1:

            /*  ================================================================
            Visa Card
            Sample number: 4111 1111 1111 1111 (16 digits)
            ================================================================ */
            if (((s.length == 16) || (s.length == 13)) && (s.substring(0, 1) == 4))
                return CheckCard(s);
            return false;
            break;

        /*  ================================================================
        MasterCard
        Sample number: 5500 0000 0000 0004 (16 digits)
        ================================================================ */ 
        case 2:
            firstdig = s.substring(0, 1);
            seconddig = s.substring(1, 2);
            if ((s.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
                return CheckCard(s);
            return false;
            break;

        /*  ================================================================
        AmericanExpress
        Sample number: 340000000000009 (15 digits)
        ================================================================ */ 
        case 3:
            firstdig = s.substring(0, 1);
            seconddig = s.substring(1, 2);
            if ((s.length == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7)))
                return CheckCard(s);
            return false;
            break;

        /*  ================================================================
        DinersClub
        Sample number: 30000000000004 (14 digits)
        ================================================================ */ 
        case 4:
            firstdig = s.substring(0, 1);
            seconddig = s.substring(1, 2);
            if ((s.length == 14) && (firstdig == 3) && ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
                return CheckCard(s);
            return false;
            break;

        /*  ================================================================
        Discover
        Sample number: 6011000000000004 (16 digits)
        ================================================================ */ 
        case 5:
            first4digs = s.substring(0, 4);
            if ((s.length == 16) && (first4digs == "6011"))
                return CheckCard(s);
            return false;

            /*  ================================================================
            EnRoute
            Sample number: 201400000000009 (15 digits)
            ================================================================ */
        case 6:
            first4digs = s.substring(0, 4);
            if ((s.length == 15) && ((first4digs == "2014") || (first4digs == "2149")))
                return CheckCard(s);
            return false;
            break;
    }
}

function IsValidDate(value) {
    return (isDate(value, 1, '/', true) == -1);
}

function IsInteger(value) {
    return isPosInteger(value, true, 0);
}

function IsReal(value) {
    return isPosFloat(value, true, 0);
}

function verify_passwords(password_form) {
    if (password_form.password.value == "") {
        alert("You must enter a password")
        password_form.password.focus()
        return false
    }
    if (password_form.password.value != password_form.verify_password.value) {
        alert("Passwords do not match, please try again")
        password_form.password.focus()
        password_form.password.select()
        return false
    }
    return true
}

function emailCheck(emailStr) {

    /* The following variable tells the rest of the function whether or not
    to verify that the address ends in a two-letter country or well-known
    TLD.  1 means check it, 0 means don't. */

    var checkTLD = 1;

    /* The following is the list of known TLDs that an e-mail address must end with. */

    var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

    /* The following pattern is used to check if the entered e-mail address
    fits the user@domain format.  It also is used to separate the username
    from the domain. */

    var emailPat = /^(.+)@(.+)$/;

    /* The following string represents the pattern for matching all special
    characters.  We don't want to allow special characters in the address. 
    These characters include ( ) < > @ , ; : \ " . [ ] */

    var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

    /* The following string represents the range of characters allowed in a 
    username or domainname.  It really states which chars aren't allowed.*/

    var validChars = "\[^\\s" + specialChars + "\]";

    /* The following pattern applies if the "user" is a quoted string (in
    which case, there are no rules about which characters are allowed
    and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
    is a legal e-mail address. */

    var quotedUser = "(\"[^\"]*\")";

    /* The following pattern applies for domains that are IP addresses,
    rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
    e-mail address. NOTE: The square brackets are required. */

    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

    /* The following string represents an atom (basically a series of non-special characters.) */

    var atom = validChars + '+';

    /* The following string represents one word in the typical username.
    For example, in john.doe@somewhere.com, john and doe are words.
    Basically, a word is either an atom or quoted string. */

    var word = "(" + atom + "|" + quotedUser + ")";

    // The following pattern describes the structure of the user

    var userPat = new RegExp("^" + word + "(\\." + word + ")*$");

    /* The following pattern describes the structure of a normal symbolic
    domain, as opposed to ipDomainPat, shown above. */

    var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");

    /* Finally, let's start trying to figure out if the supplied address is valid. */

    /* Begin with the coarse pattern to simply break up user@domain into
    different pieces that are easy to analyze. */

    var matchArray = emailStr.match(emailPat);

    if (matchArray == null) {

        /* Too many/few @'s or something; basically, this address doesn't
        even fit the general mould of a valid e-mail address. */

        //alert("Email address seems incorrect (check @ and .'s)");
        return false;
    }
    var user = matchArray[1];
    var domain = matchArray[2];

    // Start by checking that only basic ASCII characters are in the strings (0-127).

    for (i = 0; i < user.length; i++) {
        if (user.charCodeAt(i) > 127) {
            //alert("Ths username contains invalid characters.");
            return false;
        }
    }
    for (i = 0; i < domain.length; i++) {
        if (domain.charCodeAt(i) > 127) {
            //alert("Ths domain name contains invalid characters.");
            return false;
        }
    }

    // See if "user" is valid 

    if (user.match(userPat) == null) {

        // user is not valid

        //alert("The username doesn't seem to be valid.");
        return false;
    }

    /* if the e-mail address is at an IP address (as opposed to a symbolic
    host name) make sure the IP address is valid. */

    var IPArray = domain.match(ipDomainPat);
    if (IPArray != null) {

        // this is an IP address

        for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255) {
                //alert("Destination IP address is invalid!");
                return false;
            }
        }
        return true;
    }

    // Domain is symbolic name.  Check if it's valid.

    var atomPat = new RegExp("^" + atom + "$");
    var domArr = domain.split(".");
    var len = domArr.length;
    for (i = 0; i < len; i++) {
        if (domArr[i].search(atomPat) == -1) {
            //alert("The domain name does not seem to be valid.");
            return false;
        }
    }

    /* domain name seems valid, but now make sure that it ends in a
    known top-level domain (like com, edu, gov) or a two-letter word,
    representing country (uk, nl), and that there's a hostname preceding 
    the domain or country. */

    if (checkTLD && domArr[domArr.length - 1].length != 2 &&
domArr[domArr.length - 1].search(knownDomsPat) == -1) {
        //alert("The address must end in a well-known domain or two letter " + "country.");
        return false;
    }

    // Make sure there's a host name preceding the domain.

    if (len < 2) {
        //alert("This address is missing a hostname!");
        return false;
    }

    // If we've gotten this far, everything's valid!
    return true;
}

/*Check for a specificed special characters'*/
function isSpcificSpecialChar(str) {
    var spchar, getChar, SpecialChar;
    spchar = "!@#$%^*+=[]\\\';/{}|\"<>?~_";
    getChar = 'Empty';
    SpecialChar = 'No';
    for (var i = 0; i < str.length; i++) {
        for (var j = 0; j < spchar.length; j++) {
            if (str.charAt(i) == spchar.charAt(j)) {
                SpecialChar = 'Yes';
                break;
            }
            else {
                if (str.charAt(i) != ' ')
                    getChar = 'Normal';
            }
        }
    }
    if (SpecialChar == 'Yes') {
        return true;
    }
    else if (SpecialChar == 'No') {
        return false;
    }
}

/*Check for a special character'***/
function isSpecialChar(str) {
    var spchar, getChar, SpecialChar;
    spchar = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_";
    getChar = 'Empty';
    SpecialChar = 'No';
    for (var i = 0; i < str.length; i++) {
        for (var j = 0; j < spchar.length; j++) {
            if (str.charAt(i) == spchar.charAt(j)) {
                SpecialChar = 'Yes';
                break;
            }
            else {
                if (str.charAt(i) != ' ')
                    getChar = 'Normal';
            }
        }
    }
    if (SpecialChar == 'Yes') {
        return true;
    }
    else if (SpecialChar == 'No') {
        return false;
    }
}
function isSpecialCharWithoutDash(str) {
    var spchar, getChar, SpecialChar;
    spchar = "!@#$%^&*()+=[]\\\';,./{}|\":<>?~";
    getChar = 'Empty';
    SpecialChar = 'No';
    for (var i = 0; i < str.length; i++) {
        for (var j = 0; j < spchar.length; j++) {
            if (str.charAt(i) == spchar.charAt(j)) {
                SpecialChar = 'Yes';
                break;
            }
            else {
                if (str.charAt(i) != ' ')
                    getChar = 'Normal';
            }
        }
    }
    if (SpecialChar == 'Yes') {
        return true;
    }
    else if (SpecialChar == 'No') {
        return false;
    }
}
function isSpecialChar2(str) {
    var spchar, getChar, SpecialChar;
    spchar = "!@#$%^*+=-[]\\\';,./{}|\":<>?~_";
    getChar = 'Empty';
    SpecialChar = 'No';
    for (var i = 0; i < str.length; i++) {
        for (var j = 0; j < spchar.length; j++) {
            if (str.charAt(i) == spchar.charAt(j)) {
                SpecialChar = 'Yes';
                break;
            }
            else {
                if (str.charAt(i) != ' ')
                    getChar = 'Normal';
            }
        }
    }
    if (SpecialChar == 'Yes') {
        return true;
    }
    else if (SpecialChar == 'No') {
        return false;
    }
}
function IsDateGreater(DateValue1, DateValue2) {

    var DaysDiff;
    Date1 = new Date(DateValue1);
    Date2 = new Date(DateValue2);
    DaysDiff = Math.floor((Date1.getTime() - Date2.getTime()) / (1000 * 60 * 60 * 24));
    if (DaysDiff > 0)
        return true;
    else
        return false;
}

function IsDateLess(DateValue1, DateValue2) {
    var DaysDiff;
    Date1 = new Date(DateValue1);
    Date2 = new Date(DateValue2);
    DaysDiff = Math.floor((Date1.getTime() - Date2.getTime()) / (1000 * 60 * 60 * 24));    
    if (DaysDiff <= 0)
        return true;
    else
        return false;
}

function IsDateEqual(DateValue1, DateValue2) {
    var DaysDiff;
    Date1 = new Date(DateValue1);
    Date2 = new Date(DateValue2);
    DaysDiff = Math.floor((Date1.getTime() - Date2.getTime()) / (1000 * 60 * 60 * 24));
    if (DaysDiff == 0)
        return true;
    else
        return false;
}

function echeck(str) {
    //alert("in echeck");
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {

        return false;
    }
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {

        return false;
    }
    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {

        return false;
    }
    if (str.indexOf(at, (lat + 1)) != -1) {

        return false;
    }
    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {

        return false;
    }
    if (str.indexOf(dot, (lat + 2)) == -1) {

        return false;
    }
    if (str.indexOf(" ") != -1) {

        return false;
    }

    return true;
}

function IsNumeric(sText) {
    //alert("is number");
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

