function trimjava(arg,func) {
/*AUTOR:  Ariel Manquegual
OBJETIVO:  Limpia los blancos de un string
ENTRADA:	arg: string a limpiar
			func: tipo de limpiesa; izq--> limpia blanco izquierda
									der--> limpia blanco derecha
									amb--> limpia blanco izquierda, derecha
SALIDA:  string sin blancos
FECHA CREACION: 05/05/2003
FECHA MODIFICACION: xx/xx/xxxx
MODIFICACION: xxxxxxxxxxxxxxxxxxxxxx
*/
	var trimvalue = "";	
	arglen = arg.length;
	if (arglen < 1)	return trimvalue;	
	if (func == "izq" || func== "amb") {
		i = 0;		
		pos = -1;		
		while (i < arglen) {			
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {				
				pos = i;				
				break;			
			}			
		i++;		
		}	
	}
	if (func == "der" || func== "amb") {		
		var lastpos = -1;		
		i = arglen;
		while (i >= 0) {			
			if (arg.charCodeAt(i) != 32 &&!isNaN(arg.charCodeAt(i))) {
				lastpos = i;				
				break;			
			}			
			i--;		
		}	
	}	
if (func == "izq"){trimvalue = arg.substring(pos,arglen-1);}		
if (func == "der"){trimvalue = arg.substring(0,lastpos+1);}	
if (func == "amb"){trimvalue = arg.substring(pos,lastpos + 1);}	

return trimvalue;
}

