//********************************************
// Name:	initialize
// Author:	Yoann Roman
// Date:	02/14/08
// Purpose:	Binds to the comment form.
//********************************************
function initialize() {
	
	// Locate the comment form
	var frm = getObject("commentform");
	if (!frm) return;

	// Bind to its submit event
	var handler = createHandler(frm, submitComment);
	if (frm.addEventListener) {
		frm.addEventListener("submit", handler, false);
	} else if (frm.attachEvent) {
		frm.attachEvent("onsubmit", handler);
	} else {
		frm.onsubmit = handler;
	}
}

//********************************************
// Name:	submitComment(frm)
// Author:	Yoann Roman
// Date:	02/13/08
//			10/22/08
// Purpose:	Submits a comment.
//********************************************
function submitComment(frm) {

	// Get the comment form & fields
	var name = getObject("author");
	var email = getObject("email");
	var website = getObject("url");
	var comment = getObject("comment");
	var id = getObject("comment_post_id");

	// Make sure the ID field was found
	if (!id) {
		setError("Comments are closed.");
		return false;
	}

	// Initialize the form data
	var strData = "comment_post_ID=" + id.value;

	// Validate the info fields if not logged-in
	if (name) {
		strData += "&author=" + encodeValue(name.value);
		if (name.value.length == 0) {
			setError("Please enter your name to post a comment.");
			return false;
		}
		if (email) {
			strData += "&email=" + encodeValue(email.value);
			if (email.value.length == 0) {
				setError("Please enter your email address to post a comment.");
				return false;
			} else if (!validEmail(email.value)) {
				setError("Please enter your email address to post a comment.");
				return false;
			}
		}
		if (website) {
			strData += "&url=" + encodeValue(website.value);
			if (website.value.length > 0) {
				if (!validURL(website.value)) {
					setError("Please enter a valid URL or leave the field blank.");
					return false;
				}
			}
		}
	}

	// Make sure a comment was specified
	if (!comment || comment.value.length == 0) {
		setError("Please enter your comment.");
		return false;
	}

	// Add on the comment data
	strData += "&comment=" + encodeValue(comment.value);

	// Disable the submit button
	var submit = getObject("submit");
	if (submit) {
		submit.disabled = true;
		strData += "&submit=" + encodeValue(submit.value);
	}

	// Submit the post request
	toggleItem("indicator", true);
	fetchXML(frm.action, loadStatus, strData);
	return false;
}

//********************************************
// Name:	encodeValue(value)
// Author:	Yoann Roman
// Date:	010/22/08
// Purpose:	Encodes a value for use in a 
//			POST request. We should be using 
//			encodeURIComponent here but PHP 
//			isn't happy with UTF-8 right now.
//********************************************
function encodeValue(value) {
	return escape(value).replace('+', '%2B');
}

//********************************************
// Name:	setError(message)
// Author:	Yoann Roman
// Date:	02/13/08
// Purpose:	Sets the page's error message.
//********************************************
function setError(message) {

	// Set the error message
	setContent("error", message);
	toggleItem("error", (message.length != 0));

	// Enable the submit button if disabled
	var submit = getObject("submit");
	if (submit && submit.disabled) {
		submit.disabled = false;
	}
}

//********************************************
// Name:	loadStatus(xmlHttp)
// Author:	Yoann Roman
// Date:	02/13/08
// Purpose:	Processes a status update.
//********************************************
function loadStatus(xmlHttp) {

	// Is the response ready?
	if (isXMLReady(xmlHttp, true)) {

		// Remove the indicator
		toggleItem("indicator", false);

		// Was there an error?
		if (xmlHttp.status != 200) {

			// Parse the error out of the content
			var data = xmlHttp.responseText;
			if (data.search(/<title>WordPress &rsaquo; Error<\/title>/) != -1) {
				var error = data.match(/<p>(.*)<\/p>/);
				setError(error[1]);
			} else {
				setError(data);
			}
			return;

		} else {

			// For now, just reload the page; eventually, this should be 
			// changed to dynamically update the page using some library
			location.reload(true);
		}
	}
}

// Bind to the onLoad event to initialize
onLoad(initialize);