
// 注册用户验证
var register = {
	email_reg:/^[_.0-9a-z-]+@([0-9a-z-]+\.)+[a-z]{2,6}$/i,
	register_form:'form1',
	error_no : 1,
	type_str:'',
	error_note:'',
	
	get_captcha:function(){
        
        var url = 'index.php?module=account&submod=account&method=reset_captcha';
 		 $.post(url,
 		 function(rs){
            $('#auth_validate').attr('value',rs);
			var imgSrc = $('#captchaI').attr('src');
			var authflag = '&authnum=';
			var pos = imgSrc.indexOf(authflag);
			if(pos > 0) {
				base_url = imgSrc.split('&authnum=');
				$('#captchaI').attr('src',base_url[0]+'&authnum='+rs);
			} else{
				alert(account_lang['err_str1']);
			}
			$('#captchaI').src = imgSrc;
 		 });
    },
    
    check_captcha:function(){
    	var captchaT = $.trim($('#captchaT').val());
		var auth_validate = $.trim($('#auth_validate').val());
		var _me = this;
		
		if(captchaT.length == 4){  // 输入了4位才会提交，否则直接报错
			var qstr = '&auth_hid='+auth_validate+'&auth_num='+captchaT;
			var url = 'index.php?module=account&submod=account&method=check_validate'+qstr;
			$.post(url,
	 		 function(rs){
	             if(rs == 0){
	             	$('#captcha_msg').html(account_lang['err_str2']);
					_me.replaceClass('captcha_msg','fail');
					_me.replaceClass('captchaT','ipt-error');
				}else{
					$('#captcha_msg').html('');
					_me.replaceClass('captcha_msg','true');
				}
	 		 });
		} else {
			$('#captcha_msg').html(account_lang['str1']);
			this.replaceClass('captcha_msg','fail');
			this.replaceClass('captchaT','ipt-error');
		}
	},
	
	check_done: function(event,captchaEle,nextEle){
		if(!captchaEle) captchaEle = 'captchaT';
		if(!$('#'+captchaEle)) return;
		var capt_Text = $('#'+captchaEle).val();
	},
	
	check_email: function(){
		var email = $.trim($('#email').val());
		if (email ==""){
			 $('#email_msg').html('');
			 this.replaceClass('email_msg','');
			 return false;
		}else if(!email.match(this.email_reg)){
			$('#email_msg').html(account_lang['err_str4']);
			this.replaceClass('email_msg','fail');
			this.replaceClass('email','ipt-error');
			return false;
		}
		
		var _me = this;
		var qstr = '';
		var url = 'index.php?module=account&submod=account&method=check_email'+qstr;
		$.post(url,{'email':email},
 		 function(rs){
             if(rs){
             	$('#email_msg').html(rs);
             	_me.replaceClass('email_msg','fail');
             	_me.replaceClass('email','ipt-error');
			}else{
				$('#email_msg').html('');
				_me.replaceClass('email_msg','true');
			}
 		 });
	},
	
	check_corp_code: function(){
		var corp_code = $.trim($('#corp_code').val());
		if (corp_code ==""){
			 $('#corp_code_msg').html('');
			 this.replaceClass('corp_code_msg','');
			 return false;
		}else if(corp_code.length >20){
			$('#corp_code_msg').html(account_lang['err_str15']);
		    this.replaceClass('corp_code_msg','fail');
		    this.replaceClass('corp_code','ipt-error');
			return false;
		}
		
		var _me = this;
		
		var qstr = '';
		var url = 'index.php?module=account&submod=account&method=check_corp_code'+qstr;
		$.post(url,{'corp_code':corp_code},
 		 function(rs){
             if(rs){
             	$('#corp_code_msg').html(rs);
                _me.replaceClass('corp_code_msg','fail');
                 _me.replaceClass('corp_code','ipt-error');
			}else{
				$('#corp_code_msg').html('');
				_me.replaceClass('corp_code_msg','true');
			}
 		 });
	},
	
	check_reg_info: function(type){
		if($('#'+type+"_msg").className !='true'){
			switch(type){
				case "firstname":
				
				case "lastname":
					var name = $.trim($('#'+type).val());
					if (type == 'firstname'){
						note_msg = account_lang['str6'];
						fail_msg = account_lang['err_str16'];
						success_msg = account_lang['suc_str2'];
					} else if(type == 'lastname'){
						note_msg = account_lang['str7'];
						fail_msg = account_lang['err_str17'];
						success_msg = account_lang['suc_str3'];
					}
					if (name ==''){
						 $('#'+type+"_msg").html('');
			 			 this.replaceClass(type+'_msg','');
					} else if(name.length > 30){
						 $('#'+type+"_msg").html(fail_msg);
			 			 this.replaceClass(type+'_msg','fail');
			 			 this.replaceClass(type,'ipt-error');
					} else {
						$('#'+type+"_msg").html('');
			 			 this.replaceClass(type+'_msg','true');
					}
					break;
				case "passwd":
					var passwd = $('#passwd').val();
					
					 if (passwd){
					 	if (passwd.length > 30 || passwd.length < 6 ){
					 		$('#'+type+"_msg").html(account_lang['err_str8']);
					 		this.replaceClass(type+'_msg','fail');
					 		this.replaceClass(type,'ipt-error');
					 	} else {
					 		$('#'+type+"_msg").html('');
					 		this.replaceClass(type+'_msg','true');
					 	}
					 } else {
					 	$('#'+type+"_msg").html('');
				 		this.replaceClass(type+'_msg','');
					 }
					break;
				case "passwd_confirm":
					 var passwd = $('#passwd').val();
					 var confirm_passwd = $('#passwd_confirm').val();
					
					 if (passwd){
					 	if (passwd !== confirm_passwd){
					 		$('#'+type+"_msg").html(account_lang['err_str9']);
					 		this.replaceClass(type+'_msg','fail');
					 		this.replaceClass(type,'ipt-error');
					 	} else {
					 		if (passwd.length > 5 && passwd.length < 33){
					 			$('#'+type+"_msg").html('');
					 			this.replaceClass(type+'_msg','true');
					 		} else {
					 			$('#'+type+"_msg").html(' ');
					 			this.replaceClass(type+'_msg','');
					 		}
					 		
					 	}
					 	
					 } 
					break;
				case "corp_name":
					var corp_name = $.trim($('#'+type).val());
					if (corp_name ==''){
						 $('#'+type+"_msg").html('');
			 			 this.replaceClass(type+'_msg','');
					} else {
						 $('#'+type+"_msg").html('');
			 			 this.replaceClass(type+'_msg','true');
					}
					break;
				
			}
		}
	},
	
	replaceClass: function(id,value){
		$('#'+id).removeClass();
		$('#'+id).addClass(value);
	},
	
	show_msg_info: function(type){
		var msg_obj = $('#'+type+"_msg");
		var msg_className = msg_obj.attr('class');
		switch(type){
	    	case "email":
	    		if (msg_className !='fail'){
					msg_obj.html(account_lang['str12']);
		    		this.replaceClass(type+'_msg','focus');
	    		}
	    	break;
	    	case "firstname":
	    			msg_obj.html(account_lang['str13']);
	    			this.replaceClass(type+'_msg','focus');
	    	break;
	    	case "lastname":
	    		msg_obj.html(account_lang['str14']);
	    		this.replaceClass(type+'_msg','focus');
	    	break;
	    	case "passwd":
	    		if (msg_className !='fail'){
					msg_obj.html(account_lang['str15']);
		    		this.replaceClass(type+'_msg','focus');
	    		}  
    			var conf_passwd = $('#passwd_confirm').val();
    			if (conf_passwd == ''){
    				 $('#passwd_confirm').html('');
    				this.replaceClass('passwd_confirm','ipt-normal');
    				$('#passwd_confirm_msg').html('');
    			} 
	    	break;
	    	case "passwd_confirm":
				msg_obj.html(account_lang['str9']);
	    		this.replaceClass(type+'_msg','focus');
	    	break;
	    	case "corp_code":
	    		if (msg_className !='fail'){
					msg_obj.html(account_lang['str16']);
		    		this.replaceClass(type+'_msg','focus');
	    		}
	    	break;
	    	case "corp_name":
				msg_obj.html(account_lang['str17']);
	    		this.replaceClass(type+'_msg','focus');
	    	break;
		}
		
	},
	
	clearPwd: function(){
		 $('#passwd_confirm').val('');
		 $('#passwd_confirm_msg').html('');
		 this.replaceClass('passwd_confirm','ipt-normal');
		 $('#passwd_msg').html(account_lang['str15']);
		 this.replaceClass('passwd_msg','focus');
	},
	
	register_user:function(){
		 this.error_note = '';

		if($('#email')){
			var email_value = $.trim($('#email').val());
			if (email_value ==""){
				//error_no = 2;
				 var msg = account_lang['err_str3'];
				 this.append_error_note(msg);
			}else if(!email_value.match(this.email_reg)){
				//this.error_no = 2;
				var msg = account_lang['err_str4'];
				this.append_error_note(msg);
			}
		}
		
		var first_name = $.trim($('#firstname').val());
		var last_name = $.trim($('#lastname').val());
		var passwd = $.trim($('#passwd').val());
		var passwd_confirm = $.trim($('#passwd_confirm').val());
		var captchaT = $.trim($('#captchaT').val());
		var corp_code = $.trim($('#corp_code').val());
		var corp_name = $.trim($('#corp_name').val());
		var auth_validate = $.trim($('#auth_validate').val());
		var auth_id = $.trim($('#auth_id').val());
		
		//判断姓名
		var username = first_name+" "+last_name;
		if(first_name =="" || last_name ==""){
			
		}else if(first_name.length> 30 || last_name.length > 30){
			var msg = account_lang['err_str6'];
			this.append_error_note(msg);
		}

		//判断密码
		if(passwd=="" || passwd_confirm==""){
			var msg = account_lang['err_str7'];
			this.append_error_note(msg);
		}else if(passwd.length > 32 || passwd.length<6){
			var msg = account_lang['err_str8'];
			this.append_error_note(msg);
		}else if(passwd!=passwd_confirm){
			var msg = account_lang['err_str9'];
			this.append_error_note(msg);
		}
		
		//验证码
		if (captchaT =='' || captchaT.length !=4){
			var msg = account_lang['err_str2'];
			this.append_error_note(msg);
		}
		
		if (!auth_id){
			// 组织信息
			if (corp_code ==''){
				var msg = account_lang['err_str10'];
				this.append_error_note(msg);
			}else if(corp_code.length >20){
				var msg = account_lang['err_str11'];
				this.append_error_note(msg);
			}
			
			if (corp_name ==''){
				var msg = account_lang['err_str12'];
				this.append_error_note(msg);			
			}
		}
		
		 // 获取客户端时区设置
		var zone = get_time().zone;
		$('#zone_val').attr('value',zone);

		this.show_error_note();
		if (this.error_note){
			return false;
		}
	},
	append_error_note:function(msg){
		this.error_note += '<br />· '+msg;
	},
	show_error_note:function(){
		if (this.error_note){
			error_msg = account_lang['err_str13']+this.error_note;
			$('#register_error_note').css('display','block').html(error_msg);
		} else {
			$('#register_error_note').css('display','none').html('');
		}
	}
}