<!--
// Validation Flags
 	// required field 1 = on  0 = off
		var require_dop = 1; // make date of purchase required (default = 1)					
		var require_dob = 0; // date of birth required (default = 0)
		var require_email = 0; // e-mail is a required field (default = 0)
		var require_phone = 0; // phone is a required field (default = 0)
	// other validation flags	
		var validte_dob = 1; // date of birth question exists but is only validated if they make a seletion (default = 1)
		var dop_exists = 0; // we set the flag so that dop does not exist if it exists this flag will be changed in the set flag function. 
		var dob_exists = 0; // we set the flag so that dob does not exist if it exists this flag will be changed in the set flag function. 
		var email2 = 1; // e-mail2 meta name may not have the 2  (default = 1) assues the 2 is there	
		var phone_exists = 0; // we assume that a phone question does notexist so the default is 0. Set to 1 if it does exist.
		var dob_drop = 1;				

// This function looks for questions that are validated if they are an the card, validation is turned on.
function setFlags()
{
var n = document.iRegForm.elements.length;
         
        for (var i = 0; i <= n-1; i++)
                {
                     var t = document.iRegForm.elements[i].type;
					 // Date of purchase check if the DOPYYYY e name exists then we set the flag to 1 					 
					 if (t == "select-one")
					 	{
					 		// Date of purchase check if the DOPYYYY e name exists then we set the flag to 1		
							if (document.iRegForm.elements[i].name == "DOPYYYY"){dop_exists = 1;}
							// Date of purchase check if the DOBYYYY e name exists in a select then we set the flag to 1 and leave dob_drop active		
							if (document.iRegForm.elements[i].name == "DOBYYYY"){dob_exists = 1;}
						}
					//Email v email2 check if the e anme is e_Email then we set the flag to 0
					 if (t == "text")
					 	{
					 		if (document.iRegForm.elements[i].name == "e_Email"){email2 = 0; }
							//Phone check if the e name e_PhoneArea e name exists then we set the flag to 1 
							if (document.iRegForm.elements[i].name == "e_PhoneArea"){phone_exists = 1;}
							// Date of purchase check if the DOBYYYY e name exists as text then we set the flag to 0
							// validation checks the year in a text field.
							if (document.iRegForm.elements[i].name == "DOBYYYY"){dob_drop = 0;}
						}
				}

}

		
// Global Variables
var defaultEmptyOK = false

// Global Functions
function isEmpty(s)
{	
	return ((s == null) || (s.length == 0))
}

function isDigit (c)
{	return ((c >= "0") && (c <= "9"))
}

