// JavaScript Document
function validateFormOnSubmit(theForm)
{
var reason = "";

  reason += validateUsername(theForm.user_name);
  reason += validatePhone(theForm.phone_number);
  reason += validateEmail(theForm.email_id);
       
  if (reason != "")
  {
    alert("The following error(s) occurred:\n" + reason);
    return false;
  }

  return true;
}


function validateUsername(fld)
{
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "")
	{
       error = "-Name is required.\n";
    } 
	else if (illegalChars.test(fld.value))
	{
         error = "The username contains illegal characters.\n";
    }
	else
	{
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld)
{
    var error = "";
    var stripped = fld.value;

   if (fld.value == "")
   {
        error = "- Phone number is required.\n";
   }    
   else if (!(stripped.length >= 6) || !(stripped.length <= 11))
   {
     error = "-Please enter the correct phone number.\n";
   }
   else if (isNaN(parseInt(stripped)))
   {
      error = "The phone number contains illegal characters.\n";
        
   } 
    return error;
}



function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld)
{
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "")
	{
       error = "- Email id is required.\n";
    } 
	else if (!emailFilter.test(tfld))
	{              //test email for illegal characters
        error = "Please enter a valid email address.\n";
    } 
	else if (fld.value.match(illegalChars))
	{
       error = "Please enter the correct  email address.\n";
    } 
	else
	{
        fld.style.background = 'White';
    }
    return error;
}




