jQuery(document).ready(function () {
	jQuery("#menu li a").hover(
		function () {
			jQuery(this).stop().animate({opacity:0}, 600);
		},
		function () {
			jQuery(this).stop().animate({opacity:1}, 600);
		}
	);

	jQuery("#contact_form form").submit(function (e) {
		e.preventDefault();
		jQuery("#contact_form .error").remove();
		jQuery("#contact_form .status").empty().hide();
		if (Findleys.validate()) {
			jQuery("#contact_form form").slideUp("slow", function () {
				//jQuery("#contact_form .status").html("Sending...").show();
				jQuery.ajax({
					url: jQuery('#contact_form form').attr('action'),
					data: jQuery('#contact_form form').serialize(),
					type: 'post',
					cache: false,
					dataType: 'html',
					success: function (data) {
						jQuery("#contact_form .status").html(data).show();
					},
					error: function (xhr) {
						jQuery("#contact_form .status").html(xhr.statusText).show();
					}
				});
			});
		}
	});
});

var Findleys = {
	validate: function () {
		var valid = true;

		var name = jQuery('#contact_form #name');
		if (!name.val()) {
			name.prev().before("<div class='error'>Name is required!</div>");
			valid = false;
		}

		var email = jQuery('#contact_form #email');
		if (!email.val()) {
			email.prev().before("<div class='error'>Email is required!</div>");
			valid = false;
		}
		else {
			if (!this.validateEmail(email.val())) {
				email.prev().before("<div class='error'>Email is invalid!</div>");
				valid = false;
			}
		}

		var phone = jQuery('#contact_form #phone');
                if (!phone.val()) {
                        phone.prev().before("<div class='error'>Phone is required!</div>");
                        valid = false;
                }

		var comments = jQuery('#contact_form #comments');
		if (!comments.val()) {
			comments.prev().before("<div class='error'>Comments or Questions is required!</div>");
			valid = false;
		}
		return valid;
	},
	validateEmail: function (email) {
		var at = email.lastIndexOf("@");

		// Make sure the at (@) sybmol exists and  
		// it is not the first or last character
		if (at < 1 || (at + 1) === email.length) {
			return false;
		}

		// Make sure there aren't multiple periods together
		if (/(\.{2,})/.test(email)) {
			return false;
		}

		// Break up the local and domain portions
		var local = email.substring(0, at);
		var domain = email.substring(at + 1);

		// Check lengths
		if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255) {
			return false;
		}

		// Make sure local and domain don't start with or end with a period
		if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain)) {
			return false;
		}

		// Check for quoted-string addresses
		// Since almost anything is allowed in a quoted-string address,
		// we're just going to let them go through
		if (!/^"(.+)"$/.test(local)) {
			// It's a dot-string address...check for valid characters
			if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local)) {
				return false;
			}
		}

		// Make sure domain contains only valid characters and at least one period
		if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1) {
			return false;	
		}

		return true;
	}
}
