// Ajax routines

var g_url = "";
var g_data = "";

function ajax(url, data, flag, strip) {
    var req  = null;

    try {
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
        }
        else {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }

        if (req) {
            req.onreadystatechange = function() {
               // only if req shows "loaded"
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {
                        if (strip)
                            successr(req, data);
                        else
                            success(req, data);
                    } else {
                        failure(req, data);
                    }
                }
            }
            req.open("GET", url, flag);
            req.setRequestHeader("Content-Type", "text/xml");
            req.send(null);
        }
        else {
            alert("DATA.JS: ajax function error");
            return false;
        }

    } catch(e) {
       alert("DATA.JS: ajax request error: " + e);
    }
}

function success(req, data) {
    var res = req.responseXML;
    if (data == 0)
        obj = g_data;
    else
        obj = document.getElementById(data);
    if (obj)  {
        try {
            obj.innerHTML = "<pre>" + res.getElementsByTagName('Data')[0].firstChild.nodeValue +"</pre>";
        } catch(e) {
            obj.innerHTML = "<pre>" + "Data Tag Error " +e +"</pre>";
        }
    }
    else {
        if (parent.bottom) {
            obj = parent.bottom.document.getElementsByTagName(data);
            if (obj)
                obj[0].innerHTML = "<pre>" + res.getElementsByTagName('Data')[0].firstChild.nodeValue +"</pre>";
        }
    }
}

function successr(req, data) {
    var res = req.responseXML;
    obj = document.getElementById(data);
    if (obj)  {
        try {
            obj.innerHTML = "<pre>" + res.getElementsByTagName('Data')[0].firstChild.nodeValue.replace(/\n/g, ' ') +"</pre>";
        } catch(e) {
            alert("DATA.JS: success function error: " + e);
        }
    }
    else {
        if (parent.bottom) {
            obj = parent.bottom.document.getElementsByTagName(data);
            if (obj)
                obj[0].innerHTML = "<pre>" + res.getElementsByTagName('Data')[0].firstChild.nodeValue +"</pre>";
        }
    }
}

function failure(req, data) {
    var res = req.responseXML;
    obj = document.getElementById(data);
    if (obj)
        obj.lastChild.nodeValue = 'Server unreachable';
    else {
        if (parent.bottom) {
            obj = parent.bottom.document.getElementsByTagName(data);
            if (obj)
                obj[0].lastChild.nodeValue = 'Server unreachable';
        }
    }
}

function doLoad(url, data) {
   ajax(url, data, true, false);
}

function doLoadR(url, data) {
   url = 'shell?cmd=xml+httpgetr+'+ReturnUrl(url);
   ajax(url, data, true, true);
}

function doLoadS(url, data) {
   ajax(url, data, false, false);
}

function doLoadT(url, data, period) {
    g_url = url;
    g_data = data;

    ajax(url, data, true, false);
    setInterval("ajax(g_url, g_data, true, false)", period*1000);
}

function doBlank(arg1) {
    if (parent.bottom) {
        obj = parent.bottom.document.getElementsByTagName(arg1);
        if (obj)
            obj[0].innerHTML = "";
    }
}

function popup(w, h) {
	if (w == undefined) {
		var w = "820";
	}
	if (h == undefined) {
		var h = "650";
	}
	eval ("win=window.open('','Popup','location=no,status=yes,menubar=no,toolbar=no,scrollbars=yes,resizable=yes,width="+w+",height="+h+"');");

	win.focus();
}

