//Chairperson Tool

function showchairpersontoolbox(obj)
{
    var strID = obj.id;
    var strtoolboxID = strID.replace("chairperson","chairpersontoolbox");
	var objtoolbox = document.getElementById(strtoolboxID);

	objtoolbox.style.display="";
	var objtoolboxcopy = objtoolbox;
	while(objtoolboxcopy.previousSibling)
    {
    	if(objtoolboxcopy.previousSibling.id.indexOf("chairpersontoolbox") != -1)
    	{
    		objtoolboxcopy.previousSibling.style.display="none";
    	}
    	objtoolboxcopy =objtoolboxcopy.previousSibling;
    }
    
    var objtoolboxcopy = objtoolbox;
    while(objtoolboxcopy.nextSibling)
    {
    	if(objtoolboxcopy.nextSibling.id.indexOf("chairpersontoolbox") != -1)
    	{
    		objtoolboxcopy.nextSibling.style.display="none";
    	}
    	objtoolboxcopy =objtoolboxcopy.nextSibling;
    }
	
	
    obj.className="chairperson-nav-item selected";
    var elementcopy = obj;
    while(elementcopy.previousSibling)
    {
    	elementcopy.previousSibling.className="chairperson-nav-item";
    	elementcopy=elementcopy.previousSibling;
    }
    
    var elementcopy = obj;
    while(elementcopy.nextSibling)
    {
    	elementcopy.nextSibling.className="chairperson-nav-item";;
    	elementcopy=elementcopy.nextSibling;
    }
}

function showchairpersonquestions(obj, chairpersonid)
{
	obj.parentNode.className = "selected";
	obj.parentNode.previousSibling.className="";
	var toolbox = document.getElementById("chairpersontoolbox_" + chairpersonid);
	var toolboxchilddivs = toolbox.getElementsByTagName("div");
	var target;
	for(var i=0; i<toolboxchilddivs.length; i++)
	{
	  	target= toolboxchilddivs[i];
		if(target.id == "chairperson-dialog-introduction-tab")
		{
			target.style.display="none";
		}
		
		if(target.id == ("chairperson-dialog-questions-tab_" +chairpersonid))
		{
			target.style.display="";
		}
	}
}

function showchairpersonintro(obj, chairpersonid)
{
	obj.parentNode.className = "selected";
	obj.parentNode.nextSibling.className="";
	var toolbox = document.getElementById("chairpersontoolbox_" + chairpersonid);
	var toolboxchilddivs = toolbox.getElementsByTagName("div");
	var target;
	for(var i=0; i<toolboxchilddivs.length; i++)
	{
	  	target= toolboxchilddivs[i];
		if(target.id == "chairperson-dialog-introduction-tab")
		{
			target.style.display="";
		}
		
		if(target.id == ("chairperson-dialog-questions-tab_"+ chairpersonid))
		{
			target.style.display="none";
		}
	}
}

function OnGetQuestionsComplete(result,context)
{
	var x = result.split("__");
	var chairpersonid = x[0];
	
	var parentDIV = document.getElementById("chairperson-dialog-questions-tab_" + chairpersonid);
	var parentDIVchild = parentDIV.getElementsByTagName("ol");
	var parentOL;
	for(var i=0; i<parentDIVchild.length; i++)
	{
		if(parentDIVchild[i].id=="chairperson-questions")
		{
			parentOL=parentDIVchild[i];
		}
	}
	 
	var len = parentOL.childNodes.length;
	while(parentOL.hasChildNodes())
	{
		parentOL.removeChild(parentOL.firstChild);
	}
	for(var i=1; i<x.length; i++)
	{
		var newLI = createquestionLI(x[i],i);
		parentOL.appendChild(newLI);
	}
}


function createquestionLI(QuestionPair, Order)
{
	var y = QuestionPair.split("##");
	var QuestionNum = y[0];
	var Question = y[1];
	var UserID = y[2]
	var ActionsValue = y[3];
	
	var newLI =document.createElement("li");
	newLI.className ="chairperson-question question ";
	newLI.id = "chairperson-question-" + QuestionNum ;
	
	
	if (ActionsValue!= null)
	{
		if (ActionsValue.charAt(0)=="1")
		{
			newLI.className +=" question-completed";
		}
		
		if (ActionsValue.charAt(1)=="1")
		{
			newLI.className +=" question-flagged";
		}
	}
	
	var newH2 = createquestion(Question,Order);
	var newUL = createquestionaction(QuestionNum,UserID);
	var newDIV = createquestionnote(QuestionNum,UserID,ActionsValue);
	newLI.appendChild(newH2);
	newLI.appendChild(newUL);
	newLI.appendChild(newDIV);
	return newLI;
}



function createquestion(Question,Order)
{
	var newH2 = document.createElement("h2");
	var newQuestionNumSPAN =document.createElement("span");
	newQuestionNumSPAN.className ="questions-no";
	newQuestionNumSPAN.appendChild(document.createTextNode("Q"+Order));
	
	var newQuestionSPAN = document.createElement("span");
	newQuestionSPAN.className ="questions-question";
	newQuestionSPAN.appendChild(document.createTextNode(Question));
	newH2.appendChild(newQuestionNumSPAN);
	newH2.appendChild(newQuestionSPAN);
	
	return newH2;
}


