var cssClassDefault = "";
var cssClassDefaultError = "campoError";
var cssClassDefaultCombo = "select";
var cssClassDefaultErrorCombo = "campoError";


function ValidarAlMenosUnoSeleccionado(sNombreGrupoControles){
	var bAlgunoSeleccionado = false;
	var sColor = 'Red';
	var oArrElementosGrupo = document.getElementsByName(sNombreGrupoControles);
	for (var i=0; i < oArrElementosGrupo.length; i++){
		if (oArrElementosGrupo[i].checked == true){
			bAlgunoSeleccionado = true;
			break;
		}
	}
	return bAlgunoSeleccionado;
}
function SetControlInvalido(oElemento){
	oElemento.className = cssClassDefaultError;
	oElemento.focus();
}
function SetControlValido(oElemento){
	oElemento.className = cssClassDefault;
}
function SetComboInvalido(oElemento){
	oElemento.className = cssClassDefaultErrorCombo;
	oElemento.focus();
}
function SetComboValido(oElemento){
	oElemento.className = cssClassDefaultCombo;
}
function Validar(){
    return ValidarGroup('');
}
function ValidarGroup(grupo){
	var bReturnValue = true;
	var bValidacionElemento;
	var oTiposElementos = new Array();
	var oElementos;
	var grupoActual = '';
	var j = 0;
	oTiposElementos[0] = document.getElementsByTagName("input");
	oTiposElementos[1] = document.getElementsByTagName("textarea");
	oTiposElementos[2] = document.getElementsByTagName("select");
	while (j < oTiposElementos.length) {
		oElementos = oTiposElementos[j];

		for (var i=0; i < oElementos.length; i++){
		    if(oElementos[i].getAttribute('validationgroupname')==null || oElementos[i].getAttribute('validationgroupname')==undefined){
		       grupoActual = ''; 
		    }else{
		        grupoActual = oElementos[i].getAttribute('validationgroupname');
		    }
            if(grupoActual==grupo){
			    if (oElementos[i].style.display == 'none') {
				    continue;
			    }
			    if (oElementos[i].getAttribute('type') == 'button' || oElementos[i].getAttribute('type') == 'submit' || oElementos[i].getAttribute('type') == 'reset' || oElementos[i].getAttribute('type') == 'checkbox' || oElementos[i].className == 'campoBusqueda'){
				    continue;
			    }
			    if (oElementos[i].getAttribute('obligatory') == 'true' && (oElementos[i].value == '0' || oElementos[i].value == '') && j==2){
			        SetComboInvalido(oElementos[i]);
				    bReturnValue = false;
				    continue;
			    }
			    if (oElementos[i].getAttribute('obligatory') == 'false' && oElementos[i].value == '0' && j==2){
			        SetComboValido(oElementos[i]);
				    continue;
			    }
			    if (oElementos[i].getAttribute('obligatory') == 'true' && oElementos[i].getAttribute('type') == 'radio' && !ValidarAlMenosUnoSeleccionado(oElementos[i].getAttribute('name'))){
				    SetControlInvalido(oElementos[i]);
				    bReturnValue = false;
				    continue;
			    }
			    if (oElementos[i].getAttribute('obligatory') == 'true' && trim(oElementos[i].value) == ''){
				    SetControlInvalido(oElementos[i]);
				    bReturnValue = false;
				    continue;
			    }
			    if (!(oElementos[i].getAttribute('minlength') == '' || oElementos[i].getAttribute('minlength') == null) && trim(oElementos[i].value) != '' && oElementos[i].value.length < parseInt(oElementos[i].getAttribute('minlength'))){
				    SetControlInvalido(oElementos[i]);
				    bReturnValue = false;
				    continue;
			    }
			    if (oElementos[i].getAttribute('tipo') == '' || oElementos[i].getAttribute('tipo') == null || trim(oElementos[i].value) == ''){
				    SetControlValido(oElementos[i]);
				    continue;
			    }
			    eval('bValidacionElemento = Validar' + oElementos[i].getAttribute('tipo') + '(oElementos[i].value);');
			    if (bValidacionElemento){
			        SetControlValido(oElementos[i]);
			    }else{
			        if(j==2){
   				        SetComboInvalido(oElementos[i]);
   				    }else{
   				        SetControlInvalido(oElementos[i]);
   				    }
				    bReturnValue = false;
			    }
			}
		}
		j++;
	}
	return bReturnValue;
}
function ValidarNumero(sNumero){
	return !isNaN(sNumero) && sNumero.indexOf('.') == -1;
}
function ValidarNumeroPositivo(sNumeroPositivo){
	return !isNaN(sNumeroPositivo) && sNumeroPositivo.indexOf('.') == -1 && sNumeroPositivo.indexOf('-') == -1;
}
function ValidarNumeroDecimal(sNumeroDecimal){
	if (sNumeroDecimal == ''){
		return true;
	}
	if(sNumeroDecimal.indexOf('.') != -1){
		return false;
	}
	//sNumeroDecimal = ReemplazarEnCadena(sNumeroDecimal,',','.');
	sNumeroDecimal = sNumeroDecimal.replace(',','.');
	oArrNumeroDecimal = sNumeroDecimal.split('.');
	if(oArrNumeroDecimal.length > 2){
		return false;
	}
	if (isNaN(oArrNumeroDecimal[0])){
		return false;
	}
	if (oArrNumeroDecimal[1] != null && isNaN(oArrNumeroDecimal[1])){
		return false;
	}
	return true;
}
function ValidarNumeroDecimalPositivo(sNumeroDecimalPositivo){
	if (sNumeroDecimalPositivo.indexOf('-') != -1){
		return false;
	}
	if (!ValidarNumeroDecimal(sNumeroDecimalPositivo)) {
		return false;
	}
	return true;
}
function ValidarPorcentaje(sPorcentaje){
	if (!ValidarNumeroDecimal(sPorcentaje)) {
		return false;
	}
	if (parseFloat(sPorcentaje) < -100 || parseFloat(sPorcentaje) > 100){
		return false;
	}
	return true;
}
function ValidarPorcentajePositivo(sPorcentajePositivo){
	if (sPorcentajePositivo.indexOf('-') != -1){
		return false;
	}
	if (!ValidarPorcentaje(sPorcentajePositivo)) {
		return false;
	}
	return true;
}
function ValidarEMail(sEMail) {
	var oRegExp = new RegExp("^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z_])*@([0-9a-zA-Z][-.\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$");
	return sEMail.match(oRegExp);
}
function ValidarURL(sURL) {
	var oRegExp = new RegExp("(http|https|ftp)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/[^/][a-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\$#\\=~_\\-]*)*");
	return sURL.match(oRegExp);
}
function ValidarHorario(sHorario) {
	oArrHorario = sHorario.split(':');
	if (oArrHorario.length != 2) {
		return false;
	}
	if (oArrHorario[0].length != 2 || oArrHorario[1].length != 2){
		return false;
	}
	if (!ValidarNumero(oArrHorario[0]) || !ValidarNumero(oArrHorario[1])){
		return false;
	}
	if (parseInt(oArrHorario[0]) < 0 || parseInt(oArrHorario[0]) > 23){
		return false;
	}
	if (parseInt(oArrHorario[1]) < 0 || parseInt(oArrHorario[1]) > 59){
		return false;
	}
	return true;
}
function ValidarHorarioConSegundos(sHorario) {
	oArrHorario = sHorario.split(':');
	if (oArrHorario.length != 3) {
		return false;
	}
	if (oArrHorario[0].length != 2 || oArrHorario[1].length != 2 || oArrHorario[2].length != 2){
		return false;
	}
	if (!ValidarNumero(oArrHorario[0]) || !ValidarNumero(oArrHorario[1]) || !ValidarNumero(oArrHorario[2])){
		return false;
	}
	if (parseInt(oArrHorario[0]) < 0 || parseInt(oArrHorario[0]) > 23){
		return false;
	}
	if (parseInt(oArrHorario[1]) < 0 || parseInt(oArrHorario[1]) > 59){
		return false;
	}
	if (parseInt(oArrHorario[2]) < 0 || parseInt(oArrHorario[2]) > 59){
		return false;
	}
	return true;
}
function ValidarFecha(dtStr){
	var dtCh= '/';
	var minYear=1900;
	var maxYear=2050;
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1){
		strDay=strDay.substring(1);
	}
	if (strMonth.charAt(0)=="0" && strMonth.length>1){
		strMonth=strMonth.substring(1);
	}
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1){
			strYr=strYr.substring(1);
		}
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false;
	}
	return true;
}
function stripCharsInBag(s, bag){
	var i;
    var returnString = '';
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	var arrDaysInEveryMonth = new Array();
	for (var i = 1; i <= n; i++) {
		arrDaysInEveryMonth[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {
			arrDaysInEveryMonth[i] = 30;
		}
		if (i==2) {
			arrDaysInEveryMonth[i] = 29;
		}
   } 
   return arrDaysInEveryMonth;
}
function isInteger(s){
    for (var i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}
function ltrim(s) {
   return s.replace(/^\s+/, "");
}

function rtrim(s) {
   return s.replace(/\s+$/, "");
}

function trim(s) {
   return rtrim(ltrim(s));
}