/*	sphost.validation.js			July, 2005
	These functions have been created by sphost.biz, based on the work of many who have preceded us.

	They are primarily meant to validate form fields

	Functions fall into two categories:
		instant validation when leaving a field
			onBlur="validateXxx(this)"
		validation upon submittal, where you must include the calls in your own form-validation routine
*/


// Validate syntax of an email address
function validateEmailFormat(obj) {
	mailAddr = obj.value

	if (mailAddr == "") {						// allow empty
		return true
	}
	errmsg=""
	invalidChars = "/: ,;"

// valid characters
	for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (mailAddr.indexOf(badChar,0) > -1) {
 			errmsg += "\nMay not contain space, slash (/), comma (,), colon (:) or semicolon (;)"
			break
		}
	}

// at (@)
	atPos = mailAddr.indexOf("@",0)
	if (atPos == -1) {
		errmsg += "\nMust contain an '@'"
	}
	else {
		if (atPos == 0) {
			errmsg += "\nMust contain a username before the '@'"
		}
		if (mailAddr.indexOf("@",atPos+1) != -1) {
			errmsg += "\nMay only contain one '@'"
		}

// dot (.)
		dotPos = mailAddr.indexOf(".",atPos)
		if (dotPos == -1) {						// and at least one "." after the "@"
			errmsg += "\nMust contain a domain name (e.g. xxx.com) after the '@'"
		}
		else {
			if (dotPos == atPos + 1)	{
				errmsg += "\nMust have something between the '@' and the '.'"
			}
			if (dotPos +3 > mailAddr.length)	{
				errmsg += "\nMust be at least 2 characters after the '.' (e.g. .com, .org, .ca)"
			}
		}
	}

// clean up
	if (errmsg == "") {return true}
	else {
		errmsg += "\n=========================="
		errmsg += "\nEnter address as 'your-address@your-provider-domain', e.g.\n       pat@companyname.com"
		alert("Invalid email address\n==========================" + errmsg)
		obj.focus()
		obj.select()
		return false
	}
}

// Validate a money field
function validateMoney(obj) {				// must be [$]ddd[.cc]
	mny = obj.value
	if (mny == "") {					// allow empty
		return true
	}
	errmsg=""

// decimal point (yes, this assumes "." as the currency separator)
	dotPos = mny.indexOf(".",0)
	if (dotPos != -1) {				// decimal point is optional
		if (mny.indexOf(".",dotPos +1) != -1) {
			errmsg += "\nMay only contain one decimal point"
		}
		if (dotPos != mny.length - 1 && dotPos != mny.length - 3)	{
			errmsg += "\nMust be 2 digits after the decimal point"
		}
	}

// digits
	for (i=0; i<mny.length; i++) {
		testChar = mny.charAt(i)
		if (testChar != "." && (testChar < "0" || testChar > "9")) {
			errmsg += "\nMay only contain digits (and optionally one decimal point)"
		}
	}

// clean up
	if (errmsg == "") {return true}
	else {
		errmsg += "\n=========================="
		errmsg += "\nEnter as dollars (e.g. 36) or dollars & cents (e.g. 12.50)"
		alert("Invalid amount\n==========================" + errmsg)
		obj.focus()
		obj.select()
		return false
	}
}

