
$(document).ready(function() {
    lzFormOptions = {
        beforeSerialize:function(form, options){
            $(':input',form).each(function(index){
               if ($(this).val()==$(this).attr('data-placeholder')){                 
                   $(this).val('');                   
               }
            });
        },
        beforeSubmit : function(arr, form, options){                      
            var that=form.find('.fieldInput input:image')
            that.attr("src", that.attr("data-busy"));
            that.attr('disabled', 'disabled');
        },
        success:       showResponse,
        url:       AJAX_URL,
        type:      'post',
        dataType:  'json'
    };

    $('form.isAjax').ajaxForm(lzFormOptions);

    put_placeholders();
});

 function put_placeholders(){
    $('.fieldInput  input,textarea').each(function() {
        if ($(this).attr("data-placeholder") != undefined) {
            var placeholder = $(this).attr("data-placeholder");
                $(this).DefaultValue(placeholder);
        }
    });
    }

function showResponse(response, status, form, xhr){    
    form.find('.fieldInput input:image').removeAttr('disabled');
    var elems = {
        5: form.attr("data-parent"), // Success, replace the parent with success message.
        4: form.attr("data-replace"), // Error, replace form with errors
        2: form.attr("data-parent"), // Success, replace the parent with success message.
        3: form.attr("data-errors") // Error, display error message to user.
    }
    var successCode = response.action;
    if ($.inArray(successCode, [1,2,3,4,5]) == -1) {

        return false;
    }
    if (successCode == 1) {
        //        alert(response.html);
        window.location = response.html;
    } else {
        var targetString = elems[successCode];
        if (targetString.charAt(0) != '.' && targetString.charAt(0) != '#') {
            targetString = '#' + targetString;
        }

        if (successCode == 4) {
            if (targetString.indexOf("form") == -1) {
                $(targetString).replaceWith(response.html);
                $(targetString).find('form').ajaxForm(lzFormOptions);
            } else {
                $(targetString).html(response.html);
            }

        }
        if (successCode == 3) {            
            $(targetString).html(response.html);
            $(targetString).slideDown();
        }
        if (successCode == 2) {
            $(targetString).html(response.html);
        }
        if (successCode == 5) {
            $(targetString).fadeOut('slow', function(){
                $(targetString).html(response.html);
            });
            $(targetString).fadeIn('slow');
        }
        return true;
    }
}
