function validarCampo(campo, msgError, longitudMinima, longitudMaxima, funcion) {
    if (!longitudMinima  && longitudMinima != 0) {
        longitudMinima = 1
    }
    if (!longitudMaxima ) {
		longitudMaxima = Number.MAX_VALUE
    }
    var value = trim(campo.value)
    if (value.length < longitudMinima || value.length > longitudMaxima ||
        (funcion ? !eval("funcion(campo)") : false)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            _campoError = campo;
        }
        return false;
    }
    return true;
}

function validarLista(lista, msgError, indiceInicial, funcion) {
    if (!indiceInicial && indiceInicial != 0) {
        indiceInicial = 1;
    }
    if (lista.selectedIndex < indiceInicial ||
        (funcion ? !eval("funcion(lista)") : false)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            _campoError = lista;
        }
        return false;
    }
    return true;
}

function validarSeleccion(campo, msgError, funcion) {
    if (!isChecked(campo) ||
        	(funcion ? !eval("funcion(campo)") : false)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            if (campo.length) {
                _campoError = campo[0]
            } else {
                _campoError = campo;
            }
        }
        return false;
    }
    return true;
}

function validarCampoOpcional(campo, msgError, longitudMinima, longitudMaxima, funcion) {
	if (trim(campo.value).length > 0) {
		return validarCampo(campo, msgError, longitudMinima, longitudMaxima, funcion)
	}
}

function validarFecha(dia, mes, ano, msgError, funcion) {
	 if (!isIntRange(parseInt(dia.value, 10) + "", 1, 31)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            _campoError = dia;
        }
        return false;
	}
	if (mes.type != "text") {
		if (mes.selectedIndex < 1) {
			appendErrorMessage(msgError)
			if (_campoError == null) {
				_campoError = mes;
			}
			return false;
		}
	}
	if (!isInt(ano.value)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            _campoError = ano;
        }
        return false;
	}
	var valorMes = (mes.type == "text")
			? mes.value
			: mes.options[mes.selectedIndex].value;
    if (!isDateDMY(dia.value + "/" + valorMes + "/" + ano.value) ||
        (funcion ? !eval("funcion(dia, mes, ano)") : false)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            _campoError = dia;
        }
        return false;
    }
    return true;
}


/*Valida una fecha que se encuentra en un solo campo. en formato DD/MM/YYYY*/
function validarFechaUnCampo(campo, msgError, funcion){
    var value = trim(campo.value)
    if (value.length == 0 || value.length > 10 ||
    	!isDateDMY(value) ||
        (funcion ? !eval("funcion(campo)") : false)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            _campoError = campo;
        }
        return false;
    }
    return true;
}



function validarFechaOpcional(dia, mes, ano, msgError, funcion) {
	var entroMes =  (mes.type == "text")
			? (trim(dia.value).length > 0)
			: (mes.selectedIndex > 0);
	if ((trim(dia.value).length > 0) || entroMes
			|| (trim(ano.value).length > 0)) {
		return validarFecha(dia, mes, ano, msgError, funcion)
	}
}


function validarRangoFechas(dia1,mes1,ano1,dia2,mes2,ano2,msgError,funcion) {
	var valorMes1 = (mes1.type == "text")
			? mes1.value
			: mes1.options[mes1.selectedIndex].value;
	var valorMes2 = (mes2.type == "text")
			? mes2.value
			: mes2.options[mes2.selectedIndex].value;
	if (compareDatesDMY(dia1.value + "/" + valorMes1 + "/" + ano1.value, dia2.value + "/" + valorMes2 + "/" + ano2.value) >= 0) {
		appendErrorMessage(msgError)
		if (_campoError == null) {
		    _campoError = dia1;
		}
		return false;
	}
	return true;
}


