/**/

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").replaceWith("<span class='wait'>Loading...</span>");
			return true;
		}
	});
	
	$(".check-form :input").bind("change keyup focus click",function(){
		checkError(this);
	});
	
	// coverage
	$("#coverage li").hover(function(){
		mapOver( $("#boroughs area").eq( $("#coverage li").index(this) ) );
		$(this).addClass("hover");
	},function(){
		var canvas = document.getElementById("canvas");
		ctx.clearRect(0,0,canvas.width,canvas.height);
		$(this).removeClass("hover");
	});
	
	$("#boroughs area").hover(function(){
		$("#coverage li").eq( $("#boroughs area").index(this) ).addClass("hover");
		mapOver(this);
	},function(){
		$("#coverage li").removeClass("hover");
		var canvas = document.getElementById("canvas");
		ctx.clearRect(0,0,canvas.width,canvas.height);
	});
	
	// events
	$("#events li:even").addClass("alt");
	
	// fleet
	$("#fleet li").each(function(i){
		if(i % 3 ==0) $(this).before("<li class='clr'></li>");
	});
	$("#fleet li").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});
	
});

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;
}

/**/

// imagemap highlighter

var ctx;

function mapOver(e){
	var v = ($(e).attr("coords")).split(",");
	var canvas = document.getElementById("canvas");
	ctx = canvas.getContext("2d");
	var pl = 0;
	var pt = 0;
	ctx.fillStyle = "rgba(135, 206, 235, 0.7)";
	ctx.fillStyle = "rgba(255, 255, 255, 0.2)";
	//ctx.fillStyle = "rgba(255, 0, 0, 0.5)";
	ctx.strokeStyle = "rgba(29, 55, 95,0)";
	ctx.lineWidth = 1;

	ctx.clearRect(0,0,canvas.width,canvas.height);

	ctx.beginPath();
	ctx.moveTo(pl + parseInt(v[0],10), pt + parseInt(v[1],10));
	for(i=2;i<v.length;i+=2)
	{
		ctx.lineTo(pl + parseInt(v[i],10), pt + parseInt(v[i+1],10));
	}
	ctx.fill();
	ctx.stroke();
	ctx.closePath();
}


