﻿//Consider elem.focus elem.select
//Automate the process of selecting the ElementById

function formValidator() //Function is called by onsubmit event handler
{

	var isvalid = true; //Set isvalid to be tested throughout validation

	var name = document.getElementById('name').value;
	var nameerror = document.getElementById('name_error').style;
	var telno = document.getElementById('telno').value;
	var telnoerror = document.getElementById('telno_error').style;	
	var email = document.getElementById('email').value;
	var emailerror = document.getElementById('email_error').style

	if (name.length < 1) //Test that bandname is not empty
	{
		nameerror.visibility = "visible"; //If empty display the error by changing the style.visibility
		isvalid = false; //Set isvalid to false to ensure form is not processed
	}
	else
	{
		nameerror.visibility = "hidden"; //If not empty hide error by changing the style.visibility
	}
	
	var telnoExp = /^[0-9 ()]+$/; //telno regualr expression
	if (telno.length > 0)
	{
	if (!telno.match(telnoExp)) //if the input does not match the regular expression, show error
	{
		telnoerror.visibility = "visible";
		isvalid = false;
	}
	else
	{
		telnoerror.visibility = "hidden";
	}
	}   
	
	var emailExp = /^[A-Za-z0-9._-]+@[A-Za-z0-9-]+(\.[A-Za-z.]{2,5})*(\.[A-Za-z]{2,5})$/; //email regular expression
	if (!email.match(emailExp)) //if the input does not match the regular expression, show error
	{
		emailerror.visibility = "visible";
		isvalid = false;
	}
	else
	{
		emailerror.visibility = "hidden";
	}
	
	return isvalid;	

}