// AJAX Interface
// @author: Gustavo Solt
// @date: 05/2006

// Ajax Initialization
var ajax_initialized = false;

// Creates a new Ajax request
// @param string page         - Page-Name
// @param string parameters   - Parameters to give to the page
// @param string http_server  - HTTP_SERVER, complete url
// @param string values       - Values to parse
function ajax(page, parameters, http_server, values) {
    // Show the proccecing icon
    showAjaxStatus('ajaxinfo');

    // All the parameters
    all_parameters = '';
    all_parameters = all_parameters + parameters[0] + '/';
    all_parameters = all_parameters + parameters[1] + '/';
    all_parameters = all_parameters + parameters[2] + '/';

    // All the values
    all_parameters = all_parameters + values;

    // Create the ajax request
    new Ajax.Request(http_server+'ajax/'+page+'/'+all_parameters, { onSuccess: handle_ajax});
}

// Submits a form using ajax 
// @param string page         - Name of the page that should answer the ajax request
// @param string parameter    - Parameter that should submitted to the ajax as a get parameter
// @param string action       - Action of the module
// @param string http_server  - HTTP_SERVER, complete url
// @param ...                 - (as much input fields as you want) 
function ajax_post(page, module, action, http_server) {
    // Show the proccecing icon
    showAjaxStatus('ajaxinfo');

    // Collect all values following to form     
    var data   = ''; 
    var fname  = '';
    var finput = ''; 
    var fvalue = '';
    for (i=4; i<arguments.length; i++ ) {
        if (data != '') {
            data = data + '&'; 
        }
        fname  = arguments[i];
        finput = $(fname);
        fvalue = $F(finput);
        data = data + fname + '=' + encodeURIComponent(fvalue); 
    }
    var opt = { method:     'post', 
                postBody:   data, 
                onSuccess:  handle_ajax };

    // Create the ajax request
    new Ajax.Request(http_server+'ajax/'+page+'/'+module+'/'+action+'/', opt);
}

// Handles a ajax response, and updates every object found.
// @param t - HTTPResponse
function handle_ajax(t) {
    // Hide the proccecing icon
    Element.hide('ajaxinfo');

    // Update all divs
    var response   = t.responseText;
    var divupdates = new Array();
    var divupdate  = new Array();
    divupdates     = response.split('__#__');
    for (var i = 1; i < divupdates.length; ++i) {
        divupdate = divupdates[i].split('__|__');

        // Open a new windows with the link
        if (divupdate[0] == 'new_link') {
            new_win(divupdate[1]);
        // Link 
        } else if (divupdate[0] == 'href_link') {
            location.href = divupdate[1];
        // Normal update
        } else {
            $(divupdate[0]).innerHTML = divupdate[1];
        }
    }
}

// Creates a new Ajax request
// @param string page         - Page-Name
// @param string parameters   - Parameters to give to the page
// @param string http_server  - HTTP_SERVER, complete url
// @param string values       - Values to parse
function ajax_web(index, page, parameters, http_server, field, values) {
    // Show the proccecing icon
    showAjaxStatus('ajaxinfo');

    // All the parameters
    all_parameters = '';
    if ((parameters[0] != null)&&(parameters[1] != null)) {
        all_parameters = all_parameters + '&' + parameters[0] + '=' + parameters[1];
    }
    if ((parameters[2] != null)&&(parameters[3] != null)) {
        all_parameters = all_parameters + '&' + parameters[2] + '=' + parameters[3];
    }
    if ((parameters[4] != null)&&(parameters[5] != null)) {
        all_parameters = all_parameters + '&' + parameters[4] + '=' + parameters[5];
    }
    if ((parameters[6] != null)&&(parameters[7] != null)) {
        all_parameters = all_parameters + '&' + parameters[6] + '=' + parameters[7];
    }
    if ((parameters[8] != null)&&(parameters[9] != null)) {
        all_parameters = all_parameters + '&' + parameters[8] + '=' + parameters[9];
    }
    // All the values
    all_parameters = all_parameters + '&' + field + '=' + values;

    // Create the ajax request
    new Ajax.Request(http_server+index+page+all_parameters, { onSuccess: handle_ajax});
}

// Submits a form using ajax 
// @param string page         - Name of the page that should answer the ajax request
// @param string parameter    - Parameter that should submitted to the ajax as a get parameter
// @param string action       - Action of the module
// @param string http_server  - HTTP_SERVER, complete url
// @param ...                 - (as much input fields as you want) 
function ajax_post_web(index, page, http_server) {
    // Show the proccecing icon
    showAjaxStatus('ajaxinfo');

    // Collect all values following to form     
    var data   = ''; 
    var fname  = '';
    var finput = ''; 
    var fvalue = '';
    for (i=4; i<arguments.length; i++ ) {
        if (data != '') {
            data = data + '&'; 
        }
        fname  = arguments[i];
        finput = $(fname);
        fvalue = $F(finput);
        data = data + fname + '=' + encodeURIComponent(fvalue); 
    }
    var opt = { method:     'post', 
                postBody:   data, 
                onSuccess:  handle_ajax };

    // Create the ajax request
    new Ajax.Request(http_server+index+'/'+page, opt);
}
