// JavaScript Document

// div do display error messages
var errordiv = 'error'
//

//validate form
function checkForm(theForm) {
	var error='';
	// run checks, each check adds text to the error variable on error
	error+=checkText(theForm.author, 'Name', false);
	error+=checkEmail(theForm.email, false);
	
	// if error is detected run this function
	if (error != '') {
		document.getElementById(errordiv).innerHTML=error;
		document.getElementById(errordiv).style.display='block';
		return false;
	}
	// otherwise submit
	return true;
}

// check textfield for text without special characters or numbers
function checkText(field, name, notEmpty) {
	var value=field.value;
	var xChars=/[^a-zA-Z\ ]/;
	var error='';
	if (value=='') {
		if (notEmpty) {
			field.setAttribute('class', '');
		} else {
			error='Please enter your '+name+'. ';
			field.setAttribute('class', 'inValid');
		}
	} else
	if (xChars.test(value)) {
		error='name cannot contain special characters.<br />';
		field.setAttribute('class', 'inValid');
	} else {
		field.setAttribute('class', 'valid');
	}
	return error;
}

// check email address for text and proper structure
function checkEmail(field, notEmpty) {
	var value=field.value;
	var error='';
	var filter=/^.+@.+\..{2,5}$/;
	var xChars=/[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if (value=='') {
		if (notEmpty) {
			field.setAttribute('class', '');
		} else {
			error='Please enter your email address. ';
			field.setAttribute('class', 'inValid');
		}
	} else
	if (!(filter.test(value)) || xChars.test(value) ) {
		error='Please enter a valid email address. ';
		field.setAttribute('class', 'inValid');
	} else {
		field.setAttribute('class', 'valid');
	}
	return error;
}


// forms are automatically cleared on reset
// reset the error messages
function Reset(theForm) {
	document.getElementById(errordiv).innerHTML='';
	for (var i=0; i<theForm.elements.length; i++) {
		theForm.elements[i].setAttribute('class', '');
	}
}