var refno=/[A-Z,a-z]{5,30}/;
var para="";
var s ="";
function validate()
{
	var isOK = false;
	alert(isOK);
	if (refno.test(regForm.REFER1.value))
	{
		alert("Entry Ok");
	 	isOK = true;
	 }
	else {
		alert('Invalid Reference Number. Please try again.');
		isOK = false;
		//event.returnValue=false;
	}
	alert(isOK);
	event.returnValue = isOK;
	
}

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);
  }  
}

function validate_register(theForm)
{
  if (theForm.TUsername.value.length > 12){
    alert("User name too long.");
    theForm.TUsername.focus();
    return (false);
   }

   if (isBadWord(theForm.TUsername.value)){
  	 theForm.TUsername.focus();
  	 return (false);
   }	

  if (!isWhitespace(theForm.TUsername.value)){
    alert("User name should have no space on it. It should be one word only.\nYou may use " +
    "underscore or dash.\n\n Example. Jerryboy, sample_name, my-name");
    theForm.TUsername.focus();
    return (false);
  }

  if (theForm.TUsername.value == ""){
    alert("Please enter a value for the Username field.");
    theForm.TUsername.focus();
    return (false);
  }

  if ((theForm.TLastname.value == "")||(theForm.TLastname.value.length < 3)){
    alert("Please enter a valid value for the Lastname field.");
    theForm.TLastname.focus();
    return (false);
  }

  if (isBadWord(theForm.TLastname.value)){
  	 theForm.TLastname.focus();
  	 return (false);
  }	

  if ((theForm.TFirstname.value == "")||(theForm.TFirstname.value.length < 3)){
    alert("Please enter a valid value for the Firstname field.");
    theForm.TFirstname.focus();
    return (false);
  }

  if ((theForm.TMI.value.length == "")||(theForm.TFirstname.value.length < 1)){
    alert("Please enter a valid value for the Middle name field.");
    theForm.TMI.focus();
    return (false);
  }

  if (isBadWord(theForm.TFirstname.value)){
  	 theForm.TFirstname.focus();
  	 return (false);
   }	

  if (theForm.TLastname.value == theForm.TFirstname.value){
    alert("First name and Last name shouldn't be the same.");
    theForm.TLastname.focus();
    return (false);
  }

  if (theForm.TPassword.value != theForm.TPassword1.value){
 	 alert("Your password does not match.\n\nPlease try again.");
 	 theForm.TPassword.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 (!AllowOnly("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz- \r\n\f",theForm.TLastname.value,"Lastname",2)) {
    	theForm.TLastname.focus();
    	return (false);
   }

   if (!AllowOnly("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz \r\n\f",theForm.TFirstname.value,"Firstname",3))    {
    	theForm.TFirstname.focus();
    	return (false);
    }

   if (!AllowOnly("0123456789- ",theForm.TPhone.value,"Telephone number",0))  {
    	theForm.TPhone.focus();
    	return (false);
    }

	if (veryLongWord(theForm.SLocation.value)){
  		alert("You have entered a very long word or you did" +
  		"\nnot have the proper simple punctuations." +
  		"\n\nPlease check your entry.");
  		theForm.SLocation.focus();
  		return (false);
	}
	
	if (theForm.SLocation.value.length < 15){
  		alert("Your address location is too short." +
  		"\nWe'd like you to be meaningful to BIBAK.org community." +
  		"\n\nPlease check your entry.");
  		theForm.SLocation.focus();
  		return (false);
	}
	
   if (theForm.R1[0].checked==false && theForm.R1[1].checked==false){
   	 alert("Please indicate your gender.");
  	 theForm.R1[0].focus();
     return (false);
   } else {
   	 if (theForm.R1[0].checked) s = theForm.R1[0].value;
   	 if (theForm.R1[1].checked) s = theForm.R1[1].value;
   }
	
	para="u="+encodeURI(theForm.TUsername.value)+"&p="+encodeURI(theForm.TPassword.value)+"&l="+encodeURI(theForm.TLastname.value)+
		"&f="+encodeURI(theForm.TFirstname.value)+"&mi="+theForm.TMI.value+"&e="+theForm.TEmail.value+
		"&w="+encodeURI(theForm.TWebsite.value)+"&pr="+encodeURI(theForm.DProvince.value)+"&add="+encodeURI(theForm.SLocation.value)+
		"&c="+encodeURI(theForm.DCountry.value)+"&t="+encodeURI(theForm.TPhone.value)+"&s="+s;
	//alert(para);
  return (true);
}

function isWhitespace(s)
{
	var whitespace = " ";
	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (whitespace.indexOf(c) != -1) return false;
	}
	return true;
}

function isEmpty(s)
{
	return ((s == null) || (s.length ==0));
}



