/* Minimum number required elements
§	Takes the number of required form element objects in a given form, and the form object
§	Also takes optional parms:  form elements to ignore
§	Checks to see if the user has filled at least that many out
§	Returns true or false
*/
function filledAtLeast(someForm,num) {
	var filledSoFar = 0;
	var ignoreElems = new Array();
	if (arguments.length > 2) {
		for (var j=2; j<arguments.length; j++) ignoreElems[ignoreElems.length] = arguments[j];
	}
	for (var i=0; i<someForm.elements.length; i++) {
		var isIgnore = false;
		var currElem = someForm.elements[i];
		for (var k=0; k<ignoreElems.length; k++) {
			if (currElem.name == ignoreElems[k].name) isIgnore = true;
		}
		if (!isIgnore) {
			var currElemType = currElem.type;
			switch (currElemType) {
				case "checkbox":
					if (isChecked(currElem)) filledSoFar++;
					if (filledSoFar >= num) return true;
					break;
				case "radio":
					if (isChecked(currElem)) filledSoFar++;
					if (filledSoFar >= num) return true;
					break;
				case "text":
					if (isBlank(currElem.value)) filledSoFar++;
					if (filledSoFar >= num) return true;
					break;
				case "password":
					if (isBlank(currElem.value)) filledSoFar++;
					if (filledSoFar >= num) return true;
					break;
				case "textarea":
					if (isBlank(currElem.value)) filledSoFar++;
					if (filledSoFar >= num) return true;
					break;
				case "select-one":
					if (isSelected(currElem)) filledSoFar++;
					if (filledSoFar >= num) return true;
					break;
				default:
			}
		}
	}
	return false;
}

/* Default color to highlight incorrect form fields */
highlightColor = "FFFF66";

/* Required elements
§	Takes the required form element objects in a given form
§	Checks to see if the user has filled them all out
§	Highlights incompleted form fields
§	Returns true or false
*/
function filledRequired() {
	var w = 0;
	for (var i=0; i<arguments.length; i++) {
		var currElem = arguments[i];
		var currElemType = currElem.type;
		currElem.style.backgroundColor = "";
		switch (currElemType) {
			case "checkbox":
				if (!isChecked(currElem)) {
					currElem.style.backgroundColor = highlightColor;
					w++;
				}
				break;
			case "text":
				if (isBlank(currElem.value)) {
					currElem.style.backgroundColor = highlightColor;
					w++;
				}
				break;
			case "password":
				if (isBlank(currElem.value)) {
					currElem.style.backgroundColor = highlightColor;
					w++;
				}
				break;
			case "textarea":
				if (isBlank(currElem.value)) {
					currElem.style.backgroundColor = highlightColor;
					w++;
				}
				break;
			case "file":
				if (isBlank(currElem.value)) {
					currElem.style.backgroundColor = highlightColor;
					w++;
				}
				break;
			case "select-one":
				if (!isSelected(currElem)) {
					currElem.style.backgroundColor = highlightColor;
					w++;
				}
				break;
			default:
				alert("Attempting to validate unknown form element: " + currElem.type);
		}
	}
	if (w <= 0) return true;
	else return false;
}

/* Nav select checker
§	Takes a list of nav selects and makes sure that there are no gaps
§	A nav selection can not be followed by a blank selection
*/
function navSelectCheck(navForm) {
	var isHole = false;
	for (var i=0; i<navForm.elements.length; i++) {
		var lastElem = navForm.elements[i-1];
		var currElem = navForm.elements[i];
		var currElemType = currElem.type;
		switch (currElemType) {
			case "select-one":
				currElem.style.backgroundColor = "";
				if (isSelected(currElem) && isHole) {
					lastElem.style.backgroundColor = highlightColor;
					return false;
				} else if (!isSelected(currElem)) {
					isHole = true;
				} else {
					isHole = false;
				}
			default:
		}
	}
	return true;
}

/* isNum
§	Takes a string
§	Checks to see if it contains only numeric characters
§	Returns true or false
*/
function isNum(str) {
	if (isNaN(str)) return false;
	else return true;
}

/* isAlpha
§	Takes a string
§	Checks to see if it contains only alphabetic characters
§	Returns true or false
*/
function isAlpha(str) {
	var badChars = "`\"<>1234567890";
	for (var i=0; i<badChars.length; i++) {
		if (str.indexOf(badChars.charAt(i)) >= 0) return false;
	}
	return true;
}

/* isAlphaNum
§	Takes a string
§	Checks to see if it contains only alphanumeric characters (no special characters)
§	Returns true or false
*/
function isAlphaNum(str) {
	var badChars = "`\"<>";
	for (var i=0; i<badChars.length; i++) {
		if (str.indexOf(badChars.charAt(i)) >= 0) return false;
	}
	return true;
}

