function isStrEmpty (theField) {
  var tmpStr = theField.value;
  for (i = 0;  i < tmpStr.length;  i++)
  {
    var ch = tmpStr.charAt(i);
    if (ch != ' ') {
        return (false);
    }
  }
  return (true);
}

function isEmail(string) {

	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
}

function UserRegistrationCheck(theForm)
{
  if (isStrEmpty (theForm.UserId))
  {
    alert("Please enter a value for the \"UserId\" field.");
    theForm.UserId.focus();
    return (false);
  }

  if (theForm.UserId.value.length < 6)
  {
    alert("Please enter at least 6 characters in the \"UserId\" field.");
    theForm.UserId.focus();
    return (false);
  }
  if (theForm.UserId.value.length > 15)
  {
    alert("Please enter at most 15 characters in the \"UserId\" field.");
    theForm.UserId.focus();
    return (false);
  }

  var checkOK = "abcdefghijklmnopqrstuvwxyz0123456789_";
  var checkStr = theForm.UserId.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 SMALL letters and digit in the \"UserId\" field.");
    theForm.UserId.focus();
    return (false);
  }

  if (isStrEmpty (theForm.Password))
  {
    alert("Please enter a value for the \"Password\" field.");
    theForm.Password.focus();
    return (false);
  }

  if (theForm.Password.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"Password\" field.");
    theForm.Password.focus();
    return (false);
  }

  if (theForm.Password.value.length > 10)
  {
    alert("Please enter at most 10 characters in the \"Password\" field.");
    theForm.Password.focus();
    return (false);
  }
  if (theForm.Password.value == theForm.UserId.value)
  {
    alert("Please make the \"Password\" field different from the \"User Id\" field.");
    theForm.Password.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
  var checkStr = theForm.Password.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 letter, digit and \"_\" characters in the \"Password\" field.");
    theForm.Password.focus();
    return (false);
  }

  if (theForm.Password.value != theForm.Password2.value) {
    alert("\"Password\" and \"Retype Password\" fields appear to be different.  They need to be the same. Please try entering both fields again carefully.");
    theForm.Password2.focus();
    return (false);
  }

  if (theForm.PQuestion.selectedIndex == 0)
  {
    alert("Please enter password-question field.");
    theForm.PQuestion.focus();
    return (false);
  }

  if (isStrEmpty (theForm.PAnswer))
  {
    alert("Please enter password-question answer field.");
    theForm.PAnswer.focus();
    return (false);
  }
/*
  if (isStrEmpty(theForm.Email))
  {
    alert("Need to enter the \"Email\" field.");
    theForm.Email.focus();
    return (false);
  }
*/

  if (! isStrEmpty(theForm.Email)) {
    if (! isEmail(theForm.Email.value))
    {
      alert("Email provided appears to be invalid. Make sure you entered the \"Email\" in proper format and that you did not put extra spaces?");
      theForm.Email.focus();
      return (false);
    }
    if (theForm.Email.value != theForm.Email2.value) {
      alert("\"Retype Email\" field must be the same as \"Email\" field.");
      theForm.Email2.focus();
      return (false);
    }
  }

  if (isStrEmpty (theForm.Name))
  {
    alert("Please enter a value for the \"Name\" field.");
    theForm.Name.focus();
    return (false);
  }

  if (theForm.Name.value.length > 30)
  {
    alert("Please enter at most 30 characters in the \"Name\" field.");
    theForm.Name.focus();
    return (false);
  }

  var checkOK = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  var checkStr = theForm.Name.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 letter, digit and \"_\" characters in the \"Name\" field.");
    theForm.Name.focus();
    return (false);
  }

  if (isStrEmpty (theForm.Surname))
  {
    alert("Please enter a value for the \"Surname\" field.");
    theForm.Surname.focus();
    return (false);
  }

  if (theForm.Surname.value.length > 30)
  {
    alert("Please enter at most 30 characters in the \"Surname\" field.");
    theForm.Last.focus();
    return (false);
  }

  var checkOK = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  var checkStr = theForm.Surname.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 letter, digit and \"_\" characters in the \"Surname\" field.");
    theForm.Surname.focus();
    return (false);
  }

  return (true);
}
