function BOSIValidatePattern(e,p,pfinal) {

	this.e = e;
	this.p = p;
	this.pfinal = pfinal;
	this.lastvalue = '';

	e.bosivalidate = this;

	e.onkeypress = function() { this.bosivalidate.onkeypress(); };
	e.onkeyup = function() { this.bosivalidate.onkeyup(); };
	e.onblur = function() { this.bosivalidate.onblur(); };

	if (typeof(_BOSIValidatePattern_prototype_called) == 'undefined')
	{
		_BOSIValidatePattern_prototype_called = true;
		BOSIValidatePattern.prototype.onkeypress = onkeypress;
		BOSIValidatePattern.prototype.onkeyup = onkeyup;
		BOSIValidatePattern.prototype.onblur = onblur;
	}
	
	function onkeypress() {
		this.lastvalue = this.e.value;
	}
	
	function onkeyup() {
		if(this.e.value && !this.p.test(this.e.value)) {
			for(i=this.lastvalue.length;i>=0;i--) {
				this.e.value = this.lastvalue.substring(0,i);
				if(this.p.test(this.e.value)) break;
			}
		}
		
		var lastfound = null;
		var count = 0;
		for(i=30;i<180;i++) {
			if( this.p.test(this.e.value+String.fromCharCode(i)) ) {
				count++;
				lastfound = String.fromCharCode(i);
			}
		}
		if(count==1) { this.e.value += lastfound; }
		
		this.lastvalue = '';
		return true;
	}
	
	function onblur() {
		if(!this.e.value) { return true; }
		if(!this.pfinal.test(this.e.value)) { this.e.select(); this.e.focus(); return false; }
	}
}

function BOSIValidateDate(e) {

	this.e = e;
	this.p = /^($|[0-9]{1,2})($|\/)($|[0-9]{1,2})($|\/)($|[0-9]{1,4})$/;
	this.pfinal = /^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2}|[0-9]{4})$/;
	this.lastvalue = '';

	e.bosivalidate = this;

	e.onkeypress = function() { this.bosivalidate.onkeypress(); };
	e.onkeyup = function() { this.bosivalidate.onkeyup(); };
	e.onblur = function() { this.bosivalidate.onblur(); };

	if (typeof(_BOSIValidateDate_prototype_called) == 'undefined')
	{
		_BOSIValidateDate_prototype_called = true;
		BOSIValidateDate.prototype.onkeypress = onkeypress;
		BOSIValidateDate.prototype.onkeyup = onkeyup;
		BOSIValidateDate.prototype.onblur = onblur;
		BOSIValidateDate.prototype.y2k = y2k;
	}
	
	function onkeypress() {
		this.lastvalue = this.e.value;
	}
	
	function onkeyup() {
		if(this.e.value && !this.p.test(this.e.value)) {
			for(i=this.lastvalue.length;i>=0;i--) {
				this.e.value = this.lastvalue.substring(0,i);
				if(this.p.test(this.e.value)) break;
			}
		}
		
		var lastfound = null;
		var count = 0;
		for(i=30;i<180;i++) {
			if( this.p.test(this.e.value+String.fromCharCode(i)) ) {
				count++;
				lastfound = String.fromCharCode(i);
			}
		}
		if(count==1) { this.e.value += lastfound; }
		
		this.lastvalue = '';
		return true;
	}
	
	function onblur(element,pfinal) {

		if(!this.e.value) { return true; }
		if(!this.pfinal.test(this.e.value)) { this.e.select(); this.e.focus(); return false; }

		var year, month, day, rc, test;
		rc = this.e.value.match(this.pfinal);
		day = parseFloat(rc[1]); month = parseFloat(rc[2]); year = parseFloat(rc[3]);
		test = new Date(this.y2k(year),month-1,day);
		if( (this.y2k(year) == test.getFullYear()) && (month-1 == test.getMonth()) && (day == test.getDate()) ) {
			this.e.value = test.getDate() + '/' + (test.getMonth()+1) + '/' + test.getFullYear()
			return true;
		} else {
			this.e.select(); this.e.focus(); return false;
		}
	}

	function y2k(number) { return (number < 1000) ? number + 2000 : number; }
}

