function chng(x) {
if(x==0) valact()
if(x==7) { 
		document.cotizform.elements[8].value=cdx[document.cotizform.elements[7].value]
}
if(x==4) {
		document.cotizform.elements[5].value=""
		i=document.cotizform.elements[x].value
		if(obra[i]!=undefined)
		{
			b=obra[i]
			a=b.split("|")
			document.cotizform.elements[5].value=a[1]
			r=document.cotizform.elements[2].value
			document.cotizform.elements[6].value=i
		}
		
}
if(x==2) {
		document.cotizform.elements[5].value=""
		document.cotizform.elements[6].value=""
		document.cotizform.elements[3].value=""
		i=document.cotizform.elements[x].value
		if(rut[i]!=undefined) document.cotizform.elements[3].value=rut[i]
		r=document.cotizform.elements[4]
		for (j=r.length;j>1;j--) r.remove(j)
		//alert("ST:"+i+":l="+r.length)
		for (j in obra) if(j.indexOf(i)>=0)
		{
		//alert(j+ "=" + i + "="+ j.indexOf(i))
			b=obra[j]
			a=b.split("|")
			var option = document.createElement("option")
			option.value=j
			option.text=a[0]
			r.add(option)
		}
	}
}
//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.cotizform.elements[i].value!="") fld=1
                if(fld==0) document.cotizform.elements[0].value=""
                return true
        }
        if(botonpres=="Actualizar"||botonpres=="Print")
	{
		document.cotizform.elements[3].disabled=false
		document.cotizform.elements[5].disabled=false
	}
	return true
}
function insert(rrow)
{
row=nhd+nrd*ncd+rrow*(ncd+1)
rw=(document.cotizform.elements[row].value)-1
if(document.cotizform.elements[row+1].value !="")
if(document.cotizform.elements[nhd+(nrd-1)*ncd].value=="")
{
	swapin()
	for(i=nrd-1;i>rw;i--) for (j=0;j<ncd;j++)
	document.cotizform.elements[nhd+i*ncd+j].value=document.cotizform.elements[nhd+i*ncd+j-ncd].value
	for (j=0;j<ncd;j++) document.cotizform.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.cotizform.elements[nhd+i*ncd+j].value=document.cotizform.elements[nhd+i*ncd+j+ncd].value
	for (j=0;j<ncd;j++)
	document.cotizform.elements[nhd+i*ncd+j].value=""
	swapout()
}
}
function clearform()
{
lreg=nhd+nrd*ncd+lin*(ncd+1)
for (i=0;i<lreg;i++)
{
	document.cotizform.elements[i].value=""
	if(document.cotizform.elements[i].type=="checkbox")
		document.cotizform.elements[i].checked=false
}
document.cotizform.elements[i++].disabled=false
document.cotizform.elements[i++].disabled=true
document.cotizform.elements[0].readOnly=false
tp=0
for (j=i;j< document.cotizform.elements.length;j++)
if(document.cotizform.elements[j].type=="text") document.cotizform.elements[j].value=""
swapout()
}
function recop(rrow)
{
        fld=nhd+nrd*ncd+rrow*(ncd+1)+1
        var e=document.cotizform.elements[fld]
        if(e.value!="")
        {
                document.cotizform.elements[fld+1].value=descr[e.value]
                document.cotizform.elements[fld+4].value=pcost[e.value]
                document.cotizform.elements[fld+6].value=plist[e.value]
                document.cotizform.elements[fld+7].value=plist[e.value]
                document.cotizform.elements[fld+2].value=""
                document.cotizform.elements[fld+3].value=""
		document.cotizform.elements[fld+5].value=""
		document.cotizform.elements[fld+8].value=""
        }
        else
        {
                document.cotizform.elements[fld+1].value=""
                document.cotizform.elements[fld+2].value=""
                document.cotizform.elements[fld+3].value=""
                document.cotizform.elements[fld+4].value=""
                document.cotizform.elements[fld+5].value=""
                document.cotizform.elements[fld+6].value=""
                document.cotizform.elements[fld+7].value=""
                document.cotizform.elements[fld+8].value=""
        }
	totl()
}
function tot3(x)
{
	totl()
                row=nhd+ncd*x
                document.cotizform.elements[row+7].focus()
}
function totl(x)
{
        swapin()
        sumadb=0
        sumacr=0
        for(i=0;i<nrd;i++)
        {

                row=nhd+ncd*i
                if(document.cotizform.elements[row/1+1].value!="")
                {
                        document.cotizform.elements[row/1+8].value= (document.cotizform.elements[row/1+7].value/1)*(document.cotizform.elements[row+5].value/1)
                        //alert(document.cotizform.elements[row+8].value)
                        //document.cotizform.elements[row+9].value=punt1((document.cotizform.elements[row+6].value/1)+(document.cotizform.elements[row+6].value/1)*((document.cotizform.elements[row+7].value/1)+(document.cotizform.elements[row+8].value/1))/100)
                        sumadb +=(document.cotizform.elements[row+5].value/1)*(document.cotizform.elements[row/1+4].value/1)
                        //if (i==0) alert(document.cotizform.elements[row+5].value + " " +document.cotizform.elements[row+4].value+ " " +document.cotizform.elements[row+6].value)
                       sumacr +=(document.cotizform.elements[row/1+8].value/1)
                }
        }
	swapout()
        document.cotizform.NETO.value=punt0(sumadb)
        document.cotizform.elements[10].value=punt1(sumacr)
	sumac=punt1(sumacr*.19)
	
        document.cotizform.elements[12].value=punt1(sumacr/1+sumac/1)
        marg=100*(sumacr/1 - sumadb/1)/sumadb
        document.cotizform.DELIVERY.value=punt0(sumacr-sumadb)
        document.cotizform.TAXES.value= punt(marg/1)+"%"
        document.cotizform.elements[11].value=punt1(sumac/1)
        document.cotizform.TOTAL.value=punt0(sumacr/1)
}

