
function Message(to_slot, from_slot, data)
{
    this.data = data;
    this.elements = [];
    this.to_slot = to_slot;
    this.from_slot = from_slot;

    this.add_element = function(element)
    {
        this.elements.push(element);
    }
}
document.Message = Message;

function Broker()
{
    this.next_slot_id = 1;
    this.map = new Array();
    this.register_slot = function(f) {
        var slot_id = this.next_slot_id++;
        this.map[slot_id] = f;
        return slot_id;
    };
    this.send = function(message) {
        var slot_id = message.to_slot;
        if(slot_id in this.map)
            this.map[slot_id](message);
    };
}

document.broker = new Broker();

function init_request_system()
{
    var origin = document.getElementById("origin");

    var button = document.createElement("div");
    button.style.position = "absolute";
    button.style.bottom = "0px";
    button.style.left = "0px";
    button.style.backgroundColor = "red";
    button.innerHTML = "TRANSPORT";

    if(document.is_debug_mode)
        origin.appendChild(button);

    var scratch = document.createElement("div");
    scratch.id = "scratch";
    scratch.style.display = "none";
    scratch.style.position = "absolute";
    scratch.style.top = "0px";
    scratch.style.left = "0px";
    scratch.style.width = "800px";
    scratch.style.height = "400px";
    scratch.style.backgroundColor = "white";
    scratch.style.border = "1px solid black";
    scratch.style.overflow = "scroll";

    button.onclick = function(e)
    {
        scratch.style.display = "block";
    };

    origin.appendChild(scratch);
}

var next_scratch_id = 0;

function create_scratch()
{
    var d = document.getElementById("scratch");
    var id = next_scratch_id++;
    var el = document.createElement("div");
    el.id = "scratch_"+id.toString();
    d.appendChild(el);

    var hr = document.createElement("hr");
    d.appendChild(hr);

    return el;
}

var next_form_id = 0;

function start_download(url)
{
    var scratch = create_scratch();
    var iframe = document.createElement("iframe");
    scratch.appendChild(iframe);
    iframe.src = url;
}

function send_request(message)
{
    var scratch = create_scratch();
    var iframe_id = "handler_iframe_" + (next_form_id++).toString();
    var form_id = "handler_form_" + (next_form_id++).toString();

    // alert("action:"+ajax_action);

    var action = 'handle_request.php';

    var div2 = document.createElement("div");
    s = '<iframe src="" name="'+iframe_id+'" id="'+iframe_id+'" syle="width:100px;height:100px;border:1px solid blue;"></iframe>';
    s += '<form id="'+form_id+'" action="'+action+'" method="POST" enctype = "multipart/form-data" accept-charset="UTF-8" target="'+iframe_id+'">';
    // alert("TEXT:"+s);
    div2.innerHTML = s;
    scratch.appendChild(div2);

    var iframe = document.getElementById(iframe_id);
    var form = document.getElementById(form_id);

    var el = document.createElement("input");
    el.type = "hidden";
    el.name = "data";

    if(document.version)
        message.data.version = document.version;

    // alert(JSON.stringify(message.data));
    el.value = JSON.stringify(message.data);
    form.appendChild(el);

    if(message.from_slot)
    {
        var el = document.createElement("input");
        el.type = "hidden";
        el.name = "from_slot";
        el.value = JSON.stringify(message.from_slot);
        form.appendChild(el);
    }

    for(el_index in message.elements)
    {
        var el = message.elements[el_index];
        
        el.parentNode.removeChild(el);
        form.appendChild(el);
    }

    // alert("submitting ...");
    form.submit(); // There should be NO submit button, or this will not work.
}

