function enableForm() {
	//document.getElementById("form1:submitButton").style.display = "inline";
	document.forms[0].sample[0].disabled = false;
	document.forms[0].sample[1].disabled = false;
	document.forms[0].sample[2].disabled = false;
	document.forms[0].sample[3].disabled = false;	
}
// define some error codes
var cw_FieldNoError		= 0;
var cw_FieldEmptyError 		= 1;
var cw_FieldIntError 		= 2;
var cw_FieldLenError		= 3;
var cw_FieldEmailFormatError	= 4;
var cw_FieldConfirmEmailError	= 5;
var cw_OptionNotSelectedError	= 6;
var cw_FieldAlphaError		= 7;


// Here are the variables that will be set to one of the above error codes.

var cw_emailFormatError;
var cw_confirmEmailError;
var cw_firstNameError;
var cw_lastNameError;
var cw_dobError;
var cw_genderError;
var cw_address1Error;
var cw_cityError;
var cw_stateError;
var cw_ZIPError;
var cw_sampleError;


function cw_InitErrors() {
	// Set Optimistic

	cw_emailFormatError 	= cw_FieldNoError;
	cw_confirmEmailError 	= cw_FieldNoError;
	cw_firstNameError 	= cw_FieldNoError;
	cw_lastNameError 	= cw_FieldNoError;
	cw_dobError		= cw_FieldNoError;
	cw_genderError		= cw_FieldNoError;
	cw_address1Error 	= cw_FieldNoError;
	cw_cityError 		= cw_FieldNoError;
	cw_stateError 		= cw_FieldNoError;
	cw_ZIPError		= cw_FieldNoError;
	cw_sampleError		= cw_FieldNoError;
}
// We want the error codes set to NoError when the file loads.
cw_InitErrors();

function cw_Checkform(form) {

	cw_InitErrors();

	
	var firstName = document.getElementById("form1:firstName");
	var lastName = document.getElementById("form1:lastName");
	var email = document.getElementById("form1:email");
	var confirmEmail = document.getElementById("form1:confirmEmail");
	var dobDay = document.getElementById("form1:dobDay");
	var dobMonth = document.getElementById("form1:dobMonth");
	var dobYear = document.getElementById("form1:dobYear");
	var address1 = document.getElementById("form1:address1");
	var address2 = document.getElementById("form1:address2");
	var city = document.getElementById("form1:city");
	var state = document.getElementById("form1:state");
	var zip = document.getElementById("form1:zip");
	var gender = document.getElementsByName("form1:gender");
	

	// -------------------------------------
	// Set an error code for the email address fields
	// -------------------------------------
	cw_emailFormatError = cw_ValidateEmail(email.value);
	if (confirmEmail.value != email.value)
		cw_confirmEmailError = cw_FieldConfirmEmailError;
	if (cw_IsEmpty(confirmEmail.value) == true)
		cw_confirmEmailError = cw_FieldEmptyError;

	// -------------------------------------
	// Set an error code for the name fields
	// -------------------------------------
	cw_firstNameError = cw_ValidateAlpha(firstName.value);
	cw_lastNameError = cw_ValidateAlpha(lastName.value);

	// -------------------------------------
	// Set an error code for the Date of Birth
	// -------------------------------------
	if (dobDay.options[0].selected ||
            dobMonth.options[0].selected ||
            dobYear.options[0].selected)
		cw_dobError = cw_OptionNotSelectedError;

	// -------------------------------------
	// Set an error code for Gender
	// -------------------------------------	
	
	var	maleOptionCheckedIE = true;
	if (gender[2] != null) {
		maleOptionCheckedIE = !gender[2].checked;
	}		
	if (!gender[0].checked && !gender[1].checked && maleOptionCheckedIE) {
		cw_genderError = cw_OptionNotSelectedError;
	}


	// -------------------------------------
	// Set an error code for the address, city, state, & ZIP fields
	// -------------------------------------
	if (cw_IsEmpty(address1.value) == true) 
		cw_address1Error = cw_FieldEmptyError;
	if (cw_IsEmpty(city.value) == true) 
		cw_cityError = cw_FieldEmptyError;
	if (state.options[0].selected)
		cw_stateError = cw_OptionNotSelectedError;
	cw_ZIPError = cw_ValidateZIP(zip.value);

	// -------------------------------------
	// Set an error code for Sample
	// -------------------------------------
	
	var	sampleOptionCheckedIE = true;
	if (form.sample[4] != null) {
		sampleOptionCheckedIE = !form.sample[4].checked;
	}	
	
	if (!form.sample[0].checked && 
	    !form.sample[1].checked && 
	    !form.sample[2].checked && 
	    !form.sample[3].checked && sampleOptionCheckedIE)
		cw_sampleError = cw_OptionNotSelectedError;


	if ( 
		cw_emailFormatError	!= cw_FieldNoError ||
		cw_confirmEmailError	!= cw_FieldNoError ||
		cw_firstNameError 	!= cw_FieldNoError ||
		cw_lastNameError 	!= cw_FieldNoError ||
		cw_dobError 		!= cw_FieldNoError ||
		cw_genderError 		!= cw_FieldNoError ||
		cw_address1Error 	!= cw_FieldNoError ||
		cw_cityError 		!= cw_FieldNoError ||
		cw_stateError 		!= cw_FieldNoError ||
		cw_ZIPError 		!= cw_FieldNoError ||
		cw_sampleError 		!= cw_FieldNoError ) {
		
		cw_HandleErrorAlert();
		cw_HandleErrorLabels(form);
		return false;
	}
	else {
		// We made it. All is well.
		var samples = document.getElementsByName("sample");
		var selectedSample;
		for (i = 0; i < samples.length; i++) {
			if (samples[i].checked) {
				selectedSample = samples[i].value;
			}
		}
		
		document.getElementById("form1:hidden_item_code").value = selectedSample;
	}
	return true;
}


