function chng(x) {
if(x==1) { lreg=nhd+nrd*ncd+lin*(ncd+1)+1; document.prodsform.elements[lreg].disabled=false }
}
//function exswap()
//{
	//return
//}
function cons(xx) { botonpres=xx.value }
function closeform()
{
swapin()
	//if( document.prodsform.elements[0].value=="")
        if(botonpres!="Consultar")
	{
		r3=document.prodsform.elements[3]
		r4=document.prodsform.elements[4]
		r5=document.prodsform.elements[5]
		r0=r3.options[r3.selectedIndex].text
		r1=r4.options[r4.selectedIndex].text
		r2=r5.options[r5.selectedIndex].text
		r6=document.prodsform.elements[6].value
		r7=document.prodsform.elements[7].value
		r8=document.prodsform.elements[8].value
		document.prodsform.elements[1].value= r0+"-"+r1+"-"+r2+"-"+r6+"x"+r7+"("+r8+")"
		return true
	}
        swapin()
        fld=0
        if(botonpres=="Consultar")
        {
                for(i=0;i<nhd+ncd;i++) if( document.prodsform.elements[i].value!="") fld=1
                if(fld==0) document.prodsform.elements[0].value=""
                return true
        }
	return true
}
function dup() { lreg=nhd+nrd*ncd+lin*(ncd+1)+2; document.prodsform.elements[lreg].disabled=true; document.prodsform.elements[0].value="" }
function insert(rrow)
{
row=nhd+nrd*ncd+rrow*(ncd+1)
rw=(document.prodsform.elements[row].value)-1
if(document.prodsform.elements[row+1].value !="")
if(document.prodsform.elements[nhd+(nrd-1)*ncd].value=="")
{
	swapin()
	for(i=nrd-1;i>rw;i--) for (j=0;j<ncd;j++)
	document.prodsform.elements[nhd+i*ncd+j].value=document.prodsform.elements[nhd+i*ncd+j-ncd].value
	for (j=0;j<ncd;j++) document.prodsform.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.prodsform.elements[nhd+i*ncd+j].value=document.prodsform.elements[nhd+i*ncd+j+ncd].value
	for (j=0;j<ncd;j++)
	document.prodsform.elements[nhd+i*ncd+j].value=""
	swapout()
}
}
function clearform()
{
lreg=nhd+nrd*ncd+lin*(ncd+1)
for (i=0;i<lreg;i++)
{
	document.prodsform.elements[i].value=""
	if(document.prodsform.elements[i].type=="checkbox")
		document.prodsform.elements[i].checked=false
}
document.prodsform.elements[i++].disabled=false
document.prodsform.elements[i++].disabled=true
document.prodsform.elements[0].readOnly=false
tp=0
for (j=i;j< document.prodsform.elements.length;j++)
if(document.prodsform.elements[j].type=="text") document.prodsform.elements[j].value=""
swapout()
}
function recop(rrow)
{
        fld=nhd+nrd*ncd+rrow*(ncd+1)+1
        var e=document.prodsform.elements[fld]
	if(e.value!="") 
	{	
		document.prodsform.elements[fld+1].value=unid[e.value]
		document.prodsform.elements[fld+2].value=cost[e.value]
		if (unid[e.value]=="cm&sup2;")
		{
		document.prodsform.elements[fld+3].value= document.prodsform.elements[6].value*document.prodsform.elements[7].value
		tot1()
		}
		else
		document.prodsform.elements[fld+3].value=""
	}
	else
	{	
		document.prodsform.elements[fld+1].value=""
		document.prodsform.elements[fld+2].value=""
		document.prodsform.elements[fld+3].value=""
	}
	document.prodsform.elements[fld+4].value=""
	document.prodsform.elements[fld+5].value=""
	document.prodsform.elements[fld+6].value=""
	document.prodsform.elements[fld+7].value=""
}

function tot1(x)
{
	totl()
                row=nhd+ncd*x
                document.prodsform.elements[row+5].focus()
}
function tot2(x)
{
	totl()
                row=nhd+ncd*x
                document.prodsform.elements[row+6].focus()
}
function tot3(x)
{
	totl()
                row=nhd+ncd*x
                document.prodsform.elements[row+7].focus()
}
function totl(x)
{
        swapin()
        sumadb=0
        sumacr=0
        for(i=0;i<nrd;i++)
        {

                row=nhd+ncd*i
                if(document.prodsform.elements[row].value!="")
                {
                        document.prodsform.elements[row+4].value= (document.prodsform.elements[row+3].value/1)*(document.prodsform.elements[row+2].value/1)
                        document.prodsform.elements[row+7].value=punt1((document.prodsform.elements[row+4].value/1)+(document.prodsform.elements[row+4].value/1)*((document.prodsform.elements[row+5].value/1)+(document.prodsform.elements[row+6].value/1))/100)
                        sumadb +=(document.prodsform.elements[row+4].value/1)
                        //if (i==0) alert(document.prodsform.elements[row+3].value + " " +document.prodsform.elements[row+2].value+ " " +document.prodsform.elements[row+4].value)
                       sumacr +=(document.prodsform.elements[row+7].value/1)
                }
        }
	swapout()
        document.prodsform.NETO.value=punt0(sumadb)
        document.prodsform.elements[9].value=punt1(sumadb)
	sumac=punt1(sumacr/100)*100
	
        document.prodsform.elements[11].value=sumac
        marg=100*(sumacr/1 - sumadb/1)/sumadb
        document.prodsform.DELIVERY.value=punt0(sumacr-sumadb)
        document.prodsform.TAXES.value= punt(marg/1)+"%"
        document.prodsform.elements[10].value=punt(marg/1)
        document.prodsform.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])
}
lreg=nhd+nrd*ncd+lin*(ncd+1)+2;
if(document.prodsform.elements[0].value=="") document.prodsform.elements[lreg].disabled=true
else totl(0)

