/**
 * Author: Olexandr Tarasiuk
 * Date: 01.04.2009
 * Version: 1.0.0
 */
function AjaxRequest(action, onSuccess, sender) {
    this.id = AjaxRequest.COUNTER++;
    this.sender = sender;
    this.div = document.createElement("div");
    this.div.id = "net_integrio_scripting_ajax_AjaxRequest_div_" + this.id;
    this.div.style.position = "absolute";
    this.div.style.top = "1";
    this.div.style.left = "1";
    this.div.style.width = "1px";
    this.div.style.height = "1px";
    document.body.appendChild(this.div);
    this.div.innerHTML = "<iframe style=\"display:none\" name=\"net_integrio_scripting_ajax_AjaxRequest_iframe_" + this.id + "\"></iframe>";
    this.form = document.createElement("FORM");
    var form = this.form;
    this.form.method = "post";
    this.form.action = action;
    this.form.target = "net_integrio_scripting_ajax_AjaxRequest_iframe_" + this.id;
    this.div.appendChild(this.form);
    var appendInput = function(name, value) {
        var field = document.createElement("INPUT");
        field.type = "hidden";
        field.name = name;
        field.value = value;
        form.appendChild(field);
    };
    appendInput("$net.integrio.scripting.ajax.AjaxRequest$id", this.id);
    this.onSuccess = onSuccess ? onSuccess : function(response) {};
    this.setParameter = function(name, value) {
        appendInput("$net.integrio.scripting.ajax.AjaxRequest$parameters$" + name, value);
    };
    this.submit = function(timeout) {
        timeout = timeout ? timeout : 10000;
        var ret = null;
        this.onSuccess = function(response) {
            ret = response;
        };
        this.submitAsync();
        var d = new Date().getTime();
        while (ret == null) if (d + timeout < new Date().getTime()) break;
        return ret;
    };
    this.submitAsync = function() {
        AjaxRequest.REQUESTS["req_" + this.id] = this;
        this.form.submit();
    };
};
AjaxRequest.COUNTER = 0;
AjaxRequest.REQUESTS = new Array();