/*
instructors.js
last modified 11/26/06
created by Darren Smith

loads instructors XML data and outputs it as formatted HTML
*/

var req;

function parseLineBreaks(strString){
	//find formatted line breaks and convert to <p></p>
	strString = String(strString);
	strString = strString.replace(/\n\s*\n/g, "</p><p>");
	strString = strString.replace(/\n/g, "</p><p>");
	strString = "<p>" + strString + "</p>";
	return strString;
}
function getInstructors(){
		loadXMLData("_data/getInstructors.php");
}
function loadXMLData(url){
	req = false;
	//find available XML object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	//if XML capable, set listener, open XML document
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}
function processReqChange() {
	//wait for loading to complete
    if (req.readyState == 4) {
		//make sure loading was successful
        if (req.status == 200) {
			writeDetails();
        } else {
			//failure alert
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}
function writeDetails(){
	//build instructors table
	var instructorsTable = "";
	//determine number of instructors
	var instructorCount = req.responseXML.firstChild.childNodes.length;
	//loop through each instructor, add them to instructors table
	for(var i = 0; i < instructorCount; ++i){
		instructorsTable += '<div class="instructor">';
		instructorsTable += '<table class="instructorTable">';
		instructorsTable += '<tr>';
		instructorsTable += '<td class="pics"><img src="' + req.responseXML.firstChild.childNodes[i].childNodes[3].firstChild.nodeValue + '" alt="instructor picture" /></td>';
		instructorsTable += '<td><h3>' + req.responseXML.firstChild.childNodes[i].childNodes[0].firstChild.nodeValue + '</h3>';
		instructorsTable += '<h4>' + req.responseXML.firstChild.childNodes[i].childNodes[1].firstChild.nodeValue + '</h4>';
		instructorsTable += parseLineBreaks(req.responseXML.firstChild.childNodes[i].childNodes[2].firstChild.nodeValue);
		instructorsTable += '</td></tr></table>';
		instructorsTable += '</div>';
	}
	//populate instructors table
	document.getElementById("instructors").innerHTML = instructorsTable;
}