$(function(){
    // find all the input elements with title attributes
    $('input[title!=\"\"]').hint();
    $('textarea[title!=\"\"]').hint();
    
    $("#cf_form .postButton").click(function(event) {
        event.preventDefault();
        
        $("#cf_form").submit();
    });
    
    $("#cf_form .cf_new").click(function(event) {
        $("#cf_form .cf_new").hide('slow');
        $("#cf_form").find( 'input[name="cf_name"]' ).val('').blur();
        $("#cf_form").find( 'input[name="cf_email"]' ).val('').blur();
        $("#cf_form").find( 'textarea[name="cf_body"]' ).val('').blur();
        $("#cf_form .cf_item").show('slow');
        $("#cf_form .postResult" ).empty();
    });
    
    $("#cf_form .cf_new").hide();
    
    //Submit du form
    $("#cf_form").submit(function(event) {
        
        /* stop form from submitting normally */
        event.preventDefault();
        
        if ($("#cf_form").data('running')) {
           // alert('running');
            return;
        }
        $("#cf_form").data('running', true);
        
        /* get some values from elements on the page: */
        var $form = $( this ),
        cf_name = $form.find( 'input[name="cf_name"]' ).val(),
        cf_email = $form.find( 'input[name="cf_email"]' ).val(),
        cf_body = $form.find( 'textarea[name="cf_body"]' ).val(),
        url = '/ajax/lodho/contact_ajax';

        /* Send the data using post and put the results in a div */
        $.post( url, { cf_name: cf_name, cf_email: cf_email, cf_body: cf_body},
          function( data ) {
            //alert('done');
                $("#cf_form").data('running', false);
              $( "#cf_form .postResult" ).empty().append( $( data.msg ) );
              
              if (data.success) {
                $("#cf_form .cf_item").hide('slow');
                $("#cf_form .cf_new").show('slow');
              }
              //alert(data.success);
          }
        );
  });

});
