/* ======================================================================================= */
/* =================================== Library Functions ================================= */
/* ======================================================================================= */
function isLeapYear(year)
{
	return (year % 4 == 0 && (year % 100 != 0 || year %400 == 0));
}

function isEmpty(inputStr)
{
	/* Odd - does NOT work with Choice or select controls */
	if (inputStr == null || inputStr == "") {
		return true;
	}
	return false;
}

function isZipCode(inputStr)
{
	inputStr = getRawText(inputStr);
	if (isNaN(inputStr)) {
		return false;
	}
	else if (inputStr.length != 5 && inputStr.length != 9) {
		return false;
	}
	return true;
}

function isSSN(inputStr)
{
	inputStr = getRawText(inputStr);
	if (isNaN(inputStr)) {
		return false;
	}
	else if (inputStr.length != 9) {
		return false;
	}
	return true;
}

function isPhoneNumber(inputStr)
{
	// US Flavor only
	var digits = getRawText(inputStr);
	if (digits.length == 10) {
		if (isNaN(digits)) {
			return false;
		}
	}
	else if (digits.length != 10) {
		return false;
	}
	// AreaCode+555 numbers are invalid
	if (digits.substring(3, 6) == "555") {
		return false;
	}
	return true;
}

function isEmailAddress(inputStr)
{
	// Simple testing
	if (inputStr.indexOf("@") == -1) {
		return false;
	}
	if (inputStr.indexOf(".") == -1) {
		return false;
	}
	return true;
}


function getRawText(inputStr)
{
	// Remove formatting characters for Zip Codes and Phone Numbers
	var s = "" + inputStr;
	s = replaceAll(s, "-", "");
	s = replaceAll(s, " ", "");
	s = replaceAll(s, "/", "");
	s = replaceAll(s, "(", "");
	s = replaceAll(s, ")", "");
	s = replaceAll(s, ".", "");
	s = replaceAll(s, "!", "");
	s = replaceAll(s, "*", "");
	return s;
}

function replaceAll(inputStr, searchFor, replaceWith)
{
	var s = "" + inputStr;
	while (s.indexOf(searchFor) > -1) {
		pos= s.indexOf(searchFor);
		s = "" + (s.substring(0, pos) + replaceWith + s.substring((pos + searchFor.length), s.length));
	}
	return s;
}

function isChoiceEmpty(choiceCtrl)
{
	/* To use this function, the first option (0) must represent no choice */
	if (
		choiceCtrl.selectedIndex == null || 
		choiceCtrl.selectedIndex < 1 || 
		choiceCtrl.value == null) 
	{ 
		return true;
	}
	return false;
}


/* ======================================================================================= */
/* ================================== Support Functions ================================== */
/* ======================================================================================= */

function ValidationError(field, msg)
{
	// Example: return ValidationError(choice, "Please select one.");
	alert(msg);
	if (field.type == "text") {
		field.focus();
		field.select();
	}
	else if (field.type == "textarea") {
		field.focus();
		field.select();
	}
	else if (field.type == "select-one") {
		field.focus();
	}
	else if (field.type == "select-multiple") {
		field.focus();
	}
	return false;
}
