// 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" -->

function VL_IsPosInteger(strVal) {
    // Returns true if strVal is a positive integer; else returns false.
    // Empty strings are NOT positive integers.
	
    var inputStr = "" + strVal; // convert to string
    if (inputStr.length <= 0)    return false; // Empty string is NOT positive integer.
    for (var i = 0; i < inputStr.length; i++) {
        var oneChar = inputStr.charAt(i);
        if (oneChar < "0" || oneChar > "9")
            return false;
    }
    return true;
}

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";
		}
	}
}


