/*

Author: Kevin Barry
Date: 2007-11-21

These functions can be used to dynamically display lists of
academic programs on any web page on the Wilmington University web site.
Lists of Programs can be filtered by site or academic division.

To display a list on a document:
1) Link this script to the page in the head section of the page.
2) Link the script 'programs.js' in the head section of the page.
3) Add the container <div id="degreeprograms"></div> in your document where you want the list to appear
4) add an embedded script in the head section
5) to list all programs, add the line: 'window.onload = listLevels;' to the embedded script
6) to filter the list by site or division:
	a) call the function 'getPrograms()' from the script, passing it the parameter to filter by ("sites" or "divisions"),
		and the	value for that parameter (see the 'locations' or 'divisions' array for values).
		Example: getPrograms("sites","N"); or getPrograms("divisions","BEH");
	b) add the line 'window.onload = listSelectLevels;'
	
This script is also used in an interactive page where the user can choose
to display a dynamically created list of academic programs by either site or division

*/

//create custom object 'degreeprogram'
function degreeprogram (progName,webLink,lvl,deg,divs,sts) {
	this.prog = progName;
	this.url = webLink;
	this.level = lvl;
	this.degree = deg;
	this.division = divs;
	this.sites = sts;
}

//create custom object 'site'
function site (steID,steName) {
	this.siteID = steID;
	this.siteName = steName;
}
//create and fill array of sites
var locations = new Array();
locations[0] = new site("N","New Castle");
locations[1] = new site("W","Wilson Graduate Center");
locations[2] = new site("M","Middletown");
locations[3] = new site("D","Dover");
locations[4] = new site("A","Dover Air Force Base");
locations[5] = new site("G","Georgetown");
locations[6] = new site("R","Rehoboth Beach");
locations[7] = new site("B","Burlington");
locations[8] = new site("S","Salem");
locations[9] = new site("C","Cumberland");
locations[10] = new site("O","Online");

//create custom object 'division'
function division (divsID,divsName) {
	this.divID = divsID;
	this.divName = divsName;
}
//create and fill array of divisions
var divisions = new Array();
divisions[0] = new division("BEH","Behavioral Science");
divisions[1] = new division("BUS","Business");
divisions[2] = new division("EDU","Education");
divisions[3] = new division("DOC","Doctoral Studies");
divisions[4] = new division("GEN","General Studies");
divisions[5] = new division("ITAC","Information Technology and Advanced Communications");
divisions[6] = new division("NUR","Nursing and Allied Health");

//create array of academic program levels
var levels = new Array("Certificate","Associates","Bachelors","Masters","Post-Masters","Doctorate");
//create array of programs **note: array is filled by external script 'programs.js'
var degProgs = new Array();

/*
Create array for selected degree programs to be displayed on page.
Programs can be selected using any attribute of the 'degreeprogram' object
by passing this function the name of the attribute and the attribute value
(both strings).
*/
var pagePrograms = new Array();
function getPrograms(param,paramvalue) {
	pagePrograms.length = 0;
	var numProgs = 0;
	for ( i=0; i<degProgs.length; i++ ) {
		if ( eval("degProgs[i]." + param).indexOf(paramvalue) != -1 ) {
			pagePrograms[numProgs] = degProgs[i];
			numProgs++;
		}
	}
	getLevels();
}

//create array for only degree program levels for degree programs selected for the page
var pageLevels = new Array();
function getLevels() {
	pagePrograms.sort(sortPrograms);
	pageLevels.length = 0;
	var numLevels = 0;
	var tempLvl = -1;
	for ( i=0; i<pagePrograms.length; i++) {
		if ( pagePrograms[i].level != tempLvl ) {
			pageLevels[numLevels] = levels[pagePrograms[i].level];
			tempLvl = pagePrograms[i].level
			numLevels++;
		}
	}
}

//Sorts degree programs by level then by program title
function sortPrograms(a,b) {
	if(a.level == b.level) {
		if(a.prog == b.prog) {
			return 0;
		}
		return (a.prog < b.prog) ? -1 : 1;
	}
	return (a.level < b.level) ? -1 : 1;
}

var ULelmt = document.createElement("UL");
var LIelmt = document.createElement("LI");
var Aelmt = document.createElement("A");

