/**
 * @author ysw
 */
var LoginManager = function(){
	return{
		init: function(){
			this.bindEl();
			
		},
		
		bindEl: function(){
			this.usernameEl = Ext.get("username");
			this.passwordEl = Ext.get("password");
			this.randomEl = Ext.get("random");
			this.submitForm = Ext.get("submitForm");
			this.autoLoginEl = Ext.get("autoLogin");
			this.rememberMeEl = Ext.get("rememberMe");
			this.loginBtnEl = Ext.get("loginBtn");
			this.autoLoginEl.addListener("click", this.autoLoginCheck, this);
			//Ext.get("rememberMe").addListener("click", this.rememberCheck, this);
			Ext.get("replaceRandomBtn").addListener('click', this.getRandom, this);
			
			this.addLoginListener();
			
			if(Cookies.get(LoginConst.rememberMeCookie) != null){
				this.usernameEl.dom.value = Cookies.get(LoginConst.rememberMeCookie);
			}
			
			if(Cookies.get(LoginConst.autoLoginCookie) == LoginConst.autoLogin){
				this.autoLoginEl.dom.checked = true;
			}
			
			if(Cookies.get(LoginConst.rememberMeCookie) != "" && Cookies.get(LoginConst.rememberMeCookie) != null){
				this.rememberMeEl.dom.checked = true;
			}
		},

		addLoginListener: function(){
			this.loginBtnEl.addListener('click', this.loginBtnClick, this);
			if(Ext.isIE || Util.isFirefox()){
				Util.addKeyMap(Ext.getBody(), this.loginBtnClick, this);
			}else{
				Util.addKeyMap(Ext.getBody(), function(){}, this);
			}
		},

		removeLoginListener: function(){
			this.loginBtnEl.removeListener('click', this.loginBtnClick, this);
			this.keyMap = null;
		},
		
		getRandom: function(){
			Ext.Ajax.request({
			   url: Login_UrlHost.getrandompic,
			   success: this.getRandomSucc,
			   failure: this.getRandomFail
			});

		},
		
		getRandomSucc: function(response){
			Ext.getDom("randomWrap").innerHTML = response.responseText;
		},
		
		getRandomFail: function(){
			Util.alert(ErrorConst.getRandomErr);
		},
		
		loginBtnClick: function(){

			if(this.usernameEl.dom.value == ""){
				Util.alert(AlertConst.inputUserName);
				this.usernameEl.focus();
				return;
			}
			if(this.passwordEl.dom.value == ""){
				Util.alert(AlertConst.inputPassword);
				this.passwordEl.focus();
				return;
			}
			if(this.randomEl.dom.value == ""){
				Util.alert(AlertConst.inputRandom);
				this.randomEl.focus();
				return;
			}
			
			this.rememberCheck();
			this.removeLoginListener();
			this.submitForm.dom.action = Login_UrlHost.loginSubmit;
			//alert(this.submitForm.dom.action);
			Ext.Ajax.request({
	            form: this.submitForm,
	            success: this.loginSuccess,
	            failure: this.loginFailure,
	            scope: this
	        });
		},
		
		loginSuccess: function(response){
			
			//alert("返回信息："+response.responseText);
			eval("var res="+response.responseText);
			if(res.result){
				if(res.location != undefined){
					location.href = res.location;
				}else{
					location.href = "/default.php";
				}
			}else{
				Util.alert(res.errmsg);
				this.passwordEl.dom.value = "";
				this.getRandom();
				this.randomEl.dom.value = "";
				this.addLoginListener();
			}
		},
		
		loginFailure: function(response){
			Util.alert(ErrorConst.loginErr);
			this.addLoginListener();
		},
		
		rememberCheck: function(e){
			if(this.usernameEl.dom.value != ""){
				if(this.rememberMeEl.dom.checked){
					Cookies.set(LoginConst.rememberMeCookie, this.usernameEl.dom.value);
				}else{
					Cookies.clear(LoginConst.rememberMeCookie);
				}
			}
		},
		
		autoLoginCheck: function(e){
			if(e.target.checked){
				Cookies.set(LoginConst.autoLoginCookie, LoginConst.autoLogin);
			}else{
				Cookies.set(LoginConst.autoLoginCookie, LoginConst.notAutoLogin);
			}

		}
	}
}();

