function isAGoodEmail(email) {
  if (email.indexOf(" ")!=-1) return false;
  
  var atPos=email.indexOf("@");
  if (atPos<=0) return false;
  
  var dotPos=email.indexOf(".");
  if ((dotPos==email.length-1) || (dotPos==email.length-2)) return false;

  return true;
}

function isAGoodAge(age) {
  for (i=0; i<age.length; i++) {
    var j=age.charAt(i);
    if ((i==0) && (j==0)) return false;
    if ((j<'0') || (j>'9')) return false;
  }
  if (age > 127) return false;
  return true;	
}

function precheck(myform) {
  var username = myform.username.value;
  var password = myform.password.value;
  var pass2 = myform.pass2.value;
  var emailaddress = myform.emailaddress.value;
  var age = myform.age.value;
  
  if (username == "") {
    alert("您忘了输入用户代号。");
    myform.username.focus();
    return false;
  } else {
    for (i=0; i<username.length; i++) {
      unicode=username.charCodeAt(i);
      if (unicode <= 255) {
        if ((unicode<45) || ((unicode>46)&&(unicode<48)) || ((unicode>57)&&(unicode<65)) || ((unicode>90)&&(unicode<95)) || (unicode==96) || (unicode>122)) {
          alert("用户代号中请不要使用除 - 及 _ 及 . 之外的任何标点符号！");
          return false;
        }
      }
    }	
  }
  
  if (password == "") {
    alert("您忘了输入登录密码。");
    myform.password.focus();
    return false;
  }
  if (pass2 == "") {
    alert("您忘了确认密码。");
    myform.pass2.focus();
    return false;
  }
  if (emailaddress == "") {
    alert("您忘了输入电子邮件地址。");
    myform.emailaddress.focus();
    return false;
  }
  
  if (pass2 != password) {
    alert("两次输入的密码不一致。");
    myform.password.focus();
    return false;
  }
  if (!isAGoodEmail(emailaddress)) {
    alert("请输入您真实有效的电子邮件地址。");
    myform.emailaddress.focus();
    return false;
  }
  
  if (!(myform.agreement.checked)) {
    alert("请接受服务协议。");
    return false;
  }

  if ((age != "") && (!isAGoodAge(age))) {
    alert("请输入您的真实年龄。");
    myform.age.focus();
    return false;
  }

  // Save this form to the cookies
  var realname = myform.realname.value;
  var gender = 0;
  if (myform.gender[0].checked) gender = 1;
  else if (myform.gender[1].checked) gender = 2;
  var country = myform.country.value;
  var province = myform.province.value;
  var city = myform.city.value;
  var contactphone = myform.contactphone.value;
  var education = myform.education.value;
  
  setCookie("username", username);
  setCookie("password", password);
  setCookie("emailaddress", emailaddress);
  if (realname != "") setCookie("realname", realname);
  if (gender != 0) setCookie("gender", gender);
  if (age != 0) setCookie("age", age);
  if (country != "") setCookie("country", country);
  if (province != "") setCookie("province", province);
  if (city != "") setCookie("city", city);
  if (contactphone != "") setCookie("contactphone", contactphone);
  if (education != 0) setCookie("education", education);
  
  // check the size of signature
  var sign = myform.sign;
  
  if (sign.value != "") {
    if (sign.value.length > 100) {
      alert("您的签名超长。长度为" + sign.value.length + "。");
      myform.sign.focus();
      return false;
    }	
    setCookie("sign", sign.value);
  }
     
  return true;
}

