
var main = function() {

	var _username = new Input(
		'username', 
		'username-warning', 
		[
			{
				msg: __J.error_username_too_short,
				validate: function(e)
				{
					if (e && e.dom)
					{
						return (e.dom.value.length>=6);
					}
					return false;
				}
			},
			{
				msg: __J.error_username_bad_format,
				validate: function(e)
				{
					if (e && e.dom)
					{
						var re = /^[a-zA-Z]+[\w]+$/;
						
						return re.test(e.dom.value);
					}
					return false;
				}
			}
		]);

	var _email = new Input(
		'email',
		'email-warning',
		[
			{
				msg: __J.error_email_bad_format,
				validate: function(e)
				{
					if (e && e.dom)
					{
						var re = /^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,4}$/;
						
						return re.test(e.dom.value);
					}
					return false;
				}
			}
		]);

	var _password1 = new Input(
		'password1', 
		'password1-warning', 
		[
			{
				msg: __J.error_password_too_short,
				validate: function(e)
				{
					if (e && e.dom)
					{
						return (e.dom.value.length>=6);
					}
					return false;
				}
			},
			{
				msg: __J.error_password_bad_format,
				validate: function(e)
				{
					if (e && e.dom)
					{
						var str = e.dom.value;
						var i;

						for (i=0; i<str.length; ++i)
						{
							if (str.charCodeAt(i)>127)
							{
								return false;
							}
						}
						return true;
					}
					return false;
				}						
			}
		]);

	var _password2 = new Input(
		'password2', 
		'password2-warning', 
		[
			{
				msg: __J.error_password_confirm_failed,
				validate: function(e)
				{
					var e1 = E(this.password1);

					if (e1 && e.dom)
					{
						return e1.value == e.dom.value;
					}
					return false;
				},
				password1: 'password1'
			}
		]);
		
	return {
		init: function()
		{
			var v;
			var year, month, day;
			var i;

			_username.init();
			_password1.init();
			_password2.init();
			_email.init();			

			year = E('birthday-year');
			month = E('birthday-month');
			day = E('birthday-day');

			if (year && month && day)
			{
				year.options.length = 0;
				
				for (i=1900; i<=2008; ++i)
				{
					year.options[i-1900] = new Option(i);
				}
				year.selectedIndex = 73;

				day.options.length = 0;
				
				for (i=0; i<31; ++i)
				{
					day.options[i] = new Option(i+1);
				}
				day.selectedIndex = 0;

				Ext.get(year).on('change', this.load_day_list);
				Ext.get(month).on('change', this.load_day_list);
			}
			Ext.get('submit').on('click', this.on_register, this);
		},

		load_day_list: function()
		{
			var e_year = E('birthday-year');
			var e_month = E('birthday-month');
			var e_day = E('birthday-day');

			if (!e_year || !e_month || !e_day)
			{
				return ;
			}

			var year = e_year.options[e_year.selectedIndex].value;
			var month = e_month.options[e_month.selectedIndex].value;
			var day = e_day.options[e_day.selectedIndex].value;

			var days_of_mon = (30 + (((month & 9) == 8) || ((month & 9) == 1)) - (month == 2) - (!(((year % 4) == 0) && (((year % 100) != 0) || ((year % 400) == 0))) && (month == 2)));
			var i;

			e_day.options.length = 0;

			for (i=0; i<days_of_mon; ++i)
			{
				e_day.options[i] = new Option(i+1);
			}
			e_day.selectedIndex = 0;
		},

		on_register: function(event)
		{
			if (!_username.check() || !_email.check() || !_password1.check() || !_password2.check())
			{
				return ;
			}

			event.getTarget().disabled = true;			

			var birth_year_dom = Ext.fly('birthday-year').dom;
			var birth_month_dom = Ext.fly('birthday-month').dom;
			var birth_day_dom = Ext.fly('birthday-day').dom;

			var v = E('birthday-year').value;

			Ext.Ajax.request({
				url: '/register',
				form: 'register',
				scope: this,
				success: this.on_register_success,
				failure: this.on_register_failure,
				params: 
				{
					ajax: '1', 
					gender: 1-Ext.fly('gender').dom.selectedIndex,
					birthday: birth_year_dom.options[birth_year_dom.selectedIndex].value+'-'+
						birth_month_dom.options[birth_month_dom.selectedIndex].value+'-'+
						birth_day_dom.options[birth_day_dom.selectedIndex].value
				}
			});						
		},

		on_register_success: function(response)
		{
			var o = Ext.decode(response.responseText);//eval("("+response.responseText+")");

			if (o && o.error)
			{
				var e = Ext.get(E('register-failed-msg'));

				e.dom.innerHTML = o.msg;
				e.setVisibilityMode(Ext.Element.DISPLAY);
				e.show();
				
				Ext.fly('submit').dom.disabled = false;
			}
			else
			{		
				redirect('/');
			}
		},

		on_register_failure: function()
		{
			Ext.fly('submit').dom.disabled = false;
		}
	};


}();



Ext.onReady(function(){
	main.init();
});

