ap_rprintf(r,"<HEAD>\n<SCRIPT>\nvar par_id=%d;\nvar id_session=%d;\nvar par_area='%s'\nvar debug_level=%d\n</SCRIPT>\n",par_id,cou_assignNewSession(cnx), par_area, cnx->debug_level);

filename = ap_psprintf(p, "%s/spreadsheet/editBottom.head", cou_getCORootPath(cnx));

__PUT_HTML_FROM_FILE(filename)

ap_rputs("</HEAD>\n",r);

filename = ap_psprintf(p, "%s/spreadsheet/editBottom.body", cou_getCORootPath(cnx));

__PUT_HTML_FROM_FILE(filename)

// here we ought to generate spreadsheet
ap_rputs("<TABLE id=table BORDER=5",r);
ap_rputs(" LANGUAGE=javascript onkeyup='body_onkeyup()' onclick='return table_onclick()'",r);
ap_rputs(" ondblclick='table_ondblclick()' onmouseup='table_onmouseup()'",r);
ap_rputs(" onselectstart='return table_onselectstart()' onmouseover='return table_onmouseover()'",r);
ap_rputs(">\n",r);

// obtain column ids
__CREATE_QUERY("SELECT id,align,fontsize,textcolor,fontfamily,textstyle,name FROM co_%s_spreadsheet_column WHERE spreadsheet_id=%d ORDER BY column_number",par_area,par_id);
__SQL_GET_RESULT
cols = cor_GetTupleCount(cnx, corec);

// column attributes

ap_rputs("<COL width=30 align=center>\n",r);
ap_rputs("<COLGROUP id=columns width=100>\n",r);
for (i=0;i<cols;i++)
{
ap_rputs("<COL ",r);
if (strcmp(cor_GetValue(cnx, corec,i,1),"")) ap_rprintf(r," align=%s",cor_GetValue(cnx, corec,i,1));
ap_rputs(" style='",r);
if (strcmp(cor_GetValue(cnx, corec,i,3),"")) ap_rprintf(r,"color:%s",cor_GetValue(cnx, corec,i,3));
if (strcmp(cor_GetValue(cnx, corec,i,4),"")) ap_rprintf(r,";font-family:%s",cor_GetValue(cnx, corec,i,4));
if (strcmp(cor_GetValue(cnx, corec,i,2),"")) ap_rprintf(r,";font-size:%s",cor_GetValue(cnx, corec,i,2));
strncpy(textstyle,cor_GetValue(cnx, corec,i,5),3);
if (textstyle[0]=='B') ap_rputs(";font-weight:700",r);
if (textstyle[1]=='I') ap_rputs(";font-style:italic",r);
if (textstyle[2]=='U') ap_rputs(";text-decoration:underline",r);
ap_rprintf(r,"'");
if (strcmp(cor_GetValue(cnx, corec,i,6),"")) ap_rprintf(r," NAMED=1");
ap_rprintf(r," DB_ID=%s>\n",cor_GetValue(cnx, corec,i,0));
}
ap_rputs("</COLGROUP>\n",r);

// first row with column names ...
ap_rputs("<TR bgcolor=LightGrey align=center>\n<TD onclick='select_all()' id='ST'></TD>\n",r);
for (i=0;i<cols;i++)
{
ap_rprintf(r,"<TD id=%d bgcolor=F0F0F0 style='font-family:verdana' align=center",i+1);
if (strcmp(cor_GetValue(cnx, corec,i,6),"")) ap_rprintf(r,">%s",cor_GetValue(cnx, corec,i,6));
else
{
j = i;
pos = 0;
// 26 is number of characters in english alphabet
while (j>=26)
{
// 65 is an ASCI code for first letter of an alphabet
column_abr[pos++] = (char) (65+(j % 26));
j /= 26; j--;
}
column_abr[pos] = (char) (65+j);

ap_rputs(">",r);
while (pos>=0) ap_rprintf(r,"%c",column_abr[pos--]);
}
ap_rputs("</TD>\n",r);
}
__CLEAR_RESULT

ap_rputs("<TD bgcolor=black><INPUT type=button style='HEIGHT: 23px;WIDTH: 16px' value='+' onclick='add_column()'",r);
ap_rputs(" id=button1 name=button1></TD>\n</TR>\n",r);

// read spreadsheet data from database

@co_call(par_area, par_class,"get", par_area=>par_area, par_class=>par_class, par_id=>par_id);
__GET_RETURNED_RECORDSET( corec)

// obtain row attributes
__CREATE_QUERY("SELECT id,align,fontsize,textcolor,fontfamily,textstyle,name FROM co_%s_spreadsheet_row WHERE spreadsheet_id=%d ORDER BY row_number", par_area, par_id);
__SQL_GET_VRESULT(corec2)
rows = cor_GetTupleCount(cnx, corec2);

