// JavaScript Document

function validar()
{
	if ((document.frmsug.nombre.value== '' ) || (document.frmsug.apellido.value == ''))
	 {
			alert('Debe ingresar su nombre o apellido');
		   document.frmsug.nombre.focus();
		   return false;
	 }
	
	if (document.frmsug.mensaje.value == '')
	{
		   alert('Debe ingresar una descripción');
		   document.frmsug.mensaje.focus();
		   return false;
	}
	if (document.frmsug.destino.value == 'Seleccionar')
	{
       alert('Debe seleccionar un departamento');
//	   document.frmsug.msje.focus();
	   return false;
		}
	
	var Email = document.frmsug.mail.value;
    var Formato = /^([\w-\.])+@([\w-]+\.)+([a-z]){2,4}$/;
	var Comparacion = Formato.test(Email);
     if(Comparacion == false)
	 {
          alert("El correo ingresado no es válido");
          return false;
      }
	document.frmsug.submit();
}

function valida()
{
	 if (document.frmcontacto.nombre.value == "" || document.frmcontacto.apellido.value == "")
	{
       alert('Tiene que escribir su Nombre y Apellido correctamente')
       return false;
	}
	if (document.frmcontacto.destino.value == 'Seleccionar')
	{
       alert('Debe seleccionar un departamento')
	   return false;
		}
	if (document.frmcontacto.mensaje.value == '')
	{
		   alert('Debe ingresar tu mensaje')
		   return false;
		}
	

	var Email = document.frmcontacto.mail.value;
    var Formato = /^([\w-\.])+@([\w-]+\.)+([a-z]){2,4}$/;
	var Comparacion = Formato.test(Email);
     if(Comparacion == false)
	 {
          alert("El correo ingresado no es válido");
          return false;
     	}
	 document.frmcontacto.submit();
}	

function validafrmcurri()
{
	 if (document.frmcurri.nombre.value == "" || document.frmcurri.apellido.value == "")
	{
       alert('Tiene que escribir su Nombre y Apellido correctamente')
       return false;
	}
	
	var Email = document.frmcurri.mail.value;
    var Formato = /^([\w-\.])+@([\w-]+\.)+([a-z]){2,4}$/;
	var Comparacion = Formato.test(Email);
     if(Comparacion == false)
	 {
          alert("El correo ingresado no es válido");
          return false;
     	}
	 document.frmcurri.submit();
}




function validarsolicitud()
{
	if (document.frmsolicitud.nombres.value == "" || document.frmsolicitud.ap_pat.value == "" || document.frmsolicitud.ap_mat.value == "" || document.frmsolicitud.rut.value == "" || document.frmsolicitud.telefono.value == "")
	{
       alert('Los campos "Rut", "Nombres", "Apellido Materno", "Apellido Paterno", "Telefono" son obligatorios')
       //document.frmsolicitud.nombres.focus();
	   return false;
	}
	
	if (document.frmsolicitud.renta.value == "" )
	{
       alert('Debe indicar renta familiar')
       return false;
	}
	
	if (document.frmsolicitud.sexo.value == 0)
	{
       alert('Falta especificar sexo')
	   return false;
		}
	
	if (document.frmsolicitud.estadocivil.value == 0)
	{
       alert('Falta indicar estado civil')
	   return false;
	}
	document.frmsolicitud.enviar.disabled = true ;
}

	
     /*function DrawBotBoot()
    {
		var a = Math.ceil(Math.random() * 10);
    	var b = Math.ceil(Math.random() * 10);       
    	var c = a + b
        document.write("What is "+ a + " + " + b +"? ");
        document.write("<input id='BotBootInput' type='text' maxlength='2' size='2'/>");
    }    
   function ValidBotBoot(){
        var d = document.getElementById('BotBootInput').value;
        if (d == c) return true;        
        return false;
        
    }

*/

