var reInteger = /^\d+$/;
var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;

function isFloat(sFloat,bolNullOK){
	if(isEmpty(sFloat)) return !bolNullOK;
	if(!reFloat.test(sFloat)) return false;
	return true;
}

function isInteger(sInt,bolNullOK){
	if(isEmpty(sInt)) return !bolNullOK;
	if(!reInteger.test(sInt)) return false;
	return true;
}

function isEmpty(strEvaluate){
	return ((strEvaluate == null) || (strEvaluate.length == 0))
}

// tests and id/pw field to see whether the entry has a value and has characters in it and
// is less than or equal to max length
function validateId(str, desc, min, max) {
	var strErrors = "";

	var validchars = null;

	validchars = "abcdefghijklmnopqrstuvwxyz_0123456789";

	if (str != null && str.length > 0) {
		if ((max == -1 || str.length <= max) && (min == -1 || str.length >= min)) {
			for (var i = 0; i < str.length; i++) {
				var c = str.substring(i, i + 1).toLowerCase();
				if (validchars.indexOf(c) == -1) {
					strErrors += "    Invalid " + desc + ". Valid characters are A-Z, a-z, 0-9, and the underscore character.\n";
					break;
				}
			}
		}
		else {
			if (max != -1 && str.length > max)
				strErrors += "    " + desc + " cannot be more than " + max + " characters in length.\n";
			else if (min != -1 && str.length < min)
				strErrors += "    " + desc + " cannot be less than " + min + " characters in length.\n";
		}
	}
	else {
		strErrors += "    " + desc + " must contain a value.\n";
	}

	return strErrors;
}

// tests a simple edit field to see if it has a value in it and is less than or equal to max size
function validateEdit(str, desc, max, bAllowNull) {
	var strErrors = "";

	var bEmpty = false;
	if (str == null || str.length == 0)
		bEmpty = true;
	else {
		var bFoundOtherChars = false;
		for (var i = 0; i < str.length; i++) {
			if (str.substring(i, i + 1) != " ") {
				bFoundOtherChars = true;
				break;
			}
		}
		if (!bFoundOtherChars)
			bEmpty = true;
	}


	if (!bAllowNull && bEmpty) {
		strErrors += "    " + desc + " must contain a value.\n";
	}
	else if (max != -1 && str.length > max) {
		strErrors += "    " + desc + ". " + desc + " cannot be more than " + max + " characters in length.\n";
	}

	return strErrors;
}

// tests a simple numeric edit field to see if it has a numeric value in it and is less than or equal to max length and value
function validateNumericEdit(str, desc, maxlength, minvalue, maxvalue, bAllowNull) {
	var strErrors = "";

	strErrors += validateEdit(str, desc, maxlength, bAllowNull);

	if (strErrors.length == 0) {
		if (isNaN(str)) {
			strErrors += "    " + desc + " is not a valid number.\n";
		}
		else if (maxvalue != -1 && parseInt(str) > maxvalue) {
			strErrors += "    " + desc + " must be less than or equal to " + maxvalue;
			if (bAllowNull)
				strErrors += ". The field may also be left blank.\n";
			else
				strErrors += ".\n";
		}
		else if (minvalue != -1 && parseInt(str) < minvalue) {
			strErrors += "    " + desc + " must be greater than or equal to " + minvalue;
			if (bAllowNull)
				strErrors += " The field may also be left blank.\n";
			else
				strErrors += ".\n";
		}
	}

	return strErrors;
}
// tests a simple numeric edit field to see if it has a valid float value and is less than or equal to max length and value
function validateNumericFloatEdit(str, desc, maxlength, minvalue, maxvalue, bAllowNull) {
	var strErrors = "";

	strErrors += validateEdit(str, desc, maxlength, bAllowNull);

	if (strErrors.length == 0) {
		if (isNaN(str)) {
			strErrors += "    " + desc + " is not a valid number.\n";
		}
		else if (maxvalue != -1 && parseFloat(str) > maxvalue) {
			strErrors += "    " + desc + " must be less than or equal to " + maxvalue;
			if (bAllowNull)
				strErrors += ". The field may also be left blank.\n";
			else
				strErrors += ".\n";
		}
		else if (minvalue != -1 && parseFloat(str) < minvalue) {
			strErrors += "    " + desc + " must be greater than or equal to " + minvalue;
			if (bAllowNull)
				strErrors += " The field may also be left blank.\n";
			else
				strErrors += ".\n";
		}
	}

	return strErrors;
}

