function Chapter(titl,addr,targ) {
	this.titl = titl;
	this.addr = addr;
	this.targ = targ;
	this.pages;
	this.current = false;
}
function Page(titl,addr,descr) {
	this.titl = titl;
	this.addr = addr;
	this.descr = descr;
	this.current = false;
}
function navtoPage(pageUrl, targ) {
	if (targ == 'tour_content_frame') {
		jQuery('#'+targ).load(pageUrl);
	} else {
		window.open(pageUrl,targ);
	}
}
function Navigation() {
	var self = this;
	var debugMode = true;
	var xmlDoc;
	
	//parse out the data and fill the chapters array
	this.parseData = function() {
		this.mainURL = urls[0].firstChild.nodeValue;
		this.mainTarget = targs[0].firstChild.nodeValue;
//		this.debug(this.mainURL);
//		this.debug(this.mainTarget);
		
		//loop through chapter xml nodes
		for (var c=0; c<tempChapts.length;c++) {
			
			// create each chapter object to be stored in the array
			this.chapters[c] = new Chapter();

			// fill the chapter object
			var mychapt = tempChapts[c];
			this.chapters[c].titl = this.nodeData(mychapt, 'title');
			this.chapters[c].addr = this.nodeData(mychapt, 'url');
			this.chapters[c].targ = this.nodeData(mychapt, 'target');
			
			// create collection of pages
			this.chapters[c].pages = new Array();
			var tempPages = mychapt.getElementsByTagName('page');

			//loop through page xml nodes
			for (var p=0; p<tempPages.length; p++) {
				
				// create each page object to be stored in the array
				this.chapters[c].pages[p] = new Page();
				
				// fill the page object
				var mypg = tempPages[p];
				this.chapters[c].pages[p].titl = this.nodeData(mypg, 'title');
				this.chapters[c].pages[p].addr = this.nodeData(mypg, 'url');
				this.chapters[c].pages[p].descr = this.nodeData(mypg, 'description');
				this.chapters[c].pages[p].targ = this.nodeData(mypg, 'target');
			}
		}
		
		
		for (var pr=0; pr<tempProgs.length;pr++) {
			// create each program object to be stored in the array
			this.programs[pr] = new Chapter();
			// fill the chapter object
			var myprog = tempProgs[pr];
			this.programs[pr].titl = this.nodeData(myprog, 'title');

			// create collection of pages
			this.programs[pr].pages = new Array();
			var tempPages = myprog.getElementsByTagName('page');
			//loop through page xml nodes
			for (var pg=0; pg<tempPages.length; pg++) {
				
				// create each page object to be stored in the array
				this.programs[pr].pages[pg] = new Page();
				
				// fill the page object
				var mypg = tempPages[p];
				this.programs[pr].pages[pg].titl = this.nodeData(mypg, 'title');
				this.programs[pr].pages[pg].addr = this.nodeData(mypg, 'url');
				this.programs[pr].pages[pg].descr = this.nodeData(mypg, 'description');
				this.programs[pr].pages[pg].targ = this.nodeData(mypg, 'target');
			}
			
		}
		
	};

	if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) {
				self.parseData();
			}
		};
	} else if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = function () {
			self.parseData();
		};
	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load('includes/xml/ssnav.xml');
			

			
	//create collection of chapters
	this.chapters = new Array();
	this.programs = new Array();
	var tempChapts = xmlDoc.getElementsByTagName('chapter');
	var tempProgs = xmlDoc.getElementsByTagName('program');
	var urls = xmlDoc.getElementsByTagName('url');
	var targs = xmlDoc.getElementsByTagName('target');
	this.mainURL = '';
	this.mainTarget = '';
	
	this.nodeData = function(theparent, nodename) {
		var output;
		try {
			output = theparent.getElementsByTagName(nodename)[0].firstChild.nodeValue;
		} catch(err) {
			output = '';
		}
		return output;
	};
	
	this.findChapter = function() {
		var chapIndex = -1;
		for (var c=0; c<this.chapters.length;c++) {
			if (((currentChapter.titl != '') && (currentChapter.titl != undefined)) && (this.chapters[c].titl == currentChapter.titl)) {
				chapIndex = c;
				break;
			}
		}		
		return chapIndex;
	};
	this.findProgram = function() {
		var progIndex = -1;
		for (var pr=0; pr<this.programs.length;pr++) {
			if (((currentChapter.titl != '') && (currentChapter.titl != undefined)) && (this.programs[pr].titl == currentChapter.titl)) {
				progIndex = pr;
				break;
			}
		}		
		return progIndex;
	};
	
	this.findPage = function(chapIndex) {
		var pageIndex = -1;
		for (var p=0; p<this.chapters[chapIndex].pages.length; p++) {
			if (this.chapters[chapIndex].pages[p].titl == currentPage.titl) {
				pageIndex = p;
				break;
			}
		}
		return pageIndex;
	};
	this.findProgPage = function(progIndex) {
		var pageIndex = -1;
		for (var p=0; p<this.programs[progIndex].pages.length; p++) {
			if (this.programs[progIndex].pages[p].titl == currentPage.titl) {
				pageIndex = p;
				break;
			}
		}
		return pageIndex;
	};
	
	this.getNext = function(chapName,pageName) {
		var nextPageURL = '';
		var nextPageTarg = '';
//		this.debug('getNext - chapName = '+chapName.toUpperCase());
//		this.debug('getNext - pageName = '+pageName.toUpperCase());
		if((chapName.toUpperCase() == 'NONE') && pageName.toUpperCase() == 'WELCOME') {
			nextPageURL = this.chapters[0].addr;
			nextPageTarg = this.chapters[0].targ;
		} else {
			currentChapter.titl = chapName;
			currentPage.titl = pageName;
			
			var displayedChapter = new Chapter();
			var displayedPage = new Page();
			
			var nextChapter = new Chapter();
			var nextPage = new Page();
			
			var isProgram = false;
			// find the chapter data of currently displayed page
			var currChapindex = this.findChapter();
			if (currChapindex == -1) {
				var currProgIndex = this.findProgram();
				if (currProgIndex != -1) {
					//begin nav for programs
					var currPageIndex = this.findProgPage(currProgIndex);
					this.debug("this.programs["+currProgIndex+"].pages["+currPageIndex+"].titl = " + this.programs[currProgIndex].pages[currPageIndex].titl);
					if (currPageIndex == -1) {
	//					this.debug('page was not found in designated program');
							nextPageURL = this.chapters[0].addr;
							nextPageTarg = this.chapters[0].targ;
					}
				} else {
					// chapter of currently displayed page not found, so use main page
					nextPageURL = this.mainURL;
					nextPageTarg = this.mainTarget;
//					this.debug('chapter of currently displayed page not found, so use main page');
				}
			} else {
				displayedChapter = this.chapters[currChapindex];
				// chapter was found, so find currently displayed page data
				var currPageIndex = this.findPage(currChapindex);
				
				if (currPageIndex == -1) {
//					this.debug('page was not found in designated chapter');
					displayedPage = this.chapters[currChapindex].pages[currPageIndex];
					if (this.chapters[currChapindex].pages.length == 0) {
						if ((currChapindex+1) < this.chapters.length) {
							//page was not found in designated chapter, so use main page of next chapter
							nextPageURL = this.chapters[currChapindex+1].addr;
							nextPageTarg = this.chapters[currChapindex+1].targ;
//							this.debug('page was not found in designated chapter, so use main page of next chapter');
						} else {
							// designated chapter is last of all chapters, so use main page of all chapters
							nextPageURL = this.mainURL;
							nextPageTarg = this.mainTarget;
//							this.debug('designated chapter is last of all chapters, so use main page of all chapters');
						}
					} else {
						// page was not found in designated chapter, so use first page within designated chapter
						nextPageURL = this.chapters[currChapindex].pages[0].addr;
						nextPageTarg = this.chapters[currChapindex].pages[0].targ;
//						this.debug('page was not found in designated chapter, so use first page within designated chapter');
					}
				} else {
					// page was found
					if ((currPageIndex+1) < this.chapters[currChapindex].pages.length) {
						// page is not last in designated chapter, so use the next page of designated chapter
						nextPageURL = this.chapters[currChapindex].pages[currPageIndex+1].addr;
						nextPageTarg = this.chapters[currChapindex].pages[currPageIndex+1].targ;
//						this.debug('page is not last in designated chapter, so use the next page of designated chapter');
					} else {
						// page is last of designated chapter
					if ((currChapindex+1) < this.chapters.length) {
							// designated chapter is not last of all chapters, so use the main page of next chapter
							nextPageURL = this.chapters[currChapindex+1].addr;
							nextPageTarg = this.chapters[currChapindex+1].targ;
//							this.debug('designated chapter is not last of all chapters, so use the main page of next chapter');
						} else {
							// designated chapter is last of all chapters, so use main page of all chapters
							nextPageURL = this.mainURL;
							nextPageTarg = this.mainTarget;
//							this.debug('designated chapter is last of all chapters, so use main page of all chapters');
						}
					}
				}
			}
		}
		if ((nextPageURL != undefined) && (nextPageURL != '')) {
//			this.debug(nextPageURL);
//			this.debug(nextPageTarg);
			navtoPage(nextPageURL, nextPageTarg)
		}
	};

	this.getPrev = function(chapName,pageName) {
		var prevPageURL = '';
		var prevPageTarg = '';

		if((chapName.toUpperCase() == 'NONE') && pageName.toUpperCase() == 'WELCOME') {
			prevPageURL = this.mainURL;
			prevPageTarg = this.mainTarget;
		} else {
			currentChapter.titl = chapName;
			currentPage.titl = pageName;
			
			var displayedChapter = new Chapter();
			var displayedPage = new Page();
			
			var prevChapter = new Chapter();
			var prevPage = new Page();
			
			// find the chapter data of currently displayed page
			var currChapindex = this.findChapter();
			if (currChapindex != -1) {
				displayedChapter = this.chapters[currChapindex];
				// chapter was found, so find currently displayed page data
//				this.debug('chapter found');
				if (pageName.toUpperCase() == 'DEFAULT') {
					if ((currChapindex) > 0) {
						
						if (this.chapters[currChapindex-1].pages.length == 0) {
							//no pages in prev chapt so use its main page
							prevPageURL = this.chapters[currChapindex-1].addr;
							prevPageTarg = this.chapters[currChapindex-1].targ;
//							this.debug('default page of designated chapter, and no pages in prev chapt so use its main page');
						} else {
							// default page of designated chapter, so use last page of previous chapter
							prevPageURL = this.chapters[currChapindex-1].pages[this.chapters[currChapindex-1].pages.length-1].addr;
							prevPageTarg = this.chapters[currChapindex-1].pages[this.chapters[currChapindex-1].pages.length-1].targ;
//							this.debug('default page of designated chapter, so use last page of previous chapter');
						}
					} else {
						// default page of first chapter, so use absolute main page
						prevPageURL = this.mainURL;
						prevPageTarg = this.mainTarget;
//						this.debug('default page of first chapter, so use absolute main page');
					}
				} else {
					var currPageIndex = this.findPage(currChapindex);
					if (currPageIndex == -1) {
						displayedPage = this.chapters[currChapindex].pages[currPageIndex];
						// page was not found in designated chapter, so use main page of designated chapter
						prevPageURL = this.chapters[currChapindex].addr;
						prevPageTarg = this.chapters[currChapindex].targ;
//						this.debug('page was not found in designated chapter, so use main page of designated chapter');
					} else {
						// page was found
						if ((currPageIndex) > 0) {
							// page is not first in designated chapter, so use the previous page of designated chapter
							prevPageURL = this.chapters[currChapindex].pages[currPageIndex-1].addr;
							prevPageTarg = this.chapters[currChapindex].pages[currPageIndex-1].targ;
//							this.debug('page is not first in designated chapter, so use the previous page of designated chapter');
						} else {
							// page is first of designated chapter
							prevPageURL = this.chapters[currChapindex].addr;
							prevPageTarg = this.chapters[currChapindex].targ;
//							this.debug('page is first of designated chapter');
						}
					}
				}
			} else {
				// chapter of currently displayed page not found, so use absolute main page
				prevPageURL = this.mainURL;
				prevPageTarg = this.mainTarget;
//				this.debug('chapter of currently displayed page not found, so use main page');
			}
		}
		if ((prevPageURL != undefined) && (prevPageURL != '')) {
		//	window.location.href = prevPageURL;
//			this.debug(prevPageURL);
//			this.debug(prevPageTarg);
			navtoPage(prevPageURL, prevPageTarg)
		}
	};
	this.debug = function(msg) {
		if (debugMode) {
			alert(msg);
		}
	}
}

