function jqx_processResponse(data) {
    if((data !== undefined) && (data._scripts !== undefined)) {
        for(var i=0;i<data._scripts.length;i++) {
            try {
                $.globalEval(data._scripts[i]);
            } catch(err) {
            }
        }
    }
    $('#ajax_loading_container').hide();
};


function ajax_call(call_url, call_params) {
    call_params.jqx_func = call_url.split('?')[1];
    $.ajax({
            type:'POST',
            url:call_url.split('?')[0],
            data:call_params,
            success:function(data){jqx_processResponse(data);},
            dataType:'json'
          });
};


$.fn.clearForm = function() {
    return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
        return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
    else if (type == 'checkbox' || type == 'radio')
        this.checked = false;
    else if (tag == 'select')
        this.selectedIndex = -1;
  });
};


function prepareFormForAjax(form_id) {
    $(form_id).submit(function() { // action in the form:  ajax/script.php?funcName
        if(!$(form_id +' input[name=jqx_func]').attr('name')) {
            $(form_id).append(' <input type="hidden" name="jqx_func" value="'+
                                    $(form_id).attr('action').split('?')[1]+
                                    '" />'
                                 );
        }
        $('#ajax_loading_container').show();
        $.ajax({
                type:'POST',
                url:$(form_id).attr('action').split('?')[0],
                data:$(form_id).serializeArray(),
                success:function(data){jqx_processResponse(data);},
                dataType:'json'
        });
        return false;
    });
}
