
	// Author: Christopher P. Bradford
	// Email: bradfordcp@gmail.com
	// Last Updated: 1/9/2007
	// Changelog:
	//	<11/02/2006> Created
	//	<11/16/2006> Added documentation and reformatted for easier viewing.
	//	<11/16/2006> Added error control to loadXMLDoc(). It now warns a user if certain javascript functions are disabled.
	//	<12/18/2006> Fixed error in setActiveStyleSheet(), added null-object error control
	//	<01/09/2007> Rewrote bread-crumb generator to stop at last available title section.
	//  <07/12/2007> SE Teague rector added js include script and journal switcher
	
	// Function: include file
	// Helps to include other js files so we don't need to put them in the header of all html pages
	/*function include(file) {
	  var script  = document.createElement('script');
  	  script.src  = file;
  	  script.type = 'text/javascript';
  	  script.defer = true;

  	   document.getElementsByTagName('head').item(0).appendChild(script);
	}
	include('/parse/yui/yahoo/yahoo.js');
	include('/parse/yui/event/event.js');
	include('/parse/yui/dom/dom.js');
	include('/parse/yui/container/container_core.js');
	include('/parse/yui/element/element-beta.js');
	include('/parse/yui/tabview/tabview.js');
		include('/parse/yui/menu/menu.js');
	include('/parse/javascript/yui.js');
	*/
	// Function: getSlashes
	// Input: The URL to be parsed
	// Output: An array containing the position of all slashes in the URL in order of appearence.
	function getSlashes(URL){
		var slashes = 0;
		var loc = new Array();
		
		for(var i=0;URL.indexOf("/", i+1)!=-1; i = URL.indexOf("/", i+1)){
			loc[slashes] = URL.indexOf("/", i+1);
			slashes++;
		}	
		return 	loc;
	}				

	// Function: getPipes
	// Input: The title to be parsed
	// Output: An array containing the position of all pipes in the title in order of appearence.
	function getPipes(title){
		var pipes = 0;
		var loc = new Array();
		
		for(var i=0;title.indexOf("|", i+1)!=-1; i = title.indexOf("|", i+1)){
			loc[pipes] = title.indexOf("|", i+1);
			pipes++;
		}	
		return loc;
	}

	// Function: getURLSections
	// Input: The URL to be parsed
	// Output: An array containing the sections of the URL.
	function getURLSections(URL){
		var loc = new Array();
		var sections = new Array();
		
		loc = getSlashes(URL);
		
		for(i=1;i<loc.length-1;i++){
			sections[i-1] = URL.substr(loc[i]+1, loc[i+1]-loc[i]);
		}
		
		if(URL.substr(loc[loc.length-1]+1) == ""){
			sections[i-1] = "index.html";
		} else {
			sections[i-1] = URL.substr(loc[loc.length-1]+1);
		}
		
		return sections;
	}

	// Function: getTitleSections
	// Input: The title to be parsed
	// Output: An array containing the sections of the title.
	function getTitleSections(title){
		var loc = new Array();
		var sections = new Array();
		
		loc = getPipes(title);
		
		sections[0] = title.substr(0, loc[0]-1);
		for(i=1;i<loc.length;i++){
			sections[i] = title.substr(loc[i-1]+2, loc[i]-loc[i-1]-2);
		}
		sections[i] = title.substr(loc[loc.length-1]+2);

		return sections;
	}

	// Function: getBreadCrumbs
	// Input: The URL and title to be parsed
	// Output: A string containing the bread crumbs.
	function getBreadCrumbs(URL, title) {
		var URLsections = new Array();
		var titleSections = new Array();	
		var breadcrumb = "";
		var target = "http://";
		
		URLsections = getURLSections(URL);
		titleSections = getTitleSections(title);
		
		if(URLsections[URLsections.length-1] == 'index.html')
			count = URLsections.length-1;
		else
			count = URLsections.length;
		
		/*
		for(i=0;i<count;i++){
			target = target + URLsections[i];
			breadcrumb += '<a href="'+target+'">'+titleSections[i]+'</a> &raquo; ';
		}*/
		
		
		
		if(count > titleSections.length)
			count = titleSections.length;
		
		breadcrumb = "";
		breadcrumb = '<a href="http://www.library.vcu.edu/">VCU Libraries</a> &raquo; ';
		target = "http://www.library.vcu.edu/";
		
		for(i=1;i<count;i++){
			target = target + URLsections[i];
			breadcrumb += '<a href="'+target+'">'+titleSections[i]+'</a> &raquo; ';
		}
		
		/** confusing version that doesn't work right
		breadcrumb = "";
		for(a=count; a>1; a--){
			target = "http://";
			for(i=0; i<URLsections.length-(count-a); i++){
				target = target+URLsections[i];
			}
			breadcrumb = '<a href="'+target+'">'+titleSections[a-1]+'</a> &raquo; ' + breadcrumb;
		}
		
		breadcrumb = '<a href="http://www.library.vcu.edu/">VCU Libraries</a> &raquo; ' + breadcrumb;
		**/
		
		return breadcrumb;
	}
	
	// Function: loadXMLDoc
	// Input: URL of the XML document
	// Output: The contents of the XML document.
	function loadXMLDoc(url){
		var request = null;
		if(window.XMLHttpRequest){
			request = new XMLHttpRequest();
		} else if(window.ActiveXObject){
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}		
		if(request == null){
			alert("Your browser does not support all javascript functions on this site.");
			return;
		}else{
			request.open("GET", url, false);
	    	request.send(null);
   		 	return request.responseXML;
		}
	}		
	
	// Function: getNews
	// Input: The number of items it should return
	// Output: The number of items requested individually encased in li's