function createquestionaction(QuestionNum,UserID)
{

	var newUL = document.createElement("ul");
	newUL.className = "chairperson-question-actions actions";
	
	var newComplete =document.createElement("li");
	newComplete.className = "chairperson-questions-action-complete";
	newComplete.id ="CQcomplete-" + QuestionNum;
	newComplete.onclick =function(){CQAction("cmdToggleComplete",newComplete.id,QuestionNum, UserID);};
	var newFlag =document.createElement("li");
	newFlag.id="CQflag-" + QuestionNum;
	newFlag.className = "chairperson-questions-action-flag";
	newFlag.onclick=function(){CQAction("cmdToggleFlag",newFlag.id,QuestionNum, UserID);};	
	var newNote =document.createElement("li");
	newNote.id = "CQnote-" + QuestionNum;
	newNote.className = "chairperson-questions-action-note";
	newNote.onclick =function(){toggleelementdisplaywarn("chairperson-questions-answer-" + QuestionNum);};
	
	newUL.appendChild(newComplete);
	newUL.appendChild(newFlag);
	newUL.appendChild(newNote);
	
	return newUL;
 
}

function createquestionnote(QuestionNum,UserID,ActionsValue)
{
	var newDIV = document.createElement("div");
	newDIV.className = "chairperson-questions-answer";
	newDIV.id = "chairperson-questions-answer-" + QuestionNum;
	newDIV.style.display ="none";
	
	var newTEXT = document.createElement("textarea");
	newTEXT.id = "chairperson-question-text-" + QuestionNum;
	
	if(ActionsValue !=null)
	{
		newTEXT.appendChild(document.createTextNode(ActionsValue.substring("2")));
	}
	
	var newUL = document.createElement("ul");
	newUL.className = "chairperson-questions-answer-actions actions";
	var newSave = document.createElement("li");
	newSave.className = "chairperson-questions-save";
	newSave.appendChild(document.createTextNode("Save"));
	newSave.onclick=function(){CQAction("cmdSaveNote",newTEXT.id,QuestionNum, UserID);};	
	var newCancel = document.createElement("li");
	newCancel.className = "chairperson-questions-cancel";
	newCancel.appendChild(document.createTextNode("Cancel"));
	newCancel.onclick =function(){toggleelementdisplaywarn("chairperson-questions-answer-" + QuestionNum);};
	newUL.appendChild(newSave);
	newUL.appendChild(newCancel);

	
	newDIV.appendChild(newTEXT);
	newDIV.appendChild(newUL);
	
	return newDIV;
	
}

function CQAction(mode, elementID,QuestionNum, UserID)
{
	var obj = document.getElementById(elementID);
	
	if (mode =="cmdToggleComplete")
	{
		var originalClassName = obj.parentNode.parentNode.className
		obj.parentNode.parentNode.className = obj.parentNode.parentNode.className + " question-loading";
		ajaxCQtogglecomplete("cmdToggleComplete##;;" + QuestionNum +"##;;"+UserID);
		if(originalClassName.indexOf("question-completed") == -1)
		{
			obj.parentNode.parentNode.className =originalClassName+ " question-completed";
		}
		else
		{
			obj.parentNode.parentNode.className =originalClassName.replace("question-completed","")
		}				
	}
	
	if (mode =="cmdToggleFlag")
	{
		var originalClassName = obj.parentNode.parentNode.className
		obj.parentNode.parentNode.className = obj.parentNode.parentNode.className + " question-loading";
		ajaxCQtoggleflag("cmdToggleFlag##;;" + QuestionNum +"##;;"+UserID);
		if(originalClassName.indexOf("question-flagged") == -1)
		{
			obj.parentNode.parentNode.className =originalClassName+ " question-flagged";
		}
		else
		{
			obj.parentNode.parentNode.className =originalClassName.replace("question-flagged","")
		}			
	}
	
	if (mode =="cmdSaveNote")
	{
		//var questiontext = obj.innerText;
		var questiontext = obj.value;
		ajaxCQsavenote("cmdSaveNote##;;" + QuestionNum +"##;;"+UserID+"##;;"+questiontext);
		obj.parentNode.style.display="none";
	}

}

function OnToggleCompleteComplete(result,context)
{
	
}

function OnToggleFlagComplete(result,context)
{
}

function OnSaveNoteComplete(result,context)
{
}

function toggleelementdisplaywarn(elementID)
{
	var obj = document.getElementById(elementID);
	if(obj.style.display =="")
	{
		if(confirm("Are you sure you want to cancel editing and discard changes?"))
		{
			obj.style.display ="none";
		}
	}
	else
	{
		obj.style.display="";
	}
	//obj.style.display = (obj.style.display != 'none' ? 'none' : '' );
}

function OnErrorOccurs(err)
{
	window.status = err;
}



//End Chairperson Tool

// for content nav bar on principle pages
function contentShow(){
	var dropdownDiv = document.getElementById("contentDisplay");
	
	if (dropdownDiv.style.display == "none")
	{
    	dropdownDiv.style.display = "block";
    }
    else
    {
        dropdownDiv.style.display = "none";
    }
}  
// end Content nav Bar

/*Email This Page Dialogbox*/
function showemailpagedialogbox(obj, usrName, usrNameBoxID){
    var container = document.getElementById("emailPageContainer");
    container.style.display = "";
    obj.className = "selected";
    
    //populate default sender name
    var usrNameBox = document.getElementById(usrNameBoxID);
    var nameVal = (usrNameBox.value).replace(/^\s+|\s+$/g, '');
    if(nameVal == ""){
    	usrNameBox.value = usrName;
    }
    
    //clear validation
    ClearValidationMsgs();
}

