var USA = 0;
var CANADA = 1;
var OTHER = 2;

function pictureWindow(url) {
	window.open(url,'picturewindow','directories=no,height=500,width=660,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no').focus();
}

function validateName(myfield) {
	if (notNull(myfield.value)) {
		return true	
	}
	else {
		alert('Please make sure you enter your First and Last Names.')
		return false
	}
}

function validateZip(myfield,country) {
	if (country == USA) {
		// Validate according to US Postal ZIP code
		if (notNull(myfield.value)) {
			newstring = stripNonDigits(myfield.value)
			if (isSize(newstring,5) || isSize(newstring, 9)) 
				return true
		}
		myfield.focus()
		alert("Invalid zip code. Please enter 5-digit or 9-digit zip code.")
		return false
	}
	else if (country == CANADA) {
		// Validate according to Canadian Postal ZIP code
		if (notNull(myfield.value)) {
			var firstthree = myfield.value.substr(0,3)
			var lastthree = myfield.value.substr(myfield.value.length-3)
			// D, F, I, O, Q, and U are never used in Canadian postal codes. If
			// one of these letters appears in your records, it is inaccurate and
			// should be corrected.
			var validAlphabet = 'ABCEGHJKLMNPRSTVWXYZ';
			
			// The letters W and Z are not used as the first letters of postal codes;
			if ((firstthree.substr(0,1).indexOf('W') == -1) &&
			    (firstthree.substr(0,1).indexOf('X') == -1)) {
				// Canadian postal codes are always listed in the same format:
				// K1A 0B1, for example. The sequence is always Alphabetical
				// character/Number/Alpha (full space) Number/Alpha/Number.
				if ((validAlphabet.indexOf(firstthree.substr(0,1)) != -1) &&
				    isDigits(firstthree.substr(1,1)) &&
				    (validAlphabet.indexOf(firstthree.substr(2,1)) != -1) &&
				    isDigits(lastthree.substr(0,1)) &&
				    (validAlphabet.indexOf(lastthree.substr(1,1)) != -1) &&
				    isDigits(lastthree.substr(2,1))) {
					return true
				}
			}
		}
		myfield.focus()
		alert("Invalid Canadian Postal Code.  Please enter a valid Canadian Postal Code.")
		return false
	}
	myfield.value = ''
	return true
}

function stripNonDigits(str) {
	var i
	var newstring = ""
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i)
		if (isDigits(mychar)) 
			newstring += mychar
	}
	return newstring
}

function isSize(str, size) {
	if (str.length == size) 
		return true
	else
		return false
}

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

function notNull(str) {
	if (str.length == 0)
		return false
	else 
		return true
}

// -----------------------------------------------------------------
// Function    : IsEmailValid
// Language    : JavaScript
// Description : Checks if given email address is of valid syntax
// Copyright   : (c) 1998 Shawn Dorman
// http://www.goodnet.com/~sdorman/web/IsEmailValid.html
// -----------------------------------------------------------------
// Ver    Date    Description of modification
// --- ---------- --------------------------------------------------
// 1.0 09/04/1996 Original write
// 1.1 09/30/1998 CHG: Use standard header format
// -----------------------------------------------------------------
// Source: Webmonkey Code Library
// (http://www.hotwired.com/webmonkey/javascript/code_library/)
// -----------------------------------------------------------------

function IsEmailValid(theForm,ElemName)
{
	var EmailOk  = true
	var Temp     = theForm.elements[ElemName]
	var AtSym    = Temp.value.indexOf('@')
	var Period   = Temp.value.lastIndexOf('.')
	var Space    = Temp.value.indexOf(' ')
	var Length   = Temp.value.length - 1   // Array is from 0 to length-1

	if ((AtSym < 1) ||                     // '@' cannot be in first position
	    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
	    (Period == Length ) ||             // Must be atleast one valid char after '.'
	    (Space  != -1))                    // No empty spaces permitted
	   {  
	      EmailOk = false
	      alert('Please enter a valid e-mail address.')
	      Temp.focus()
	   }
	return EmailOk
}

var STATECODES = "AL/AK/AZ/AR/CA/CO/CT/DE/DC/FL/GA/HI/ID/IL/IN/IA/KS/" +
                 "KY/LA/ME/MD/MA/MI/MN/MS/MO/MT/NE/NV/NH/NJ/NM/NY/NC/" +
                 "ND/OH/OK/OR/PA/RI/SC/SD/TN/TX/UT/VT/VA/WA/WV/WI/WY";

var CANADACODES = "AB/BC/MB/NB/NF/NT/NS/ON/PE/QC/SK/YT";

function setCountry(theForm) {
	var selectedStateIndex = theForm.state.selectedIndex;
	var selectedState = theForm.state[selectedStateIndex].value;
	if (STATECODES.indexOf(selectedState) != -1 && selectedState.indexOf("/") == -1)
		theForm.country.selectedIndex = USA;
	else if (CANADACODES.indexOf(selectedState) != -1 && selectedState.indexOf("/") == -1)
		theForm.country.selectedIndex = CANADA;
	else
		theForm.country.selectedIndex = OTHER;
}

