window.onload = function(){
    $("#menu li a").hover(function(){
        $(this).css("background", "url('images/menu-item-bg.gif') repeat-x");
    }, function(){
        $(this).css("background", "");
    });
}

function contact() {

	var form = $("form");
	var nameIn = $("[name='name']", form);
	nameIn.focus();
	
	form.submit(function() {
		//alert("heylo");
		
		var message = "";
		
		var nameIn = $("[name='name']", this);
		if (nameIn.val() == "") {
			nameIn.css("border", "4px solid red");
			message+="Please give your name.\n";
			nameIn.focus();
		} else {
			nameIn.css("border", "");
		}
		
		var emailIn = $("[name='email']", this);
		var contactNumberIn = $("[name='contactNumber']", this);
		if (emailIn.val() == "" && contactNumberIn.val() == "") {
			emailIn.css("border", "4px solid red");
			emailIn.focus();
			message+="Please give your email or number so we can get back to you.\n";
		} else {
			emailIn.css("border", "");
		}
		
		if (emailIn.val() != "" && emailIn.val().match(/^[^ ]+@[^ ]+\.[^ ]+$/gi) == null) {
			emailIn.css("border", "4px solid red");
			emailIn.focus();
			message+="Your email address doesn't look right, please correct.\n";
		}
		
		var captchaIn = $("[name='captcha']", this);
		if (message == "" && captchaIn.val() == "") {
			captchaIn.css("border", "4px solid red");
			captchaIn.focus();
			message+="Please enter the letters in the picture. If you can't read it click 'Try another'.";
		} else {
			captchaIn.css("border", "");
		}
		
		if (message == "") {
			var form = this;
			var captchaImage = $("#captcha");
			$.ajax({
				type: "post",
				url: "contact",
				data: $(form).serialize(),
				success: function() {
					// Reset form
					$(':input', form).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
					captchaImage.each(function() {this.src = this.src+"?123";})
					alert("Thanks for your message. You'll be hearing from us very soon.");
					nameIn.focus();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					if (XMLHttpRequest.status == 401) {
						captchaImage.each(function() {this.src = this.src+"?123";})
						captchaIn.css("border", "4px solid red");
						captchaIn.val("");
						captchaIn.focus();
						alert("Sorry the letters you entered did not match those in the picture. Please try again with the new image.")
					} else {
						alert("Umm.. sorry we had trouble sending your email. Please copy your message and send it to info@ukpowerboatcharter.com");
					}
				}
			});
		} else {
			alert(message);
		}
		
		return false;
	})
}
