jiyiri.register_namespace('jiyiri.page.user_reg');

/* jiyiri.require_once('jiyiri.helper.net.ajax'); */
/* jiyiri.require_once('jiyiri.helper.net.parameter'); */
/* jiyiri.require_once('jiyiri.helper.validatehelper.data_validate'); */
/* jiyiri.require_once('jiyiri.helper.htmlelementhelper.HTMLElementHelper'); */
/* jiyiri.require_once('jiyiri.ui.dateinputer'); */
/* jiyiri.require_once('jiyiri.ui.Beautifier'); */
/* jiyiri.require_once('jiyiri.helper.client.SWFObject'); */

( 
function()
{
	/**
	 * User Reg Page UI Managers
	 *
	 * @author Rocky
	 * @version 1.0
	 * */
	var UserRegUIManager = Class.create();
	UserRegUIManager._instance = null;
	UserRegUIManager.GetInstance = function()
	{
		if(UserRegUIManager._instance==null)
		{
			UserRegUIManager._instance = new UserRegUIManager();
		}
		return UserRegUIManager._instance;
	};
	
	UserRegUIManager.prototype =
	{
			reg_success_callback : null,
			initialize:function()
			{
				this._form = $('user_reg_form');
				this._account = $('account');
    			this._password = $('password');
				this._confirm_password = $('confirm_password');
    			this._real_name = $('realname');
    			this._sex_male = $('sex_male');
				this._sex_female = $('sex_female');
				this._state_area_ctl = $('state_area');
				this._error_ctl = $('state_list_error');
				this._state_list_ctl = $('state_list_area');
    			this._date_inputer = new jiyiri.ui.DateInputer(
						$('dateinputer_solar_btn'),
						$('dateinputer_lunar_btn'),
						$('dateinputer_year'),
						$('dateinputer_month'),
						$('dateinputer_day'),
						$('dateinputer_issolar')
					);
			},
			initialShow:function()
			{
				this._date_inputer.show();	
				jiyiri.ui.Beautifier.radio();
				
				var swfobject = new SWFObject('/Public/v3/daniel_swf.swf','daniel_swf',1,1,9,'#FFFFFF');
				swfobject.write("daniel_swf");
			},
			regist_success_callback:function(f)
			{
				this.reg_success_callback = f;
			},
			doReg:function()
			{	
				this._cleanError();
				if( UserRegUIManager.GetInstance()._checkForm())
				{
					UserRegUIManager.GetInstance()._change_form_state( false ); /* 变换Form模式 */
					var param  = new jiyiri.helper.net.Parameter();
					param.add_param('account',this._account.value);
					param.add_param('password',this._password.value);
					param.add_param('realname',this._real_name.value);
					param.add_param('sex',(this._sex_male.checked ? '1' : (this._sex_female.checked ? '2' : '0')));
					param.add_param('birthday_y',this._date_inputer.get_year());
					param.add_param('birthday_m',this._date_inputer.get_month());
					param.add_param('birthday_d',this._date_inputer.get_day());
					param.add_param('birthday_issolar',this._date_inputer.get_issolar());
					jiyiri.helper.net.Ajax.post_tp( 'User','doReg',param,UserRegUIManager.GetInstance()._completeUserReg);
				}
			},
			_checkForm:function()
			{
				var email = UserRegUIManager.GetInstance()._account.value;
				var password = UserRegUIManager.GetInstance()._password.value;
				var comfirm_password = UserRegUIManager.GetInstance()._confirm_password.value;
				var real_name = UserRegUIManager.GetInstance()._real_name.value
		
				/* Check Account(Email) */
				if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( email ))
				{
					UserRegUIManager.GetInstance()._showError('Email不能为空');
					return false;
				}
				if(!jiyiri.helper.validatehelper.DataValidater.CheckEmail( email ))
				{
					UserRegUIManager.GetInstance()._showError('Email格式错误');
					return false;
				}
				
				/* Check Password */
				if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( password ))
				{
					UserRegUIManager.GetInstance()._showError('密码不能为空');
					return false;
				}
				if(!jiyiri.helper.validatehelper.DataValidater.CheckPasswordLength( password ))
				{
					UserRegUIManager.GetInstance()._showError('密码长度不得小于6位');
					return false;
				}
				if(!jiyiri.helper.validatehelper.DataValidater.CheckSame( password ,comfirm_password))
				{
					UserRegUIManager.GetInstance()._showError('两次密码输入不相同');
					return false;
				}
				
				/* Check RealName */
				if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( real_name ))
				{
					UserRegUIManager.GetInstance()._showError('真实姓名不能为空');
					return false;
				}
				if(!jiyiri.helper.validatehelper.DataValidater.CheckMaxLength( real_name , 16 ))
				{
					UserRegUIManager.GetInstance()._showError('真实姓名长度不能超过16');
					return false;
				}
				if(!jiyiri.helper.validatehelper.DataValidater.CheckMinLength( real_name , 2 ))
				{
					UserRegUIManager.GetInstance()._showError('真实姓名长度小于2');
					return false;
				}
				
				/* Check Date */
				if( $F('dateinputer_month') == 0 || $F('dateinputer_day') == 0)
				{
					UserRegUIManager.GetInstance()._showError('请选择生日的日期 ');
					return false;
				}
				
				
				
				return true;
			},
			_showError:function( msg )
			{
				if( UserRegUIManager.GetInstance()._error_ctl == null )
				{
					UserRegUIManager.GetInstance()._error_ctl = document.createElement( 'div' );
					UserRegUIManager.GetInstance()._error_ctl.className = "erronotice";
					UserRegUIManager.GetInstance()._error_ctl.id = "state_list_error";
					UserRegUIManager.GetInstance()._state_area_ctl.appendChild( UserRegUIManager.GetInstance()._error_ctl )
				}
				else
				{
					UserRegUIManager.GetInstance()._error_ctl.style.display = '';
				}
				UserRegUIManager.GetInstance()._error_ctl.innerHTML = msg;
				try
				{
					parent.jiyiri.ui.Dialog.GetInstance().auto_resize();
				}
				catch(e)
				{
				}
			},
			_cleanError:function()
			{
				if( UserRegUIManager.GetInstance()._error_ctl != null )
				{
					UserRegUIManager.GetInstance()._error_ctl.style.display = 'none';
				}
			},
			_change_form_state:function(is_enable)
			{
				var form = UserRegUIManager.GetInstance()._form;
				if(is_enable)
				{
					Element.hide('loading');
					form.enable();
				}
				else {
					Element.show('loading');
					form.disable();
				}
			},
			_completeUserReg:function( response )
			{
				UserRegUIManager.GetInstance()._change_form_state( true ); /* 变换Form模式 */
				
				if(response.status!=200)
				{
					ServerError();
					return;
				}
				var rst = null;
				try {
					eval('rst='+response.responseText);
					if(!rst)
					{
						ServerError();
						return;
					}
					else {
						if(rst.status==0)
						{
							UserRegUIManager.GetInstance()._showError(rst.info);
						}
						else {
							UserRegUIManager.GetInstance()._state_list_ctl.innerHTML = "<div>注册成功,请稍候...</div>";
							if(null==UserRegUIManager.GetInstance().reg_success_callback)
							{
								if (undefined == rst.info.RedirectPage) {
									location.href=__APP__+'/UserReg/PhoneNotifyMethod';
								} else {
									location.href = rst.info.RedirectPage;
								}
							}
							else
							{
								UserRegUIManager.GetInstance().reg_success_callback();
							}
						}
					}
				}
				catch (ex) {
					ServerError();
				}
			}
	}

	 /* 命名空间注册 */
	jiyiri.page.user_reg.UserRegUIManager = UserRegUIManager;
} 
)();
