/**
 * @author alex
 */

 
$(function(){ 
	$('button#submitButton').bind('click', function(){
		var location = $('#location').val();
		var comments = $('#comments').val();
		var email = $('#email').val();
		var ip = $('#ip').val();
		var name = $('#name').val();
		if(trim(name) == "" || trim(location) == "" || trim(comments) == "" || validateEmail(email) == false)
		{
			alert("You must supply your name, location, email and comments to submit this form.");	
		}
		else
		{
			$.post("/ajax/comment_submit", { GUESTBOOKS_location: location,  GUESTBOOKS_message: comments, GUESTBOOKS_user_ip: ip, GUESTBOOKS_name: name, GUESTBOOKS_email: email },
			  function(data){
				    if(data == "success")
					{
						alert("Thank you for your comment.");
					}
					else
					{
						alert("Unfortunately there was an issue with the form and we did not receive your comment, please try again later.");
					}
			  });
		}
	});
	 
});

function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function validateEmail(email)
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false)
   {
		return false;
   }
	return true;
}
