﻿var PassUtils = {
  trim: function(text) {
    if (typeof(text) == "string") {
      return text.replace(/^\s*|\s*$/, "");
    } else {
      return text;
    }
  },
  isEmail: function(email) {
    var tomid	= /^(\d|[a-zA-Z])+(-|\.|\w)*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    return tomid.test( email );
  },
  password: function(password) {
	var tompwd	= /^[a-zA-Z0-9-_\?\.`!@#\$%\^\*\(\)\+\|\{\}\'\[\]\";:\/\?> <,]{6,32}$/;
	return tompwd.test(password);
  },
  isChinese: function(str) {
	var nLen = str.length;
	for(i = 0; i < nLen; i ++) {
	  if(str.charCodeAt(i) > 255)	return true;
	}
	return false;
  },
  autoComplete: function(id) {
	var tomid = this.trim(document.getElementById(id).value);
	var constant = "@tom.com";
	if(tomid.length > 0 && tomid.indexOf("@") == -1) {
	  document.getElementById(id).value = tomid + constant;
	}
	return;
  },
  GetCookie: function(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
      var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return this.GetCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
  },
  GetCookieVal: function(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
  },
  pwdKeypress: function(e) {
    e = e ? e : window.event;
    if (e.keyCode == 13) PassUtils.loginSub();
  },
  checkLogin: function() {
	var tomid = document.getElementById("tomid");
	var tompwd = document.getElementById("tompwd");
	if (tomid.value == "" || tomid.value == "输入邮箱") {
		tom.alert("请输入用户名", "INFO");
		tomid.focus();
		return false;
	}
	if (tompwd.value == "" || tompwd.value == "输入密码") {
		tom.alert("请输入密码", "INFO");
		tompwd.focus();
		return false;
	}
	try {
    	var params = new tom.XHConn.Parameter().append("tomid", tomid.value).append("tompwd", tompwd.value).append("rdm", new Date().getTime()).stringValue();
    	var xhconn = new tom.XHConn();
        xhconn.connectAsync("/passValidate.php", "post", params, function(xhr) {
            if (xhr.responseText == "true") {
              document.getElementById("loginForm").submit();
            } else {
              tom.alert("用户名或密码不正确", "ERROR");
            }
          });
	} catch (e) {
	  return true;
	}  
      
	return false;
  },
  init: function() {
    document.getElementById("backurl").value = encodeURIComponent(document.location.href);
	 var username = this.GetCookie("TOM_QU");
	 document.getElementById("nologin").style.display  = "none";
	 document.getElementById("login").style.display  = "none";
	 if(username != null) {
	   var lastUserName = username;
		//username	= username.substr(0, username.indexOf('@'));
		document.getElementById("username").innerHTML = username;
		document.getElementById("login").style.display  = "";
	 } else {
		document.getElementById("nologin").style.display  = "";
	 }
  }
}

PassUtils.init();