function HttpReqObject() {
	//Create an object to hold the XMLHttpRequest
	var req; //Request object
	try {req = new XMLHttpRequest();	}
	catch (e1) {
		try {req = new window.ActiveXObject("Msxml2.XMLHTTP");	}
		catch (e2) {
			try {req = new window.ActiveXObject("Microsoft.XMLHTTP");}
			catch (e3) {req = false; window.status = "XMLHttpRequest error "+e3; }
		}
	}
	return req;
}
function ajax(formmethod, paramstring, url, targetelem, callback, callbackparam) {
    showAjaxRunning(targetelem); //Starts the icon that shows that our HTTP request is running
	var method = formmethod.toUpperCase();
    var finalUrl = url; //URL to the server file
    if (method == 'GET') {
		var jsTime = new Date().getTime();
		if (url.indexOf("?") > -1) {finalUrl = url + "&time="+jsTime;}
		else {finalUrl = url + "?time="+jsTime;	}
    }
	var client = new HttpReqObject();
	client.open(method, finalUrl, true);
   if (method == 'POST') { client.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); }
	client.onreadystatechange = function() {
        //Check the state:
		if (client.readyState == 4 && client.status == 200) {
			if (typeof callback == 'function') {callback(targetelem, client.responseText, callbackparam); }
         hideAjaxRunning(); //Hide the icon as our HTTP request is not running any longer
		}
		else {
		window.status = "Checks HTTP status "+client.readyState; //The request is still not ready and in the right status
		}
	}
    if (method == 'POST') {
        client.send(paramstring); //With POST-method we send the parameters this way
    }
    else {
        client.send(null); //With GET-method the parameters are send as part of the URL, so here we send null
    }
}


//*** Functions for individual handling of response

function visCallback(targetelement,response, callbackparam) {
	try {
		if (targetelement.innerHTML) {
			targetelement.innerHTML = response;
			setTimeout(function() {targetelement.innerHTML = ''}, 5000);
		}
		else if (document.getElementById(targetelement)) {
			document.getElementById(targetelement).innerHTML = response;
			setTimeout(function() {document.getElementById(targetelement).innerHTML = ''}, 5000);
		}
		else { alert(response); }
	} catch(e) {}
}

function callBackNone(arg1,arg2,arg3) {
    return;
}
function ajaxShowAlert(targetelement, txt, extraparam) {
    txt = txt.replace(/\r\n/g,""); //Remove line shifts
    window.alert(txt);  
}
function ajaxShowStatus(targetelement, ajaxTxt, extraparam) {
    //Gets a Ajax-response and shows the content as a window alert:
    var txt = ajaxTxt.substring(ajaxTxt.indexOf("<msg>")+5, ajaxTxt.indexOf("</msg>") );    
    var msg = txt.replace(/\r\n/g,""); //Remove line shifts
    window.status = msg;  
}

//*** Assisting functions:
function parseXML(text){
    ix = text.indexOf('<?xml');
    if (ix>=0){
        text = text.substring(ix,text.length);
    }

    ix = text.indexOf('</root>');
    if (ix>=0){
        text = text.substring(0,ix+7);
    }

    text=trim(text);
    if (window.ActiveXObject){
        var doc=new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(text);
    }
    else{// code for Mozilla, Firefox, Opera, etc.
        var parser=new DOMParser();
        var doc=parser.parseFromString(text,"text/xml");
    }
    return doc.documentElement;
}

var ajaxspinner = null; var ajaxspinTimer = null;
function showAjaxRunning(target) {
	ajaxspinner = document.createElement("div");
	ajaxspinner.className = "ajaxspinner";
	ajaxspinner.innerHTML = "Systemet arbejder...";
	if (document.getElementById(target) ) {document.getElementById(target).appendChild(ajaxspinner);}
	else {document.body.appendChild(ajaxspinner); }
	ajaxspinTimer = setTimeout(hideAjaxRunning, 5000);
}
function hideAjaxRunning() {
	try {
		if (ajaxspinTimer != null) {clearTimeout(ajaxspinTimer); ajaxspinTimer = null; }
		if (ajaxspinner != null) { ajaxspinner.parentNode.removeChild(ajaxspinner);    }
	} catch(e) {}
}

function retrieveFieldvalues(f) {
	try {
		var flds = f.elements; var paramliste = new Array(); var paramstr = "";
		for (var i=0; i<flds.length; i++) {
			if (flds[i].type == "checkbox" && !flds[i].checked) {} //Ikke-valgte checkbokse springes over
			else if (flds[i].type == "button") {} //Knapper springes over
			else {
				paramliste.push(flds[i].name + "=" + flds[i].value);
			}
		}
		paramstr = paramliste.join('&'); return paramstr;
	} catch(e) {window.alert = "Kan ikke finde formularen med data.";}
}