function cw_HandleErrorAlert() {
	var errorstring = "";
	errorstring += "We did not receive all the required information. Please check your entries below and resubmit.\n";

	// Check for empty text inputs
	if ( cw_emailFormatError 	== cw_FieldEmptyError)
		errorstring += "Please enter your Email.\n";
	if ( cw_confirmEmailError 	== cw_FieldEmptyError)
		errorstring += "Please enter your Confirm Email.\n";
	if ( cw_firstNameError 		== cw_FieldEmptyError) 
		errorstring += "Please enter your First name.\n";
	if ( cw_lastNameError 		== cw_FieldEmptyError)
		errorstring += "Please enter your Last name.\n";
	if ( cw_dobError	 	== cw_OptionNotSelectedError)
		errorstring += "Please enter your Date of Birth.\n";
	if ( cw_genderError	 	== cw_OptionNotSelectedError)
		errorstring += "Please select your Gender.\n";
	if ( cw_address1Error 		== cw_FieldEmptyError)
		errorstring += "Please enter your Address.\n";
	if ( cw_cityError 		== cw_FieldEmptyError)
		errorstring += "Please enter your City.\n";
	if ( cw_stateError 		== cw_OptionNotSelectedError)
		errorstring += "Please select your State.\n";
	if ( cw_ZIPError 		== cw_FieldEmptyError) 
		errorstring += "Please enter your Zip.\n";
	if ( cw_sampleError	 	== cw_OptionNotSelectedError)
		errorstring += "Please Pick Your Flavor.\n";

	// check for invalid input
	if (cw_ZIPError == cw_FieldLenError) 
		errorstring += "The ZIP Code must be at least 5 numbers.\n\n";
	if (cw_emailFormatError == cw_FieldEmailFormatError)
		errorstring += "Please enter a valid E-mail.\n\n";
	if (cw_confirmEmailError == cw_FieldConfirmEmailError)
		errorstring += "Please re-enter your Email or Confirm Email.\n They do not match.\n\n";

	if ( cw_firstNameError 	== cw_FieldAlphaError) 
		errorstring += "First Name contains invalid characters.\n";
	if ( cw_lastNameError 	== cw_FieldAlphaError) 
		errorstring += "Last Name contains invalid characters.\n";
	
	alert(errorstring);
}
function cw_HandleErrorLabels(form) {
    if (document.getElementById) {
	if (document.getElementById("requiredLabel")) {
		document.getElementById("requiredLabel").style.color 		= "#f00";
		document.getElementById("requiredLabel").style.fontStyle 	= "italic";
	}
	// reset the color in case this is their second error.
	// Check for empty text inputs
	if ( cw_emailFormatError 	!= cw_FieldNoError) {
		document.getElementById("emailLabel").style.color 		= "#f00";
		document.getElementById("emailLabel").style.fontStyle 		= "italic";
	}
	else {
		document.getElementById("emailLabel").style.color 		= "#FFFFFF";
		document.getElementById("emailLabel").style.fontStyle 		= "normal";
	}
	if ( cw_confirmEmailError 	== cw_FieldEmptyError || cw_confirmEmailError == cw_FieldConfirmEmailError) {
		document.getElementById("confirmEmailLabel").style.color 	= "#f00";
		document.getElementById("confirmEmailLabel").style.fontStyle 	= "italic";
	}
	else {
		document.getElementById("confirmEmailLabel").style.color 	= "#FFFFFF";
		document.getElementById("confirmEmailLabel").style.fontStyle 	= "normal";
	}
	if ( cw_firstNameError 		!= cw_FieldNoError) {
		document.getElementById("firstNameLabel").style.color 	= "#f00";
		document.getElementById("firstNameLabel").style.fontStyle 	= "italic";
	}
	else {
		document.getElementById("firstNameLabel").style.color 		= "#FFFFFF";
		document.getElementById("firstNameLabel").style.fontStyle 	= "normal";
	}
	if ( cw_lastNameError 		!= cw_FieldNoError) {
		document.getElementById("lastNameLabel").style.color 		= "#f00";
		document.getElementById("lastNameLabel").style.fontStyle	= "italic";
	}
	else {
		document.getElementById("lastNameLabel").style.color 		= "#FFFFFF";
		document.getElementById("lastNameLabel").style.fontStyle 	= "normal";
	}
	if ( cw_address1Error 		!= cw_FieldNoError) {
		document.getElementById("address1Label").style.color 		= "#f00";
		document.getElementById("address1Label").style.fontStyle 	= "italic";
	}
	else {
		document.getElementById("address1Label").style.color 		= "#FFFFFF";
		document.getElementById("address1Label").style.fontStyle 	= "normal";
	}
	if ( cw_cityError 		!= cw_FieldNoError) {
		document.getElementById("cityLabel").style.color 		= "#f00";
		document.getElementById("cityLabel").style.fontStyle 		= "italic";
	}
	else {
		document.getElementById("cityLabel").style.color 		= "#FFFFFF";
		document.getElementById("cityLabel").style.fontStyle 		= "normal";
	}
	if ( cw_ZIPError 		!= cw_FieldNoError)  {
		document.getElementById("ZIPLabel").style.color 		= "#f00";
		document.getElementById("ZIPLabel").style.fontStyle 		= "italic";
	}
	else {
		document.getElementById("ZIPLabel").style.color 		= "#FFFFFF";
		document.getElementById("ZIPLabel").style.fontStyle 		= "normal";
	}

	// Check for unselected options
	if ( cw_dobError	 	== cw_OptionNotSelectedError) {
		document.getElementById("dobLabel").style.color 		= "#f00";
		document.getElementById("dobLabel").style.fontStyle 		= "italic";
	}
	else {
		document.getElementById("dobLabel").style.color 		= "#FFFFFF";
		document.getElementById("dobLabel").style.fontStyle 		= "normal";
	}
	if ( cw_genderError	 	== cw_OptionNotSelectedError) {
		document.getElementById("genderLabel").style.color 		= "#f00";
		document.getElementById("genderLabel").style.fontStyle 		= "italic";
	}
	else {
		document.getElementById("genderLabel").style.color 		= "#FFFFFF";
		document.getElementById("genderLabel").style.fontStyle 		= "normal";
	}

	if ( cw_stateError 		== cw_OptionNotSelectedError) {
		document.getElementById("stateLabel").style.color 		= "#f00";
		document.getElementById("stateLabel").style.fontStyle 		= "italic";
	}
	else {
		document.getElementById("stateLabel").style.color 		= "#FFFFFF";
		document.getElementById("stateLabel").style.fontStyle 		= "normal";
	}
	if ( cw_sampleError 		== cw_OptionNotSelectedError) {
		document.getElementById("sampleLabel").style.color 		= "#f00";
		document.getElementById("sampleLabel").style.fontStyle 		= "italic";
	}
	else {
		document.getElementById("sampleLabel").style.color 		= "#FFFFFF";
		document.getElementById("sampleLabel").style.fontStyle 		= "normal";
	}
    }
}