function Validaciones(){
/*AUTOR:  Ariel Manquegual
OBJETIVO:  Realiza validacion de campos de un formulario
ENTRADA:   
SALIDA:    
FECHA CREACION: 05/05/2003
FECHA MODIFICACION: xx/xx/xxxx
MODIFICACION: xxxxxxxxxxxxxxxxxxxxxx
*/

	var formu = document.forms[0];
	var Sw=true;
	var RetornoMsg="";
//	alert(formu.elements.length);
	for (c=0; c<formu.elements.length; c++){
		// ****** INICIO VALIDACION TIPO SELECT  ****//
		//alert(formu.elements[c].name + '->' + formu.elements[c].type + '-->' + formu.elements[c].validacion);
		if (formu.elements[c].type=='hidden'){
		//alert(formu.elements[c].name + '->' + formu.elements[c].type + '-->' + formu.elements[c].validacion);
			Sw=true;
			if (formu.elements[c].validacion){
				var CheckName = formu.elements[c].validacion;
				var nombre="";
				var ch = "_";
				for (i = 0;  i < CheckName.length;  i++){
					if (Sw){
						if (CheckName.charAt(i)=="_"){Sw=false;}
						else{nombre = nombre + CheckName.charAt(i);}
					}else{
						if (CheckName.charAt(i)=="F"){
							var Dia = eval("formu."+formu.elements[c].Dia+".value;");
							var Mes = eval("formu."+formu.elements[c].Mes+".value;");
							var Anio = eval("formu."+formu.elements[c].Anio+".value;");
							
							if (Dia!="" || Mes!="" || Anio!=""){
								if (!ValidaFecha(Dia, Mes, Anio)){
								RetornoMsg = RetornoMsg + 'Ingrese fecha correcta en el campo ' + nombre + '\n';
//									alert('Ingrese fecha correcta en el campo '+nombre);
//									eval("formu."+formu.elements[c].Dia+".focus();");
//									return false;
								}
							}
						}

						//******** validacion rut *******************
						if (CheckName.charAt(i)=="R"){
							var Rut = eval("formu."+formu.elements[c].Rut+".value;");
							var Digito = eval("formu."+formu.elements[c].Digito+".value;");
							
							if (Rut!="" || Digito!=""){	
								if (!ValidaRut(Rut, Digito)){
									RetornoMsg = RetornoMsg + 'Ingrese un RUT válido en el campo  el campo '+ nombre + '\n';								
//									alert('Ingrese un RUT válido en el campo  el campo '+nombre);
//									eval("formu."+formu.elements[c].Rut+".focus();");
//									return false;
								}
							}
						}
						//************************************


						if (CheckName.charAt(i)=="N" || CheckName.charAt(i)=="M" || CheckName.charAt(i)=="I" || CheckName.charAt(i)=="O"){
							var Dia1 = eval("formu."+formu.elements[c].Dia1+".value;");
							var Mes1 = eval("formu."+formu.elements[c].Mes1+".value;");
							var Anio1 = eval("formu."+formu.elements[c].Anio1+".value;");
							var Dia2 = eval("formu."+formu.elements[c].Dia2+".value;");
							var Mes2 = eval("formu."+formu.elements[c].Mes2+".value;");
							var Anio2 = eval("formu."+formu.elements[c].Anio2+".value;");
							var Nombre2 = formu.elements[c].Nombre;
							if (!ValidaFechas(Dia1, Mes1, Anio1, CheckName.charAt(i), Dia2, Mes2, Anio2)){
								if (CheckName.charAt(i)=="N"){
									var TipoMsg ="menor";
								}else if  (CheckName.charAt(i)=="M"){
									var TipoMsg ="mayor";
								}else if  (CheckName.charAt(i)=="I"){
									var TipoMsg ="menor o igual";
								}else if  (CheckName.charAt(i)=="O"){
									var TipoMsg ="mayor o igual";
								}
								msg = "El valor en "+ nombre + " debe ser " + TipoMsg + " que el valor en " + Nombre2;
								RetornoMsg = RetornoMsg + msg + nombre + '\n';
//								alert(msg);
//								eval("formu."+formu.elements[c].Dia1+".focus();");
//								return false;
							}
						}
						/********* validacion Check y  Radio ************/
						if (CheckName.charAt(i)=="<" || CheckName.charAt(i)=="=" || CheckName.charAt(i)==">"){

							var Nombre = formu.elements[c].Nombre;
							var Numero = formu.elements[c].Numero;
							var CantSelec=0;
						if (eval("formu."+Nombre+";"))
						{
							if (eval("formu."+Nombre+".length;")){
								var Cantidad = eval("formu."+Nombre+".length;");
								PosInicio = "(0)";
								for (Con=0; Con<Cantidad; Con++){
									if (eval("formu."+Nombre+"("+Con+").checked;")){
										CantSelec++;
									}
								}
							}else{
								PosInicio = "";
								if (eval("formu."+Nombre+".checked;")){
									CantSelec++;
								}
							} 

							if (CheckName.charAt(i)==">"){
								if (parseInt(CantSelec) > parseInt(Numero)){
									RetornoMsg = RetornoMsg + 'Debe seleccionar '+Numero+' Opción en ' + nombre + '\n';								
//									alert('Debe seleccionar '+Numero+' Opción en '+nombre);
//									eval("formu."+Nombre+PosInicio+".focus();")
//									return false;
								}
								if (parseInt(CantSelec) < 1){
									RetornoMsg = RetornoMsg + 'Debe seleccionar '+Numero+' Opción en ' + nombre + '\n';								
//									alert('Debe seleccionar '+Numero+' Opción en '+nombre);
//									eval("formu."+Nombre+PosInicio+".focus();")
//									return false;
								}
							}

							if (CheckName.charAt(i)=="<"){
								if (parseInt(CantSelec) < parseInt(Numero)){
									RetornoMsg = RetornoMsg + 'Debe seleccionar '+Numero+' Opción en ' + nombre + '\n';																
//									alert('Debe seleccionar '+Numero+' Opción en '+nombre);
//									eval("formu."+Nombre+PosInicio+".focus();")
//									return false;
								}
							}

							if (CheckName.charAt(i)=="="){
								if (parseInt(CantSelec) != parseInt(Numero)){
									RetornoMsg = RetornoMsg + 'Debe seleccionar '+Numero+' Opción en ' + nombre + '\n';																								
//									alert('Debe seleccionar '+Numero+' Opción en '+nombre);
//									eval("formu."+Nombre+PosInicio+".focus();")
//									return false;
								}
							}
						}else{
							RetornoMsg = RetornoMsg + 'Debe seleccionar '+Numero+' Opción en '+nombre + '\n';																														
//							alert('Debe seleccionar '+Numero+' Opción en '+nombre);
	//						return false;
						}


						}
						/********* Fin validacion Check y  Radio ************/
					}
				}
			}
		}

		if (formu.elements[c].type=='select-one'){
			Sw=true;
			if (formu.elements[c].validacion){
				var CheckName = formu.elements[c].validacion;
				var nombre="";
				var ch = "_";
				for (i = 0;  i < CheckName.length;  i++){
					if (Sw){
						if (CheckName.charAt(i)=="_"){Sw=false;}
						else{nombre = nombre + CheckName.charAt(i);}
					}else{
						if (CheckName.charAt(i)=="V"){
	//						alert(formu.elements[c].selectedIndex);
							if (formu.elements[c].selectedIndex<=0){
								//formu.elements[c].focus();
								RetornoMsg = RetornoMsg + 'El campo '+ nombre+ ' es obligatorio' + '\n';
//								alert('El campo '+ nombre+ ' es obligatorio');
//								return false;
//								break;
							}
						}
					}
				}
			}
		}
		// ****** FIN INICIO VALIDACION TIPO SELECT  ****//

		// ****** INICIO VALIDACION TIPO TEXT  ****//
//		alert(formu.elements[c].type + '-->' + formu.elements[c].validacion);
		if (formu.elements[c].type=='text' || formu.elements[c].type=='textarea' || formu.elements[c].type=='password' || formu.elements[c].type=='hidden'){
			Sw=true;
			if (formu.elements[c].validacion){
				var CheckName = formu.elements[c].validacion;
				var CheckValue = trimjava(formu.elements[c].value,'amb');
				var nombre="";
				var ch = "_";
				for (i = 0;  i < CheckName.length;  i++){
					if (Sw){
						if (CheckName.charAt(i)=="_"){Sw=false;}
						else{nombre = nombre + CheckName.charAt(i);}
					}else{
						if (CheckName.charAt(i)=="V"){
							if (CheckValue==""){
								//formu.elements[c].focus();
								RetornoMsg = RetornoMsg + 'El campo '+ nombre+ ' es obligatorio' + '\n';								
//								alert('El campo '+ nombre+ ' es obligatorio');
//								return false;
//								break;
							}
						}
						if (CheckName.charAt(i)=="E"){
							if (CheckValue!=""){
								if (!ValidaMail(CheckValue)){
							//		formu.elements[c].focus();
								RetornoMsg = RetornoMsg + 'Ingrese Email Valido en el campo '+ nombre + '\n';
//									alert('Ingrese Email Valido en el campo '+ nombre);
//									return false;
//									break;
								}
							}
						}
						if (CheckName.charAt(i)=="N"){
							if (!NoNumerico(CheckValue)){
							//	formu.elements[c].focus();
								RetornoMsg = RetornoMsg + 'Debe ingresar solo números en el campo '+ nombre + '\n';								
//								alert('Debe ingresar solo números en el campo '+ nombre);
//							return false;
//								break;
							}
						}
						if (CheckName.charAt(i)=="F"){
							if (CheckValue!=""){
								if (!ReconoceFecha(CheckValue)){
								//	formu.elements[c].focus();
								RetornoMsg = RetornoMsg + 'Ingrese fecha correcta en el campo '+ nombre + '\n';									
//									alert('Ingrese fecha correcta en el campo '+ nombre);
	//								return false;
//									break;
								}
							}
						}

					}
				}			
			}
		}

		// ****** FIN INICIO VALIDACION TIPO TEXT  ****//
	}
//	Habilitar();
	
	if (RetornoMsg!=''){
		alert(RetornoMsg);
		return false;
	}else{
		return true;
	}
}


