var validacion="<%=rs_val(2)%>";

function mayuscula(objeto)
{
	alert (objeto);
}
function despliega(objeto)
{


//alert(objeto.selectedIndex)

	if (objeto.selectedIndex==0){
		 	document.getElementById("contestar").style.display="none";
			document.getElementById("news").style.display="none";
		  	document.getElementById("transparencia").style.display="none";
			//document.getElementById("menor").style.display="none";
			document.getElementById("capaTxtMensaje").style.display="none";
			document.getElementById("cod_region").selectedIndex=0;
			document.getElementById("Interes").checked=false;
			document.getElementById("checkCorreo").checked=false;
			document.getElementById("checkPresencial").checked=false;
			document.getElementById("sexo").style.display="block";
			document.getElementById("fechanac").style.display="block";
			document.getElementById("razonsocial").style.display="block";
			document.getElementById("direccion").style.display="none";
			document.getElementById("checkEmail").checked=false;
 }
	
	
	if ((objeto.selectedIndex!=0)&&(objeto.selectedIndex!=7)&& (objeto.selectedIndex!=10)){
			document.getElementById("contestar").style.display="block";
			document.getElementById("news").style.display="none";
		  	document.getElementById("transparencia").style.display="none";
			document.getElementById("capaTxtMensaje").style.display="block";
			//document.getElementById("menor").style.display="none";
			document.getElementById("cod_region").selectedIndex=0;
			document.getElementById("Interes").checked=false;
			document.getElementById("checkCorreo").checked=false;
			document.getElementById("checkPresencial").checked=false;
			document.getElementById("sexo").style.display="block";
			document.getElementById("fechanac").style.display="block";
			document.getElementById("razonsocial").style.display="block";
			document.getElementById("direccion").style.display="none";
			document.getElementById("checkEmail").checked=false;
	}
	
	
	
	if (objeto.selectedIndex==7){ // ME INTERESA RECIBIR INFORMACION
			document.getElementById("contestar").style.display="none";
			document.getElementById("news").style.display="block";
			document.getElementById("transparencia").style.display="none";
			document.getElementById("capaTxtMensaje").style.display="none";
			//document.getElementById("menor").style.display="none";
			document.getElementById("checkCorreo").checked=false;
			document.getElementById("checkPresencial").checked=false;
			document.getElementById("sexo").style.display="block";
			document.getElementById("fechanac").style.display="block";
			document.getElementById("razonsocial").style.display="block";
			document.getElementById("direccion").style.display="none";
			document.getElementById("checkEmail").checked=false;
	}

	
	if (objeto.selectedIndex==10){ // TRANSPARENCIA
			document.getElementById("contestar").style.display="none";
			document.getElementById("news").style.display="none";
			document.getElementById("transparencia").style.display="block";
			document.getElementById("capaTxtMensaje").style.display="block";
			//document.getElementById("menor").style.display="block";
			document.getElementById("cod_region").selectedIndex=0;
			document.getElementById("Interes").checked=false;
			document.getElementById("sexo").style.display="none";
			document.getElementById("fechanac").style.display="none";
			document.getElementById("razonsocial").style.display="none";
			document.getElementById("direccion").style.display="block";
	}
	}

function isEmpty(who) {
					var testArr=who.split("");
					if(testArr.length==0)
						return true;
					var toggle=0;
					for(var i=0; i<testArr.length; i++) {
						if(testArr[i]==" ") {
							toggle=1;
							break;
						}
					}
					if(toggle)
						return true;
					return false;
				}

				function isValid(who) {
					var invalidChars=new Array("~","!","@","$","%","^","*","(",")","+","=","[","]",":",",","\"","|","{","}","\\","/","<",">","?");
					var testArr=who.split("");
					for(var i=0; i<testArr.length; i++) {
						for(var j=0; j<invalidChars.length; j++) {
							if(testArr[i]==invalidChars[j]) {
								return false;
							}
						}
					}
					return true;
				}

				function isfl(who) {
					var invalidChars=new Array("-","_",".");
					var testArr=who.split("");

					which=0;
					for(var i=0; i<2; i++) {
						for(var j=0; j<invalidChars.length; j++) {
							if(testArr[which]==invalidChars[j]) {
								return false;
							}
						}
						which=testArr.length-1;
					}
					return true;
				}

				function isDomain(who) {
					var invalidChars=new Array("-","_",".");
					var testArr=who.split("");
					if(testArr.length<2||testArr.length>4) {
						return false;
					}
					for(var i=0; i<testArr.length; i++) {
						for(var j=0; j<invalidChars.length; j++) {
							if(testArr[i]==invalidChars[j]) {
								return false;
							}
						}
					}
					return true;
				}

 function isEmail(valor)
    {
	
																	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))
																	{
																		return (true);
																	} 
																	else 
																	{
																		alert("Estimado usuario, ingrese un email válido ya que será utilizado para enviarle la respuesta.");
																		return (false);
																	}
	
    }


