Ext.namespace("tw");

tw.hosted= function() {};
tw.hosted.prototype = {
	submitted: false,
	
	checkEmail: function() {
		var target = Ext.get('email');
		var email = target.getValue();
		if (!email.trim()) {
			target.next('em').update("required");
			target.addClass('form-error');
			return;
		}
		if (!Ext.form.VTypes.email(email)) {
			target.next('em').update("invalid");
			target.addClass('form-error');
			return;
		}
		Ext.Ajax.request({
			url: '/products/checkemail/',
			success: function(response, options) {
				var res = Ext.decode(response.responseText);
				if (res.success) {
					target.next('em').update("");
					target.removeClass('form-error');
				} else {
					target.next('em').update("account already exists!");
					target.addClass('form-error');
				}
			},
			params: {
				'email': email
			},
			scope: this
		});
		
		target.next('em').update("");
		target.removeClass('form-error');
	},
	
	checkPassword: function() {
		var target = Ext.fly('password');
		var password = target.getValue();
		if (!password) {
			target.next('em').update("required");
			target.addClass('form-error');
			return;
		}
		
		target.next('em').update("");
		target.removeClass('form-error');
	},
	
	checkPassword2: function() {
		var target = Ext.get('password2');
		var password = Ext.get('password').getValue();
		var password2 = target.getValue();
		
		if (!password || password != password2) {
			Ext.fly('password').next('em').update("no match");
			Ext.fly('password').addClass('form-error');
			Ext.fly('password2').addClass('form-error');
			return;
		}
		
		Ext.fly('password').next('em').update("");
		Ext.fly('password').removeClass('form-error');
		Ext.fly('password2').removeClass('form-error');
	},
	
	checkRequired: function(e) {
		var target = Ext.fly(e.target);
		if (!target.getValue().trim()) {
			target.addClass('form-error');
		} else {
			target.removeClass('form-error');
		}
	},
	
	checkAllRequired: function() {
		var fields = Ext.select("input.required");
		fields.each(function(field){
			if (!field.getValue().trim()) {
				field.addClass('form-error');
			} else {
				field.removeClass('form-error');
			}
		}, this);
	},
	
	checkForm: function(e) {
		e.preventDefault();
		this.checkEmail();
		this.checkPassword();
		this.checkPassword2();
		this.checkAllRequired();
		var errors = Ext.select('input.form-error');
		if (errors.getCount()) {
			alert("Please fix the fields marked in red!");
		} else {
			if (this.submitted) { return; }
			this.submitted = true;
			document.forms.trialForm.action = '/products/hosted-trial/';
			document.forms.trialForm.submit();
		}
	}
};

Ext.onReady(function(){
	var hosted = new tw.hosted();
	Ext.fly('email').on('blur', hosted.checkEmail, hosted);
	Ext.fly('password').on('blur', hosted.checkPassword, hosted);
	Ext.fly('password2').on('blur', hosted.checkPassword2, hosted);
	Ext.fly('submit-button').on('click', hosted.checkForm, hosted);
	Ext.select('input.required').on('blur', hosted.checkRequired, hosted);
});