function isInteger (s)
{	var i;
	if (isEmpty(s)) 
		if (isInteger.arguments.length == 1) return defaultEmptyOK;
		else return (isInteger.arguments[1] == true);
	// Search through string's characters one by one
	// until we find a non-numeric character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++)
	{
		// Check that current character is number.
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	// All characters are numbers.
	return true;
}

// State/Prov Validation Componets

function isStateCode(s)
{	
	var STvalue = document.iRegForm.ST_PROV.options.selectedIndex

	if (STvalue == 0) return false
		else return true;
}

// Zip Postal Code Validation Compnents
var digitsInZIPCode1 = 5
var digitsInZIPCode2 = 9
var digitsInCanPostCode = 6
var charsInCanPostCode = 7

function isZIPCode (s)
{
	if (isEmpty(s)) 
		if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
		else return (isZIPCode.arguments[1] == true);
	return (isInteger(s) && 
			((s.length == digitsInZIPCode1) ||
			 (s.length == digitsInZIPCode2)))
}
function isCanPostCode (s)
{ alert("isCanPostCode("+s+")");
	if (isEmpty(s)) 
		if (isCanPostCode.arguments.length == 1) return defaultEmptyOK;
		else return (isCanPostCode.arguments[1] == true);
	return (isInteger(s) && 
			((s.length == digitsInCanPostCode) ||
			 (s.length == charsInCanPostCode)))
}
function isUSCanPostCode (s)
{
	if (isEmpty(s)) {
		return defaultEmptyOK;
	}
	else if (isUSCanPostCode.arguments.length >= 1) {
		if (isZIPCode(s)) {
			return true;
			return isCanPostCode(s);
		}
	}
}

// E-mail Validation Components
<!-- Begin
function emailCheck (email) {

//trim spaces around the address
trimIn = email.value
trim =  trimIn.split(" ")
trimOut = trim.join("")
email.value = trimOut

var emailStr = email.value

/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	email.focus()
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The e-mail username doesn't seem to be valid.")
    email.focus()
	return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
			email.focus()
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain (the middle part name@domain.ext of the address) name in the e-mail address doesn't seem to be valid.")
	email.focus()
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
  var longDomains=new Array("name","info","aero","museum","coop","mobi") 
  var goodExt = 0;
  for (i in longDomains)
  {
  	if (domArr[domArr.length-1] == longDomains[i])
	{goodExt = 1}
  }
  

if ((goodExt != 1) && (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3)) {
	// the address must end in a two letter or three letter word.
   alert("The e-mail address must end in a valid extention.")
   email.focus()
   return false}
// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This e-mail address is missing a hostname!"
   email.focus()
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!

return true;
}
//  End -->

// DOB Validation Components

function isMonth ()
{   
var dbmonth = document.iRegForm.DOBMO.options.selectedIndex
	if (dbmonth == 0) return false;
}		

function isMonthEmpty ()
{   
var dbmonth = document.iRegForm.DOBMO.options.selectedIndex
	if (dbmonth == 0) return true;
}	

function isYear (y)
{   
	if (dob_drop == 1){
		var dbyear= document.iRegForm.DOBYYYY.options.selectedIndex
        	if (dbyear == 0) return true;
		 }
	
	else { 
		return (isInteger(y) && y.length == 4 && y <= 2002 && y > 1900)
		 }
}

function isYearEmpty (y)
{   
	if (dob_drop == 1){
		var dbyear= document.iRegForm.DOBYYYY.options.selectedIndex
        if (dbyear == 0) return true;
	}
	else
	{
		if(y.length == 0)	return true;
		}	
}

function isYearSelected ()
{   
var dbyear = document.iRegForm.DOBYYYY.options.selectedIndex
        if (dbyear == 0) return false;
}       

function isYearNotSelected ()
{   
var dbyear= document.iRegForm.DOBYYYY.options.selectedIndex
        if (dbyear == 0) return true;
}       


// Phone validation components


var digitsInUSPhoneNumber = 10;

function isUSPhoneNumber (s)
{  
	if (require_phone == 1)
		{return (isInteger(s) && s.length == digitsInUSPhoneNumber)}
	else if (s == null || s == "") return true;
		else
		{return (isInteger(s) && s.length == digitsInUSPhoneNumber)}
}

// Check Form
function checkform( thisform ) 
{
	// JavaScript Check. If they don't have JS then this value won't get changed.
	thisform.jsactive.value = "";
		
	// Title Validation
	var titleCheck = false;
	for(i=0;i<thisform.e_Title_List.length;i++)
		{
			if(thisform.e_Title_List[i].checked == true) var titleCheck = true;
		}
	if(titleCheck == false)
		{ alert('Please enter a valid title (Mr., Mrs., Ms., Miss)'); document.location.hash = "TITLE"; return false; }
		
	// Name Validation
		//First
	if(thisform.e_NAME_F.value == "") 
		{ alert('Please enter a valid first name'); thisform.e_NAME_F.focus(); return false; }
		//Last
	if(thisform.e_NAME_L.value == "")
		{ alert('Please enter a valid last name'); thisform.e_NAME_L.focus(); return false; }
	
	// Address Validation	
		//Street
	if(thisform.ADDRESS.value == "") 
		{ alert('Please enter a valid address'); thisform.ADDRESS.focus(); return false; }	
		//City
	if(thisform.CITY.value == "") 
		{ alert('Please enter a valid city'); thisform.CITY.focus(); return false; }	
		//State Prov
	if(isStateCode () == false) 
		{ alert('Please select a state'); thisform.ST_PROV.focus(); return false; }
		//Zip-Postal Code
	if(isUSCanPostCode ( thisform.ZIP_POST.value ) == false) 
		{ alert('Please enter a valid ZIP/Postal code'); thisform.ZIP_POST.focus(); return false; }
	
	// E-mail Validation (only if a value is entered)	
	if (email2 == 1)
		{	
	if (require_email == 1){
			{ if(emailCheck( thisform.e_Email2 )==false) return false; }
		}
	else {
			 if(thisform.e_Email2.value != "")
			{ if(emailCheck( thisform.e_Email2 )==false) return false; }	
  		 }
		}
	else
		{
		if (require_email == 1){
			{ if(emailCheck( thisform.e_Email )==false) return false; }
		}
	else {
			 if(thisform.e_Email.value != "")
			{ if(emailCheck( thisform.e_Email )==false) return false; }	
  		 }
		}	 
		 
	// Phone Validation
	if (phone_exists == 1){
		if(isUSPhoneNumber ( thisform.e_PhoneArea.value + thisform.e_PhoneExch.value + thisform.e_PhoneL4.value ) == false) 
		{ alert('Please enter a valid phone number'); thisform.e_PhoneArea.focus(); return false; }
	}
	
	// DOB Vlidation
	if (dob_exists == 1){
		if((isMonthEmpty () == true && isYearEmpty ( thisform.DOBYYYY.value ) == true) && require_dob == 0) 
			{
	 		
			}
		else
			{ 
				//Month
				if(isMonth () == false) 
					{ alert('Please select your birth month.'); thisform.DOBMO.focus(); return false; }	
			
				//Year Text
				if (dob_drop == 1){	
					if(isYearSelected () == false) 
           		     	{ alert('Please select birth year.'); thisform.DOBYYYY.focus(); return false; }
			 	  }
				else {
					if(isYear ( thisform.DOBYYYY.value ) == false) 
						{ alert('Please enter a valid four digit birth year. For example 1950'); thisform.DOBYYYY.focus(); return false; }
					 } 
			}
		}		 		
	
	// Date of purchase Validation
	if (dop_exists == 1){
		var	MValue = thisform.e_DOP_MM.options[thisform.e_DOP_MM.options.selectedIndex].value;
		var	DValue = thisform.e_DOP_DD.options[thisform.e_DOP_DD.options.selectedIndex].value;
		var	YValue = thisform.DOPYYYY.options[thisform.DOPYYYY.options.selectedIndex].value;
		if(require_dop == 1){
			if (MValue == 0)
		{alert("Please select a month for the date of purchase."); thisform.e_DOP_MM.focus(); return false; }
			if (DValue == 0)
		{alert("Please select a day for the date of purchase."); thisform.e_DOP_DD.focus(); return false; }
			if (YValue == 0)
		{alert("Please select a year for the date of purchase."); thisform.DOPYYYY.focus(); return false; }
		    }
	}
		
	// This will look on the page for any addtional custom scripting to be performed on subission.
	if (custom( thisform )== false){return false;}
	
	//Tell the form it is OK to submit
	return true;
}
//-->

