/**
 * PHP Contact Form
 * Version: 2010-09-08 23:30:00
 * Author:  Christian L. Dünweber
 * Webpage: http://www.dunweber.com/docs/scripts/#contact_form
 * Copyright (C) 2006-2010 Christian L. Dünweber
 * This program is distributed under the GNU General Public License,
 * see <http://www.gnu.org/licenses/gpl.html>.
 */

/**
 * 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, 'challenge answer')) {
		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 valid e-mail address.');
		form.email.focus();
		return false;
	}
	
	return true;
}