function NoNumerico(Valor){
/*AUTOR:  Ariel Manquegual
OBJETIVO:  Realiza validacion de datos numericos
ENTRADA:   Valor: string con los datos a revisar
SALIDA:    "true" si son solo numeros "false" en caso contrario
FECHA CREACION: 05/05/2003
FECHA MODIFICACION: xx/xx/xxxx
MODIFICACION: xxxxxxxxxxxxxxxxxxxxxx
*/
	var checkOK = "0123456789";
	for (Numerici = 0;  Numerici < Valor.length;  Numerici++){
		ch = Valor.charAt(Numerici);
		for (Numericj = 0;  Numericj < checkOK.length;  Numericj++)
			if (ch == checkOK.charAt(Numericj)){
				break;
			}
			if (Numericj == checkOK.length){
				return false;
				break;
			}
	}



return true;
}

function ReconoceFecha(Valor){
/*AUTOR:  Ariel Manquegual
OBJETIVO:  Realiza validacion de datos numericos y separa fecha 
ENTRADA:   Valor: string con los datos a revisar
SALIDA:    "true" si son solo numeros "false" en caso contrario
FECHA CREACION: 04/09/2003
FECHA MODIFICACION: xx/xx/xxxx
MODIFICACION: xxxxxxxxxxxxxxxxxxxxxx
*/
	var checkOK = "0123456789/";
	var dia='', mes='', anio='', Sw=true, SwCon=0;

	for (Numerici = 0;  Numerici < Valor.length;  Numerici++){
		ch = Valor.charAt(Numerici);
		for (Numericj = 0;  Numericj < checkOK.length;  Numericj++)
			if (ch == checkOK.charAt(Numericj)){
				break;
			}
			if (Numericj == checkOK.length){
				return false;
				break;
			}
	}

	
	for (Numerici = 0;  Numerici < Valor.length;  Numerici++){
		ch = Valor.charAt(Numerici); Sw=true;
		if (ch=='/'){SwCon++; Sw=false;}
		if (SwCon==0 && Sw){dia = dia + ch;}
		if (SwCon==1 && Sw){mes = mes + ch;}
		if (SwCon==2 && Sw){anio = anio + ch;}
	}

	if (!ValidaFecha(dia, mes, anio)){
		return false;
	}

return true;
}

