$(document).ready(function(){

	//ColorBox Init
	$("a[rel='blueroom_popup']").colorbox();
	$("a.gallery").colorbox();
	$("a[rel='customcake_popup']").colorbox();
	$("a[rel='popup_comingsoon']").colorbox();
	$("a[rel='cakegallery']").colorbox();
	$("a.contact").colorbox({inline:true, href:"#form_wrapper"});
	
	$("#form").submit(function(){	
	
		// If using an ID other than #email or #error then replace it here
		var name = $("#name");
		var email = $("#email");
		var phone = $("#phone");
		var comment = $("#comment");
		
		// ID's of all required fields and data string for ajax.
		var required = ["name", "email", "phone", "comment"];
		dataString = $("#form").serialize();
		
		// The error notices
		errornotice = $("#error");
		emptyerror = "Please fill out this field.";
		emailerror = "Please enter a valid e-mail.";
		phoneerror = "Please enter a valid phone number.";
	
		//Validate required fields
		for (i=0;i<required.length;i++) {
			var input = $('#'+required[i]);
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
				errornotice.fadeIn(750);
			} else {
				input.removeClass("needsfilled");
			}
		}
		// Validate the e-mail.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}
		if (!/^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/.test(phone.val())) {
			phone.addClass("needsfilled");
			phone.val(phoneerror);
		}

		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(".input_text").hasClass("needsfilled")) {
			return false;
		} else {
			errornotice.hide();
			//alert (dataString);return false;  
			$.ajax({  
			  type: "POST",
			  url: "includes/ajax_submit.php",
			  data: dataString, 
			  success: function() { 
             	$('#form').hide();
             	$('#success').fadeIn("slow");
			  }, 
			  failure: function() {
			  	alert('Sorry, unexpected error. Please try again later.');
			  } 
			});  
			return false; 
		}
	});
	
	// Clears any fields in the form when the user clicks on them
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled")){
			$(this).val("");
			$(this).removeClass("needsfilled");
	   }
	});
	
});	



