var LoginPage = {
	init: function(args) {
		this.pinHelpTitle = args.pinHelpTitle;
		
		Ext.get('needAccount').on({
			click: this.showSubscription
		});
		
		Ext.get('alreadyHaveAccount').on({
			click: this.showLogin
		});
		
		Ext.get('alreadyHaveAccount2').on({
			click: this.showLogin
		});
		
		Ext.get('forgotPassword').on({
			click: this.showResetPW
		});
		
		Ext.get('email').on({
			focus: this.loginInputClick,
			blur: this.loginInputBlur
		});
			
		new Ext.Button({
			text: args.submitLogin,
			renderTo: 'login',
			cls: 'btn-primary',
			type: 'submit',
			handler: function() {
				document.forms.login_form.submit();
			}
		});

		new Ext.Button({
			text: args.submitRegistration,
			renderTo: 'doRegister',
			cls: 'btn-primary',
			type: 'submit',
			handler: this.onRegistrationSubmit
		});

		new Ext.Button({
			text: args.submitResetPW,
			renderTo: 'doResetPW',
			cls: 'btn-primary',
			type: 'submit',
			handler: function() {
				document.forms.reset_password_form.submit();
			}
		});
	},
		
	onRegistrationSubmit: function() {
		var valid = true;
		var field = Ext.get('signup_email');
		
		// this mail check is _very_ simplistic - we'll do more complete testing server side
		if (field && ! /^\w+([\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(field.getValue())) {
			Ext.get('signup_email_error').setDisplayed('inline');
			valid = false;
		}
		else {
			Ext.get('signup_email_error').setDisplayed('none');
		}
		
		field = Ext.get('signup_password');
		var pw = field.getValue();
		if (pw.length < 6) {
			Ext.get('signup_password_hint').addClass('error').setDisplayed('inline');
			valid = false;
		}
		else {
			Ext.get('signup_password_hint').removeClass('error').setDisplayed('none');
		}

		field = Ext.get('signup_password2');
		if (field && field.getValue() != pw) {
			Ext.get('signup_password2_error').setDisplayed('inline');
			valid = false;
		}
		else {
			Ext.get('signup_password2_error').setDisplayed('none');
		}
		
		if (valid) {
			document.forms.signupform.submit();
		}
	},
	
	loginInputClick: function(ev) {
		if (this.value == this.defaultValue)
			this.value = '';
	},
	
	loginInputBlur: function(ev) {
		if (this.value == '')
			this.value = this.defaultValue;
	},
	
	showLogin: function() {
		Ext.get('signupbox').setDisplayed('none');
		Ext.get('resetpasswordbox').setDisplayed('none');
		Ext.get('loginbox').setDisplayed('block');
	},
	
	showResetPW: function() {
		Ext.get('signupbox').setDisplayed('none');
		Ext.get('loginbox').setDisplayed('none');
		Ext.get('resetpasswordbox').setDisplayed('block');
	},
	
	showSubscription: function() {
		Ext.get('signupbox').setDisplayed('block');
		Ext.get('resetpasswordbox').setDisplayed('none');
		Ext.get('loginbox').setDisplayed('none');
	}
};