function separaMiles(num)
{
	num = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1.');
	num = num.split('').reverse().join('').replace(/^[\.]/,'');
	return num;
}



function sacaMiles(num)
{
	num=num.replace(".","");
	num=num.replace(".","");
	num=num.replace(".","");
	return num;
}


function validaRut (objetoRut)
{
	// valida el rut
	// Primer paso: Obtener el rut que ingreso el usuario
    var rutCompleto = objetoRut.value;
    // Eliminamos los caracteres raros, espacios, puntos, guiones.
    // Pasamos a minusculas, y separamos el rut y el digito verificador
    rutCompleto = rutCompleto.replace(/[ \.-]/g, "" );
    rutCompleto = rutCompleto.toLowerCase();


    var dv = rutCompleto.substring(rutCompleto.length - 1);
    var rut = rutCompleto.substring(0, rutCompleto.length - 1)


    var valido = true;
    // Primero comprobamos que el dv sea o un digito o una k
    valido = valido && dv.match(/^[\dk]$/);
    // luego vemos que el rut solo contenga digitos
    valido = valido && rut.match(/^\d+$/);
    // y por ultimo aplicamos la regla de calculo del DV
    valido = valido && verificarDV(rut, dv);

    if (valido){
        objetoRut.value = rut + "-" + dv;
        return true;
    }
	else
		return false;
}

// funcion digito verificador
function verificarDV(rut, dv)
{
    var multiplicador = 9;
    var aux = rut;
    var suma = 0;
    while(aux > 0){
        var unidades = aux % 10 ;
        aux = (aux - unidades) / 10;
        suma += (multiplicador * unidades);
        multiplicador--;
        if (multiplicador < 4){
            multiplicador = 9;
        }
    }
    digito = suma % 11;
    if (digito == 10){
        digito = "k";
    }
    if (dv == digito){
        return true;
    }
    else {
        return false;
    }
}
function exportaExcel(n)
 {
 	var oExcel = new ActiveXObject("Excel.Application");
	var oBook = oExcel.Workbooks.Add;
 	var oSheet = oBook.Worksheets(1);
 	for (var y=0;y<n.rows.length;y++)

 {
 for (var x=0;x<n.rows(y).cells.length;x++){
 oSheet.Cells(y+1,x+1) =n.rows(y).cells(x).innerText;
 }
 }
  oExcel.Visible = true;
 oExcel.UserControl = true;
 }

function validaLetras(obj)
{
	letras="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	texto=obj.value;
	
	for (i = 0; i < texto.length; i++) 
	{
		marcaEsta=false;
		ch = texto.charAt(i);
		for (j = 0; j < letras.length; j++)
		{
			if (ch == letras.charAt(j))
			{
				marcaEsta=true;
			}
		}
		if (!marcaEsta) 
		{
			return false;
		}
	}
	return true;
}