function OnEmailThisPageComplete(result, context){
	document.getElementById("emailPageContainer").style.display = "none";
	ClearEmailThisPageControls();
}

function hideVldMessage(obj){
	if(!obj.isvalid){
		obj.style.visibility = "hidden";
		obj.isvalid = true;
		obj.errormessage = "";
	}
}

function closeEmailPageContainer(obj){
    var container = document.getElementById("emailPageContainer");
    container.style.display = "none";
}
/*End Email This Page Dialogbox*/

/*Quicklinks Dialogbox*/
function showquicklinksdialogbox(obj){
	var container = document.getElementById("quickLinksContainer");
    container.style.display="";
    obj.className="selected";
}

function closequicklinksdialogbox(obj){
	var container = document.getElementById("quickLinksContainer");
    container.style.display = "none";
}

function OnQuickLinksTaskComplete(result, context){
	var parentDIV = document.getElementById("quickLinks-dialog-tab");
	var parentDIVchild = parentDIV.getElementsByTagName("ol");
	var parentOL;
	for(var i=0; i<parentDIVchild.length; i++){
		if(parentDIVchild[i].id=="quiz-quicklinks"){
			parentOL=parentDIVchild[i];
		}
	}
	
	var len = parentOL.childNodes.length;
	while(parentOL.hasChildNodes()){
		parentOL.removeChild(parentOL.firstChild);
	}
	
	var links = result.split(":ql:");
	for(var i=1; i<links.length; i++){
		var newLI = creatQuickLinkLi(links[i]);
		parentOL.appendChild(newLI);
	}
}

function creatQuickLinkLi(quickLink){
	var linkValues = quickLink.split(":qli:");

	var linkId = linkValues[0];
	var linkUrl = linkValues[1];
	var linkTitle = linkValues[2];
	var linkDate = linkValues[3];
	
	var newLi = document.createElement("li");
	newLi.className = "quiz-quicklinks-link";
	newLi.id = "quicklink";
	
	var newH3 = createQuiklinkContent(linkUrl, linkTitle, linkDate);
	var newUl = createQuicklinkRemoveAction(linkId);
	
	newLi.appendChild(newH3);
	newLi.appendChild(newUl);
	
	return newLi
}

function createQuiklinkContent(linkUrl, linkTitle, linkDate){
	var newH3 = document.createElement("h3");
	newH3.className = "quiz-quicklinks-title";
	
	var quickLinkSpan = creatNewQuickLinkSpan(linkUrl, linkTitle);
	var qickLinkDateSpan = createNewQuickLinkDateSpan(linkDate);
	
	newH3.appendChild(quickLinkSpan);
	newH3.appendChild(qickLinkDateSpan);
	
	return newH3;
}

function creatNewQuickLinkSpan(linkUrl, linkTitle){
	var newQLSpan = document.createElement("span");
	newQLSpan.className = "quiz-quicklinks-title";
	
	var quickLinkUrl = createQuickLinkUrl(linkUrl, linkTitle);
	
	newQLSpan.appendChild(quickLinkUrl);
	
	return newQLSpan;
}

function createQuickLinkUrl(url, title){
	var newUrl = document.createElement("a");
	newUrl.setAttribute('href', url);
	newUrl.setAttribute('target', '_blank');
	newUrl.innerHTML = title;
	
	return newUrl;
}

function createNewQuickLinkDateSpan(date){
	var newQLDateSpan = document.createElement("span");
	newQLDateSpan.className = "quiz-quicklinks-link-date";
	newQLDateSpan.appendChild(document.createTextNode(date));
	
	return newQLDateSpan
}

function createQuicklinkRemoveAction(linkId ){
	var removeQlUl = document.createElement("Ul");
	removeQlUl.onclick = function(){ajaxqucklinkcommand(":arg:Remove:arg:" + linkId);};
	removeQlUl.className = "quiz-quicklinks-actions actions";
	
	var removeQlLi = createQlRemoveLi(linkId);
	removeQlUl.appendChild(removeQlLi);
	
	return removeQlUl
}

function createQlRemoveLi(linkId){
	var newLi = document.createElement("Li");
	newLi.className = "quiz-quicklinks-action-delete";
	
	var newSpan = createRemoveSpan(linkId);
	newLi.appendChild(newSpan);
	
	return newLi;
}

function createRemoveSpan(linkId){
	var newSpan = document.createElement("span");
	newSpan.appendChild(document.createTextNode("Remove"));
	
	return newSpan;
}
/*End Quicklinks Dialogbox*/

/*Quiz*/
function showquizconsole()
{
	document.getElementById("quizconsole").style.display="";
}

function showquiztab(obj,id)
{
	obj.parentNode.className ="selected";
	var LIcopy = obj.parentNode;
	while(LIcopy.previousSibling)
    {
    	LIcopy.previousSibling.className = "";
    	LIcopy=LIcopy.previousSibling;
    }
    
	var LIcopy=obj.parentNode;
    while(LIcopy.nextSibling)
    {
    	LIcopy.nextSibling.className ="";
    	LIcopy=LIcopy.nextSibling;
    }

	var tab = document.getElementById(id);
	tab.style.display="";
	var tabcopy = tab;
	while(tabcopy.previousSibling)
    {
    	tabcopy.previousSibling.style.display="none";
    	tabcopy=tabcopy.previousSibling;
    }
    
	var tabcopy = tab;
    while(tabcopy.nextSibling)
    {
    	tabcopy.nextSibling.style.display="none";
    	tabcopy=tabcopy.nextSibling;
    }


}