function cw_IsEmpty(str) {
	// Doesn't catch white space
	if (str == null || str == "") {
		return true;
	}
	return false;
}
function cw_IsPosInteger(val) {
	str = val.toString();
	for (var i = 0; i < str.length; i++) {
		var thisChar = str.charAt(i);
		if (thisChar < "0" || thisChar > "9") {
			return false;
		}
	}
	return true;
}
function cw_IsAlpha(str) {
	// This allows 'a-z' 'A-Z' '.' '-' and backspace
	var isalpha = false;
	//if (charCode == 8) {
	//	return true;
	//}
	for (var i = 0; i < str.length; i++) {
		var thisChar = str.charAt(i);
		if ((thisChar >= "a" && thisChar <= "z") || 
		    (thisChar >= "A" && thisChar <= "Z") || 
		     thisChar == "." || thisChar == '-' || thisChar == " " || thisChar == "'") {
			 isalpha = true;
		}
		else {
			return false;
		}
	}
	return true;
}
function cw_ValidatePosIntLen(str,len) {
	if (cw_IsEmpty(str) == true) {
		return cw_FieldEmptyError;
	}
	else if (str.length != len) {
		return cw_FieldLenError;
	}
	else if (cw_IsPosInteger(str) == true) {
		// It's not empty, it's the right lenght, and it contains only numbers.
		return cw_FieldNoError;
	}
	else return cw_FieldIntError;
}

