// a function to validate valid characters as entries
function AllowOnly(checkOK,checkStr,Mess,valuelen)
{
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }

  if (!allValid){
    alert("You have an invalid entry for your " + Mess + " field.\nCheck the following:\n\n" +
    "Names should contain valid characters only.\nTel Numbers shoud contain numbers only.\n\Address may contain alpha-numeric characters.");
    return (false);
  } 

  if (checkStr.length < valuelen) {
  	alert(Mess + " is required and it seems that the you input less or equal to " + valuelen + " characters.\n\n"+
  	"Please check it again." );
    return (false);
  } else {
  	return (true);
  }  
}

//Check valid emails
function isEmailAddr(email){
var result = false;
if (email.length > 3){
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0){
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1)&&(theStr.length > pindex+1))
      result = true;
  }
}
return result;
}

//Start validating the entries in the form
function validator(theForm)
{
  if (theForm.TName.value == ""){
    alert("Please enter your name as a contact person.");
    theForm.TName.focus();
    return (false);
  }
  
    if (!AllowOnly("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz \r\n\f",theForm.TName.value,"Name",3)) {
    	theForm.TName.focus();
    	return (false);
   }

	if (!isEmailAddr(theForm.TEmail.value)){
  		alert("Email format is not valid.");
  		theForm.TEmail.focus();
  	return (false);
  }

   if (!AllowOnly("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz \r\n\f",theForm.TUsername.value,"Username",3))    {
    	theForm.TUsername.focus();
    	return (false);
    }

	if (theForm.pass1.value != theForm.pass2.value){
		alert ("Your password does not match. Please re-type password.");
		theForm.pass2.focus();
		return (false);
	}
	
   if (!AllowOnly("0123456789- ",theForm.TPhone.value,"Telephone number",7))  {
    	theForm.TPhone.focus();
    	return (false);
    }
}