function OnGetQZQuestionsComplete(result,context)
{
	if (result=="")
	{
		document.getElementById("quiz-no-current-principle").style.display="";
		document.getElementById("quiz-current-principle-topics").style.display="none";
		return;
	}
	
	var y = result.split("##*");
	var userID = y[0];
	var principleID = y[1];
	var x = y[2].split("##$");
	var parentLI = document.getElementById("quiz-topic-principlename");
	
	var parentDIVchild = parentLI.getElementsByTagName("div");
	var len = parentDIVchild.length;
	for(var i=0; i<len; i++)
	{
		parentLI.removeChild(parentDIVchild[0]);
	}
	 
	for(var i=0; i<x.length; i++)
	{
		var newDIV = createqzquestionDIV(x[i],i,"C");
		parentLI.appendChild(newDIV);
	}
	
	/*Add Check Answer Button*/
	var parentOL = document.getElementById("quiz-currrent-check-answers");
	while(parentOL.hasChildNodes())
	{
		parentOL.removeChild(parentOL.firstChild);
	}
	var newLI = document.createElement("li");
	newLI.className="quiz-questions-action-mark";
	newLI.appendChild(document.createTextNode("Check Answers"));
	newLI.onclick=function(){CheckCurrentQZAnswer(userID,principleID);};
	parentOL.appendChild(newLI);
}

function createqzquestionDIV(x,i,mode)
{
	var y= x.split("##_");
	var QuestionID = y[0];
	var Question = y[1];
	var Answers = y[2].split("%%%");	
	
	var newDIV =document.createElement("div");
	newDIV.className="question";
	newDIV.id = mode+"quiz-question-" + QuestionID;
	
	var newH3 =document.createElement("h3");
	var newSPANNo =document.createElement("span");	
	newSPANNo.className = "questions-no";
	newSPANNo.appendChild(document.createTextNode("Q" + (i+1)));
	var newSPANQ = document.createElement("span");
	newSPANQ.className = "questions-question"
	newSPANQ.appendChild(document.createTextNode(Question));
	newH3.appendChild(newSPANNo);
	newH3.appendChild(newSPANQ);
	
	var newUL = document.createElement("ul");
	newUL.className = "quiz-question-answers";
	for(var i=0; i<Answers.length;i++)
	{
		var newLI = createqzanswerLI(Answers[i],QuestionID,i,mode);
		newUL.appendChild(newLI);
	}
	
	newDIV.appendChild(newH3);
	newDIV.appendChild(newUL);
	
	return newDIV;
	
}

function createqzanswerLI(Answer, QuestionID,i,mode)
{
	var newLI = document.createElement("li");
	newLI.className = "quiz-questions-answer";
	newLI.id=mode+"quiz-question-"+QuestionID+"-answer-"+(i+1);
	
	var newLABEL = document.createElement("label");
	var newINPUT;
	try
	{
		 newINPUT= document.createElement("<input type=\"radio\" name=\"" + mode+"quiz-radio-"+QuestionID+"-answer-"+(i+1) +"\" />");
	}
	catch(err)
	{
		newINPUT=document.createElement('input');
	}
	
	newINPUT.setAttribute("type","radio");
	newINPUT.setAttribute("name",mode+"quiz-radio-"+QuestionID+"-answer-"+(i+1));
	newINPUT.id = mode+"quiz-radio-"+QuestionID+"-answer-"+(i+1);
	newINPUT.onclick =function(){changeQZAnsFocus(newLI.id);};
	var newSPAN = document.createElement("span");
	newSPAN.className ="quiz-questions-answer-title";
	newSPAN.appendChild(document.createTextNode(Answer));
	
	newLABEL.appendChild(newINPUT);
	newLABEL.appendChild(newSPAN);
	
	newLI.appendChild(newLABEL);
	return newLI;
	
}

function changeQZAnsFocus(id)
{
	var targetLI = document.getElementById(id);
	targetLI.className += " selected";
	var LIcopy = targetLI ;
	while(LIcopy.previousSibling)
    {
    	LIcopy.previousSibling.className = LIcopy.previousSibling.className.replace("selected","");
    	LIcopy.previousSibling.firstChild.firstChild.checked =false;
    	LIcopy=LIcopy.previousSibling;
    }
    
	var LIcopy=targetLI;
    while(LIcopy.nextSibling)
    {
    	LIcopy.nextSibling.className = LIcopy.nextSibling.className.replace("selected","");
    	LIcopy.nextSibling.firstChild.firstChild.checked =false;
    	LIcopy=LIcopy.nextSibling;
    }
}

function OnGetQZAllQuestionsComplete(result,context)
{
	var y = result.split("##!");
	var userID = y[0];
	var principle = y[1].split("##&");
	
	var parentOL = document.getElementById("quiz-all-priciples-questions");
	while(parentOL.hasChildNodes())
	{
		parentOL.removeChild(parentOL.firstChild);
	}
	
	for(var i=0; i<principle.length; i++)
	{
		var newLI = createqzPrincipleQs(principle[i]);
		parentOL.appendChild(newLI);
	}
	
	/*Add Check Answer Button*/
	var parentOL = document.getElementById("quiz-all-check-answers");
	while(parentOL.hasChildNodes())
	{
		parentOL.removeChild(parentOL.firstChild);
	}
	var newLI = document.createElement("li");
	newLI.className="quiz-questions-action-mark";
	newLI.appendChild(document.createTextNode("Check Answers"));
	newLI.onclick=function(){CheckAllQZAnswers(userID);};
	parentOL.appendChild(newLI);

}

