var requiredFields = new Array("txtGBXUserId", "txtPassword", "txtFirstName", "txtLastName", "txtEmail", "txtPhone", "txtAddress", "txtCity", "txtZIP"); 
var fieldNames = new Array("Login ID", "Password", "First Name", "Last Name", "Email Address", "Telephone", "Mailing Address", "City", "Zip / Postal Code");  
var FormOK = LoginOK = PasswordOK = FNameOK = LNameOK = EmailOK = PhoneOK = AddressOK = CityOK = ZipOK = true; 

function checkFields(input) {

	//check email and confirm email field values match
	if (document.AForm.txtEmail.value != document.AForm.txtEmailConfirm.value) {
		alert("The Email and Confirm Email fields do not match"); 
		return false;  
	}
					
	Coun = (document.AForm.Country.selectedIndex)-0;
	if (Coun == 0){
		if (document.AForm.Country.value == "") { alert("The Country field requires valid input"); return false;  }
	}	
	
	StateProv = (document.AForm.State.selectedIndex)-0;
	if (StateProv == 0){
		if (document.AForm.State.value == "") { 
			if (document.AForm.Country.value == "United States") {
				alert("The Region / State / Province field requires valid input"); return false;  
			}
			if (document.AForm.Country.value == "Canada") {
				alert("The Region / State / Province field requires valid input"); return false;  
			}
		}
	}
	
	ut = (document.AForm.UserType.selectedIndex)-0;
	if (ut == 0){
		if (document.AForm.UserType.value == "") { alert("The User Type field requires valid input"); return false;  }
	}	
	if (ut == 3){
		if (document.AForm.txtCompanyName.value == "") { alert("The Company Name field requires valid input"); return false;  }
	}	
				
	if (!(checkRequiredFieldsBC(input))) {return false;}	
	if (!(LoginOK)) { alert("The Login ID field requires valid input"); return false; }
	if (!(PasswordOK)) { alert("The Password field requires valid input"); return false; }
	if (!(FNameOK)) { alert("The First Name field requires valid input"); return false; }
	if (!(LNameOK)) { alert("The Last Name field requires valid input"); return false; }
	if (!(EmailOK)) { alert("The Email Address field requires valid input"); return false; } 
	//if (!(PhoneOK)) { alert("The Telephone field requires valid input"); return false; }
	if (!(PhoneOK)) { alert("The Telephone field requires valid input - Phone Format: 1112223333"); return false; }
	if (!(AddressOK)) { alert("The Mailing Address field requires valid input"); return false; }
	if (!(CityOK)) { alert("The Company City field requires valid input"); return false; }
	if (!(ZipOK)) { alert("The Company ZIP / Postal Code field requires valid input"); return false; }
	else {
	return true; 
	}
}	
		
function checkRequiredFieldsBC(input)
{             
   
    var fieldCheck   = true;
    var fieldsNeeded = "\nA value must be entered in the following field(s):\n\n\t";

    for(var fieldNum=0; fieldNum < requiredFields.length; fieldNum++) {
        if ((input.elements[requiredFields[fieldNum]].value == "") ||
            (input.elements[requiredFields[fieldNum]].value == " ")) {

            fieldsNeeded += fieldNames[fieldNum] + "\n\t";
            fieldCheck = false;
        }
    }
  
    if (fieldCheck == true)
    {
		FormOK = true
        return true;
    }

    else
    {
		FormOK = false
        alert(fieldsNeeded);
        return false;
    }
}


