//**********************************************************
// this java script coding checks acts as a basic form 
// validator with several kind of checks .
//**********************************************************
//COPYRIGHT HOLDER:  ATUL SEHGAL @ REAL TIME.  
//**********************************************************

function regformvalidator(RegForm)
{

//----------------------------------------------------------------------------------------------

	  if (RegForm.Title.selectedIndex==-1)
	  {
			alert("Please Select Title!");
			RegForm.Title.focus();
			return false;
	  }
     
//----------------------------------------------------------------------------------------------

	if (RegForm.FirstName.value=="")
	{
			alert("First Name cannot be null");
			RegForm.FirstName.focus();
			return (false);
	}
	
	if (RegForm.FirstName.value.length < 2)
	{
		alert("Please enter at least 2 characters in the \"FirstName\" field.");
		RegForm.FirstName.focus();
		return (false);
	}		

		var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .";
		var checkStr =RegForm.FirstName.value;
		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("Please enter only valid entries in the \"First Name\" field.");
		  RegForm.FirstName.focus();
		  allValid = true;
		  return (false);
		}

//----------------------------------------------------------------------------------------------
    
     if (RegForm.LastName.value=="")
     {
			alert("LastName cannot be null");
			RegForm.LastName.focus();
			return false;
	 }

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .";
	var checkStr = RegForm.LastName.value;
	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("Please enter only valid entries in the \"Last Name\" field.");
	  RegForm.LastName.value="";
	  RegForm.LastName.focus();
	  allValid = true;
	  return (false);
	 }

//----------------------------------------------------------------------------------------------

     if (RegForm.Age.value=="")
	 {
	  alert("Age cannot be null");
	  RegForm.Age.focus();
	  return false;
	 } 
	 //&&(RegForm.Age.value > 120)
	 if  ( (isNaN(RegForm.Age.value)==true)||(RegForm.Age.value < 1) )
	 {
	   alert("Enter the Age in Positive Numbers");
	   RegForm.Age.focus();
	   return false;
	 } 
     
     if (RegForm.Age.value >120  )
	{
	   alert("Enter the Age Less Than 120");
	   RegForm.Age.focus();
	   return false;
	}

//----------------------------------------------------------------------------------------------


  if (RegForm.Address.value=="")
  {
		alert("Address 1 cannot be null");
		RegForm.Address.focus();
		return false;
  } 

  var checkadd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 -/,.#";
  var checkStr = RegForm.Address.value;
  var allValid = true;
  
  	for (i = 0;  i < checkStr.length;  i++)
		{
		  ch = checkStr.charAt(i);
		  for (j = 0;  j < checkadd.length;  j++)
		    if (ch == checkadd.charAt(j))
		      break;
		  if (j == checkadd.length)
		  {
		    allValid = false;
		    break;
		  }
		}
  if (!allValid)
  {
    alert("Please enter only valid entries in the \"Address 1\" field.");
    RegForm.Address.value="";
    RegForm.Address.focus();
    allValid = true;
    return (false);
  }
  
  
//----------------------------------------------------------------------------------------------

  var checkadd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 -/,.#";
  var checkStr = RegForm.Address1.value;
  var allValid = true;
  	for (i = 0;  i < checkStr.length;  i++)
		{
		  ch = checkStr.charAt(i);
		  for (j = 0;  j < checkadd.length;  j++)
		    if (ch == checkadd.charAt(j))
		      break;
		  if (j == checkadd.length)
		  {
		    allValid = false;
		    break;
		  }
		}
	if (!allValid)
	{
	  alert("Please enter only valid entries in the \"Address 2\" field.");
	  RegForm.Address1.value="";
	  RegForm.Address1.focus();
	  allValid = true;
	  return (false);
	}


//----------------------------------------------------------------------------------------------


 if (RegForm.City.value=="")
  {
		alert("City cannot be null");
		RegForm.City.focus();
		return false;
  }
  
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .";
  var checkStr = RegForm.City.value;
  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("Please enter only valid entries in the \"City\" field.");
    RegForm.City.value="";
    RegForm.City.focus();
    allValid = true;
    return (false);
   }


//----------------------------------------------------------------------------------------------


if (RegForm.State.value=="")
  {
		alert("State cannot be null");
		RegForm.State.focus();
		return false;
  }
   var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .";
   var checkStr = RegForm.State.value;
   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("Please enter only valid entries in the \"State\" field.");
     RegForm.State.value="";
     RegForm.State.focus();
     allValid = true;
     return (false);
   }

