<!--

function trimString(str) {
  str = (this != window) ? this : str
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '')
}
String.prototype.trim = trimString
function trimStringLeadingZeroes(str) {
  str = (this != window) ? this : str
  return str.replace(/^0+/g, '')
}
String.prototype.trimLeadingZeroes = trimStringLeadingZeroes


function testStringLength(str, min, max, desc) {
	if (str.length < min) {
		alert(desc + " must have a minimum of " + min + " characters.")
		return false
	}
	if (str.length > max) {
		alert(desc + " must have a maximum of " + max + " characters.")
		return false
	}
	return true
}


function testStringReasonableChar(str, desc) {
	/* block backslash, angle, curly and square braces, double quotes */
	var invalidChar = "<>~|^\\{}[]\""
	for (var i = 0; i < invalidChar.length; i++) {
		var badChar = invalidChar.charAt(i);
		if (str.indexOf(badChar, 0) != -1) {
			alert(desc + " must not contain the character '" + badChar + "'.")
			return false
		}
	}
	return true
}

function testStringEmail(str) {
	/* assume already passed testStringLength() and testStringReasonableChar()  */
	var invalidChars = " :,;'"
	for (var i = 0; i < invalidChars.length; i++) {
		var badChar = invalidChars.charAt(i)
		if (str.indexOf(badChar,0) > -1) {
			alert("Email address must not contain the character '" + badChar + "'.")
			return false
		}
	}
	var atPos = str.indexOf("@",1)
	if (atPos == -1) {
		alert("Email address missing '@'.")
		return false
	}
	if (str.indexOf("@",atPos+1) != -1) {
		alert("Email address has more than one '@'.")
		return false
	}
	var periodPos = str.indexOf(".", atPos)
	if (periodPos == -1) {
		alert("Email address must have one or more periods after the '@'.")
		return false
	}
	if (periodPos + 3 > str.length) {
		alert("Email address must have a minimum of two characters after the last period.")
		return false
	}
	return true
}

function testStringNumber(str) {
	if (str == "")
		return false
	for (var i = 0; i < str.length; i++) {
		if (str.charAt(i) < "0" || str.charAt(i) > "9")
			return false
	}
	return true
}

function testStringNumberRange(str, min, max, desc) {
	if (! testStringNumber(str)) {
		alert(desc + " must be a number.");
		return false;
	}
	var i = Number(str);
	if (i < min) {
		alert(desc + " must not be smaller than " + min + ".")
		return false
	}
	if (i > max) {
		alert(desc + " must not be larger than " + max + ".")
		return false
	}
	return true
}

function testStringPhoneNumber(str) {
	if (str.length == 0)
		return true;
	if (str.length > 20) {
		alert("Phone number limited to 20 characters.");
		return false;
	}
	if (str.length < 12) {
		alert("Phone number must be entered, minimally, as ###-###-####");
		return false;
	}
	return true;
}

/* zip must be empty/null or 5 digits */
function testStringZipCode(str) {
	if (str.length == 0)
		return true;
	if (str.length != 5 || ! testStringNumber(str)) {
		alert("Zip code must be 5 digits.")
		return false
	}
	return true;
}


function daysInMonth(mo, yr) {
	var n = 31
	if (mo == 4 || mo == 6 || mo == 9 || mo == 11)
		n = 30
	else if (mo == 2) 
		n = ((yr % 4 == 0) && (!(yr % 100 == 0) || (yr % 400 == 0))) ? 29 : 28 
	return n
}
function testStringDate(str, desc) {
	var dateSepChar = "/";
	var minYear = 1900;
	var maxYear = 2100;
	var pos1 = str.indexOf(dateSepChar)
	var pos2 = str.indexOf(dateSepChar, pos1 + 1)
	var strMonth = str.substring(0, pos1).trimLeadingZeroes()
	var strDay = str.substring(pos1 + 1, pos2).trimLeadingZeroes()
	var strYear = str.substring(pos2 + 1).trimLeadingZeroes()
	if (pos1 == -1 || pos2 == -1 ||
		! testStringNumber(strMonth) ||
		! testStringNumber(strDay) ||
		! testStringNumber(strYear)) 
	{
		alert(desc + " must be entered as mm/dd/yy or mm/dd/yyyy.") 
		return false
	}
	var month = parseInt(strMonth)
	var day = parseInt(strDay)
	var year = parseInt(strYear)
	if (year < 100) {
		var now = new Date()
		var y = now.getFullYear() & 100
		year = (year > y) ? year + 1900 : year + 2000
	}
	if (month < 1 || month > 12 ||
		year < minYear || year > maxYear ||
		day < 1 || day > daysInMonth(month, year))
	{
		alert(desc + " must be a valid date between " + minYear + " and " + maxYear + ".")
		return false
	}
	return true
}


function testStringSSN(str) {
	var ssnSepChar = "-";
	var pos1 = str.indexOf(ssnSepChar)
	var pos2 = str.indexOf(ssnSepChar, pos1 + 1)
	if (pos1 == -1 || pos2 == -1) {
		alert("Social security number must be entered using '-' as in 333-22-4444.")
		return false
	}
	var str3 = str.substring(0, pos1)
	var str2 = str.substring(pos1 + 1, pos2)
	var str4 = str.substring(pos2 + 1)
	if (str3.length != 3 || ! testStringNumber(str3) ||
		str2.length != 2 || ! testStringNumber(str2) ||
		str4.length != 4 || ! testStringNumber(str4))
	{
		alert("Social security number must be entered as 333-22-4444.")
		return false
	}
	return true
}

function testStringMoney(str) {
	var sepChar = ".";
	var pos1 = str.indexOf(sepChar)
	var pos2 = str.indexOf(sepChar, pos1 + 1)
	if (pos1 != -1 && pos2 != -1) {
		alert("Money must be entered with only one decimal point.")
		return false
	}
	var dollars;
	if (pos1 == -1) {
		dollars = str;
		if (! testStringNumber(dollars)) {
			alert("Money must be entered as a number.");
			return false;
		}
	} else {
		dollars = str.substring(0, pos1)
		var cents = str.substring(pos1 + 1)
		if (cents.length < 1) {
			alert("Money missing cents after decimal point.");
			return false;
		}
		if (! testStringNumber(cents)) {
			alert("Money must be entered as a number.");
			return false;
		}
		if (cents.length > 2) {
			alert("Money limited to 2 decimal places.");
			return false;
		}
		if (dollars.length > 0 && ! testStringNumber(dollars)) {
			alert("Money must be entered as a number.");
			return false;
		}
	}
	return true;
}

// -->
