function Field(id, event){
	var _this = this;
	this.id = id;
	this._funcs = [];
	this._field = G(id);
	this.name = this._field.name;
	this.result = {};
	this._events = event;
	if (event && event != "") {
		if (typeof(event) == "string") this._events = [event];
		for (var n = 0; n < this._events.length; n++) {
			var e = this._events[n];
			if (e && typeof(e) == "string" && e.substring(0, 2) == "on") {
				if (this._field.addEventListener)
					this._field.addEventListener(e.substring(2, e.length), function(){_this.run();}, false);
				else if (this._field.attachEvent) this._field.attachEvent(e, function(){_this.run();});
			}
		}
	}
};
Field.Extends(System, "Field");
System.extend(Field.prototype, {
	add:function(func,msgFailure){
		this._funcs.push({func:func,msg:msgFailure});
	}
    ,run: function(){
        this.result.value = $F(this.id);
		for (var n = 0; n < this._funcs.length; n++) {
			var item = this._funcs[n];
			if (!item.func(this.result.value)) {
				this.result.message = this._funcs[n].msg;
				this.dispatchEvent(new System.Event("onfailure"));
				return false;
			}
		}
		this.dispatchEvent(new System.Event("onsuccess"));
		return true;
    }
    ,check: function(){
    	this.dispatchEvent(new System.Event("beforesubmit"));
    }
});

function Validator(form){
    this._form = o(form);
	this._active = true;
    this._fields = {};
    this.query = {};
	var _this=this;
	function parseQuery(fields){
		var q=[];
		for(var n=0;n<fields.length;n++) q.push(fields.name+"="+encodeURI(fields.result.value));
		return "?"+q.join("&");
	}
	this._form.onsubmit = function(){
		return _this.valid();
	};
};
Validator.Extends(System, "Validator");
System.extend(Validator.prototype, {
    add: function(field){
        this._fields[field.id]=field;
    }
	,valid:function(){
	
		
		var _this = this;
		this._active = true;
		for(var f in this._fields)
			if(!this._fields[f].run()) this._active = false;
		if(this._active) 
			for(var f in this._fields)
			this._fields[f].check();
		this.dispatchEvent(new System.Event("beforesubmit"));
		return this._active;
	}
});
