// JavaScript Document
function switchSubscribeSubmit(validBOOL,submit_element) {
	
	if(validBOOL == true){
		if(submit_element.value == 'Complete Purchase'){ 
			//document.getElementById('submitButton').style.display = 'none';			
			submit_element.value = 'Please Wait.';			
			
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function updateError(formElement,errorMsg) {
	//var errDisplay = document.getElementById('error_' + formElement.name);
	
	var container_p = document.createElement('p');
	container_p.className = "error";
	
	container_p_text = document.createTextNode(errorMsg);	
	container_p.appendChild(container_p_text);	
	
	var errDisplay = document.getElementById('error_' + formElement.id);	
	errDisplay.appendChild(container_p);	
	new Effect.Appear('error_' + formElement.id);
}

function displayErrorHeader(errorDivID) {
	if(!errorDivID)
		errorDivID = 'errorMsgHeader';
	
	errorDiv = document.getElementById(errorDivID);	
	errorMsg = "Please correct the errors highlighted below";
	if(errorDiv) {	
		var container_p = document.createElement('p');
		container_p.className = "error";
		
		container_p_text = document.createTextNode(errorMsg);
		container_p.appendChild(container_p_text);	
		
		errorDiv.appendChild(container_p);
		new Effect.Appear(errorDiv);
	} else {
		alert(errorMsg);
	}
}

function clearPreviousErrors(formOBJ) {
		
	all_divs = document.getElementsByTagName('div');
	for(i = 0; i < all_divs.length; i++) {
		if(all_divs[i].id.search(/errorMsgHeader/) != -1) {
			all_divs[i].display = 'none';
			if(all_divs[i].hasChildNodes())
				all_divs[i].removeChild(all_divs[i].firstChild);	
		}
	}	
		
	for (var i = 0; i < formOBJ.elements.length; i++) {	
		current_input_error = document.getElementById('error_' + formOBJ[i].id);
		
		if(current_input_error && current_input_error.hasChildNodes())
		{			
			//new Effect.Fade(current_input_error.id);
			current_input_error.style.opacity = 0;
			current_input_error.style.display = "none";
			for(j=0;j<current_input_error.childNodes.length;j++) {
				current_input_error.removeChild(current_input_error.childNodes[j]);
			}
		}			
	}
}

function submitContact(x) {
	var validBOOL = true;	
	
	clearPreviousErrors(x);
	var firstField = "";
	
	if(x.ContactName.value=="") {		
		updateError(x.ContactName,'Please enter your Name.');
		if(!firstField) firstField = x.ContactName;
		validBOOL = false;		
	}
	
	if(x.ContactCompany.value=="") {		
		updateError(x.ContactCompany,'Please enter your Company.');
		if(!firstField) firstField = x.ContactCompany;
		validBOOL = false;		
	}
	
	if(!checkEmail(x.ContactEmail)) {		
		updateError(x.ContactEmail,'Please enter a valid email address');
		if(!firstField) firstField = x.ContactEmail;
		validBOOL = false;		
	} 
	
	if(x.ContactPhone.value=="") {		
		updateError(x.ContactPhone,'Please enter your Phone.');
		if(!firstField) firstField = x.ContactPhone;
		validBOOL = false;		
	}
	
	if(x.ContactAddress.value=="") {		
		updateError(x.ContactAddress,'Please enter your Address.');
		if(!firstField) firstField = x.ContactAddress;
		validBOOL = false;		
	}
	
	if(x.ContactCity.value=="") {		
		updateError(x.ContactCity,'Please enter your City.');
		if(!firstField) firstField = x.ContactCity;
		validBOOL = false;		
	}
	
	if(x.ContactState.value=="") {		
		updateError(x.ContactState,'Please enter your State.');
		if(!firstField) firstField = x.ContactState;
		validBOOL = false;		
	}
	
	if(x.ContactZip.value=="") {		
		updateError(x.ContactZip,'Please enter your Zip.');
		if(!firstField) firstField = x.ContactZip;
		validBOOL = false;		
	}		
	
	if (validBOOL == true) {		
		return true;
	} else {
		if(firstField)
			firstField.focus();
			
		displayErrorHeader();		
		return false;
	}
}

function submitReview(x) {
	var validBOOL = true;	
	
	clearPreviousErrors(x);
	var firstField = "";
	
	if(x.CommentName.value=="") {		
		updateError(x.CommentName,'Please enter your Name.');
		if(!firstField) firstField = x.CommentName;
		validBOOL = false;		
	}
	
	if(!checkEmail(x.CommentEmail)) {		
		updateError(x.CommentEmail,'Please enter a valid email address');
		if(!firstField) firstField = x.CommentEmail;
		validBOOL = false;		
	} 		
	
	/*if(x.CommentRating.value=="") {		
		updateError(x.CommentRating,'Please select your Rating.');
		//if(!firstField) firstField = x.CommentRating;
		validBOOL = false;		
	}*/	
	
	if(x.CommentComments.value=="") {		
		updateError(x.CommentComments,'Please enter your Review.');
		if(!firstField) firstField = x.CommentComments;
		validBOOL = false;		
	}
	
	if (validBOOL == true) {		
		return true;
	} else {
		if(firstField)
			firstField.focus();
			
		displayErrorHeader();		
		return false;
	}
}

function submitComment(x) {
	var validBOOL = true;	
	
	clearPreviousErrors(x);
	var firstField = "";
	
	if(x.CommentAuthor.value=="") {		
		updateError(x.CommentAuthor,'Please enter your Name.');
		if(!firstField) firstField = x.CommentAuthor;
		validBOOL = false;		
	}
	
	if(!checkEmail(x.CommentEmail)) {		
		updateError(x.CommentEmail,'Please enter a valid email address');
		if(!firstField) firstField = x.CommentEmail;
		validBOOL = false;		
	} 			
	
	if(x.CommentBody.value=="") {		
		updateError(x.CommentBody,'Please enter your Comments.');
		if(!firstField) firstField = x.CommentBody;
		validBOOL = false;		
	}
	
	if (validBOOL == true) {		
		return true;
	} else {
		if(firstField)
			firstField.focus();
			
		displayErrorHeader();		
		return false;
	}
}

// Newsletter sign up in header
function HeadVal(x){
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(x.heademail.value))){
		alert('Please enter a valid email address to join our email list.');
		x.heademail.focus();
		x.heademail.value = "";
		return false;
	}
}


// Newsletter sign up in body
function BodyVal(x){
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(x.bodyemail.value))){
		alert('Please enter a valid email address to join our email list.');
		x.bodyemail.focus();
		x.bodyemail.value = "";
		return false;
	}
}



