/**
 * PHP Contact Form
 * Version: 2011-09-04 21:37:00 (UTC+2)
 * Author:  Christian L. Dünweber
 * Webpage: http://www.dunweber.com/docs/scripts/#contact_form
 * Copyright (C) 2006-2011 Christian L. Dünweber
 * This program is distributed under the GNU General Public License,
 * see <http://www.gnu.org/licenses/gpl.html>.
 *
 * Challenge/security image adapted from article by Edward Eliot:
 * http://articles.sitepoint.com/article/toughen-forms-security-image
 * Font by courtesy of: http://www.webpagepublicity.com/free-fonts.html
 * Icon by courtesy of: http://www.famfamfam.com/
 */

/**
 * Check form fields for valid content.
 * @param	String ID of the form.
 * @return	boolean True if all fields where valid.
 */
function checkFormContent(form)
{
	if (checkField(form.name, 'your name') &&
		checkEmail(form) &&
		checkField(form.subject, 'a subject') &&
		checkField(form.message, 'a message') &&
		checkField(form.challengeAnswer, 'the verification text from the image')) {
		return true;
	}

	return false;
}

/**
 * Check if form field is empty.
 * @param	String field ID.
 * @param	String description of the field.
 * @return	boolean True if anything is written in the field.
 */
function checkField(field, description)
{
	if (field.value.length < 1) {
		alert('You forgot to enter ' + description + '.');
		field.focus();
		return false;
	}

	return true;
}	

/**
 * Check if an e-mail adress looks valid, i.e. on the form x@x.xxx.
 * @param	String form ID - must contain a field with id="email"
 * @return	boolean True if anything e-mail looks good.
 */
function checkEmail(form)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(form.email.value)) {
		alert('Please enter a valid e-mail address.');
		form.email.focus();
		return false;
	}
	
	return true;
}

