function xhrRequest(type) {
	var xhrSend;
	if (!type) {
		type = 'xml';
	}
	if (window.ActiveXObject) {
		try {
			xhrSend = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhrSend = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} else if (window.XMLHttpRequest) {
		xhrSend = new XMLHttpRequest();
		if (xhrSend.overrideMimeType) {
			xhrSend.overrideMimeType('text/' + type);
		}
	}
	return (xhrSend);
}

function nextAd(divid) {
	var xhrRec = xhrRequest();
	var adDiv = document.getElementById(divid);
	xhrRec.open('GET', '/banner/ajax-banner.php?ts=' + new Date().getMilliseconds(), true);
	xhrRec.onreadystatechange = function() {
		if(xhrRec.readyState == 4 && xhrRec.status == 200) {
			loadBanner(adDiv, xhrRec.responseXML);
		}
	}
	xhrRec.send(null);
}

function loadBanner(adDiv, xml, isStatic) {
        var reload_after = xml.getElementsByTagName('reload').item(0).firstChild.nodeValue;
        var content_type = xml.getElementsByTagName('type').item(0).firstChild.nodeValue;
        //var divid = xml.getElementsByTagName('divid').item(0).firstChild.nodeValue;
        var link = xml.getElementsByTagName('link').item(0).firstChild.nodeValue;

        var html_content;
        switch(content_type) {
        case '0'://Ren HTML
                html_content = xml.getElementsByTagName('content').item(0).firstChild.nodeValue;
                break;
        default:
                html_content = "Type not defined!";
        }
	adDiv.innerHTML = html_content;
	adDiv.onclick = "top.location.href='" + link + "'"; //Relative burde virke, selvom absolutte URL'er er at foretraekke

    /*try {
        clearTimeout(to);
    } catch (e) {}*/
	var timeoutfunc = "nextAd('" + adDiv.id + "')";
	to = setTimeout(timeoutfunc, parseInt(reload_after));
}

function loadAdDivs() {
	var adDivs = getElementsByClassName("rotating-banner");
	for(i=0;i<adDivs.length;i++) {
		if(adDivs[i].id) nextAd(adDivs[i].id);
	}
}

//Loaded in onload.js
//window.onload = loadAdDivs;
