
//escribir direccion de correo con javascript para evitar spam
function MailLink (usuario, dominio, tld, texto, sl) {

 var arroba = '@'
 var punto = '.'

 var etiqueta = 'ma' + '' + 'il'
 var dospuntos = 'to:'
 var localizador = usuario
 localizador = localizador + arroba + dominio
 localizador = localizador + punto + tld
	if (texto!="")
		 document.write('<a href="' + etiqueta + dospuntos + localizador + '" class="mail">' + texto + '</a>');
	else{
		if (sl=='true')
			document.write('<a href="' + etiqueta + dospuntos + localizador + '" class="mail">' + saltolinea(localizador) + '</a>');	 
		else
			document.write('<a href="' + etiqueta + dospuntos + localizador + '" class="mail">' + localizador + '</a>');	 
	}	
}

function saltolinea(cadena){
	var miString = cadena
	var result = "" 
	
	for (i=0;i<miString.length-1;i++) { 
		if (i%12==0 && i!=0){
			result += miString.charAt(i) 
		    result += "<br>" 
		}    
		else
		    result += miString.charAt(i) 
	
	} 
	result += miString.charAt(miString.length - 1) 
	return(result);
}	

// mostrar-ocultar input file en la administracion para subir imagenes
function mostrar_caja_img(){   
	if (document.getElementById('mod_img').checked)
		document.getElementById('caja_img').style.display = "block";	
	else
		document.getElementById('caja_img').style.display = "none";
}


//mostrar-ocultar formulario para enviar informacion a otra persona
function showhide (id_elto){
	if(document.getElementById(id_elto).style.display != "block")
		document.getElementById(id_elto).style.display = "block";
	else	
		document.getElementById(id_elto).style.display = "none";
}


// quita espacios en blanco al comienzo y al final
function trim( value ) {
	var txt=value;
	var re_der = /((\s*\S+)*)\s*/;
	var re_izq = /\s*((\S+\s*)*)/;
	
	txt=txt.replace(re_der, "$1");
	txt=txt.replace(re_izq, "$1");
	return txt;
	
}


//valida email
function esEmailValido(elemento) {
	var s = elemento;

    var filter=s.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);


	if (s.length == 0 ) 
		return true;
	if (filter)
		return true;
	else
		return false;
}


//valida formulario para enviar info a otra persona
function validar_envio(nombre,correo1,correo2){ 
   
    if (trim(nombre.value).length == 0) {
         alert('Debe ingresar su nombre');
         nombre.focus();
         return false;
    }     
    
    if(trim(correo1.value).length == 0) {
         alert('Debe ingresar su e-mail');
         correo1.focus();
         return false;
    }
	if (!esEmailValido(trim(correo1.value))) {
         alert('Su e-mail debe ser válido');
         correo1.select();
         correo1.focus();
         return false;
    }
    
    if(trim(correo2.value).length == 0) {
         alert('Debe ingresar e-mail destinatario');
         correo2.focus();
         return false;
    }
	if (!esEmailValido(trim(correo2.value))) {
         alert('El e-mail destinatario debe ser válido');
         correo2.select();
         correo2.focus();
         return false;
    }
    
    return true;
}

//valida formulario de curriculum
function validar_curriculum(f) { 
   var er_telefono = /^([0-9\s\+\-])+$/                  //numeros, espacios, + o - 
 
    if (trim(f.nombres.value).length == 0) {
         alert('Debe ingresar Nombres');
         f.nombres.focus();
         return false;
    }     
    if (trim(f.apellidos.value).length == 0) {
         alert('Debe ingresar Apellidos');
         f.apellidos.focus();
         return false;
    }     
    if(trim(f.email.value).length == 0) {
         alert('Debe ingresar un E-mail');
         f.email.focus();
         return false;
    }
	if (!esEmailValido(trim(f.email.value))) {
         alert('El E-mail debe ser válido');
         f.email.select();
         f.email.focus();
         return false;
    }  
    if (trim(f.carrera.value).length == 0) {
         alert('Debe ingresar Carrera');
         f.carrera.focus();
         return false;
    }     

    //comprueba campos de telefonos (permite campos vacios y guiones)
    if(trim(f.telefono.value).length != 0) {
		if( !er_telefono.test(f.telefono.value) ) {
		  alert('Contenido del campo Telefono no válido, ingrese solo numeros, espacios y guión');
		  f.telefono.select();
		  f.telefono.focus();
		  return false
		}
	}  
    return true;
}