function verificarChileSeleccionado()
{
			if(document.f2.selPais.selectedIndex!=46)			
			{
					alert("document.f2.selPais.selectedIndex!=46");
			}
}

function valida()
{
	
	if(estaVisibleLaCapa("capaPersonaNatural")){
		var respuesta = validarinput(document.f2.nombres);
		if (respuesta == false ){
			return false;
		}
		var respuesta =validarinput(document.f2.apellidos);
		if (respuesta == false ){
			return false;
		}
			
	}
	else//si NO estaVisibleLaCapa("capaPersonaNatural")
	{
		var respuesta =validarinput(document.f2.razonSocial);
		if (respuesta == false )
		{
			return false;
		}						
	}
	
	if (document.f2.objetivo.selectedIndex!=10)
		{
						/*if(document.f2.lugar_conexion.value == 0)
						{
									alert("Estimado Usuario, debe elegir un Lugar de Conexión");
									document.f2.lugar_conexion.focus();
									return false;
						}	*/			
						if(estaVisibleLaCapa("capaPersonaNatural")){
	
						var respuesta =validarinput(document.f2.fechaNac);
						
						if (respuesta == false ){
										return false;
						}
						
						var resultado=valFecha(document.f2.fechaNac);
								
						if(resultado == false){
								alert("Estimado Usuario, debe Ingresar una Fecha Valida.");
								return false;
						}
						
						if (document.f2.sexo[0].checked==true)
						{
											//ciudadano seleccionó mujer
						}else if(document.f2.sexo[1].checked==true)
						{
											//ciudadano seleccionó hombre
						}else{
											alert("Estimado Usuario, debe Señalar el Sexo");
											document.f2.sexo[1].focus();
											return false;
						}
						
		}
		
		
		}
	
	if(document.f2.selInstitucion.value == 0)	{
					alert("Estimado Usuario, debe seleccionar una Institución");
					document.f2.selInstitucion.focus();
					return false;					
		}
	
	
		if(document.f2.objetivo.value == 0)	{
					alert("Estimado Usuario, debe seleccionar el objetivo de su contacto");
					document.f2.objetivo.focus();
					return false;					
		}
		
			
		/*
																*******  VALIDAR AQUI EL SI(CHILE) , obligar a validar region  y comuna*****
		*/
				
		if (document.f2.objetivo.selectedIndex==1) /* CONSULTAS */
		{
		    //var respuesta =validarinput(document.f2.txtmensaje);
				 //var respuesta =(document.f2.txtmensaje);
	        //if (respuesta == false ){
						if(document.f2.txtmensaje.value==""){
							
						alert("Debe ingresar la consulta");
						document.f2.txtmensaje.focus();
	            return false;
					 }
							if (document.f2.contestar_a[0].checked==true)
							{						
										
										if(document.f2.email.value.length == 0)
										{
												document.f2.email.value=" ";//para que evalue la funcion isEmail
										}								
										var respuesta =isEmail(document.f2.email.value);
										if (respuesta == false )
										{			
														document.f2.email.focus();
														return false;
										}
										
							}
							if (document.f2.contestar_a[1].checked==true)
							{
										var respuesta =validarinput(document.f2.dir);
										if (respuesta == false )
										{
											return false;
										}
							}
							if (document.f2.contestar_a[2].checked==true)
							{
														var respuesta =validarinput(document.f2.fono);
														if (respuesta == false )
														{
																		return false;
														}
														var respuesta = esNumero(document.f2.fono.value);
														if (respuesta == false )
														{
																		return false;
														}	
							}
		}

		if (document.f2.objetivo.selectedIndex==7) /* news */
		{				
							if ((document.f2.Interes[0].checked==false)&&(document.f2.Interes[1].checked==false)&&(document.f2.Interes[2].checked==false))
							{
								alert("Estimado Usuario, seleccione su Area de Interes");
								return false;
							}
							
					if (document.f2.Interes[0].checked==true){
						if (document.f2.cod_region.value==0){
							alert("Estimado Usuario, seleccione región de interés.");
							document.f2.cod_region.focus();
							return false;
						}
					}	

					if (document.f2.Interes[0].checked==true)
					{
						var clickeado=false;
								if (document.f2.Interes_Agenda)
								{
												for (i=0;i<document.f2.Interes_Agenda.length;i++)
												{
															if (document.f2.Interes_Agenda[i].checked==true)
															{
																clickeado=true;
															}
												}
							
												if (clickeado==false)
												{
													alert("Estimado Usuario, seleccione Revistas y/o Publicaciones de Interes")
													return false;
												}
								}
					}			
						//alert("velide..");
		 //	return false;
		 }



		/* TODAS, MENOS SOLICITUD DE NEWS Y TRANSPARECNIA */
		if (document.f2.objetivo.selectedIndex!=7 && document.f2.objetivo.selectedIndex!=10)
		{
				//var respuesta =validarinput(document.f2.txtmensaje);
			//	var respuesta =(document.f2.txtmensaje);
										//if (respuesta == false )
										
					  if(document.f2.txtmensaje.value==""){
			 
							alert("Debe ingresar la consulta");
							document.f2.txtmensaje.focus();
						 return false;		
						}	

				
							if (document.f2.contestar_a[0].checked==true)
							{						
										
										if(document.f2.email.value.length == 0)
										{
												document.f2.email.value=" ";//para que evalue la funcion isEmail
										}								
										var respuesta =isEmail(document.f2.email.value);
										if (respuesta == false )
										{			
														document.f2.email.focus();
														return false;
										}
							}
							if (document.f2.contestar_a[1].checked==true)
							{
										var respuesta =validarinput(document.f2.dir);
										if (respuesta == false )
										{
											return false;
										}
							}
							if (document.f2.contestar_a[2].checked==true)
							{
														var respuesta =validarinput(document.f2.fono);
														if (respuesta == false )
														{
																		return false;
														}
														var respuesta = esNumero(document.f2.fono.value);
														if (respuesta == false )
														{
																		return false;
														}
							}
					
		}
		
		
		if (document.f2.objetivo.selectedIndex==10) /* TRANSPARENCIA */
		{	
					//var respuesta =validarinput(document.f2.txtmensaje);
				//	var respuesta =(document.f2.txtmensaje);
					//if (respuesta == false ){
									
					//}	
					
					
						if(document.f2.txtmensaje.value==""){
					 
							alert("Debe ingresar la consulta");
							document.f2.txtmensaje.focus();
							return false;
						}
					
					
			/*if(!document.f2.checkbox2.checked && !document.f2.checkbox3.checked && !document.f2.checkbox4.checked)
					{
						 alert("Estimado Usuario, debe seleccionar una Preferencia para recibir la respuesta");
							document.f2.checkbox2.focus();
							return false;							
					}*/
					
					
		}
		
		if(frames.regionComuna.document.getElementById('selPais').value == 46)             //si eligio chile
		{
						if(frames.regionComuna.document.getElementById('region').value == 16)          //si no ha elegido region
						{
									alert("Estimado Usuario, debe Seleccionar Region");
									frames.regionComuna.document.getElementById('region').focus();										
									return false;
						}
						else																																																												           //si eligió alguna region
						{
									if(frames.regionComuna.document.getElementById('selComuna').value==0)       //si no ha elegido comuna
									{
										alert("Estimado Usuario, debe Seleccionar Comuna");
										frames.regionComuna.document.getElementById('selComuna').focus();									
									
										return false;
									}
						}
																																																	
		}
				if (document.f2.objetivo.selectedIndex==10) /* TRANSPARENCIA */
		{	

					var respuesta =validarinput(document.f2.txtDireccion);
					if (respuesta == false )
					{
									return false;
					}

					
		}
		
		var respuesta =isEmail(document.f2.email.value);
					if (respuesta == false )
					{
						document.f2.email.focus();
						return false;
					}
		if(document.f2.checkbox2.checked)
					{		
								if(document.f2.email.value.length == 0)
								{
										document.f2.email.value=" ";//para que evalue la funcion isEmail
								}								
								var respuesta =isEmail(document.f2.email.value);
								if (respuesta == false )
								{			
												document.f2.email.focus();
												return false;
								}
					}
		
		var respuesta =validarinput(document.f2.validador);//el campo de captcha
	 if (respuesta == false )
		{
	     return false;
		}		
	 

		
		
var indicePais = window.frames.regionComuna.document.f3.selPais.selectedIndex;
var nombrePais =  window.frames.regionComuna.document.f3.selPais.options[indicePais].text;
	
var indiceComuna = window.frames.regionComuna.document.f3.selComuna.selectedIndex;
//alert(indiceComuna);
var nombreComuna =  window.frames.regionComuna.document.f3.selComuna.options[indiceComuna].text;

document.f2.hiddNombrePais.value =nombrePais;
document.f2.hiddNombreComuna.value =nombreComuna ;


//alert(document.f2.hiddNombrePais.value);
//alert(document.f2.hiddNombreComuna.value);
//return false;

 return true;





}