/* isAlphaNumHTML
§	Takes a string
§	Checks to see if it contains only alphanumeric characters (no special characters)
§	HTML permissible!!
§	Returns true or false
*/
function isAlphaNumHTML(str) {
	var badChars = "`\"";
	for (var i=0; i<badChars.length; i++) {
		if (str.indexOf(badChars.charAt(i)) >= 0) return false;
	}
	return true;
}

/* isAssetName
§	Takes a string, parses out the path and uses only the file name
§	Checks to see if it contains only characters appropriate for asset names, plus 80 char limit
§	Returns true or false
*/
function isAssetName(str) {
	var startInd = (str.lastIndexOf("\\") == -1) ? 0 : (str.lastIndexOf("\\") + 1);
	var assetName = str.substring(startInd,str.length);
	if (assetName.length > 80) return false;
	var badChars = " `~!@#$%^&*()=+[]{}\\|:;',<>/?\"";
	for (var i=0; i<badChars.length; i++) {
		if (assetName.indexOf(badChars.charAt(i)) >= 0) return false;
	}
	return true;
}

/* isLibraryName
§	Takes a string
§	Checks to see if it contains only characters appropriate for folder & library names
§	Returns true or false
*/
function isLibraryName(str) {
	var badChars = "`~!@#$%^&*()=+[]{}\\|:;',.<>/?\"";
	for (var i=0; i<badChars.length; i++) {
		if (str.indexOf(badChars.charAt(i)) >= 0) return false;
	}
	return true;
}

/* isEmail
§	Takes a string
§	Checks to see if it is a valid email address (exact specs to be determined later)
§	Returns true or false
*/
function isEmail(str) {
	if (str.length == 0) return true;
	var badChars = " /:,!;\"'`";
	for (i=0; i<badChars.length; i++) {
		var badChar = badChars.charAt(i);
		if (str.indexOf(badChar,0) != -1) return false;
	}
	var atPos = str.indexOf("@",1);
	if (atPos == -1) return false;
	if (str.indexOf("@",atPos+1) != -1) return false;
	var periodPos = str.indexOf(".",atPos);
	if (periodPos == -1) return false;
	if (periodPos+3 > str.length)	return false;
	return true;
}

/* isURL
§	Takes a string
§	Checks to see if it is a valid URL address (exact specs to be determined later)
§	Returns true or false
*/
function isURL(str) {
	if (str.length == 0) return true;
	var badChars = " \"'`";
	for (i=0; i<badChars.length; i++) {
		var badChar = badChars.charAt(i);
		if (str.indexOf(badChar,0) != -1) return false;
	}
	var periodPos = str.lastIndexOf(".");
	if (periodPos == -1) return false;
	if (periodPos+3 > str.length) return false;
	return true;
}

/* isPhoneFax
§	Takes a string
§	Checks to see if it is a valid phone or fax number, allows alpha chars
§	Returns true or false
*/
function isPhoneFax(str) {
	var okChars = ".()- 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for (var i=0; i<str.length; i++) {
		if (okChars.indexOf(str.charAt(i)) < 0) return false;
	}
	return true;
}

/* isNumericPhoneFax
§	Takes a string
§	Checks to see if it is a valid phone or fax number (exact specs to be determined later)
§	Returns true or false
*/
function isNumericPhoneFax(str) {
	var okChars = ".()- 0123456789";
	for (var i=0; i<str.length; i++) {
		if (okChars.indexOf(str.charAt(i)) == -1) return false;
	}
	return true;
}

/* isZip
§	Takes a string
§	Checks to see if it is a valid zip code (exact specs to be determined later)
§	Returns true or false
*/
function isZip(str) {
	if (str.length < 5) return false;
	var okChars = "- 1234567890";
	for (var i=0; i<str.length; i++) {
		if (okChars.indexOf(str.charAt(i)) < 0) return false;
	}
	return true;
}

/* isBlank
§	A utility function that returns true if a string contains only whitespace characters
§	Returns true or false
*/
function isBlank(str){
	if (str == "") return true;
    for (var i=0; i<str.length; i++) {
        var c = str.charAt(i);
        if ((c!= ' ') && (c!= '\n') && (c!= '\t')) return false;
    }
    return true;
}

/* isSelected
§	Takes a select object, and optional selectedIndex to ignore
§	Checks to see if an option has been selected, ignoring the first option (assumes first option will be default)
§	Returns true or false
*/
function isSelected(dropdownObj) {
	var selIndex = dropdownObj.selectedIndex;
	if (selIndex) {
		if (dropdownObj[selIndex].value == "") return false;
		else return true;
	}
	return false;
}

/* isChecked
§	Takes a checkbox object
§	Checks to see if it is checked
§	Returns true or false
*/
function isChecked(checkObj) {
	if (checkObj.checked) return true;
	else return false;
}

/* isMaxLength
§	Takes a string, and a number
§	Checks to see if the string's length is equal to or less than the number
§	Returns true or false
*/
function isMaxLength(str,maxLength) {
	if (str.length <= maxLength) return true;
	else return false;
}