var req = null;
var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADING = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLETE = 4;

var newCmt = "";
var notesSection = "";
var newCommentSection = "";

function sendData(newComment, userFName, userLName, fontColor) 
{
	//alert("New_Comment:" + newComment + ", First Name:" + userFName + ", Last Name:" + userLName);
	
	newCmt = newComment;
	
	url = "forum_comment_processing.asp?NEW_COMMENT=" + newCmt + "&USR_FNAME=" + userFName + "&USR_LNAME=" + userLName + "&FONT_COLOR=" + fontColor;
	
	notesSection = document.getElementById('total_section');
	newCommentSection = document.getElementById('new_note');
	
	loadXMLDoc(url);
}

function retrieveData() 
{
	url = "forum_comment_retrieval.asp";
	
	notesSection = document.getElementById('total_section');
	newCommentSection = document.getElementById('new_note');
	
	loadXMLDoc(url);
}

function loadXMLDoc(url) 
{
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) 
	{
		req = new XMLHttpRequest();
		
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	} 
	// branch for IE/Windows ActiveX version
	else if (window.ActiveXObject) 
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");		
		if (req) 
		{
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChange() 
{
	if(req.readyState == READY_STATE_UNINITIALIZED)
	{
	     //alert("0");
	}
	else if(req.readyState == READY_STATE_LOADING)
	{
		//alert("1");
		data = req.responseXML;
		//alert(data);
	}
	else if(req.readyState == READY_STATE_LOADED)
	{
		//alert("2");
	}
	else if(req.readyState == READY_STATE_INTERACTIVE)
	{
		//alert("3");
	}
	
	// only if req shows complete
	if (req.readyState == READY_STATE_COMPLETE) 
	{
		//alert("4");
		//alert(req.status);
		
		if(req.status == "200") 
		{
			data = req.responseXML.documentElement;
			
			//alert(data);
			
			if(data != null)
			{
				strTotalRecs = data.getElementsByTagName('TOTAL_RECORDS')[0].firstChild.data;
				iTotalRecs = parseInt(strTotalRecs);
				//alert("iTotalRecs = " + iTotalRecs);
				
				strTotalUsers = data.getElementsByTagName('TOTAL_USERS')[0].firstChild.data;
				iTotalUsers = parseInt(strTotalUsers);
				//alert("iTotalUsers = " + iTotalUsers);
				
				strUserMatches = data.getElementsByTagName('USER_MATCH')[0].firstChild.data;
				//iUserMatches = parseInt(strUserMatches);
				//alert("iUserMatches = " + strUserMatches);
				
				strNotesSection = "";
				for (var count = 0; count < iTotalRecs; count++)
				{
					//Get forum id for future use
					strForumID = data.getElementsByTagName('FORUM_ID')[count].firstChild.data;
					//alert(strForumID);
									
					//Format to specific color (not done yet - need UI
					strNotesSection = strNotesSection + "\n<B><font color="
					strNotesSection = strNotesSection + data.getElementsByTagName('FONT_COLOR')[count].firstChild.data;
					strNotesSection = strNotesSection + ">";
					
					//Get the User Name (is ID for now)
					strNotesSection = strNotesSection + data.getElementsByTagName('FORUM_USER')[count].firstChild.data;
					
					//Add Date
					strNotesSection = strNotesSection + "(";
					strNotesSection = strNotesSection + data.getElementsByTagName('DATE_POSTED')[count].firstChild.data;
					strNotesSection = strNotesSection + "):";
					
					//Add comment
					strNotesSection = strNotesSection + data.getElementsByTagName('FORUM_TEXT')[count].firstChild.data;
					
					//Close font tag
					strNotesSection = strNotesSection + "\n</font></B>";
					
					//Break after done
					strNotesSection = strNotesSection + '\n<BR>';
				}
				
				notesSection.innerHTML = strNotesSection;
				newCommentSection.value = "";
			}
			else
			{
				notesSection.innerHTML = "No Data Available";
			}
				
	    }		
		else 
		{
			notesSection.innerHTML = '';
		}
	}
}