function validateIntEdit(str, desc, maxlength, minvalue, maxvalue, bAllowNull) {
	var strErrors = "";

	if (isNaN(str)) {
		strErrors += "    " + desc + " is not a valid number.\n";
	}
	else if (str.indexOf(".") != -1)
		strErrors += "    " + desc + " must be a whole number.\n";
	else
		strErrors += validateNumericEdit(str, desc, maxlength, minvalue, maxvalue, bAllowNull);

	return strErrors;
}

function valueContainsAtLeastOneNumber(str) {
	var bConditionMet = false;

	var searchString = "1234567890";

	for (var i = 0; i < str.length; i++) {
		var c = str.substring(i, i + 1);
		if (searchString.indexOf(c) != -1) {
			bConditionMet = true;
			break;
		}
	}

	return bConditionMet;
}

function valueContainsAtLeastOneLetter(str) {
	var bConditionMet = false;

	var searchString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	for (var i = 0; i < str.length; i++) {
		var c = str.substring(i, i + 1);
		if (searchString.indexOf(c) != -1) {
			bConditionMet = true;
			break;
		}
	}

	return bConditionMet;
}

function valueContainsAtLeastOneUppercaseLetter(str) {
	var bConditionMet = false;

	var searchString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	for (var i = 0; i < str.length; i++) {
		var c = str.substring(i, i + 1);
		if (searchString.indexOf(c) != -1) {
			bConditionMet = true;
			break;
		}
	}

	return bConditionMet;
}

function valueContainsAtLeastOneLowercaseLetter(str) {
	var bConditionMet = false;

	var searchString = "abcdefghijklmnopqrstuvwxyz";

	for (var i = 0; i < str.length; i++) {
		var c = str.substring(i, i + 1);
		if (searchString.indexOf(c) != -1) {
			bConditionMet = true;
			break;
		}
	}

	return bConditionMet;
}

var offImgArray = new Array();
var onImgArray = new Array();

function imageOn(imgName) {
	if (document.images) {
		document.images[imgName].src = onImgArray[imgName].src;
	}
}

function imageOff(imgName) {
	if (document.images) {
		document.images[imgName].src = offImgArray[imgName].src;
	}
}

function setMsg(msg) {
	window.status = msg;
	return true;
}

function setMsgOff() {
	window.status = ""
	return true;
}


function scrollTop() {
	window.scrollTo(0,0);
}

var imgBtnOffArray = new Array();
imgBtnOffArray[0] = new Image(33, 23);
imgBtnOffArray[0].src = "/csr/content/images/menuIcon_off.gif";

var imgBtnOnArray = new Array();
imgBtnOnArray[0] = new Image(33, 23);
imgBtnOnArray[0].src = "/csr/content/images/menuIcon_on.gif";

function menuBtnImageOff(imgName) {
	if (document.images) {
		document.images[imgName].src = imgBtnOffArray[0].src;
	}
}

function menuBtnImageOn(imgName) {
	if (document.images) {
		document.images[imgName].src = imgBtnOnArray[0].src;
	}
}

function buttonOn(btn) {
	btn.className="buttnon";
}

function buttonOff(btn) {
	btn.className="buttn";
}

function buttonOn2(btn) {
	btn.className="buttn";
}

function buttonOff2(btn) {
	btn.className="buttnblu";
}


// synchronizes the first box based on the check state of the second box
// allows to checkboxes to function like radio buttons
// it does not allow a checkbox to be disabled by clicking on itself
function synchCheckbox(firstCheck, secondCheck) {
	if (secondCheck.checked)
		firstCheck.checked = !secondCheck.checked;
	else
		secondCheck.checked = true;

}