function createqzPrincipleQs(principle)
{
	var x = principle.split("##*");
	var principleID = x[0];
	var principleName= x[1];
	var principleCaption = x[2];
	var questions = x[3].split("##$");
	
	var newLI= document.createElement("li");
	newLI.id="quiz-topic-" + principleID;
		
	var newH2 = document.createElement("h2");
	newH2.className = "quiz-topic-title";
	newH2.appendChild(document.createTextNode(principleName));
	
	var newP = document.createElement("p");
	newP.className="quiz-topic-description";
	newP.appendChild(document.createTextNode(principleCaption));
	
	newLI.appendChild(newH2);
	newLI.appendChild(newP);
	
	for(var i=0; i<questions.length; i++)
	{
		var newDIV = createqzquestionDIV(questions[i],i,"A");
		newLI.appendChild(newDIV);
	}

	return newLI;
}

function CheckCurrentQZAnswer(userID,principleID)
{
	var parentLI = document.getElementById("quiz-topic-principlename");
	var parentDIVchild = parentLI.getElementsByTagName("div"); 
	var AnswerSet="";
	
	for(var i=0; i<parentDIVchild.length; i++)
	{
		var QuestionID = parentDIVchild[i].id.replace("Cquiz-question-","");
		
		var j=1;
		var checkedNum=0;
		while(document.getElementById("Cquiz-radio-"+QuestionID+"-answer-"+j))
		{
			var currentradio = document.getElementById("Cquiz-radio-"+QuestionID+"-answer-"+j);
			if(currentradio.checked)
			{
				checkedNum=j;
			}
			j++;
		}
		AnswerSet+=QuestionID+";"+checkedNum+"#";
	}
	
	AnswerSet = AnswerSet.slice(0,-1);
	var parameter = "cmdCheckCurrentAnswer"+"##;;" +principleID +"##;;"+userID+"##;;"+AnswerSet;
	
	ajaxCheckCurrentQZAnswer(parameter);
}

function CheckAllQZAnswers(userID)
{
	var parentOL = document.getElementById("quiz-all-priciples-questions");
	var parentLIChild= parentOL.getElementsByTagName("li"); 
	var AnswerSet="";
	
	for(var i=0; i<parentLIChild.length; i++)
	{
		var QuestionLI = parentLIChild[i];
		var parentDIVchild = QuestionLI.getElementsByTagName("div");
		
		for(var j=0; j<parentDIVchild.length; j++)
		{

			var QuestionID = parentDIVchild[j].id.replace("Aquiz-question-","");
			var x=1;
			var checkedNum=0;
			while(document.getElementById("Aquiz-radio-"+QuestionID+"-answer-"+x))
			{
				var currentradio = document.getElementById("Aquiz-radio-"+QuestionID+"-answer-"+x);
				if(currentradio.checked)
				{
					checkedNum=x;
				}
				x++;
			}
			AnswerSet+=QuestionID+";"+checkedNum+"#";
		}
	}
	AnswerSet = AnswerSet.slice(0,-1);
	var parameter = "cmdCheckAllAnswers"+"##;;" +userID+"##;;"+AnswerSet;
	
	ajaxCheckAllQZAnswers(parameter);
	
}
function OnGetCheckCurrentQZAnswerComplete(result,context)
{
	var y=result.split(";");
	var UserID = y[0];
	var PrincipleID = y[1];
	var intcorrect = y[2];
	var inttotal = y[3];
	var AnswerPairs = y[4].split("#");
	
	showquiztab(document.getElementById('quiz-dialog-nav').firstChild.childNodes[2].firstChild,'quiz-dialog-review-tab');
	document.getElementById("quiz-results-none").style.display="none";
	
	var resultDIV= document.getElementById("quiz-results");
	resultDIV.style.display="";
	document.getElementById("quiz-review-score").innerText="";
	document.getElementById("quiz-review-score-summary").innerText="";
	document.getElementById("quiz-review-score").appendChild(document.createTextNode(Math.round(intcorrect*100/inttotal) + "%"));
	document.getElementById("quiz-review-score-summary").appendChild(document.createTextNode("Correct: "+ intcorrect+ " / "+ inttotal));
	
	//Incorrect Answers
	var parentOL = document.getElementById("quiz-review-questions");
	while(parentOL.hasChildNodes()){
		parentOL.removeChild(parentOL.firstChild);
	}
	
	for(var i=0; i<AnswerPairs.length;i++)
	{
		BuildIncorrectAnswers(AnswerPairs[i],"C");
	}
	
	for(var i=0; i<AnswerPairs.length;i++)
	{
		BuildCurrentQZResults(AnswerPairs[i],"C");
	}
	document.getElementById("quiz-review-view-results").onclick= function(){showquiztab(document.getElementById('quiz-dialog-nav').firstChild.firstChild.firstChild,'quiz-dialog-current-principle-tab');};
	document.getElementById("quiz-review-try-again").onclick=function(){ToolBoxOnClickCurrentPrinciple();};
	//End Incorrect Answers

}

