function trim(value, mode)
{

	if (value==null || value.length==0) return value;
	if (mode==null) mode = 'A';
	mode = mode.toUpperCase();
	if (mode=='N') return value;
	var result = value;
	if (mode=='L' || mode=='A')
		result = result.replace(/^\x20+/,'');
	if (mode=='R' || mode=='A')
		result = result.replace(/\x20+$/,'');
	if (mode=='A')
		for(;result!=result.replace(/\x20\x20/g,' ');result=result.replace(/\x20\x20/g, ' '));
	return result;
}

var Validators = null;

function RegisterValidator(oValidatorType)
{
	if (Validators==null) Validators = new Array();
	Validators[oValidatorType.Type] = oValidatorType;
}


//--------
function ValidatorType(type, fn_validate)
{
	this.Type = type;
	this.Validate = fn_validate;	//retorna la representacion del valor o null
	return this;
}


function validator_fn_regular_expression(value, regexp)
{
	if (arguments.length!=2) 
		throw('regular_expression validator: insufficient arguments. Use fn(value, regexp)');
	var result = regexp.test(value);
	return (result) ? value : null;
}

function validator_fn_date(value, regexp, format)
{
	if (arguments.length<3)
		throw('date validator: insufficient arguments. Use fn(value, regexp, format, [min], [max])');
		
	var dp = format.split('-');
	var vMatch = value.match(regexp);
	if (vMatch==null) return null;
	if (dp.length!=(vMatch.length-1)) {
		delete vMatch;
		return null;
	}
	
	var ds = new Array();
	ds.day = ds.month = ds.year = 0;
	for(i=0;i<dp.length;i++) 
		eval('ds.' + dp[i] + ' = ' + vMatch[i+1]);
	var dt = new Date(ds.year, ds.month-1, ds.day);
	result = (dt.getDate()==ds.day && dt.getMonth()==(ds.month-1) && (dt.getYear()==ds.year || dt.getFullYear()==ds.year))
	if (result && arguments.length>3) {
		var dmin = (arguments[3]!=null) ? validator_fn_date(arguments[3], regexp, format) : null;
		if (dmin!=null) {
			result = result && (dt.valueOf()>dmin.valueOf());
			delete dmin;
		}
		if (result && arguments.length==5) {
			var dmax = validator_fn_date(arguments[4], regexp, format);
			if (dmax!=null) {
				result = result && (dt.valueOf()<dmax.valueOf());
				delete dmax;
			}
		}
	}
	delete ds;
	delete vMatch;
	return (result) ? dt : null;
}


function validator_fn_integer(value)
{
	if (arguments.length!=1) 
		throw('integer validator: insufficient arguments. Use fn(value)');
	var value = validator_fn_regular_expression(value, /[0-9]*/);
	if (value==null) return null;
	var integerValue = Number(value);
	var result = (integerValue.toString() == value);
	return (result) ? integerValue : null;
}

function validator_fn_real(value, regexp)
{
	if (arguments.length!=2) 
		throw('real validator: insufficient arguments. Use fn(value, regexp)');
	var vMatch = value.match(regexp);
	if (vMatch==null) 
		return null;
	if (vMatch.length!=2) 
		throw('real validator: regexp erronea. No incluye el patrón de captura del separador decimal');
	var realValue = (vMatch[1]!='') ? Number(value.replace(vMatch[1],'.')) : Number(value);
	var result = !isNaN(realValue);
	return (result) ? realValue : null;
}


RegisterValidator(new ValidatorType('regular_expression', validator_fn_regular_expression));
RegisterValidator(new ValidatorType('date', validator_fn_date));
RegisterValidator(new ValidatorType('integer', validator_fn_integer));	
RegisterValidator(new ValidatorType('real', validator_fn_real));	







function Validate(srch) { 
    b = String.fromCharCode(window.event.keyCode).search(srch);
    return (b>=0); 
}


var wnd_error = null;
function show_error_window(sMsg)
{
		html = 
			'<html>\n' +
			'<head>\n' +
			'<title>Portfolio Personal</title>\n' +
			'<link rel="stylesheet" href="../portfolio.css">\n' +
			'<link rel="stylesheet" href="../portfolio_links.css">\n' +
			'<link rel="stylesheet" href="../portfolio_2.css">\n' +
			'<link rel="stylesheet" href="errormsg.css">\n' +
			'</head>\n' +
			'<body>\n' +
			'<div class="textocuadrogris">\n' +
			'Algunos datos faltan o son incorrectos. Puede utilizar la siguiente \n'+
			'lista para guiarse en la corrección:\n'+
			'<ul>\n' + sMsg + '</ul>\n'+
			'</div>\n'+
			'<center>\n' +
			'<a href="javascript:top.close()"><img src="../imagenes/boto_cerrar.gif" border=0></a>\n'+
			'</center>\n' +
			'</body>\n' + 
			'</html>';
			
		if (wnd_error!=null && !wnd_error.closed) wnd_error.close();
		pos_left = screen.availWidth - 260;
		wnd_error = window.open('errormsg.asp', 'err_msg', 'left=' + pos_left + ',location=no,status=no,width=250,height=300,scrollbars=yes,resizable=yes');
		wnd_error.document.open();
		wnd_error.document.write(html);
		wnd_error.document.close();
}



function unload() {
	if (wnd_error!=null && !wnd_error.closed) wnd_error.close();
}

function solonumeros()
{
var tecla = window.event.keyCode;
if (tecla != /[.,-_$%&/()=?¿¡'+`´ç^*Ç¨ª!º]/ || tecla > 57)
{
window.event.keyCode=0;
}
}