function ValidaMail(Valor){
/*AUTOR:  Ariel Manquegual
OBJETIVO:  Realiza validacion de email valido
ENTRADA:   Valor: string con los datos a revisar
SALIDA:    "true" si es mail con formato valido "false" en caso contrario
FECHA CREACION: 05/05/2003
FECHA MODIFICACION: xx/xx/xxxx
MODIFICACION: xxxxxxxxxxxxxxxxxxxxxx
*/
	if (!/^(.+\@.+\..+)$/.test(Valor)){return false; }
	return true;
}



function Habilitar(){
/*AUTOR:  Ariel Manquegual
OBJETIVO:  habilita los objetos de un formulario
ENTRADA:   
SALIDA:    
FECHA CREACION: 07/05/2003
FECHA MODIFICACION: xx/xx/xxxx
MODIFICACION: xxxxxxxxxxxxxxxxxxxxxx
*/
	var formu = document.forms[0];
	for (c=0; c<formu.elements.length; c++){
		if (formu.elements[c].disabled){formu.elements[c].disabled= false;}
	}
}


function ValidaFecha(Dia, Mes, Anio){
/*AUTOR:  Ariel Manquegual
OBJETIVO:  Valida que la fecha ingresada sea valida
ENTRADA:	
		Dia= dia a validar
		Mes= Mes a validar
		Anio= Año a validar
SALIDA: True si fecha es valida, False si fecha es incorrecta
FECHA CREACION: 13/06/2003
FECHA MODIFICACION: xx/xx/xxxx
MODIFICACION: xxxxxxxxxxxxxxxxxxxxxx
*/

/*	alert(Dia);
	alert(Mes);
	alert(Anio);
*/
	// Basico Error  & Chr(13)
	if (Mes < 1 || Mes > 12) return false;
	if (Dia < 1 || Dia > 31) return false;
	if (Anio.length == 4){
		if ((Anio < 1900) || (Anio > 2100))return false;
	}else{
		return false;
	}

	//Avanzado Error & Chr(13)
	// meses de 30 dias
	if (Mes == 4 || Mes == 6 || Mes == 9 || Mes == 11){
		if (Dia == 31) return false;
	}
	// Febrero,Años bisiestos & Chr(13)
	if (Mes == 2){
	// Febrero & Chr(13)
		var g=parseInt( Anio / 4 )
		if ( isNaN(g) ){
			return false;
		}
		if (Dia > 29) return false;
		if (Dia == 29 && ((Anio /4)!=parseInt(Anio/4))) return false;
	}

	return true;
}