/*	function getNews(MAX_ITEMS){
		var newsDoc = loadXMLDoc("/parse/javascript/news.xml");
		var items = new Array();
		var picks = new Array();
		var target = null;
		var text = null;
		var line = "";
		var found = null;
		var begtag = null;
		var endtag = null;
		var fintag = null;
		picks[0] = 0;
		
		items = newsDoc.getElementsByTagName('item');
		if(items.item(0) != null){
			for(i=1; i<MAX_ITEMS; i++){
				found = false;
				while(!found){
					picks[i] = Math.floor(Math.random()*100);
					if(picks[i] < items.length){
						for(j=0; j<picks.length; j++){
							if(picks[i] == picks[j] && i!=j){
								found = false;
								break;
							}  else {
								found = true;
							}
						}
					}
				}
			 }
			begtag = "";
			for(i=0; i<MAX_ITEMS; i++){
				target = items.item(picks[i]).getElementsByTagName('link').item(0).childNodes[0].nodeValue;
				text = items.item(picks[i]).getElementsByTagName('title').item(0).childNodes[0].nodeValue;
				line += '<a href="'+target+'">&raquo; '+text+'</a>';
			}
			endtag = "</ul>";
			fintag = begtag+line+endtag;
			document.write(fintag);
		}
	}*/
           
	
	
	// Function: getDynamicContent
	// Input: The type of item being requested
	// Output: The correct content based on target from the XML document
	function getDynamicContent(target){
		var xmlDoc = loadXMLDoc("/parse/randomContent.xml");
		var set= null;
		var items = null;
		var targets = { ImageLine : 0, DidYouKnow : 1, Spotlight : 2, Resource : 3}
		
		
		switch(target){
			case 'ImageLine':
				set = xmlDoc.getElementsByTagName('set')[targets.ImageLine].getElementsByTagName('option');
				break;
			case 'DidYouKnow': 
				set = xmlDoc.getElementsByTagName('set')[targets.DidYouKnow].getElementsByTagName('option');
				break;
			case 'Spotlight':
				set = xmlDoc.getElementsByTagName('set')[targets.Spotlight].getElementsByTagName('option');
				break;
			case 'Resource':
				set = xmlDoc.getElementsByTagName('set')[targets.Resource].getElementsByTagName('option');
				break;
		}
		
		items = getItems(set);
		
		switch(target){
			case 'ImageLine':
				getDynamicImage(items);
				break;
			case 'DidYouKnow': 
				getDynamicText(items);
				break;
			case 'Spotlight':
				document.write('<dl><dt>');				
				getDynamicImage(items);
				document.write('</dt><dd>');
				getDynamicText(items);
				document.write('</dd></dl>');
				break;
			case 'Resource':
				getDynamicImage(items);
				getDynamicText(items);
				break;
		}

}
	
	// Function: getDynamicText
	// Input: An array of items from the randomContent.xml file.
	// Output: Writes the specified content, reutrns the fact used.
	function getDynamicText(items){
		for(i=0; i<items.length; i++){
			if(items[i].getAttribute('type') == 'text')
				document.write(items[i].childNodes[0].nodeValue);
		}
	}
	
	// Function: getDynamicImage
	// Input: An array of items from the randomContent.xml file.
	// Output: Writes the source for an image.
	function getDynamicImage(items){
		for(i=0; i<items.length; i++){
			if(items[i].getAttribute('type') == 'image')
				document.write(items[i].childNodes[0].nodeValue);
		}
	}
	
	// Function: getItems
	// Input: A set from randomContent.xml
	// Output: An array of items from the chosen set.
	function getItems(set){
		var found = false;
		var items = null;
		var fact = null;
		
		while(!found){
			fact = Math.floor(Math.random()*100);
			if(fact < set.length && fact >= 0)
				found = true;
		}
		
		items = set[fact].getElementsByTagName('item');
		return items;
	}
	
	// Function: openWin
	// Input: The url the window should contain
	// Output: A new window is opened with the specified content
	function openWin(url) {
    	window.open(url, '', 'width=850,height=400,directories=yes,menubar=yes,location=yes,left=75,toolbar=yes,scrollbars=yes,resizable=yes,status=yes,top=100');
	}
	
	// Function: setActiveStyleSheet
	// Input: The title of the style sheet to switch to...
	// Output: None, but the active stylesheet is changed.
	function setActiveStyleSheet(titles) {
		if(titles != null){
			var names = titles.split(',');
			var i, sheets, b;
			for(i=0; (sheets = document.getElementsByTagName("link")[i]); i++) {
				if(sheets.getAttribute("rel").indexOf("style") != -1 && sheets.getAttribute("title") && sheets.getAttribute("rel").	indexOf("alt") != -1	) {
					sheets.disabled = true;
					for(b=0; b < names.length; b++) {
						if(sheets.getAttribute("title") == names[b] ) {
							sheets.disabled = false;
						}
					}
				}
			}
		}
	}
	
	// Function: getActiveStyleSheet
	// Input: None!
	// Output: Returns the active stylesheets delimited by commas
	function getActiveStyleSheet() {
	  var i, sheets, titles;
	  for(i=0; (sheets = document.getElementsByTagName("link")[i]); i++) {
	    if(sheets.getAttribute("rel").indexOf("style") != -1 && sheets.getAttribute("title") && !sheets.disabled) {
			titles += sheets.getAttribute("title") + ",";
		}
	  }
	  return titles;
	}
	
	// Function: getPreferredStyleSheet
	// Input: None!
	// Output: The preferred stylesheets or NULL
	function getPreferredStyleSheet() {
	  var i, sheets;
	  for(i=0; (sheets = document.getElementsByTagName("link")[i]); i++) {
	    if(sheets.getAttribute("rel").indexOf("style") != -1
	       && sheets.getAttribute("rel").indexOf("alt") == -1
	       && sheets.getAttribute("title")
	       ) return sheets.getAttribute("title");
	  }
	  return null;
	}
	
	// Function: createCookie
	// Input: Name of the cookie, Value going into the cookie, Number of days until the cookie expires
	// Output: None!, but a new cookie is created.
	function createCookie(name,value,days) {
	  if (days) {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
	  }
	  else expires = "";
	  document.cookie = name+"="+value+expires+"; path=/";
	}
	
	// Function: readCookie
	// Input: The name of the cookie to read
	// Output: The contents of the cookie or NULL
	function readCookie(name) {
	  var nameEQ = name + "=";
	  var ca = document.cookie.split(';');
	  for(var i=0;i < ca.length;i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0){
			var returnVal = c.substring(nameEQ.length,c.length);
			if(returnVal.indexOf("undefined") != -1)
				returnVal = "undefined," + returnVal.substr(9);
			return returnVal;
		}
	  }
	  return null;
	}
	
	// Create the onLoad event handler
	window.onload = function(e) {
	  var cookie = readCookie("style");
	  var title = cookie ? cookie : getPreferredStyleSheet();
	  setActiveStyleSheet(title);
	}
	
	// Create the onUnload event handler
	window.onunload = function(e) { 	
	  var title = getActiveStyleSheet();
	  createCookie("style", title, 365);
	}
	
	
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
	
	
	/* Created by SERector 01/16/2007. 
	Updated 2009
	this is used for the Journal Finder search on the Find Information page */
	 function journalSwitch() {
	  	var x = document.getElementById("journalsForm");
		for (i=0; i<x.quicksearch_type.length;i++) {
			if (x.quicksearch_type[i].checked) {
				user_input = x.quicksearch_type[i].value;
				searchTerm = x.search.value;
				if (user_input=="Journals") {
					x.action="http://www.library.vcu.edu/phpapps/webscripts/quicksearch.php?quicksearch_type="+user_input+"&search="+searchTerm;
					//x.submit();
				} else {
					x.action="http://www.library.vcu.edu/phpapps/webscripts/catalog.php?stype=browse&scan_code=TRT&search="+searchTerm+"&base=periodicals";
					}
			}
		}
	}
	
		/* This is going to different places intentionally; Title begins with searches are not being logged */
	 function catalogSwitch() {
	  	var x = document.getElementById("catalogSearch");
		for (i=0; i<x.stype2.length;i++) {
			if (x.stype2[i].checked) {
				user_input = x.stype2[i].value;
				searchTerm = x.search.value;
				if (user_input=="Anywhere") {
					x.action="http://www.library.vcu.edu/phpapps/webscripts/quicksearch.php";
				} else {
					x.action="http://www.library.vcu.edu/phpapps/webscripts/catalog.php?stype=browse&scan_code=trt&search="+searchTerm;
					}
			}
		}
	}
