/* --------------------------------------------------------------------------------
	file:		DataValidation.js
	purpose:	contains data validation routines
	created by:	Brandon Kelly
	date:		March 29, 2002
   -------------------------------------------------------------------------------- */

var isW3C = (document.getElementById != null);
var isNN4 = (document.layers != null);
var isIE4 = (document.all != null);
var isNS6 = (!isIE4 && isW3C);

var iKey;											// ascii value for key that was pressed

/*	-------------------------------------------------------------------------------
	HELPER FUNCTIONS, IF THIS WERE A CLASS, THESE MEMBERS WOULD BE PRIVATE
	-------------------------------------------------------------------------------- */

function charFromCharCode (charCode) {
	return unescape('%' + charCode.toString(16));
}

function ensureCharacterInPropertybag(sCharacter, sPropertybag) {
	if (sPropertybag.indexOf(sCharacter) == -1) 
		return false;
	else 
		return true;
}

function isMac() {
	// function determines if we're using a Mac..
	var agentString = navigator.userAgent.toLowerCase();
	if (agentString.indexOf('mac') != -1) {
		return true;
	} else {
		return false;
	}
}
function toUpperCase(e) {
	// when called from a keypress event attached to an input tag, this function makes
	// the value of the input tag upper case, ensuring the data is entered that way.
	if (isIE4) {
		if (window.event.keyCode >= 97 && window.event.keyCode <=122)
			window.event.keyCode = window.event.keyCode - 32;			
	} else {
		e.target.value = e.target.value.toUpperCase();
	}
}
		
function toLowerCase(e) {
	// when called from a keypress event attached to an input tag, this function makes
	// the value of the input tag lower case, ensuring the data is entered that way.
	if (isIE4) {
		if (window.event.keyCode >= 65 && window.event.keyCode <=90)
			window.event.keyCode = window.event.keyCode + 32;				
	} else {
		e.target.value = e.target.value.toLowerCase();
	}
}

/*	-------------------------------------------------------------------------------
	GLOBAL FUNCTIONS FOR EVERYDAY USE
	-------------------------------------------------------------------------------- */

function capitilizeFormObjects(e) {
	// function iterates through all input tags and ensures values are entered
	// in the proper case.  This function is called onSubmit() for each form.
	var oCollection = document.getElementsByTagName("INPUT");
	var i;
	for (i=0;i<oCollection.length;i++) {
		// if our current element in our iteration is a text field that doesn't
		// have the word "email" in the id, we want to make it upper case.
		if ((oCollection[i].type.toLowerCase() == "text") && (oCollection[i].id.toLowerCase().indexOf("email") == -1)) {
			oCollection[i].value = oCollection[i].value.toUpperCase();
		} else if ((oCollection[i].type.toLowerCase() == "text") && (oCollection[i].id.toLowerCase().indexOf("email") != -1)) {
			oCollection[i].value = oCollection[i].value.toLowerCase();
		}
	}
}
//<input... onkeypress="return ensure(event);">

function ensureCharInBag(e, sPropertybag) {
	if (isIE4) { iKey = window.event.keyCode; } else { iKey = e.which; }
	if ((iKey == 8) || (iKey == 0)) { return true; }
	
	var sChar = charFromCharCode(iKey);
	return ensureCharacterInPropertybag(sChar, sPropertybag);
}

function ensureAlphaNumericStrict(e) {
	if (isIE4) { iKey = window.event.keyCode; } else { iKey = e.which; }
	if ((iKey == 8) || (iKey == 0)) { return true; }
	
	var sChar = charFromCharCode(iKey);
	var sPropertybag = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	return ensureCharacterInPropertybag(sChar, sPropertybag);
}

function ensureAlphaNumeric(e) {
	if (isIE4) { iKey = window.event.keyCode; } else { iKey = e.which; }
	if ((iKey == 8) || (iKey == 0)) { return true; }
	
	var sChar = charFromCharCode(iKey);
	var sPropertybag = " /-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	return ensureCharacterInPropertybag(sChar, sPropertybag);
}

