﻿<!--

var dbase = new Array();
var clonetimediv;
var clonealphadiv;
var curdiv;	//this is the div that is currently displayed on screen

var encldivnm = "bigdiv";

var checkedON = false; //this should be set by the cookie or defaults to off; if TRUE, it means to display checked items in their own div at the top

// the following code checks to see if the browser supports textcontent or if you have to use innertext instead
var hasInnerText;


function readdlisteners(outerdiv) {
	var panarr = getArraybyClass(outerdiv, "paneldiv");
	for (var i=0;i<panarr.length;i++) {
		panarr[i] = getUniquebyClass(panarr[i],"paneltit");
		if (window.addEventListener) {
			panarr[i].addEventListener("click", panelclick, true);
		}
		else if (window.attachEvent) {
			var temhtml = panarr[i].parentNode.innerHTML;
			var j=temhtml.indexOf("paneltit");
			if (j != -1) {
				var newtem = temhtml.substring(0,j+9)+" onclick="+'"panel' +'click()"'+temhtml.substring(j+9,temhtml.length);
				panarr[i].parentNode.innerHTML = newtem;
			};
		}
		else if (document.getElementById) {
			panarr[i].onclick = panelclick;
		};
	};
};



function initialize() {

/*
dbase is an array of all of the node ids plus some data about each node, used for sorting etc.
*/

/* new strategy: make two divs, neither in the document itself, one for alpha and one for time; when the user switches, clone the clone then use it to replace encldiv
make sure to keep track of the variable used to point to the active (currently displayed) html
*/

/* idx ends up being an array of arrays; each column represents a particular sorttype; each row represents the id number (ordinal number at load time) of the element that goes in that place for that particular sort; for example, idx[5][2] contains a whole number that identifies the html element at load time that goes sixth (5+1) in the third (2+1) sort type */

	var alltags=document.all? document.all : document.getElementsByTagName("*");
	var encldiv = document.getElementById(encldivnm);	//get a pointer to the enclosing div for all the data
	curdiv = encldiv;
	encldiv.normalize();
	
	// first, strip the encldiv of everything that isn't within an internal div (no comments, no tabs, no extra text, etc.)
	var testname = "";
	for (i=encldiv.childNodes.length-1; i>=0; i--) {
		testname = encldiv.childNodes[i].tagName ? encldiv.childNodes[i].tagName : "nodiv";
		if (testname.toLowerCase()!="div") {encldiv.removeChild(encldiv.childNodes[i])};
	};
	
	//next, clone the main enclosing div (ordered by default by time) to keep a permanent copy of it
	clonetimediv = encldiv.cloneNode(true);
	// (restoring the starting point is easy, just replace the encldiv as a child in its parent with the mainhtml clone)
	
	// because Safari doesn't copy added event listeners when it clones, this has to be done manually:
//	if (BrowserDetect.browser == "Safari") {readdlisteners(clonetimediv)};
		
	
	//next, create a new element that will be a div (clone) to hold all of the speaker and chair divs for the alpha sort; initially this div will have the elements in the wrong order
	
	var alldivsarray = new Array();
	alldivsarray = encldiv.getElementsByTagName("DIV");
	// make a new temparray from temparr containing only clones of those divs that are speaker or chair divs
	// since they are clones, we can modify them in the next few steps
	var alphadivsarray = new Array();
	var j=0;
	var divclass = "";
	for (var i=0; i<alldivsarray.length; i++) {
		divclass = alldivsarray[i].className;
		if (divclass=="speakerdiv" || divclass=="chairdiv") {
			alphadivsarray[j++]=alldivsarray[i].cloneNode(true);
		};
	};
	
	// make another temparray of lastnames and index numbers into the alphadivsarray

		function striptag(str) {
			var i1 = str.indexOf("<");
			var i2 = str.indexOf(">");
			var itest = str.indexOf("<",i1+1);
			if (itest!=-1 && itest<i2) {str = str.substring(0,i1+1)+striptag(str.substring(i1+1))}
			i1 = str.indexOf("<");	//must recalculate because values may change when string changes in previous line
			i2 = str.indexOf(">");
			return str.substring(0,i1)+str.substring(i2+1,str.length);
		};

		function lastnamefirst(fnl) {	//needed a few lines down
			// move last name to beginning
			if (fnl.indexOf("Sha Xin") != 0) {
				linex = fnl.lastIndexOf(" ");
				fnl = fnl.substring(linex+1,fnl.length) + " " + fnl.substring(0,linex);
			};
			return fnl;
		};
		
	var ordarray = new Array(alphadivsarray.length);
	var lname="";
	var namediv;
	var curlength = ordarray.length;
	for (var i = 0; i<curlength; i++) {
		namediv = getUniquebyClass(alphadivsarray[i],"persname");
		lname = namediv.innerHTML;
		if (lname.indexOf("Respondent") == 0) {lname += ", Respondent"};
	 	//might as well strip the "Chair:" and "Speaker:" and "Respondent: " tags while we're in here
		lname = lname.substring(lname.indexOf(":")+2);
		// need to add in the panel titles after the chair names, and add "Respondent" in parens after respondent names
		if (alphadivsarray[i].className == "chairdiv") {
			panelnm = alphadivsarray[i].getAttribute("id");
			panelnm = "idt" + panelnm.substring(3,panelnm.length-1);
			paneldiv = document.getElementById(panelnm);
			panelnm = paneldiv.innerHTML;
			panelnm = panelnm.substring(panelnm.indexOf("|")+2,panelnm.length);
			lname = lname + ", Chair, " + panelnm;
			};
		//also need to add code to put checkboxes in to each persname (going to use innerHTML instead of cloning a new input element because of the event model confusion
		namediv.innerHTML = "<input type=\"checkbox\" onclick=\"do_speakcheck(event, this)\">"+lname;
		var ltem = "";
		ltem = (hasInnerText) ? namediv.innerText : namediv.textContent;  //make it a literal (doesn't work in Safari)
		if (ltem != "") {lname = ltem};
		// next strip out any tags
		while (lname.indexOf("<")!=-1 && lname.indexOf(">")!=-1) {
			lname = striptag(lname);
		};
		// next strip the comma and after in the names with titles
		lnindex = lname.indexOf(",");
		if (lnindex != -1) {lname = lname.substring(0,lname.indexOf(","))};
		//deal with cases with two authors
		if (lname.indexOf("&") != -1) {
			lnam2 = lname.substring(lname.lastIndexOf("&")+2);
			lname = lname.substring(0,lname.lastIndexOf("&")-1);
			lnam2 = lastnamefirst(lnam2);
			//now create a new record in ordarray (will it have to point to a new record in alphadivsarray?)
			newidx = alphadivsarray.length;
			ordarray[ordarray.length] = lnam2 +":::"+newidx;
			alphadivsarray[newidx] = alphadivsarray[i].cloneNode(true);
			cloneddiv = alphadivsarray[newidx];
			// now I need to fix the id num in the html of the cloned record to make it unique
			cloneddiv.setAttribute("id","xx"+cloneddiv.getAttribute("id").substring(2));
			// plus change the references in the hide/reveal ids to something unique
			clonedabstr = getUniquebyClass(cloneddiv, "abstracttxt");
			if (clonedabstr.id.length>0) {
				oldidn = clonedabstr.id;
				clonedabstr.setAttribute("id","xx"+oldidn.substring(2));
				aychtee = cloneddiv.innerHTML;
				fixspot = aychtee.indexOf(oldidn);
				if (aychtee[fixspot-1]!="x" && fixspot > 0) {cloneddiv.innerHTML = aychtee.substring(0,fixspot)+"xx"+aychtee.substring(fixspot+2,aychtee.length)};			
			};
		};
		lname = lastnamefirst(lname);
			
		// finally, add the index number for the record in alphadivsarray so it can be recovered after the sort
		lname += ":::"+i;
		ordarray[i]=lname;
		//also, add the panel title to the alphadivs of panel chairs
	};

	//having created the array, now it must be sorted (by alpha)
	ordarray.sort();
	
	// create a new element that contains the alphadivs but in order
	clonealphadiv = document.createElement("div");
	clonealphadiv.setAttribute("id","bigdiv");
	var alphatitle = document.createElement("div");
	alphatitle.setAttribute("class","sectitle");
	clonealphadiv.style.display = "block";
	alphatitle.className = "sectitle";
	alphatitle.innerHTML = "Panel Participants";
	clonealphadiv.appendChild(alphatitle);
	for (i=0; i<ordarray.length; i++) {
		lname = ordarray[i];
		lindex = lname.indexOf(":::")+3;
		lname = lname.substring(lindex);
		clonealphadiv.appendChild(alphadivsarray[ parseInt( lname ) ]);
	};
	
	return;
};




