if( document.prodsform.elements[0].value=="")
{
	document.prodsform.elements[9].readOnly = false
	document.prodsform.elements[10].readOnly = false
	document.prodsform.elements[11].readOnly = false
}
function chng(x) {
if(x==1) { lreg=nhd+nrd*ncd+lin*(ncd+1)+1; document.prodsform.elements[lreg].disabled=false }
if(x==7) { if (document.prodsform.elements[x].value/1>0 && document.prodsform.elements[x-1].value/1>0) factor(1) }
if(x==6) { if (document.prodsform.elements[x].value/1>0 && document.prodsform.elements[x+1].value/1>0) factor(1) }
}
//function exswap()
//{
	//return
//}
function cons(xx) { botonpres=xx.value }
function closeform()
{
swapin()
        if(botonpres!="Consultar")
	{
		if( document.prodsform.elements[1].value=="")
		{
			document.prodsform.elements[12].value="00"
			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
			while(r6.length < 3) r6=" "+r6
			while(r7.length < 3) r7=" "+r7
			//document.prodsform.elements[1].value= r0+"-"+r1+"-"+r2+"-"+r6+"x"+r7+"("+r8+")"
			document.prodsform.elements[1].value= r0+"-"+r1+"-"+r2+"-"+r6+"x"+r7
		}
		if(document.prodsform.elements[12].value=="") { alert("Definir Status"); return false } 
		if( document.prodsform.elements[0].value=="") document.prodsform.elements[14].value=today("")
		return true
	}
        if(botonpres=="Consultar")
        {
		fld=0
                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
        //fld=nhd+nrd*ncd
        var e=document.prodsform.elements[fld]
	und=unid[e.value]
	if(e.value!="") 
	{	
		document.prodsform.elements[fld+1].value=unid[e.value]
		document.prodsform.elements[fld+2].value=cost[e.value]
		if(document.prodsform.elements[fld+5].value=="")
		{
			document.prodsform.elements[fld+5].value=10
			document.prodsform.elements[fld+6].value=100
		}
		//alert (und.substr(0,2) + "cm"+  und.length)
		if (und.substr(0,2)=="cm" && und.length==3)
		document.prodsform.elements[fld+3].value= document.prodsform.elements[6].value*document.prodsform.elements[7].value
	//	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+5].value=""
		document.prodsform.elements[fld+6].value=""
	}
	document.prodsform.elements[fld+4].value=""
	document.prodsform.elements[fld+7].value=""
	tot1(rrow)
}

function tot1(x)
{
                row=nhd+ncd*x
	totl()
                document.prodsform.elements[row+5].focus()
}
function tot2(x)
{
                row=nhd+ncd*x
	totl()
                document.prodsform.elements[row+6].focus()
}
function tot3(x)
{
                row=nhd+ncd*x
	totl()
                document.prodsform.elements[row+7].focus()
}
function factor(x)
{
        for(i=0;i<nrd;i++)
        {
                row=nhd+ncd*i
		var e=document.prodsform.elements[row]
		if (e.value!="")
		{
			 und=unid[e.value]
			if (und.substr(0,2)=="cm" && und.length==3) document.prodsform.elements[row+3].value= document.prodsform.elements[6].value*document.prodsform.elements[7].value
		}
		//else
		//document.prodsform.elements[row+3].value=""
	}
	swapout()
	totl(1)
}
function totl(x)
{
        swapin()
        sumadb=0
        sumacr=0
        for(i=0;i<nrd;i++)
        {
                row=nhd+ncd*i
                if(document.prodsform.elements[row].value!="")
                {
                        fact1=document.prodsform.elements[row+2]
                        fact2=document.prodsform.elements[row+3]
			//alert(fact1.value + "--->" +fact2.value)
                        document.prodsform.elements[row+4].value=punt1(fact1.value*fact2.value)
                        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=0
        if(sumadb) 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
	//alert("h en punt1 es:"+h)
        h=h + "."
        sp=h.split(".")
        sp[1]=sp[1].substr(0,2)
	//alert("h="+ h+ " sp="+sp)
        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)
if( document.prodsform.elements[0].value!="")
{
        for(i=0;i<nrd;i++)
        {
                row=nhd+ncd*i
		fff=document.prodsform.elements[row]
                if(fff.value!="")
                {
			//alert(document.prodsform.elements[row+1].value)
			//alert(document.prodsform.elements[row+2].value)
			document.prodsform.elements[row+1].value=unid[fff.value]
			document.prodsform.elements[row+2].value=cost[fff.value]
			//alert(fff.value)
                        fact1=document.prodsform.elements[row+2]
                        fact2=document.prodsform.elements[row+3]
			//alert(fact1.name + "--->" +fact1.value)
			//alert(fact2.name + "--->" +fact2.value)
                        //document.prodsform.elements[row+4].value=punt1(fact1.value*fact2.value)
                        //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()
	totl(0)
}
