// JavaScript Document

function checkReg(c, config){
	var $ = function (o){return document.getElementById(o);};
	var trim = function (str) {return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1');};

	var username_toolong  = '<font color="#FF0000">用户名超过16个字符，请输入一个较短的用户名。</font>';
	var username_tooshort = '<font color="#FF0000">用户名小于4个字符，请输入一个较长的用户名。</font>';
	var username_illegal  = '<font color="#FF0000">用户名包含敏感字符或被系统屏蔽，请重新填写。</font>';
	var passwd_illegal    = '<font color="#FF0000">密码位数不正确(4-16位字符)或包含非法字符。</font>';
	var passwd2_illegal   = '<font color="#FF0000">请重复输入上面的密码</font>';
	var email_illegal 	  = '<font color="#FF0000">Email 地址无效，请重新填写。</font>';
	
	var reg_right 		  = '输入正确';
	
	this.config = config || {username:0, pwd:1, pwd2:2,email:3};
	this.container = $(c);//form表单！id="reg",name="reg"
	var inner;
	this.r = null;
	var _this = this;
	
	var warning = function (obj, p, msg){
		var obj = typeof Object ? obj : $(obj);
		obj.className = p;
		obj.innerHTML = msg;
	};
	
	this.__construct = function(){
		var el = _this.container.getElementsByTagName('input');
		inner = _this.container.getElementsByTagName('span');
		
		_this.container.onsubmit = function(){
			//return false;};
			return _this.checkReg()};//提交
		for (var i in _this.config){
			el[_this.config[i]].onblur = function (){
				//alert(_this.config[i]);
				return _this.check(this.id)
			};//移开光标
		}
		//el[_this.config[1]].onkeypress = function (){return _this.check(this.id)};
	}();
	
	this.check = function (id){
		var config = _this.config;
		switch (id){
			case 'username':
				_this.username();
				break;
				
			case 'pwd':
				_this.pwd();
				break;
			
			case 'pwd2':
				_this.pwd2();
				break;
			case 'email':
				_this.email();
				break;
		}

	};
	

	this.getContenAjax = function (page, n){
	  var request_url = page+ "&"+new Date().getTime();
	   var myajax = new Ajax.Request(
		request_url,
		{
			 method:'get',//get 
			 evalScripts: true,
			 onComplete : function (req){_this.r = req.responseText; ajax_sever(n);} 
			 //onSuccess:function (req){_this.r = req.responseText;}
		});
	  //alert();
		
	}
	
	var name;
	
	var ajax_sever = function(n){
		switch (n){
			case 0:
				var num = n
				var _error = '<font color="#FF0000">账号已被占用，请重新填写</font>'
				break;
		}
		
		var name = trim(_this.r) ;
		if (name == 'true'){
			warning(inner[num], 'reg_right', reg_right);
			return _this.name = true;
		}else{
			warning(inner[num], 'reg_error', _error);
			return _this.name = false;
		}
	};

	this.username = function (){//校检账号有效性
		var num = _this.config.username;
		var username = trim($('username').value);
		var unlen = username.replace(/[^\x00-\xff]/g, "**").length;
		var reg = /[^A-Za-z0-9_\-]/g;
		if(unlen < 4 || unlen > 16 ){
			warning(inner[num], 'reg_error', unlen < 4 ? username_tooshort : username_toolong);
			return false;
		}else if(reg.test(username)){
			warning(inner[num], 'reg_error', username_illegal);
			return false;
		}else{
			_this.getContenAjax('handle/regCheckHandle.php?username='+encodeURIComponent(username)+'&'+new Date().getTime(), 0);

		}
	};
	
	this.pwd = function (){//校验密码有效性
		var num = _this.config.pwd;
		var pwd = $('pwd').value;
		var unlen = pwd.length;
		
		if(unlen < 4 || unlen > 16 || /[\'\"\\]/.test(pwd)){
			warning(inner[num], 'reg_error', passwd_illegal);
			return false;	
		}else{
			warning(inner[num], 'reg_right', reg_right);
			return true;		
		}
	};
	
	this.pwd2 = function (){//校验重复密码有效性
		var num = _this.config.pwd2;
		var pwd = $('pwd').value;
		var pwd2 = $('pwd2').value;
		var unlen = pwd2.length;
		if(pwd == ''){
			warning(inner[num], 'reg_error', passwd_illegal);
			return false;
		}else if(pwd != pwd2){
			warning(inner[num], 'reg_error', passwd2_illegal);
			return false;
		}else{
			warning(inner[num], 'reg_right', reg_right);
			return true;
		}
	};
	
	this.email = function(){ //校验email有效性
		var num = _this.config.email;
		//alert(inner[num]);
		var email = trim($('email').value);
	    var checkemail = !(/^[\-\.\w]+@[\.\-\w]+(\.\w+)+$/.test(email));
	    if (checkemail){
		   warning(inner[num], 'reg_error', email_illegal);
		   return false;	
	    }else{
		   warning(inner[num], 'reg_right', reg_right);
		   return true;	
	    }
	};

	
	this.checkReg = function (){ //提交
		_this.username(); _this.pwd(); _this.pwd2(); _this.email()
		
		var c = 0;
		for (var i in _this.config){
			
			if(inner[_this.config[i]].className == 'reg_right'){
				c++;
			}else{
				c--;
			}
		}
		
		if(trim($("webshopurl").value) == '' && trim($("shopaddr").value) == ''){
			alert("网店地址和实体店地址任填一项！");
			return false;
		}
		
		if(trim($("qq").value) == '' && trim($("wangwang").value) == ''){
			alert("QQ和旺旺任填一项！");
			return false;
		}
		
        if($("regPro").checked == false){
			alert("请阅读并同意注册协议！");
			return false;
		}
		
		if(c == 4){
			return true;
		}else{
		   return false;
		}
		
		
		
	};
	
}

var checkReg = new checkReg('reg');