/* ***********************
The functions below are required for the financial aid page
************************** */

function showDescription(div_id){
		if(document.getElementById(div_id).className=='coursedescriptionShow'){
			document.getElementById(div_id).className='coursedescriptionHide';
		}else{
			document.getElementById(div_id).className='coursedescriptionShow';
		}
}
function showAllDescriptions(course_total){
		for (i = 1; i <= course_total; i++) {
			document.getElementById('course' + i).className='coursedescriptionShow';
			document.getElementById('courseclose' + i).style.display='inline';
		}
}
function hideAllDescriptions(course_total){
		for (i = 1; i <= course_total; i++) {
			document.getElementById('course' + i).className='coursedescriptionHide';
			document.getElementById('courseclose' + i).style.display='none';
		}
}

/* ***********************
Open new window
************************** */
function openCuricWindow(theUrl,theWindowName){
	window.open(theUrl,theWindowName,"width=705,height=532,resizable,scrollbars=yes,status=1");
}

/* ***********************
Track the application button
************************** */
function trackAppBTN(theUrl,targ){
	s.pageName="AIO ADA Tools: Tour: Next Steps: Apply Now Button";
	s.channel="AIO ADA Tools";
	s.prop1="Tour";
	s.prop2="Next Steps";
	s.prop3="Apply Now Button";
	s.eVar7="Tour";
	s.events="event11";
	s.hier1="AIO ADA Tools|Tour|Next Steps|Apply Now Button";
	var s_code=s.t();
	if(s_code)document.write(s_code);
	if (targ == 'tour_content_frame') {
		jQuery('#'+targ).load(theUrl);
	} else {
		window.open(theUrl,targ);
	}
}