//checks to see if the container object passed to the function contains
//an unordered list HTML element
function checkProgList(list) {
	for ( i=0; i<list.childNodes.length; i++ ) {
		if ( list.childNodes[i].nodeName == "UL" ) {
			return true;
		}
	}
	return false;
}

//removes the a child unordered list HTML element from the parent element
//passed to the function. 
function clearProgList(list) {
	for ( i=0; i<list.childNodes.length; i++ ) {
		if ( list.childNodes[i].nodeName == "UL" ) {
			for ( j=list.childNodes[i].childNodes.length; j>0; j-- ) {
				list.childNodes[i].lastChild.setAttribute("id",null);
				list.childNodes[i].removeChild(list.childNodes[i].lastChild);
			}
			list.childNodes[i].removeAttribute("id");
			list.removeChild(list.childNodes[i]);
		}
	}
}

//lists all of the possible academic program levels from the
//entire array of degree programs 'degProgs'
function listLevels() {
	var listContainer = document.getElementById("degreeprograms");
	listContainer.appendChild(ULelmt.cloneNode(false));
	listContainer.lastChild.setAttribute("id","programlist");
	var programList = document.getElementById("programlist");
	for ( i=0; i<levels.length; i++) {
		programList.appendChild(LIelmt.cloneNode(false));
		programList.lastChild.setAttribute("id",levels[i]);
		programList.lastChild.appendChild(Aelmt.cloneNode(false));
		programList.lastChild.lastChild.setAttribute("href","#");
		programList.lastChild.lastChild.onclick = listProgsByLevel;
		programList.lastChild.lastChild.appendChild(document.createTextNode(levels[i]))
	}
}

//lists matching academic programs for each program level when a user
//clicks on the level in the list created by the function above
function listProgsByLevel() {
	degProgs.sort(sortPrograms);
	var tempLevel = this.parentNode.getAttribute("id");
	var listContainer = document.getElementById(tempLevel);
	if ( checkProgList(listContainer) ) {
		clearProgList(listContainer);
		listContainer.firstChild.style.backgroundImage = "url('../images/greentriangle_right.gif')"
		listContainer.firstChild.style.backgroundPosition = "6px center";
	}
	else {
		listContainer.firstChild.style.backgroundImage = "url('../images/greentriangle_down.gif')"
		listContainer.firstChild.style.backgroundPosition = "left center";
		listContainer.appendChild(ULelmt.cloneNode(false));
		listContainer.lastChild.setAttribute("id",tempLevel+"List");
		var programList = document.getElementById(tempLevel+"List");
		for ( i=0 ; i<degProgs.length ; i++ ) {
			if ( levels[degProgs[i].level] == tempLevel ) {
				programList.appendChild(LIelmt.cloneNode(false));
				programList.lastChild.appendChild(Aelmt.cloneNode(false));
				programList.lastChild.lastChild.setAttribute("href",degProgs[i].url)
				programList.lastChild.lastChild.appendChild(document.createTextNode(degProgs[i].prog));
			}
		}
	}
}

//creates a list of program levels using the pageLevels array
//created with the 'getLevels' function
function listSelectLevels() {
	var listContainer = document.getElementById("degreeprograms");
	if ( checkProgList(listContainer) ) {
		clearProgList(listContainer);
	}
	listContainer.appendChild(ULelmt.cloneNode(false));
	listContainer.lastChild.setAttribute("id","programlist");
	var programList = document.getElementById("programlist");
	for ( i=0; i<pageLevels.length; i++) {
		programList.appendChild(LIelmt.cloneNode(false));
		programList.lastChild.setAttribute("id",pageLevels[i]);
		programList.lastChild.appendChild(Aelmt.cloneNode(false));
		//programList.lastChild.lastChild.setAttribute("href","");
		programList.lastChild.lastChild.onclick = listSelectProgsByLevel;
		programList.lastChild.lastChild.appendChild(document.createTextNode(pageLevels[i]))
	}
}