// Validates an ip address. If the address is valid, it returns an array of 4 numbers
// representing the ip address. If the address is invalid, it returns false.
function validateIp(strIp, desc) {
	var aNumbers = null;

	var validchars =".0123456789";

	var bContinue = true;
	if (strIp != null && strIp.length > 0) {
		// test for invalid characters
		for (var i = 1; i < strIp.length; i++) {
			var c = strIp.substring(i-1, i).toLowerCase();
			if (validchars.indexOf(c) == -1) {
				alert("Invalid " + desc + ". Valid characters are 0-9 and the period character.");
				bContinue = false;
				break;
			}
		}

		if (bContinue) {
			var firstPeriod = strIp.indexOf(".");
			if (firstPeriod != -1 && firstPeriod > 0) {
				var secondPeriod = strIp.indexOf(".", firstPeriod + 1);
				if (secondPeriod != -1 && (secondPeriod > firstPeriod + 1)) {
					var thirdPeriod = strIp.indexOf(".", secondPeriod + 1);
					if (thirdPeriod != -1 && (thirdPeriod > secondPeriod + 1) && (thirdPeriod < strIp.length - 1)) {
						var number1 = strIp.substring(0, firstPeriod);
						var number2 = strIp.substring(firstPeriod + 1, secondPeriod);
						var number3 = strIp.substring(secondPeriod + 1, thirdPeriod);
						var number4 = strIp.substring(thirdPeriod + 1);

						if (number4.indexOf(".") != -1 || isNaN(number1) || isNaN(number2) || isNaN(number3) || isNaN(number4)) {
							alert("Invalid IP Address. " + desc + " is not formatted correctly. Please check the entry and try again.");
						}
						else {
							number1 = parseInt(number1);
							number2 = parseInt(number2);
							number3 = parseInt(number3);
							number4 = parseInt(number4);

							if (number1 < 0 || number1 > 255 ||
									number2 < 0 || number2 > 255 ||
									number3 < 0 || number3 > 255 ||
									number4 < 0 || number4 > 255) {
								alert("Invalid IP Address. " + desc + " is not formatted correctly. Please check the entry and try again.");
							}
							else {
								// address is valid
								aNumbers = new Array();
								aNumbers[0] = number1;
								aNumbers[1] = number2;
								aNumbers[2] = number3;
								aNumbers[3] = number4;
							}
						}
					}
					else {
						alert("Invalid IP Address. " + desc + " is not formatted correctly. Please check the entry and try again.");
					}
				}
				else {
					alert("Invalid IP Address. " + desc + " is not formatted correctly. Please check the entry and try again.");
				}
			}
			else {
				alert("Invalid IP Address. " + desc + " is not formatted correctly. Please check the entry and try again.");
			}
		}
	}
	else {
		alert("Invalid IP Address. " + desc + " cannot be empty.");
	}

	return aNumbers;
}

function prepareXmlString(str) {
	if (str != null && str.length > 0) {
		str = str.replace(/</g, "&lt;");
		str = str.replace(/>/g, "&gt;");
		str = str.replace(/&/g, "&#38;");
	}

	return str;
}


// skip leading and trailing whitespace
// and return everything in between
function ltrim(s) {
	return s.replace( /^\s*/, "" )
}
function rtrim(s) {
	return s.replace( /\s*$/, "" );
}

function trim(s) {
	return rtrim(ltrim(s));
}

function validEmail(email) {
	var result = false;

	var illegalChars = ",<>/?\";:\\|]}[{=+)(*&^%$#!`~";

	if (email.length > 0) {
		email = trim(email);
		var theStr = new String(email);

		for (var i = 0; i < illegalChars.length; i++) {
			var nIllegalIdx = theStr.indexOf(illegalChars.substring(i, i + 1));
			if (nIllegalIdx >= 0)
				return false;
		}

		var index = theStr.indexOf("@");
		if (index > 0)
		{
			var pindex = theStr.indexOf(".",index);
			if ((pindex > index + 1) && (theStr.length > pindex + 1)) {
				if (theStr.indexOf(",") == -1)
					result = true;
			}
		}
	}

	return result;
}

// validates a list of emails separated by commas
function validateEmailList(emailstring) {
	var bValid = true;

	if (emailstring.length > 0) {
		emailstring = trim(emailstring);
		var strEmailString = new String(emailstring);
		var aEmails = strEmailString.split(",");
		for (var i = 0; i < aEmails.length; i++) {
			var strEmail = trim(aEmails[i]);
			if (!validEmail(strEmail)) {
				bValid = false;
				break;
			}
		}
	}

	return bValid;
}

function formatMoney(dMoney) {
	var strFormatted = "0.00";
	var strTotal = dMoney.toString();
	var pos = strTotal.indexOf(".");

	if (pos < 0)
		strFormatted = strTotal + ".00";
	else if ((strTotal.substr( pos + 1, strTotal.length)).length == 1 )
		strFormatted = strTotal + "0";
	else
		strFormatted = strTotal;

	return strFormatted;
}