function BuildIncorrectAnswers(AnswerPair,mode)
{
	var y = AnswerPair.split("%");
	var QuestionID = y[0];
	var CorrectAnswer = y[1];
	var Answer = y[2];
	
	if(Answer != CorrectAnswer)
	{
		var parentOL = document.getElementById("quiz-review-questions");

		var newLI = document.createElement("li");
		newLI.className = "review-answer";
		
		var newPTitle = document.createElement("p");
		newPTitle .className = "review-answer-title";
		var newPAnswer = document.createElement("p");
		newPAnswer .className = "review-answer-yours";
		var newSPAN = document.createElement("span");
		newSPAN.className = "review-answer-answer";
		
		//newPTitle.appendChild(document.createTextNode(document.getElementById(mode+"quiz-question-"+QuestionID).firstChild.childNodes[1].innerText));
		newPTitle.appendChild(document.createTextNode(document.getElementById(mode+"quiz-question-"+QuestionID).firstChild.childNodes[1].firstChild.nodeValue));
		newPAnswer.appendChild(document.createTextNode("Your answer: "));
		
		if(Answer == "0")
		{
			newSPAN.appendChild(document.createTextNode("Not answered"));
		}
		else
		{
			//newSPAN.appendChild(document.createTextNode(document.getElementById(mode+"quiz-question-"+QuestionID+"-answer-"+Answer).firstChild.childNodes[1].innerText));
			newSPAN.appendChild(document.createTextNode(document.getElementById(mode+"quiz-question-"+QuestionID+"-answer-"+Answer).firstChild.childNodes[1].firstChild.nodeValue));
		}
		
		newPAnswer.appendChild(newSPAN);
		newLI.appendChild(newPTitle);
		newLI.appendChild(newPAnswer);
		parentOL.appendChild(newLI);	
	}
}

function BuildCurrentQZResults(AnswerPair,mode)
{
	var y = AnswerPair.split("%");
	var QuestionID = y[0];
	var CorrectAnswer = y[1];
	var Answer = y[2];
	
	var QDiv = document.getElementById(mode+"quiz-question-"+QuestionID);
	QDiv.className=QDiv.className.replace("question-incorrect","");
	
	if((Answer != CorrectAnswer)||Answer=="0")
	{
		QDiv.className+=" question-incorrect";
	}
	
}

function OnGetQZPastResultsComplete(result,context)
{
	var parentTable = document.getElementById("quiz-review-previous-results");
	
	while(parentTable.hasChildNodes()){
		parentTable.removeChild(parentTable.firstChild);
	}

	var newTHEAD = document.createElement("thead");
	var newTR = document.createElement("tr");
	var newTHPrinciple = document.createElement("th");
	var newTHDate = document.createElement("th");
	var newTHScore = document.createElement("th");
	newTHPrinciple.appendChild(document.createTextNode("Principle"));
	newTHDate.appendChild(document.createTextNode("Date"));
	newTHScore.appendChild(document.createTextNode("Score"));
	
	newTR.appendChild(newTHPrinciple);
	newTR.appendChild(newTHDate);
	newTR.appendChild(newTHScore);
	newTHEAD.appendChild(newTR);
	
	
	var newTBODY = document.createElement("tbody");
	if(result!="")
	{
	    BuildQZResultTable(result, newTBODY);
	}
	
	parentTable.appendChild(newTHEAD);
	parentTable.appendChild(newTBODY);
	
}

function BuildQZResultTable(result, newTBODY)
{
	var y= result.split("#");
	
	for(var i=0; i<y.length;i++)
	{
		x=y[i].split(";");
		
		var newTR = document.createElement("tr");
		newTR.className="quiz-review-previous-result";
		
		var newTDPrinciple =document.createElement("td");
		var newTDDate =document.createElement("td");
		var newTDScore =document.createElement("td");	
		newTDPrinciple.className="quiz-review-previous-result-principle";
		newTDDate.className="quiz-review-previous-result-date";
		newTDScore.className="quiz-review-previous-result-score";
		newTDPrinciple.appendChild(document.createTextNode(x[0]));
		newTDDate.appendChild(document.createTextNode(x[1]));
		newTDScore.appendChild(document.createTextNode(x[2] + " / " + x[3]));
		
		newTR.appendChild(newTDPrinciple);
		newTR.appendChild(newTDDate);
		newTR.appendChild(newTDScore);
		
		newTBODY.appendChild(newTR);
	}
}

function OnGetCheckAllQZAnswersComplete(result,context)
{
	var y=result.split(";");
	var UserID = y[0];
	var intcorrect = y[1];
	var inttotal = y[2];
	var AnswerPairs = y[3].split("#");
	
	showquiztab(document.getElementById('quiz-dialog-nav').firstChild.childNodes[2].firstChild,'quiz-dialog-review-tab');
	document.getElementById("quiz-results-none").style.display="none";
	
	var resultDIV= document.getElementById("quiz-results");
	resultDIV.style.display="";
	
	document.getElementById("quiz-review-score").innerText="";
	document.getElementById("quiz-review-score-summary").innerText="";
	document.getElementById("quiz-review-score").appendChild(document.createTextNode(Math.round(intcorrect*100/inttotal) + "%"));
	document.getElementById("quiz-review-score-summary").appendChild(document.createTextNode("Correct: "+ intcorrect+ " / "+ inttotal));
	
	//Incorrect Answers
	var parentOL = document.getElementById("quiz-review-questions");
	while(parentOL.hasChildNodes()){
		parentOL.removeChild(parentOL.firstChild);
	}
	
	for(var i=0; i<AnswerPairs.length;i++)
	{
		BuildIncorrectAnswers(AnswerPairs[i],"A");
	}
	
	for(var i=0; i<AnswerPairs.length;i++)
	{
		BuildCurrentQZResults(AnswerPairs[i],"A");
	}
	
	document.getElementById("quiz-review-view-results").onclick= function(){showquiztab(document.getElementById('quiz-dialog-nav').firstChild.childNodes[1].firstChild,'quiz-dialog-all-principles-tab');};
	document.getElementById("quiz-review-try-again").onclick=function(){ToolBoxOnClickAllPrinciples();};
	//End Incorrect Answers

}

