/**/

var error_phone = "At least one phone number required";
var error_email1 = "Valid email required";
var error_email2 = "Emails do not match";
var error_terms = "You must agree to the terms and conditions";
var error_date = "The selected date is in the past";
var error_submit = "Please check if all required fields are filled in";

var ok = true;

$(function(){
	$(".form p.indent:even").addClass("alt");
	$(".altify > :even").addClass("alt");
	
	// quote-form validation
	$(".check-form").submit(function(){
		ok = true;
		$(this).find(".required").each(function(i){
			if(!$(this).val()){
				addError(this);
			} else {
				removeError(this);
			}
		});
		checkEmails();
		checkPhones();
		checkTerms();
		checkDate();
		if(!ok){
			addError($(".check-form button[type=submit]"), error_submit);
			return false;
		} else {
			removeError($(".check-form button[type=submit]"));
			$(".check-form button").hide();
			$(".check-form").append($("<p><em class='wait'>Loading...</em></p>"));
			return true;
		}
	});
	
	$(".check-form :input").bind("change keyup focus click",function(){
		checkError(this);
	});
	
});

function checkError(e){
	if($(e).hasClass("required")){
		if($(e).val()){
			removeError(e);
		} else {
			addError(e);
		}
	}

	if($(e).hasClass("terms")){
		checkTerms();
	}
	if($(e).hasClass("phone")){
		checkPhones();
	}
	if($(e).hasClass("email")){
		checkEmails();
	}
	if($(e).hasClass("date")){
		checkDate();
	}
}

function checkTerms(){
	var terms = $(".check-form").find("#terms");
	if(terms.length){
		if(!terms.is(":checked")){
			addError(terms, error_terms);
		} else {
			removeError(terms);
		}
	}
}

function checkPhones(){
	var phone_home = $(".check-form").find("#phone-home");
	var phone_work = $(".check-form").find("#phone-work");
	var phone_mobile = $(".check-form").find("#phone-mobile");

	if(phone_home.length && phone_work.length && phone_mobile.length){
		if(!phone_home.val() && !phone_work.val() && !phone_mobile.val()){
			addError(phone_home, error_phone);
			addError(phone_work, error_phone);
			addError(phone_mobile, error_phone);
		} else {
			removeError(phone_home);
			removeError(phone_work);
			removeError(phone_mobile);
		}
	}
}

function checkEmails(){
	var email1 = $(".check-form").find("#email1");
	var email2 = $(".check-form").find("#email2");
	if(email1.length && email2.length){
		if(checkmail(email1.val())){
			removeError(email1);
			if(email2.val() != email1.val()){
				addError(email2, error_email2);
			} else {
				removeError(email2);
			}
		} else {
			addError(email1, error_email1);
		}
	}
}

function checkDate(){
	var day = $("#day");
	var month = $("#month");
	var year = $("#year");
	
	if(day.length && month.length && year.length){
		var hiredate = new Date(month.val()+" "+day.val()+", "+year.val());
		var today = new Date();
		if(today - hiredate > 1000*60*60*24){
			addError(year, error_date);
		} else {
			removeError(year);
		}
	}
}

function addError(e,message){
	ok = false;
	//if(!$(e).parent().find(".error-popup").length){
		//removeError(e);
		if(!message) message = "Required";
		$(e).addClass("error");
		if(!$(e).parent().find(".error-popup").length){
			$(e).parent().append("<span class='error-popup'>"+message+"</span>");
			$(e).parent().find(".error-popup").fadeIn("fast");
		}
	//}
}

function removeError(e){
	$(e).removeClass("error");
	$(e).parent().find(".error-popup").fadeOut("fast",function(){ $(this).remove() });
}

function checkmail(str){
	var filter =/^.+@.+\..{2,3}$/;
	testresults=false;
	if(filter.test(str))
		testresults=true;
	return testresults;
}

/**/

