function isEmpty(string) {
	var whitespace = " \t\n\r";

	if ((string == null) || (string.length == 0)) {
		return true;
	}
    for (i = 0; i < string.length; i++) {
		var chr = string.charAt(i);
		if (whitespace.indexOf(chr) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}

function tooLong(string, max) {
	if (string.length > max) {
		return true;
	}
}

function stripChars (string, accept) {
// string to clean, acceptable characters
	var i;
	var returnString = "";

	for (i = 0; i < string.length; i++) {
		var chr = string.charAt(i);
		if (accept.indexOf(chr) != -1) returnString += chr;
	}

	return returnString;
}

function showError(ourform, where, warning) {
	ourform.elements[where].focus();
	if (document.getElementById) {
		document.getElementById(where).style.backgroundColor = '#fc0';
	}
	alert(warning);
}


function checkForm() {
	ourform = document.forms['contact'];
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;

	if (document.getElementById) {
		document.getElementById('name').style.backgroundColor = '#eee';
		document.getElementById('email').style.backgroundColor = '#eee';
		document.getElementById('msg').style.backgroundColor = '#eee';
	}

	if (isEmpty(ourform.name.value)) {
 		showError(ourform, "name", "Please make sure you enter your name");
		return false;
	}

	if (isEmpty(ourform.email.value)) {
 		showError(ourform, "email", "Please make sure you enter your email address");
		return false;
	}

	if (isEmpty(ourform.msg.value)) {
 		showError(ourform, "msg", "Please make sure you enter a message");
		return false;
	}

	if (!(filter.test(ourform.email.value))) {
		showError(ourform, "email", "Please check your email address - it does not appear to be valid");
		return false;
	}

	if (tooLong(ourform.name.value, 40)) {
 		showError(ourform, "name", "Sorry, your name exceeds the maximum 40 characters");
		return false;
	}

	if (tooLong(ourform.email.value, 80)) {
 		showError(ourform, "name", "Sorry, your name exceeds the maximum 80 characters");
		return false;
	}

	if (tooLong(ourform.phone.value, 40)) {
 		showError(ourform, "name", "Sorry, your name exceeds the maximum 40 characters");
		return false;
	}

	else {
		if (document.all||document.getElementById) {
			document.forms[0].submit.disabled=true;
		}
		return true;
	}
}