function ensureChar(e) {
	if (isIE4) { iKey = window.event.keyCode; } else { iKey = e.which; }
	if ((iKey == 8) || (iKey == 0)) { return true; }
	
	var sChar = charFromCharCode(iKey);
	var sPropertybag = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	return ensureCharacterInPropertybag(sChar, sPropertybag);
}

function ensureEmailChar(e) {
	if (isIE4) { iKey = window.event.keyCode; } else { iKey = e.which; }
	if ((iKey == 8) || (iKey == 0)) { return true; }
	
	var sChar = charFromCharCode(iKey);
	var sPropertybag = "-_@0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
	return ensureCharacterInPropertybag(sChar, sPropertybag);
}

function ensureInteger(e) {
	if (isIE4) { iKey = window.event.keyCode; } else { iKey = e.which; }
	if ((iKey == 8) || (iKey == 0)) { return true; }
	
	var sChar = charFromCharCode(iKey);
	var sPropertybag = "0123456789";
	return ensureCharacterInPropertybag(sChar, sPropertybag);
}

function ensureIntegerWithSpace(e) {
	if (isIE4) { iKey = window.event.keyCode; } else { iKey = e.which; }
	if ((iKey == 8) || (iKey == 0)) { return true; }
	
	var sChar = charFromCharCode(iKey);
	var sPropertybag = "0123456789 ";
	return ensureCharacterInPropertybag(sChar, sPropertybag);
}

function ensureNoKeypress(e) {
	return false;
}

function ensureNumeric(e) {
	if (isIE4) { iKey = window.event.keyCode; } else { iKey = e.which; }
	if ((iKey == 8) || (iKey == 0)) { return true; }
	
	var sChar = charFromCharCode(iKey);
	var sPropertybag = "0123456789.";
	return ensureCharacterInPropertybag(sChar, sPropertybag);
}

function ensureShippingChar(e) {
	if (isIE4) { iKey = window.event.keyCode; } else { iKey = e.which; }
	if ((iKey == 8) || (iKey == 0)) { return true; }
	
	var sChar = charFromCharCode(iKey);
	var sPropertybag = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
	return ensureCharacterInPropertybag(sChar, sPropertybag);
}

function ensureSSNCharacter(e) {
	if (isIE4) { iKey = window.event.keyCode; } else { iKey = e.which; }
	if ((iKey == 8) || (iKey == 0)) { return true; }
	
	var sChar = charFromCharCode(iKey);
	var sPropertybag = "0123456789-";
	return ensureCharacterInPropertybag(sChar, sPropertybag);
}

function initObjects() {
	// function iterates through all input tags and ensures values are entered
	// in the proper case.  This function is called onLoad() for all pages.
	var oCollection = document.getElementsByTagName("INPUT");
	var i;
	var mac = isMac();
	
	for (i=0;i<oCollection.length;i++) {
		// if our current element in our iteration is a text field that doesn't
		// have the word "email" in the id, we want to make it upper case.
		if ((oCollection[i].type.toLowerCase() == "text") && (oCollection[i].id.toLowerCase().indexOf("email") == -1)) {
			if (isIE4 && !mac) {
				oCollection[i].attachEvent("onkeypress", toUpperCase);
			} else {
				oCollection[i].addEventListener("keyup", toUpperCase, false);
			}
		} else if ((oCollection[i].type.toLowerCase() == "text") && (oCollection[i].id.toLowerCase().indexOf("email") != -1)) {
			if (isIE4 && !mac) {
				oCollection[i].attachEvent("onkeypress", toLowerCase);
			} else {
				oCollection[i].addEventListener("keyup", toLowerCase, false);
			}
		}
		
		if ((oCollection[i].type.toLowerCase() == "checkbox") || (oCollection[i].type.toLowerCase() == "radio")) {
			if (isNS6) {
				oCollection[i].className = "";
			}
		}
	}
}

function ensureMutEx(obj) {
	var sID = new String(obj.id);
    var sBaseID = new String(sID.slice(sID.lastIndexOf("_") + 1));
    var aInputItems = document.getElementsByTagName("INPUT");

	for (i=0;i<aInputItems.length;i++) {
		var sCompareID = new String(aInputItems[i].id);
		if (sCompareID.lastIndexOf(sBaseID) != -1) {
			aInputItems[i].checked = false;
		}
    }
	obj.checked = true;
}    