#!/bin/sh

# disable filename globbing
set -f

echo Content-type: text/html
echo
BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
pc=./tmp/pc
activ=./tmp/activ
paci=./tmp/paci

zonas=./tmp/zonas
./appl/sh/zonas  | awk -F"^" '
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
{
zo[$1]=$2"^"trail($3)
}
trail($4)!=""  {oz[$4]=$1}
trail($5)!=""  {oz[$5]=$1}

END {
for (i in zo) print i"^" zo[i]
for (i in oz) print i"^" oz[i] >"tmp/oz"
} ' > $zonas

# |  1 H00 correl 03 15   0 | 2 H01 codigo 03 00   3 | 3 H02 identi 30 00   6 |
# |  4 H03 observ 512 12  36 |

#  5 D00 comuna  5   51 |  6 D01 comuna  5   51 | 
pacie=./tmp/pacie

./appl/sh/pacie | awk -F"^" ' 
BEGIN { x="1^2^5^3^4^7^15^19^21";split(x,y) }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
ant!=$1 {
ant=$1
for (i=1;i<10;i++) printf "%s^",trail($y[i])
print ""
} '  - |
awk -F"^" ' 
FILENAME == ARGV[1] { zo[$1]=$3 ; next }
FILENAME == ARGV[2] { oz[$1]=$2 ; next }
{ a[$9]++ ; rg1++}
substr($9,1,2)=="00"  {
	rg2++
	b[$8]++
	c[$6]++
	z[oz[substr($6,1,5)]]++
	T="tmp/T"zo[oz[substr($6,1,5)]]
	print >T
}
END {
	for (i in a) printf "%s^%s^%5.2f %%\n",substr(i,4),a[i],100*a[i]/rg1 >"tmp/ST"
	for (i in b) printf "%s^%s^%5.2f %%\n",substr(i,4),b[i],100*b[i]/rg2 >"tmp/IS"
	for (i in c) printf "%s^%s^%5.2f %%\n",substr(i,7),c[i],100*c[i]/rg2 >"tmp/CO"
	for (i in z) printf "%s^%s^%5.2f %%\n",zo[i],z[i],100*z[i]/rg2 >"tmp/ZO"
} ' $zonas tmp/oz -

sort tmp/CO -o tmp/CO
sort tmp/IS -o tmp/IS
sort tmp/ST -o tmp/ST
sort tmp/ZO -o tmp/ZO

# |  1 H00 indice 05 15   0 | 2 H01 codigo 13 07   5 | 3 H02 patern 40 00  18 |
# |  4 H03 matern 40 00  58 | 5 H04 nombre 40 00  98 | 6 H05 direcc 50 00 138 |
# |  7 H06 comuna 05 55 188 | 8 H07 ciudad 20 00 193 | 9 H08 sexoss 09 50 213 |
# | 10 H09 nacimt 10 00 222 |11 H10 contac 40 00 232 |12 H11 telefo 40 00 272 |
# | 13 H12 tcelul 40 00 312 |14 H13 correo 50 00 352 |15 H14 actges 10 00 402 |
# | 16 H15 renova 10 00 412 |17 H16 diagno 80 00 422 |18 H17 observ 512 13 502 |
# | 19 H18 isapre 02 51 1014 |20 H19 fechat 10 00 1016 |21 H20 status 02 53 1026 |

# 22 D00 presta  2   54 | 23 D01 modali  2   52 | 24 D02 topes   2   01 | 
# 25 D03 di      2   00 | 26 D04 di      2   00 | 27 D05 di      2   00 | 
# 28 D06 di      2   00 | 29 D07 di      2   00 | 30 D08 di      2   00 | 
# 31 D09 di      2   00 | 32 D10 di      2   00 | 33 D11 di      2   00 | 
# 34 D12 di      2   00 | 35 D13 di      2   00 | 36 D14 di      2   00 | 
# 37 D15 di      2   00 | 38 D16 di      2   00 | 39 D17 di      2   00 | 
# 40 D18 di      2   00 | 41 D19 di      2   00 | 42 D20 di      2   00 | 
# 43 D21 di      2   00 | 44 D22 di      2   00 | 45 D23 di      2   00 | 
# 46 D24 di      2   00 | 47 D25 di      2   00 | 48 D26 di      2   00 | 
# 49 D27 di      2   00 | 50 D28 di      2   00 | 51 D29 di      2   00 | 
# 52 D30 di      2   00 | 53 D31 di      2   00 | 54 D32 di      2   00 | 
# 55 D33 di      2   00 | 56 D34 di      2   00 | 57 D35 di      2   00 | 
# 58 D36 di      2   00 | 59 D37 di      2   00 | 60 D38 di      2   00 | 
# 61 D39 di      2   00 | 62 D40 total   1   01 | 

