// Validation.js
//

// Portions adapted from FormChek.js, (c) 1997 Netscape Communications Corporation
// http://developer.netscape.com/docs/examples/javascript/formval/overview.html

// Use by placing in your <head> tag:
//     <script language="JavaScript" type="text/javascript" src="/includes/validationlib.js"></script>
// (Don't forget the closing script tag, and put nothing inside it).
//
// Note that this will not work with browsers that do not support JavaScript 1.2 or above.
// Most browsers should support this, though!
// To support younger browsers, SSI-include it into your page instead, like:
//     <!--#include virtual="/includes/validationlib.js" -->

//
// REGION Site-specific functions
//

function VL_IsAccountNumber(strAcctNum) {
	// Returns true if the string looks like a valid AccountNumber; something that
	// should be parsable by the AccountNumber object. Note that these regexes currently
	// aren't as forgiving about whitespace compared to the AccountNumber object
	var oRegexOpenCOrLegacyCIS = new RegExp("^([0-9]{3,10}(?:-[0-9]{1,5}|[0-9]{5})|[0-9]{1,7})$");
	var oRegexOpenC = new RegExp("^([0-9]{3,10}(?:-[0-9]{1,5}|[0-9]{5}))$");
	var oRegexLegacyCIS = new RegExp("^([0-9]{1,7})$");
	
	return strAcctNum != null && oRegexOpenC.test(strAcctNum);
}

function VL_AccountNumberAlert() {
	alert("Please enter the account number without spaces or dashes.\nThis number could be up to 15 digits long (excluding the dash).");
}

//
// REGION Validation Functions
//

function VL_IsPosInteger(strVal) {
    // *** DEPRECATED ***
    // This function is a misnomber for VS_IsNonNegativeInteger, and used often in legacy code.
    // Please change your function call to use VL_IsNonNegativeInteger or VL_IsPositiveInteger as appropriate.
    return VL_IsNonNegativeInteger(strVal);
}

function VL_IsNonNegativeInteger(strVal) {
    // Returns true if strVal is a normalized, well-formed positive integer OR zero; else returns false
    // Empty strings are NOT non-negative
    // "+1234" is NOT non-negative
    // Zeroes ARE non-negative
    // null is NOT non-negative
	// other notations are NOT honored (e.g. 1e12)
    var oRegexNNInt = new RegExp("^[0-9]+$");
    return oRegexNNInt.test(VL_ToString(strVal, ""));
}

function VL_IsPositiveInteger(strVal) {
    // Returns true if strVal is a normalized, well-formed positive integer; else returns false.
    // Empty strings are NOT positive integers.
    // Zeroes are NOT positive integers
    // null is NOT a positive integer
	// other notations are NOT honored (e.g. 1e12)
    var oRegexPosInt = new RegExp("^0*[1-9][0-9]*$");
    return oRegexPosInt.test(VL_ToString(strVal, ""));
}

function VL_IsEmpty(strVal) {
    // Returns true if inputVal is empty; else returns false.
    // Whitespace is not considered emptiness. For whitespace 
	// checks, we should use something like IsWhitespace
    return (strVal == null || strVal == "");
}

function VL_IsLengthBetween(strVal, iMinLength, iMaxLength) {
    // Returns true if strVal's length is between iMinLength and
    // iMaxLength, inclusive.
    // (so if iMinLength is 3 and iMaxLength is 5, all values of strVal
    // with lengths of 3, 4 or 5 will cause this function to return true; else false). 

    // If min/max are flipped, swap them.
    if (iMinLength > iMaxLength) {
        var iTemp = iMaxLength;
        iMaxLength = iMinLength;
        iMinLength = iTemp;
    }
    strVal += ""; // convert to string
    return (strVal.length >= iMinLength && strVal.length <= iMaxLength);
}


function VL_IsAlphanumeric (strVal) {
	// Returns true if strVal is alphanumeric.
	// Empty strings are NOT alphanumeric.
	
	if (strVal.length <= 0) return false;
	
	for (var i=0; i<strVal.length; i++) {   
		var c = strVal.charAt(i);
		if ( !(VL_IsLetter(c) || VL_IsDigit(c)) )
			return false;
	}
	return true;
}
    
function VL_IsLetter (strChar) {
    // Note: This will return false for anything longer than 1 char.
    return ( ((strChar >= "a") && (strChar <= "z")) || ((strChar >= "A") && (strChar <= "Z")) );
}

function VL_IsDigit (strChar) {
    // Note: This will return false for anything longer than 1 digit.
    // Note: "12", "343", "4567", "00" etc. are not digits.
    // Use the integer tests instead, for these.
    return ((strChar >= "0") && (strChar <= "9"));
}

function VL_IsEmail(strEmail) {
    var oRegexEmail = new RegExp("^(?:(?:(?:(?:[!#$%&'*+\\-/0123456789=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz\\{\\|\\}~]+)|\"(?:\\\\[\\\\\"]|[^\"])+\")(?:\\.(?:(?:[!#$%&'*+\\-/0123456789=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz\\{\\|\\}~]+)|\"(?:\\\\[\\\\\"]|[^\"])+\"))*)@(?:[!#$%&'*+\\-/0123456789=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz\\{\\|\\}~]+)(?:\\.(?:[!#$%&'*+\\-/0123456789=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz\\{\\|\\}~]+))+)$")
    return (strEmail != null && oRegexEmail.test(strEmail));
}


function VL_Trim(strText) {
    // Returns the string with beginning and ending whitespace removed
    if (strText == null) { return ""; }
    strText = strText + ""; // Cast to string
    return strText.replace(/^\s*|\s*$/g,"");
}

function VL_setElementVisibility(bDisplay) {
	// setElementVisibility(bDisplay, strElementID1, strElementID2, ..., strElementIDn)
	var oElement = null;
	for (var i=1; i<arguments.length; ++i) {
		if ( document.getElementById &&
			(oElement = document.getElementById(arguments[i])) &&
			 oElement.style != null) {
			 oElement.style.display = bDisplay ? "" : "none";
		}
	}
}

//
// REGION Helper Functions
//
function VL_ToString(strVal, defaultValue) {
    if (strVal == null) return defaultValue;
	return "" + strVal;
}

