// JavaScript Document
// Author : Omar Smith

// When the form is loaded, the initForm function is called
window.onload = initForm;

// Puts all required input fields into an array for error checking
function initForm() {
	var allTags = document.getElementsByTagName("*");
	//focusIt("zipCode");
	for (var i=0; i<allTags.length; i++) {
		if (allTags[i].className.indexOf("reqd") > -1) {
			allTags[i].onblur = fieldCheck;
		}
	}
}

//Checks each required field and highlights if errors found 
function fieldCheck() {
	
	//Checks if the email field is selected and performs validation
	if ((this.value != "") && (this.id=="email")) 
	{
		var tfld = trim(this.value); // value of field with whitespace trimmed off
		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ; //accepted email format eg. abc@lirr.org
    	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ; //characters an email is not allowed to have
		
		//If the email does not conform to the accepted format highlight it
		if (!emailFilter.test(tfld)) {              
        	this.className += " highlight";
			document.getElementById("errorArea").innerHTML = "&raquo; Invalid Email address entered.";
    	}
		
		//Tests for illegal characters in the email address
		else if (illegalChars.test(tfld)) {              
        	this.className += " highlight";
			document.getElementById("errorArea").innerHTML = "&raquo; Illegal Characters found in Email address.";
    	}
		
		//Email address is in correct format
		else
		{ 
			this.className = "reqd";
			document.getElementById("errorArea").innerHTML = "";
		}
	}
	
	//Validates the Zip Code entered
	else if ((this.value != "") && (this.id=="zipCode"))
	{
		var zfld = trim(this.value);
		var zipFilter = /^(\d{5})/;
		
		//checks if zip code is a 5 digit number
		if (!zipFilter.test(zfld)) {              
        	this.className += " highlight";
			document.getElementById("errorArea").innerHTML = "&raquo; Zip Code must contain 5 digits.";
    	}
		//Zip code is good
		else
		{ 
			this.className = "reqd";
			document.getElementById("errorArea").innerHTML = "";
		}
	}
	
	//Checks the phone entry fields for correct format
	else if ((this.value != "") && ((this.id=="dayPhone")||(this.id=="dayPhone2")||(this.id == "dayPhone3")))
	{
		var pfld = trim(this.value);
		var phoneFilter = /^(\d{3})/;
		
		//checks if this is the 3rd phone field which takes 4 digits
		if (this.id == "dayPhone3")
		{
			phoneFilter = /^(\d{4})/;
		}
		
		//tests the phone fields for correct format
		if (!phoneFilter.test(pfld)) {              
        	this.className += " highlight";
			document.getElementById("errorArea").innerHTML = "&raquo; Invalid Phone Number";
    	}
		// Phone fields are good
		else
		{ 
			this.className = "reqd";
			document.getElementById("errorArea").innerHTML = "";
		}
	}
	//Default Case where the required field is empty u leave the field unhilighted
	else 
	{
		this.className = "reqd";
		document.getElementById("errorArea").innerHTML = "";
	}
	
}

// Display Processing... when the submit button is presssed
function displayMsg()
{
	document.getElementById("errorArea").className = "successMsg";
	document.getElementById("errorArea").innerHTML = "&raquo; Processing your entry ...";
}

//Only accept numbers to be pressed/entered in a specified field
function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57)){
			this.className += " highlight";
			document.getElementById("errorArea").className = "errorArea";
			document.getElementById("errorArea").innerHTML = "&raquo; This field only accepts numbers!";
			return false;
		 }
		 else
         return true;
      }

//function to remove white spaces
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

//Auto tab when field limit is reached
var phone_field_length=0;
function TabNext(obj,event,len,next_field) 
{
	if (event == "down") {
		phone_field_length=obj.value.length;
		}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
			if (phone_field_length == len) {
				next_field.focus();
				}
			}
		}
}

//Clears the fields when the reset button is clicked
function clearFields(){
	document.anniversaryGiveaway.firstName.value="";
	document.anniversaryGiveaway.lastName.value="";
	document.anniversaryGiveaway.addr1.value="";
	document.anniversaryGiveaway.addr2.value="";
	document.anniversaryGiveaway.city.value="";
	document.anniversaryGiveaway.state.value="NY";
	document.anniversaryGiveaway.zipCode.value="";
	document.anniversaryGiveaway.email.value="";
	document.anniversaryGiveaway.dayPhone.value="";
	document.anniversaryGiveaway.dayPhone2.value="";
	document.anniversaryGiveaway.dayPhone3.value="";
	document.anniversaryGiveaway.ageRuleAgreement.checked="";
	document.getElementById("errorArea").style.color = 'darkblue';
	document.getElementById("errorArea").innerHTML = "&raquo; Please fill in all fields.";
	document.anniversaryGiveaway.zipCode.className = "reqd";
	document.anniversaryGiveaway.email.className = "reqd";
	document.anniversaryGiveaway.dayPhone.className = "reqd";
	document.anniversaryGiveaway.dayPhone2.className = "reqd";
	document.anniversaryGiveaway.dayPhone3.className = "reqd"; 
}

function clearError(){
	document.getElementById("errorArea").innerHTML = "";	
}

function focusIt(mytext)
{
  if (mytext == "dayPhone")
  {
	  this.document.getElementById("dayPhone2").className += " highlight";
	  this.document.getElementById("dayPhone3").className += " highlight";
  }
  this.document.getElementById(mytext).focus();
  this.document.getElementById(mytext).className += " highlight";
}