if(document.getElementById("register").value!="")
{
cl=document.getElementById("rutcliente")
pr=document.getElementById("rutproveedor")
if(cl!=null) tx=cl
if(pr!=null) tx=pr
acrut(tx,tx.value)
}
function formatrut(tx)
{
	txt=tx.value
	tx1=""
	tt=txt.length
	while(tt)
	{
		if(txt.substr(0,1)!="." && txt.substr(0,1)!="-" ) { tx1=tx1+txt.substr(0,1) } 
		tt--
		txt=txt.substr(1)
	}
	tx1=tx1.toUpperCase()
	acrut(tx,tx1)
}
function acrut(tx,tx1)
{
lg=tx1.length
switch (lg) {
case 10:
return tx1
case 9:
	tx.value=tx1.substr(0,2)+"."+tx1.substr(2,3)+"."+tx1.substr(5,3)+"-"+tx1.substr(8,1)
break
case 8:
	tx.value=tx1.substr(0,1)+"."+tx1.substr(1,3)+"."+tx1.substr(4,3)+"-"+tx1.substr(7,1)
break
case 7:
	tx.value=tx1.substr(0,1)+"."+tx1.substr(1,3)+"."+tx1.substr(4)
break
case 6:
	tx.value=tx1.substr(0,3)+"."+tx1.substr(3)
break
case 5:
	tx.value=tx1.substr(0,2)+"."+tx1.substr(2)
break
case 4:
	tx.value=tx1.substr(0,1)+"."+tx1.substr(1)
break
case 3:
	tx.value=tx1
break
case 2:
	tx.value=tx1
break
case 1:
	tx.value=tx1
break
}
return
}
function validarut (rut)
{
        x=dv(document.clienform.elements[1].value)
        if(x!= -1)
        {
                if(x==10) x="K"
                alert("DIGITO VERIFICADOR INVALIDO\nDEBERIA SER "+x)
                alert("RUT INVALIDO")
                document.clienform.elements[1].focus()
                document.clienform.elements[275].disabled = true
        }
        else document.clienform.elements[275].disabled = false
}
function dv(str)
{
        var a = new Array(0,2,3,4,5,6,7,2,3,4,5)
        var line = new Array()
        i=0
        j=0
        while(str.substr(i,1)!="")
        {
                l=str.substr(i,1)
                if (!isNaN(l) || l=="K")
                {
                        line[j]=l
                        j++
                        line[j]=0
                }
                i++
        }
        dv1=line[j-1]
        if (dv1=='K') dv1=10
        line[j]=0
        lc=0
        for (i=1;i<j;i++) lc +=line[j-i-1]*a[i]
        dv2 =lc%11
        if (dv2 ) dv2 =11 - dv2
        if (dv1 == dv2) return (-1)
        else return(dv2)
}
