// Detail Page Operations

// generic table
function fwTable(title, rows)
{
   this.title = title;
   this.rows = rows;
}

// generic table row
function fwRow(cells)
{
   this.cells = cells;
}

// generic table column
function fwCell(title, color, align, head, indexRange, textcolor)
{
   if (title != '')
   {
      this.title = title;
   }
   else
   {
      this.title = '&nbsp';
   }
   this.color = color;
   this.align = align;
   this.head = head;
   this.textcolor = textcolor;
   this.hint = indexRange;
}

// definition for details page image
function fwImage(tooltip, url, align)
{
   this.align = align;
   this.tooltip = tooltip;
   this.url = url;
}

// definition of text item
function fwText(header, value, align)
{
   this.header = header;
   this.value = value;
   this.align = align;
}

function fwTextx(header, value, align)
{
   this.header = header;
   this.value = value;
   this.align = align;
}

// Definition of details page item.
// Define fText, fwImage, and fwTable objects and place them in
// the corresponding arrays: fwTextArray, fwImageArray, and fwTableArray.
// Construct fwDetailArray containing fwDetailItem for every element
// to show on the detail page in the display order.
function fwDetailItem(type, index)
{
   this.type = type;              // any one of {fwText, fwImage, fwTable}
   this.index = index;            // index of item in the array
}

var tblcnt = 0;
// table to HTML formatter
function insertDetailTable(tbl)
{
  var pre, post, span;
  var crttbl = tblcnt;
  tblcnt = tblcnt + 1;
  var hdrstr = 'header_' + crttbl.toString() + '_';
  document.write('<div align="center"><center><p>');
  document.write('<table style="WIDTH: 100%;" border="1" cellpadding="2" cellspacing="0" bordercolor="#000084"><tbody>');
  document.write('<tr><td align="center" bgcolor="#000084">' + toTableHeader(tbl.title) + '</td></tr></tbody></table>');

  document.write('<table style="WIDTH: 100%;" border="1" cellpadding="2" cellspacing="0" bordercolor="#000084"><tbody>');

  for (var i = 0; i < tbl.rows.length; i++)
  {
    // create table body
    document.write('<tr>');
    for (var j = 0; j < tbl.rows[i].cells.length; j++)
    {
      var crid = i.toString() + j.toString();
      pre = '<span tabIndex="0" title=""><font face="' + cGeneralFont + '" size="1">';
      post = '</font></span>';

      // start new cell
      if ( i == 0 )
      {
        //table header tag
        document.write('<th id="' + hdrstr + crid.toString() + '"');
      }
      else
      {
        if ( j == 0 )
        {
          document.write('<th id="' + hdrstr + crid.toString() + '"');
        }
        else
        {
          document.write('<td headers="' + hdrstr + '0' + j.toString() + '"');
        }
      }
      if (tbl.rows[i].cells[j].color != '')
      {
        document.write(' bgcolor="' + tbl.rows[i].cells[j].color +'"');
      }
      else
      {
        if (tbl.rows[i].cells[j].head != '')
        {
          document.write(' bgcolor="#EDEDFF"');
        }
      }
      if (tbl.rows[i].cells[j].align != '')
      {
        document.write(' align="' + tbl.rows[i].cells[j].align +'"');
      }

      document.write('>');
      if (tbl.rows[i].cells[j].textcolor != '' )
      {
        document.write('<font color="' + tbl.rows[i].cells[j].textcolor + '">');
      }

      // write cell data out
      //first write the title wich will constitute the cell's hint
      var hint = tbl.rows[i].cells[j].hint;
      if( hint != '' )
      {
        pre = pre.toString().replace( 'title=""', 'title="' + hint + '"' );
      }
      document.write(pre + tbl.rows[i].cells[j].title + post);

      if (tbl.rows[i].cells[j].textcolor != '' )
      {
        document.write('</font>');
      }
      if ( i == 0 )
      {
        //table header tag
        document.write('</th>');
      }
      else
      {
        if ( j == 0 )
        {
          document.write('</th>');
        }
        else
        {
          document.write('</td>');
        }
      }
    }
    document.write('</tr>');
  }
  document.write('</tbody></table>');
  document.write('</p></center></div>');
  document.write('<br>');
}

