function validate_contact_by_email_form()
{
    	 firstname = document.contact_by_email.firstname;
   	 lastname = document.contact_by_email.lastname;
   	 email = document.contact_by_email.email;

 if (IsEmpty(firstname))
   {
   	   firstname.style.background = 'Yellow';
	   alert(firstname_lng_al+" "+error_required_lng_al);
	   return false;
   }
 if (!validateUsername(firstname))
   {
   	   firstname.style.background = 'Yellow';
	   alert(firstname_lng_al+" "+error_required_lng_al+", "+error_alpha_lng_al);
	   return false;
   }
 firstname.style.background = 'White';
 if (IsEmpty(lastname))
   {
   	   lastname.style.background = 'Yellow';
	   alert(lastname_lng_al+" "+error_required_lng_al);
	   return false;
   }
 if (!validateUsername(lastname))
   {
    	   lastname.style.background = 'Yellow';
 	   alert(lastname_lng_al+" "+error_required_lng_al+", "+error_alpha_lng_al);
 	   return false;
   }
 lastname.style.background = 'White';

 if (IsEmpty(email))
   {
	   alert(email_lng_al+" "+error_required_lng_al);
	   return false;
   }
  if (!validateEmail(email))
     {
        email.style.background = 'Yellow';
  	   alert(email_lng_al+" "+error_required_lng_al+", "+error_val_lng_al);
  	   return false;
   	}
   email.style.background = 'White';
   return true;
}

function validate_contact_by_phone_form()
{
    	 firstname = document.contact_by_phone.firstname;
   	 lastname = document.contact_by_phone.lastname;
   	 phone = document.contact_by_phone.phone;
   	 mobile = document.contact_by_phone.mobile;

 if (IsEmpty(firstname))
   {
   	   firstname.style.background = 'Yellow';
	   alert(firstname_lng_al+" "+error_required_lng_al);
	   return false;
   }
 if (!validateUsername(firstname))
   {
   	   firstname.style.background = 'Yellow';
	   alert(firstname_lng_al+" "+error_required_lng_al+", "+error_alpha_lng_al);
	   return false;
   }
 firstname.style.background = 'White';
 if (IsEmpty(lastname))
   {
   	   lastname.style.background = 'Yellow';
	   alert(lastname_lng_al+" "+error_required_lng_al);
	   return false;
   }
 if (!validateUsername(lastname))
    {
    	   lastname.style.background = 'Yellow';
 	   alert(lastname_lng_al+" "+error_required_lng_al+", "+error_alpha_lng_al);
 	   return false;
   }
 lastname.style.background = 'White';
 if (IsEmpty(phone) && IsEmpty(mobile))
   {
   	   phone.style.background = 'Yellow';
   	   mobile.style.background = 'Yellow';
	   alert(phone_lng_al+" "+or_lng_al+" "+mobile_lng_al+" "+error_required_lng_al);
	   return false;
   }

 if (!validatePhone(phone) && !IsEmpty(phone))
    {
     if (IsEmpty(mobile))
    	   {
         mobile.value = "";
    	   }
    	   phone.style.background = 'Yellow';
    	   mobile.style.background = 'White';
 	   alert(phone_lng_al+" "+error_numeric_lng_al);
 	   return false;
    }
 phone.style.background = 'White';
 if (!validatePhone(mobile) && !IsEmpty(mobile))
    {
    	   if (IsEmpty(phone))
    	   	{
    	   	 phone.value = "";
    	   	}
    	   mobile.style.background = 'Yellow';
    	   phone.style.background = 'White';
 	   alert(mobile_lng_al+" "+error_numeric_lng_al);
 	   return false;
    }
 mobile.style.background = 'White';

   return true;
}

function IsEmpty(aTextField)
{
	var pattern ="[A-z0-9_ /\+-]+";
	var re = new RegExp(pattern);

   if ((aTextField.value.length==0) || (aTextField.value==null) || (aTextField.value=="")
	   || (aTextField.value==" "))
   {
      return true;
   }
   if (trim(aTextField.value).match(re))
	{
      return false;
	}
  return true;
}

