jiyiri.register_namespace('jiyiri.page.user');

/* jiyiri.require_once('jiyiri.helper.net.Ajax'); */
/* jiyiri.require_once('jiyiri.helper.eventhelper.EventHelper'); */
/* jiyiri.require_once('jiyiri.helper.validatehelper.data_validate'); */
/* jiyiri.require_once('jiyiri.ui.Beautifier'); */

( function() {
	var LoginPage = Class.create();
	LoginPage._instance = null;
	LoginPage.GetInstance = function() {
		if (LoginPage._instance == null) {
			LoginPage._instance = new LoginPage();
		}
		return LoginPage._instance;
	};

	LoginPage.prototype = {
		initialize : function() {
			this._input_account = $('account');
			this._input_password = $('password');
			this._checkbox_autologin = $('autologin');
			this._btn_login = $('loginbutton');
			this._erronotice_container = $('erronotice');
			this._on_success_callback = null;
			this._login_form_ctl = $('login_form');

			this._login_form_ctl.onsubmit = jiyiri.helper.eventhelper.EventHelper
			.create_callback_function(this, '_on_login_button_click');

		},
		init_show : function(on_success_callback) {
			if (typeof (on_success_callback) != 'undefined') {
				this._on_success_callback = on_success_callback;
			}
			//jiyiri.ui.Beautifier.checkbox();
           // jiyiri.ui.Beautifier.get_beautifier().handle_element($('autologin'));
		},
		_on_login_button_click : function() 
		{
			this._hide_err_notice();
			var account = $F(this._input_account);
			var password = $F(this._input_password);
			var autologin = this._checkbox_autologin.checked ? 1 : 0;
			if (account == '') {
				this._show_err_notice('请输入账号');
				return false;
			}
			if (!jiyiri.helper.validatehelper.DataValidater.CheckEmail(account) && !jiyiri.helper.validatehelper.DataValidater.CheckPhone(account))
			{
				this._show_err_notice('账号格式错误');
				return false;
			}
			if (password == '') {
				this._show_err_notice('请输入密码');
				return false;
			}
			var param = new jiyiri.helper.net.Parameter();
			param.add_param('account', account);
			param.add_param('password', password);
			param.add_param('autologin', autologin);
			this._btn_login.disabled = true;
			jiyiri.helper.net.Ajax.post_tp('User', 'doLogin', param,
					jiyiri.helper.eventhelper.EventHelper
							.create_callback_function(this,
									'_on_complete_login'));

			return false;
		},
		_on_complete_login : function(response) {
			var rst = jiyiri.helper.net.Ajax
					.parse_tp_return(response.responseText);
			if (!rst.status) {
				this._btn_login.disabled = false;
				this._show_err_notice(rst.info);
			} else {
				if (null !== this._on_success_callback) {
					this._on_success_callback();
				} else {
					if (undefined == rst.info.RedirectPage) {
						location.href = __APP__ + '/Reminder/';
					} else {
						location.href = rst.info.RedirectPage;
					}
				}
			}
		},
		_show_err_notice : function(msg) {
			this._erronotice_container.innerHTML = msg;
			Element.show(this._erronotice_container);
			try
			{
				parent.jiyiri.ui.Dialog.GetInstance().auto_resize();
			}
			catch(e)
			{
			}
		},
		_hide_err_notice : function() {
			Element.hide(this._erronotice_container);
		}
	};

	/* 注册控件 */
	jiyiri.page.user.LoginPage = LoginPage;
})();