function punt1(h)
{
        sig=""
        if (h/1 <0) { sig="-" ;h *= -1 }
        h = h/1 + .5
        h=h + ""
        sp=h.split(".")
        return(sig+sp[0])
}
function punto(h)
{
        sig=""
        if (h/1 <0) { sig="-" ;h *= -1 }
        h = h/1 + .005
        h=h + ""
        sp=h.split(".")
        if(sp[1]==undefined) sp[1]="00"
        sp[1]=sp[1].substr(0,2)
        return(sig+sp[0]+"."+sp[1])
}

function punt0(h)
{
        sig=""
        if (h/1 <0) { sig="-" ;h *= -1 }
        h = h/1 + .005
        h=h + ""
        sp=h.split(".")
        if(sp[1]==undefined) sp[1]="00"
        sp[1]=sp[1].substr(0,2)
        zz=sp[0].toString()
        r=zz.length
        xx=""
        for (i=r;i>3;i -=3) xx= "," + zz.substr(i-3,3)+ xx
        if(r<4) xx= zz.substr(0,r)
        else xx= zz.substr(0,i)+ xx
        return(sig+xx)
}
function punt(h)
{
        sig=""
        if (h/1 <0) { sig="-" ;h *= -1 }
        h = h/1 + .005
        h=h + ""
        sp=h.split(".")
        if(sp[1]==undefined) sp[1]="00"
        sp[1]=sp[1].substr(0,2)
        zz=sp[0].toString()
        r=zz.length
        xx=""
        for (i=r;i>3;i -=3) xx= "," + zz.substr(i-3,3)+ xx
        if(r<4) xx= zz.substr(0,r)
        else xx= zz.substr(0,i)+ xx
        return(sig+xx+"."+sp[1])
}

if(document.cotizform.elements[0].value!="")
{
		i=document.cotizform.elements[2].value
		r=document.cotizform.elements[4]
		for (j=0;j<r.length+2;j++) r.remove(2)
		//alert(":"+i+":")
		for (j in obra)
	{
		//alert(j+ "=" + i + "="+ j.indexOf(i))
		if(j.indexOf(i)>=0)
		{
			b=obra[j]
			a=b.split("|")
			var option = document.createElement("option")
			option.value=j
			option.text=a[0]
			r.add(option)
		}
	}
	document.cotizform.elements[4].value=document.cotizform.elements[6].value
	totl()
}
else
{
		r=document.cotizform.elements[4]
		for (j=0;j<r.length+2;j++) r.remove(2)
		for (j in obra)
	{
			b=obra[j]
			a=b.split("|")
			var option = document.createElement("option")
			option.value=j
			option.text=a[0]
			r.add(option)
	}
}
function textad(xxx)
{
	if(xxx <5) ttp=(518+xxx*22)
	else ttp=(218+xxx*22)
        fld=nhd+nrd*ncd+xxx*(ncd+1)+1
	vari="../texta.htm?"+fld
	if(document.cotizform.elements[fld].value!="")
	{
		param="width=550,height=195,top="+ttp+",left=170,menubar=no,titlebar=no,scrollbars=no,resizable=no,mimimizable=no,dialog=no"
		var obj_calwindow = window.open(vari,"",param)
	}
}
document.cotizform.elements[3].disabled=true
document.cotizform.elements[5].disabled=true