cat tmp/ST | awk -F"^" ' BEGIN { 
T["01"]="STATUS^LT"
T["02"]="PACIENTES^RN"
T["03"]=" [%] ^RT"
tx=4
	for (i=1;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<tx;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}
printf "\n"
}
END {
	printf "TOTAL^"
	for (i=2;i<tx;i++)
		printf "%s^",tt[i]
print ""
} ' - |

awk -F"^" ' BEGIN {
ttop("Inhedo - RESUMEN DE PACIENTES") ;bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF; i+=2) 
	{
		al[(i+1)/2]=substr($(i+1),1,1)
		tp[(i+1)/2]=substr($(i+1),2)
		labelf($i,al[(i+1)/2])
	}
print "</tr>"
next
}
$1=="TOTAL" {
tp[2]="N"
for (i=1; i<NF; i++) labelf($i,al[i],tp[i])

print "</tr>"
}
$1!="TOTAL" {
for (i=1; i<NF; i++) label($i,al[i],tp[i],tit[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 foot() } 
function select(name,selv,vals)
{
	file="sort -t\"^\" -k2 ./tmp/"name
printf "<td>"
printf "<p><select id=\"S%s\" style=\"font-family: Arial; font-size: 8pt ;border:1px solid #7F9DB9;background-color:#FFFFFF\"  >\n",$1$8,selv
printf "<option value=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"
	while ((file | getline linex) >0)
	{
		split(linex,lin) 
		printf "<option value=\"%s\">%s\n",lin[1],lin[2]
	}
	printf "</select></p>\n"
	close(file)
}
function boton(value,alig,lar,type,tt)
{
        printf "<td bgcolor=\"%s\" id=\"S%s\">\n",bgcolor,$1""$8""i
	printf "<p align=\"center\">"
	if(type=="S") printf "<input type=\"submit\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"agendH00\"></p>\n",lar,alg[alig],value
	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" id=\"K%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8""i
#	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8
print "</td>"
print "</td>"
}
function chkb(value)
{
	printf "<td><p><input type=\"checkbox\" style=\"font-family: Arial; font-size: 8pt\" size=\"%s\" name=\"C%s\"></p></td>\n",1,++line
}
function punto (dat,dec)
{
        if (dat+0==0) return ""
        sig=""
        if(dat<0)
        {
                dat *= -1
                sig="-"
        }
        pun=sprintf("%%.%df",dec)
        da=sprintf (pun,dat)
        if(dec) l=length(da)-dec-1
        else l=length(da)
        dat=substr(da,l+2)
        dato=substr(da,1,l)
	if (l>9) pun=substr(dato,1,l-9)"."substr(dato,l-8,3)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if (l>6) pun=substr(dato,1,l-6)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if(l >3) pun=substr(dato,1,l-3)"."substr(dato,l-2)
        else pun=dato
        if (dec) pun=pun","dat
        return sig""pun
}
function label(value,alig,tip,tt)
{
	printf "<td id=\"P%s%s\" bgcolor=\"%s\"><p ",$1$8,i,bgcolor
	if(tt) printf " onmouseover=\"showdiv(event,%c%s%c);\" onmousemove=\"showdiv(event,%c%s%c);\" onmouseout=\"javascript:document.getElementById(%cflotante%c).style.display=%cnone%c;\" ",39,$1,39,39,$1,39,39,39,39,39
	if(substr(tip,1,1)=="N") if(value <0) printf " align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(-value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
	print "</td>"
}
function labelf(value,alig,tip)
{
if(tip=="T" && value!="TOTAL") value="&nbsp"
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labeld(value,alig,tip)
{
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labels(value,alig)
{
print "<td bordercolor=\"#C0C0C0\" bgcolor=\"#003399\" align=\"center\">"
printf "<p align=\"%s\"><b><font color=\"#FFFFFF\">&nbsp;%s&nbsp;</font></b></p>\n",alg[alig],value
print "</td>"
}
function ttop(value)
{
print "<html>"
print "<head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
print "<meta name=\"GENERATOR\" content=\"Microsoft FrontPage 4.0\">"
print "<meta name=\"ProgId\" content=\"FrontPage.Editor.Document\">"
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"../age1.css\">"
printf "<title>%s</title>\n",value
print "</head>"
print "<center>"
print "<body bgcolor=\"#D0D0DF\">"
#print "<p align=\"center\"><img border=\"0\" src=\"../../logol.jpg\" width=\"200\" height=\"97\"></p>"
print "<form method=\"post\" action=\"../../cgi-bin/agend.cgi\" name=anlsform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
printf "<H3>RESUMEN DE PACIENTES<br>POR STATUS</H3>\n"
printf "<input type=\"hidden\" value=\"Consultar\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
if (1 == 0)
{
print "</table>"
print "<table><tr>"
labels("SALDOS","C")
labels("VALORES","R")
print "</tr>"
print "<tr>"
label("ANTERIOR","L","T")
label(cta[4],"R","N")
print "</tr>"
print "<tr>"
label("DEL PERIODO","l","T")
label(cta[5],"R","N")
print "</tr>"
print "<tr>"
label("ACTUAL","L","T")
label(cta[6],"R","N")
print "</tr>"
}
print "</center>"
print "</form>"
print "<div id=\"flotante\">"
print "        <br /><span id=\"posicion\"></span>"
print "</div>"

print "<script language=\"Javascript\" src=\"../js/agenda.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/age1.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/pac.js\"> </script>"
print "</body>"
print "</html>"
} ' - 
cat tmp/IS | awk -F"^" ' BEGIN { 
T["01"]="ISAPRE^LT"
T["02"]="PACIENTES^RN"
T["03"]=" [%] ^RT"
tx=4
	for (i=1;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<tx;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}
printf "\n"
}
END {
	printf "TOTAL^"
	for (i=2;i<tx;i++)
		printf "%s^",tt[i]
print ""
} ' - |

awk -F"^" ' BEGIN {
ttop("Inhedo - RESUMEN DE PACIENTES") ;bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF; i+=2) 
	{
		al[(i+1)/2]=substr($(i+1),1,1)
		tp[(i+1)/2]=substr($(i+1),2)
		labelf($i,al[(i+1)/2])
	}
print "</tr>"
next
}
$1=="TOTAL" {
tp[2]="N"
for (i=1; i<NF; i++) labelf($i,al[i],tp[i])

print "</tr>"
}
$1!="TOTAL" {
for (i=1; i<NF; i++) label($i,al[i],tp[i],tit[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 foot() } 
function select(name,selv,vals)
{
	file="sort -t\"^\" -k2 ./tmp/"name
printf "<td>"
printf "<p><select id=\"S%s\" style=\"font-family: Arial; font-size: 8pt ;border:1px solid #7F9DB9;background-color:#FFFFFF\"  >\n",$1$8,selv
printf "<option value=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"
	while ((file | getline linex) >0)
	{
		split(linex,lin) 
		printf "<option value=\"%s\">%s\n",lin[1],lin[2]
	}
	printf "</select></p>\n"
	close(file)
}
function boton(value,alig,lar,type,tt)
{
        printf "<td bgcolor=\"%s\" id=\"S%s\">\n",bgcolor,$1""$8""i
	printf "<p align=\"center\">"
	if(type=="S") printf "<input type=\"submit\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"agendH00\"></p>\n",lar,alg[alig],value
	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" id=\"K%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8""i
#	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8
print "</td>"
print "</td>"
}
function chkb(value)
{
	printf "<td><p><input type=\"checkbox\" style=\"font-family: Arial; font-size: 8pt\" size=\"%s\" name=\"C%s\"></p></td>\n",1,++line
}
function punto (dat,dec)
{
        if (dat+0==0) return ""
        sig=""
        if(dat<0)
        {
                dat *= -1
                sig="-"
        }
        pun=sprintf("%%.%df",dec)
        da=sprintf (pun,dat)
        if(dec) l=length(da)-dec-1
        else l=length(da)
        dat=substr(da,l+2)
        dato=substr(da,1,l)
	if (l>9) pun=substr(dato,1,l-9)"."substr(dato,l-8,3)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if (l>6) pun=substr(dato,1,l-6)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if(l >3) pun=substr(dato,1,l-3)"."substr(dato,l-2)
        else pun=dato
        if (dec) pun=pun","dat
        return sig""pun
}
function label(value,alig,tip,tt)
{
	printf "<td id=\"P%s%s\" bgcolor=\"%s\"><p ",$1$8,i,bgcolor
	if(tt) printf " onmouseover=\"showdiv(event,%c%s%c);\" onmousemove=\"showdiv(event,%c%s%c);\" onmouseout=\"javascript:document.getElementById(%cflotante%c).style.display=%cnone%c;\" ",39,$1,39,39,$1,39,39,39,39,39
	if(substr(tip,1,1)=="N") if(value <0) printf " align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(-value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
	print "</td>"
}
function labelf(value,alig,tip)
{
if(tip=="T" && value!="TOTAL") value="&nbsp"
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labeld(value,alig,tip)
{
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labels(value,alig)
{
print "<td bordercolor=\"#C0C0C0\" bgcolor=\"#003399\" align=\"center\">"
printf "<p align=\"%s\"><b><font color=\"#FFFFFF\">&nbsp;%s&nbsp;</font></b></p>\n",alg[alig],value
print "</td>"
}
function ttop(value)
{
print "<html>"
print "<head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
print "<meta name=\"GENERATOR\" content=\"Microsoft FrontPage 4.0\">"
print "<meta name=\"ProgId\" content=\"FrontPage.Editor.Document\">"
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"../age1.css\">"
printf "<title>%s</title>\n",value
print "</head>"
print "<center>"
print "<body bgcolor=\"#D0D0DF\">"
#print "<p align=\"center\"><img border=\"0\" src=\"../../logol.jpg\" width=\"200\" height=\"97\"></p>"
print "<form method=\"post\" action=\"../../cgi-bin/agend.cgi\" name=anlsform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
printf "<H3>RESUMEN DE PACIENTES<br>POR ISAPRE</H3>\n"
printf "<input type=\"hidden\" value=\"Consultar\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
if (1 == 0)
{
print "</table>"
print "<table><tr>"
labels("SALDOS","C")
labels("VALORES","R")
print "</tr>"
print "<tr>"
label("ANTERIOR","L","T")
label(cta[4],"R","N")
print "</tr>"
print "<tr>"
label("DEL PERIODO","l","T")
label(cta[5],"R","N")
print "</tr>"
print "<tr>"
label("ACTUAL","L","T")
label(cta[6],"R","N")
print "</tr>"
}
print "</center>"
print "</form>"
print "<div id=\"flotante\">"
print "        <br /><span id=\"posicion\"></span>"
print "</div>"

print "<script language=\"Javascript\" src=\"../js/agenda.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/age1.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/pac.js\"> </script>"
print "</body>"
print "</html>"
} ' - 

cat tmp/CO | awk -F"^" ' BEGIN { 
T["01"]="COMUNA^LT"
T["02"]="PACIENTES^RN"
T["03"]=" [%] ^RT"
tx=4
	for (i=1;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<tx;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}
printf "\n"
}
END {
	printf "TOTAL^"
	for (i=2;i<tx;i++)
		printf "%s^",tt[i]
print ""
} ' - |

awk -F"^" ' BEGIN {
ttop("Inhedo - RESUMEN DE PACIENTES") ;bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF; i+=2) 
	{
		al[(i+1)/2]=substr($(i+1),1,1)
		tp[(i+1)/2]=substr($(i+1),2)
		labelf($i,al[(i+1)/2])
	}
print "</tr>"
next
}
$1=="TOTAL" {
tp[2]="N"
for (i=1; i<NF; i++) labelf($i,al[i],tp[i])

print "</tr>"
}
$1!="TOTAL" {
for (i=1; i<NF; i++) label($i,al[i],tp[i],tit[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 foot() } 
function select(name,selv,vals)
{
	file="sort -t\"^\" -k2 ./tmp/"name
printf "<td>"
printf "<p><select id=\"S%s\" style=\"font-family: Arial; font-size: 8pt ;border:1px solid #7F9DB9;background-color:#FFFFFF\"  >\n",$1$8,selv
printf "<option value=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"
	while ((file | getline linex) >0)
	{
		split(linex,lin) 
		printf "<option value=\"%s\">%s\n",lin[1],lin[2]
	}
	printf "</select></p>\n"
	close(file)
}
function boton(value,alig,lar,type,tt)
{
        printf "<td bgcolor=\"%s\" id=\"S%s\">\n",bgcolor,$1""$8""i
	printf "<p align=\"center\">"
	if(type=="S") printf "<input type=\"submit\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"agendH00\"></p>\n",lar,alg[alig],value
	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" id=\"K%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8""i
#	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8
print "</td>"
print "</td>"
}
function chkb(value)
{
	printf "<td><p><input type=\"checkbox\" style=\"font-family: Arial; font-size: 8pt\" size=\"%s\" name=\"C%s\"></p></td>\n",1,++line
}
function punto (dat,dec)
{
        if (dat+0==0) return ""
        sig=""
        if(dat<0)
        {
                dat *= -1
                sig="-"
        }
        pun=sprintf("%%.%df",dec)
        da=sprintf (pun,dat)
        if(dec) l=length(da)-dec-1
        else l=length(da)
        dat=substr(da,l+2)
        dato=substr(da,1,l)
	if (l>9) pun=substr(dato,1,l-9)"."substr(dato,l-8,3)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if (l>6) pun=substr(dato,1,l-6)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if(l >3) pun=substr(dato,1,l-3)"."substr(dato,l-2)
        else pun=dato
        if (dec) pun=pun","dat
        return sig""pun
}
function label(value,alig,tip,tt)
{
	printf "<td id=\"P%s%s\" bgcolor=\"%s\"><p ",$1$8,i,bgcolor
	if(tt) printf " onmouseover=\"showdiv(event,%c%s%c);\" onmousemove=\"showdiv(event,%c%s%c);\" onmouseout=\"javascript:document.getElementById(%cflotante%c).style.display=%cnone%c;\" ",39,$1,39,39,$1,39,39,39,39,39
	if(substr(tip,1,1)=="N") if(value <0) printf " align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(-value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
	print "</td>"
}
function labelf(value,alig,tip)
{
if(tip=="T" && value!="TOTAL") value="&nbsp"
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labeld(value,alig,tip)
{
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labels(value,alig)
{
print "<td bordercolor=\"#C0C0C0\" bgcolor=\"#003399\" align=\"center\">"
printf "<p align=\"%s\"><b><font color=\"#FFFFFF\">&nbsp;%s&nbsp;</font></b></p>\n",alg[alig],value
print "</td>"
}
function ttop(value)
{
print "<html>"
print "<head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
print "<meta name=\"GENERATOR\" content=\"Microsoft FrontPage 4.0\">"
print "<meta name=\"ProgId\" content=\"FrontPage.Editor.Document\">"
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"../age1.css\">"
printf "<title>%s</title>\n",value
print "</head>"
print "<center>"
print "<body bgcolor=\"#D0D0DF\">"
#print "<p align=\"center\"><img border=\"0\" src=\"../../logol.jpg\" width=\"200\" height=\"97\"></p>"
print "<form method=\"post\" action=\"../../cgi-bin/agend.cgi\" name=anlsform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
printf "<H3>RESUMEN DE PACIENTES<br>POR COMUNA</H3>\n"
printf "<input type=\"hidden\" value=\"Consultar\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
if (1 == 0)
{
print "</table>"
print "<table><tr>"
labels("SALDOS","C")
labels("VALORES","R")
print "</tr>"
print "<tr>"
label("ANTERIOR","L","T")
label(cta[4],"R","N")
print "</tr>"
print "<tr>"
label("DEL PERIODO","l","T")
label(cta[5],"R","N")
print "</tr>"
print "<tr>"
label("ACTUAL","L","T")
label(cta[6],"R","N")
print "</tr>"
}
print "</center>"
print "</form>"
print "<div id=\"flotante\">"
print "        <br /><span id=\"posicion\"></span>"
print "</div>"

print "<script language=\"Javascript\" src=\"../js/agenda.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/age1.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/pac.js\"> </script>"
print "</body>"
print "</html>"
} ' - 


cat tmp/ZO | awk -F"^" ' BEGIN { 
T["01"]="ZONA^LT"
T["02"]="PACIENTES^RN"
T["03"]=" [%] ^RT"
tx=4
	for (i=1;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<tx;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}
printf "\n"
}
END {
	printf "TOTAL^"
	for (i=2;i<tx;i++)
		printf "%s^",tt[i]
print ""
} ' - |

awk -F"^" ' BEGIN {
ttop("Inhedo - RESUMEN DE PACIENTES") ;bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF; i+=2) 
	{
		al[(i+1)/2]=substr($(i+1),1,1)
		tp[(i+1)/2]=substr($(i+1),2)
		labelf($i,al[(i+1)/2])
	}
print "</tr>"
next
}
$1=="TOTAL" {
tp[2]="N"
for (i=1; i<NF; i++) labelf($i,al[i],tp[i])

print "</tr>"
}
$1!="TOTAL" {
for (i=1; i<NF; i++) label($i,al[i],tp[i],tit[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 foot() } 
function select(name,selv,vals)
{
	file="sort -t\"^\" -k2 ./tmp/"name
printf "<td>"
printf "<p><select id=\"S%s\" style=\"font-family: Arial; font-size: 8pt ;border:1px solid #7F9DB9;background-color:#FFFFFF\"  >\n",$1$8,selv
printf "<option value=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"
	while ((file | getline linex) >0)
	{
		split(linex,lin) 
		printf "<option value=\"%s\">%s\n",lin[1],lin[2]
	}
	printf "</select></p>\n"
	close(file)
}
function boton(value,alig,lar,type,tt)
{
        printf "<td bgcolor=\"%s\" id=\"S%s\">\n",bgcolor,$1""$8""i
	printf "<p align=\"center\">"
	if(type=="S") printf "<input type=\"submit\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"agendH00\"></p>\n",lar,alg[alig],value
	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" id=\"K%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8""i
#	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8
print "</td>"
print "</td>"
}
function chkb(value)
{
	printf "<td><p><input type=\"checkbox\" style=\"font-family: Arial; font-size: 8pt\" size=\"%s\" name=\"C%s\"></p></td>\n",1,++line
}
function punto (dat,dec)
{
        if (dat+0==0) return ""
        sig=""
        if(dat<0)
        {
                dat *= -1
                sig="-"
        }
        pun=sprintf("%%.%df",dec)
        da=sprintf (pun,dat)
        if(dec) l=length(da)-dec-1
        else l=length(da)
        dat=substr(da,l+2)
        dato=substr(da,1,l)
	if (l>9) pun=substr(dato,1,l-9)"."substr(dato,l-8,3)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if (l>6) pun=substr(dato,1,l-6)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if(l >3) pun=substr(dato,1,l-3)"."substr(dato,l-2)
        else pun=dato
        if (dec) pun=pun","dat
        return sig""pun
}
function label(value,alig,tip,tt)
{
	printf "<td id=\"P%s%s\" bgcolor=\"%s\"><p ",$1$8,i,bgcolor
	if(tt) printf " onmouseover=\"showdiv(event,%c%s%c);\" onmousemove=\"showdiv(event,%c%s%c);\" onmouseout=\"javascript:document.getElementById(%cflotante%c).style.display=%cnone%c;\" ",39,$1,39,39,$1,39,39,39,39,39
	if(substr(tip,1,1)=="N") if(value <0) printf " align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(-value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
	print "</td>"
}
function labelf(value,alig,tip)
{
if(tip=="T" && value!="TOTAL") value="&nbsp"
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labeld(value,alig,tip)
{
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labels(value,alig)
{
print "<td bordercolor=\"#C0C0C0\" bgcolor=\"#003399\" align=\"center\">"
printf "<p align=\"%s\"><b><font color=\"#FFFFFF\">&nbsp;%s&nbsp;</font></b></p>\n",alg[alig],value
print "</td>"
}
function ttop(value)
{
print "<html>"
print "<head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
print "<meta name=\"GENERATOR\" content=\"Microsoft FrontPage 4.0\">"
print "<meta name=\"ProgId\" content=\"FrontPage.Editor.Document\">"
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"../age1.css\">"
printf "<title>%s</title>\n",value
print "</head>"
print "<center>"
print "<body bgcolor=\"#D0D0DF\">"
#print "<p align=\"center\"><img border=\"0\" src=\"../../logol.jpg\" width=\"200\" height=\"97\"></p>"
print "<form method=\"post\" action=\"../../cgi-bin/agend.cgi\" name=anlsform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
printf "<H3>RESUMEN DE PACIENTES<br>POR ZONA</H3>\n"
printf "<input type=\"hidden\" value=\"Consultar\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
if (1 == 0)
{
print "</table>"
print "<table><tr>"
labels("SALDOS","C")
labels("VALORES","R")
print "</tr>"
print "<tr>"
label("ANTERIOR","L","T")
label(cta[4],"R","N")
print "</tr>"
print "<tr>"
label("DEL PERIODO","l","T")
label(cta[5],"R","N")
print "</tr>"
print "<tr>"
label("ACTUAL","L","T")
label(cta[6],"R","N")
print "</tr>"
}
print "</center>"
print "</form>"
print "<div id=\"flotante\">"
print "        <br /><span id=\"posicion\"></span>"
print "</div>"

print "<script language=\"Javascript\" src=\"../js/agenda.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/age1.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/pac.js\"> </script>"
print "</body>"
print "</html>"
} ' - 

