/******************
 * creating the xmlHttpRequest object
 *****/
function createXmlHttpReq() {
    var xmlhttp = false;
    /*@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 = false;
	}
    }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
	    xmlhttp = new XMLHttpRequest();
	} catch (e) {
	    xmlhttp=false;
	}
    }
    if (!xmlhttp && window.createRequest) {
	try {
	    xmlhttp = window.createRequest();
	} catch (e) {
	    xmlhttp=false;
	}
    }
    return xmlhttp;
}

/******************
 * retrieve the amount of new and total messages of private message system
 *****/
var xmlhttpNumMsg = false; // one object for page lifecycle
function getNumMessages() {
    var xmlhttp = false;
    if (!xmlhttpNumMsg) {
	xmlhttp = createXmlHttpReq();
	xmlhttpNumMsg = xmlhttp;
    } else {
	xmlhttp = xmlhttpNumMsg;
    }
    if (xmlhttp) {
	xmlhttp.open("GET", "/external/ajax.php?inbox=true", true);
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4 && xmlhttp.status == 200) {
		if (xmlhttp.responseXML != null) {
		    data = xmlhttp.responseXML.getElementsByTagName("msg");
		    if (data == null) return;
		    if (data[0].firstChild == null) return;
		    if (data[0].firstChild.firstChild == null) return;
		    var newMsg = data[0].firstChild.firstChild.nodeValue;
		    var totMsg = data[0].lastChild.firstChild.nodeValue;
		    if (newMsg > 0) {
			alert("U heeft "+newMsg+" nieuw"+(newMsg>1?"e":"")+" bericht"+(newMsg>1?"en":"")+" van de "+totMsg+" berichten in totaal");
		    }
		    elem = document.getElementById("pmtot");
		    if (elem != null) elem.innerHTML = totMsg;
		    elem = document.getElementById("pmnew");
		    if (elem != null) elem.innerHTML = newMsg;
		    setTimeout("getNumMessages()", 240000); // call this again after 5 mins
		}
	    }
	}
	xmlhttp.send(null)
    } else {
    
    }
}
/******************
 * retrieve the status of teh radio
 *****/
var xmlhttpShout = false; // one object for page lifecycle
function getShoutStatus() {
    var xmlhttp = false;
    if (!xmlhttpShout) {
	xmlhttp = createXmlHttpReq();
	xmlhttpShout = xmlhttp;
    } else {
	xmlhttp = xmlhttpShout;
    }
    if (xmlhttp) {
	xmlhttp.open("GET", "/external/shoutstatus.php", true);
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4 && xmlhttp.status == 200) {
		if (xmlhttp.responseXML != null) {
		    data = xmlhttp.responseXML;
		    var online = data.getElementsByTagName("streaming")[0].firstChild.nodeValue;

		    elem = document.getElementById("ffm");
		    if (online == "true") {
			var program = data.getElementsByTagName("program")[0].firstChild.nodeValue;
			var song = data.getElementsByTagName("song")[0].firstChild.nodeValue;
			if (elem != null) elem.innerHTML = "<a href=\"http://fridgefm.com/listen.m3u\"><img src=\"/pics/winamp.gif\" border='0'/> Tune in!</a><br />"+
							   "<a href=\"http://fridgefm.com/listen.asx\"><img src=\"/pics/wmp.gif\" border='0'/> Tune in!</a>";
		    } else {
			if (elem != null) elem.innerHTML = "Offline, <a href=\"http://www.fridgefm.com\">bekijk de showtijden</a>";
		    }
		    setTimeout("getShoutStatus()", 300000); // call this again after 5 mins
		}
	    }
	}
	xmlhttp.send(null)
    } else {
    
    }
}

/******************
 * if we have ajax we submit the poll via ajax, preventing the reload of a page
 *****/
var xmlhttpPoll = false;
function doAJAXpollSubmit(f) {
    var xmlhttp = false;
    if (!xmlhttpPoll) {
	xmlhttp = createXmlHttpReq();
	xmlhttpPoll = xmlhttp;
    } else {
	xmlhttp = xmlhttpPoll;
    }
    var elem = document.getElementById("pollcontent");
    if (elem != null && xmlhttp) {
	var vote = -1;
	for (i = 0; i < f.vote.length; i++) {
	    if (f.vote[i].checked) {
		vote = f.vote[i].value;
	    }
	}
	if (vote > -1) {
	    setVis("pollbtn", false);
	    xmlhttp.open("POST", "/external/ajax.php", true);
	    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status == 200) {
		    if (xmlhttp.responseText != null) {
			elem.innerHTML = xmlhttp.responseText;
		    }
		}
	    }
	    xmlhttp.send("id="+f.id.value+"&vote="+vote);
	}
    } else { return true; }
    
    return false;
}

AJAXfunctions = true;
