// New stuff
function changeSize()
{
	if($("leftCol").offsetHeight > $("rightCol").offsetHeight)
	{
		$("rightCol").style.height = ($("leftCol").offsetHeight) + "px";
	}
	if($("rightCol").offsetHeight > $("leftCol").offsetHeight)
	{
		$("leftCol").style.height = ($("rightCol").offsetHeight - 60) + "px";
	}
}

// Global Vars
var curOn;
var dObj = new Object;

// Maing page stuff
function bullOn(who)
{
	document.getElementById("bull"+who).src = "../images/box_bullet_on.gif";
}
function bullOff(who)
{
	document.getElementById("bull"+who).src = "../images/box_bullet.gif";
}
// Check Observer name from feedback page
function checkLogin(r)
{
	if(!r)
	{
		$('loginBtn').value = "Verifying...";
		$('loginBtn').disabled = true;
		var url = "check_login.php";
		var data = "&fn=" + escape(document.form1.fn.value) + "&ln=" + escape(document.form1.ln.value);
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: checkLogin});
	}
	else if(r.responseText == "FAILED")
	{
		$('instructions').innerHTML = "<span style='color:#990000'>Sorry.  There is no record of that supervisor.  Are you sure you entered the same name that you entered on the device?  Please try again.</span>";
		$('loginBtn').value = "Log In";
		$('loginBtn').disabled = false;
	}
	else if(r.responseText > 0)
	{
		$('loginBtn').value = "Logging in...";
		document.form1.superID.value = r.responseText;
		document.form1.submit();
	}
	else
	{          
		alert("Error:\n" + r.responseText);
	}
	return false;
}
// Feedback observation list functions
function hiMe(who)
{
	document.getElementById("r"+who).style.backgroundColor = "#FFFF99";
}
function unHi(who,even)
{
	if(even)
		document.getElementById("r"+who).style.backgroundColor = "#FFFFFF";
	else
		document.getElementById("r"+who).style.backgroundColor = "#EEEEEE";
}
function rowBg(who,col)
{
	document.getElementById("r"+who).style.backgroundColor = col;
}
function showMenu(who)
{
	if(curOn >= 0)
	{
		document.getElementById("f"+curOn).style.display = "none";
	}
	document.getElementById("f"+who).style.display = "block";
	curOn = who;
}
function delObs(who)
{
	if(confirm("WARNING: Permanently delete this observation?"))
	{
		$("delForm").del.value = who;
		$("delForm").submit();
	}
}
function delCff(who)
{
	if(confirm("WARNING: Permanently delete this observation?"))
	{
		$("delForm").del.value = who;
		$("delForm").submit();
	}
}
function nextPage(who,id)
{
	var fw = document.getElementById('framework'+who).value;
	window.location = "results.php?fw="+fw+"&id="+id+"&auth=1";
}
function nextPageDemo(who,id)
{
	var fw = document.getElementById('framework'+who).value;
	window.location = "http://superior.casenex.com/users/results.php?fw="+fw+"&id="+id;
}
// Open demo window
function demo()
{
	url = "../video_player2.php?movie=tpr.flv&mt=TPR Introductory Video";
	win = "_blank";
	attributes = "left=20,top=20,resizable=1,toolbar=0,width=350,height=350";
	window.open(url,win,attributes);	
}
function kp(who)
{
	url = "penn/video_player.php?id="+who;
	win = "kp_player";
	props = "left=20,top=20,width=610,height=385,toolbar=0,resizable=1";
	window.open(url, win, props);
}
// Results functions
function focusMe(who)
{
	for(i=1;i<=3;i++)
	{
		if(i==who)
			document.getElementById("ta"+i).style.backgroundColor = "#FFFFFF";
		else
			blurMe(i);
	}
	needSave();
}
function blurMe(who)
{
	document.getElementById("ta"+who).style.backgroundColor = "#EDEFF3";
}
function needSave()
{
	if(!saving)
	{
		saved = false;
		document.getElementById("saveBtn").disabled = false;
		document.getElementById("saveBtn").value = "Save";
	}
	changed = true;
}
function saveMe()
{
	document.getElementById("saveBtn").value = "Saving...";
	document.getElementById("saveBtn").disabled = true;
	//document.getElementById("div1").innerHTML = document.getElementById("ta1").value.split("\n").join("<br>");
	//document.getElementById("div2").innerHTML = document.getElementById("ta2").value.split("\n").join("<br>");
	//document.getElementById("div3").innerHTML = document.getElementById("ta3").value.split("\n").join("<br>");
	saving = true;
	changed = false;
	updateTable();
}
function addBreaks(str)
{
	str = str.split("\n").join("<br>");
	return str;
}
function doneSaving()
{
	saved = true;
	saving = false;
	if(changed)
	{
		document.getElementById("saveBtn").disabled = false;
		document.getElementById("saveBtn").value = "Save";
	}
	else
	{
		document.getElementById("saveBtn").value = "Saved";
	}
	
}
function checkAdmin()
{
	document.getElementById("saveBtn").value = "Saving...";
	document.getElementById("saveBtn").disabled = true;
	//document.getElementById("div1").innerHTML = document.getElementById("ta1").value.split("\n").join("<br>");
	//document.getElementById("div2").innerHTML = document.getElementById("ta2").value.split("\n").join("<br>");
	//document.getElementById("div3").innerHTML = document.getElementById("ta3").value.split("\n").join("<br>");
	saving = true;
	changed = false;
	updateTable();
}
function editInfo()
{
	for(i=1;i<=9;i++)
	{
		if(i != 3 && i != 1)
		{
			// getting rid of teacher and school for now
			document.getElementById("t"+i).style.display = "none";
			document.getElementById("e"+i).style.display = "block";
		}
	}
	document.getElementById("edit").innerHTML = "[<a href='javascript:saveInfo()'>save</a> | <a href='javascript:cancelInfo()'>cancel</a>]";
}
function cancelInfo()
{
	for(i=1;i<=9;i++)
	{
		document.getElementById("e"+i).style.display = "none";
		document.getElementById("t"+i).style.display = "block";
	}
	for(i=0;i<4;i++)
	{
		document.getElementById(objArray[i]).value = eval(objArray[i]);
	}
	for(i=4;i<objArray.length;i++)
	{
		document.getElementById(objArray[i]).selectedIndex = eval(objArray[i]);
	}
	document.getElementById("edit").innerHTML = "[<a href='javascript:editInfo()'>edit</a>]";
}
function saveInfo(r)
{
	if(!r)
	{
		var data = "";
		document.getElementById("google").style.display = "block";
		document.getElementById("edit").innerHTML = "[Saving...]";
		fname = document.getElementById("fname").value;
		lname = document.getElementById("lname").value;
		dateM = document.getElementById("dateM").selectedIndex;
		dateD = document.getElementById("dateD").selectedIndex;
		dateY = document.getElementById("dateY").selectedIndex;
		school = document.getElementById("school").value;
		students = document.getElementById("students").value;
		subject = document.getElementById("subject").selectedIndex;
		grade = document.getElementById("grade").selectedIndex;
		assignment = document.getElementById("assignment").selectedIndex;
		context = document.getElementById("context").selectedIndex;
		activity = document.getElementById("activity").selectedIndex;
		for(i=0;i<objArray.length;i++)
		{
			document.getElementById(objArray[i]).disabled = true;
			data += "&" + objArray[i] + "=" + escape(eval(objArray[i]));
		}
		data += "&id=" + document.form2.obsID.value;
		var url = "update_contextual.php";
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: saveInfo});
	}
	else if(r.responseText == "SUCCESS")
	{
		for(i=1;i<=9;i++)
		{
			document.getElementById("e"+i).style.display = "none";
			document.getElementById("t"+i).style.display = "block";
		}
		for(i=0;i<objArray.length;i++)
		{
			document.getElementById(objArray[i]).disabled = false;
		}
		document.getElementById("edit").innerHTML = "[<a href='javascript:editInfo()'>edit</a>]";
		document.getElementById("google").style.display = "none";
		fillContextual();
	}
	else
	{
		alert("Error:\n" + r.responseText);
	}
}
function fillContextual()
{
	document.getElementById("dateM").options.selectedIndex = dateM;
	document.getElementById("dateD").options.selectedIndex = dateD;
	document.getElementById("dateY").options.selectedIndex = dateY - 0;
	document.getElementById("subject").options.selectedIndex = subject;
	document.getElementById("grade").options.selectedIndex = grade;
	document.getElementById("assignment").options.selectedIndex = assignment;
	document.getElementById("context").options.selectedIndex = context;
	document.getElementById("activity").options.selectedIndex = activity;
	document.getElementById("t1").innerHTML = lname + ", " + fname;
	document.getElementById("t2").innerHTML = document.getElementById("dateM").value + " " + document.getElementById("dateD").value + ", " + document.getElementById("dateY").value;
	document.getElementById("t3").innerHTML = school;
	document.getElementById("t5").innerHTML = students;
	document.getElementById("t4").innerHTML = document.getElementById("subject").value;
	document.getElementById("t6").innerHTML = document.getElementById("grade").value;
	document.getElementById("t7").innerHTML = document.getElementById("assignment").value;
	document.getElementById("t8").innerHTML = document.getElementById("context").value;
	document.getElementById("t9").innerHTML = document.getElementById("activity").value;
}
function startResults()
{
	fillContextual();
	loopMe();
}
function loopMe()
{
	counter--;
	if(counter <= 0)
	{
		if(!saved && !saving)
		{
			saveMe();
		}
		counter = 60;
	}
	setTimeout("loopMe()", 1000);
}
function updateTable(r)
{
	if(!r)
	{
		var url = "update_obs.php";
		var data = "&txt1=" + escape(document.form2.txt1.value) + "&txt2=" + escape(document.form2.txt2.value) + "&txt3=" + escape(document.form2.txt3.value) + "&id=" + document.form2.obsID.value + "&txt1id=" + document.form2.txt1id.value + "&txt2id=" + document.form2.txt2id.value + "&txt3id=" + document.form2.txt3id.value;
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: updateTable});
	}
	else if(r.responseText == "FAILED")
	{
		alert("Error:\n" + "There was a database error.  Try again");
	}
	else if(r.responseText == "SUCCESS")
	{
		doneSaving();
	}
	else
	{          
		alert("Error:\n" + r.responseText);
	}
	return false;
}
function printResults()
{
	str1 = $("ta1").value.replace(/\n/g, "<br>");
	str2 = $("ta2").value.replace(/\n/g, "<br>");
	str3 = $("ta3").value.replace(/\n/g, "<br>");
	$("print1").innerHTML = str1;
	$("print2").innerHTML = str2;
	$("print3").innerHTML = str3;
	$("print1").style.display = "block";
	$("print2").style.display = "block";
	$("print3").style.display = "block";
	$("ta1").style.display = "none";
	$("ta2").style.display = "none";
	$("ta3").style.display = "none";
	window.print();
}
function shareResults()
{
	if (navigator.appName.indexOf("Netscape")!=-1&&parseInt(navigator.appVersion)>=5)
		$("shareBox").style.MozOpacity=1;
	else if (navigator.appName.indexOf("Microsoft")!=-1&&parseInt(navigator.appVersion)>=4)
		$("shareBox").style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity=100);";
	$("emailBtn").value = "Send";
	$("emailBtn").disabled = false;
	$("shareBox").style.display = "block";	
	posArray = findPos($("iconRow"));
	$("shareBox").style.left = (posArray[0]) + "px";
	$("shareBox").style.top = (posArray[1] + 20) + "px";
	$("shareURL").value = window.location.href.slice(0,-7);
	$("shareURL").focus();
	$("shareURL").select();
}
function emailResults()
{
	$("emailBtn").value = "Sending...";
	$("emailBtn").disabled = true;
	emailTxt = $("emails").value;
	emailTxt = emailTxt.split(" ").join("");
	sharePerson = $("shareName").value;
	shareTeachr = $("shareTeach").value;
	str = window.location.href;
	lastSeven = str.slice(-7);
	if(lastSeven == "&auth=1")
		shareLink = str.slice(0,-7);
	var url = "email_results.php";
	var data = "&emails=" + escape(emailTxt) + "&shareName=" + escape(sharePerson) + "&shareTeach=" + escape(shareTeachr) + "&shareLink=" + escape(shareLink);
	var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: doneEmailResults});
}
function doneEmailResults(r)
{
	if(r.responseText == "SUCCESS")
	{
		fadeObj("shareBox");
	}
	else
	{
		alert(r.responseText);	
	}
}
function fadeObj(objId, opacity)
{
	delay = 100;
	if(!opacity && isNaN(opacity))
	{
		opacity = 80;
		arg = "fadeObj('" + objId + "', " + opacity + ")";
		setTimeout(arg,delay);
	}
	else if(opacity > 0)
	{
		opacity -= 20;
		arg = "fadeObj('" + objId + "', " + opacity + ")";
		setTimeout(arg,delay);
	}
	obj = $(objId);
	if (navigator.appName.indexOf("Netscape")!=-1&&parseInt(navigator.appVersion)>=5)
		obj.style.MozOpacity=opacity/100;
	else if (navigator.appName.indexOf("Microsoft")!=-1&&parseInt(navigator.appVersion)>=4)
		$("shareBox").style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+opacity+");";
}
function closeShare()
{
	$("shareBox").style.display = "none";	
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}
function moreInfo(who)
{
	url = "more_info_"+who+".htm";
//	name = "info";
	attributes = "resizable=1,toolbar=0,location=0,status=0,scrollbars=1,menubar=0,directories=0,width=500,height=600";
//	newwindow = window.open(url,name,attributes);
	window.open(url,"mywindow",attributes);
}
function openChron(movie, id)
{
	url = "../m_training/chronology.php?curMovie=" + movie + "&curID=" + id;
	win = "chron";
	props = "left=20,top=20,width=610,height=400,toolbar=0,resizable=1";
	window.open(url, win, props);
}
function openPlayer(movie, id)
{
	url = "../m_training/player_full.php?curMovie=" + movie + "&curID=" + id + "&showing=false";
	win = "playerFull";
	props = "left=20,top=20,width=470,height=395,toolbar=0,resizable=1";
	window.open(url, win, props);
}
function videoWin(movie, title, width, height)
{
	url = "../video_player.php?movie="+movie+"&mt="+title;
	win = "playerFull";
	props = "left=20,top=20,width="+width+",height="+height+",toolbar=0,resizable=1";
	window.open(url, win, props);
}
function showHide(which)
{
	if(which)
	{
		document.getElementById("codesTable").style.display = "block";
		document.getElementById("showHideP").innerHTML = "Locking and Unlocking Chronologies (Show | <a href='#' onclick='showHide(false)'>Hide</a>)";
	}
	else
	{
		document.getElementById("codesTable").style.display = "none";
		document.getElementById("showHideP").innerHTML = "Locking and Unlocking Chronologies (<a href='#' onclick='showHide(true)'>Show</a> | Hide)";
	}
}
function lock(who)
{
	document.codeForm.lock.value=who;
	document.codeForm.unlock.value=0;
	document.codeForm.submit();
}
function unlock(who)
{
	document.codeForm.unlock.value=who;
	document.codeForm.lock.value=0;
	document.codeForm.submit();
}
function ajaxlock(who,i)
{
	document.getElementById("lockunlock"+who).innerHTML = "locking...";
	document.getElementById("videoRow").value = i;
	document.getElementById("movieId").value = who;
	var url = "lock_unlock.php";
	var data = "&id=" + who + "&lock=1";
	var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: doneLock});
}
function ajaxunlock(who,i)
{
	document.getElementById("lockunlock"+who).innerHTML = "unlocking...";
	document.getElementById("videoRow").value = i;
	document.getElementById("movieId").value = who;
	var url = "lock_unlock.php";
	var data = "&id=" + who + "&lock=0";
	var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: doneUnlock});	
}
function doneLock(r)
{
	if(r.responseText != "success")
		alert("Error" + r.responseText);
	videoRow = document.getElementById("videoRow").value;
	movieId = document.getElementById("movieId").value;
	document.getElementById("lockunlock"+videoRow).innerHTML = "LOCK | <a href='javascript:ajaxunlock("+movieId+","+videoRow+")'>UNLOCK</a>";	
}
function doneUnlock(r)
{
	if(r.responseText != "success")
		alert("Error" + r.responseText);
	videoRow = document.getElementById("videoRow").value;
	movieId = document.getElementById("movieId").value;
	document.getElementById("lockunlock"+videoRow).innerHTML = "<a href='javascript:ajaxlock("+movieId+","+videoRow+")'>LOCK</a> | UNLOCK";
}
// ====== Training Fxns ======
function turnOn()
{
	document.getElementById("roundBtn").style.display = "block";
}
function turnOff(responseText)
{
	responseArray = responseText.split("|");
	document.getElementById("roundBtn").style.display = "block";
	document.getElementById("roundBtnId").disabled = true;
	document.getElementById("pulldown").disabled = true;
	//document.getElementById("launchBtn").style.display = "block";
	document.roundForm.newRound.value = responseArray[0];
	document.roundForm.m.value = responseArray[1];
	document.roundForm.roundID.value = responseArray[2];
	document.roundForm.submit();
}
function openWin(movie, id, roundNum)
{
	url = "player_full.php?curMovie=" + movie + "&curID=" + id + "&showing=false&roundNum=" + roundNum;
	win = "playerFull";
	props = "left=20,top=20,width=470,height=395,toolbar=0,resizable=1";
	window.open(url, win, props);
}
function launchWin()
{
	url = "player_full.php?curMovie=" + document.roundForm.m.value + "&curID=" + document.roundForm.roundID.value + "&showing=false&roundNum=" + document.roundForm.newRound.value;
	win = "playerFull";
	props = "left=20,top=20,width=470,height=395,toolbar=0,resizable=1";
	window.open(url, win, props);
}
function focusTxt(who,first,last)
{
	for(i=first;i<=last;i++)
	{
		if(i==who)
			document.getElementById("txt"+i).style.backgroundColor = "#FFFFFF";
		else
			document.getElementById("txt"+i).style.backgroundColor = "#E1E1E1";
	}
}
function specialChar(str)
{
	var isGood = true;
	noArr = new Array("<",">","~","|","^","/","\\");
	for(i=0;i<noArr.length;i++)
	{
		if(str.indexOf(noArr[i]) >= 0)
			isGood = false;
	}
	if(isGood)
		return true;
	else
		return false;
}
function checkIt(create)
{
	for(i=1;i<=8;i++)
	{
		document.getElementById('e'+i).style.display = "none";
	}
	if(document.userForm.fn.value == "")
	{
		document.getElementById('e1').innerHTML = "Please enter your first name.";
		document.getElementById('e1').style.display = "block";
		return false;
	}
	if(document.userForm.fn.value.length > 45)
	{
		document.getElementById('e1').innerHTML = "Names cannot contain more than 45 characters.";
		document.getElementById('e1').style.display = "block";
		return false;
	}
	if(!specialChar(document.userForm.fn.value))
	{
		document.getElementById('e1').innerHTML = "Please use only letters and numbers.";
		document.getElementById('e1').style.display = "block";
		return false;
	}
	if(document.userForm.ln.value == "")
	{
		document.getElementById('e2').innerHTML = "Please enter your last name.";
		document.getElementById('e2').style.display = "block";
		return false;
	}
	if(!specialChar(document.userForm.ln.value))
	{
		document.getElementById('e2').innerHTML = "Please use only letters and numbers.";
		document.getElementById('e2').style.display = "block";
		return false;
	}
	if(document.userForm.ln.value.length > 45)
	{
		document.getElementById('e2').innerHTML = "Names cannot contain more than 45 characters.";
		document.getElementById('e2').style.display = "block";
		return false;
	}
	if(document.userForm.un.value.length < 4 || document.userForm.un.value.length > 45)
	{
		document.getElementById('e3').innerHTML = "Username must be at between 4 and 45 characters.";
		document.getElementById('e3').style.display = "block";
		return false;
	}
	if(!specialChar(document.userForm.un.value))
	{
		document.getElementById('e3').innerHTML = "Please use only letters and numbers.";
		document.getElementById('e3').style.display = "block";
		return false;
	}
	if(document.userForm.pw1.value != document.userForm.pw2.value)
	{
		document.getElementById('e5').innerHTML = "Your passwords to not match.";
		document.getElementById('e5').style.display = "block";
		return false;
	}
	if(document.userForm.pw1.value.length < 4 || document.userForm.pw1.value.length > 45)
	{
		document.getElementById('e5').innerHTML = "Password must be between 4 and 45 characters.";
		document.getElementById('e5').style.display = "block";
		return false;
	}
	if(!specialChar(document.userForm.pw1.value))
	{
		document.getElementById('e5').innerHTML = "Please use only letters and numbers.";
		document.getElementById('e5').style.display = "block";
		return false;
	}
	email = document.userForm.email.value;
	atLoc = email.indexOf("@");
	dotLoc = email.lastIndexOf(".");
	if(atLoc < 1 || dotLoc < atLoc+2 || email.length < dotLoc+3)
	{
		document.getElementById('e6').innerHTML = "Invalid e-mail address.";
		document.getElementById('e6').style.display = "block";
		return false;
	}
	if(!specialChar(document.userForm.email.value))
	{
		document.getElementById('e6').innerHTML = "You have used an invalid character.";
		document.getElementById('e6').style.display = "block";
		return false;
	}
	if(document.userForm.email.value.length > 45)
	{
		document.getElementById('e6').innerHTML = "Email address cannot contain more than 45 characters.";
		document.getElementById('e6').style.display = "block";
		return false;
	}
	if(document.userForm.affiliation.value == 'top')
	{
		document.getElementById('e7').innerHTML = "Please select an affiliation.";
		document.getElementById('e7').style.display = "block";
		return false;
	}
	if(document.userForm.menuDistrict.value == 0)
	{
		document.getElementById('e8').innerHTML = "Please select a district.";
		document.getElementById('e8').style.display = "block";
		return false;
	}
	if(document.userForm.dist.value.length > 45)
	{
		document.getElementById('e8').innerHTML = "District name cannot contain more than 45 characters.";
		document.getElementById('e8').style.display = "block";
		return false;
	}
	if(create)
		createUser();
	else
		updateUser();
	return false;
}
function createUser(r)
{
	if(!r)
	{
		document.getElementById('submitBtn').value = "Working...";
		document.getElementById('submitBtn').disabled = true;
		var fn = document.userForm.fn.value;
		var ln = document.userForm.ln.value;
		var un = document.userForm.un.value;
		var pw = document.userForm.pw1.value;
		var email = document.userForm.email.value;
		var aff_id = document.userForm.affiliation.value;
		var dist_id = document.userForm.menuDistrict.value;
		var dist = document.userForm.dist.value;
		var url = "create_user.php";
		var data = "&fn=" + escape(fn) + "&ln=" + escape(ln) + "&un=" + escape(un) + "&pw=" + escape(pw) + "&email=" + escape(email) + "&aff_id=" + escape(aff_id) + "&dist_id=" + escape(dist_id) + "&dist=" + escape(dist);
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: createUser});
	}
	else if(r.responseText == "EXISTS")
	{
		document.getElementById('submitBtn').value = "Try Again";
		document.getElementById('submitBtn').disabled = false;
		document.getElementById('e3').innerHTML = "Username already taken";
		document.getElementById('e3').style.display = "block";
		document.userForm.un.focus();
	}
	else if(r.responseText == "EXISTS2")
	{
		document.getElementById('submitBtn').value = "Try Again";
		document.getElementById('submitBtn').disabled = false;
		document.getElementById('e6').innerHTML = "Email address already taken";
		document.getElementById('e6').style.display = "block";
		document.userForm.email.focus();
	}
	else if(r.responseText == "SUCCESS")
	{
		document.userForm.submit();
	}
	else
	{          
		alert("Error:\n" + r.responseText);
	}
	return false;
}
function updateUser(r)
{
	if(!r)
	{
		document.getElementById('submitBtn').value = "Saving...";
		document.getElementById('submitBtn').disabled = true;
		var fn = document.userForm.fn.value;
		var ln = document.userForm.ln.value;
		var un = document.userForm.un.value;
		var pw = document.userForm.pw1.value;
		var email = document.userForm.email.value;
		var aff_id = document.userForm.affiliation.value;
		var dist_id = document.userForm.menuDistrict.value;
		var dist = document.userForm.dist.value;
		var url = "update_user.php";
		var data = "&fn=" + escape(fn) + "&ln=" + escape(ln) + "&un=" + escape(un) + "&pw=" + escape(pw) + "&email=" + escape(email) + "&aff_id=" + escape(aff_id) + "&dist_id=" + escape(dist_id) + "&dist=" + escape(dist);
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: updateUser});
	}
	else if(r.responseText == "FAILED")
	{
		alert("Error:\n" + "There was a database error.  Try again");
		$('submitBtn').value = "Try Again";
		$('submitBtn').disabled = false;
	}
	else if(r.responseText == "SUCCESS")
	{
		document.userForm.submit();
	}
	else
	{          
		alert("Error:\n" + r.responseText);
	}
	return false;
}
function checkUser(r)
{
	if(!r)
	{
		$('loginBtn').innerHTML = "Checking";
		$('loginBtn').disabled = true;
		var url = "../users/check_user.php";
		var data = "&un=" + escape(document.loginForm.loginUN.value) + "&pw=" + escape(document.loginForm.loginPW.value);
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: checkUser});
	}
	else
	{
		if(r.responseText == "FAILED")
		{
			$('loginBtn').innerHTML = "Try Again";
			$('loginBtn').disabled = false;
			$('createForgot').innerHTML = "<a href='javascript:forgotPass()'>Forgot Password?</a>";
		}
		else if(r.responseText == "SUCCESS")
		{
			window.location = "../users/my_tpr.php";
			//document.loginForm.submit();
		}
		else
		{          
			alert("Error:\n" + r.responseText);
		}
	}
	return false;
}
function forgotPass()
{
	$('circleLogin').style.display = "none";
	$('circleForgot').style.display = "block";
	$('loginBtn2').innerHTML = "Send";
	$('forgot').select();
}
function cancelForgot()
{
	$('circleLogin').style.display = "block";
	$('circleForgot').style.display = "none";
}
function emailMe(r)
{
	if(!r)
	{
		$('loginBtn2').innerHTML = "Sending...";
		$('loginBtn2').disabled = true;
		var url = "../users/email.php";
		var data = "&email=" + escape($('forgot').value);
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: emailMe});
	}
	else if(r.responseText == "SUCCESS")
	{
		//$('circleLogin').style.display = "block";
		//$('circleForgot').style.display = "none";
		$('loginBtn2').innerHTML = "Sent!";
		$('loginBtn2').disabled = false;
	}
	else
	{          
		alert("Sorry.  No such email in our database.");
	}
	return false;
}
function createids(r)
{
	if(!r)
	{
		$('createidsbtn').value = "Working...";
		$('createidsbtn').disabled = true;
		var url = "create_ids.php";
		var data = "&id=" + escape(document.form1.id.value);
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: createids});
	}
	else if(r.responseText == "SUCCESS")
	{
		window.location = "ids.php";
	}
	else
	{          
		alert("Error:\n" + r.responseText);
	}
	return false;
}
function iForgot(flip)
{
	if(flip)
	{
		$('loginDiv').style.display = "none";
		$('emailDiv').style.display = "block";
	}
	else
	{
		$('loginDiv').style.display = "block";
		$('emailDiv').style.display = "none";
	}
}
function onDiv(start,end,who)
{
	for(i=start;i<=end;i++)
	{
		if(i==who)
			$('onDiv'+i).style.display = "block";
		else
			$('onDiv'+i).style.display = "none";
	}
}
// === Knowledge Builder Stuff ===
var page = 1;
var saveKb = false;
function hime(who)
{
	if(who!=page)
		document.getElementById("btn"+who).style.backgroundColor = "#CFCFED";
}
function unhi(who)
{
	if(who!=page)
		document.getElementById("btn"+who).style.backgroundColor = "#A4A4DD";
}
function changePage(who)
{
	document.getElementById("btn"+page).style.backgroundColor = "#A4A4DD";
	document.getElementById("q"+page).style.display = "none";
	document.getElementById("btn"+who).style.backgroundColor = "#FFFFFF";
	document.getElementById("q"+who).style.display = "block";
	page = who;
}
function doFirst()
{
	changePage(1);
	scoreStr = $("kb_score").value;
	answStr = $("aStr").value;
	countArray = $("countStr").value.split("|");
	strCount = 0;
	correct = 0;
	for(i=1;i<=countArray.length;i++)
	{
		for(j=1;j<=countArray[i-1];j++)
		{
			
			if(scoreStr.substr(strCount,1) != "-")
			{
				if(scoreStr.substr(strCount,1) == "A")
					si = 11;
				else if(scoreStr.substr(strCount,1) == "B")
					si = 12;
				else
					si = parseInt(scoreStr.substr(strCount,1))+1;
				$('a'+i+j).selectedIndex = si;
			}
			if(answStr.length > 1)
			{
				/*
				if(answStr.substr(strCount,1) != scoreStr.substr(strCount,1))
					$('pd'+i+j).style.backgroundColor = "#990000";
				else
					correct++;
				*/
				$('a'+i+j).disabled = true;
			}
			
			strCount++;
		}
	}
	if(answStr.length > 1)
	{
		//alert("Score: " + correct + " out of " + answStr.length);
		alert("Thank you!  Now, please log on to MY TPR and take the BONUS test under My Progress.");
		$('clearBtn').disabled = true;
		$('submitBtn').disabled = true;
		$('saveKb').disabled = true;
	}
}
function saveChanges(r)
{
	if(!r)
	{
		countArray = $("countStr").value.split("|");
		scoreStr = "";
		for(i=1;i<=countArray.length;i++)
		{
			for(j=1;j<=countArray[i-1];j++)
			{
				if($('a'+i+j).value == "10")
					scoreVal = "A";
				else if($('a'+i+j).value == "11")
					scoreVal = "B";
				else
					scoreVal = $('a'+i+j).value;
				scoreStr += scoreVal;
			}
		}
		$('saveKb').value = "Saving...";
		$('saveKb').disabled = true;
		var url = "save_kb.php";
		if(saveKb)
			var submitKb = 1;
		else
			var submitKb = 0;
		var data = "&str=" + escape(scoreStr) + "&kb=" + $('kb').value + "&submitKb=" + submitKb;
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: saveChanges});
	}
	else if(r.responseText == "SUCCESS")
	{
		$('saveKb').value = "Save Changes";
		$('saveKb').disabled = false;
		if(saveKb)
		{
			window.location = "knowledge_builder.php?kb="+$('kb').value+"&skb="+$('kb').value;	
		}
	}
	else
	{          
		alert("Error:\n" + r.responseText);
	}
	return false;

}
function clearScores()
{
	if(confirm("Clear all scores?"))
	{			   
		countArray = $("countStr").value.split("|");
		for(i=1;i<=countArray.length;i++)
		{
			for(j=1;j<=countArray[i-1];j++)
			{
				$('a'+i+j).selectedIndex = 0;
			}
		}
	}
}
function submitScores()
{
	saveKb = true;
	saveChanges();
}
// === My Teacher Functions === 
var teachArray = new Array();
var sortDesc = false;
var sortCol = 0;
var teacherDelete;
function makeList()
{
	// Start by removing all the nodes
	for(i=$("teachList").childNodes.length-1;i>=0;i--)
	{
		var c = $("teachList").childNodes[i];
		c.parentNode.removeChild(c);
	}
	// Remake the list from teachArray
	for(i=0;i<teachArray.length;i++)
	{
		if(teachArray[i])
		{
			var first = document.createTextNode(teachArray[i][0]);
			var last = document.createTextNode(teachArray[i][1]);
			var school = document.createTextNode(teachArray[i][2]);
			//var teacherID = document.createTextNode(teachArray[i][3]);
			var teacherID = teachArray[i][3];
			var newLi = makeNode(first, last, school, teacherID);
			newLi.id = "item"+i;
			$("teachList").appendChild(newLi);
		}
	}
}
function addLine(insertID)
{
	var first = document.createTextNode($("teachFirst").value);
	var last = document.createTextNode($("teachLast").value);
	var school = document.createTextNode($("teachSchool").value);
	var newLi = makeNode(first, last, school, insertID);
	newLi.id = "item"+countRows();
	$("teachList").appendChild(newLi);
	$("teachFirst").value = "";
	$("teachLast").value = "";
	$("teachSchool").value = "";
	$("teachFirst").focus();
	return false;
}
function addTeacher(r)
{
	if(!r)
	{
		first = $("teachFirst").value;
		last = $("teachLast").value;
		school = $("teachSchool").value;
		$('addBtn').value = "Working...";
		$('addBtn').disabled = true;
		var url = "add_teacher.php";
		var data = "&first=" + escape(first) + "&last=" + escape(last) + "&school=" + escape(school);
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: addTeacher});
	}
	else
	{
		pos = r.responseText.indexOf(":");
		if(r.responseText.substr(0,pos) == "SUCCESS")
		{
			$('addBtn').value = "Add";
			$('addBtn').disabled = false;
			addLine(r.responseText.substr(pos+1));
		}
		else
		{
			alert("Error:\n" + r.responseText);		
		}
	}
	return false;
}
function makeNode(first, last, school, teacherID)
{
	var newLi = document.createElement("li");
	var newDiv1 = document.createElement("div");
	var newDiv2 = document.createElement("div");
	var newDiv3 = document.createElement("div");
	var newDiv5 = document.createElement("div");
	var newImg = document.createElement('img');
	newImg.src="../images/close_btn.gif";
	newImg.me = first.nodeValue + " " + last.nodeValue;
	newImg.teacherID = teacherID;
	newImg.onclick = deleteFxn;
	newDiv1.className='teachText';
	newDiv2.className='teachText';
	newDiv3.className='teachText';
	newDiv5.className='ieSpacing';
	newImg.className = 'closeBtn';
	newDiv1.appendChild(first);
	newDiv2.appendChild(last);
	newDiv3.appendChild(school);
	newLi.appendChild(newDiv1);
	newLi.appendChild(newDiv2);
	newLi.appendChild(newDiv3);
	newLi.appendChild(newImg);
	newLi.appendChild(newDiv5);
	return newLi;
}
function deleteFxn()
{
	if($('google').style.display != "block")
	{
		if(confirm("Delete " + this.me +  " (ID:" + this.teacherID + ")?"))
		{
			this.parentNode.parentNode.removeChild(this.parentNode);
			teacherDelete = this.teacherID;
			deleteTeacher();
		}
	}
}
function deleteTeacher(r)
{
	if(!r)
	{
		$('google').style.display = "block";
		var url = "delete_teacher.php";
		var data = "&teacherID=" + teacherDelete;
		var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: deleteTeacher});
	}
	else if(r.responseText == "SUCCESS")
	{
		$('google').style.display = "none";
	}
	else
	{          
		alert("Error:\n" + r.responseText);
	}
	return false;	
}
function countRows()
{
	count = 0;
	for(i=0;i<$("teachList").childNodes.length;i++)
	{
		var c = $("teachList").childNodes[i];
		if (c.tagName == 'LI')
			count++;
	}
	return count;
}
function sortList(who)
{
	// Create multidimensional array: Array(Array(first, last, school),Array(...))
	teachArray = new Array();
	for(i=0;i<$("teachList").childNodes.length;i++)
	{
		var c = $("teachList").childNodes[i];
		if (c.tagName == 'LI')
		{
			teachArray[i] = new Array();
			for(j=0;j<c.childNodes.length;j++)
			{
				if(c.childNodes[j].className == 'teachText')
				{
					teachArray[i][j] = c.childNodes[j].innerHTML;
				}
			}
		}		
	}
	// Figure out the arrow gif
	for(i=0;i<=2;i++)
	{
		if(i==who)
		{
			if(who == sortCol)
			{
				sortDesc = !sortDesc;
				if(sortDesc)
					$("a"+i).src = "../images/arrow_dn.gif";
				else
					$("a"+i).src = "../images/arrow_up.gif";
			}
			else
			{
				sortDesc = true;
				$("a"+i).src = "../images/arrow_dn.gif";
			}			
		}
		else
		{
			$("a"+i).src = "../images/t.gif";
		}
	}
	// Sort the list
	sortCol = who;
	if(who == 0)
		teachArray.sort(sortByFirst);
	else if(who == 1)
		teachArray.sort(sortByLast);
	else if(who == 2)
		teachArray.sort(sortBySchool);
	// Remake the list
	makeList();
}
function sortByFirst(a, b) {
    var x = a[0].toLowerCase();
    var y = b[0].toLowerCase();
	if(sortDesc)
	    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	else
		return ((x > y) ? -1 : ((x < y) ? 1 : 0));
}
function sortByLast(a, b) {
    var x = a[1].toLowerCase();
    var y = b[1].toLowerCase();
	if(sortDesc)
	    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	else
		return ((x > y) ? -1 : ((x < y) ? 1 : 0));
}
function sortBySchool(a, b) {
    var x = a[2].toLowerCase();
    var y = b[2].toLowerCase();
    if(sortDesc)
	    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	else
		return ((x > y) ? -1 : ((x < y) ? 1 : 0));
}
function sortFirst()
{
	sortList(0);
}
function getDistricts(r)
{
	if(!r)
	{
		selectedItem = document.forms['userForm'].affiliation.selectedIndex;
		aff = document.forms['userForm'].affiliation.options[selectedItem].value;
		if(aff == "top")
		{
			$("e8").style.display = "none"
			document.forms['userForm'].menuDistrict.options.length = 0;
			document.forms['userForm'].menuDistrict.options[0] = new Option("Select Affiliation",9999);
			document.forms['userForm'].menuDistrict.disabled = true;
		}
		else if(aff == 0)
		{
			$("e8").style.display = "none"
			document.forms['userForm'].menuDistrict.options.length = 0;
			document.forms['userForm'].menuDistrict.options[0] = new Option("N/A",9999);
			document.forms['userForm'].menuDistrict.disabled = true;
		}
		else
		{
			$("e8").innerHTML = "Working...";
			$("e8").style.display = "block";
			var url = "../get_districts.php";
			var data = "&aff=" + aff;
			var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: getDistricts});
		}
	}
	else
	{
		$("e8").style.display = "none";
		if(r.responseText != "none")
		{
			dArray = r.responseText.split("|");
			document.forms['userForm'].menuDistrict.options.length = 0;
			document.forms['userForm'].menuDistrict.options[0] = new Option("-- Select --",0);
			for(i=0;i<dArray.length;i++)
			{
				arr = dArray[i].split("~");
				document.forms['userForm'].menuDistrict.options[i+1] = new Option(arr[1],arr[0]);
			}
			document.forms['userForm'].menuDistrict.options[dArray.length+1] = new Option("Other...",9999);
			document.forms['userForm'].menuDistrict.disabled = false;
		}
		else
		{
			document.forms['userForm'].menuDistrict.options.length = 0;
			document.forms['userForm'].menuDistrict.options[0] = new Option("N/A",9999);
		}
	}
	return false;
}
/*
function fillList()
{
	str = $("txt7").value.toLowerCase();
	$("dList").innerHTML = "";
	if(str.length > 0)
	{
		ret = "If you see your district, please click it:<br>";
		count = 0;
		for(var d in dObj)
		{
			haystack = dObj[d].toLowerCase();
			pos = haystack.indexOf(str);
			if(pos >= 0)
			{
				ret += "<a href='javascript:pickMe("+d+")'>";
				ret += dObj[d];
				ret += "</a><br>";
				count++;
			}
		}
		if(count <= 14)
		{
			$("dList").innerHTML = ret;
		}
	}
	else
	{
		$("dList").innerHTML = "Type your district above.";
	}
}
function pickMe(who)
{
	$("txt7").value = dObj[who];
	$("dist_id").value = who;
	$("dList").innerHTML = "";
}
*/
/*
function checkDistrict()
{
	selectedItem = document.forms['userForm'].menuDistrict.selectedIndex;
	who = document.forms['userForm'].menuDistrict.options[selectedItem].value;
	if(who == 9999)
	{
		document.getElementById('districtDiv').style.display = "block";
		document.getElementById('txt7').focus();
	}
	else
	{
		document.getElementById('districtDiv').style.display = "none";
	}
}
function makeDist(s)
{
	distArray = s.split("~");
	districtArray = new Array();
	for(i=0;i<distArray.length;i++)
	{
		theArray = distArray[i].split("|");
		theKey = theArray[0];
		theArray.shift();
		districtArray[theKey] = theArray;
	}
}
function changeAffiliation()
{
	selectedItem = document.forms['userForm'].affiliation.selectedIndex;
	who = document.forms['userForm'].affiliation.options[selectedItem].value;
	document.forms['userForm'].menuDistrict.options.length = 0;
	if(!districtArray[who])
	{
		document.forms['userForm'].menuDistrict.options[0] = new Option("N/A",85);
		document.forms['userForm'].menuDistrict.disabled = true;
		document.getElementById('districtDiv').style.display = "none";
		checkDistrict(0);
	}
	else if(districtArray[who].length == 0)
	{
		document.forms['userForm'].menuDistrict.options[0] = new Option("Enter District Below",0);
		document.forms['userForm'].menuDistrict.disabled = true;
		checkDistrict(1);
	}
	else
	{
		document.forms['userForm'].menuDistrict.options[0] = new Option("-- Select --",0);
		for(i=0;i<districtArray[who].length;i++)
		{
			arr = districtArray[who][i].split("##");
			document.forms['userForm'].menuDistrict.options[i+1] = new Option(arr[1],arr[0]);
		}
		document.forms['userForm'].menuDistrict.options[districtArray[who].length+1] = new Option("Other...",9999);
		document.forms['userForm'].menuDistrict.disabled = false;
		document.getElementById('districtDiv').style.display = "none";
	}	
}
*/
function checkDistrict(dis)
{
	selectedItem = document.forms['userForm'].menuDistrict.selectedIndex;
	who = document.forms['userForm'].menuDistrict.options[selectedItem].value;
	if(who == 9999 || dis == 1)
	{
		document.getElementById('districtDiv').style.display = "block";
		document.getElementById('txt7').focus();
	}
	else
	{
		document.getElementById('districtDiv').style.display = "none";
	}
}
/*
function checkDist(id)
{
	for(i=0;i<document.forms['userForm'].menuDistrict.options.length;i++)
	{
		if(document.forms['userForm'].menuDistrict.options[i].value == id)
			document.forms['userForm'].menuDistrict.selectedIndex = i;
	}
	alert(id);
}
*/
function showHide2(who,show)
{
	if(show)
	{
		document.getElementById('showHide'+who).innerHTML = " [show|<a href='javascript:showHide2("+who+",false)'>hide</a>]";
		document.getElementById('sec'+who).style.display = "block";
	}
	else
	{
		document.getElementById('showHide'+who).innerHTML = " [<a href='javascript:showHide2("+who+",true)'>show</a>|hide]";	
		document.getElementById('sec'+who).style.display = "none";
	}
}
