// JavaScript Document
//object detection to return the correct object depending upon broswer type. Used by the getAXHA(); function.
function getNewHttpObject() {
    var objType = false;
    try {
        objType = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            objType = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            objType = new XMLHttpRequest();
        }
    }
    return objType;
}

//Function used to update page content with new xhtml fragments by using a javascript object, the dom, and http.
function getAXAH(url,elementContainer){
	document.getElementById(elementContainer).innerHTML = '<img src="/northernireland/schools/images/misc/loader.gif"/>';
	var theHttpRequest = getNewHttpObject();
	theHttpRequest.onreadystatechange = function() {processAXAH(elementContainer);};
	theHttpRequest.open("GET", url);
	theHttpRequest.send(false);

		function processAXAH(elementContainer){
		   if (theHttpRequest.readyState == 4) {			   
				document.getElementById(elementContainer).innerHTML = theHttpRequest.responseText;
		   }
		/*if (theHttpRequest.status == 200) {
		} else {
			document.getElementById(elementContainer).innerHTML="<p><span class='redtxt'>Error!<\/span> HTTP request return the following status message:&nbsp;" + theHttpRequest.statusText +"<\/p>";
		}*/
	}

}
/* Gets the full list of available links, then randomises them and takes the top three */
function getThreeRandomLinksForOutAndAbout(url, elementContainer){
	document.getElementById(elementContainer).innerHTML = '<img src="/northernireland/schools/images/misc/loader.gif"/>';
	var theHttpRequest = getNewHttpObject();
	theHttpRequest.onreadystatechange = function() {processAXAH(elementContainer);};
	theHttpRequest.open("GET", url);
	theHttpRequest.send(false);

		function processAXAH(elementContainer){
		   if (theHttpRequest.readyState == 4) {
			   var resp = document.createElement("div");
			   resp.innerHTML = theHttpRequest.responseText;
			   var items = resp.getElementsByTagName("div");
			   var new_items = new Array();
			   for(var i =0;i<items.length;i++){
				   if(items[i].className == "item"){
					   new_items.push(items[i]);
				   }
			   }
			   
			   new_items.sort(randSort);
			   if(new_items.length == 0){
				   document.getElementById(elementContainer).innerHTML = theHttpRequest.responseText;
			   } else {
				   var new_div = document.createElement("div");
				   new_div.innerHTML += new_items[0].innerHTML;
				   if(new_items[1]){
				   	new_div.innerHTML += new_items[1].innerHTML;
				   }
				   if(new_items[2]){
					   new_div.innerHTML += new_items[2].innerHTML;
				   }
				   //new_div.innerHTML += "<p>The BBC is not responsible for the content of external sites</p>";
				   
				   document.getElementById(elementContainer).innerHTML = new_div.innerHTML;
			   }
		   }
	}
}

function randSort(){
	return Math.round(Math.random())-0.5; 
}