var err = false;

$(document).ready(function(){
    
    //open / close the form
    $(".trigger").click(function(){
    	$(".sliding-form").toggle("fast");
		return false;
	});    

    //bind the submit button to the ajax request
    $('#contact-form').ajaxForm( { 
            target:        '#contact-form', 
            beforeSubmit:  presubmit,  // pre-submit callback 
            success:       postsubmit // post-submit callback
     });

 });
 
// pre-submit callback

function presubmit(formData, jqForm, options) {

    err=false
   
    //let the loading gif appear
    $('#loading').css('visibility','visible');
    //check for errors in name or email length
         
    var name = $('input[name=feed_name]').fieldValue(); 
    var email = $('input[name=feed_email]').fieldValue();
    var message = $('textarea[name=feed_message]').fieldValue();
 
    if (!name[0]) { 
        $('#error-name').show('fast');
        err = true; 
    }
    else if ($('#error-name').is(':visible')) {
        $('#error-name').hide('fast');
    }
    
    if( !message[0]  ){

        $('#error-message').show('fast');
        err = true;
    }
    else if ($('#error-message').is(':visible')) {
        $('#error-message').hide();
    }
         
    if( !email[0]){

        $('#error-email-length').show('fast');
        err = true;
    }
    else if ($('#error-email-length').is(':visible')) {
        $('#error-email-length').hide('fast');
    }
    
    if ( ! /^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/.test(email[0]) ){
        $('#error-email-invalid').show('fast');
        err = true;       
    }
    else if ($('#error-email-invalid').is(':visible')) {
        $('#error-email-invalid').hide('fast');
    }
    
    if( err == true ){
        $('#loading').css('visibility','hidden');
        return false;
    }
    else
        return true; 
} 
 
// post-submit callback 
function postsubmit(responseText, statusText, xhr, $form)  { 
    $("#contact-form").hide('slow').after('<h2>Thank you! Your feedback has been sent.</h2>');
    $('#loading').css('visibility','hidden');
} 
