
Validator = Class.create();
Validator.prototype = {
	
	initialize: function(parent, id, options) {
		this.parent = parent;
		this.id = id;
		this.self = $(this.id);
		switch(this.self.type){
			case 'select-one':
			case 'checkbox':
			case 'radio':
			case 'hidden':
				 this.target = this.self.parentNode;
				break;
			default:
				this.target = this.self;
		}
		
		this.options = options;
							
		//if (console) console.log(this.target);
		this.type = options.type;
		this.args = options.args;
		this.status = (this.args[2]) ? true : false;
		if (this.type == 'cmd') {
			this.updater = new Updater(this.id+'_updater', options.app, '', {parent:this});	
		}
		this.active = true;
		this.injectBehaviours();
	},
	
	validateOnServer: function(cmd) {
		if (this.parent && this.args[1]) {
			this.parent.status.show(this.args[1]);
		}
		var callParams = [];
		callParams.push('value=' + this.self.value);
		this.updater.run(callParams, cmd);
	},
	
	ajaxUpdate: function(ajaxResponse) {
		var result = ajaxResponse.getElementsByTagName('result')[0];
		var value = result.getAttribute('value');
		if (value == "error") {
			if (this.parent) {
				var msg = RicoUtil.getContentAsString(result);
				if (msg) {
					this.parent.status.show(msg, 'error');
				} else {
					this.parent.status.hide();	
				}
			}
			this.status = true;
			Element.addClassName(this.target, "error");
		} else {
			if (this.parent) {
				this.parent.status.hide();	
			}
			this.status = false;
			Element.removeClassName(this.target, "error");
		}
		
	},
	
	injectBehaviours: function() {
		if (!this.options.no_blur) this.self.onblur = this.blurHandler.bindAsEventListener(this);
	},
	
	changeState: function(error) {
		if (error) {
			Element.addClassName(this.target, "error");
			this.status = false;
		} else {
			this.status = true;
			Element.removeClassName(this.target, "error");
		}
	},
	
	blurHandler: function(e) {
		
		if (this.active) {
			var error = false;
			switch(this.type) {
				case "exists":
					if (this.self.type == "checkbox" || this.self.type == 'radio') {
						if (!this.self.checked) error = true;
					} else {
						if (!this.self.value) error = true;
					}
					break;
				case "length":
					if (this.self.value.length < this.args[0]) {
						if (this.args[1] == 'optional') {
							//if (console) console.log('optional running : "' + this.self.value + '"');
							if (this.self.value) error = true;
						} else {
							error = true;
						}
					}
					break;
				case "matches":
					var target;
					if (target = $(this.args[0])) {
						if (this.self.value != target.value && (this.self.value || target.value)) error = true;
					}
					break;
				case "numeric_only":
					var reNumbers = /^([0-9]+)$/;
					//if (console) console.log(this.self.value);
					if (!reNumbers.test(this.self.value)) error = true; 
					break;
				case "cmd":
					this.validateOnServer(this.args[0]);
					break;
				case "email":
					var reEmail = /^(?:\w+\.?)*\w+@(?:[-\w]+\.?)+\.\w+$/;
					if (!reEmail.test(this.self.value)) error = true; 
					break;
			}
		}
		
		this.changeState(error);
		
	},
	
	setActive: function(state) {
		this.active = state;
	}
	
};