/*End Quiz*/


/*Mark Principles*/
function OnMarkPrincipleTaskComplete(result, context){
	if(result == "read" || result == "unread"){
		changeToolboxMarkPrincipleLink(result);
		markCurrentPrincipleNavNode(result);
	}
}

function changeToolboxMarkPrincipleLink(status){
	var toolboxLink = document.getElementById("toolbox-mark-read");
	while(toolboxLink.hasChildNodes()){
		toolboxLink.removeChild(toolboxLink.firstChild);
	}
	var newUrl = createNewMarkPrincipleLink(status);
	toolboxLink.appendChild(newUrl);
}

function markCurrentPrincipleNavNode(status){
	var navLink = document.getElementById("navigationActive");
	if(navLink != null){
	    if(status == "unread"){
		    navLink.className = "principle-read-selected";
		    navLink.parentNode.className = "principle-read-selected";
	    }
	    else if(status == "read"){
		    navLink.className = "";
		    navLink.parentNode.className = "";
	    }
	}
}

function createNewMarkPrincipleLink(title){
	var urlHref = "javascript:ajaxmarkprinciplecommand('" + title + "');";
	var toolTip = "Mark this principle as '" + title + "'";
	var newUrl = document.createElement("a");
	newUrl.setAttribute('href', urlHref);
	newUrl.setAttribute('title', toolTip);
	newUrl.innerHTML = "Mark Principle as " + title;
	
	return newUrl;
}

function markReadPrincipleNodes(urls){
	var urlList = urls.split(";");
	var navItems = getPrincipleNavItems(); 
	
	var DivsInNode = null;
	var nodeUrl = null;
	if(navItems != null){
		for(var i=0; i<urlList.length; i++){
			for(var j=0; j<navItems.length; j++){
				nodeUrl = getUrlForNode(navItems[j]);
				if(urlList[i] == nodeUrl){
					DivsInNode = getFirstDiv(navItems[j]);
					if (DivsInNode.length > 0){ 
						DivsInNode[0].className = "principle-read-selected";
						navItems[j].className = "principle-read-selected";
					}
					else{
						navItems[j].className = "principle-read";
					}
				}
			}
		}
	}
}

function getPrincipleNavItems(){
	var principlesLIs = null;
	var principleNav = document.getElementById("navigationInternal");
	var principlesUl = principleNav.getElementsByTagName("UL");
	principlesLIs = principlesUl[0].getElementsByTagName("LI");

	return principlesLIs;
}

function getUrlForNode(nodeLi){
	var liLinks = nodeLi.getElementsByTagName("A");
	var linkUrl = liLinks[0].href;
	
	return linkUrl
}

function getFirstDiv(nodeLi){
	var nodeDivs = nodeLi.getElementsByTagName("DIV");

	return nodeDivs;
}
/*End Mark Principles*/

/*User Notes*/
function showToolboxNotes(){
	ajaxUserNotesCommand(""); 
	showUserNotesDialogBox(this);
}

function showUserNotesDialogBox(obj){
	var container = document.getElementById("userNotesContainer");
    container.style.display="";
    obj.className="selected";
}

function closeUserNotesDialogbox(obj){
	var container = document.getElementById("userNotesContainer");
	container.style.display="none";
}

function OnCancelUserNotesComplete(result, context){
	var resultSet = result.split(":");
	var changed = resultSet[0];
	if(changed == "changed"){
		var closeConfirm = confirm("Are you sure you want to cancel editing this note and discard all changes?");
		if(closeConfirm == true){
			closeUserNotesDialogbox(this);
		}
	}
	else{
		closeUserNotesDialogbox(this);
	}
}

function OnSaveUserNotesComplete(result, context){
	//do nothing
}

function OnSaveCloseUserNotesComplete(result, context){
	//do nothing
}

function OnViewUserNotesComplete(result, context){
	var noteBoxId = result.substring(0, result.indexOf(":"));
	var userNote = result.substring(result.indexOf(":") + 1);
	
	var noteBox = document.getElementById(noteBoxId);
	noteBox.value = userNote;
}

function ajaxUserNotesPrintCommand(noteBoxId){
	var userNote = ReplaceCarriageReturn((document.getElementById(noteBoxId)).value, "<br />");
	PrintUserNotes(userNote);
}

function ReplaceCarriageReturn(bodyTextString, replaceWith){
	bodyTextString = escape(bodyTextString); //encode all characters in text area to find carriage return character

	for(var i=0; i < bodyTextString.length; i++) {
		//loop through string, replacing carriage return encoding with HTML break tag
		if(bodyTextString.indexOf("%0D%0A") > -1) {
			//Windows encodes returns as \r\n hex
			bodyTextString = bodyTextString.replace("%0D%0A",replaceWith);
		}
		else if(bodyTextString.indexOf("%0A") > -1) {
			//Unix encodes returns as \n hex
			bodyTextString = bodyTextString.replace("%0A",replaceWith);
		}
		else if(bodyTextString.indexOf("%0D") > -1) {
			//Macintosh encodes returns as \r hex
			bodyTextString = bodyTextString.replace("%0D",replaceWith);
		}
	}
	bodyTextString = unescape(bodyTextString) //decode all characters in text area back
	return bodyTextString;
}