var patron = new Array(2,2,4)
function Validar_fecha(elem,separador,pat,numerico) {
    if(elem.valoranterior != elem.value) { 
valor = elem.value;
largo = valor.length;
valor = valor.split(separador);
valor2 = "";
 
    for(i=0; i<valor.length; i++) {
        valor2 += valor[i]; 
    }
 
    if(numerico){
        for(j=0; j<valor2.length; j++){
            if(isNaN(valor2.charAt(j))){
                letra = new RegExp(valor2.charAt(j),"g");
                valor2 = valor2.replace(letra,"");
            }
        }
    }
 
valor = "";
valor3 = new Array();
    for(n=0; n<pat.length; n++) {
        valor3[n] = valor2.substring(0,pat[n]);
        valor2 = valor2.substr(pat[n]);
    }
 
    for(q=0; q<valor3.length; q++) {
        if(q == 0) {
            valor = valor3[q];
        }else{
            if(valor3[q] != "") {
                if (valor3[1] > 12 ) {
                    valor = valor3[2];
                } else if (valor3[0] > 31) {
                    valor = valor3[1] + separador + valor3[2];
                }else{
                    valor += separador + valor3[q];
                }
 
            }
        }
    }
 
    elem.value = valor;
    elem.valoranterior = valor;
    }
}

function checkFields(objTextoStrRut, objTextoRut) {
// Validación de todos los elementos del formulario
rut = rut;
if( rut == 0 ){
alert( "El rut está vacío, ingrese uno, por favor");
}
else {
var rut = objTextoStrRut.value;
var tmpstr = "";
for ( i=0; i < rut.length ; i++ )
if ( rut.charAt(i) != ' ' && rut.charAt(i) != '.' && rut.charAt(i) != '-' )
tmpstr = tmpstr + rut.charAt(i);
rut = tmpstr;
if ( validaRut(objTextoStrRut) ) {


return true;
}
else {
return false;
}
}
}

function checkDV(crut) {
largo = crut.length;

if ( largo > 2 )
rut = crut.substring(0, largo - 1);
else
rut = crut.charAt(0);
dv = crut.charAt(largo-1);

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() )
return false;
return true;
}

function validaRut(objTextoStrRut) {
texto = objTextoStrRut.value;
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("Debe ingresar el rut completo");

objTextoStrRut.focus();
objTextoStrRut.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" ) {


objTextoStrRut.select();
alert("El valor ingresado no corresponde a un rut válido");
objTextoStrRut.focus();
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++) {
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);

objTextoStrRut.value = invertido;

if ( checkDV(texto) ) {
return true;
}
else {

objTextoStrRut.select();
alert("El rut es incorrecto");
objTextoStrRut.focus();
return false;
}
}

function muestra_oculta(id){
	if (document.getElementById){ //se obtiene el id
			var el = document.getElementById(id); //se define la variable "el" igual a nuestro div
			el.style.display = (el.style.display == 'none') ? 'block' : 'none'; //damos un atributo display:none que oculta el div
			}
}
window.onload = function()
{/*hace que se cargue la función lo que predetermina que div estará oculto hasta llamar a la función nuevamente*/
//	muestra_oculta('contenido_a_mostrar');/* "contenido_a_mostrar" es el nombre que le dimos al DIV */
	}
	
	
	
	

function MostrarOcultar(parr)
{
	var np = 'p' + parr; /*numero de parrafo*/
	var na = 'a' + parr; /*numero de enlace*/

	var elem = document.getElementById(np);
	var enlace = document.getElementById(na);

	if (elem.style.display=='block')
	{
	/*si esta visible lo oculta y cambia el texto del enlace*/
		elem.style.display = 'none';
		enlace.title = 'Ampliar información';
	}
	else{
	/*si esta oculto lo muestra y cambia el texto del enlace*/
		elem.style.display = 'block';
		enlace.title = 'Ocultar información';}
	return;
}

function validarsimu()
{
	
	if ((document.frmsimu.rut.value== '' ) || (document.frmsimu.monto.value == '') || (document.frmsimu.meses.value == ''))
	 {
			alert('Rut, Monto Solicitado, y Plazo en Meses, no pueden estar vacios');
		   //document.frmsug.nombre.focus();
		   return false;
	 }
	
	if (document.frmsimu.tipo_cred.value == 0)
	{
		   alert('Debes escoger una opcion distinta a Seleccionar ');
		   //document.frmsug.mensaje.focus();
		   return false;
	}
	document.frmsimu.submit();
}


