function chng(x) {
if(x==0)
{
        x=dv(document.persoform.elements[0].value)
        if(x!= -1)
        {
                if(x==10) x="K"
                alert("DIGITO VERIFICADOR INVALIDO\nDEBERIA SER "+x)
                //alert("RUT INVALIDO")
                document.persoform.elements[0].focus()
                document.persoform.elements[18].disabled = true
        }
        else
	{
		document.persoform.elements[18].disabled = false
		document.persoform.elements[0].value=rut.toUpperCase()
	}
}
}
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"||l=="k")
                {
                        line[j]=l
                        j++
                        line[j]=0
                }
                i++
        }
	rut=""
        for (i=0;i<j;i++) rut=rut+line[i]
	for(i=j;i<9;i++) rut=0+rut
	rut=rut.substr(0,2)+"."+rut.substr(2,3)+"."+rut.substr(5,3)+"-"+rut.substr(8,1)
        dv1=line[j-1]
        if (dv1=='K' || 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)
}
//function exswap()
//{
	//return
//}
function cons(xx) { botonpres=xx.value }
function closeform()
{
        swapin()
        fld=0
        if(botonpres=="Consultar")
        {
                for(i=0;i<nhd+ncd;i++) if( document.persoform.elements[i].value!="") fld=1
                if(fld==0) document.persoform.elements[0].value=""
                return true
        }
	return true
}
function insert(rrow)
{
row=nhd+nrd*ncd+rrow*(ncd+1)
rw=(document.persoform.elements[row].value)-1
if(document.persoform.elements[row+1].value !="")
if(document.persoform.elements[nhd+(nrd-1)*ncd].value=="")
{
	swapin()
	for(i=nrd-1;i>rw;i--) for (j=0;j<ncd;j++)
	document.persoform.elements[nhd+i*ncd+j].value=document.persoform.elements[nhd+i*ncd+j-ncd].value
	for (j=0;j<ncd;j++) document.persoform.elements[nhd+i*ncd+j].value=""
	swapout()
}
else alert ("NO PERMITE NUEVA INSERCION")
else
{
	swapin()
	for(i=rw; i<nrd-1;i++) for (j=0;j<ncd;j++)
	document.persoform.elements[nhd+i*ncd+j].value=document.persoform.elements[nhd+i*ncd+j+ncd].value
	for (j=0;j<ncd;j++)
	document.persoform.elements[nhd+i*ncd+j].value=""
	swapout()
}
}
function clearform()
{
lreg=nhd+nrd*ncd+lin*(ncd+1)
for (i=0;i<lreg;i++)
{
	document.persoform.elements[i].value=""
	if(document.persoform.elements[i].type=="checkbox")
		document.persoform.elements[i].checked=false
}
document.persoform.elements[i++].disabled=false
document.persoform.elements[i++].disabled=true
document.persoform.elements[0].readOnly=false
tp=0
for (j=i;j< document.persoform.elements.length;j++)
if(document.persoform.elements[j].type=="text") document.persoform.elements[j].value=""
swapout()
}