function PrintUserNotes(notes){
	newWin = window.open("", "PrintUserNotes","left=100,top=100,width=400,height=400");
	newWin.document.write("<HTML>\n<HEAD>\n");
	newWin.document.write("<TITLE>Print User Notes</TITLE>\n");
	newWin.document.write("<script>\n");
	newWin.document.write("function chkstate(){\n");
	newWin.document.write("if(document.readyState==\"complete\"){\n");
	newWin.document.write("window.close();\n");
	newWin.document.write("}\n");
	newWin.document.write("else{\n");
	newWin.document.write("setTimeout(\"chkstate()\",2000);\n");
	newWin.document.write("}\n");
	newWin.document.write("}\n");
	newWin.document.write("function print_win(){\n");
	newWin.document.write("window.print();\n");
	newWin.document.write("chkstate();\n");
	newWin.document.write("window.close();\n");
	newWin.document.write("}\n");
	newWin.document.write("<\/script>\n");
	newWin.document.write("</HEAD>\n");
	newWin.document.write("<BODY onload=\"print_win()\">\n");
	newWin.document.write("<p>\n");
	newWin.document.write(notes);
	newWin.document.write("\n");
	newWin.document.write("</p>\n");
	newWin.document.write("</BODY>\n");
	newWin.document.write("</HTML>\n");
	newWin.document.close();
}
/*End User Notes*/

/*Interactive Graph*/
function renderGraphBar(barId, intBoxId){
	var barDiv = document.getElementById(barId);
	var intBox = document.getElementById(intBoxId);
	var width = 0;
	var v = parseInt(intBox.value, 10);	
	
	if(v>0)
		width = v;
	if(v>100)
		width = 100;
	
	barDiv.style.width = (width * 2) + "px";
	
	if(v>100)
		barDiv.style.borderColor = "#ff0000";
	else
		barDiv.style.borderColor = "#ffffff";
}

function updateTotal(txtTotId, intBoxId) {
	var totBox = document.getElementById(txtTotId);
	var intBox = document.getElementById(intBoxId);
	
	var tot = getTotal('interactiveGraph');
	var v = parseInt(intBox.value, 10);	
	
	if(v>100)
		intBox.className = "GT100";
	else if(v<=100) 
		intBox.className = "LT100";
		
	if(tot>100) 
		totBox.className = "GT100";
	else if(tot==100) 
		totBox.className = "EQ100";
	else if(tot<100) 
		totBox.className = "LT100";
				
	totBox.value = tot;
}

function getTotal(divId){
	var parentDiv = document.getElementById(divId);
	var txtBoxes = parentDiv.getElementsByTagName("input");
	var total = 0;
	var v = 0;
	for(var i=0; i<txtBoxes.length-1; i++){
		v = parseInt(txtBoxes[i].value, 10);
		if(v>0)
			total = total + v;
	}
	
	return total;
}
	
function callUpdate(txtTotId, intBoxId, barId){
	updateTotal(txtTotId, intBoxId);
	renderGraphBar(barId, intBoxId);
}
/*End Interactive Graph*/

/*Registration Autocomplete Textbox*/

function OnGetBoardNameComplete(result, context){

if (result !="")
{
	var resultArray = result.split("##");
	var strTextBoxID = resultArray[0];
	var strSuggestions = resultArray[1];
	var textbox = document.getElementById(strTextBoxID);
	
	var ajaxOL = document.getElementById("AjaxOLBoardName")
	if (ajaxOL ==null)
	{
		var newBR =document.createElement("br");
		var newSpacerSPAN = document.createElement("span");
		var ajaxOL = document.createElement("ol");
		
		newBR.id ="BRSpacer";
		
		newSpacerSPAN.id="AjaxSPANSpacer";
		newSpacerSPAN.className = "AjaxSPANSpacer";
		
		ajaxOL.id="AjaxOLBoardName";
		ajaxOL.className="AjaxOLBoardName";
		PopulateAjaxOL(ajaxOL, strSuggestions,strTextBoxID);
		
		textbox.parentNode.insertBefore(ajaxOL,textbox.nextSibling);
		textbox.parentNode.insertBefore(newSpacerSPAN,textbox.nextSibling);
		textbox.parentNode.insertBefore(newBR,textbox.nextSibling);
	}
	else
	{
		PopulateAjaxOL(ajaxOL, strSuggestions,strTextBoxID);
	}
}
else
{
	ClearAjaxOL();
}

}

function PopulateAjaxOL(ajaxOL, strSuggestions, strTextBoxID)
{
	var x = strSuggestions.split(";");
	
	while(ajaxOL.hasChildNodes())
	{
		ajaxOL.removeChild(ajaxOL.firstChild);
	}
	
	for(var i=0; i<x.length; i++)
	{
		var newLI = document.createElement("li");
		newLI.innerText = x[i];
		newLI.id = "AjaxLI"+i;
		newLI.onclick =function(){document.getElementById(strTextBoxID).value = this.innerText;ClearAjaxOL();};
		ajaxOL.appendChild(newLI);
	}
}

function ClearAjaxOL()
{
	var ajaxOL= document.getElementById("AjaxOLBoardName");
	var ajaxSPANSpacer = document.getElementById("AjaxSPANSpacer");
	var BRSpacer = document.getElementById("BRSpacer");
	
	if (ajaxOL !=null)
	{
		var parent = ajaxOL.parentNode;
		parent.removeChild(ajaxOL);
		parent.removeChild(ajaxSPANSpacer);
		parent.removeChild(BRSpacer);
	}
}
/*End Registration Autocomplete Textbox*/
