﻿//author cgomez 15052008	

// States
var aStates = new Array();

aStates[0] = 'AL';
aStates[1] = 'AK';
aStates[2] = 'AZ';
aStates[3] = 'AR';
aStates[4] = 'CA';
aStates[5] = 'CO';
aStates[6] = 'CT';
aStates[7] = 'DE';
aStates[8] = 'DC';
aStates[9] = 'FL';
aStates[10] = 'GA';
aStates[11] = 'HI';
aStates[12] = 'ID';
aStates[13] = 'IL';
aStates[14] = 'IN';
aStates[15] = 'IA';
aStates[16] = 'KS';
aStates[17] = 'KY';
aStates[18] = 'LA';
aStates[19] = 'ME';
aStates[20] = 'MD';
aStates[21] = 'MA';
aStates[22] = 'MI';
aStates[23] = 'MN';
aStates[24] = 'MS';
aStates[25] = 'MO';
aStates[26] = 'MT';
aStates[27] = 'NE';
aStates[28] = 'NV';
aStates[29] = 'NH';
aStates[30] = 'NJ';
aStates[31] = 'NM';
aStates[32] = 'NY';
aStates[33] = 'NC';
aStates[34] = 'ND';
aStates[35] = 'OH';
aStates[36] = 'OK';
aStates[37] = 'OR';
aStates[38] = 'PA';
aStates[39] = 'RI';
aStates[40] = 'SC';
aStates[41] = 'SD';
aStates[42] = 'TN';
aStates[43] = 'TX';
aStates[44] = 'UT';
aStates[45] = 'VT';
aStates[46] = 'WA';
aStates[47] = 'WV';
aStates[48] = 'WI';
aStates[49] = 'WY';
aStates[50] = 'VA';

//Original labels color
var sRightColor = "#ffffff";
//Error labels color
var sWrongColor = "#ff3333";

// Submit the form when data is right
function sendSubmit(bValidate, bAgree, bPOBox){

    if (!bAgree)
	{
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML = "<p>You must agree to the Master the Flavor Giveaway Rules before entering the sweepstakes.</p>";
        return false;
	}
	else if (!bValidate)
	{
		document.getElementById("error").style.display = "block";
		if (!bPOBox)
		    document.getElementById("error").innerHTML = "<p>Some fields were incomplete or missing. Please correct the highlighted fields and re-submit the form.</p>";
	    else
	        document.getElementById("error").innerHTML = "<p>We're sorry, addresses containing PO Boxes cannot be used.</p>";	    
	
        return false;
	}
	else
		document.getElementById("sendForm").submit();
}

// Validate the fields
function validate(){

	resetFields();

	var bReturn = true;
	var bAgree = true;
	var bPOBox = false;

	if (document.getElementById(txtNameID).value == ""){
		document.getElementById("firstname").style.color = sWrongColor;
		bReturn = false;
	}
	if (document.getElementById(txtLastnameID).value == ""){
		document.getElementById("lastname").style.color = sWrongColor;
		bReturn = false;
	}
	
	var addressElem = document.getElementById(txtAddressID);
	var addressText = addressElem.value.toLowerCase();
	
	if (addressText == ""){
		document.getElementById("address").style.color = sWrongColor;
		bReturn = false;
	}
	
	if (addressText.indexOf("po box") != -1 ||
	    addressText.indexOf("p.o. box") != -1)
	{
	    document.getElementById("address").style.color = sWrongColor;
		bReturn = false;
		bPOBox = true;
	}
	    
	
	
	if (document.getElementById(txtAddressID).value == ""){
		document.getElementById("address").style.color = sWrongColor;
		bReturn = false;
	}
	
	if (document.getElementById(cmbStateID).value == "0"){
		document.getElementById("state").style.color = sWrongColor;
		bReturn = false;
	}
	if (document.getElementById(txtCityID).value == ""){
		document.getElementById("city").style.color = sWrongColor;
		bReturn = false;
	}
	if (document.getElementById(txtCodeID).value == "" || document.getElementById(txtCodeID).value.length < 5 ){
		document.getElementById("zipcode").style.color = sWrongColor;
		bReturn = false;
	}
	if (document.getElementById(txtEmailID).value == "" || !validateEmail(document.getElementById(txtEmailID).value)){
		document.getElementById("email").style.color = sWrongColor;
		bReturn = false;
	}
	if (document.getElementById(txtConfirm_emailID).value == "" || !validateEmail(document.getElementById(txtConfirm_emailID).value) ){
		document.getElementById("emailcopy").style.color = sWrongColor;
		bReturn = false;
	}
	else if (document.getElementById(txtConfirm_emailID).value != document.getElementById(txtEmailID).value){
		document.getElementById("email").style.color = sWrongColor;
		document.getElementById("emailcopy").style.color = sWrongColor;
		bReturn = false;
	}
	
	if (!document.getElementById(cbAgreeID).checked)
	{
	    document.getElementById(cbAgreeID).style.color = sWrongColor;
	    bAgree = false;
	}
	
	
	sendSubmit(bReturn, bAgree, bPOBox);

	return bReturn;
}

// Set the fields like the original
function resetFields(){
	document.getElementById("error").style.display = "none";
	document.getElementById("firstname").style.color = sRightColor;
	document.getElementById("lastname").style.color = sRightColor;
	document.getElementById("address").style.color = sRightColor;
	document.getElementById("state").style.color = sRightColor;
	document.getElementById("city").style.color = sRightColor;
	document.getElementById("zipcode").style.color = sRightColor;
	document.getElementById("email").style.color = sRightColor;
	document.getElementById("emailcopy").style.color = sRightColor;
}

// Restrict the zip code only to write numbers
function validateNumbers(evt){
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
		((evt.which) ? evt.which : 0));
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		return false;
	}
	return true;
}

// Validate the email format
function validateEmail(sEmail) {
	var at = "@";
	var dot = ".";
	var lat = sEmail.indexOf(at);
	var lstr = sEmail.length;
	var ldot = sEmail.indexOf(dot);
	
	if (sEmail.indexOf(at) == -1){
	   return false;
	}

	if (sEmail.indexOf(at) == -1 || sEmail.indexOf(at) == 0 || sEmail.indexOf(at) == lstr){
	   return false;
	}

	if (sEmail.indexOf(dot)==-1 || sEmail.indexOf(dot)==0 || sEmail.indexOf(dot)==lstr){
		return false;
	}

	 if (sEmail.indexOf(at, (lat + 1)) != -1){
		return false;
	 }

	 if (sEmail.substring(lat - 1, lat) == dot || sEmail.substring(lat + 1, lat + 2) == dot){
		return false;
	 }
	 
	 if (sEmail.substring(lstr, lstr - 1) == dot){
		return false;
	 }

	 if (sEmail.indexOf(dot, (lat + 2)) == -1){
		return false;
	 }
	
	 if (sEmail.indexOf(" ") != -1){
		return false;
	 }

	 return true;				
}