//valida formulario de contacto
function validar_contacto(f) { 
   var er_telefono = /^([0-9\s\+\-])+$/                  //numeros, espacios, + o - 
    if (trim(f.titulo.value).length == 0) {
         alert('Debe ingresar un Título');
         f.titulo.focus();
         return false;
    } 
    if (trim(f.comentario.value).length == 0) {
         alert('Debe ingresar un Comentario');
         return false;
    }        
    if (trim(f.nombre.value).length == 0) {
         alert('Debe ingresar un Nombre');
         f.nombre.focus();
         return false;
    }     
    if(trim(f.email.value).length == 0) {
         alert('Debe ingresar un E-mail');
         f.email.focus();
         return false;
    }
	if (!esEmailValido(trim(f.email.value))) {
         alert('El E-mail debe ser válido');
         f.email.select();
         f.email.focus();
         return false;
    }  
    //comprueba campos de telefonos (permite campos vacios y guiones)
    if(trim(f.telefono.value).length != 0) {
		if( !er_telefono.test(f.telefono.value) ) {
		  alert('Contenido del campo Telefono no válido, ingrese solo numeros, espacios y guión');
		  f.telefono.select();
		  f.telefono.focus();
		  return false
		}
	}  
    if(trim(f.fax.value).length != 0) {
		if( !er_telefono.test(f.fax.value) ) {
		  alert('Contenido del campo Fax no válido, ingrese solo numeros, espacios y guión');
		  f.fax.select();
		  f.fax.focus();
		  return false
		}
	} 	    
    return true;
}


//valida formulario de registro
function validar_registro(f) { 
   var er_telefono = /^([0-9\s\+\-])+$/                  //numeros, espacios, + o - 
   
    if(trim(f.rut.value).length == 0) {
         alert('Debe ingresar Rut');
         f.rut.focus();
         return false;
    }
    if (!Rut(f.rut.value)){
    	return false;
    }
    if (trim(f.password.value).length < 6) {
         alert('Debe ingresar una Clave de a lo menos 6 caracteres');
         f.password.select();
         f.password.focus();
         return false;
    } 
    if (trim(f.confirma_pwd.value).length < 6) {
         alert('Debe ingresar la confirmación de su Clave');
         f.confirma_pwd.select();
         f.confirma_pwd.focus();
         return false;
    }
    if (trim(f.password.value)!=trim(f.confirma_pwd.value)) {
         alert('La clave ingresada y la confirmacion de su clave no coinciden');
         f.password.focus();
         return false;
    }   
    if(trim(f.nombres.value).length == 0) {
         alert('Debe ingresar su Nombre');
         f.nombres.focus();
         return false;
    }
    if(trim(f.ap_paterno.value).length == 0) {
         alert('Debe ingresar su Apellido Paterno');
         f.ap_paterno.focus();
         return false;
    }
    if(trim(f.ap_materno.value).length == 0) {
         alert('Debe ingresar su Apellido Materno');
         f.ap_materno.focus();
         return false;
    }    
    //comprueba campos de telefonos (permite campos vacios y guiones)
    if(trim(f.telefono.value).length != 0) {
		if( !er_telefono.test(f.telefono.value) ) {
		  alert('Contenido del campo Telefono no válido, ingrese solo numeros, espacios y guión');
		  f.telefono.select();
		  f.telefono.focus();
		  return false
		}
	}  
    if(trim(f.fax.value).length != 0) {
		if( !er_telefono.test(f.fax.value) ) {
		  alert('Contenido del campo Fax no válido, ingrese solo numeros, espacios y guión');
		  f.fax.select();
		  f.fax.focus();
		  return false
		}
	}     
    if(trim(f.email.value).length == 0) {
         alert('Debe ingresar su E-mail ');
         f.email.focus();
         return false;
    }
	if (!esEmailValido(trim(f.email.value))) {
         alert('Su E-mail debe ser válido');
         f.email.select();
         f.email.focus();
         return false;
    }                   	    
    return true;
}


// validar 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("Debe ingresar un digito verificador valido");		
		document.frm_noticia.rut.focus();		
		document.frm_noticia.rut.select();		
		return false;	
	}	
	return true;
}

function revisarDigito2( crut )
{	
	largo = crut.length;	
	if ( largo < 2 )	
	{		
		alert("Debe ingresar el rut completo")		
		document.frm_noticia.rut.focus();		
		document.frm_noticia.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("EL rut es incorrecto")		
		document.frm_noticia.rut.focus();		
		document.frm_noticia.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("Debe ingresar el rut completo")		
		document.frm_noticia.rut.focus();		
		document.frm_noticia.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("El valor ingresado no corresponde a un R.U.T valido");			
			document.frm_noticia.rut.focus();			
			document.frm_noticia.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);	

	document.frm_noticia.rut.value = invertido.toUpperCase()		

	if ( revisarDigito2(texto) )		
		return true;	

	return false;
}

/*function showhide(capa)
{	var b = window.document.getElementsByName(capa);
	for (x = 0; x < b.length; x++)
	{	if (b[x].style.display == 'none')
		{	b[x].style.display = 'block';	
			
			b[x].style.opacity = '0.2'; 
			b[x].style.-moz-opacity = '0.2'; 
			b[x].style.filter = 'alpha(opacity=20)';
			
		} 
		else
		{	b[x].style.display = 'none';	}
	}
}*/
