// baseURL is the location where the comment engine is stored
// because of security concerns, it must be located within the
// same domain as this JS file.
// In this case -- I use /projects/comment_engine/
var baseURL = "/projects/comment_engine/";

function getPageName() {
	var url = document.URL;
	url = url.substring(url.lastIndexOf("\/")+1,url.lastIndexOf('\.'));
	return url;
}

function submitForm() {
	var frm = document.theform;
	// post the comment
	var URL = baseURL + "comment_engine.php?action=post&page=" + getPageName();

	var username = document.getElementById('username');
	var ip = document.getElementById('IP');
	var comment = document.getElementById('txtCommentEntry');

	var postStr = "username=" + encodeURIComponent(username.value) + "&" 
		+ "IP=" + encodeURIComponent(ip.value) + "&"
		+ "comment=" + encodeURIComponent(comment.value) + "&";
	makerequest(URL, refreshComments, 'POST', postStr);
	refreshCommentForm();
}

function refreshComments() {
	var URL = baseURL + "comment_engine.php?action=get_comments&page=" + getPageName();
	makerequest(URL, document.getElementById('comment_list'));

}
function refreshCommentForm() {
	var URL = baseURL + "comment_engine.php?action=get_form&page=" + getPageName();
	makerequest(URL, document.getElementById('comment_form'));
}

// Lightweight AJAX Component
function GetXHR()
{
  var XHR=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    XHR=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      XHR=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      XHR=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return XHR;
}

function makerequest(serverPage, obj, getOrPost, str)
{
	var undefined;
	if (str == undefined) { str = null; }
	if (getOrPost == undefined || getOrPost.toUpperCase() != "POST") 
	{ getOrPost = "GET"; }
	
	var XHR;
	
	if(XHR = GetXHR()) {
		XHR.open (getOrPost, serverPage, true);
		if (getOrPost.toUpperCase() == "POST")
		{
			XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			XHR.setRequestHeader("Content-Length", str.length);
			XHR.setRequestHeader("Connection", "close");
		}
		XHR.onreadystatechange = function() {
			if (XHR.readyState == 4 && XHR.status == 200 && obj != null)
			{
				switch(typeof(obj)) {
					// Allows for AJAX chaining //
					case "function":
						obj(XHR.responseText);
						break;
					case "object":
						if (obj.innerHTML)	
						{ 
							obj.innerHTML = XHR.responseText;
						}
						else obj.src = XHR.responseText;
						break;
					
					default:
						obj = XHR.responseText; 
						break;
				}
	
			}
		}
		XHR.send(str);
	}
}

// Sets up the comment form on pageLoad.
window.onload = function() {
	var comments = document.getElementById('comments');

	var comment_form = document.createElement('div');
	comment_form.setAttribute('id', 'comment_form');
	comment_form.innerHTML = "&nbsp;"
	
	var comment_list = document.createElement('div');
	comment_list.setAttribute('id', 'comment_list');
	comment_list.innerHTML = "&nbsp;"

	comments.appendChild(comment_form);
	comments.appendChild(comment_list);

	refreshCommentForm();
	refreshComments();

}

