var notesDiv
var notesTable
var notesTableBody
var sId, cId
		
//Save and display student notes
function saveNotes(myNotes, studentId, classId,chapter,section,slideNo){
	initNotes()
	createXMLHttpRequest();
	disableNotes();
	var url = 'data/student_notes.asp';
	var queryString = 'myNotes=' + escape(myNotes) + '&studentId=' + studentId + '&classId=' + classId + '&chapter=' + chapter + '&section=' + section + '&slideNo=' + slideNo;
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = notesBack;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	xmlHttp.send(queryString);
	
			
}

function disableNotes(){
	document.getElementById("myNotes").value = "Saving..."
	document.getElementById("myNotes").setAttribute("disabled",true)
	document.getElementById("chapter").setAttribute("disabled",true)
	document.getElementById("section").setAttribute("disabled",true)
	document.getElementById("slideNo").setAttribute("disabled",true)
	
}	

function clearNotes(){
	document.getElementById("chapter").removeAttribute("disabled")
	document.getElementById("chapter").selectedIndex = 0
	document.getElementById("myNotes").removeAttribute("disabled")
	document.getElementById("myNotes").value = "Enter your notes here."
	document.getElementById("section").removeAttribute("disabled")
	document.getElementById("section").value = ""
	document.getElementById("slideNo").removeAttribute("disabled")
	document.getElementById("slideNo").value = ""
}
		
function initNotes(){
	notesDiv = document.getElementById("notes")
	notesTable = document.getElementById("notesTable")
	notesTableBody = document.getElementById("notesBody")
}
		
function deleteNotes(url,studentId,classId){
	if(confirm("Delete this note?")){
		sId = studentId
		cId = classId
		createXMLHttpRequest();
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = deleteBack;
		xmlHttp.send(null);
	}else{
		alert("Delete Canceled")
	}
}
		
function showAllNotes(studentId, classId){
	initNotes()
	
	showLoadingMsg()
	createXMLHttpRequest();
	sId = studentId
	cId = classId
	var url = 'data/student_notes.asp?studentId=' + studentId + '&classId=' + classId
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = notesBack;
	xmlHttp.send(null);
}

function showLoadingMsg(){
	clearOldNotes()	
	var tr, td, textNode, strong
	
	textNode = document.createTextNode("Loading...")
	strong = document.createElement("strong")
	strong.appendChild(textNode)
	td = document.createElement("td")
	td.style.backgroundColor='rgb(255,255,0)'
	td.appendChild(strong)
	tr = document.createElement("tr")
	tr.appendChild(td)
	notesTableBody.appendChild(tr)	
}

function deleteBack(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			showAllNotes(sId,cId)		
		}
	}
}
	
function notesBack(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			clearNotes();
			setupNotes()	
		}
	}
}
		
function setupNotes(){
	clearOldNotes()
	
	var xmlDoc = xmlHttp.responseXML;
	removeWhitespaceNodes(xmlDoc)
	var allNotes = xmlDoc.getElementsByTagName("note");
	var currentNote = null;
	var tr, td, strong, p, textNode, span
	var br, a, dId, url
			
	for (var i = 0; i < allNotes.length; i++){
		currentNote = allNotes[i]
		textNode = document.createTextNode(currentNote.childNodes[3].childNodes[0].nodeValue + ": ")
		strong = document.createElement("strong")
		strong.appendChild(textNode)
		p = document.createElement("p")
		p.appendChild(strong)
		textNode = document.createTextNode("Delete")
		dId = currentNote.childNodes[0].childNodes[0].nodeValue
		a = document.createElement("a");
		url = "/oc2/data/delete_notes.asp?dId=" + dId
		a.setAttribute("href", url);
		a.style.font = "normal 10px arial"
		a.onclick = function(){deleteNotes(this.href,sId,cId);return false;};
		a.appendChild(textNode)
		p.appendChild(a)
								
		br = document.createElement("br")
		p.appendChild(br)
		
		span = document.createElement("span")
		span.className = "microText"
		textNode = document.createTextNode("Date: " + currentNote.childNodes[2].childNodes[0].nodeValue)
		span.appendChild(textNode)
		p.appendChild(span)
		br = document.createElement("br")
		p.appendChild(br)
		
		span = document.createElement("span")
		span.className = "microText"
		textNode = document.createTextNode("Section: " + currentNote.childNodes[4].childNodes[0].nodeValue + " Slide No: " + currentNote.childNodes[5].childNodes[0].nodeValue)
		span.appendChild(textNode)
		p.appendChild(span)
		br = document.createElement("br")
		p.appendChild(br)
		
		//textNode = document.createTextNode(currentNote.childNodes[1].childNodes[0].nodeValue)
			
		var arrText = currentNote.childNodes[1].childNodes[0].nodeValue.split("[br]")
		for(var j = 0; j < arrText.length; j++){
					
			textNode = document.createTextNode(arrText[j]);
			p.appendChild(textNode);
			if(j != arrText.length){
				br = document.createElement("br")
				p.appendChild(br)
			}
		}
				
		td = document.createElement("td")
		switch(i){
			case 0:
				td.setAttribute("bgColor", "#F7FD59");
				break;
			case 1:
				td.setAttribute("bgColor", "#F7FB8B");
				break;
			case 2:
				td.setAttribute("bgColor", "#F9FBC0");
				break;
			default:
				td.setAttribute("bgColor", "white");
		}
					
		td.appendChild(p)
		tr = document.createElement("tr")
		tr.appendChild(td)
		notesTableBody.appendChild(tr)
	}
			
}
		
function clearOldNotes(){
	var ind = notesTableBody.childNodes.length;
	for(var i = ind - 1; i >= 0; i--){
		notesTableBody.removeChild(notesTableBody.childNodes[i]);
	}	
}

