﻿// JScript File
//register
function fIsNumber (sV,sR) {
	var sTmp;
	if(sV.length==0){ return (false);}
	for (var i=0; i < sV.length; i++){
		sTmp= sV.substring (i, i+1);
		if (sR.indexOf (sTmp, 0)==-1) {return (false);}
	}
	return (true);
}

function inputfocus(obj){
	obj.parentNode.parentNode.className = 'fieldsetDiv_blur';
	var o=$(obj.id+'_tips');
//	var g=$('check'+obj.id);
//	g.style.display = 'none';
	if(o)o.style.display = 'inline';
}
function inputblur(obj){
	obj.parentNode.parentNode.className = 'fieldsetDiv';
	var o=$(obj.id+'_tips');
	if(o)o.style.display = 'none';
}
function $(id) {
    return document.getElementById(id);
}
var lastUserName = lastPassword = lastEmail = lastSecCode = lastMphone = '';

function checkUserName() {
var userName = trim($('username').value);

if(userName == lastUserName) {
return;
} else {
lastUserName = userName;
}
var cu = $('checkusername');
var unLen = userName.replace(/[^\x00-\xff]/g, "**").length;

if(unLen < 5 || unLen > 20) {
warning(cu, unLen < 5 ? '用户名小于5个字符' : '用户名超过 20 个字符');
return;
}
else if(fIsNumber(userName.charAt(0), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') != 1){
warning(cu,'请输入以字母为开头的用户名');
return;
}
else if(fIsNumber(userName, "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-") != 1){
warning(cu,'用户名应该是数字、字母、下划线或连字符');
return;
}
else
ajaxresponse('checkusername',userName);
}

function checkPassword(confirm) {
var password = $('password').value;
if(!confirm && password == lastPassword) {
return;
} else {
lastPassword = password;
}
var cp = $('checkpassword');
if(password == '' || /[\'\"\\]/.test(password)) {
warning(cp, '密码空或包含非法字符');
return false;
} else {
cp.style.display = '';
cp.innerHTML = '<img src="images/check_right.gif" width="13" height="13">';
if(!confirm) {
checkPassword2(true);
}
return true;
}
}
function checkPassword2(confirm) {
var password = $('password').value;
var password2 = $('password2').value;
var cp2 = $('checkpassword2');
if(password2 != '') {
checkPassword(true);
}
if(password == '' || (confirm && password2 == '')) {
cp2.style.display = 'none';
return;
}
if(password != password2) {
warning(cp2, '两次输入的密码不一致');
} else {
cp2.style.display = '';
cp2.innerHTML = '<img src="images/check_right.gif" width="13" height="13">';
}
}
function checkSeccode() {
var seccodeVerify = $('seccode').value;
if(seccodeVerify == lastSecCode) {
return;
} else {
lastSecCode = seccodeVerify;
}
ajaxresponse('checkseccode', 'op=checkseccode&seccode=' + (document.all && document.charset == 'utf-8' ? encodeURIComponent(seccodeVerify) : seccodeVerify));
}
function ajaxresponse(objname, data) {
    var x = new AJAX('js/ajax_chk.aspx',function (s) {var obj = getObj(objname);if (trim(s) == "succeed") {obj.style.display = "";obj.innerHTML = "<img src=\"images/check_right.gif\" width=\"13\" height=\"13\">";obj.className = "warning";} else {alert(trim(s));warning(obj, s);}});
    x.doPost('q='+objname+'&v='+encodeURI(data));
}
function warning(obj, msg) {
if((ton = obj.id.substr(5, obj.id.length)) != 'password2') {
$(ton).select();
}
obj.style.display = '';
obj.innerHTML = '<img src="images/check_error.gif" width="13" height="13"> &nbsp; ' + msg;
obj.className = "warning";
}
function trim(str) {
return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1');
}
function checkPwd(pwd){
if (pwd == "") {
$("chkpswd").className = "psdiv0";
$("chkpswdcnt").innerHTML = "";
} else if (pwd.length < 3) {
$("chkpswd").className = "psdiv1";
$("chkpswdcnt").innerHTML = "太短";
} else if(!isPassword(pwd) || !/^[^%&]*$/.test(pwd)) {
$("chkpswd").className = "psdiv0";
$("chkpswdcnt").innerHTML = "";
} else {
var csint = checkStrong(pwd);
switch(csint) {
case 1:
$("chkpswdcnt").innerHTML = "很弱";
$( "chkpswd" ).className = "psdiv"+(csint + 1);
break;
case 2:
$("chkpswdcnt").innerHTML = "一般";
$( "chkpswd" ).className = "psdiv"+(csint + 1);
break;
case 3:		
$("chkpswdcnt").innerHTML = "很强";
$("chkpswd").className = "psdiv"+(csint + 1);
break;
}
}
}
function isPassword(str){
if (str.length < 3) return false;
var len;
var i;
len = 0;
for (i=0;i<str.length;i++){
if (str.charCodeAt(i)>255) return false;
}
return true;
}
function charMode(iN){ 
if (iN>=48 && iN <=57) //数字 
return 1; 
if (iN>=65 && iN <=90) //大写字母 
return 2; 
if (iN>=97 && iN <=122) //小写 
return 4; 
else 
return 8; //特殊字符 
} 
//计算出当前密码当中一共有多少种模式 
function bitTotal(num){ 
modes=0; 
for (i=0;i<4;i++){ 
if (num & 1) modes++; 
num>>>=1; 
} 
return modes; 
} 

//返回密码的强度级别 
function checkStrong(pwd){ 
modes=0; 
for (i=0;i<pwd.length;i++){ 
//测试每一个字符的类别并统计一共有多少种模式. 
modes|=charMode(pwd.charCodeAt(i)); 
} 
return bitTotal(modes);
}
function checkEmail(){
	var email=$('email').value;
	if(email == lastEmail) {
	return;
	} else {
		lastEmail = email;
	}
	var cp = $('checkemail');
	if(email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1){
	warning(cp, '电子邮箱格式不正确。');
	return;
	}
	ajaxresponse('checkemail',email);
}
function checkMphone(){
	var mphone=$('mphone').value;
	if(mphone == lastMphone) {
	return;
	} else {
		lastMphone = mphone;
	}
	var cp = $('checkmphone');
	if(mphone.length<1){cp.style.display = '';cp.innerHTML = '';return true;}
	if(mphone.search(/^1[358]\d{9}$/) == -1){
	warning(cp, '手机号码格式不正确。');
	return false;
	} else {
	cp.style.display = '';
	cp.innerHTML = '<img src="images/check_right.gif" width="13" height="13">';
	return true;
	}
}