function changeRegionStProv() {
	
	Coun = document.AForm.Country
	RSP = document.AForm.State
	
	with (Coun) {
		switch (selectedIndex){
			//Canada
			//case 39:
			case 2:
				for (var i = RSP.options.length; i > 0; i--) {
					RSP.options[i] = null;
				}
				RSP.options[0] = new Option("Select a Province","")
				RSP.options[1] = new Option("Alberta","Alberta")
				RSP.options[2] = new Option("British Columbia","British Columbia")
				RSP.options[3] = new Option("Manitoba","Manitoba")
				RSP.options[4] = new Option("New Brunswick","New Brunswick")
				RSP.options[5] = new Option("Newfoundland","Newfoundland")
				RSP.options[6] = new Option("Northwest Territories","Northwest Territories")
				RSP.options[7] = new Option("Nova Scotia","Nova Scotia")
				RSP.options[8] = new Option("Nunavut","Nunavut")
				RSP.options[9] = new Option("Ontario","Ontario")
				RSP.options[10] = new Option("Prince Edward Island","Prince Edward Island")
				RSP.options[11] = new Option("Quebec","Quebec")
				RSP.options[12] = new Option("Saskatchewan","Saskatchewan")
				RSP.options[13] = new Option("Yukon Territory","Yukon Territory")
				RSP.options[0].selected = true;
				break
			//United States	
			//case 235:
			case 1:
				for (var i = RSP.options.length; i > 0; i--) {
					RSP.options[i] = null;
				}
				RSP.options[0] = new Option("Select a State","")
				RSP.options[1] = new Option("Alabama","Alabama")
				RSP.options[2] = new Option("Alaska","Alaska")
				RSP.options[3] = new Option("Arizona","Arizona")
				RSP.options[4] = new Option("Arkansas","Arkansas")
				RSP.options[5] = new Option("California","California")
				RSP.options[6] = new Option("Colorado","Colorado")
				RSP.options[7] = new Option("Connecticut","Connecticut")
				RSP.options[8] = new Option("Delaware","Delaware")
				RSP.options[9] = new Option("Dist. of Columbia","Dist. of Columbia")
				RSP.options[10] = new Option("Florida","Florida")
				RSP.options[11] = new Option("Georgia","Georgia")
				RSP.options[12] = new Option("Hawaii","Hawaii")
				RSP.options[13] = new Option("Idaho","Idaho")
				RSP.options[14] = new Option("Illinois","Illinois")
				RSP.options[15] = new Option("Indiana","Indiana")
				RSP.options[16] = new Option("Iowa","Iowa")
				RSP.options[17] = new Option("Kansas","Kansas")
				RSP.options[18] = new Option("Kentucky","Kentucky")
				RSP.options[19] = new Option("Louisiana","Louisiana")
				RSP.options[20] = new Option("Maine","Maine")
				RSP.options[21] = new Option("Maryland","Maryland")
				RSP.options[22] = new Option("Massachusetts","Massachusetts")
				RSP.options[23] = new Option("Michigan","Michigan")
				RSP.options[24] = new Option("Minnesota","Minnesota")
				RSP.options[25] = new Option("Mississippi","Mississippi")
				RSP.options[26] = new Option("Missouri","Missouri")
				RSP.options[27] = new Option("Montana","Montana")
				RSP.options[28] = new Option("Nebraska","Nebraska")
				RSP.options[29] = new Option("Nevada","Nevada")
				RSP.options[30] = new Option("New Hampshire","New Hampshire")
				RSP.options[31] = new Option("New Jersey","New Jersey")
				RSP.options[32] = new Option("New Mexico","New Mexico")
				RSP.options[33] = new Option("New York","New York")
				RSP.options[34] = new Option("North Carolina","North Carolina")
				RSP.options[35] = new Option("North Dakota","North Dakota")
				RSP.options[36] = new Option("Ohio","Ohio")
				RSP.options[37] = new Option("Oklahoma","Oklahoma")
				RSP.options[38] = new Option("Oregon","Oregon")
				RSP.options[39] = new Option("Pennsylvania","Pennsylvania")
				RSP.options[40] = new Option("Rhode Island","Rhode Island")
				RSP.options[41] = new Option("South Carolina","South Carolina")
				RSP.options[42] = new Option("South Dakota","South Dakota")
				RSP.options[43] = new Option("Tennessee","Tennessee")
				RSP.options[44] = new Option("Texas","Texas")
				RSP.options[45] = new Option("Utah","Utah")
				RSP.options[46] = new Option("Vermont","Vermont")
				RSP.options[47] = new Option("Virginia","Virginia")
				RSP.options[48] = new Option("Washington","Washington")
				RSP.options[49] = new Option("West Virginia","West Virginia")
				RSP.options[50] = new Option("Wisconsin","Wisconsin")
				RSP.options[51] = new Option("Wyoming","Wyoming")
				RSP.options[0].selected = true;
				break		
			default:
				for (var i = RSP.options.length; i >= 0; i--) {
					RSP.options[i] = null;
				}
				RSP.options[0] = new Option("-----","")				
				break
		}
	}
}

