/* -- form helper / contact form -- */
function verify_contactform()
{
	var error="NONE";

	// check name
	error=chckName(document.contactform.name.value);
	if(error != "NONE") { alert(error); document.contactform.name.focus(); document.contactform.name.select(); }
	else
	{
		// check company ONLY if entered
		if (document.contactform.company.value != "") { error=chckName(document.contactform.company.value); }
		if(error != "NONE") { alert(error); document.contactform.company.focus(); document.contactform.company.select(); }
		else
		{
			// check email
			error=chckEmail(document.contactform.email.value, document.contactform.email2.value);
			if (error != "NONE") { alert(error); document.contactform.email.focus(); document.contactform.email.select(); }
			else
			{
				// check message [max 350 characters]
				error=charLimit(document.contactform.message.value,350);
				if(error != "NONE") { alert(error); document.contactform.message.focus(); document.contactform.message.select(); }
				else
				{
					// send if no error
					document.contactform.submit();
				}
			}
		}
	}
}

/* -- subroutines -- */
function chckName(str)
{
	if (str.length < 2) { return "Name must contain atleast 2 characters."; }
	var illegalChars = /[\W_]/; // allow only letters and numbers
	if ( illegalChars.test(str.replace(/[\(\)\.\-\ ]/g, '')) ) { return "Name must contain only characters or numbers."; }
	return "NONE";
}
function chckEmail(str1,str2)
{
	// check if match
	if ( str1 != str2) { return "Email mismatch."; }

	var filter=/^.+@.+\..{2,3}$/
	if (! filter.test(str1) ) return "The email address is invalid.";
	return "NONE";
}
function charLimit(str, limit)
{
	if(str.length > limit) { return "The text exceeds a limit of "+limit+" characters."; }
	if(str.length <= 0) { return "The text is empty."; }
	return "NONE";
}