$(document).ready(function() {

	$("#SubmitForm").click(function() {});
	

	//Submit guestbook entry with ajax
	var options = { 
		target:        '.guestbook-message', 
	    beforeSubmit:  handleSubmit,
	    success:       handleSuccess, 
	    error:         handleError,
	    url:           'web/handlers/guestbookhandler.php'
	}; 
	$('#guestbookform').ajaxForm(options);

});

//Before submit
function handleSubmit(formData, jqForm, options) { 
	
	var form 		= jqForm[0];
	var name 		= form.name.value;
	var message 	= form.message.value; 
	

    if(name == "" || message.length == 0){
    	alert("Vänligen fyll i samtliga fält!"); 
    	return false; 
	}
    	  
    $('.guestbook-message').html('<span class=\"loader\">Skickar ...</span>');

    return true;
}



//After submit after success
function handleSuccess(responseText, statusText)  { 	
	
	var css = "";
	if(statusText == "success"){
		css = "success";
	}else{
		css = "error";
	}
	
	$('.guestbook-message').addClass(css);
	$('.guestbook-message').html("<span class=\"success\">Tack för ditt inlägg!</span>");
	$('#guestbookform').slideUp('slow');
	$('#guestbook-listing');

	$.get("web/handlers/entryHandler.php", function(data){
		$('.guestbook').replaceWith(data);
	});

	$('#guestbook-listing').animate({
		height: '+=180',
		duration: 100
	  }, 1000, function() {
		// Animation complete.
	  });


}
//Handle error
function handleError(xhr, statusText)  { 
	
	$('.guestbook-message').addClass('error');
	$('#guestbookform').slideDown('slow');
	$('.guestbook-message').html(xhr.responseText);
}
