
var blogRequests = new Array();

function BlogRequest() {
	this.sectionIdentifier;
	this.aRandomInt;
	this.aBlogName;
	this.commentSuccessIdentifier;
	this.commentFailureIdentifier;

	var me = this;
	
	this.handleReadyState = getReadyStateHandler;

	/*
	 * Update comments area of page to reflect contents of blog comments
	 * described in XML document.
	 */
	this.updateCommentResults = function (resultsXML) {

	  var resultsNode = resultsXML.getElementsByTagName("comments")[0];
	  var comments = resultsNode.getElementsByTagName("comment");
	
	  var htmlText = "";
	  if (comments.length > 0) {
	    htmlText += "<h3>Comments</h3>";
		htmlText +="<table class='table-list' width='100%'>"
	  }
	
	  for (var I = 0 ; I < comments.length ; I++) {
		formId = "report_abuse_"+me.aRandomInt+"_comment_"+I;
		successSectionId = "successBlock_"+me.aRandomInt+"_comment_"+I;
		errorSectionId = "errorBlock_"+me.aRandomInt+"_comment_"+I;
		
	    htmlText += "<tr><td>";
		htmlText += "<div class='blog-comment'>";
		htmlText += "<form action=\"javascript:reportAbuse('"+ formId +"', '"+ successSectionId +"', '"+ errorSectionId +"', '" + this.sectionIdentifier + "' );\" name='"+ formId +"' id='"+ formId +"'>";
		
		var comment = comments[I];
	    var commentText = comment.getElementsByTagName("text")[0].firstChild.nodeValue;
	    
		htmlText += "<input type=\"submit\" value=\"Report Abuse\" class=\"btn-report-abuse\"/>";
		htmlText += "<pre>"+commentText+"</pre>";
	
	    var author = comment.getElementsByTagName("author")[0].firstChild.nodeValue;
	    var created = comment.getElementsByTagName("created")[0].firstChild.nodeValue;
	    htmlText += "<span class='font-note'>Posted by " + author + " on " + created + "</span>";
	    htmlText += "<br/>";
		htmlText += "		<input type=\"hidden\" name=\"action\" value=\"report-abuse\" />";
	    
		var commentId = comment.getAttribute("id");
		htmlText += "		<input type=\"hidden\" name=\"blogId\" value=\""+commentId+"\" />";
		htmlText += "		<input type=\"hidden\" name=\"comment\" value=\""+commentText+"\" />";
		htmlText += "</form>";
		htmlText += "<span id=\""+ successSectionId +"\" class='msg-success' style=\"display: none;\">The administrator has been informed.</span>";
		htmlText += "<span id=\""+ errorSectionId +"\" class='msg-error' style=\"display: none;\"></span>";
		htmlText += "</div>";
		htmlText +="</td></tr>";
	
	  }
	  if(comments.length > 0){
		htmlText += "</table>";
		document.getElementById(me.sectionIdentifier).innerHTML = htmlText;
	  }
	}
	/*
	 * Get blog comments
	 * blogName - the name of the blog
	 * sectionId - the section to update
	 * theRandomInt - the random integer to use for identifying this blog
	 */
	this.getComments = function() {

	  var req = newXMLHttpRequest();
	  var dateFormat = "EEEE, MMMM d, yyyy h:mm:ss a";   // Tuesday, April 29, 2008 8:06:12 AM

	  req.onreadystatechange = me.handleReadyState(req, me.updateCommentResults);
	  req.open("POST", "/KTBU/dbHandler.do", true);
	  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  req.send("action=get_comments&blogName="+this.aBlogName+"&dateFormat="+dateFormat);
		
	}

	/*
	 * Update results area of page to reflect contents of XML document.
	 */
	this.updatePostResults = function(resultsXML) {
	  me.updateSectionResults(resultsXML);
	
	  var resultsNode = resultsXML.getElementsByTagName("formResults")[0];
	  var results = resultsNode.getAttribute("results");
	  if (results == "success") {
	  	getBlogComments(me.aBlogName, me.sectionIdentifier, me.aRandomInt);
	  }
	}

	/*
	 * Update results area of page to reflect contents of XML document.
	 */
	this.updateSectionResults = function(resultsXML) {
	  var resultsNode = resultsXML.getElementsByTagName("formResults")[0];
	  var results = resultsNode.getAttribute("results");
	  if (results == "success") {
	    document.getElementById(me.commentFailureIdentifier).style.display = 'none';
	    document.getElementById(me.commentSuccessIdentifier).style.display = 'block';
	  }
	  if (results == "error") {
	    document.getElementById(me.commentFailureIdentifier).style.display = 'block';
	    document.getElementById(me.commentSuccessIdentifier).style.display = 'none';
	
	    // One day we may want to add parsing of the XML to tailor the error block
	  }
	  toggleButtons("enable");
	}

	this.reportAbuse = function(formId) {
	  var req = newXMLHttpRequest();
	
	  req.onreadystatechange = me.handleReadyState(req, me.updateSectionResults);
	  
	  req.open("POST", "/KTBU/sendEmail.do", true);
	  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  req.send(convertFormToQueryString(document.getElementById(formId)));
	}
	
	this.postComment = function(formId) {
	  var req = newXMLHttpRequest();

	  req.onreadystatechange = me.handleReadyState(req, me.updatePostResults);

	  req.open("POST", "/KTBU/dbHandler.do", true);
	  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  req.send(convertFormToQueryString(document.getElementById(formId)));
	
	}
}

