//
// Validador de Rut
// Descargado desde http://juque.f2o.org/
//
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.getElementById('rut').focus();		
		document.getElementById('rut').select();		
		return false;	
	}	
	return true;
}

function revisarDigito2( crut )
{	
	largo = crut.length;	
	if ( largo < 2 )	
	{		
		alert("Debe ingresar el rut completo")		
		document.getElementById('rut').focus();		
		document.getElementById('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.getElementById('rut').focus();		
		document.getElementById('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.getElementById('rut').focus();		
		document.getElementById('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.getElementById('rut').focus();			
			document.getElementById('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.getElementById('rut').value = invertido.toUpperCase()		

	if ( revisarDigito2(texto) )		
		return true;	

	return false;
}


//inscripcion programa educativo

function IngresaPrograma()
{
	var formulario;
	formulario = window.document.inscripcion;
		
	if (formulario.nombre.value== '')
	{
		window.alert('Debe ingresar su nombre');
		formulario.nombre.focus();	
		return;
	}
	if (formulario.apellidos.value== '')
	{
		window.alert('Debe ingresar sus apellidos');
		formulario.apellidos.focus();	
		return;
	}
	if (formulario.rut.value== '')
	{
		window.alert('Debe ingresar su rut');
		formulario.rut.focus();	
		return;
	}
	
	if (formulario.profesion.value== '')
	{
		window.alert('Debe ingresar su profesión');
		formulario.profesion.focus();	
		return;
	}
	
	if (formulario.ciudad.value== '')
	{
		window.alert('Debe ingresar su ciudad');
		formulario.ciudad.focus();	
		return;
	}
	if (formulario.comuna.value== '')
	{
		window.alert('Debe ingresar su comuna');
		formulario.comuna.focus();	
		return;
	}
	if (formulario.nombre_institucion.value== '')
	{
		window.alert('Debe ingresar el nombre de la institución donde trabaja');
		formulario.nombre_institucion.focus();	
		return;
	}
	
	if (formulario.direccion.value== '')
	{
		window.alert('Debe ingresar la dirección de la institución donde trabaja');
		formulario.direccion.focus();	
		return;
	}
	if (formulario.telefono.value== '')
	{
		window.alert('Debe ingresar un teléfono');
		formulario.telefono.focus();	
		return;
	}
	
	if (formulario.email.value== '')
	{
		window.alert('Debe ingresar su email');;
		formulario.email.focus();	
		return;
	}
	
	if ((formulario.email.value.indexOf ('@',0) == -1 || formulario.email.value.indexOf ('.',0) == -1) &&  formulario.email.value != "")
 {
		window.alert('El email debe ser válido');
		formulario.email.focus();	
		return;
	}
	if (formulario.email2.value== '')
	{
		window.alert('Debe repetir el correo de contacto');;
		formulario.email2.focus();	
		return;
	}
	
	
	
	if ((formulario.email2.value.indexOf ('@',0) == -1 || formulario.email2.value.indexOf ('.',0) == -1) &&  formulario.email2.value != "")
 {
		window.alert('El email debe ser válido');
		formulario.email2.focus();	
		return;
	}
	
	if (formulario.email2.value!= formulario.email.value)
	{
		window.alert('Los correos no son iguales');;
		formulario.email.focus();	
		return;
	}
	
	
	
	
	
	
	if (formulario.pass.value== '')
	{
		window.alert('Debe ingresar su clave');;
		formulario.pass.focus();	
		return;
	}
	
	
	if (formulario.pass2.value== '')
	{
		window.alert('Debe repetir la clave');;
		formulario.pass2.focus();	
		return;
	}
	
	
	
	
	if (formulario.pass.value!= formulario.pass2.value)
	{
		window.alert('Las claves no son iguales');;
		formulario.pass.focus();	
		return;
	}
	
	
	
	
	
	
	
	
	
	
	
	if (formulario.nombre.value== '')
	{
		window.alert('Debe ingresar su nombre');
		formulario.nombre.focus();	
		return;
	}
	
	
	
		formulario.submit();
	}


