 BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
$APPLDIR/sh/order $1 |
awk -F"^" ' BEGIN {
TH["03"]=2
TH["26"]=2
TH["06"]=6
TH["07"]=3
TH["01"]=2
TD["37"]=2
NH=31
ND=7
}

function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
FILENAME==ARGV[1] && NR==1 { for (i=1;i<=NH;i++) reg[sprintf("%.2d",i-1)]=$i }
FILENAME==ARGV[1] { for (i=NH+1;i<=NF;i++) doc[sprintf("%.2d",NR-1)sprintf("%.2d",i-1-NH)]=$i
next }
/orderD/ {
m0=$0
m1=""
for (i=0;i<ND;i++)
{
        x=index(m0,"orderD")
        y=index(substr(m0,2),">")
        re=substr(m0,x+6,4)
        m1=m1""sprintf("%s value=\"%s\"%s",substr(m0,1,y),trail(doc[re]),substr(m0,y+1,1))
        m0=substr(m0,y+2)
}
print m1
next
}
/DOCorder/ { print ; next }
/textarea / {
print 
x=index($0,"orderH")
re=substr($0,x+6,2)
printf trail(reg[re])
next
}
/img name=/ {
if (photo=="") print
else
{
name=substr($0,12,8)
y=index($0,"../Ros")
printf "%s../IMAG/order/%s\">\n",substr($0,1,y-1),photo,photo
}
next
}
/input type=\"hidden/ {
x=index($0,"orderH")
y=index($0,">")
re=substr($0,x+6,2)
if (re=="00") rdonly="readonly"
else rdonly=""
printf "%s value=\"%s\" %s %s\n",substr($0,1,y-1),trail(reg[re]),rdonly,substr($0,y)
photo=trail(reg[re])
next
}
/input type=\"checkbox/ {
x=index($0,"orderH")
y=index($0,">")
re=substr($0,x+6,2)
rdonly=""
if (re=="00") rdonly="readonly"
chkd=""
if (reg[re]+0==1) chkd="checked"
printf "%s value=\"%s\" %s %s %s\n",substr($0,1,y-1),trail(reg[re]),rdonly,chkd,substr($0,y)
next
}
/input type=\"text/ {
x=index($0,"orderH")
y=index($0,">")
re=substr($0,x+6,2)
if (re=="00") rdonly="readonly"
else rdonly=""
printf "%s value=%c%s%c %s %s\n",substr($0,1,y-1),39,trail(reg[re]),39,rdonly,substr($0,y)
next
}
/select name=\"orderH/ { print
x=index($0,"orderH")
indx=substr($0,x+6,2)
re=trail(substr(reg[indx],1,TH[indx]))
while (index($0,"/select") ==0 ) 
{
	getline
	y=index($0,">")
	x=index($0,"=")
	if(re==substr($0,x+2,TH[indx])) printf "%s selected %s\n",substr($0,1,y-1),substr($0,y)
	else
	print
}
next } 
/Consultar/ {
	y=index($0,">")
	printf "%s disabled %s\n",substr($0,1,y-1),substr($0,y)
	next
}
{print}
' - ../order.htm
