/*
 *  $Id: form.validation.js 10 2010-04-15 20:13:33Z stuart $
 *  
 *  stuconnolly.com   
 *
 *  Copyright (c) 2010 Stuart Connolly. All rights reserved.
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
/**
 * 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;
	}
}
