// JavaScript Document
function addLoadEvent(func) {
	// Allow multiple loaders
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
			window.onload = function() {
				oldonload();
				func();
			}
	}
}

function StaffListLinks() {
	// Attach handlers to Staff Links
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	var stafflinks = document.getElementById("stafflist");
	if (!stafflinks.done) {
		stafflinks.done = 1;
		var a = stafflinks.getElementsByTagName("a");
		for (var i=0; i<a.length; i++) {
			elm = a[i];
			elm.onclick = function () {
				// Image Path comes from page code
				if (httpRequest("GET",this.href+'&ajax=1',true,UpdatePerson)) {
					// Block normal link
					return false;
				} else {
					// allow normal link
					return true;
				}
			};
		}
	}
}

function ActivityListLinks() {
	// Attach handlers to Staff Links
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	var actlinks = document.getElementById("activitylist");
	if (!actlinks.done) {
		actlinks.done = 1;
		var a = actlinks.getElementsByTagName("a");
		for (var i=0; i<a.length; i++) {
			elm = a[i];
			elm.onclick = function () {
				// Image Path comes from page code
				if (httpRequest("GET",this.href+'&ajax=1',true,UpdateActivity)) {
					// Block normal link
					return false;
				} else {
					// allow normal link
					return true;
				}
			};
		}
	}
}

function NewsListLinks() {
	// Attach handlers to Staff Links
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	var actlinks = document.getElementById("newslist");
	if (!actlinks.done) {
		actlinks.done = 1;
		var a = actlinks.getElementsByTagName("a");
		for (var i=0; i<a.length; i++) {
			elm = a[i];
			elm.onclick = function () {
				// Image Path comes from page code
				if (httpRequest("GET",this.href+'&ajax=1',true,UpdateNews)) {
					// Block normal link
					return false;
				} else {
					// allow normal link
					return true;
				}
			};
		}
	}
}


var request;

function UpdateContent(id,val) {
    var elm = document.getElementById(id);
	if (elm) {
		elm.innerHTML=val;
	}
}

function UpdateImage(id,val) {
    var elm = document.getElementById(id);
	if (elm) {
		elm.src=val;
	}
}

//event handler for XMLHttpRequest to Update the Persons info
function UpdateNews(){
    if(request.readyState == 4){
        if(request.status == 200){
            var resp =  request.responseText;
            //alert(resp);
            var func = new Function("return "+resp);	// resp is in JSON format
            var activity = func();	// We now have an object which is the response
			UpdateContent("n_desc",activity.n_desc);
			UpdateContent("n_sdesc",activity.n_sdesc);
			UpdateImage("n_pic",activity.n_pic);
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}

//event handler for XMLHttpRequest to Update the Persons info
function UpdateActivity(){
    if(request.readyState == 4){
        if(request.status == 200){
            var resp =  request.responseText;
            //alert(resp);
            var func = new Function("return "+resp);	// resp is in JSON format
            var activity = func();	// We now have an object which is the response
			UpdateContent("a_desc",activity.a_desc);
			UpdateImage("a_pic",activity.a_pic);
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}


//event handler for XMLHttpRequest to Update the Persons info
function UpdatePerson(){
    if(request.readyState == 4){
        if(request.status == 200){
            var resp =  request.responseText;
            //alert(resp);
            var func = new Function("return "+resp);	// resp is in JSON format
            var person = func();	// We now have an object which is the response
			UpdateContent("s_name",person.s_name);
			UpdateContent("s_position",person.s_position);
			UpdateContent("s_bornplace",person.s_bornplace);
			UpdateContent("s_birthday",person.s_birthday);
			UpdateContent("s_favact",person.s_favact);
			UpdateContent("s_cool",person.s_cool);
			UpdateContent("s_uncool",person.s_uncool);
			UpdateContent("s_music",person.s_music);
			UpdateContent("s_tv",person.s_tv);
			UpdateContent("s_film",person.s_film);
			UpdateImage("s_pic",person.s_pic);
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}

/* Initialize a Request object that is already constructed */
function initReq(reqType,url,bool,rfunc){
    /* rfunc specifies the function that will handle the HTTP response */
    request.onreadystatechange=rfunc;
    request.open(reqType,url,bool);
    request.send(null);
}

/* Wrapper function for constructing a Request object.
 Parameters:
  reqType: The HTTP request type such as GET or POST.
  url: The URL of the server program.
  asynch: Whether to send the request asynchronously or not.
  rfunc the return handler
*/
function httpRequest(reqType,url,asynch,rfunc){
    //Mozilla-based browsers
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject){
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request){
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
     }
    //the request could still be null if neither ActiveXObject
    //initializations succeeded
    if(request){
       initReq(reqType,url,asynch,rfunc);
	   return true;
    }  else {
       return false;
	}
}

// Add page load handlers
addLoadEvent(StaffListLinks);
addLoadEvent(ActivityListLinks);
addLoadEvent(NewsListLinks);

