function validCharacters(checkStr)
{
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_ ";
	var allValid = true;
	var validGroups = 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;
	    }
  	}

	return allValid;
}

function validNumbers(checkStr)
{
  var checkOK = "0123456789-.";
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";

  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 (ch == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else
    {allNum += ch;}
  }

  if (!allValid)
  {return (false);}

  if (decPoints > 1 || !validGroups)
  {
    return (false);
  }

  return (true);
}


function trim(str, chars)
{return ltrim(rtrim(str, chars), chars);}

function ltrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function checkrequired(type)
{
    var pass=true;
    var pwd = "";
    var cpwd = "";
    var f1 = document.getElementById("f1");

    if (type == '1')
    {
      if (trim(f1.tfirst_name.value).length == 0)
      {
         alert("First Name cannot be empty.");
         f1.tfirst_name.focus();
         return false;
      }
      if (!validCharacters(f1.tfirst_name.value))
      {
          alert("First Name cannot contain any special characters. Only albhabets [A-Z], numerals [0-9] and underscore[_] are allowed.");
          f1.tfirst_name.focus();
          return false;
      }


      if (trim(f1.tlast_name.value).length == 0)
      {
         alert("Last Name cannot be empty.");
         f1.tlast_name.focus();
         return false;
      }
      if (!validCharacters(f1.tlast_name.value))
      {
          alert("Last Name cannot contain any special characters. Only albhabets [A-Z], numerals [0-9] and underscore[_] are allowed.");
          f1.tlast_name.focus();
          return false;
      }

      if (!emailCheck(f1.temail_id.value))
      {return false;}


      if (trim(f1.taddress.value).length == 0)
      {
         alert("Address cannot be empty.");
         f1.taddress.focus();
         return false;
      }


      if (trim(f1.tcity.value).length == 0)
      {
         alert("City cannot be empty.");
         f1.tcity.focus();
         return false;
      }
      if (!validCharacters(f1.tcity.value))
      {
          alert("City cannot contain any special characters. Only albhabets [A-Z], numerals [0-9] and underscore[_] are allowed.");
          f1.tlast_name.focus();
          return false;
      }


      if (trim(f1.tstate.value).length == 0)
      {
         alert("State cannot be empty.");
         f1.tstate.focus();
         return false;
      }
      if (!validCharacters(f1.tstate.value))
      {
          alert("State cannot contain any special characters. Only albhabets [A-Z], numerals [0-9] and underscore[_] are allowed.");
          f1.tstate.focus();
          return false;
      }

      if (trim(f1.tzipcode.value).length == 0)
      {
         alert("Zip Code cannot be empty.");
         f1.tzipcode.focus();
         return false;
      }
      if (!validCharacters(f1.tzipcode.value))
      {
          alert("Zip Code cannot contain any special characters. Only albhabets [A-Z], numerals [0-9] and underscore[_] are allowed.");
          f1.tzipcode.focus();
          return false;
      }

      if (trim(f1.tbirth_day.value).length == 0)
      {
         alert("Invalid Birth Date. Specify a valid birth date");
         f1.tbirth_day.focus();
         return false;
      }
      if (!validNumbers(f1.tbirth_day.value))
      {
          alert("Invalid Birth Date. Day should contain only numbers.");
          f1.tbirth_day.focus();
          return false;
      }


      if (trim(f1.tbirth_year.value).length != 4)
      {
         alert("Invalid Birth Year. Year must be of 4 digit.");
         f1.tbirth_year.focus();
         return false;
      }
      if (!validNumbers(f1.tbirth_year.value))
      {
          alert("Invalid Birth Date. Year should contain only numbers.");
          f1.tbirth_year.focus();
          return false;
      }
    }
    else if (type == '2')
    {
      if (!f1.tagree.checked)
      {
          alert("You cannot proceed ahead without accepting the user aggreement.");
          return false;
      }
    }
    return true;
}

function validate(type)
{
	if (checkrequired(type))
	{return true;}
	else
	{return false;}
}

