var Signup	= new Class({
	options : {
		showErrorsInline 	: true,
		dateFormat 			: 'dd/MM/yyyy',
		errorMsg			: {
			'color'			: '#FF0000',
			'font-size'		: '12px',
			'padding-left'	: '8px'
		}
	},
	
	initialize : function(form, options){
		this.setOptions(this.options, options);
		this.form	= $(form);
		this.submit = $('submit');
		this.elements = this.form.getElements('.required');
		
		this.list = [];
		
		this.form.addEvent('submit', function(e){
			e.stop();
			var doSubmit = this.validation();
			if(!doSubmit){
				return;
			}
			
			this.submit.value = "Please wait...";
			this.submit.setProperty('disabled', true);
			this.form.set('send', {
				onSuccess:function(txt){
					this.result(txt);
				}.bind(this),
					
				onFailure:function(){
					this.setMsg(this.submit, "Syste busy, please try later");
					$('validimage').src = '/user.go?m=validcode&random=' + Math.random();
					this.submit.value = "Submit";
					this.submit.setProperty('disabled', false);
				}.bind(this)
			});

			this.form.send();
			
		}.bind(this));
	},
	
	result : function(jsontxt){
		var results = JSON.decode(jsontxt);
		if($chk(results) && results.result == 'true'){
			this.submit.value = results.msg[0].value;
			window.location = '/user.go?m=home';
			
			return;
		}
		
		$('validimage').src = '/user.go?m=validcode&random=' + Math.random();
		this.list.each(function(el,index){
			this.clearMsg(el);
		}.bind(this));
		results.msg.each(function(item,index){
			if($chk($(item.key))){
				this.setMsg($(item.key), item.value);
			}else{
				this.setMsg(this.submit, item.value);
			}
		}.bind(this));
		
		this.submit.value = "Submit";
		this.submit.setProperty('disabled', false);
	},
	
	validation : function(){
		var doSubmit = true;
		this.list.each(function(el,index){
			this.clearMsg(el);
		}.bind(this));
		
		this.elements.each(function(el,index){
			if(!this.validate(el)){
				doSubmit = false;
			}else{
				this.clearMsg(el);
			}
		}.bind(this));
		
		if($('passwd').value != $('passwd2').value){
			this.setMsg($('passwd2'), 'password different');
			doSubmit = false;
		}
		
		return doSubmit;
	},
	
	validate : function(el){
		var valid = true;
		
		switch(el.type){
			case 'text':
			case 'password':
			case 'textarea':
			case 'select-one':
				if(el.value != ''){
					if(el.hasClass('email')){
						var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
						if(el.value.toUpperCase().match(regEmail)){
							valid = true;
						}else{
							valid = false;
							this.setMsg(el, 'Please enter a valid email address');
						}
					}
					
					if(el.hasClass('number')){
						var regNum = /[-+]?[0-9]*\.?[0-9]+/;
						if(el.value.match(regNum)){
							valid = true;
						}else{
							valid = false;
							this.setMsg(el, 'Please enter a valid number');
						}
					}
				
					if(el.hasClass('date')){
						var d = Date.parseExact(el.value, this.options.dateFormat);
						if(d != null){
							valid = true;
						}else{
							valid = false;
							this.setMsg(el, 'Please enter a valid date in the format: '+this.options.dateFormat.toLowerCase());
						}
					}
				}else{
					valid = false;
					this.setMsg(el);
				}
				break;
			case 'checkbox':
				if(!el.checked){
					valid = false;
					this.setMsg(el);
				}else{
					valid = true;
				}
				break;
		}
		return valid;
	},
	
	setMsg : function(el, msg){
		if(!$chk(el))
			return;
		if(msg == undefined){
			msg = el.title;
		}
		if(this.options.showErrorsInline){
			if(!$chk(el.error) || el.error == undefined){
				el.error = new Element('span').set('text', msg).injectAfter(el);
				el.error.setStyles(this.options.errorMsg);
			}else{
				el.error.set('text', msg);
			}
		}
		this.list.include(el);
	},
	
	clearMsg : function(el){
		this.list.erase(el);
		if($chk(el) && $chk(el.error) && el.error != undefined){
			el.error.dispose();
			el.error = undefined;		
		}
	}
	
});	
Signup.implement(new Events, new Options);