function muestraDireccion()
{
	 var elSelect = document.getElementById("selInstitucion");
		var elOption = elSelect.options[elSelect.selectedIndex];
		var laDireccion = elOption.title;
		var laCeldaDeTabla = document.getElementById("direccionUnidad");
		laCeldaDeTabla.style.fontWeight="bold";
		laCeldaDeTabla.innerHTML = "("+laDireccion+")";
}


function validarinput(f) {
	var nombre = f.name.toUpperCase();

	if (f.value.length < 1)
	{
			alert("Estimado Usuario, debe llenar todos los campos obligatorios para efectos de gestión interna"/*+ nombre.replace("TXT","")*/);
			f.focus();
			return (false);
	}
	var checkOK = ' ABCDEFGHIJKLMNNÑOPQRSTUVWXYZÁÉÍÓÚabcdefghijklmnñopqrstuvwxyzáéíóú.,!¡?¿()@0123456789&:;[]{}_-"#\'';
	var checkStr = f.value;
	var allValid = true;
	for (i = 0; i < checkStr.length; i++) 
	{
		ch = checkStr.charAt(i);
		for (j = 0; j < checkOK.length; j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) 
		{
                allValid = false;
                break;
            }
        }
        if (!allValid) 
        {
            alert("Estimado Usuario, escriba sólo letras, números, signos(. , ! ¡ ? ¿ ( ) @ : ; & { } [ ] \" { } [ ]),  en el campo "+ nombre+" , y no escriba saltos de linea");
            f.focus();
            return (false);
        } 
  }

        function validarinput2(f) {
            var nombre = f.name.toUpperCase();

        var checkOK = ' ABCDEFGHIJKLMNNÑOPQRSTUVWXYZÁÉÍÓÚabcdefghijklmnñopqrstuvwxyzáéíóú.,!¡?¿()@0123456789&:;[]ç{}_-"#\'';
        var checkStr = f.value;
        var allValid = true;
        for (i = 0; i < checkStr.length; i++) 
        {
            ch = checkStr.charAt(i);
            for (j = 0; j < checkOK.length; j++)
                if (ch == checkOK.charAt(j))
                    break;
            if (j == checkOK.length) 
            {
                allValid = false;
                break;
            }
        }
        if (!allValid) 
        {
            alert("Estimado Usuario, escriba sólo letras, números, signos(. , ! ¡ ? ¿ ( ) @ : ; & { } [ ] \" _ -),  en el campo "+ nombre);
            f.focus();
            return (false);
        }
        }																							