function validarEntero(campo, msgError, limiteInferior, limiteSuperior,
		funcion) {
	if (!limiteInferior && limiteInferior != 0) {
		limiteInferior = Number.NEGATIVE_INFINITY
		limiteSuperior = Number.MAX_VALUE
	}
	if (!isIntRange(campo.value, limiteInferior, limiteSuperior) ||
        (funcion ? !eval("funcion(campo)") : false)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            _campoError = campo;
        }
        return false;
	}
	return true;
}

function validarEnteroOpcional(campo, msgError, limiteInferior, limiteSuperior, funcion) {
	if (trim(campo.value).length > 0) {
		return validarEntero(campo, msgError, limiteInferior, limiteSuperior, funcion)
	}
}


function validarFloat(campo, msgError, limiteInferior, limiteSuperior, funcion) {
	if (!limiteInferior && limiteInferior != 0) {
		limiteInferior = Number.NEGATIVE_INFINITY
		limiteSuperior = Number.MAX_VALUE
	}
	if (!isFloatRange(campo.value, limiteInferior, limiteSuperior) ||
        (funcion ? !eval("funcion(campo)") : false)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            _campoError = campo;
        }
        return false;
	}
	return true;
}

function validarFloatOpcional(campo, msgError, limiteInferior, limiteSuperior, funcion) {
	if (trim(campo.value).length > 0) {
		return validarFloat(campo, msgError, limiteInferior, limiteSuperior, funcion)
	}
}

function validarEmail(campo, msgError) {
	if (!isEmail(campo.value)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            _campoError = campo;
        }
        return false;
	}
	return true;
}


function validarEmailOpcional(campo, msgError) {
	if (trim(campo.value).length > 0) {
		return validarEmail(campo, msgError);
	}
}


//var _mensajeAdvertencia_en = "Please check the following item(s): \n\n";
//var _mensajeAdvertencia_es = "I- Por favor revise esto(s) item(s): \n\n";

function validarForma_en(forma) {
	var _mensajeAdvertencia = "Please check the following item(s): \n\n";
	
	_errores = ""
	_campoError = null;

	eval("hacerValidaciones_" + forma.name +"(forma)");
	if (_errores != "") {
		alert(_mensajeAdvertencia + _errores);
		_campoError.focus();
		return false;
	}
	return true;
}

function validarForma_es(forma) {
    var _mensajeAdvertencia = "Por favor revise esto(s) item(s): \n\n";

 	_errores = ""
	_campoError = null;
	
	eval("hacerValidaciones_" + forma.name +"(forma)");
	if (_errores != "") {
		alert(_mensajeAdvertencia + _errores);
		_campoError.focus();
		return false;
	}
	return true;
}


 function controlarLongitudTextArea(teclaPresionada, campo, maximaLongitud) {
     var longitud = campo.value.length;

     var valTecla = teclaPresionada.which ? parseInt(teclaPresionada.which, 10)
             : (teclaPresionada.keyCode ? parseInt(teclaPresionada.keyCode, 10)
 : null);

     if (valTecla != null) {
         if ((valTecla != 8) && (valTecla != 46)) {
             if (longitud > maximaLongitud-1) {
                 return false;
             }
         }
     }
     return true;
}






/**
Estas funciones estଠprobadas en browsers Netscape 4.x y Explorer 4.x.
*/
function rtrim(cadena) {
    cadena += "";
    for (var i = cadena.length -1; (i >= 0) && ((cadena.charAt(i) == ' ')); i--)
        ;
    return cadena.substring(0, i+1);
}

function ltrim(cadena) {
    cadena += "";
    for (var i = 0; (i < cadena.length) && ((cadena.charAt(i) == ' ')); i++)
        ;
    if (i == cadena.length) {
        return "";
    }
    return cadena.substring(i);
}

function trim(cadena) {
    return ltrim(rtrim(cadena));
}

/** Las siguientes funciones se utilizan para validar un n� de punto flotante. */
var r1Float = new RegExp("(\,\,)|([0-9]{4}\,)|(^[0]{0,}\,)|([\,]+[0-9]{0,2}[\,$])|(\\..*[\\.\,])");
var r2Float = new RegExp("(((\\[?)[0-9]\\,[0-9]{3}(\\]?))|(^[0-9]*))(\\.[0-9]+)?$")

