// whitespace characters
var whitespace = " \t\n\r";
// Check whether string s is empty.
function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

// US Zips
function isValidUSZip(s)
{
	var reValid
	var reException
	var bValid
	
	
	reException = new RegExp(/^00000|000000000$/)
	reValid = new RegExp(/^\d{5}\D\d{4}$|^\d{5}$|^\d{9}$/)	
	
	bValid = reValid.test(s)
	
	if (bValid == true) {
		if (reException.test(s) == true) {
			bValid = false
		}
	}
	
	return bValid
	
}

// Valid CA format is a1a1a1 or Char Dig Char Dig Char Dig
function isValidCAZip(s)
{
	var reValid 
	reValid = new RegExp(/^\D\d\D\d\D\d$|^\D\d\D.\d\D\d$/i)	
	return reValid.test(s)
	
}

// Valid email is 2char(or more) @ 2char(or more) . 3-4 char
function isValidEmail(s)
{
	var reValid 
	reValid = new RegExp(/^.{2,}[@].{2,}[.].{2,4}$/i)	
	return reValid.test(s)
}

function isValidPhone(s)
{
	var reValid 
	reValid = new RegExp(/^[^0-1]{1}\d{9}$|^[^2-9]{1}\d{10}$/)	
	return reValid.test(s)
}

// Compares s to the valid 50 state abbreviations
function isValidUSState(s)
{
	var reValid 
	// Unfortunately this must all be on one line....
	reValid = new RegExp(/^AK$|^AL$|^AR$|^AZ$|^CA$|^CO$|^CT$|^DC$|^DE$|^FL$|^GA$|^HI$|^IA$|^ID$|^IL$|^IN$|^KS$|^KY$|^LA$|^MA$|^MD$|^ME$|^MI$|^MN$|^MO$|^MS$|^MT$|^NB$|^NC$|^ND$|^NH$|^NJ$|^NM$|^NV$|^NY$|^OH$|^OK$|^OR$|^PA$|^RI$|^SC$|^SD$|^TN$|^TX$|^UT$|^VA$|^VT$|^WA$|^WI$|^WV$|^WY$/i)
									
	return reValid.test(s)
}

// Compares s to the valid 13 province abbreviateions
function isValidCAState(s)
{
	var reValid 
	reValid = new RegExp(/^AB$|^BC$|^MB$|^NB$|^NL$|^NT$|^NS$|^NV$|^ON$|^PE$|^QC$|^SK$|^YT$/i)
	return reValid.test(s)
}


			
			// Users must enter a first and last name, if they do not they can not continue
			function CheckValidation()
			{
				exit = false;
				var strFirstName
				var strEmail
				
				strEmail = document.frmIndex.Email.value				
				strFirstName = document.frmIndex.fname.value
				
				if (isEmpty(strFirstName)) {
					alert("You must enter your name before proceeding.")
					return;
				}
				if ( !isEmail(strEmail) ) {
					alert("Invalid Email address.  Please re-enter.")
					return;
				}
				else {
					document.frmIndex.submit()
				}
			}

			function CheckCourse1()
			{
				exit = false;
				var strFirstName
				var strEmail
				
				strEmail = document.frmFreeCourse.Email.value				
				strFirstName = document.frmFreeCourse.fname.value
				
				if (isEmpty(strFirstName)) {
					alert("You must enter your name before proceeding.")
					return;
				}
				if ( !isEmail(strEmail) ) {
					alert("Invalid Email address.  Please re-enter.")
					return;
				}
				else {
					document.frmFreeCourse.submit()
				}
			}

			function CheckCourse2()
			{
				exit = false;
				var strFirstName
				var strEmail
				
				strEmail = document.frmFreeCourse2.Email.value				
				strFirstName = document.frmFreeCourse2.fname.value
				
				if (isEmpty(strFirstName)) {
					alert("You must enter your name before proceeding.")
					return;
				}
				if ( !isEmail(strEmail) ) {
					alert("Invalid Email address.  Please re-enter.")
					return;
				}
				else {
					document.frmFreeCourse2.submit()
				}
			}

			function CheckCourse3()
			{
				exit = false;
				var strFirstName
				var strEmail
				
				strEmail = document.frmFreeCourse3.Email.value				
				strFirstName = document.frmFreeCourse3.fname.value
				
				if (isEmpty(strFirstName)) {
					alert("You must enter your name before proceeding.")
					return;
				}
				if ( !isEmail(strEmail) ) {
					alert("Invalid Email address.  Please re-enter.")
					return;
				}
				else {
					document.frmFreeCourse3.submit()
				}
			}

			function CheckCourse4()
			{
				exit = false;
				var strFirstName
				var strEmail
				
				strEmail = document.frmFreeCourse4.Email.value				
				strFirstName = document.frmFreeCourse4.fname.value
				
				if (isEmpty(strFirstName)) {
					alert("You must enter your name before proceeding.")
					return;
				}
				if ( !isEmail(strEmail) ) {
					alert("Invalid Email address.  Please re-enter.")
					return;
				}
				else {
					document.frmFreeCourse4.submit()
				}
			}

			function CheckCourse5()
			{
				exit = false;
				var strFirstName
				var strEmail
				
				strEmail = document.frmFreeCourse5.Email.value				
				strFirstName = document.frmFreeCourse5.fname.value
				
				if (isEmpty(strFirstName)) {
					alert("You must enter your name before proceeding.")
					return;
				}
				if ( !isEmail(strEmail) ) {
					alert("Invalid Email address.  Please re-enter.")
					return;
				}
				else {
					document.frmFreeCourse5.submit()
				}
			}