/*
<!---
	<fuseDoc>
		<title>Skin : Javascript : Events</title>
		<item>
			<description>
				<![CDATA[
					<p>This script file is invoked by switch_script.cfm template 
					and it is one of the default scripts.</p>
			
					<p>All events are triggered on window onload.</p>
				]]>
			</description>
			<date> 08/03/2005</date>
			<author>JLN</author>
		</item>
	</fuseDoc>
--->
*/

var loaded = new Array(0);
var initted = false;
addEvents = function()
{
	// pre-load images
	sideToggleOpen = new Image();
	sideToggleClose = new Image();
	sideToggleOpen.src = current_graphics_path + "sidemenu_open.gif";
	sideToggleClose.src = current_graphics_path + "sidemenu_close.gif";
	arrowOpen = new Image();
	arrowClose = new Image();
	arrowOpen.src = current_graphics_path + "arrow.gif";
	arrowClose.src = current_graphics_path + "arrow_d.gif";
		
		
	// focus first element
	if (document.forms[0])
	{
		if ((location.href.indexOf("help") == -1) && (location.href.indexOf("addWritingPrompt") == -1))
		{
			if (document.forms[0].elements.length)
			{
				for (i=0; i < document.forms[0].elements.length; i++)
				{
					if (document.forms[0].elements[i].type == "text")
					{
						document.forms[0].elements[i].focus();
						break;
					}
				}
			}
		}
	}

	// onclick event for side menu toogle
	if (document.getElementById("sidemenutoggle"))
	{
		document.getElementById("sidemenutoggle").onclick = function()
		{
			toggleControlpanel();
		};
	}
	
	//fade script
	ps = document.getElementsByTagName("p");
	if (!loaded[0])
	{
		for (i=0; i < ps.length; i++)
		{
			if (ps[i].className == "error")
			{
				ps[i].setAttribute("id","error_"+i);
				fadeElement("error_"+i, 20, 1000, "#FFFFFF", "#EABEBE");
			}
			if (ps[i].className == "success")
			{
				ps[i].setAttribute("id","error_"+i);
				fadeElement("error_"+i, 20, 1000, "#FFFFFF", "#CCE3C2");
			}
		}
	}
	
	// form cancel button
	var referer = "";
	if (document.forms.length)
	{
		for (i=0; i < document.forms.length; i++)
		{
			for (j=0; j < document.forms[i].elements.length; j++)
			{
				if (document.forms[i].elements[j].name == "referer")
				{
					referer = document.forms[i].elements[j].value;
				}
				if (document.forms[i].elements[j].type == "button" && (document.forms[i].elements[j].id == "ExerciseDone" || document.forms[i].elements[j].value == "Cancel"))
				{
					document.forms[i].elements[j].onclick = function()
					{
						try {
							if ( $("#href").length > 0 && $("#href").val().length > 0 ) {
								window.location.href = $("#href").val();
							} else if (referer.length) {
								window.location.href = referer;
							} else {
								history.back();
							}
						} catch(e){}
					}
				}
			}
		}
	}
	
	// delete links
	ahrefs = document.getElementsByTagName("A");
	for (i=0; i < ahrefs.length; i++)
	{
		if (ahrefs[i].className == "delete")
		{
			ahrefs[i].onclick = function()
			{
				fuse = this.name;
				ID = this.id;
				if (confirm("Are you sure you want to delete this?",""))
				{
					window.location.href = "index.cfm?f=" + fuse + "&ID=" + ID;
				}
				else
					return false;
			}
		}
	}
	
	// add student script
	if (document.getElementById("addStudent"))
	{
		document.getElementById("addStudent").onclick = function()
		{
			theForm = document.forms[0];
			hiddenFName = document.createElement("INPUT");
			hiddenFName.setAttribute("type", "hidden");
			hiddenFName.setAttribute("name", "pass");
			hiddenFName.setAttribute("value", "true");
			theForm.appendChild(hiddenFName);
			document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
			document.forms[0].addTenStart.value = parseInt(document.forms[0].addTenStart.value) + 1;
			theForm.submit();
		}
	}
	
	// init editor
/*	
	if (document.getElementById("widgEditor"))
	{
		widgInit();
		setTimeout("sizeBorders()",100);
	}
*/	
	// init WC editor
//	if (document.getElementById("WCEditor"))
	if (document.getElementById("widgEditor") && !initted)
	{
		initted = true;
		//WCInit();
		widgInit();
		setTimeout("sizeBorders()",100);

		if (document.forms[0].href && document.forms[0].isLocked.value == "FALSE")
		{
			copyValue = function(){document.getElementById("ifOrgBody").value = document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML;}
			setTimeout("copyValue()",1000);
			// add check dirty
			var submitting = false;
			/*if (document.forms["form_basic"]) {
				document.forms["form_basic"].onsubmit = function()
				{
					try{submitting = true;}catch(e){}
				}
			}*/
			
			document.forms["iSearchForm"].onsubmit = function()
			{
				theSearchStr = escape(this.searchStr.value);
				for (i=0; i < this.searchType.length; i++)
				{
					if (this.searchType[i].checked)
						theSearchType = this.searchType[i].value;
				}
				href = "index.cfm?f=search_results&searchStr=" + theSearchStr + "&searchType=" + theSearchType;
				doEssayClick(href);
				return false;
			}
			
			hrefs = document.getElementsByTagName("A");
			
			for (a=0; a < hrefs.length; a++)
			{
				hrefs[a].onclick = function()
				{
					doEssayClick(this.href);
					this.href = "#";
				}
			}
			
			function doEssayClick(href)
			{
				try{
					orgValue = document.getElementById("ifOrgBody").value;
					newValue = document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML;
					
					// check existance of an error
					hasError = false;
					ps = document.getElementsByTagName("P");
					for (p=0; p < ps.length; p++)
					{
						if (ps[p].className == "error")
						{
							hasError = true;
						}
					}
					
					if (newValue != orgValue && !submitting || hasError)
					{
						if (location.href.toUpperCase().indexOf("FESSAYID") == -1)
						{
							// new essay
							if (confirm("Click 'OK' to save this essay.",""))
							{
								document.getElementById("iTraverseOverride").value = "TRUE";
								document.getElementsByTagName("input")[1].value = newValue;
								document.forms["form_basic"].submit();
							}
							else
								location.href = href;
						}
						else
							submitTHisEssay()

						function submitTHisEssay()
						{
							//document.getElementById("iTraverseOverride").value = "TRUE";
							document.forms["form_basic"].href.value = href;
							document.forms["form_basic"].autoSave.value = "TRUE";
							document.getElementsByTagName("input")[1].value = newValue;
							document.forms["form_basic"].submit();
						}
					}
					else
						location.href = href;
				}catch(e){}
			}
		}
	}
	
	// select default rubric
	if (document.getElementById("selectDefaultRubric"))
	{
		document.getElementById("selectDefaultRubric").onchange = function()
		{
			if (document.getElementById("selectDefaultRubric").selectedIndex != 0)
			{
				document.forms[0].groups.value = -1;
				document.forms[0].fElements.value = -1;
				document.forms[0].f.value = display_fuse.split("_")[0] + "_manageRubrics_addRubric";
				document.forms[0].getDefault.value = "true";
				elem = document.getElementsByTagName("TABLE")[1];
				elem.parentNode.removeChild(elem);
				document.forms[0].submit();
			}
		}
	}
	
	// add a rubric group
	if (document.getElementById("addRubricGroup"))
	{
		document.getElementById("addRubricGroup").onclick = function()
		{
			document.forms[0].groups.value = parseInt(document.forms[0].groups.value) + 1;
			document.forms[0].f.value = display_fuse.split("_")[0] + "_manageRubrics_addRubric";
			document.forms[0].submit();
		}
	}
	
	// add a rubric score category
	if (document.getElementById("addRubricScore"))
	{
		document.getElementById("addRubricScore").onclick = function()
		{
			document.forms[0].fElements.value = parseInt(document.forms[0].fElements.value) + 1;
			document.forms[0].f.value = display_fuse.split("_")[0] + "_manageRubrics_addRubric";
			document.forms[0].submit();
		}
	}
	
	// add a editmark group
	if (document.getElementById("addEditMarkGroup"))
	{
		document.getElementById("addEditMarkGroup").onclick = function()
		{
			document.forms[0].fElements.value = parseInt(document.forms[0].fElements.value) + 1;
			document.forms[0].f.value = "teacher_manageEditmarks_addEditmarks";
			document.forms[0].submit();
		}
	}
	
	// add a comment group
	if (document.getElementById("addCommentGroup"))
	{
		document.getElementById("addCommentGroup").onclick = function()
		{
			document.forms[0].fElements.value = parseInt(document.forms[0].fElements.value) + 1;
			document.forms[0].f.value = "teacher_manageComments_addComments";
			document.forms[0].submit();
		}
	}
	
	
	// remove category row
	if (document.getElementById("form_matrix"))
	{
		as = document.getElementsByTagName("a");
		for (j=0; j < as.length; j++)
		{
			if (as[j].id.split("_")[0] == "removeThis")
			{
				as[j].onclick = function()
				{
					document.forms[0].f.value = display_fuse.split("_")[0] + "_manageRubrics_addRubric";
					document.forms[0].whichGroup.value = this.name.split("_")[1];
					document.forms[0].groups.value = parseInt(document.forms[0].groups.value) - 1;
					document.forms[0].fElements.value = parseInt(document.forms[0].fElements.value) - 1;
					document.forms[0].submit();
				}
			}
		}
	}
	
	// remove rubric element
	if (document.getElementById("form_matrix"))
	{
		as = document.getElementsByTagName("a");
		for (j=0; j < as.length; j++)
		{
			if (as[j].id.split("_")[0] == "removeThisEl")
			{
				as[j].onclick = function()
				{
					document.forms[0].f.value = display_fuse.split("_")[0] + "_manageRubrics_addRubric";
					document.forms[0].whichElement.value = this.name.split("_")[1];
					document.forms[0].fElements.value = parseInt(document.forms[0].fElements.value) - 1;
					document.forms[0].submit();
				}
			}
		}
	}
	// remove editmark element
	if (document.getElementById("form_matrix"))
	{
		as = document.getElementsByTagName("a");
		for (j=0; j < as.length; j++)
		{
			if (as[j].id.split("_")[0] == "removeThisEmEl")
			{
				as[j].onclick = function()
				{
					document.forms[0].f.value = "teacher_manageEditmarks_addEditmarks";
					document.forms[0].whichElement.value = this.name.split("_")[1];
					document.forms[0].fElements.value = parseInt(document.forms[0].fElements.value) - 1;
					document.forms[0].submit();
				}
			}
		}
	}
	// select default edit mark group
	if (document.getElementById("selectDefaultEditmark"))
	{
		document.getElementById("selectDefaultEditmark").onchange = function()
		{
			if (document.getElementById("selectDefaultEditmark").selectedIndex != 0)
			{
				document.forms[0].fElements.value = -1;
				document.forms[0].f.value = "teacher_manageEditmarks_addEditmarks";
				document.forms[0].getDefault.value = "true";
				document.forms[0].submit();
			}
		}
	}
	// remove comment element
	if (document.getElementById("form_matrix"))
	{
		as = document.getElementsByTagName("a");
		for (j=0; j < as.length; j++)
		{
			if (as[j].id.split("_")[0] == "removeThisCmtEl")
			{
				as[j].onclick = function()
				{
					document.forms[0].f.value = "teacher_manageComments_addComments";
					document.forms[0].whichElement.value = this.name.split("_")[1];
					document.forms[0].fElements.value = parseInt(document.forms[0].fElements.value) - 1;
					document.forms[0].submit();
				}
			}
		}
	}
	// select default rubric
	if (document.getElementById("selectDefaultComment"))
	{
		document.getElementById("selectDefaultComment").onchange = function()
		{
			if (document.getElementById("selectDefaultComment").selectedIndex != 0)
			{
				document.forms[0].fElements.value = -1;
				document.forms[0].f.value = "teacher_manageComments_addComments";
				document.forms[0].getDefault.value = "true";
				document.forms[0].submit();
			}
		}
	}
	// onchange event for selecting a class in compose message
	if (document.getElementById("selectClassID"))
	{
		document.getElementById("selectClassID").onchange = function()
		{
			for (i=0; i < document.forms[0].elements.length; i++)
			{
				if (document.forms[0].elements[i].type == "checkbox")
				{
					document.forms[0].elements[i].checked = false;
				}
			}

			document.getElementsByTagName("input")[1].value = document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML;
			document.forms[0].f.value = "teacher_manageMessages_addMessage";
			document.forms[0].submit();
		}
	}
	
	// open/close messages
	if (document.getElementById("table_matrix"))
	{
		as = document.getElementsByTagName("a");
		for (i=0; i < as.length; i++)
		{
			if (as[i].className == "readMessage")
			{
				as[i].onclick = function()
				{
					obj = document.getElementById(this.id.split("_")[0] + "_div");
					objtd = document.getElementById(this.id.split("_")[0] + "_td");
					objto = document.getElementById(this.id.split("_")[0] + "_to");
					objsubject = document.getElementById(this.id.split("_")[0] + "_subject");
					
					if (obj.style.display == "block")
					{
					 	obj.style.display = "none";
					 	objtd.style.display = "inline";
						location.href = location.href.replace("#top","") + "#top";
					}
					else
					{
						
						orgClass = this.parentNode.className;
						
						if (obj)
							obj.style.display = "block";
					 	if (objtd)
							objtd.style.display = "none";
						if (objto)
							objto.className = "normal";
						if (objsubject)
							objsubject.className = "normal";
							
							//window.open(current_path + "/" + display_fuse + "/" + "dsp_" + display_fuse.split("_")[0] + "_manageMessages_getMessage.cfm?Teacher_Student_MessageID=" + this.id.split("_")[0] + "&ID=" + current_userID);
							path_arr = current_path.split("/").slice(2,current_path.split("/").length);
							new_cur_path = "/";
							for (i=0; i < path_arr.length; i++)
							{
								new_cur_path += path_arr[i] + "/";
							}
							loadXMLDoc(current_path + display_fuse + "/" + "dsp_" + display_fuse.split("_")[0] + "_manageMessages_getMessage.cfm?Teacher_Student_MessageID=" + this.id.split("_")[0] + "&ID=" + current_userID, this.id.split("_")[0]);
							sizeBorders();
							// manipulate message count text
							messCont = document.getElementById("messageNum");
							messS = document.getElementById("messageS");

					if (orgClass == "bold")
					{
							if (messCont.innerHTML != "no")
							{
								theNumber = messCont.innerHTML.replace("(","");
								theNumber = theNumber.replace(")","");
								theNumber = parseInt(theNumber)
								if (theNumber < 2)
								{
									messCont.innerHTML = "no";
									messS.innerHTML = "s";
								}
								else
								{
									messCont.innerHTML = "(" + (parseInt(theNumber) - 1) + ")";
									if ((parseInt(theNumber) - 1) == 1)
										messS.innerHTML = "";
									else
										messS.innerHTML = "s";
								}
							}
						}
					}
				}
			}
		}
	}
	
	// edit mark button init
	if (document.getElementById("editmarkContainer"))
	{
		lis = document.getElementsByTagName("a");
		for (i=0; i < lis.length; i++)
		{
			/*if (lis[i].className == "editMarkHeader")
			{
				lis[i].onclick = function()
				{
					toggleDisplay(this);
				}
			}*/
			if (lis[i].className == "editMarkItem")
			{
				lis[i].onclick = function()
				{
					insertEditmark(this); //essayModule2.js
				}
			}
			if (lis[i].className == "editMarkItemIcon")
			{
				lis[i].onclick = function()
				{
					insertEditmarkIcon(this); //essayModule2.js
				}
			}
		}
	}
	
	// choose reference section
	
	if (document.getElementById("chooseReference"))
	{
		document.getElementById("chooseReference").onchange = function()
		{
			if (this.options[this.selectedIndex].value.length)
				location.href = location.href.split("&")[0] + "&referenceID=" + this.options[this.selectedIndex].value;
		}
	}
	
	// select functions for essay shares
	if (document.getElementById("classmates"))
	{
		document.getElementById("mateButton").onclick = function()
		{
			moveSelectedOptions(document.forms[0].classmates,document.forms[0].sharepool);
		}
		document.getElementById("poolButton").onclick = function()
		{
			moveSelectedOptions(document.forms[0].sharepool,document.forms[0].classmates);
		}
		document.forms[0].onsubmit = function()
		{
			for (i=0; i < document.forms[0].sharepool.options.length; i++)
			{
				document.forms[0].shares.value += document.forms[0].sharepool.options[i].value + ",";
			}
		}
	}
	
	// select topmenuid for assignments
	if (document.getElementById("fTopMenuID"))
	{
		document.getElementById("fTopMenuID").onchange = function()
		{
			if (this.options.selectedIndex != 0)
			{
				this.form.f.value = document.forms[0].f.value.split(".")[0];
				this.form.action = this.form.action + "#addExercise";
				this.form.submit();
			}
		}
	}
	if (document.getElementById("fSubMenuID"))
	{
		document.getElementById("fSubMenuID").onchange = function()
		{
			if (this.options.selectedIndex != 0)
			{
				this.form.f.value = document.forms[0].f.value.split(".")[0];
				this.form.action = this.form.action + "#addExercise";
				this.form.submit();
			}
		}
	}
	if (document.getElementById("fTestTopMenuID"))
	{
		document.getElementById("fTestTopMenuID").onchange = function()
		{
			if (this.options.selectedIndex != 0)
			{
				this.form.f.value = document.forms[0].f.value.split(".")[0];
				this.form.action = this.form.action + "#addTest";
				this.form.submit();
			}
		}
	}
	if (document.getElementById("fTestSubMenuID"))
	{
		document.getElementById("fTestSubMenuID").onchange = function()
		{
			if (this.options.selectedIndex != 0)
			{
				this.form.f.value = document.forms[0].f.value.split(".")[0];
				this.form.action = this.form.action + "#addTest";
				this.form.submit();
			}
		}
	}
	if (document.getElementById("fWpMenuID"))
	{
		document.getElementById("fWpMenuID").onchange = function()
		{
			if (this.options.selectedIndex != 0)
			{
				this.form.f.value = document.forms[0].f.value.split(".")[0];
				this.form.action = this.form.action + "#addWritingPrompt";
				this.form.submit();
			}
		}
	}
	
	// time limit for assignment essays
	//not using this event
	if (document.getElementById("isTimed"))
	{
		document.getElementById("isTimed").onclick = function()
		{
			if (document.getElementById("fTimeLimit").style.display == "block")
				document.getElementById("fTimeLimit").style.display = "none";
			else
				document.getElementById("fTimeLimit").style.display = "block";
		}
	}

	//clear minute field while unchecked / set fTimed value
	if (document.getElementById("isTimedCheck")) { 
		document.getElementById("isTimedCheck").onchange = function() { 
		
			if (!document.forms[0].isTimedCheck.checked) { 
				if (document.forms[0].fTimeLimit.value.length) {
					document.forms[0].fTimeLimit.value = "";
				}
			}
			if (document.forms[0].isTimedCheck.checked) { 
				document.forms[0].fTimed.value = 1;
			}
		}
	}
	
	// assignments add exercise
	if (document.getElementById("addExercise"))
	{
		document.getElementById("addExercise").onclick = function()
		{
			if (!document.forms[0].addExercise.value.length)
			{
				document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
				document.forms[0].addExercise.value = 1;
				document.forms[0].action = document.forms[0].action + "#addExercise";
				document.forms[0].submit();
			}
			else
			{
				document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
				document.forms[0].addExercise.value = "";
				document.forms[0].action = document.forms[0].action + "#addExercise";
				document.forms[0].submit();
			}
		}
	}
	if (document.getElementById("addTest"))
	{
		document.getElementById("addTest").onclick = function()
		{
			if (!document.forms[0].addTest.value.length)
			{
				document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
				document.forms[0].addTest.value = 1;
				document.forms[0].action = document.forms[0].action + "#addTest";
				document.forms[0].submit();
			}
			else
			{
				document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
				document.forms[0].addTest.value = "";
				document.forms[0].action = document.forms[0].action + "#addTest";
				document.forms[0].submit();
			}
		}
	}
	if (document.getElementById("addWritingPrompt"))
	{
		document.getElementById("addWritingPrompt").onclick = function()
		{
			if (!document.forms[0].addWritingPrompt.value.length)
			{
				document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
				document.forms[0].addWritingPrompt.value = 1;
				document.forms[0].action = document.forms[0].action + "#addWritingPrompt";
				document.forms[0].submit();
			}
			else
			{
				document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
				document.forms[0].addWritingPrompt.value = "";
				document.forms[0].action = document.forms[0].action + "#addWritingPrompt";
				document.forms[0].submit();
			}
		}
	}
	if (document.getElementById("addEssay"))
	{
		document.getElementById("addEssay").onclick = function()
		{
			if (!document.forms[0].addEssay.value.length)
			{
				document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
				document.forms[0].addEssay.value = 1;
				document.forms[0].action = document.forms[0].action + "#addEssay";
				document.forms[0].submit();
			}
			else
			{
				document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
				document.forms[0].addEssay.value = "";
				document.forms[0].action = document.forms[0].action + "#addEssay";
				document.forms[0].submit();
			}
		}
	}
	if (document.getElementById("addJournal"))
	{
		document.getElementById("addJournal").onclick = function()
		{
			if (!document.forms[0].addJournal.value.length)
			{
				document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
				document.forms[0].addJournal.value = 1;
				document.forms[0].action = document.forms[0].action + "#addJournal";
				document.forms[0].submit();
			}
			else
			{
				document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
				document.forms[0].addJournal.value = "";
				document.forms[0].action = document.forms[0].action + "#addJournal";
				document.forms[0].submit();
			}
		}
	}	
	//assignment select class
	if (document.getElementById("assignSelectClassID"))
	{
		document.getElementById("assignSelectClassID").onchange = function()
		{
			for (i=0; i < document.forms[0].elements.length; i++)
			{
				if (document.forms[0].elements[i].type == "checkbox")
				{
					document.forms[0].elements[i].checked = false;
				}
			}
			document.forms[0].f.value = "teacher_manageAssignments_addAssignment";
			document.forms[0].submit();
		}
	}
	//assignment select class in assignment template
	if (document.getElementById("assignSelectThisClassID"))
	{
		document.getElementById("assignSelectThisClassID").onchange = function()
		{
			for (i=0; i < document.forms[0].elements.length; i++)
			{
				if (document.forms[0].elements[i].type == "checkbox")
				{
					document.forms[0].elements[i].checked = false;
				}
			}
			document.forms[0].f.value = "teacher_manageAssignments_addThisAssignment";
			document.forms[0].submit();
		}
	}
	// select an assignment template
	if (document.getElementById("assignTemplateID"))
	{
		document.getElementById("assignTemplateID").onchange = function()
		{
			document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
			document.forms[0].addExercise.value = "";
			document.forms[0].addTest.value = "";
			document.forms[0].addEssay.value = "";
			document.forms[0].addWritingPrompt.value = "";
			document.forms[0].submit();
		}
	}
	
	// onclick event for logout
	
	if (document.getElementById("logOut"))
	{
		document.getElementById("logOut").onclick = function()
		{
			logOutConfirm = confirm("Are you sure you wish to log out?");
			if (logOutConfirm)
			{
				location.href = this.href;
			}
			else
			{
				return false;
			}
		}
	}
	
	// onclick event for exercise and writing prompt interface
	
	if (document.getElementById("resetExercise"))
	{
		document.getElementById("resetExercise").onclick = function()
		{
			theconfirm = confirm("Are you sure you wish to reset?");
			if (theconfirm)
			{
				if (document.forms[0].fReset)
				{
					document.forms[0].fReset.value = true;
				}
				document.forms[0].submit();
			}
			else
			{
				return false;
			}
		}
	}
	
	//inline error handling for exercises/tests
	if (document.getElementById("submitSolution"))
	{
		document.getElementById("submitSolution").onclick = function()
		{
			selected = false;
			for (i=0; i < document.forms[0].fExercise_Question_SolutionID.length; i++)
			{
				if (document.forms[0].fExercise_Question_SolutionID[i].checked)
				{
					selected = true;
				}
			}
			if (selected)
				document.forms[0].submit();
			else
			{
				document.getElementById("inlineError").style.display = "block";
				fadeElement("error_0", 20, 1000, "#FFFFFF", "#EABEBE");
			}
		}
	}
	
	// turn in essays
	
	/*if (document.getElementById("returnEssayIn"))
	{
		document.getElementById("returnEssayIn").onclick = function()
		{
			confirmT = confirm("Are you sure you want to return this essay?");
			if (confirmT)
			{
				document.forms[0].isDone.value = true;
				document.getElementsByTagName("input")[1].value = document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML;
				document.forms[0].submit();
			}
		}
	}*/
	// return share
	/*if (document.getElementById("returnShare"))
	{
		document.getElementById("returnShare").onclick = function()
		{
			confirmT = confirm("Are you sure you want to return this essay?");
			if (confirmT)
			{
				document.forms[0].changeStatus.value = true;
				document.getElementsByTagName("input")[1].value = document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML;
				document.forms[0].submit();
			}
		}
	}*/
/*
	// return returnWritingpromptIn
	if (document.getElementById("returnWritingpromptIn")) {
		document.getElementById("returnWritingpromptIn").onclick = function() {
			confirmWp = confirm("Are you sure you want to return this Writingprompt?","");
			if (confirmWp) {
				document.forms[0].submit();
			}
			else {
				return false;
			}
		}
	}

	if (document.getElementById("fGrade"))
	{
		document.forms[0].elements["fGrade"].blur();
	}
*/
	if (document.getElementById("turnEssayIn"))
	{
		document.getElementById("turnEssayIn").onclick = function()
		{
			confirmT = confirm("Are you sure you want to turn this essay in?");
			if (confirmT)
			{
				document.forms[0].isDone.value = true;
				document.getElementsByTagName("input")[1].value = document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML;
				document.forms[0].submit();
			}
		}
	}
	
	// timer for essays
	/*if (document.getElementById("countDown"))
	{
		startCountDown();
	}*/
	
	// open close rubrics
	if (document.getElementById("openCloseRubric"))
	{
		document.getElementById("openCloseRubric").onclick = function()
		{
			if (document.getElementById("rubricCont").style.display == "block")
			{
				document.getElementById("rubricCont").style.display = "none";
				document["arrow"].src = arrowOpen.src;
			}
			else
			{
				document.getElementById("rubricCont").style.display = "block";
				document["arrow"].src = arrowClose.src;
			}
		}
	}
	
	//add buttons
	if (document.getElementById("addButton"))
	{
		document.getElementById("addButton").onclick = function()
		{
			location.href = "index.cfm?f=" + this.name;
		}
		if (document.getElementById("addButton1"))
		{
			document.getElementById("addButton1").onclick = function()
			{
				location.href = "index.cfm?f=" + this.name;
			}
		}
	}
	
	//mg12-08-08 add buttons
	if (document.getElementById("NewJournal"))
	{
		document.getElementById("NewJournal").onclick = function()
		{
			document.forms[0].submit();
		}
	}	
	
	// turn in journal
/*	mg 12-29
	if (document.getElementById("JournalTurnIn"))
	{
		document.getElementById("JournalTurnIn").onclick = function()
		{
			confirmT = confirm("Are you sure you want to turn in this Journal?");
			if (confirmT)
			{
				document.forms[0].isDone.value = true;
				document.getElementsByTagName("input")[1].value = document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML;				
				document.forms[0].submit();
			}
		}
	}	
*/	
	
	// add onchange event for choose class under manage grades
	if (document.getElementById("ChooseClassID"))
	{
		document.getElementById("ChooseClassID").onchange = function()
		{
			if (document.forms[0].fViewSelectedProgressReport)
				{
					document.forms[0].fViewSelectedProgressReport.value = false;
				}
			this.form.submit();
		}
	}
	// add onchange event for choose assignment under single assignment grade
	if (document.getElementById("chooseAssignmentID"))
	{
		document.getElementById("chooseAssignmentID").onchange = function()
		{
			if (this.options.selectedIndex != 0)
			{
				this.form.submit();
			}
		}
	}
    // add onchange event for choose student
	if (document.getElementById("chooseStudentID"))
	{
		document.getElementById("chooseStudentID").onchange = function()
		{
			if (this.options.selectedIndex != 0)
			{
				this.form.submit();
			}
		}
	}

	// do score math for essay rubrics
	if (document.getElementById("scored"))
	{
		var theTotal = 0;
		var scored = 0;
		
		selects = document.forms[0].elements;

		for (s=0; s < selects.length; s++)
		{
			if (selects[s].type == "select-one")
			{
				if (selects[s].className == "rubricScore")
				{
					selects[s].onchange = function()
					{
						if (this.options[this.selectedIndex].value.length)
						{
							document.getElementById("elDesc").innerHTML = document.getElementById(this.value).innerHTML;
							scored = 0;
							selectss = document.forms[0].elements;
							for (ss=0; ss < selectss.length; ss++)
							{
								if (selectss[ss].type == "select-one")
								{
									if (parseInt(selectss[ss].options[selectss[ss].selectedIndex].value.split("|")[1]))
									{
										scored = eval(parseInt(scored) + parseInt(selectss[ss].options[selectss[ss].selectedIndex].value.split("|")[1]));
									}
								}
							}
							document.getElementById("scored").innerHTML = scored;
							document.getElementById("per").innerHTML = Math.round(eval(scored/theTotal * 100)) + "%";
						}
					}
					
					theTotal = eval(parseInt(theTotal) + parseInt(selects[s].options[1].value.split("|")[1]));
					document.getElementById("scored").innerHTML = scored;
				}
			}
		}
		
		document.getElementById("total").innerHTML = theTotal;
	}
	

	// onchange event for selecting a class in writing prompts
	if (document.getElementById("promptsSelectClassID"))
	{
		document.getElementById("promptsSelectClassID").onchange = function()
		{
			document.forms[0].submit();
		}
	}
	
	// onchange class, set studentID nothing
	if (document.getElementById("promptsSelectClassID")) 
	{ 
		if (document.getElementById("promptsSelectStudentID")) 
		{
			document.getElementById("promptsSelectClassID").onchange = function()
			{
				document.forms[0].fStudentID.value = "";
				document.forms[0].submit();
			}
		}
	}
	
	// onchange event for selecting a student in writing prompts
	if (document.getElementById("promptsSelectStudentID"))
	{
		document.getElementById("promptsSelectStudentID").onchange = function()
		{
			document.forms[0].submit();
		}
	}
	
	//onclick event to show hints in writing prompts
	if (document.getElementById("eventQhint"))
	{
		document.getElementById("eventQhint").onclick = function()
		{
			if (document.getElementById("objectQhint").style.display == "block")
			{
				document.getElementById("objectQhint").style.display = "none";
				document["arrow1"].src = arrowOpen.src;
			}
			else
			{
				document.getElementById("objectQhint").style.display = "block";
				document["arrow1"].src = arrowClose.src;
			}
		}
	}
	if (document.getElementById("eventQsol"))
	{
		document.getElementById("eventQsol").onclick = function()
		{
			if (document.getElementById("objectQsol").style.display == "block")
			{
				document.getElementById("objectQsol").style.display = "none";
				document["arrow2"].src = arrowOpen.src;
			}
			else
			{
				document.getElementById("objectQsol").style.display = "block";
				document["arrow2"].src = arrowClose.src;
			}
		}
	}
	
	// switch essay version
	if (document.getElementById("essayVersionID"))
	{
		document.getElementById("essayVersionID").onchange = function()
		{
			if (this.options.selectedIndex != 0)
			{
				//document.forms[0].fEssayID.value = this.options[this.selectedIndex].value;
				document.forms[0].target = "_blank";
				document.forms[0].action = "index.cfm?f=teacher_manageAssignments_printView&showBody=false&versionView=true&fessayid=" + this.options[this.selectedIndex].value;
				document.getElementsByTagName("input")[1].value = document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML;
				document.forms[0].submit();
				document.forms[0].target = "_self";
				document.forms[0].action = "index.cfm";
			}
		}
	}
	// print button for rubric
	if (document.getElementById("printRubric"))
	{
		document.getElementById("printRubric").onclick = function()
		{
			temp = document.forms[0].f.value;
			tempa = document.forms[0].action;
			document.forms[0].f.value = "teacher_manageRubrics_viewRubric";
			document.forms[0].target = "_blank";
			document.forms[0].action = document.forms[0].action + "?showBody=false";
			document.forms[0].submit();
			document.forms[0].f.value = temp;
			document.forms[0].action = tempa;
			document.forms[0].target = "_self";
		}
	}
	
	// print button for administration rubric
	if (document.getElementById("printAdministrationRubric"))
	{
		document.getElementById("printAdministrationRubric").onclick = function()
		{
			temp = document.forms[0].f.value;
			tempa = document.forms[0].action;
			document.forms[0].f.value = "administration_manageRubrics_viewRubric";
			document.forms[0].target = "_blank";
			document.forms[0].action = document.forms[0].action + "?showBody=false";
			document.forms[0].submit();
			document.forms[0].f.value = temp;
			document.forms[0].action = tempa;
			document.forms[0].target = "_self";
		}
	}
	
	// print button for editmarks
	if (document.getElementById("printEditmark"))
	{
		document.getElementById("printEditmark").onclick = function()
		{
			temp = document.forms[0].f.value;
			document.forms[0].f.value = "teacher_manageEditmarks_viewEditmarks";
			document.forms[0].target = "_blank";
			document.forms[0].action = document.forms[0].action + "?showBody=false";
			document.forms[0].submit();
			document.forms[0].f.value = temp;
			document.forms[0].target = "_self";
		}
	}
	
	// print button for comments
	if (document.getElementById("printComment"))
	{
		document.getElementById("printComment").onclick = function()
		{
			temp = document.forms[0].f.value;
			document.forms[0].f.value = "teacher_manageComments_viewComments";
			document.forms[0].target = "_blank";
			document.forms[0].action = document.forms[0].action + "?showBody=false";
			document.forms[0].submit();
			document.forms[0].f.value = temp;
			document.forms[0].target = "_self";
		}
	}
	
	// toggle student check boxes
	if (document.getElementById("toggleStudents"))
	{
		document.getElementById("toggleStudents").onclick = function()
		{
			for (i=0; i < document.forms[0].elements.length; i++)
			{
				if (document.forms[0].elements[i].name == "fStudentID")
				{
					if (document.forms[0].elements[i].checked == true)
						document.forms[0].elements[i].checked = false;
					else
						document.forms[0].elements[i].checked = true;
				}
			}
		}
	}
	
	// search function
	/*
	if (document.getElementById("searchButton"))
	{
		document.getElementById("searchButton").onclick = function()
		{
			try{
				if (document.searchForm.searchStr.value.length)
				{
					for (i=0; i < document.searchForm.searchType.length; i++)
					{
						if (document.searchForm.searchType[i].checked)
							searchType = document.searchForm.searchType[i].value;
					}
					searchStr = escape(document.searchForm.searchStr.value);
			    	searchUrl = "index.cfm?f=search_results&searchStr=" + searchStr + "&searchType=" + searchType;
					location.href = searchUrl;
				}
			}catch(e){}
		}
	}
	*/

	//help function
	if (document.getElementById("help"))
	{
		if (ie)
		{
			document.getElementById("help").onmouseover = function()
			{
				updateHelp(this);
			}
			document.getElementById("help").onmouseout = function()
			{
				ihideHelp();
			}
		}
		else
		{
			document.getElementById("help").addEventListener("mouseover", updateHelp, false);
			document.getElementById("help").addEventListener("mouseout", ihideHelp, false);
		}
	}
	/*
	// show cal
	if (document.getElementById("fTriggerCal"))
	{
		document.getElementById("fTriggerCal").onfocus = function()
		{
			document.getElementById("wcCal").style.display = "block";
		}
		document.getElementById("fCloseCal").onclick = function()
		{
			document.getElementById("wcCal").style.display = "none";
		}
	}
	
	// show cal - second trigger
	if (document.getElementById("fTriggerCal2"))
	{
		document.getElementById("fTriggerCal2").onfocus = function()
		{
			document.getElementById("wcCal2").style.display = "block";
		}
		document.getElementById("fCloseCal2").onclick = function()
		{
			document.getElementById("wcCal2").style.display = "none";
		}
	}
	
	// prev next for calendar
	if (document.getElementById("fChangeCalp"))
	{
		document.getElementById("fChangeCalp").onclick = function()
		{
			thisFunc(this);
		}
		document.getElementById("fChangeCaln").onclick = function()
		{
			thisFunc(this);
		}
		
		function thisFunc(obj)
		{
			document.forms[0].dateOpen.value = true;
			document.forms[0].current_date.value = obj.name;
			document.forms[0].f.value = document.forms[0].f.value.split(".")[0];
			document.forms[0].submit();
		}
	}
	*/
	// disable submit for trial request
	if (document.getElementById("trialSubmit"))
	{
		document.forms[0].onsubmit = function()
		{
			setTimeout("this.submit()", 10);
			document.getElementById("trialSubmit").disabled = true;
		}
	}
	
	//start gradebook
	if (document.getElementById("gradeMatrix"))
	{
		flag = true;
		if (ie && this.event.type == "resize")
			flag = false;
			
		if (flag)
			initSort();
	}
	
	// dictionary highlighting
	if (document.getElementById("dictionary"))
	{
		d = document.getElementById("dictionary");
		spans = d.getElementsByTagName("span");
		
		for (s=0; s < spans.length; s++)
		{
			if (spans[s].className == "sxTag")
			{
				try{
				theText = spans[s].innerHTML;
				spans[s].innerHTML = "";
				a = document.createElement("a");
				a.href= "index.cfm?f=search_results&searchType=Dictionary&searchStr=" + theText;
				a.innerHTML = theText;
				a.style.fontWeight = "bold";
				spans[s].appendChild(a);
				}catch(e){}
			}
		}
	}
	
	// dictionary highlighting : synonyms
	if (document.getElementById("dictionary"))
	{
		dd = document.getElementById("dictionary");
		spans = dd.getElementsByTagName("span");
		
		for (s=0; s < spans.length; s++)
		{
			if (spans[s].className == "scTag")
			{
				theCapText = spans[s].innerHTML;
				spans[s].innerHTML = "";
				a = document.createElement("a");
				a.href= "index.cfm?f=search_results&searchType=Dictionary&searchStr=" + theCapText;
				a.innerHTML = theCapText;
				a.style.fontWeight = "bold";
				spans[s].appendChild(a);
			}
		}
	}
	
	// dictionary highlighting : rsynonyms see : ssTag
	if (document.getElementById("dictionary"))
	{
		ss = document.getElementById("dictionary");
		spans = ss.getElementsByTagName("span");
		
		for (s=0; s < spans.length; s++)
		{
			if (spans[s].className == "ssTag")
			{
				var ssa = spans[s].innerHTML.split(",");
				spans[s].innerHTML = "synonyms see ";
				spans[s].style.fontStyle = "italic";
				if (spans[s].innerHTML.indexOf("synonyms") != -1)
				{
					spans[s].innerHTML = spans[s].innerHTML.replace("synonyms","<strong>synonyms</strong>");
				}
				
				for (is = 0; is < ssa.length; is++)
				{
					theSSText = ssa[is];
					a = document.createElement("a");
					a.href= "index.cfm?f=search_results&searchType=Dictionary&searchStr=" + theSSText;
					a.innerHTML = theSSText;
					a.style.fontWeight = "bold";
					a.style.marginRight = "5px";
					a.style.textTransform = "uppercase";
					a.style.fontSize = "x-small";
					a.style.fontStyle = "normal";
					spans[s].appendChild(a);
				}
			}
		}
	}
	
	// dictionary highlighting : related
	if (document.getElementById("dictionary"))
	{
		ddd = document.getElementById("dictionary");
		spans = ddd.getElementsByTagName("span");
		
		for (s=0; s < spans.length; s++)
		{
			if (spans[s].className == "rxtTag")
			{
				theRelatedText = spans[s].innerHTML;
				spans[s].innerHTML = "";
				a = document.createElement("a");
				a.href= "index.cfm?f=search_results&searchType=Dictionary&searchStr=" + theRelatedText;
				a.innerHTML = theRelatedText;
				a.style.fontWeight = "bold";
				spans[s].appendChild(a);
			}
		}
	}
	
	// dictionary highlighting : compare, dx - dxt - dxn -
	if (document.getElementById("dictionary"))
	{
		if (ie)
		{
			_dictionary = document.getElementById("dictionary");
			spans = _dictionary.getElementsByTagName("SPAN");
		}
		else
		{
			dictionary = document.getElementById("dictionary");
			spans = dictionary.getElementsByTagName("SPAN");
		}
		
		var cnt = 0;
		for (s=0; s < spans.length; s++)
		{
			// find parent dx tag
			if (spans[s].id == "dx")
			{
				chNodes = spans[s].childNodes;
				for (ch=0; ch < chNodes.length; ch++)
				{
					if (chNodes[ch].nodeType == 1)
					{
						chhNodes = chNodes[ch].childNodes;
						for (chh=0; chh < chhNodes.length; chh++)
						{
							if (chhNodes[chh].nodeType == 3 && chhNodes[chh].nodeValue.length)
							{
								inst = chhNodes[chh].nodeValue.split(",");
								contSpan = document.createElement("SPAN");
								
								for (i=0; i < inst.length; i++)
								{
									if (i != 0)
									{
										comma = document.createElement("SPAN");
										comma.innerHTML = ", ";
										contSpan.appendChild(comma);
									}
									a = document.createElement("A");
									a.innerHTML = inst[i];
									a.className = "upper";
									a.href = "index.cfm?f=search_results&searchType=Dictionary&searchStr=" + inst[i];
									contSpan.appendChild(a);
								}
								space = document.createElement("SPAN");
								space.innerHTML = "&nbsp;";
								contSpan.appendChild(space);
								chhNodes[chh].parentNode.replaceChild(contSpan, chhNodes[chh]);
							}						
						}
					}
					if (chNodes[ch].nodeType == 3 && chNodes[ch].nodeValue.length && cnt != 0)
					{
						chNodes[ch].nodeValue = "; " + chNodes[ch].nodeValue;
					}
				}
				cnt++;
			}
		}
	}
	
	// submit event for import 
	if (document.getElementById("importSubmit"))
	{
		thisForm = document.forms[0];
		thisForm.onsubmit = function(){return false;}
		document.getElementById("importSubmit").onclick = function()
		{
			// hide select objects
			thisForm.getElementsByTagName("SELECT")[0].style.visibility = "hidden";
			loadObj = document.getElementById("importLoadingScreen");
			wObj = document.getElementById("workspace");
			
			if (ie)
				loadObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=95)";
			else
				loadObj.style.opacity = "0.95";
			
			loadObj.style.position = "absolute";
			loadObj.style.display = "block";
			loadObj.style.top = "50px";
			loadObj.style.left = 0;
			loadObj.style.width = wObj.offsetWidth + "px";
			loadObj.style.background = "#fff";
			
			thisForm.submit();
		}
	}
	
	// word count
	if (document.getElementById("wordCount"))
	{
		function replaceAll(oldStr, findStr, repStr) 
		{
		  var srchNdx = 0;  
		  var newStr = ""; 
		  while (oldStr.indexOf(findStr,srchNdx) != -1)  
		  {
		    newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
		    newStr += repStr;
		    srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);       
		  }
		  newStr += oldStr.substring(srchNdx,oldStr.length);           
		  return newStr;
		}
		var wcto = null;
		go = function()
		{
			words = document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML.replace(/<[^>]+>/g,"");
			words = replaceAll(words,"&nbsp;","");
			document.getElementById("wordCount").innerHTML = (words.split(" ").length - 2);
			if (wcto){clearTimeout(wc);}
			wc = setTimeout("go()", 100);
		}
		if (wcto){clearTimeout(wc);}
		wc = setTimeout("go()", 1000);
	}
	
	// send back shared essay (student to student)
	if (document.getElementById("iReturnShare"))
	{
		document.getElementById("iReturnShare").onclick = function()
		{
			confirmT = confirm("Are you sure you want to return this essay?");
			if (confirmT)
			{
				document.forms[0].returnShare.value = "TRUE";
				document.getElementsByTagName("input")[1].value = document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML;
				document.forms[0].submit();
			}
		}
	}
	
	// send back essay without a grade
	/*if (document.getElementById("iReturnNoGrade"))
	{
		document.getElementById("iReturnNoGrade").onclick = function()
		{
			document.forms[0].returnNoGrade.value = "TRUE";
			document.forms[0].submit();
		}
	}*/
	
	// size subjet node length
	sizeSubject = function()
	{
		if (document.getElementById("table_matrix"))
		{
			sps = document.getElementsByTagName("span");
			for (i=0; i < sps.length; i++)
			{
				workspaceWidth = document.getElementById("workspace").clientWidth;
				if (sps[i].className == "summary")
				{
					// get ANCHOR text
					for (tt=0; tt < sps[i].parentNode.childNodes.length; tt++)
					{
						if (sps[i].parentNode.childNodes[tt].tagName == "A")
						{
							temp = sps[i].parentNode.childNodes[tt].innerHTML.length;
						}
					}
					num = ((Math.pow(workspaceWidth,3)) / Math.pow(8,7.5) - temp);
					sps[i].innerHTML = left(sps[i].title, num) + "...";
					//document.getElementById("status").innerHTML = temp;
				}
			}
		}
	}
	setTimeout("sizeSubject()",10);
	
	loaded[0] = 1;
}

