KT.Loginer =(function(){
	var submiting 	= false;
	var logindialog = null;
	var fo = null;
	return {
		submit:function(f)
		{
			fo = f;
                        f.action = '/user/login.php';
			mfx.form.freeze(f.submit);
			this.email 		= f.email;
			this.passwd		= f.passwd;
			this.forgetbtn		= f.forgetpasswd;
			this.expired	= f.expired;
			this.emailerr	= $$('#loginarea h2')[0];
			this.passwderr	= $$('#loginarea h2')[1];
			var chkresult 	= this.chkform();
			if(submiting ===false && chkresult===true)
				this.ajax();
			if(chkresult===false)
				mfx.form.unFreeze(f.submit);
		},
                forget:function()
                 {
			fo = $$('#loginarea form')[0];
                        f = fo;
			chker = true;
			this.email 		= f.email;
			this.passwd		= f.passwd;
			this.forgetbtn		= f.forgetpasswd;
			this.emailerr	= $$('#loginarea h2')[0];
                        if(KT.Chk.isNum(this.email.value))
                        {
                                this.email.value += '@qq.com';
				this.emailerr.innerHTML='';
                        }
                        else if(KT.Chk.isEmail(this.email.value))
			{
				this.emailerr.innerHTML='';
			}
			else
			{
				this.emailerr.innerHTML = "<font color=red>取回密码要输入电子邮件地址</font>";
				this.email.focus();
				chker = false;
			}
			if(submiting ===false && chker ===true)
                        {
                            f.action = '/user/forget_passwd.php';
                            this.forgetpost();
                        }

                 },
                forgetpost: function(){
			submiting = true;
			var  o = $$('#loginarea form');
			logindialog = new KT.Dialog({"content":"正在发送邮件,请稍候...","title":"登录","width":220,"height":80,"alert":true}).create();
			o.set('send',{onSuccess:function(e)
			{
				this.logined(JSON.decode(e));
				submiting = false;
				mfx.form.unFreeze($('submiter'));
				mfx.form.unFreeze($('forgetpasswd'));
			}.bind(this)});
			o.send();
                
                },
		chkform:function()
		{
			var chker 			= false;
			var length_min_pass = 6;
                        if(KT.Chk.isNum(this.email.value))
                        {
                                this.email.value += '@qq.com';
				this.emailerr.innerHTML='';
				chker = true;
                        }
                        else if(KT.Chk.isEmail(this.email.value))
			{
				this.emailerr.innerHTML='';
				chker = true;
			}
			else
			{
				this.emailerr.innerHTML = "请输入正确的电子邮件地址";
				this.email.focus();
				return false;
			}

			if(KT.Chk.isPasswd(this.passwd.value))
			{
				this.passwderr.innerHTML = '';
				chker = true;
			}
			else
			{
				this.passwderr.innerHTML = "请按格式输入:6-16位的英文字母,数字或下划线,区分大小写";
				this.passwd.focus();
				return false;
			}
			return chker;
		},
		ajax:function()
		{
			submiting = true;
			var  o = $$('#loginarea form');
			logindialog = new KT.Dialog({"content":"正在登录中,请稍候...","title":"登录","width":220,"height":80,"alert":true}).create();
			o.set('send',{onSuccess:function(e)
			{
				this.logined(JSON.decode(e));
				submiting = false;
				mfx.form.unFreeze($('submiter'));

			}.bind(this)});
			o.send();
		},
		logined:function(req)
		{
			var vf = parseInt(req.result);
			if(vf===0)
			{
				logindialog.setContent("登录成功，正在跳转，请稍候...");
				this.success();
				return true;
			}
			else if(vf===1)
                        {
                            _site = this.email.value.split('@')[1];
                            $$('#email_site').set('html','<div style="color:red;">请到<a target="_blank" href="http://www.'+_site+'">'+_site+'</a>查阅来自宽途的邮件, 从邮件重设你的密码</div>');
                        }
			else if(vf===-2)
			{
				this.emailerr.innerHTML = "邮件地址或密码不正确，请重新填写";
				this.email.focus();
			}
			else if(vf===-3)
			{
				this.emailerr.innerHTML = "此邮件地址不存在，请重新填写";
				this.email.focus();
			}
			else if(vf===-4)
			{
				this.passwderr.innerHTML = "密码不正确，请重新填写";
				this.passwd.focus();
			}
			else if(vf=== -1)
			{
				this.emailerr.innerHTML = "登录服务暂时无法使用";
				this.email.focus();
			}
			if(logindialog!=null)
			{
				logindialog.close();
				logindialog = null;
			}
		},
		success:function()
		{
			if($chk(fo) && $chk(fo.return_url) && $chk(fo.return_url.value)){
				window.location.replace(fo.return_url.value);
			}else{
				window.location.replace('/user/myspace.php');
			}
		}
	}//return ends
})();


/*==Register the loginer with dom==
	1add click event to submit button;
	2add keyup event to other inputs;
	3show the div with form
*/

KT.Loginer.Fox = function(){
		var submit = function(){KT.Loginer.submit($$('#loginarea form')[0])};
		mfx.form.unFreeze($('submiter').addEvent('click',submit));
		[$('email'),$('passwd')].each(function(el)
		{
			el.addEvent('keydown',function(e){
				e=new Event(e);
				if(e.code==13){
					submit.call();
					return false;
				}
			})
		})
		$('loginarea').removeClass('cantseeit');
};