function isFloat(number) {
	number = trim(number);
	
	if (number.length == 0) {
		return false;
	}
	
	if (isNaN(parseFloat(number))) {		
		return false;
	} else {		
		return true;
	}	
}


/*Esta funcion recibe un float y un rango y lo valida*/
function isFloatRange(number, min, max)  {
	if (!isFloat(number)) {
		return false;
	}
	var theValue = (number.split(",").join(""))*1;
	return theValue >= min && theValue <= max
}

var r1Int = new RegExp("(\,\,)|([0-9]{4}\,)|(^[0]{0,}\,)|([\,]+[0-9]{0,2}[\,$])");
var r2Int = new RegExp("((\\[?)[0-9]\\,[0-9]{3}(\\]?)$)|(^[0-9]+)$")

function isInt(number) {
	number = trim(number + "");
	return (!r1Int.test(number) && r2Int.test(number));
}

/*Esta funcion recibe un entero y un rango y lo valida*/
function isIntRange(number, min, max)  {
	if (!isInt(number)) {
		return false;
	}
	var theValue = (number.split(",").join(""))*1;
	return theValue >= min && theValue <= max
}


/*Esta funcion valida una hora y retorna falso o verdadero*/
/* Recibe el formato de HH:MM*/
function isTime(time)  {
	var HP
	time = trim(time)
	HP = time.split(":")
	if (HP.length != 2) {
		return false;
	}
	return(HP[0] < 24 && HP[0] >= 0 && HP[1] < 60 && HP[1] >= 0)
}


/*Esta funcion valida una fecha y retorna falso o verdadero*/
/* Recibe el formato de Dia/Mes/Ano*/
function isDateDMY(date)  {
	var FP
	date = trim(date)
	FP = date.split("/")
	if (FP.length != 3) {
		return false
	}
	return isSplitDateDMY(FP[0], FP[1], FP[2])
}


function isSplitDateDMY(day, month, year)  {
	if (!isInt(day) || !isInt(month) || !isInt(year)) {
		return false;
	}
	date = new Date(year, month-1, day)
	return ((date.getDate() == (day)*1) && ((date.getMonth()+1) == (month)*1) && (date.getFullYear() == (year)*1));
}


// Retorna si la fecha inferior efectivamente es inferior
// que la fecha superior. La fecha debe encontrarse en formato dd/mm/yyyy
// -1 a < b, 0 a == b, 1 a > b
function compareDatesDMY(a, b) {
	var firstDateArray, secondDateArray;
	firstDateArray = a.split("/");
	secondDateArray = b.split("/");
	aDate = new Date(firstDateArray[2], firstDateArray[1]-1, firstDateArray[0]);
	bDate = new Date(secondDateArray[2], secondDateArray[1]-1, secondDateArray[0]);
	if (aDate.getTime() < bDate.getTime()) {
		return -1;
	}
	if (aDate.getTime() == bDate.getTime()) {
		return 0;
	}
	return 1;
}

function isChecked(field) {
	if (!field) {
		return false;
	}
	if (field.length) {
		for (var i = 0; i < field.length; i++) {
			if (field[i].checked) {
				return true;
			}
		}
		return false;
	}
	return field.checked;
}


/** Valida un email */
function isEmail(str) {
  var indexLT = str.indexOf('<');
  var indexGT = str.indexOf('>');
  var email = str;
  if (indexGT > indexLT && indexGT > -1 && indexLT > -1) {
  	email = email.substring(indexLT+1, indexGT);
  }
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(email) && r2.test(email));
}

/** Estas son la validaciones en si de las formas */

var _campoError = null;
var _errores    = "";

function appendErrorMessage(msgError) {
    _errores += ((_errores != "") ? "\n":"") + "        " + msgError
}