function cw_ValidateAlpha(str,len) {
	if (cw_IsEmpty(str) == true) {
		return cw_FieldEmptyError;
	}
	else if (cw_IsAlpha(str) == true) {
		// It's not empty, and it only contains what we are considering letters.
		return cw_FieldNoError;
	}
	else return cw_FieldAlphaError;
}
function cw_ValidateEmail(str) {
	// Must a have one and only one '@' not as the first or last character.
	// The '@' must be followed by a '.'.
	// Must have at least one '.' not the first or last character.
	// Cannot have a ' '.

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (cw_IsEmpty(str) == true) {
		return cw_FieldEmptyError;
	}
	if (str.indexOf(at)==-1) {
		return cw_FieldEmailFormatError;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
		return cw_FieldEmailFormatError;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
		return cw_FieldEmailFormatError;
	}
	if (str.indexOf(at,(lat+1))!=-1) {
		return cw_FieldEmailFormatError;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		return cw_FieldEmailFormatError;
	}
	if (str.indexOf(dot,(lat+2))==-1) {
		return cw_FieldEmailFormatError;
	}
	if (str.indexOf(" ")!=-1) {
		return cw_FieldEmailFormatError;
	}
	return cw_FieldNoError;					
}
function cw_ValidateZIP(str) {
	if (cw_IsEmpty(str) == true) {
		return cw_FieldEmptyError;
	}
	if (str.length == 5) {
		return cw_ValidatePosIntLen(str,5);
	}
	return cw_FieldLenError;
}
function cw_CheckEnter(e) {
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	status = charCode;
	if (charCode == 13) {
		return false;
	}
	return true;
}
function cw_CheckEnterInt(e) {
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	status = charCode;
	if (charCode == 13) {
		return false;
	}
	if (charCode == 8 || charCode == 9) {
		return true;
	}
	if (charCode == 0) { // this is what gets sent for a tab
		return true;
	}
	if (charCode < 48 || charCode > 57) {
		return false;
	}
	return true;
}
function KeyPress(what,e,max,action) {
    if (document.layers) {
        if (e.target.value.length >= max)
            eval(action);
    }
    else if (document.all) {
        if (what.value.length > (max-1))
            eval(action);
    }
}


function checkFlavors() {
	if (document.getElementById) {
		var berry = document.getElementById("berry").style.display == 'none';
		var orange = document.getElementById("orange").style.display == 'none';
		var honey = document.getElementById("honey").style.display == 'none';
		var citrus = document.getElementById("citrus").style.display == 'none';
		
		var berryInput = document.getElementById("berryInput");
		var orangeInput = document.getElementById("orangeInput");
		var honeyInput = document.getElementById("honeyInput");
		var citrusInput = document.getElementById("citrusInput");
		
		var samplelabel = document.getElementById("sampleLabel");
		samplelabel.style.display = "none";
		if (berry && orange && honey && !citrus) {
			citrusInput.checked = true;
		} else if (berry && orange && !honey && citrus) {
			honeyInput.checked = true;
		} else if (berry && !orange && honey && citrus) {
			orangeInput.checked = true;
		} else if (!berry && orange && honey && citrus) {
			berryInput.checked = true;
		} else {
			samplelabel.style.display = "block";
		}
	}
}