function ValidaFechas(Dia1, Mes1, Anio1, Compara, Dia2, Mes2, Anio2){
/*AUTOR:  Ariel Manquegual
OBJETIVO: Valida intervalos de fechas
ENTRADA:	
		Dia = dia a validar
		Mes = Mes a validar
		Anio= Año a validar
SALIDA: True si fecha es valida, False si fecha es incorrecta
FECHA CREACION: 13/06/2003
FECHA MODIFICACION: xx/xx/xxxx
MODIFICACION: xxxxxxxxxxxxxxxxxxxxxx
*/
	if (Compara=="N"){
		if (parseInt(Anio1) > parseInt(Anio2)){
			return false;
		}

		if (parseInt(Mes1) > parseInt(Mes2)){
			if (parseInt(Anio1) >= parseInt(Anio2)){
				return false;
			  }
		}

		if (parseInt(Dia1) >= parseInt(Dia2)){	
			if (parseInt(Mes1) >= parseInt(Mes2)){
				if (parseInt(Anio1) >= parseInt(Anio2)){
					return false;
				}
			}
		}

		return true;
	}

	if (Compara=="I"){
		if (parseInt(Anio1) < parseInt(Anio2)){
			return false;
		}

		if (parseInt(Mes1) < parseInt(Mes2)){
			if (parseInt(Anio1) <= parseInt(Anio2)){
				return false;
			  }
		}

		if (parseInt(Dia1) < parseInt(Dia2)){	
			if (parseInt(Mes1) <= parseInt(Mes2)){
				if (parseInt(Anio1) <= parseInt(Anio2)){
					return false;
				}
			}
		}

		return true;
	}

	if (Compara=="M"){
		if (parseInt(Anio1) < parseInt(Anio2)){
			return false;
		}

		if (parseInt(Mes1) < parseInt(Mes2)){
			if (parseInt(Anio1) <= parseInt(Anio2)){
				return false;
			  }
		}

		if (parseInt(Dia1) <= parseInt(Dia2)){	
			if (parseInt(Mes1) <= parseInt(Mes2)){
				if (parseInt(Anio1) <= parseInt(Anio2)){
					return false;
				}
			}
		}

		return true;
	}

	if (Compara=="O"){
		if (parseInt(Anio1) < parseInt(Anio2)){
			return false;
		}

		if (parseInt(Mes1) < parseInt(Mes2)){
			if (parseInt(Anio1) <= parseInt(Anio2)){
				return false;
			  }
		}

		if (parseInt(Dia1) < parseInt(Dia2)){	
			if (parseInt(Mes1) <= parseInt(Mes2)){
				if (parseInt(Anio1) <= parseInt(Anio2)){
					return false;
				}
			}
		}

		return true;
	}
}


function ValidaRut(Rut, Digito){
/*AUTOR:  Ariel Manquegual
OBJETIVO: Valida Rut valido
ENTRADA:	
		Rut = valor del rut
		Digito = Digito rut
SALIDA: True si rut valida, False si rut es incorrecta
FECHA CREACION: 04/09/2003
FECHA MODIFICACION: xx/xx/xxxx
MODIFICACION: xxxxxxxxxxxxxxxxxxxxxx
*/
	
	Valor = Rut +"-"+ Digito;
	var checkStr = Valor;
	var ch = '-';
	var hayguion='0';

	if (checkStr == '0-0' || checkStr=='-'){return false;}

	for (ConRut=0 ; ConRut < checkStr.length; ConRut++){
		if (ch == checkStr.charAt(ConRut)){hayguion='1'}
	}

	if (hayguion == '0'){return false;}

	var checkOK = '0123456789-Kk';
	for (ConRut = 0;  ConRut < checkStr.length;  ConRut++){
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++){
			if (ch == checkOK.charAt(j)) break;
		}
		if (j == checkOK.length){
			return false;
			break;
		}
	}

	var largo = checkStr.length;
	var m = 2, r = 0, lar = largo - 3, num;
	
	for (ConRut=lar; ConRut > -1; ConRut--){
		if (ConRut == lar){
			var d = checkStr.charAt(ConRut+2);
			if(d == 'K' ){var d = 10;}
			if(d == 'k' ){var d = 10;}
			if(d == 0 ){var d = 11;}
		}
		
		num = checkStr.charAt(ConRut);
		var r1 = num * m;
		r = r + r1;
		var m2 = m + 1;
		var m = m2;
		if (m == 8){ m = 2;}
	}
	
	var r2 = r % 11;
	var r3 = 11 - r2;
	if (largo > 0){if (d != r3){return false;}}

return true;

}