RegisterManager = function(){
	return{
		init: function(){
			this.bindEl();
		},
		
		bindEl: function(){
			this.usernameEl = Ext.get("username");
			this.nicknameEl = Ext.get("nickname");
			this.nicknametipEl = Ext.get("nicknametip");
			this.passwordEl = Ext.get("password");
			this.repeatpassEl = Ext.get("repeatpass");
			this.questionSelEl = Ext.get("questionSel");
			this.questionInputEl = Ext.get("questionInput");
			this.inputQuesWrapEl = Ext.get("inputQuesWrap");
			this.answerEl = Ext.get("answer");
			this.randomEl = Ext.get("random");
			this.submitForm = Ext.get("submitForm");
			Ext.get("replaceRandomBtn").addListener('click', this.getRandom, this);
			Ext.get("registerBtn").addListener('click', this.regBtnClick, this);
			this.questionSelEl.addListener('change', this.questionSel, this);
			//this.autoLoginEl = Ext.get("autoLogin");
			//this.autoLoginEl.addListener("click", LoginManager.autoLoginCheck, this);
			//this.nicknameEl.addListener("blur", this.checkNickname, this);
			
			Util.addKeyMap(Ext.getBody(), this.regBtnClick, this);
			
			//if(Cookies.get(LoginConst.autoLoginCookie) == LoginConst.autoLogin){
			//	this.autoLoginEl.dom.checked = true;
			//}
		},
		
		getRandom: function(){
			LoginManager.getRandom();
		},
		
		questionSel: function(e){
			if(this.questionSelEl.dom.value == "1"){
				this.inputQuesWrapEl.setDisplayed(true);
			}else{
				this.inputQuesWrapEl.setDisplayed(false);
			}
		},
		
		checkNickname: function(){
			var url = Login_UrlHost.checkNickname + this.nicknameEl.dom.value;
			Ext.Ajax.request({
			   url: url,
			   success: this.checkNameSucc,
			   failure: this.checkNameFailure,
			   scope: this
			});
		},
		
		checkNameSucc: function(response){
			eval("var res="+response.responseText);
			if(res.result){
				this.nicknametipEl.dom.innerHTML = "";
			}else{
				if(res.state == "01014"){
					this.nicknametipEl.dom.innerHTML = AlertConst.nickNameValid;
				}else{
					Util.alert(res.errmsg);
				}
			}
		},
		
		checkNameFailure: function(){
			Util.alert(ErrorConst.checkNicknameErr);
		},
		
		regBtnClick: function(){
			if(!document.getElementById("serviceDesBtn").checked){
				Util.alert(AlertConst.acceptService);
				return;
			}

			if(this.usernameEl.dom.value == ""){
				Util.alert(AlertConst.inputUserName);
				this.usernameEl.focus();
				return;
			}
			if(!this.validEmail(this.usernameEl.dom.value)){
				Util.alert(AlertConst.EmailValid);
				this.usernameEl.focus();
				return;
			}
			if(this.usernameEl.dom.value.length > 40){
				Util.alert(AlertConst.userNameValid);
				this.usernameEl.focus();
				return;
			}
			if(this.nicknameEl.dom.value.trim() == ""){
				Util.alert(AlertConst.inputNickname);
				this.nicknameEl.focus();
				return;
			}
			if(this.passwordEl.dom.value == ""){
				Util.alert(AlertConst.inputPassword);
				this.passwordEl.focus();
				return;
			}
			if(this.passwordEl.dom.value.length < 6){
				Util.alert(AlertConst.passwordLen);
				this.passwordEl.focus();
				return;
			}
			if(this.repeatpassEl.dom.value == ""){
				Util.alert(AlertConst.inputRepeatpass);
				this.repeatpassEl.focus();
				return;
			}
			if(this.repeatpassEl.dom.value != this.passwordEl.dom.value){
				Util.alert(AlertConst.passNotSame);
				this.repeatpassEl.focus();
				return;
			}
			if(this.questionSelEl.dom.value != "1"){
				this.questionInputEl.dom.value = this.questionSelEl.dom.value;
			}
			if(this.questionInputEl.dom.value == ""){
				Util.alert(AlertConst.inputQuestion);
				this.questionInputEl.focus();
				return;
			}
			if(this.answerEl.dom.value == ""){
				Util.alert(AlertConst.inputAnswer);
				this.answerEl.focus();
				return;
			}
			if(this.randomEl.dom.value == ""){
				Util.alert(AlertConst.inputRandom);
				this.randomEl.focus();
				return;
			}
			
			this.submitForm.dom.action = Login_UrlHost.registerSubmit;
			//alert(this.submitForm.dom.action);
			Ext.Ajax.request({
	            form: this.submitForm,
	            success: this.regSuccess,
	            failure: this.regFailure,
	            scope: this
	        });
		},
		
		regSuccess: function(response){
			//alert("返回信息："+response.responseText);
			eval("var res="+response.responseText);
			if(res.result){
				if(res.location != undefined){
					location.href = res.location;
				}else{
					location.href = "/default.php";
				}
			}else{
				Util.alert(res.errmsg);
				this.passwordEl.dom.value = "";
				this.repeatpassEl.dom.value = "";
				this.getRandom();
				this.randomEl.dom.value = "";
			}
		},
		
		regFailure: function(response){
			Util.alert(ErrorConst.registerErr);
		},
		
		validEmail: function(email){
			var emailtest=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
			return emailtest.test(email);
		}
	}
}();