/*
 * Get blog comments
 * blogName - the name of the blog
 * sectionId - the section to update
 * theRandomInt - the random integer to use for identifying this blog
 */
function getBlogComments(blogName, sectionId, theRandomInt) {

  var blogReq = new BlogRequest();
  blogReq.sectionIdentifier = sectionId;
  blogReq.aRandomInt = theRandomInt;
  blogReq.aBlogName = blogName;
  blogRequests.push(blogReq);
  blogReq.getComments(blogName, sectionId, theRandomInt);
}

/*
 * Report abusive comment via Ajax call
 * formId - the ID of the form to retreive the values to be sent via email
 * successSectionId - the ID of the section where the success message is stored
 * errorSectionId - the ID of the section where the error message is stored (not implemented)
 */
function reportAbuse(formId, successSectionId, errorSectionId, sectionId) {
  toggleButtons("disable");
  
  var blogReq = new BlogRequest();
  blogReq.commentSuccessIdentifier = successSectionId;
  blogReq.commentFailureIdentifier = errorSectionId;
  blogReq.sectionIdentifier = sectionId;
  blogRequests.push(blogReq);
  
  blogReq.reportAbuse(formId);
}

/*
 * Post a comment via Ajax call
 * formId - the ID of the form to retreive the values to be posted
 * successSectionId - the ID of the section where the success message is stored
 * errorSectionId - the ID of the section where the error message is stored (not implemented)
 * blogName - the name of the blog
 * sectionId - the section to update
 * theRandomInt - the random integer to use for identifying this blog
 */
function postComment(formId, successSectionId, errorSectionId, blogName, sectionId, theRandomInt) {
  validateForm(formId);
  
  if(validForm){
	  toggleButtons("disable");
	  var blogReq = new BlogRequest();
	  blogReq.commentSuccessIdentifier = successSectionId;
	  blogReq.commentFailureIdentifier = errorSectionId;
	  blogReq.aBlogName = blogName;
	  blogReq.sectionIdentifier = sectionId;
	  blogReq.aRandomInt = theRandomInt;
	  blogRequests.push(blogReq);
	  
	  blogReq.postComment(formId);
  }
}

function toggleButtons(action) {
  var enableButtons = null;

  if (action == "enable") {
    enableButtons = true;
  }
  else if (action == "disable") {
    enableButtons = false;
  }

  var pageButtons = document.getElementsByTagName("button");

  if (pageButtons) {
    for(pb = 0; pb < pageButtons.length; pb++) {
      pageButtons[pb].disabled = enableButtons;
    }
  }
}
