function KeyIsNrut(evt)

{

	var isNav = (navigator.appName.indexOf("Netscape") != -1);

	var isIE = (navigator.appName.indexOf("Microsoft") != -1);



	if (isNav)

	{

		if (evt.which == 13 || evt.which == 8 || (evt.which >= 48 && evt.which <= 57) )

			return true;

		return false;

	}

	else

		if (isIE)

		{

			evt = window.event;

			if (evt.keyCode == 13 || evt.keyCode == 8 || (evt.keyCode >= 48 && evt.keyCode <= 57) )

				return true;

			return false;

		}

		else

		{

			alert("Error: Su Navegador no es soportado por esta aplicación.");

			return false;			

		}

}



function KeyIsDVrut(evt)

{

	var isNav = (navigator.appName.indexOf("Netscape") != -1);

	var isIE = (navigator.appName.indexOf("Microsoft") != -1);



	if (isNav)

	{

		if (evt.which == 13 || evt.which == 75 || evt.which == 107 || evt.which == 8 || (evt.which >= 48 && evt.which <= 57) )

			return true;

		return false;

	}

	else

		if (isIE)

		{

			evt = window.event;

			if (evt.keyCode == 13 || evt.keyCode == 75 || evt.keyCode == 107 || evt.keyCode == 8 || (evt.keyCode >= 48 && evt.keyCode <= 57) )

				return true;

			return false;

		}

		else

		{

			alert("Error: Su Navegador no es soportado por esta aplicación.");

			return false;			

		}

}



function checkFilledFields()

{

	//saca el focus para estabilizar los datos.

    document.f.rut.blur();

    document.f.dgv.blur();

    if ( (document.f.dgv.value == "") || (document.f.rut.value == "") )

	{

		return false;

	}

    else

    {

    	return true;

    }

}

  

//Valida largo de un string.

function testlargo(texto,largomin,largomax)

{

	if ((texto.length < largomin) || (texto.length > largomax))

	{

      //alert("El rut debe tener entre 3 y 9 digitos.");

      return false;

    }

    else

    {

    	return true;

    }

}





//Chequea que los campos contengan datos válidos.

function checkDataFields()

{

	if ( testlargo(document.f.rut.value,3,9) )

  {

		return true;

	}

  	else

  	{

  		return false

  	}

}



//Chequea el dígito verificador.

function checkDV()

{

	rut = document.f.rut.value;

  	dgv = document.f.dgv.value;



  	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 != dgv.toLowerCase() )

    {

    	return false;

    }

  	else

   	{

		return true;

	}

}





//Chequea todo.

function checkAll()

{

	if ( !checkFilledFields() )

	{

    	alert("Complete todos los datos por favor");

    	return false;

    }

  	if ( !checkDataFields() )

  	{

    	alert("Lo sentimos,los datos ingresados no son correctos");

    	document.f.rut.value="";

    	document.f.dgv.value="";

    	return false;

    }

  	if ( !checkDV() )

  	{

    	alert("Su RUT no es válido. Ingrese correctamente su RUT, por favor.")

    	document.f.rut.value="";

    	document.f.dgv.value="";

    	return false;

    }



  	return true;

}