// Promo sign up validation
function PromoValidate(x){
	if(x.name.value == ""){
		alert('Please enter your name.');
		x.name.focus();
		return false;
	} else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(x.email.value))){
		alert('Please enter a valid email address.');
		x.email.focus();
		x.email.value = "";
		return false;
	} else if(x.zip.value == ""){
		alert('Please enter your zip code.');
		x.zip.focus();
		return false;
	}
}


// Subscribe validation
function SubscribeValidate(x){
	if(x.name.value == ""){
		alert('Please enter your name.');
		x.name.focus();
		return false;
	} else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(x.email.value))){
		alert('Please enter a valid email address.');
		x.email.focus();
		x.email.value = "";
		return false;
	} else if(x.address.value == ""){
		alert('Please enter your address.');
		x.address.focus();
		return false;			
	} else if(x.city.value == ""){
		alert('Please enter your city.');
		x.city.focus();
		return false;		
	} else if(x.state.value == ""){
		alert('Please enter your state.');
		x.state.focus();
		return false;			
	} else if(x.zip.value == ""){
		alert('Please enter your zip code.');
		x.zip.focus();
		return false;
	} else if(x.B_FirstName.value == ""){
		alert('Please enter your First Name under Billing Information.');
		x.B_FirstName.focus();
		return false;		
	} else if(x.B_LastName.value == ""){
		alert('Please enter your Last Name under Billing Information.');
		x.B_LastName.focus();
		return false;
	} else if(x.B_Address.value == ""){
		alert('Please enter your Address under Billing Information.');
		x.B_Address.focus();
		return false;
	} else if(x.B_City.value == ""){
		alert('Please enter your City under Billing Information.');
		x.B_City.focus();
		return false;
	} else if(x.B_State.value == ""){
		alert('Please enter your State under Billing Information.');
		x.B_State.focus();
		return false;
	} else if(x.B_Zip.value == ""){
		alert('Please enter your Zip under Billing Information.');
		x.B_Zip.focus();
		return false;
	} else if(!CheckCardNumber(x))	{
		return false;		
	}
}

// Same as subscription information for subscriber form
function SameAs() {
	var x = document.Subscription;
	if(document.getElementById('setSameAs').checked){
		// find first name
		for (i=0; i<=x.name.value.length; i++) {
			if(x.name.value.substring(i, i+1) == " ") {
				x.B_FirstName.value = x.name.value.substring(0, i);
				x.B_LastName.value  = x.name.value.substring(i+1, x.name.value.length);
			}
		}
		
		x.B_Address.value = x.address.value;
		x.B_City.value = x.city.value;
		x.B_State.value = x.state.value;
		x.B_Zip.value = x.zip.value;
	}
}


// Member registration validation
function RegisterValidate(x){
	if(x.name.value == ""){
		alert('Please enter your name.');
		x.name.focus();
		return false;
	} else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(x.email.value))){
		alert('Please enter a valid email address.');
		x.email.focus();
		x.email.value = "";
		return false;
	} else if(x.password.value == ""){
		alert('Please enter your password.');
		x.password.focus();
		return false;		
	} else if(x.password.value != x.password2.value){
		alert('Your passwords did not match. Please try again.');
		x.password.value = "";
		x.password2.value = "";
		x.password.focus();
		return false;	
	} else if(x.zip.value == ""){
		alert('Please enter your zip code.');
		x.zip.focus();
		return false;
	}
}

// Member login validation
function LoginValidate(x){
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(x.email.value))){
		alert('Please enter a valid email address.');
		x.email.focus();
		x.email.value = "";
		return false;
	} else if(x.password.value == ""){
		alert('Please enter your password.');
		x.password.focus();
		return false;
	}
}

// Member forgot password validation
function ForgotPasswordValidate(x){
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(x.email.value))){
		alert('Please enter a valid email address.');
		x.email.focus();
		x.email.value = "";
		return false;
	}
}

// joining newsletter validation
function JoinNewsletterValidate(x){
	var checkbox_choices = 0;
	for (i = 0; i < x.newsletter.length; i++)	{
		if (x.newsletter[i].checked) {
			checkbox_choices = checkbox_choices + 1;
		}
	}
	
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(x.email.value))){
		alert('Please enter a valid email address.');
		x.email.focus();
		x.email.value = "";
		return false;
	} else if (!(checkbox_choices)){
		alert('Please choose at least 1 newsletter list.');
		return false;
	}
}