﻿//***********************
//*** Data Validation
//***********************
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) {
        return false;
    }
}

function isEmail(emailAddress) {
    return (emailAddress.indexOf(".") > 2) && (emailAddress.indexOf("@") > 0);
}

function isZip(ZipCode) {
    reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
    if (!reZip.test(ZipCode)) {
        return false;
    }
    return true;
}

function isNumber(val) {
    val = val.replace('$', '');
    val = val.replace(',', '');

    if (isNaN(val)) {
        return false;
    }
    else {
        return true;
    }
}

function isPhone(phone) {
    var error = false;
    var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');

    if (phone.value == "") {
        error = true;
        //didn't enter a phone number
    } else if (isNaN(parseInt(stripped))) {
        error = true;
        //phone number contains illegal characters
    } else if (!(stripped.length == 10)) {
        error = true;
        //phone number is the wrong length. 
    }
    return !error;
}

function isURL(urlStr) {
    try {
        if (urlStr == "" || urlStr == null) {
            return false;

        }
        if (urlStr.indexOf(" ") != -1) {
            //alert("Spaces are not allowed in a URL");
            return false;
        }

        urlStr = urlStr.toLowerCase().replace('https://', 'http://');

        var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
        var validChars = "\[^\\s" + specialChars + "\]";
        var atom = validChars + '+';
        //var urlPat = /^http:\/\/(\w*)\.([\-\+a-z0-9]*)\.(\w*)/;
        var urlPat = /^http:\/\/(\w*)\.([\-\+a-z0-9]*)\.(\w*)/;
        var matchArray = urlStr.match(urlPat);

        if (matchArray == null) {
            //alert("The URL seems incorrect \ncheck it begins with http://\n and it has 2 .'s");
            //return false;
        }

        var user = matchArray[2];
        var domain = matchArray[3];

        for (i = 0; i < user.length; i++) {
            if (user.charCodeAt(i) > 127) {
                //alert("This domain contains invalid characters.");
                return false;
            }
        }

        for (i = 0; i < domain.length; i++) {
            if (domain.charCodeAt(i) > 127) {
                //alert("This domain name contains invalid characters.");
                return false;
            }
        }

        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;
            }
        }
    }
    catch (e) {
        return false;
    }

    return true;
}

function IsBadText(sText) {
    var doConvert = false;
    if (sText.toString().length > 0) {
        sText = xmlUnEscape(sText);
        if (sText == sText.toUpperCase() || sText == sText.toLowerCase()) {
            doConvert = true;
        }
        else {

            var i = 0;
            var numCaps = 0;

            for (i = 0; i < sText.length; i++) {

                if ((sText.charCodeAt(i) > 64 && sText.charCodeAt(i) < 91) || (sText.charCodeAt(i) > 96 && sText.charCodeAt(i) < 123)) {
                    if (sText.charAt(i).toLocaleString() == sText.charAt(i).toLocaleString().toUpperCase()) {
                        numCaps++;
                    }
                }
            }
            var ratio = (numCaps * 100) / sText.length;
            if (ratio > 25) {
                doConvert = true;
            }
        }
    }
    return doConvert;
}

