var formSubmit = false;
$(document).ready(function() {

	$('form:not(.filter) :input:visible:first').focus();

	$(".phone").change(function() {
		var phone = $("#phone1").val() + "" + $("#phone2").val() + "" + $("#phone3").val();
		$("#fields_phone").val(phone);
	})

	$("#opt_in_form_index").submit(function() {
		return handleIndexFormSubmit();
	});


	/*  $("#opt_in_form").submit(function() {
	 return handleFormSubmit();
	 });*/

});


function handleIndexFormSubmit() {

	var phone = $("#phone1").val() + "" + $("#phone2").val() + "" + $("#phone3").val();
	$("#fields_phone").val(phone);

	var fn = $("#fields_fname").val();
	var ln = $("#fields_lname").val();
	var address = $("#fields_address1").val();
	var addresspobox = address.toLowerCase();
	var city = $("#fields_city").val();
	var state = $("#fields_state").val();
	var zip = $("#fields_zip").val();
	var phone = $("#fields_phone").val();
	var em = $("#fields_email").val();
	var error = Array();
	var err = "";

	if ((fn.length < 2)) {
		err += "- Please enter your first name \r\n";
	}
	if ((ln.length < 2)) {
		err += "- Please enter your last name \r\n";
	}
	if (address.length < 4) {
		err += "- Please enter a valid address \r\n";
	}
	if (addresspobox.match('po box') || addresspobox.match('p.o. box') || addresspobox.match('pobox')) {
		err += "- We do not ship to P.O. Boxes \r\n";
	}
	if (city.length < 4) {
		err += "- Please enter a valid city \r\n";
	}
	if (state.length != 2) {
		err += "- Please select a valid state \r\n";
	}
	if (zip.length != 5 || isNaN(zip)) {
		err += "- Please enter a valid zip code \r\n";
	}
	if (phone.length != 10 || isNaN(phone)) {
		err += "- Please enter a valid phone \r\n";
	}
	if (!isValidEmail(em)) {
		err += "- Please enter a valid email address \r\n";
	}
//  if($('#agree:checked').val() == null) {
//		err += "- Please Agree to Terms and Conditions \r\n";
//	}

	if (err.length > 1) {
		alert("Please fix the following errors: \r\n" + err);
		formSubmit = true;
		return false;
	} else {
		formSubmit = true;
		return true;
	}

}

function postIndexForm() {
	$.ajax({
		type: "POST",
		url: "request.php",
		data: $("#opt_in_form_index").serialize(),
		async: true,
		success: function(data) {
			alert(data);
		}
	});
}


function isValidEmail(str) {
	//return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
	return emailPattern.test(str);
}

function pageUnload() {
	if (formSubmit != true) {
		//return 'Are you sure you want to leave?';
		return "WAIT!  Are you sure you want to navigate away from this page?\n\nIf you really want to have the best skin of your life then you can't afford to not get your Risk-Free trial order of AuraVie SkinCare - NOW!\n\nThere are only 11 of these Exclusive Trials left to be given away today!\n\nPress OK to continue, or Cancel to stay on the current page.";
		/*
		return "*****************************************************\n\n" + "\n\nWAIT! Are you sure you want to navigate away from this page?" + "\n\nIf you really want to have the best skin of your life, can you really afford to not get your trial of AuraVie SkinCare – NOW?!  " +"\n\nThere are 011 AuraVie SkinCare Trials left to be given away today!" + "\n\nPress OK to continue, or Cancel to stay on the current page." + "\n\nPlease Stay and Get Your RISK FREE TRIAL! \n\n******************************************************";*/
	}
}
window.onbeforeunload = pageUnload;
