/**
 * JavaScript code for andrewcavers.com by Andrew Cavers 2009
 * Uses jquery library
 * http://jquery.com
 * and simplemodal plugin
 * http://www.ericmmartin.com/projects/simplemodal/
 */
 
$(document).ready(function () {
	$('a#contact_link').click(function (e) {
		e.preventDefault();
		$('#modalContent').modal({onOpen: modalOpen, onClose: modalClose, close: false});
		$("#contactname").val("Name");
		$("#contactemail").val("Email");
		$("#contactmessage").val("Your message");
	});
	
	$('#messagesubmit').click(function () {
		checkMessage();
		return false;
	});
	
	$("#contactmessage").focus(function(){
		$(this).css({fontStyle: "normal"});
		if (($(this).val() == "Your message") || ($(this).val() == "Please enter a message")){
			$(this).val("");
		}
		$(this).css({fontStyle: "normal"});
		$(this).css({textAlign: "left"});
		$(this).css({color: "#000"});
		$(this).css({borderColor: "#336699"});
	});
	$("#contactmessage").blur(function(){
		if ($(this).val() == "") {
			$(this).css({textAlign: "center"});
			$(this).css({fontStyle: "italic"});
			$(this).css({color: "#ccc"});
			$(this).val("Your message");
		}
		$(this).css({borderColor: "#444"});
	});
	
	$("#contactname").focus(function(){
		if (($(this).val() == "Name") || ($(this).val() == "Please enter your name")){
			$(this).val("");
		}
		$(this).css({fontStyle: "normal"});
		$(this).css({textAlign: "left"});
		$(this).css({color: "#000"});
		$(this).css({borderColor: "#336699"});
	});
	$("#contactname").blur(function(){
		if ($(this).val() == "") {
			$(this).css({textAlign: "center"});
			$(this).css({fontStyle: "italic"});
			$(this).css({color: "#ccc"});
			$(this).val("Name");
		}
		$(this).css({borderColor: "#444"});
	});
	
	$("#contactemail").focus(function(){
		if (($(this).val() == "Email") || ($(this).val() == "Please enter your email address")){
			$(this).val("");
		}
		$(this).css({fontStyle: "normal"});
		$(this).css({textAlign: "left"});
		$(this).css({color: "#000"});
		$(this).css({borderColor: "#336699"});
	});
	$("#contactemail").blur(function(){
		if ($(this).val() == "") {
			$(this).css({textAlign: "center"});
			$(this).css({fontStyle: "italic"});
			$(this).css({color: "#ccc"});
			$(this).val("Email");
		}
		$(this).css({borderColor: "#444"});
	});
});

function checkMessage(){
	var ok = "yes";
	var name = $("#contactname").val();
	var email = $("#contactemail").val();
	var message = $("#contactmessage").val();
	var emailFilter=/^.+@.+\..{2,3}$/;
	var illegalEmailChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	var removeSpaces = /\s+/g;
	name = name.replace(removeSpaces, "_");
	//check name length
	if ((name == "Please enter your name") || (name == "Name")) {
		$("#contactname").css({borderColor: "#f66"});
		$("#contactname").css({textAlign: "center"});
		$("#contactname").css({fontStyle: "italic"});
		$("#contactname").css({color: "#ccc"});
		$("#contactname").val("Please enter your name");
		ok = "no";	
	}
	else if ((name.length < 2) || (name.length > 255)) {
		$("#contactname").css({backgroundColor: "#f66"});
		ok = "no";	
	}
	else {
		$("#contactname").css({backgroundColor: "#fff"});
	}
	if ((email == "Email") || (email == "Please enter your email address") || (!(emailFilter.test(email))) || (email.match(illegalEmailChars))) {
		$("#contactemail").css({borderColor: "#f66"});
		$("#contactemail").css({textAlign: "center"});
		$("#contactemail").css({fontStyle: "italic"});
		$("#contactemail").css({color: "#ccc"});
		$("#contactemail").val("Please enter your email address");
		ok = "no";	
	}
	else {
		$("#contactemail").css({backgroundColor: "#fff"});
	}
	if ((message == "Your message") || (message == "Please enter a message")) {
		$("#contactmessage").css({borderColor: "#f66"});
		$("#contactmessage").css({textAlign: "center"});
		$("#contactmessage").css({fontStyle: "italic"});
		$("#contactmessage").css({color: "#ccc"});
		$("#contactmessage").val("Please enter a message");
		ok = "no";	
	}
	else {
		$("#contactmessage").css({backgroundColor: "#fff"});
	}
	if (ok == "yes"){
		saveComment(name, email, message);
		return false;
	}
	else {
		return false;
	}
}


function saveComment(name, email, message) {
	// first hide contact form
	$("#contactformholder").fadeOut('slow', function () {
		$("#contactformholder").html("<h3>Sending...</h3>");
		$("#contactformholder").fadeIn('slow', function(){
			$.post("contact.php", { name: name, email: email, message: message }, function(data){
				$("#contactformholder").fadeOut('slow', function(){
					if (data == "<p>Fail</p>"){
						$("#contactformholder").html(data);
						$("#contactformholder").fadeIn('slow');
					}
					else {
						$("#contactformholder").html(data);
						$("#contactformholder").fadeIn('slow');
					}
				});
			});
		});
	});
}


function modalOpen (dialog) {
	dialog.overlay.fadeIn('slow');
	dialog.container.fadeIn('slow');
	dialog.data.fadeIn('slow');
}

function modalClose (dialog) {
	dialog.data.fadeOut('slow');
	dialog.container.fadeOut('slow');
	dialog.overlay.fadeOut('slow', function () {
		$.modal.close();
	});
}
