var debug_mode = false;
var request_type = "GET";
var request_uri = "/main.php?";

function debug(text) {
    if (debug_mode)
        alert("RSD: " + text)
}

function init_xmlhttp() {
    debug("init_xmlhttp() called..")

    var A;
    try {
        A=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            A=new ActiveXObject("Microsoft.XMLHTTP");
        } catch (oc) {
            A=null;
        }
    }
    if(!A && typeof XMLHttpRequest != "undefined")
        A = new XMLHttpRequest();
    if (!A)
        debug("Could not create connection object.");
    return A;
}

function show_wait_cursor() {
    document.body.style.cursor = "progress";
    document.getElementById('wait').style.display='block';
}

function show_default_cursor() {
    document.body.style.cursor = "default";
    document.getElementById('wait').style.display='none';
}

function rand_str() {
    var chars = "0123456789abcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    return randomstring;
}

function call_remote(func_name, div, callback, func_request_type, args) {
    var i, x, n;
    var uri;
    var post_data;
    var local_request_type;
	var tmp_html;

    show_wait_cursor();

    if (func_request_type)
        local_request_type = func_request_type;
    else
        local_request_type = request_type;

    uri = func_name;
    post_data = null;

    x = init_xmlhttp();
    x.open(local_request_type, uri, true);
    x.onreadystatechange = function() {
        if (x.readyState != 4)
            return;
        debug("received " + x.responseText);

        var data;
        data = x.responseText;
        if (data == "") {
            //alert("Error: no data");
        }
        else {
            callback(data, div);
        }

        show_default_cursor();
    }
    x.send(post_data);
    //alert(func_name + " uri = " + uri + "/post = " + post_data);
    debug(func_name + " uri = " + uri + "/post = " + post_data);
    debug(func_name + " waiting..");
    delete x;
}

function refresh_content(html, divid) {
  set_div(divid, html);
}

function refresh_div(html, div) {
  document.getElementById('display_table').style.top = document.body.scrollTop;
  document.getElementById(div).innerHTML = html;
  document.getElementById('display_table').style.display='block';
  document.getElementById(div).style.display='block';

  setTimeout("document.getElementById('display_table').style.display='none';", 3000);
}

function refresh_small1(html) {
  set_div('small1', html);
}

function set_div(divid, html) {
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(divid).innerHTML = html;
    } else {
        if (document.layers) {
            document.divid.innerHTML = html;
        } else {
            document.all.divid.innerHTML = html;
        }
    }
}

function set_div_r(html, divid) {
	set_div(divid, html);
}

function doShow(layer) 
{
  document.getElementById(layer).style.display='block';
}

function doHide(layer) 
{
  document.getElementById(layer).style.display='none';
}