// 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 (ifEmpty(s)) 
       if (isEmail.arguments.length == 1) return alert('Please Enter Email Address');
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return alert('invalid email1'); //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 alert('Invalid Email Address without  "@" '); //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 alert('Invalid Email Address without  "." '); //false;
    else return true;
}	

// Check whether string s is empty.
function ifEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)
{   
	var i;
	var whitespace = " \t\n\r";
	
    // Is s empty?
    if (ifEmpty(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;
}

function checkPassword(checkString) 
{
	if ((checkString.length >= 4) && (checkString.length <=8)) 
		{ return checkString; }
	else
		{ 
		 alert('Invalid Password, Password must be 4 - 8 characters in length');
		return (''); 
		}
}

function checkPassword2(checkString) 
{
	//also check for spaces here
	var tot3=0
	
	for (j = 0; j < checkString.length ; j++) 
	{
    	ch2 = checkString.substring(j, j+1);
        
	if (!( (ch2 >= "a" && ch2 <= "z") || (ch2 >= "A" && ch2 <= "Z" )  || (ch2 >="0"  && ch2 <= "9") )  )
		{
	   tot3=tot3+1;
		}
	}
	
	if (tot3>=1)
	{
		alert('Do not use Spaces or AlphaNumeric Characters in the Password field');    	
		document.AForm.txtPassword.value = "" ;
	}
	
	if ((checkString.length >= 4) && (checkString.length <=8)) 
		{ return checkString; }
	else
		{ 
		 alert('Invalid Password, Password must be 4 - 8 characters in length');
		 document.AForm.txtPassword.value = "" ;
		 //document.AForm.txtPassword.focus() ; 
		////return (''); 
		}	
}

function checkLoginId(checkString) 
{
	//also check for spaces here
	var tot4=0
	
	for (j = 0; j < checkString.length ; j++) 
	{
    	ch4 = checkString.substring(j, j+1);
        
	if (!( (ch4 >= "a" && ch4 <= "z") || (ch4 >= "A" && ch4 <= "Z" )  || (ch4 >="0"  && ch4 <= "9") )  )
		{
	   tot4=tot4+1;
		}
	}
	
	if (tot4>=1)
	{
		alert('Do not use Spaces or AlphaNumeric Characters in the Login ID field');    	
		document.AForm.txtGBXUserId.value = "" ;
		//document.AForm.txtGBXUserId.focus() ; 
	}
		
}

function checkLoginId2(checkString) 
{
	//also check for spaces here
	var tot3=0
	
	for (j = 0; j < checkString.length ; j++) 
	{
    	ch2 = checkString.substring(j, j+1);
        
	if (!( (ch2 >= "a" && ch2 <= "z") || (ch2 >= "A" && ch2 <= "Z" )  || (ch2 >="0"  && ch2 <= "9") )  )
		{
	   tot3=tot3+1;
		}
	}
	
	if (tot3>=1)
	{
		alert('Do not use Spaces or AlphaNumeric Characters in the Login ID field');    	
		document.AForm.txtGBXUserId.value = "" ;
	}
	
}