function mostrarCamposCorrespondientes(objSelect)
{
		var elSelect = objSelect;
		var opcionDeSelect = elSelect.value;
		if(opcionDeSelect == 1)
		{
				document.getElementById("capaPersonaNatural").style.display="block";
				document.getElementById("capaPersonaJuridica").style.display="none";
				document.f2.nombres.value="";
				document.f2.apellidos.value="";
		}
		else if(opcionDeSelect == 2)
		{
				document.getElementById("capaPersonaNatural").style.display="none";
				document.getElementById("capaPersonaJuridica").style.display="block";
				document.f2.razonSocial.value="";
		}
}

function estaVisibleLaCapa(strIdCapa){
			if(document.getElementById(strIdCapa).style.display == "block")
			{
					return true;
			}
			else if(document.getElementById(strIdCapa).style.display == "none")
			{
					return false;
			}
}
																								
																								
																								/********************PARA VALIDACION DE RUT******************/
function revisarDigito( dvr )
{	
	dv = dvr + ""	
	if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')	
	{		
		alert("Estimado Usuario, debe ingresar un digito verificador valido");		
		//window.document.f2.rut.focus();		
		//window.document.f2.rut.select();		
		return false;	
	}	
	return true;
}

function revisarDigito2( crut )
{	
	largo = crut.length;	
	if ( largo < 2 )	
	{		
		alert("Estimado Usuario, debe ingresar el rut completo")		
		//window.document.f2.rut.focus();		
		//window.document.f2.rut.select();		
		return false;	
	}	
	if ( largo > 2 )		
		rut = crut.substring(0, largo - 1);	
	else		
		rut = crut.charAt(0);	
	dv = crut.charAt(largo-1);	
	revisarDigito( dv );	

	if ( rut == null || dv == null )
		return 0	

	var dvr = '0'	
	suma = 0	
	mul  = 2	

	for (i= rut.length -1 ; i >= 0; i--)	
	{	
		suma = suma + rut.charAt(i) * mul		
		if (mul == 7)			
			mul = 2		
		else    			
			mul++	
	}	
	res = suma % 11	
	if (res==1)		
		dvr = 'k'	
	else if (res==0)		
		dvr = '0'	
	else	
	{		
		dvi = 11-res		
		dvr = dvi + ""	
	}
	if ( dvr != dv.toLowerCase() )	
	{		
		alert("Estimado Usuario, eL rut es incorrecto")		
		//window.document.f2.rut.focus();		
		//window.document.f2.rut.select();		
		return false	
	}

	return true
}

