/**
 * Class Validator
 * @author fangel
 * @copyright 2007, fangel
 */

Validator = function( formElem, rowClassString, hintClassString ) {
	this.form = $(formElem);
	this.rowClass = rowClassString;
	this.hintClass = hintClassString;
	return this;
}

Validator.prototype.form = null;
Validator.prototype.rowClass = '';
Validator.prototype.hintClass = '';
Validator.prototype.eventsToMonitor = ['keyup', 'focus', 'blur'];
Validator.prototype.validators = new Hash();
Validator.prototype.images = {valid: 'valid.gif', invalid: 'invalid.gif', width: null, height: null};
Validator.prototype.elements = new Array();

Validator.prototype.setImages = function( validSrc, invalidSrc, widthInt, heightInt ) {
	this.images = {valid: validSrc, invalid: invalidSrc, width: widthInt, height: heightInt};
	return this;
}

Validator.prototype.setEventsToMonitor = function( eventArray ) {
	this.eventsToMonitor = eventArray;
	return this;
}

Validator.prototype.addValidator = function( nameString, validatorObj ) {
	this.validators.set( nameString, validatorObj );
	return this;
}

Validator.prototype.apply = function() {
	var inputArray = $ES('.' + this.rowClass + ' input', this.form);
	for( var i=0; i < inputArray.length; i++ ) {
		if( inputArray[i].getAttribute('type') != 'submit' && inputArray[i].getAttribute('title') != null ) {
			for( var j = 0; j < this.eventsToMonitor.length; j++ ) {
				inputArray[i].addEvent(this.eventsToMonitor[j], this.monitorChange.bindAsEventListener(this, inputArray[i].getAttribute('title')));
			}
			this.elements.push( inputArray[i] );
		}
	}
	
	if( this.elements.length > 0 ) {
		this.form.addEvent( 'submit', this.checkEntireForm.bindAsEventListener(this) );
	}
}

Validator.prototype.monitorChange = function( event, validator ) {
	var event = new Event(event);
	this.runCheck( event.target, validator );
}

Validator.prototype.runCheck = function( elemNode, validator ) {
	var valid = this.validators.get( validator ).valid( elemNode.value );
	if( ! valid ) {
		var imgSrc = this.images.invalid;
		var textText = this.validators.get( validator ).hint();
	} else {
		var imgSrc = this.images.valid;
		var textText = '';
	}
	
	var htmlString = '<img src="' + imgSrc + ((this.images.width != null && this.images.height != null) ? '" width="' + this.images.width + '" height="' + this.images.height + '"' : '') + ' /> ' + textText;
	$E('.' + this.hintClass, elemNode.getParent()).setHTML( htmlString );
	return valid;
}

Validator.prototype.checkEntireForm = function( event ) {
	var event = new Event(event);
	var valid = true;
	for( var i = 0; i < this.elements.length; i++ ) {
		if( ! this.runCheck( this.elements[i], this.elements[i].getAttribute('title') ) ){
			valid = false;
		}
	}
	
	if( ! valid ) { event.preventDefault(); }
	return valid;
}

Validator.prototype.addValidator('min2chars', { 
	valid: function( value ) { return (value.length > 2) }, 
	hint: function() { return '' }
});

Validator.prototype.addValidator('notempty', {
	valid: function( value ) { return (value.length > 0); },
	hint: function() { return ''; }
});

Validator.prototype.addValidator('email', {
	valid: function( value ) { var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; return filter.test( value ); },
	hint: function() { return ''; }
});