/**********************************************************
 * form.validation.js
 *
 * Stuart Connolly
 * http://stuconnolly.com/
 *
 * Copyright (c) 2009 Stuart Connolly. All rights reserved.
 **********************************************************/
 
/**
 * Validates the comment form's input elements.
 */
function validateCommentForm()
{
	var errorMessage = '';
	var form = document.forms['commentform'];

	if (trim(form.author.value).length == 0) {
		errorMessage = "- The author field is blank\n";
	}

	if (trim(form.email.value).length == 0) {
		errorMessage += "- The email field is blank\n";
	}
	else {
		if (validateEmailAddress(trim(form.email.value)) == false) {
			errorMessage += "- The email address '" + trim(form.email.value) + "' is not valid.\n";
		}
	}

	if (trim(form.comment.value).length == 0) {
		errorMessage += "- The comment field is blank\n";
	}

	if (trim(form.year.value).length == 0) {
		errorMessage += "- The current year field is blank\n";
	}
	else {
		if (isNaN(trim(form.year.value))) {
			errorMessage += "- The current year field is not numeric\n";
		}
		else {
			var date = new Date();

			if (trim(form.year.value) != date.getUTCFullYear()) {
				errorMessage += '- Anti-spam measure failed.';
			}
		}
	}

	if (errorMessage.length > 0) {
		alert("Form validation failed. Please take note of the following:\n\n" + errorMessage);

		return false;
	}
}

/**
 * Validates the contact form's input elements.
 */
function validateContactForm()
{
	var errorMessage = '';
	var form = document.forms['contactform'];

	if (trim(form.name.value).length == 0) {
		errorMessage = "- The author field is blank\n";
	}

	if (trim(form.email.value).length == 0) {
		errorMessage += "- The email field is blank\n";
	}
	else {
		if (validateEmailAddress(trim(form.email.value)) == false) {
			errorMessage += "- The email address '" + trim(form.email.value) + "' is not valid.\n";
		}
	}

	if (trim(form.message.value).length == 0) {
		errorMessage += "- The message field is blank\n";
	}

	if (trim(form.year.value).length == 0) {
		errorMessage += "- The current year field is blank\n";
	}
	else {
		if (isNaN(trim(form.year.value))) {
			errorMessage += "- The current year field is not numeric\n";
		}
		else {
			var date = new Date();

			if (trim(form.year.value) != date.getUTCFullYear()) {
				errorMessage += '- Anti-spam measure failed. ';
			}
		}
	}

	if (errorMessage.length > 0) {
		alert("Form validation failed. Please take note of the following:\n\n" + errorMessage);

		return false;
	}
}