//Daniel Arlitt - 11/07/2009 help box

var fitHelpBoxToViewport = function() {
	var obj = $('#helpBox');
	obj.css('top','-21px');
	$('#helpBox .helpContent').css('height','250px');
	if ( obj.is(':visible') ) {
		var offset = obj.offset();
		var cssTop = parseInt(obj.css('top'));
		if ( obj.height() > $(window).height() ) {
			var difference = parseInt($('#helpBox .helpContent').css('height')) - (obj.height() - $(window).height());
			if (difference < 100) {
				difference = 100;
			}
			$('#helpBox .helpContent').css('height',difference + 'px');
		}
		
		difference = $(window).height() - (obj.height() + offset.top + 10);
		if ( difference < 0 ) {
			var newTop = cssTop + difference;
			if ( offset.top - Math.abs(difference) < 0 ) {
				// the difference will push the object off the top of the page so use offset.top instead
				newTop = cssTop - offset.top;
			}
			obj.css('top',newTop + 'px');
			
		}
	}
}

$(document).ready(function() {					   
	/**********************************************************************************/
	/* TEACHER VIEW ONLY events to edit mark lists in the sidebar of the essay editor */
	/**********************************************************************************/
	$("#editmarkContainer").accordion({
		autoHeight: false,
		collapsible: true
	});
	/*$(".editMarkHeader").next().hide();
	$(".editMarkHeader").bind('click',function() {
		$(this).next().slideToggle('fast');
	});*/
	
	/*$(".editMarkItem").each(function() {
		$(this).unbind('click');
		$(this).bind('click',function() {
			insertEditmark($(this).get(0));
		});
	});
	
	$(".editMarkItemIcon").each(function() {
		$(this).unbind('click');
		var counter = 0;
		$(this).bind('click',function() {
			insertEditmarkIcon($(this).get(0));
		});
	});*/
	
	/************************************************************************/
	/* STUDENT VIEW ONLY events for the help box in the student's exercises */
	/************************************************************************/
	$("#helpBoxContainer .helpTab").css('cursor','pointer');
	
	// move the help window to fit the viewport
	$(window).resize(function() {
		fitHelpBoxToViewport();
	});
	
	$("body").bind('click',function(e) {
		var targ = $(e.target);
		var toggleHelp = false;
		var closeHelp = true;
		for (var i = 0; i < targ.parents().length; i ++) {
			if (i > 0) {
				targ = targ.parent()
			}
			if (targ.hasClass('helpTab')) {
				toggleHelp = true;
				break;
			}
			if (targ.attr('id') == 'helpBox') {
				closeHelp = false;
				break;
			}
		}
		if (toggleHelp) {
			$('#helpBox').toggle();
		} else if (closeHelp) {
			$('#helpBox').hide();
		}
	});
	
	if ( $.browser.msie && parseFloat($.browser.version) < 7 ) {
		var helpPosition = function() {
			$('#helpBoxContainer').css({
									   'position':'absolute',
									   'top':'170px',
									   'right':'190px'
									   });
			$('#helpBoxContainer').css('top', ($(this).scrollTop()+170) + "px");
		}
		$(window).scroll(function() {
			helpPosition();
		});
		$('#helpBox').show();
		helpPosition();
		$('#helpBox').hide();
	}
	
	$("#helpBox .helpTitle").bind('click',function() {
		var visible = false;
		if ( $(this).next().is(':visible') ) {
			visible = true;
		}
		$("#helpBox .helpContent:visible").slideUp('fast');
		if ( !visible ) {
			$(this).next().slideDown('fast',fitHelpBoxToViewport);
		} else {
			$("#helpBox .helpContent:visible").slideUp('fast');
			$('#helpBox').css('top','-21px');
		}
	}).css('cursor','pointer');
	
	$(".dateField").datepicker();
});