//need a routine that takes an array A and a sort order of indexes into A and sorts A (actually this will only happen in the initial sort)

function on_usersort(sorttype) {
	//basic strategy is to clone the appropriate clone and then replace the curdiv
	var newclone = sorttype ? clonealphadiv.cloneNode(true) : clonetimediv.cloneNode(true);
	curdiv.parentNode.replaceChild(newclone,curdiv);
	curdiv = newclone;
	if (!sorttype) {addlisteners()};
	var alltags=document.all? document.all : document.getElementsByTagName("*");
	ccollect=getElementbyClass(alltags, "abstracttxt");
	revivecontent(false);
	revivechecked( (sorttype) ? "alpha" : "time");
	document.getElementById("container").focus();
};

	
function checkedtotop(temparray) {   // this subroutine goes through an array of indexes in order and sorts the checkboxed ones to the top, shifting the others ahead


	var numchecked = 0;
	for (var i = 0; i<sortables.length; i++) {
		if (checkboxbool[temparray[i]] && i>numchecked) {
			var movingidx = temparray[i];
			for (var j = i; j > numchecked; j--) {
				temparray[j] = temparray[j-1];
			};
			temparray[numchecked++] = movingidx;
		};
	};
};

// ?? HTML Section: Below here is html specific code. Above here is sorting logic.

function getElementbyClass(rootobj, classname) { //getElementbyClass goes through an array of objects and returns an array of just those meeting the class definition
	var temparray=new Array()
	var inc=0
	var rootlength=rootobj.length
//	document.write(rootlength+"=rootlength <br />");	//debug line
	for (i=0; i<rootlength; i++){
		if (rootobj[i].className==classname) {
			temparray[inc++]=rootobj[i];
//			document.write(temparray[inc-1]);	//debug line
		};
	}
	return temparray;
};

function getUniquebyClass(rootobj, classname) {
	vartemarray = new Array();
	temarray = rootobj.getElementsByTagName("*");
	for (var i = 0; i < temarray.length; i++) {
		if (temarray[i].className == classname) {return temarray[i]};
	};
	return -1;
};

// floating banner doesn't work in Safari, please fix

function do_onsubmit1(formobj) {
	var obj = formobj.elements["usortcrit"];
	var scrit = parseInt(obj.value);
	on_usersort(scrit);
	return false;
};


/* if (enablepersist=="on" && document.getElementById) {
window.onunload=saveswitchstate; }; */	//don't do this until cookie system is turned on
//-->