//第一步验证用户名
GetPassValidateUserManager = function(){
	return{
		init: function(){
			this.bindEl();
		},
		
		bindEl: function(){
			this.usernameEl = Ext.get("username");
			this.randomEl = Ext.get("random");
			this.submitForm = Ext.get("submitForm");
			Ext.get("replaceRandomBtn").addListener('click', this.getRandom, this);
			Ext.get("submitBtn").addListener('click', this.submitBtnClick, this);
			Util.addKeyMap(Ext.getBody(), this.submitBtnClick, this);
		},
		
		getRandom: function(){
			LoginManager.getRandom();
		},
		
		submitBtnClick: function(){
			if(this.usernameEl.dom.value == ""){
				Util.alert(AlertConst.inputUserName);
				this.usernameEl.focus();
				return false;
			}
			if(this.randomEl.dom.value == ""){
				Util.alert(AlertConst.inputRandom);
				this.randomEl.focus();
				return false;
			}
			this.submitForm.dom.action = Login_UrlHost.loadGetPasswd;
			this.submitForm.dom.submit();
			
			/*var url = Login_UrlHost.getPassValidateUser + this.usernameEl.dom.value;
			//alert(url);
			Ext.Ajax.request({
				url: url,
	            success: this.getPassSuccess.createDelegate(this,[this.usernameEl.dom.value],1),
	            failure: this.getPassFailure,
	            scope: this
	        });
			
			this.submitForm.dom.action = Login_UrlHost.getPassValidateUser;
			//alert(this.submitForm.dom.action);
			Ext.Ajax.request({
	            form: this.submitForm,
	            success: this.getPassSuccess,
	            failure: this.getPassFailure,
	            scope: this
	        });*/
		}
		/*
		getPassSuccess: function(response, userName){
			//alert("返回信息："+response.responseText);
			eval("var res="+response.responseText);
			if(res.result){
				location.href = Login_UrlHost.loadGetPasswd + userName;
			}else{
				Util.alert(res.errmsg);
				this.getRandom();
				this.randomEl.dom.value = "";
			}
		},
		
		getPassFailure: function(response){
			Util.alert(ErrorConst.getPassErr);
		}*/
	}
}();

//第二步输入答案时用
GetPasswdManager = function(){
	return{
		init: function(){
			this.bindEl();
		},

		bindEl: function(){
			this.answerEl = Ext.get("answer");
			this.randomEl = Ext.get("random");
			this.submitForm = Ext.get("submitForm");
			Ext.get("submitBtn").addListener('click', this.submitBtnClick, this);
			Ext.get("replaceRandomBtn").addListener('click', this.getRandom, this);
			Util.addKeyMap(Ext.getBody(), this.submitBtnClick, this);
		},
		
		getRandom: function(){
			LoginManager.getRandom();
		},
		
		submitBtnClick: function(){
			if(this.answerEl.dom.value == ""){
				Util.alert(AlertConst.inputAnswer);
				this.answerEl.focus();
				return;
			}
			if(this.randomEl.dom.value == ""){
				Util.alert(AlertConst.inputRandom);
				this.randomEl.focus();
				return;
			}
			
			this.submitForm.dom.action = Login_UrlHost.getPasswdSubmit;
			//alert(this.submitForm.dom.action);
			Ext.Ajax.request({
	            form: this.submitForm,
	            success: this.getPassSuccess,
	            failure: this.getPassFailure,
	            scope: this
	        });
		},
		
		getPassSuccess: function(response){
			//alert("返回信息："+response.responseText);
			eval("var res="+response.responseText);
			if(res.result){
				Util.alert(AlertConst.sendEmailToUser);
			}else{
				Util.alert(res.errmsg);
				this.getRandom();
				this.randomEl.dom.value = "";
			}
		},
		
		getPassFailure: function(response){
			Util.alert(ErrorConst.getPassErr);
		}
	}
}();

//激活
LoginAcitveManager = function(){
	return{
		init: function(userid){
			//this.uid = userid;
			Ext.get("activeBtn").addListener("click", this.activeBtnClick, this);
		},

		activeBtnClick: function(){
			if(!document.getElementById("serviceDesBtn").checked){
				Util.alert(AlertConst.acceptService);
				return;
			}
			location.href = Login_UrlHost.loginAcitve;
			/*var url = Login_UrlHost.loginAcitve;
			Ext.Ajax.request({
			   url: url,
			   method: 'POST',
			   success: this.activeSucc,
			   failure: this.acitveFail,
			   scope: this
			});*/

		},

		activeSucc: function(response){
			eval("var res="+response.responseText);
			if(res.result){
				if(res.location != undefined){
					location.href = res.location;
				}else{
					location.href = "/default.php";
				}
			}else{
				Util.alert(res.errmsg);
			}
		},

		acitveFail: function(){
			Util.alert(ErrorConst.loginActiveErr);
		}
	}
}();

