// JavaScript Document

function valform(arg)
{
 with(arg)
 { 
 
  		if(emptyvalidation(name,"You forgot to enter your name")==false)
		{
			name.focus();
			return false;
		}
		if(name.value!="")
		{
			if(validate(name,0)==false)
			{
		  	window.alert("You have entered an invalid name");
		  	name.focus();
			name.select();
			return false;
			}
		}
			  if(emptyvalidation(email,"You forgot to enter email address")==false)
			{
				email.focus();
				return false;
			}	
	  if(email.value!="")
			{
				if(validate(email,1)==false)
				{
				   window.alert("You have entered an invalid email address");
				   email.focus();
				   email.select();
				   return false;
				}
			} 
		if(emptyvalidation(address,"You forgot to enter address")==false)
		{
			address.focus();
			return false;
		}
		if(emptyvalidation(tel,"You forgot to enter phone number")==false)
		{
			tel.focus();
			return false;
		}	
		if(tel.value!="")
		{
		if(validate(tel,2)==false)
		{
			window.alert("You have entered an invalid phone number");
			tel.focus();
			tel.select();
			return false;
			}
		}
		if(emptyvalidation(comments,"You forgot to enter comments")==false)
		{
			comments.focus();
			return false;
		}

		if(emptyvalidation(ver_code,"You forgot to enter the verification code")==false)
		{
			ver_code.focus();
			return false;
		}
				
		}
  arg.submit();
}

function emptyvalidation(entered, alertbox)
{
	with (entered)
	{
		while (value.charAt(0) == ' ')
			value = value.substring(1);
		while (value.charAt(value.length - 1) == ' ')
			value = value.substring(0, value.length - 1);
		if (value==null || value=="")
		{
			if (alertbox!="") alert(alertbox);
			return false;
		}
		else return true;
	}
}

function validate(objForm,field) 
{
	var reg; //0: name, 1: email, 2: phone
	switch(field)
	{
	case 0:
		reg = /^[A-Za-z ]{3,}$/;
		break;
	case 1:
		reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		break;
	case 2:
		reg = /^[0-9\-]{1,}$/;
		break;
	case 3:
		reg = /^[0-9\-]{5,9}$/;
		
	}
  // var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = objForm.value;
   if(reg.test(address) == false)
   {
      //alert('Invalid Email Address');
      return false;
   }
}