/*CS This is for the alumni pages to list the school partnerships available*/

//Write info to page
function createSchoolDiv(xmlDoc, schoolIndex) {	
	var div = document.getElementById("schoolList");
	var schoolType=xmlDoc.getElementsByTagName("schoolType"); 
	var school=schoolType[schoolIndex].getElementsByTagName("school");	
	
	if (schoolIndex == 6) { //If this is an MSE school
		var div2 = null;
		var newDiv = null;
		var testPrev; //Previous school
		var testCurrent; //Current school
		
		//create new divs for each mse type
		engDiv = document.createElement("div");
		engDiv.setAttribute("id","engDiv");
		engTable = document.createElement('table');
		engTbody = document.createElement('tbody');
		engRow = document.createElement('tr');
		engCell = document.createElement('td');
	
		bioDiv = document.createElement("div");
		bioDiv.setAttribute("id","bioDiv");
		bioTable = document.createElement('table');
		bioTbody = document.createElement('tbody');
		bioRow = document.createElement('tr');
		bioCell = document.createElement('td');
		
		chemDiv = document.createElement("div");
		chemDiv.setAttribute("id","chemDiv");
		chemTable = document.createElement('table');
		chemTbody = document.createElement('tbody');
		chemRow = document.createElement('tr');
		chemCell = document.createElement('td');
		
		physDiv = document.createElement("div");
		physDiv.setAttribute("id","physDiv");
		physTable = document.createElement('table');
		physTbody = document.createElement('tbody');
		physRow = document.createElement('tr');
		physCell = document.createElement('td');
		
		mathDiv = document.createElement("div");
		mathDiv.setAttribute("id","mathDiv");
		mathTable = document.createElement('table');
		mathTbody = document.createElement('tbody');
		mathRow = document.createElement('tr');
		mathCell = document.createElement('td');
		
		for(var i=0; i<school.length; i++) { 
			
			testCurrent = school[i].getAttribute("name");
			var lineBreak = document.createElement('br');
			var em = document.createElement('em');
			var theData = document.createTextNode(school[i].getAttribute("name"));
		//	var theData2 = document.createTextNode(" - " + school[i].getAttribute("coll"));
			var programType = school[i].getAttribute("program");

			if(testCurrent == testPrev && programType != prevProg || testCurrent != testPrev){		
				if(programType == "Engineering") {
					engCell.appendChild(theData);
			//		engCell.appendChild(em);
			//		em.appendChild(theData2);
					engCell.appendChild(lineBreak);
					testPrev = school[i].getAttribute("name");
					prevProg = school[i].getAttribute("program");
				}
			
				if(programType == "Biology") {
					bioCell.appendChild(theData);
				//	bioCell.appendChild(em);
			    //	em.appendChild(theData2);
					bioCell.appendChild(lineBreak);
					testPrev = school[i].getAttribute("name");
					prevProg = school[i].getAttribute("program");
				}	
			
					if(programType == "Chemistry") {
					chemCell.appendChild(theData);
				//	chemCell.appendChild(em);
			    //	em.appendChild(theData2);
					chemCell.appendChild(lineBreak);
					testPrev = school[i].getAttribute("name");
					prevProg = school[i].getAttribute("program");
				}
			
				if(programType == "Physics") {
					physCell.appendChild(theData);
				//	physCell.appendChild(em);
				//	em.appendChild(theData2);
					physCell.appendChild(lineBreak);
					testPrev = school[i].getAttribute("name");
					prevProg = school[i].getAttribute("program");
				}
			
				if(programType == "Mathematics") {	
					mathCell.appendChild(theData);
				//	mathCell.appendChild(em);
				//	em.appendChild(theData2);
					mathCell.appendChild(lineBreak);
					testPrev = school[i].getAttribute("name");
					prevProg = school[i].getAttribute("program");
				}
			} 
		}
		
		var parentDiv = null;
		div2 = document.getElementById("engineering"); //Container for school list
		if(div2 != null) {
		parentDiv = div2.parentNode; 
		parentDiv.replaceChild(engDiv, div2);
		engDiv.appendChild(engTable);
		engTable.appendChild(engTbody);
		engTbody.appendChild(engRow);
		engRow.appendChild(engCell);
		}
		
		var parentDiv = null;
		div2 = document.getElementById("biology"); //Container for school list
		if(div2 != null) {
		parentDiv = div2.parentNode; 
		parentDiv.replaceChild(bioDiv, div2);
		bioDiv.appendChild(bioTable);
		bioTable.appendChild(bioTbody);
		bioTbody.appendChild(bioRow);
		bioRow.appendChild(bioCell);
		}
				
		var parentDiv = null;
		div2 = document.getElementById("chemistry"); //Container for school list
		if(div2 != null) {
		parentDiv = div2.parentNode; 
		parentDiv.replaceChild(chemDiv, div2);
		chemDiv.appendChild(chemTable);
		chemTable.appendChild(chemTbody);
		chemTbody.appendChild(chemRow);
		chemRow.appendChild(chemCell);
		}
				
		var parentDiv = null;
		div2 = document.getElementById("physics"); //Container for school list
		if(div2 != null) {
		parentDiv = div2.parentNode; 
		parentDiv.replaceChild(physDiv, div2);
		physDiv.appendChild(physTable);
		physTable.appendChild(physTbody);
		physTbody.appendChild(physRow);
		physRow.appendChild(physCell);
		}
				
		var parentDiv = null;
		div2 = document.getElementById("mathematics"); //Container for school list
		if(div2 != null) {
		parentDiv = div2.parentNode; 
		parentDiv.replaceChild(mathDiv, div2);
		mathDiv.appendChild(mathTable);
		mathTable.appendChild(mathTbody);
		mathTbody.appendChild(mathRow);
		mathRow.appendChild(mathCell);
		}
	}
	
	else {
		var schTable = document.createElement('table');
		schTable.setAttribute('id','schTable');
		schTable.setAttribute('cellpadding','0');
		schTable.setAttribute('cellspacing','0');
		var schTbody = document.createElement('tbody');
		var schRow = document.createElement('tr');
		var schCell = document.createElement('td');
	
		for(var i=0; i<school.length; i++) {
			
			var lineBreak = document.createElement('br');
			var testPrev; //Previous school
			var testCurrent = school[i].getAttribute("name"); //Current school
			var theData = document.createTextNode(school[i].getAttribute("name")); //Create text for the school name
			
			if(schoolIndex==4){
				var theData2 = document.createTextNode(" - " + school[i].getAttribute("coll"));
				var em = document.createElement('em');
			}
			
			if(testCurrent != testPrev || schoolIndex == 4){ //Because we want to display the different colleges for med school
			schCell.appendChild(theData); //append the school name to the cell
			
				if(schoolIndex==4){
					schCell.appendChild(em); 
					em.appendChild(theData2);
				}
			
				schCell.appendChild(lineBreak);
				schRow.appendChild(schCell);//append the cell to the row
				schTbody.appendChild(schRow);//append the row to the table
				schTable.appendChild(schTbody);
				div.appendChild(schTable);//append the new div to the page div
				testPrev = school[i].getAttribute("name"); //Set current school to previous school
			}
		}
	}
}