//----------------------------------------------------------------------------------------------

 
   if (RegForm.PinCode.value=="")
  {
		alert("PinCode cannot be null");
		RegForm.PinCode.focus();
		return false;
  }
  
   //CHECK FOR CORRECT LENGTH
   if(RegForm.PinCode.value.length<5)
   { 
		alert("PIN \ ZIP Code cannot be less than 5 no's");
		RegForm.PinCode.focus();
		return false;
   }
   
  var checkpc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 -";
  var checkStr = RegForm.PinCode.value;
  var allValid = true;
  
      for (i = 0;  i < checkStr.length;  i++)
		{
		  ch = checkStr.charAt(i);
		  for (j = 0;  j < checkpc.length;  j++)
		    if (ch == checkpc.charAt(j))
		      break;
		  if (j == checkpc.length)
		  {
		    allValid = false;
		    break;
		  }
		}

  
  if (!allValid)
  {
    alert("Please enter only valid entries in the \"PinCode\" field.");
    RegForm.PinCode.value="";
    RegForm.PinCode.focus();
    allValid = true;
    return (false);
  }


//----------------------------------------------------------------------------------------------

 
  if ((RegForm.Country.selectedIndex==-1)||(RegForm.Country.selectedIndex==0))
  {
		alert("Please Select Your Country!");
		RegForm.Country.focus();
		return false;
  }

  
  
//----------------------------------------------------------------------------------------------
  
  if (RegForm.CountryCode.value=="")
  {
   alert("Country Code cannot be null");
   RegForm.CountryCode.focus();
   return false;
  }
  
 // CHECK FOR NOT A NUMBER and positive number
	if ((isNaN(RegForm.CountryCode.value)==true)||(RegForm.CountryCode.value < 1))
	{
	   alert("Enter the Country Code in Positive Numbers");
	   RegForm.CountryCode.focus();
	   return false;
	} 
	
  
//----------------------------------------------------------------------------------------------
  
  if (RegForm.CityCode.value=="")
  {
   alert("CityCode cannot be null");
   RegForm.CityCode.focus();
   return false;
  }
  
  // CHECK FOR NOT A NUMBER and positive number
	if ((isNaN(RegForm.CityCode.value)==true)||(RegForm.CityCode.value < 1))
	{
	   alert("Enter the City Code in Positive Numbers");
	   RegForm.CityCode.focus();
	   return false;
	} 
  
  
  
//----------------------------------------------------------------------------------------------
  
 if (RegForm.Phone.value=="")
  {
   alert("Phone cannot be null");
   RegForm.Phone.focus();
   return false;
  }
  
  // CHECK FOR NOT A NUMBER and positive number
	if ((isNaN(RegForm.Phone.value)==true)||(RegForm.Phone.value < 1))
	{
	   alert("Enter the Phone in Positive Numbers");
	   RegForm.Phone.focus();
	   return false;
	} 
	
	if (RegForm.Phone.value.length < 5)
	{
	   alert("Enter atleast Five digits Phone Number");
	   RegForm.Phone.focus();
	   return false;
	}
	
//----------------------------------------------------------------------------------------------

	
	// CHECK FOR NOT A NUMBER and positive number
	if ((isNaN(RegForm.Fax.value)==true)||(RegForm.Fax.value < 1))
	{
	   alert("Enter the Fax in Positive Numbers");
	   RegForm.Fax.focus();
	   return false;
	}

	if (RegForm.Fax.value!="")
	{
		if (RegForm.Fax.value.length < 5)
		{
			alert("Enter atleast Five digits Fax Number");
			RegForm.Fax.value="";
			RegForm.Fax.focus();
			return false;
		}
	}	
	
//----------------------------------------------------------------------------------------------
  
  if (RegForm.Email.value=="")
  {
		alert("Email cannot be null");
		RegForm.Email.focus();
		return false;
  }
  
  var checkEmail = "@.";
  var checkStr = RegForm.Email.value;
  var EmailValid = false;
  var EmailAt = false;
  var EmailPeriod = false;
  
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkEmail.length;  j++)
    {
      if (ch == checkEmail.charAt(j) && ch == "@")
        EmailAt = true;
      if (ch == checkEmail.charAt(j) && ch == ".")
        EmailPeriod = true;
	  if (EmailAt && EmailPeriod)
		break;
	  if (j == checkEmail.length)
		break;
     }
                          // if both the @ and . were in the string
    if (EmailAt && EmailPeriod)
    {
		EmailValid = true
		break;
    }
  }
  if (!EmailValid)
  {
    alert("The \"Email\" field has invalid entry");
    RegForm.Email.focus();
    return (false);
  }

  if(RegForm.Email.value.length<7)
  {
		alert("Email address can not be less than Seven chars");
		RegForm.Email.focus();
		return false;
  }

  
  
//----------------------------------------------------------------------------------------------
}
