botonpres=1
function chng(x) {
if(x==1)
{
	x=dv(document.clienform.elements[1].value)
        if(x== -1)
	{
		document.clienform.elements[275].disabled = false
		document.clienform.elements[1].value=rut.toUpperCase()
	}
        if(botonpres=="Consultar") return
        if(x!= -1)
        {
         
                //alert("DIGITO VERIFICADOR INVALIDO\nDEBERIA SER "+x)
                if(botonpres!="Consultar") alert("RUT INVALIDO")
                document.clienform.elements[1].select()
                document.clienform.elements[1].focus()
                document.clienform.elements[275].disabled = true
		return -1
        }
        else
        {
                document.clienform.elements[275].disabled = false
                document.clienform.elements[1].value=rut.toUpperCase()
		return 
        }
}
}
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.clienform.elements[i].value!="") fld=1
                if(fld==0) document.clienform.elements[0].value="*"
                return true
        }
	if(chng(1) == -1) return false
	if( document.clienform.elements[0].value=="")
	for(i=1;i<rutc.length;i++) 
	if(document.clienform.elements[1].value== rutc[i])
	{
		alert ("EL RUT "+rutc[i]+" YA EXISTE")
		return false
	}
	return true
}
function insert(rrow)
{
row=nhd+nrd*ncd+rrow*(ncd+1)
rw=(document.clienform.elements[row].value)-1
if(document.clienform.elements[row+1].value !="")
if(document.clienform.elements[nhd+(nrd-1)*ncd].value=="")
{
	swapin()
	for(i=nrd-1;i>rw;i--) for (j=0;j<ncd;j++)
	document.clienform.elements[nhd+i*ncd+j].value=document.clienform.elements[nhd+i*ncd+j-ncd].value
	for (j=0;j<ncd;j++) document.clienform.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.clienform.elements[nhd+i*ncd+j].value=document.clienform.elements[nhd+i*ncd+j+ncd].value
	for (j=0;j<ncd;j++)
	document.clienform.elements[nhd+i*ncd+j].value=""
	swapout()
}
}
function clearform()
{
lreg=nhd+nrd*ncd+lin*(ncd+1)
for (i=0;i<lreg;i++)
{
	document.clienform.elements[i].value=""
	if(document.clienform.elements[i].type=="checkbox")
		document.clienform.elements[i].checked=false
}
document.clienform.elements[i++].disabled=false
document.clienform.elements[i++].disabled=true
document.clienform.elements[0].readOnly=false
tp=0
for (j=i;j< document.clienform.elements.length;j++)
if(document.clienform.elements[j].type=="text") document.clienform.elements[j].value=""
swapout()
}
