/* ======================================================================================= */
/* =================================== 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(phone, country) {
	// Backward support for validation for US that did not include Country
	if (country == null) {
		return isPhoneNumberUS(phone);
	}
	if (country == "United States") {
		return isPhoneNumberUS(phone);
	}

	// Generic Non-US validation

	// If the user has entered +1 or +01, remove it
	if (phone.substring(0, 2) == "+1") { phone = phone.substring(2, phone.length); }
	if (phone.substring(0, 3) == "+01") { phone = phone.substring(3, phone.length); }
	
	var digits = getRawText(phone);	  					// Remove all non-formatting characters
	if (isNaN(digits)) { return false; }				// Must be digits only
	if (digits.length < 5) { return false; }		// Must be at least 5 numbers in length
	return true;		
}

function isPhoneNumberUS(inputStr)
{
  var npa, nxx, npa_filter, nxx_filter;

	// Some US users are entering numbers like 1-222-333-4444 - this chunk removes the leading "1" (or "+1")
	if (inputStr.substring(0,1) == "1" && inputStr.length > 11) { inputStr = inputStr.substring(1, inputStr.length); }
	if (inputStr.substring(0,2) == "+1" && inputStr.length > 11) { inputStr = inputStr.substring(2, inputStr.length); }

	var digits = getRawText(inputStr);					// Remove all non-formatting characters
	if (digits.length == 10) {
		if (isNaN(digits)) { return false; }
	}
	else if (digits.length != 10) {	return false;	}

  // Check for obvious invalid NPA's (like 000/123-4567, 001.., 011...)
	npa = digits.substring(0,3);
	if (npa < 201) { return false; }
  npa_filter = '300,400,500,600,700,900,999';
	if (npa_filter.indexOf(npa) > -1) { return false; } 
	
	// Check for obvious invalid NXX's (like 123/555-4567)
	nxx = digits.substring(3, 6);
  nxx_filter = '000,555';
	if (nxx_filter.indexOf(nxx) > -1) { 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;
}
