function http_create() {
    var http_obj = null;
    if(window.XMLHttpRequest)
        http_obj = new XMLHttpRequest();
    else if (window.ActiveXObject) {
        try {
            http_obj = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            try {
                http_obj = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {}
        }
    }
    return http_obj;
}

var http = http_create()
var http2 = http_create()
var http_counter = http_create()

function http_loader(http_obj, load_obj) {
    if (http_obj && load_obj) {
        if(http_obj.readyState != 4 || http_obj.status != 200 && http_obj.status != 304)
            try {
                load_obj.innerHTML = '<div class="http-error">' + http_obj.statusText + '</div>'
            } catch(e) {}

        else
            if(load_obj.onHTTPLoad)
                load_obj.onHTTPLoad(http_obj);
            else
                load_obj.innerHTML = http_obj.responseText;
        
    }
}

function http_get(url, http_obj, load_id, loading_html) {
    if(http_obj) {
        load_obj = document.getElementById(load_id.toString());
        if (load_obj) {
            load_obj.innerHTML = loading_html;
            http_obj.open('get', encodeURI(url), true);
            http_obj.onreadystatechange = function() { return http_loader(http_obj, load_obj); }
            if (window.XMLHttpRequest)
                http_obj.send(null);
            else
                http_obj.send();
        }
    }
}



function http_send(url, id) {
    http_get(url, http, id, '<div class="loading">Trwa pobieranie danych.<br/><img src="http://media.cojestgrane.pl/fx/working.gif"/></div>');
}

function http2_send(url, id) {
    http_get(url, http2, id, '<div class="loading">Trwa pobieranie danych.<br/><img src="http://media.cojestgrane.pl/fx/working.gif"/></div>');
}

function counter(url, id) {
    http_get(url, http_counter, id, '...')
}


function http_get_auto(url, id, http_obj) {
    http_get(url, http_obj, id, '<img src="http://media.cojestgrane.pl/fx/loading.gif"/>');
}

function iframe_onload(frame) {
    
    http2.open('get', '/monitor/', true);
    if (window.XMLHttpRequest)
        http2.send(null);
    else
        http2.send();
}

document_write = (function (fn) 
	{ 
	    return function (str) 
	    { 
            var idx = str.search('></iframe>');
            if(idx != -1) {
                str = str.substr(0,idx) + ' onload="javascript:iframe_onload(this);"' + str.substr(idx, str.length);
            }
	        var elem = fn.call(document, str); 
	        return elem; 
	    }; 
	})(document.write); 