function validatePhone(input_str)
{
   var ValidChars = "0123456789-+";
   var numericChars = "0123456789";
   var strChar;
   var blnResult = true;
   var stripped = input_str.value.replace(/[\(\)\.\-\+\ ]/g, '');

   if (input_str.value.length == 0)
	   return false;
   if (stripped.length < 3)
   	   return false;
   if ((stripped.length == 3)  && (input_str.value == '000'))
	   return false;

   for (i = 0; i < input_str.value.length && blnResult == true; i++)
   {
      strChar = input_str.value.charAt(i);
	 if (ValidChars.indexOf(strChar) == -1)
        {
         blnResult = false;
        }
      if ((strChar == '+') || (strChar == '-'))
        {
		if (i > 0 && i < input_str.value.length - 1)
		  {

			preCh = input_str.value.charAt(i-1);
			nexCh = input_str.value.charAt(i+1);
			if ((numericChars.indexOf(preCh) == -1) || (numericChars.indexOf(nexCh) == -1))
			  {
			   blnResult = false;
			  }
		  }
        }
   }
   return blnResult;
 }



function validateUsername(fld)
{
    var valResult = true;
    var illegalChars = /[^A-z0-9_-]/; // allow letters, numbers, and underscores
    var allowedChars = /[A-z]{2}/;// Name must contain at least 2 letters
    var numericChars = /[0123456789_-]/;//Name cann't contain 2 numbers, and underscores together.
    var MINLEN = 2;// Name must contain at least 2 letters
    var MAXLEN = 25;
    if (fld.value == "")
    	 {
        valResult = false;
    	 }
    else if ((fld.value.length < MINLEN) || (fld.value.length > MAXLEN))
    	 	 {
        	  valResult = false;
    		 }
    	    else if ((illegalChars.test(fld.value)) || (fld.value.indexOf("__") != -1)
    	    		    || (fld.value.indexOf("--") != -1) || (fld.value.indexOf("_-") != -1)
    	    		    || (fld.value.indexOf("-_") != -1))//ValidChars.indexOf(strChar) == -1
    	    		 {
         		  valResult = false;
    			 }
    	    	    else if (!allowedChars.test(fld.value))
    	    		 	 {
         		  	  valResult = false;
    			 	 }
    	    	    		 else
    	    	    		 	{
    	    	    		 	 for (i = 0; i < MINLEN && valResult == true; i++)
    	    	    		 	 	{
	    	    	    		 	 if (numericChars.test(fld.value.charAt(i)))
    	    		 	 	   	   {
         		  	  	    	    valResult = false;
    			 	 	   	   }
    	    	    		 	 	}
    			 	 	}
    return valResult;
}

function validateEmail(fld) {
    var valEmResult=true;
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "")
    	 {
       valEmResult = false;
      }
    else if (!emailFilter.test(tfld)) //test email for illegal characters
    		 {
        	  valEmResult = false;
    		 }
    	    else if (fld.value.match(illegalChars))
    	    		 {
        		  valEmResult = false;
    			 }
    return valEmResult;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

 /*

   // Check for valid phone number.

   // Checks if the user has supplied a non empty phone number
   if (isset($_REQUEST['phone']) && !empty($_REQUEST['phone']))

       // String without spaces
   {
       $phoneNumber = trim($_REQUEST['phone']);

       // Regex to make sure the phone number is all digits incl =-()."  " and 2 to 15 characters long

       if (ereg("[0-9\.\9\(\)\-\_\+' '\]{2,15}$", $phoneNumber) == false) {

       // Append to the valResult list if the regex failed
           $valResults[] = "valResult
   "

   // Bunch of HTML code to make valResult screen look nice

   ;}}

   */


/*

function validatePhone(fld) {
    var valResult = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

   if (fld.value == "") {
        valResult = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        valResult = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        valResult = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return valResult;
}

*/


function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