//lists matching academic programs from the 'pagePrograms' array when a user
//click on a level from list created by the function above
function listSelectProgsByLevel() {
	pagePrograms.sort(sortPrograms);
	var tempLevel = this.parentNode.getAttribute("id");
	var listContainer = document.getElementById(tempLevel);
	if ( checkProgList(listContainer) ) {
		clearProgList(listContainer);
		listContainer.firstChild.style.backgroundImage = "url('../images/greentriangle_right.gif')"
		listContainer.firstChild.style.backgroundPosition = "6px center";
	}
	else {
		listContainer.firstChild.style.backgroundImage = "url('../images/greentriangle_down.gif')"
		listContainer.firstChild.style.backgroundPosition = "left center";
		listContainer.appendChild(ULelmt.cloneNode(false));
		listContainer.lastChild.setAttribute("id",tempLevel+"List");
		var programList = document.getElementById(tempLevel+"List");
		for ( i=0 ; i<pagePrograms.length ; i++ ) {
			if ( levels[pagePrograms[i].level] == tempLevel ) {
				programList.appendChild(LIelmt.cloneNode(false));
				programList.lastChild.appendChild(Aelmt.cloneNode(false));
				programList.lastChild.lastChild.setAttribute("href",pagePrograms[i].url)
				programList.lastChild.lastChild.appendChild(document.createTextNode(pagePrograms[i].prog));
			}
		}
	}
}

function listAllProgs() {
	listLevels();
	expandProgLists();
}

function expandProgLists() {
	var listContainer = document.getElementById("programlist");
	for ( i=0; i<listContainer.childNodes.length; i++ ) {
		if ( listContainer.childNodes[i].nodeName == "LI" ) {
			//alert(listContainer.childNodes[i].firstChild.nodeName);
			listContainer.childNodes[i].firstChild.handleEvent("click");
		}
	}
}

var INPUTelmt = document.createElement("INPUT");
var LABELelmt = document.createElement("LABEL");
var BRelmt = document.createElement("BR");

//clears the sets of radio buttons for sites and divisions
function removeListOptions() {
	var optionList = document.getElementById("locationList");
	for ( i=optionList.childNodes.length; i>0; i-- ) {
		optionList.removeChild(optionList.lastChild);
	}
	optionList = document.getElementById("divisionList");
	for ( i=optionList.childNodes.length; i>0; i-- ) {
		optionList.removeChild(optionList.lastChild);
	}
}
//creates a set of radio buttons for each site using the 'locations' array
//clicking on a site radio button displays programs for that site only
//using the 'getPrograms' function
function listLocationOptions() {
	var listContainer = document.getElementById("degreeprograms");
	if ( checkProgList(listContainer) ) {
		clearProgList(listContainer);
	}
	var locationOptions = document.getElementById("locationList");
	removeListOptions();
	for ( i=0; i<locations.length; i++ ) {
		locationOptions.appendChild(INPUTelmt.cloneNode(false));
		locationOptions.lastChild.setAttribute("type","radio");
		locationOptions.lastChild.setAttribute("name","progLocation");
		locationOptions.lastChild.setAttribute("id",locations[i].siteID);
		locationOptions.lastChild.setAttribute("value",locations[i].siteID);
		locationOptions.lastChild.onclick = function(){getPrograms("sites",this.getAttribute("id"));listSelectLevels();};
		locationOptions.appendChild(LABELelmt.cloneNode(false));
		locationOptions.lastChild.setAttribute("for",locations[i].siteID);
		locationOptions.lastChild.appendChild(document.createTextNode(locations[i].siteName));
		locationOptions.appendChild(BRelmt.cloneNode(false));
	}
}
//creates a set of radio button for each division using the 'divisions' array
//clicking on a division radio button displays programs for that division only
//using the 'getPrograms' function
function listDivisionOptions() {
	var listContainer = document.getElementById("degreeprograms");
	if ( checkProgList(listContainer) ) {
		clearProgList(listContainer);
	}
	var divisionOptions = document.getElementById("divisionList");
	removeListOptions();
	for ( i=0; i<divisions.length; i++ ) {
		divisionOptions.appendChild(INPUTelmt.cloneNode(false));
		divisionOptions.lastChild.setAttribute("type","radio");
		divisionOptions.lastChild.setAttribute("name","division");
		divisionOptions.lastChild.setAttribute("id",divisions[i].divID);
		divisionOptions.lastChild.setAttribute("value",divisions[i].divID);
		divisionOptions.lastChild.onclick = function(){getPrograms("division",this.getAttribute("id"));listSelectLevels();};
		divisionOptions.appendChild(LABELelmt.cloneNode(false));
		divisionOptions.lastChild.setAttribute("for",divisions[i].divID);
		divisionOptions.lastChild.appendChild(document.createTextNode(divisions[i].divName));
		divisionOptions.appendChild(BRelmt.cloneNode(false));
	}
}