﻿var xmlHttp;
var myReqNum = 0;
var http = new Array();

function popDDL(page,section)
{
	myReqNum++;
	var myLocalReq=myReqNum;
	var url="PopulateDDL.php?page="+page+"&section="+section+"&topic=all";
	var theReq;
	
	http[myLocalReq] = GetXmlHttpObject();
	
	http[myLocalReq].open('get', url);
	theReq = http[myLocalReq];
	http[myLocalReq].onreadystatechange = function(){stateChangedDDL2(section,theReq);};
	http[myLocalReq].send(null);
}

function popDDL3(page,section,topic)
{
	myReqNum++;
	var myLocalReq=myReqNum;
	var url="PopulateDDL.php?page="+page+"&section="+section+"&topic="+topic;
	var theReq;
	
	http[myLocalReq] = GetXmlHttpObject();
	
	http[myLocalReq].open('get', url);
	theReq = http[myLocalReq];
	http[myLocalReq].onreadystatechange = function(){stateChangedDDL2(section,theReq);};
	http[myLocalReq].send(null);
}

function popDDL2(section)
{
	myReqNum++;
	var myLocalReq=myReqNum;
	var url="PopulateDDL.php?page=articles&section="+escape(section)+"";
	var theReq;
	
	http[myLocalReq] = GetXmlHttpObject();
	
	http[myLocalReq].open('get', url);
	theReq = http[myLocalReq];
	http[myLocalReq].onreadystatechange = function(){stateChangedDDL2("all",theReq);};
	http[myLocalReq].send(null);
}

function showUser(section, noteID)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	var url="getlecture.php";
	url=url+"?q="+noteID;
	
	xmlHttp.onreadystatechange=function() {stateChanged(section);};
	xmlHttp.open("GET",url,true);
	
	xmlHttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");

	xmlHttp.send(null);
}

function popComments(str)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="popLecture.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function addComment()
{ 
	//alert("addComment() Called");
	var page 	= document.getElementById("page").value;
	var pageID= document.getElementById("pageID").value;
	var user 	= document.getElementById("user").value;
	var title 	= document.getElementById("title").value;
	var text 	= document.getElementById("text").value;

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="addlecture.php";
	url=url+"?page="+page;
	url=url+"&title="+title;
	url=url+"&text="+text;
	url=url+"&pageID="+pageID;
	url=url+"&user="+user;
	xmlHttp.onreadystatechange=stateChangedAdd;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged(section) 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("txtHint_"+section).innerHTML=xmlHttp.responseText;
		
		var rt = xmlHttp.responseText;
		rt = rt.substring(rt.indexOf("<b>"),rt.indexOf("<br>"));
		rt = rt.replace(/<b>/gi, "");
		rt = rt.replace(/<\/b>/gi, "");
		rt = rt.replace(/<i>/gi, "");
		rt = rt.replace(/<\/i>/gi, "");
		document.title = rt
		
		document.getElementById("divTitle").innerHTML = rt;
		//document.getElementById("divTitle").innerHtml = document.getElementById("title").value;
	} 
}

function stateChangedAdd(section) 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("divTitle").innerHTML = xmlHttp.responseText ;
	} 
}

function stateChangedDDL() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		//alert("Trying to put stuff in 'notes'");
		document.getElementById("notes").innerHTML=xmlHttp.responseText;
	} 
}

function stateChangedDDL2(section,theReq) 
{ 	
	if (theReq.readyState==4 || theReq.readyState=="complete")
	{ 
		//alert("Trying to put stuff in 'notes_"+section+"' with : "+theReq.responseText);
		document.getElementById("notes_"+section).innerHTML=theReq.responseText;
	} 
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		//Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
