var RegistrationWeasel = new Class({
	
	dblclickcatch : false,
				
	initialize : function() {

	},
	
	submit: function() {
		
		if(this.validate()) {
			
			if(!oRegistrationWeasel.dblclickcatch) {
				oRegistrationWeasel.dblclickcatch = true;
				
				var post = new Hash();
				post.set('first-name', $('first-name').get('value'));
				post.set('last-name', $('last-name').get('value'));
				post.set('reg-username', $('reg-username').get('value'));
				post.set('email', $('email').get('value'));
				post.set('reg-password', $('reg-password').get('value'));
				post.set('company-name', $('company-name').get('value'));
				post.set('invitation-code', $('invitation-code').get('value'));
				post.set('accounttypeid', $('selected-box').getElement('span.account-label').getProperty('accounttypeid').toInt());
				
				if(post.get('accounttypeid') > 2) {
					post.set('initquota', $('selected-box').getElement('span.pre-comma-amount').get('text').toInt());
				} else {
					post.set('initquota', null);
				}
				
				post.set('timezoneminuteoffset', oTimezoneDetector.minuteoffset);
				post.set('timezonehouroffset', oTimezoneDetector.houroffset);
				post.set('timezoneolson', oTimezoneDetector.olson);
				
				
				
				$('submit-button').set('text', oLocalization.ajaxloading);
	
				new Request.JSON({
					method: 'post',
					data: JSON.encode(post),
					url: '/ajax/RegistrationWeasel.submit.ajax.php',
					onFailure: function(exc) {
						oRegistrationWeasel.error([oLocalization.js_default_ajax_error]);
					},
					onError: function() {
						oRegistrationWeasel.error([oLocalization.js_default_ajax_error]);
					},
					onSuccess: function(o) {
						
						switch(o.success) {
							case(-1):
								oRegistrationWeasel.error([oLocalization.js_default_ajax_error]);
								break;
							case(0):
								if($chk(o.errorids)) {
									for(var i=0; i < o.messages.length; i++) {
										if($chk(o.errorids[i])) {
											if(!$(o.errorids[i]).hasClass('error')) {
												$(o.errorids[i]).addClass('error');
												$(o.errorids[i]).getParent('td').getElement('span.label').addClass('error');
											}
										}
									}
								}
								oRegistrationWeasel.error(o.messages);
								break;
							case(1):
								oRegistrationWeasel.success(o.messages);
								break;
						}
						
						
					}
				}).send();
				
			}
			
		}
			
	},
	
	validate: function() {
		
		if($chk(this.Feedback)) {
			this.Feedback.hide();
			$(this.Feedback.feedbackboxcontainerid).set('style','padding-top:0px;');
		}
		
		var valid = true;
		var messages = [];
		var c = 0;
		
		$('payment-table').getElements('input').each(function(el) {		
			el.removeClass('error');
			el.getParent('td').getElement('span.label').removeClass('error');
		});
		
		$('payment-table').getElements('input.non-empty').each(function(el) {
			if(el.get('value').trim() == '') {
				if(!el.hasClass('error')) {
					el.addClass('error');
					el.getParent('td').getElement('span.label').addClass('error');
				}
				messages[c] = el.get('errmessage');
				c = c+1;
				valid = false;
			}
		});

		if(!valid) {
			this.dblclickcatch = false;	
			this.error(messages);
		}
		
		return valid;
		
	},
	
	success: function(messages) {
		
		new Fx.Tween($('payment-table-container'), {
			onComplete:function() {
				$('payment-table-container').setStyle('height', $('payment-table-container').getSize().y);
				$('payment-table-container').set('html', $('thank-you-container').get('html'));
				new Fx.Tween($('payment-table-container'), {
					onComplete:function() {
												
					}
				}).start('opacity',1);
				
			}
		}).start('opacity',0);
		
		/*this.Feedback = new SuccessBox(messages, '11px', true);
		this.Feedback.setCustomFeedbackboxcontainerId('registration-feedback');
		this.Feedback.show();*/
		
		if(Context.get('mobile')) {
			this.h = ($('registration-feedback').getPosition().y - 70);
			this.oScroll = new Fx.Scroll($(document.body), {onComplete : function() {
				//
			}});
			
			setTimeout("oRegistrationWeasel.oScroll.start(0, oRegistrationWeasel.h);", 500);
		}
		
	},
	
	error: function(messages) {	
		
		var fsize = '11px';
		if(Context.get('mobile')) {
			fsize = null;
		}
		
		this.Feedback = new ErrorBox(messages, fsize, true);
		this.Feedback.setCustomFeedbackboxcontainerId('registration-feedback');
		this.Feedback.show();

		this.h = ($(this.Feedback.feedbackboxcontainerid).getPosition().y - 70);
		this.oScroll = new Fx.Scroll($(document.body), {onComplete : function() {
			$(oRegistrationWeasel.Feedback.feedbackbox.get('id')).highlight('#ad0000')
		}});
		
		setTimeout("oRegistrationWeasel.oScroll.start(0, oRegistrationWeasel.h);", 500);
		
		this.dblclickcatch = false;
		$('submit-button').set('text', oLocalization.getstarted);
		
	}
	
});