function BOSIValidateDatetime(e) {

	this.e = e;
	this.p = /^($|[0-9]{1,2})($|\/)($|[0-9]{1,2})($|\/)($|[0-9]{1,4})( +([0-9]{0,2})?(:[0-9]{0,2})?(:[0-9]{0,2})?)?$/;
	this.pfinal = /^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2}|[0-9]{4})( +([0-9]{1,2}):([0-9]{1,2})(:([0-9]{1,2}))?)?$/;
	this.lastvalue = '';

	e.bosivalidate = this;

	e.onkeypress = function() { this.bosivalidate.onkeypress(); };
	e.onkeyup = function() { this.bosivalidate.onkeyup(); };
	e.onblur = function() { this.bosivalidate.onblur(); };

	if (typeof(_BOSIValidateDatetime_prototype_called) == 'undefined')
	{
		_BOSIValidateDatetime_prototype_called = true;
		BOSIValidateDatetime.prototype.onkeypress = onkeypress;
		BOSIValidateDatetime.prototype.onkeyup = onkeyup;
		BOSIValidateDatetime.prototype.onblur = onblur;
		BOSIValidateDatetime.prototype.y2k = y2k;
	}
	
	function onkeypress() {
		this.lastvalue = this.e.value;
	}
	
	function onkeyup() {
		if(this.e.value && !this.p.test(this.e.value)) {
			for(i=this.lastvalue.length;i>=0;i--) {
				this.e.value = this.lastvalue.substring(0,i);
				if(this.p.test(this.e.value)) break;
			}
		}
		
		var lastfound = null;
		var count = 0;
		for(i=30;i<180;i++) {
			if( this.p.test(this.e.value+String.fromCharCode(i)) ) {
				count++;
				lastfound = String.fromCharCode(i);
			}
		}
		if(count==1) { this.e.value += lastfound; }
		
		this.lastvalue = '';
		return true;
	}
	
	function onblur(element,pfinal) {

		if(!this.e.value) { return true; }
		if(!this.pfinal.test(this.e.value)) { this.e.select(); this.e.focus(); return false; }

		var year, month, day, rc, test, hrs, mins, secs;
		rc = this.e.value.match(this.pfinal);
		day = parseFloat(rc[1]); month = parseFloat(rc[2]); year = parseFloat(rc[3]);
		hrs = parseFloat(rc[5]); mins = parseFloat(rc[6]); secs = parseFloat(rc[8]);
		test = new Date(this.y2k(year),month-1,day,hrs,mins,secs);
		if( (this.y2k(year) == test.getFullYear()) && (month-1 == test.getMonth()) && (day == test.getDate()) ) {
			this.e.value = test.getDate() + '/' + (test.getMonth()+1) + '/' + test.getFullYear() + ' ' + test.getHours() + ':' + test.getMinutes() + ':' + test.getSeconds()
			return true;
		} else {
			this.e.select(); this.e.focus(); return false;
		}
	}

	function y2k(number) { return (number < 1000) ? number + 2000 : number; }
}

function BOSIValidateNumber(e,isfloat,minimum,maximum) {

	this.e = e;
	if(isfloat) {
		this.p = /^-?([0-9]+)|([0-9]*.[0-9]+)$/;
		this.pfinal = /^-?([0-9]+)|([0-9]*.[0-9]+)$/;
	} else {
		this.p = /^(-|[0-9])[0-9]*$/;
		this.pfinal = /^-?[0-9]+$/;
	}
	this.lastvalue = '';

	e.bosivalidate = this;

	e.onkeypress = function() { this.bosivalidate.onkeypress(); };
	e.onkeyup = function() { this.bosivalidate.onkeyup(); };
	e.onblur = function() { this.bosivalidate.onblur(); };

	if (typeof(_BOSIValidateNumber_prototype_called) == 'undefined')
	{
		_BOSIValidateNumber_prototype_called = true;
		BOSIValidateNumber.prototype.onkeypress = onkeypress;
		BOSIValidateNumber.prototype.onkeyup = onkeyup;
		BOSIValidateNumber.prototype.onblur = onblur;
	}
	
	function onkeypress() {
		this.lastvalue = this.e.value;
	}
	
	function onkeyup() {
		if(this.e.value && !this.p.test(this.e.value)) {
			for(i=this.lastvalue.length;i>=0;i--) {
				this.e.value = this.lastvalue.substring(0,i);
				if(this.p.test(this.e.value)) break;
			}
		}
		
		var lastfound = null;
		var count = 0;
		for(i=30;i<180;i++) {
			if( this.p.test(this.e.value+String.fromCharCode(i)) ) {
				count++;
				lastfound = String.fromCharCode(i);
			}
		}
		if(count==1) { this.e.value += lastfound; }
		
		this.lastvalue = '';
		return true;
	}
	
	function onblur(element,pfinal) {

		if(!this.e.value) { return true; }
		if(!this.pfinal.test(this.e.value)) { this.e.select(); this.e.focus(); return false; }

		var v = parseFloat(this.e.value);
		if( (this.minimum==null || v >= this.minimum) && (this.maximum==null || v <= this.maximum) ) {
			return true;
		} else {
			this.e.select(); this.e.focus(); return false;
		}
	}
}

/* Some predefined masks */
var pMaskIdentifier      = /^([A-Za-z][A-Za-z0-9_.]*)?$/;
var pMaskIdentifierFinal = /^[A-Za-z][A-Za-z0-9_.]*$/;