// presentation of one text element
function insertText(index)
{
   if (HasInstance(dTextArray) && HasInstance(dTextArray[index]) )
   {
      if (dTextArray[index].align != '')
      {
         document.write('<p align="' + dTextArray[index].align + '">' + toHeader(dTextArray[index].header) + '<br>' + toNormal(dTextArray[index].value) + '</p>');
      }
      else
      {
         document.write('<p>' + toHeader(dTextArray[index].header) + '<br>' + toNormal(dTextArray[index].value) + '</p>');
      }
   }
}

function insertTextx(index)
{
   if (HasInstance(dTextArray) && HasInstance(dTextArray[index]) )
   {
      if (dTextArray[index].align != '')
      {
         document.write('<p align="' + dTextArray[index].align + '">' + toHeader(dTextArray[index].header) + '<br>' + toPlain(dTextArray[index].value) + '</p>');
      }
      else
      {
         document.write('<p>' + toHeader(dTextArray[index].header) + '<br>' + toPlain(dTextArray[index].value) + '</p>');
      }
   }
}

// presentation of one image element
function insertImage(index)
{
   if (HasInstance(dImageArray) && HasInstance(dImageArray[index]))
   {
      document.write('<div><center>');
      document.write(toHeader(dImageArray[index].tooltip) + '<br>');
      document.write('<img src="' + dImageArray[index].url + '" title="' + dImageArray[index].tooltip + '" alt="' + dImageArray[index].tooltip + '" tabIndex="0">');
      document.write('</center></div><br>');
   }
}

// presentation of one table element
function insertTable(index)
{
   if (HasInstance(dTableArray) && HasInstance(dTableArray[index]))
   {
      insertDetailTable(dTableArray[index]);
   }
}

// presentation details page
function insertDetail(detailArray)
{
   if (HasInstance(detailArray))
   {
      for (var i = 0; i < detailArray.length; i++)
      {
         if (HasInstance(detailArray[i]))
         {
            switch (detailArray[i].type)
            {
               case 'fwText' :
                  insertText(detailArray[i].index); 
                  break;
               case 'fwTextx' :
                  insertTextx(detailArray[i].index);
                  break;
               case 'fwImage' :
                  insertImage(detailArray[i].index);
                  break;
               case 'fwTable' :
                  insertTable(detailArray[i].index);
                  break;
               default :
                  document.write('<br>' + toHeader(getString("rJS_Detail_Str_01")) + '<br>' + toNormal(getString("rJS_Detail_Str_02")));
            }
         }
      }
   }
}

function toTableHeader(text)
{
   return '<span tabIndex="0"><font face="' + cGeneralFont + '" size="2" color="#FFFFFF"><strong>' + text + '</strong></font></span>';
}

// presentation of measure name
function dInsertItemsTitle()
{
   document.write(toChapter(dMl));
}

// presents page title
function dInsertTitle()
{
   document.write(toChapter(getString("rJS_Detail_Str_03")));
}

// left hand-side page
function dInsertDesc()
{
   insertDetail(dDetailArrayL);
}

// right hand-side page
function dInsertItems()
{
   insertDetail(dDetailArrayR);
}

// inserts footer
function dInsertPublishedDate()
{
   document.write(toPublishedDate(dPublished));
}
// inserts footer
function dInsertFooter()
{
   document.write(toFooter(dLeftFooter, dCentreFooter, dRightFooter, dFooterBorder));
}
// inserts header
function dInsertGlobalHeader()
{
   document.write(toGlobalHeader(dLeftHeader, dCentreHeader, dRightHeader, dHeaderBorder));
}