var FormValidate = Class.create();
FormValidate.prototype = {
	initialize: function(el, options) {
		this.el = $(el);
		this.initialized = false;
		this.els = new Array();
		
		this.validateForm = this.validate.bindAsEventListener(this);
		this.el.onsubmit = function(){
			return false;
		}
		this.cunstructEls();
		Event.observe(this.el, "submit", this.validateForm );
	},
	cunstructEls: function (){
		els = [];
		this.el.descendants().each(function(el){
			if(Element.readAttribute(el, 'validate')){
				obj = {	element:el,
						validation: function(){
							returnData = true;
							Element.readAttribute(el, 'validate').split(' ').each(function(s){
								if(s.indexOf('_')>=0){
									sval = s.split('_')[0]+'_x';
								}else{
									sval = s;
								}
								if(Object.isFunction(FormValidate.prototype[sval])){
									if(s.indexOf('_')>=0){
										elval =  el.value.toJSON() + ',"' + s.split('_')[1] + '"';
									}else{
										elval =  el.value.toJSON();
									}
									if(!eval('FormValidate.prototype.'+sval+'('+elval+')')){
										returnData = false;
									}
								}								
							});
							return returnData;
							},
							txt:'Not Valid.',
							hasBox:false
						};
				if(Element.readAttribute(el, 'title')){
					obj.txt = Element.readAttribute(el, 'title');
					el.title = "";
				}			
				els.push(obj);
			}	
		});
		this.els = els;
	},
	
	validate: function (){
		this.clearForm(this.el);
		submit = true;
		this.els.each(function(o) {
		  	if(!o.validation()){
		  		o.element.addClassName('error');
		  		FormValidate.prototype.errorBox(o);
		  		submit = false;
		  	}
		});
		if(submit){
			this.submitForm();
		}
	},
	clearForm: function(el){
		$$(".errorTip").each(function(e){
			Effect.Puff(e,{queue:'end',duration:.6});
		});
	
		$$(".error").each(function(e){
			Element.removeClassName(e, 'error');
		});
	},
	submitForm: function(){
		//stripScripts
		new Ajax.Updater(this.el.id, this.el.action, {
			  parameters: this.el.serialize(true),
			  onSuccess: function(n){
			  	this.el.style.display = 'none';
				Effect.Appear(this.el);
			  }
			});
	},
	number: function(n){
		 var valn = "[0-9]";
		 var regex = new RegExp(valn);
		 return regex.test(n);
	},
	alphaN: function(str){
		 var valn = "^[a-zA-Z0-9]{3}";
		 var regex = new RegExp(valn);
		 return regex.test(str.gsub('\n',' '));
	},
	alphaN_x: function(str,x){
		 var valn = "^[a-z A-Z 0-9 \r]{"+x+"}";
		 var regex = new RegExp(valn);
		 return regex.test(str.gsub('\n',' '));
	},
	email: function(e){
		 var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		 var regex = new RegExp(emailReg);
		 return regex.test(e);
	},
	errorBox: function(o){
		if(!o.hasBox){
		
			this.errorTip = Builder.node("div", {className: "errorTip", style: "display: none;" }, [
				Builder.node("div", {className: "xboxcontent"}, o.txt) 
			]);
			
			document.body.insertBefore(this.errorTip, document.body.childNodes[0]);
			Element.clonePosition(this.errorTip, o.element,{setWidth:false,
												setHeight:false,
												offsetLeft:Element.getWidth(o.element)+3,
												offsetTop:-4 
												});
			showBox = this.errorTip;
			o.hasBox = this.errorTip;
		}
		else
		{
			showBox = o.hasBox;
		}
		
		Effect.Appear(showBox,{queue: 'end', duration:.4});
	}
	
}	