function Rut(texto)
{	
	var tmpstr = "";	
	for ( i=0; i < texto.length ; i++ )		
		if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
			tmpstr = tmpstr + texto.charAt(i);	
	texto = tmpstr;	
	largo = texto.length;	

	if ( largo < 2 )	
	{		
		alert("Estimado Usuario, Debe ingresar el rut completo")		
		//window.document.f2.rut.focus();		
		//window.document.f2.rut.select();		
		return false;	
	}	

	for (i=0; i < largo ; i++ )	
	{			
		if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" && texto.charAt(i) != "K" )
 		{			
			alert("Estimado Usuario, el valor ingresado no corresponde a un RUT valido");			
			//window.document.f2.rut.focus();			
			//window.document.f2.rut.select();				
			return false;		
		}	
	}	

	var invertido = "";	
	for ( i=(largo-1),j=0; i>=0; i--,j++ )		
		invertido = invertido + texto.charAt(i);	
	var dtexto = "";	
	dtexto = dtexto + invertido.charAt(0);	
	dtexto = dtexto + '-';	
	cnt = 0;	

	for ( i=1,j=2; i<largo; i++,j++ )	
	{		
		//alert("i=[" + i + "] j=[" + j +"]" );		
		if ( cnt == 3 )		
		{			
			dtexto = dtexto + '.';			
			j++;			
			dtexto = dtexto + invertido.charAt(i);			
			cnt = 1;		
		}		
		else		
		{				
			dtexto = dtexto + invertido.charAt(i);			
			cnt++;		
		}	
	}	

	invertido = "";	
	for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )		
		invertido = invertido + dtexto.charAt(i);	

	window.document.f2.rut.value = invertido.toUpperCase()		

	if ( revisarDigito2(texto) )		
		return true;	

	return true;
}


function esNumero(strNumero){ 
	var intNumero;
	intNumero=parseInt(strNumero);	
		if (isNaN(intNumero)) {
				alert("Estimado Usuario, el campo solo acepta numeros");
				return false;
		}else{
				return true;
		}
}


	 function numericos()
	 { 
        if (event.keyCode >= 48 && event.keyCode <= 57)
		return true; 	  
        return false; 
	 } 
										