// translate data into HTML
pos = 0;
for (i=1;i<=rows;i++)
{
// row style
ap_rputs("<TR bgcolor=white",r);
if (strcmp(cor_GetValue(cnx, corec2,i-1,1),"")) ap_rprintf(r," align=%s",cor_GetValue(cnx, corec2,i-1,1));
ap_rputs(" style='",r);
if (strcmp(cor_GetValue(cnx, corec2,i-1,3),"")) ap_rprintf(r,"color:%s",cor_GetValue(cnx, corec2,i-1,3));
if (strcmp(cor_GetValue(cnx, corec2,i-1,4),"")) ap_rprintf(r,";font-family:%s",cor_GetValue(cnx, corec2,i-1,4));
if (strcmp(cor_GetValue(cnx, corec2,i-1,2),"")) ap_rprintf(r,";font-size:%s",cor_GetValue(cnx, corec2,i-1,2));

strncpy(textstyle,cor_GetValue(cnx, corec2,i-1,5),3);
if (textstyle[0]=='B') ap_rputs(";font-weight:700",r);
if (textstyle[1]=='I') ap_rputs(";font-style:italic",r);
if (textstyle[2]=='U') ap_rputs(";text-decoration:underline",r);

if (!strcmp("",cor_GetValue(cnx, corec2,i-1,6)))
{
// column obtains default name
ap_rprintf(r,"' bordercolor=MistyRose DB_ID=%s>\n<TD bgcolor=F0F0F0 style='font-family:verdana' id=%d>%d</TD>\n",cor_GetValue(cnx, corec2,i-1,0),i,i);
}
else
{
// column obtains defined name
ap_rprintf(r,"' bordercolor=MistyRose DB_ID=%s NAMED=1>\n<TD bgcolor=F0F0F0 style='font-family:verdana' id=%d>%s</TD>\n",cor_GetValue(cnx, corec2,i-1,0),i,cor_GetValue(cnx, corec2,i-1,6));
}

for (j=1;j<=cols;j++)
{
if (pos==cor_GetTupleCount(cnx, corec)) ap_rputs("<TD TYP='STR'>.</TD>\n",r);
else
{
if ((i==atoi(cor_GetValue(cnx, corec,pos,1))) && (j==atoi(cor_GetValue(cnx, corec,pos,0))))
{
ap_rputs("<TD TYP='STR'",r);
ap_rprintf(r," EXPR='%s'", cou_buildEncodedHTMLForScript( cnx, cor_GetValue(cnx, corec,pos,8)));

if (strcmp(cor_GetValue(cnx, corec,pos,3),"")) ap_rprintf(r," align=%s",cor_GetValue(cnx, corec,pos,3));

ap_rputs(" style='",r);
if (strcmp(cor_GetValue(cnx, corec,pos,5),"")) ap_rprintf(r,"color:%s",cor_GetValue(cnx, corec,pos,5));
if (strcmp(cor_GetValue(cnx, corec,pos,6),"")) ap_rprintf(r,";font-family:%s",cor_GetValue(cnx, corec,pos,6));
if (strcmp(cor_GetValue(cnx, corec,pos,4),"")) ap_rprintf(r,";font-size:%s",cor_GetValue(cnx, corec,pos,4));

strncpy(textstyle,cor_GetValue(cnx, corec,pos,7),3);
if (textstyle[0]=='B') ap_rputs(";font-weight:700",r);
if (textstyle[1]=='I') ap_rputs(";font-style:italic",r);
if (textstyle[2]=='U') ap_rputs(";text-decoration:underline",r);

cell_value = ap_escape_html(p, cor_GetValue(cnx, corec,pos,2));
if (strcmp(cell_value,"")) ap_rprintf(r,"'>%s</TD>\n",cell_value);
else ap_rputs("'>.</TD>\n",r);

pos++;
}
else ap_rputs("<TD TYP='STR'>.</TD>\n",r);
}
}
ap_rputs("</TR>\n",r);
}
ap_rputs("<TR>\n<TD bgcolor=black><INPUT type=button value='+'",r);
ap_rputs(" onclick='add_row()' id=button2 name=button2></TD>\n</TR>\n",r);
ap_rputs("</TABLE>\n<SCRIPT>\n",r);

@co_call(par_area, '"expression"',"getCellRefs", par_area=>par_area, par_class=>'"expression"', par_id=>par_id);

ap_rputs("</SCRIPT>\n<SPAN id=inserter></SPAN></BODY>",r);

__CLEAR_RESULT
cor_Clear(cnx, corec2);