/******************************************************************************/
/* COOKIE CONTROLS START */
/******************************************************************************/


//Automaticaly move cursor to the next field
function moveOnMax(field,nextFieldID){
  if(field.value.length >= field.maxLength){
    document.getElementById(nextFieldID).focus();
  }
}


// Set cookie
function cookie_set (name, value)
{
	var path = "/";
	
	// Set the cookie
	document.cookie = name + "=" + escape (value) + "; path=" + path;
}



// Check for session cookie
function cookie_get(name) 
{ 
	// Check for cookie 
	var index = document.cookie.indexOf(name + "=");
    if (index == -1) return null;

	// Extract the cookie
	index = document.cookie.indexOf("=", index) + 1;
	var endstr = document.cookie.indexOf(";", index);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(index, endstr));
}


// Set the cookies
function cookie_init()
{
	// Declare cookie vars
	et_first_name_val = cookie_get("et_first_name_var");
	et_last_name_val = cookie_get("et_last_name_var");	
	et_email_val = cookie_get("et_email_var");	
	et_zip_code_val = cookie_get("et_zip_code_var");		
	et_list_id_val = cookie_get("et_list_id_var");	

	
	// Set the document.cookie to the form values
	cookie_set("et_first_name_var", document.subscribeForm["First Name"].value);
	cookie_set("et_last_name_var", document.subscribeForm["Last Name"].value);
	cookie_set("et_email_var", document.subscribeForm["Email Address"].value);
	cookie_set("et_zip_code_var", document.subscribeForm["Zipcode"].value);
	cookie_set("et_list_id_var", document.subscribeForm["Check 1"].value);
}	


/******************************************************************************/
/* COOKIE CONTROLS END */
/******************************************************************************/


/******************************************************************************/
/* FORM VALIDATION START */
/******************************************************************************/


// Declare validation vars
var error
var bag = "";
var acceptChars ="0123456789./-() "
var acceptNum = "0123456789"
var zipBag = ""
var acceptAreaCodeNum = "0123456789"
var acceptPhoneNum = "0123456789.-()"


function checkTipForm()
{
	if (!checkFirstName())
	{
		alert(error);
		document.ohsTipForm["FirstName"].focus();
		return false
	}
	else if (!checkLastName())
	{
		alert(error);
		document.ohsTipForm["LastName"].focus();
		return false
	}
	else if (!checkPhone())
	{
		alert(error);
		document.ohsTipForm["phone1"].focus();
		return false
	}
	else if (!checkEmail())
	{
		alert(error);
		document.ohsTipForm["Email"].focus();
		return false
	}
	else if (!checkFormTip())
	{
		alert(error);
		document.ohsTipForm["tip"].focus();
		return false
	}
	else if (!checkZip())
	{
		alert(error);
		document.ohsTipForm["Zip"].focus();
		return false
	}	
	else
	{
		// Set cookies in the event of an error
		cookie_init();
		return true
	}
}


//did you enter your first name?
function checkFirstName()
{
	if (document.ohsTipForm["FirstName"].value == "")
	{
		error = "Please enter your first name."
		return false
	}
	else
	{
		return true
	}
}


//did you enter your last name?
function checkLastName()
{
	if (document.ohsTipForm["LastName"].value == "")
	{
		error = "Please enter your last name."
		return false
	}
	else
	{
		return true
	}
}


//did you enter your telephone number?
function checkPhone()
{
	var phoneNum = document.ohsTipForm["phone1"].value;
	phoneNum += document.ohsTipForm["phone2"].value;
	phoneNum += document.ohsTipForm["phone3"].value;
	var p = parseInt(phoneNum);
	if (phoneNum == "")
	{
		error = "Please enter your telephone number."
		return false
	}
	else if (isNaN(document.ohsTipForm["phone1"].value) || isNaN(document.ohsTipForm["phone2"].value) || isNaN(document.ohsTipForm["phone3"].value))
	{
		error = "Please enter a valid telephone number."
		document.ohsTipForm["phone1"].value = "";
		document.ohsTipForm["phone2"].value = "";
		document.ohsTipForm["phone3"].value = "";
		return false
	}
	else if (phoneNum.length < 10)
	{
		error = "Phone number not long enough, please re-enter your telephone number."
		document.ohsTipForm["phone1"].value = "";
		document.ohsTipForm["phone2"].value = "";
		document.ohsTipForm["phone3"].value = "";
		return false
	}
	else
	{
		return true;
	}
}


//did you enter an email address?
function checkEmail()
{
	var formEmail = document.ohsTipForm["Email"].value
	if (formEmail == "")
	{
		error = "Please enter an email address."
		return false
	}
	else if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.ohsTipForm["Email"].value))
	{
		return (true)
	}
	else
	{
		error = "Please enter a valid email address."
		return false
	}
}


//did you enter your last name?
function checkFormTip()
{
	if (document.ohsTipForm["tip"].value == "")
	{
		error = "Please enter a tip."
		return false
	}
	else
	{
		return true
	}
}

//Is the zip code formatted correctly?
function checkZip()
{
	var formZip = document.ohsTipForm["Zip"].value;
	var x = parseInt(formZip);
		
	if (x<9999 || isNaN(x))
	{
	    error = "Please enter a 5-digit zip code in the correct format.";
		document.ohsTipForm["Zip"].value = "";
		return false;
	}
	else
	{
		return true
	}
}


/******************************************************************************/
/* FORM VALIDATION END */
/******************************************************************************/
