// If we need to display some info about loading
var busybee='';
var busybeecont='';
// Global used with async call
var req;

/*
/ Function for synchronious HTTP call
/ Atention! Synchronious call works well in Firefox and Opera, but IE is buggy
*/
function loadHTML(URL,method,content){
    reqmethod=(method&&method=='POST')?method:'GET';
    var request = getHTTPObject();
    var flag=(reqmethod=='POST')?content:'';
   	if (request){
	    request.open(reqmethod, URL, false);
       if(reqmethod=='POST'){
            httpreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            httpreq.setRequestHeader("Content-length", content.length);
        }
        httpreq.setRequestHeader("Connection", "close");
        httpreq.send(flag);
	    var response = request.responseXML.documentElement;
        var method = response.getElementsByTagName('method')[0];
        method =(method!=null&&method.firstChild!=null)?method.firstChild.data:'';
        var target = response.getElementsByTagName('target')[0];
        target=(target!=null&&target.firstChild!=null)?target.firstChild.data:'';
        var hint = response.getElementsByTagName('hint')[0];
        hint=(hint!=null&&hint.firstChild!=null)?hint.firstChild.data:'';
        var content = response.getElementsByTagName('content')[0];
        content=(content!=null&&content.firstChild!=null)?content.firstChild.data:'';
        return eval(method + '(content,target,hint)');
    }
}

/*
/ Asynchronious HTTP call. Use whenever is possible, 
/ i.e. whenever there is no need for two or more successive http call one after each other
*/
function asyncloadHTML(URL,method,content){
	reqmethod=(method&&method=='POST')?method:'GET';
    req=getHTTPObject();
    var flag=(reqmethod=='POST')?content:'';
    if (req) {
        busyBee();
        req.onreadystatechange = function() { if (req.readyState==4) asyncCallBack();}
        req.open(reqmethod, URL, true);
        if(reqmethod=='POST'){
            req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            req.setRequestHeader("Content-length", content.length);
        }
        req.setRequestHeader("Connection", "close");
        req.send(flag);
    }

}

/*
/ Callback function which extracts data from XML stream
*/
function asyncCallBack(){
    if (req.status == 200) {
        var hint ='';
        var response = req.responseXML.documentElement;
        var method = response.getElementsByTagName('method')[0];
        method =(method!=null&&method.firstChild!=null)?method.firstChild.data:'';
        var target = response.getElementsByTagName('target')[0];
        target=(target!=null&&target.firstChild!=null)?target.firstChild.data:'';
        var hint = response.getElementsByTagName('hint')[0];
        hint=(hint!=null&&hint.firstChild!=null)?hint.firstChild.data:'';
        var content = response.getElementsByTagName('content')[0];
        content=(content!=null&&content.firstChild!=null)?content.firstChild.data:'';
        busyBee(busybeecont);
        req=null;
        return eval(method + '(content,target,hint)');
    } 
}

/*
/ Function shows info about http request
*/
function busyBee(bbcontent){
    busybee=document.getElementById('bread');
    if(busybee){
	    if(bbcontent!=null){
			if(!bbcontent.match('Oppdatering')){busybee.innerHTML=bbcontent;}
			else {busybee.innerHTML='';}
	        busybeecont='';
	        busybee=null;
		} else {
	        if(busybee){
	            busybeecont=busybee.innerHTML;
	            busybee.innerHTML='<span style="background-color:#ff0000;color:#ffffff;">&nbsp;Oppdatering...&nbsp;</span>';
	        }
	    }
    }
}

/*
/ Function which returns HTTP object depending of browser
*/
function getHTTPObject(){
    var xmlhttp=null;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
    catch (e) {
        try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
        catch (E) { xmlhttp = null;}
    }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        try { xmlhttp = new XMLHttpRequest();}
        catch (e) {xmlhttp=null;}
    }
    if (!xmlhttp && window.createRequest) {
        try { xmlhttp = window.createRequest();}
        catch (e) {xmlhttp=null;}
    }
    return xmlhttp;
}