/**
 * @author JohnL
 */

function validateEmail(email)
{
	if (email.length > 7) {
		if(email.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$") != null)
		{
			return true;
		}
	}
	return false;
}

jQuery(document).ready(function()
{
	var bwrapEl = document.createElement("div");
   	bwrapEl.id = "questionSubmitResponse";
	jQuery("#questionBox").append(bwrapEl);
	jQuery("#questionSubmitResponse").hide();
	jQuery("form#askQuestion").attr("action","javascript:");
	jQuery("form#askQuestion").submit(function()
	{
		var name = jQuery("form#askQuestion input#name").val();
		var phone = jQuery("form#askQuestion input#phone").val();
		var emailAddress = jQuery("form#askQuestion input#emailAddress").val();
		var question = jQuery("form#askQuestion textarea#question").val();
		if(validateEmail(emailAddress))
		{
			jQuery.post("/askQuestion/",{'name':name,'phone':phone,'emailAddress':emailAddress,'question':question}, 
			function(data)
			{
				jQuery("#questionBox #questionSubmitResponse").html(data);
				jQuery("#questionBox #questionSubmitResponse").show("slow");
				setTimeout(function()
				{
					jQuery("#questionBox #questionSubmitResponse").hide("slow");
				}, 8000);
			});
			jQuery("form#askQuestion")[0].reset();

			return true;
		}
		jQuery("#questionBox #questionSubmitResponse").html("<p><b>Email Not Valid</b></p>");
				jQuery("#questionBox #questionSubmitResponse").show("slow");
				setTimeout(function()
				{
					jQuery("#questionBox #questionSubmitResponse").hide("slow");
				}, 3000);
		
		return false;
	});
});
