var xmlHttp;

function postComment() {
    var poststr = "fName=" + encodeURI( document.getElementById("fName").value ) +
                  "&fComment=" + encodeURI( document.getElementById("fComment").value ) +
				  "&fEntryID=" + encodeURI( document.getElementById("fEntryId").value );
	makePOSTRequest('lib/comments.php', poststr);
}

function makePOSTRequest(url, parameters) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
      xmlHttp.onreadystatechange = function alertContents() {
      if (xmlHttp.readyState == 4) {
         if (xmlHttp.status == 200) {
			var count = parseInt(document.getElementById('commentCount').innerHTML);
				count += 1;
			if( count == 1 )
				document.getElementById('commentCount').innerHTML = count + ' Comment';
			else
				document.getElementById('commentCount').innerHTML = count + ' Comments';
			document.getElementById('comments').innerHTML += xmlHttp.responseText;
			document.getElementById('commentContainer').style.display = 'none';
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
      xmlHttp.open('POST', url, true);
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", parameters.length);
      xmlHttp.setRequestHeader("Connection", "close");
      xmlHttp.send(parameters);
	  
}

function submitComment(){
	var name = document.getElementById("fName");
	var comment = document.getElementById("fComment");
	  if(name.value == "" || comment.value == ""){
		alert("Error: Comment form incomplete!");
	  }else{
		document.getElementById("commentForm").submit();
	  }	
}


function GetXmlHttpObject( handler ){ 
	if (window.XMLHttpRequest){ //Mozilla, Safari
		http = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		try{
			http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e){
			try{
				http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){}
		}
	}
	return http;
}

function $(id){ return document.getElementById(id); }