/**
 * 用户注册表单处理脚本
 *
 * @author bottle hhyisw@163.com
 *
 * @version $Id$
 */

var checkForm;
var $checkState;
/**
 * 处理 Ajax 返回的JSON数据
 * @param string $data JSON数据 array('state'=>'成功1 或 失败0', 'message'=>'提示信息', .....)
 */
var ajaxJsonData = function ($data){
	try{
		var $return = eval('(' + $data + ')');
	}catch(e){					
		$data = '{"state":"0"}';
		var $return = eval("(" + $data + ")");
	}
	$return.state = parseInt($return.state);
	return $return;
}

$(document).ready(function() {
	checkForm = function(submit){
		$checkState = true;
		$('input.inputstyle').each(function(){
			var $this      = $(this);
			var $inputname = $this.attr('name');
			var $tip       = $('div.' + $inputname + '_tip');		

			/**
			 * input 获得焦点事件邦定
			 */
			$this.focus(function(){
				$this.removeClass('inputstyle').addClass('inputstyle2');
			});
			
			/**
			 * 验证用户名
			 */
			if($inputname == 'account'){
				$this.blur(function(){
					var reg=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if(!reg.test($this.val())) { // 验证邮件格式合法性
						$tip.html(LANG['msg_email_n']);
						$checkState = false;
						return false;
					}
					$.ajax({
						url: '/ajax/UserExist.html',
						data: {username:$this.val(),formhash:$('input[@name="formhash"]').val()},
						type: 'POST',
						datatype: 'json',
						cache: false,
						success:function($data){
							var $state = ajaxJsonData($data);
							if($state.state){
								$checkState = false;
								$tip.html(LANG['msg_email_e']);
							}else{
								$this.removeClass('inputstyle2').addClass('inputstyle');
								$tip.html(LANG['msg_email_y']);
							}
							return false;
						},
						error:function(e){
							$checkState = false;
							//alert(e.message);
							return false;
						}
					});	
					//return false;
				});
			}
			
			/**
			 * 验证用户密码
			 */

			// 验证密码字符类型
			var CharMode = function(iN){
				if (iN>=48 && iN <=57) return 1; //数字
				if (iN>=65 && iN <=90) return 2; //大写字母
				if (iN>=97 && iN <=122) return 4; //小写
				return 8; //特殊字符
			}

			var bitTotal = function(num){
				modes=0;
				for (i=0;i<4;i++){
					if (num & 1) modes++;
					num>>>=1;
				}
				return modes;
			}
			
			var checkStrong = function(pwd){
				if (pwd.length <= 1)
				return 0;
				Modes=0;
				for (i=0;i<pwd.length;i++){
					//测试每一个字符的类别并统计一共有多少种模式.
					Modes |= CharMode(pwd.charCodeAt(i));
				}
				return bitTotal(Modes);
			}

			var checkPwd = function(pwdvalue){
				var $o1 = $('#reg_password_l');
				var $o2 = $('#reg_password_m');
				var $o3 = $('#reg_password_h');

				if(pwdvalue.length >= 1) {
					switch(checkStrong(pwdvalue)) {
						case 0:
						case 1:
						$o1.attr('className','r');
						$o2.attr('className','');
						$o3.attr('className','');
						break;
						case 2:
						$o1.attr('className','');
						$o2.attr('className','m');
						$o3.attr('className','');
						break;
						default:
						$o1.attr('className','');
						$o2.attr('className','');
						$o3.attr('className','h');
						break;
					}
				} else {
					$o1.attr('className','');
					$o2.attr('className','');
					$o3.attr('className','');
				}
			}

			if($inputname == 'password'){
				$this.blur(function(){
					var $pwdlength = $this.val().length;
					if($pwdlength > 20 || $pwdlength < 6 || $pwdlength == 0){
						$checkState = false;
						$tip.html(LANG['msg_pwd_n']);
					}else{
						$this.removeClass('inputstyle2').addClass('inputstyle');
						$tip.html(LANG['msg_pwd_y']);
					}
					return false;
				});
				
				$this.keyup(function(){
					checkPwd($this.val());
					//return false;
				});
			}
			// 验证确认密码
			if($inputname == 'repassword'){
				$this.blur(function(){
					if($this.val() == $('#reg_password').val() && $this.val().length > 0){
						$this.removeClass('inputstyle2').addClass('inputstyle');
						$tip.html(LANG['msg_pwd_r']);
					}else{
						$checkState = false;
						$tip.html(LANG['msg_pwd_e']);
					}
					return false;
				});
			}
			
			/**
			 * 验证昵称
			 */
			if($inputname == 'nickname'){
				$this.blur(function(){
					if($this.val().length > 16 || $this.val().length == 0){
						$checkState = false;
						$tip.html(LANG['msg_nickname_n']);
					}else{
						$tip.html(LANG['msg_nickname_y']);
					}
					return false;
				});
			}
			
			/**
			 * 验证验证码
			 */
			if($inputname == 'imagecode'){
				$this.blur(function(){
					if($this.val().length !== 5){
						$checkState = false;
						$tip.html(LANG['msg_imagecode_n']);
					}else{
						$tip.html(LANG['msg_imagecode_y']);
					}
					return false;
				});
			}
			
		});

		if(submit){
			if($('input#id_agree').attr('checked') != true) return false;
			$('input.inputstyle').each(function(){
				$(this).trigger('blur');
			});
			return $checkState;
		}
		return false;
	}

	checkForm(false);
});