<!--
// Deals with all aspects of building the HTML presented to the user

// Build the HTML of the question (title, question and values)
// Individual renderers held in the pagebuild_xxx.js files
// The individual js files are included in main.html
// Requires the "question" variable to have been previously populated
function BuildQuestionHTML ( )
{
  var s = "" ;
  switch ( question.type )
  {
    case solutionType :
      s += Build_Solutions() ;
      break;
    case reportType :
      s += Build_Report() ;
      break;
    case numericType :
      s += Build_Numeric() ;
      break;
    case dateType :
      s += Build_Date() ;
      break;
    default :
      s += Build_Question() ;
  }
  return ( s ) ;
}

function BuildQuestionText ( )
{
  var s = "" ;
  switch ( question.type )
  {
    case solutionType :
      s += BuildSolutions_AsText() ;
      break;
    case reportType :
      s += BuildReport_AsText() ;
      break;
    case numericType :
      s += BuildNumeric_AsText() ;
      break;
    case dateType :
      s += BuildDate_AsText() ;
      break;
    default :
      s += BuildQuestion_AsText() ;
  }
  return ( s ) ;
}

function isInQuestionFrame( doc )
{
  return ( ( doc == document.getElementById("questionFrame").document ) ) ;
}

function NumToDate ( n )
{
  var s = n + ' ' ;
  s = s.substr ( 6 , 2 ) + '-' + s.substr ( 4 , 2 ) + '-' + s.substr ( 0 , 4 ) ;
  return ( s ) ;
}

// Build the HTML of the dialog question (question and values)
// Renderer held in the pagebuild_mquestion.js file
// The individual js files are included in main.html
// Requires the "question" variable to passed with the attribute to add
function BuildMQuestionHTML ( question )
{
  return ( Build_MQuestion( question ) ) ;
}

// Build the HTML for a single answer for use in the audit
function GetAnswerHTML ()
{
  var i,c ;
  var s = "" ;
  var ss = "" ;
  for ( i = answers.length - 1 ; i >= 0 ; i-- )
  {
    if ( answers[i].backChainID == 0 )
    {

/*    Left in for attachment port
      var answerDiv = "ans" + answers[i].id ;
      if ( ! popupAttachments )
        ss = ',' + answers[i].id ;
      else
        ss = '' ;
      s += '<table width="100%" border="0" cellspacing="0" cellpadding="0">\n' +
           '<tr class="answer" align="top">' +
           '<td width="70%">' +
             '<a class="vqa" href="javascript:parent.ShowAttachment(\'' + answers[i].attachment + '\'' + ss + ')">' +
             '<img src="' + GetImage('addinfo.gif') + '" border="0" alt="Show Documentation">&nbsp;&nbsp;' +
              answers[i].text +
             '</a>' +
           '</td>' +
           '<td>';
*/
      var answerDiv = "ans" + answers[i].id ;
      s += '<table width="100%" border="0" cellspacing="0" cellpadding="0">\n' +
           '<tr class="answer" align="top">' +
           '<td width="70%">' +
           '&nbsp;' +
           answers[i].text +
           '</td>' +
           '<td>';

      var ss = "" ;
      if ( ( answers[i].type == numericType ) || ( answers[i].type == dateType ) )
      {
        var vNum = answers [ i ] . currentValue ;
        if ( vNum != NO_VALUE_NUMERIC )
        {
          if ( answers[i].type == numericType )
            ss = vNum ;
          else
            ss = NumToDate ( vNum ) ;
        }
      }
      else
      {
        var vIdx = GetAnswerValueIndex ( i ) ;
        if ( vIdx >= 0 )
          ss = answers[i].values[vIdx].text ;
      }
      if ( ss == "" )
        ss = Text_Unanswered ; // Should never be used

      s += '<a class="vqa" href="javascript:parent.ShowDiv(\'' + answerDiv + '\');">' + ss + '</a>' ;
      s += '</td></tr></table>\n' ;
/*
      if ( ! popupAttachments )
        s += Build_InlineAttachment ( answers[i].id , false ) ;
*/
      s += '<div id=' + answerDiv + ' style="display:none">' +
           '<table width="100%" border="0" cellspacing="0" cellpadding="0">\n' ;
      for ( c = -1 ; c < answers[i].values.length ; c++ )
      {
        var valName = "" ;
        var valID = 0 ;
        var valAttach = "" ;
        if ( c == -1 )
        {
          valName = Text_Unanswered ;
          valID = 0 ;
          valAttach = '' ;
        }
        else
        {
          valName = answers[i].values[c].text ;
          valID = answers[i].values[c].id ;
          if ( answers[i].values[c].id == answers[i].currentValue )
            valName += '&nbsp;<img src="' + GetImage('current.gif') + '" border="0">' ;
/*
          valAttach = answers[i].values[c].attachment ; // not used at present
*/
        }
        s += '<tr class="value">' +
             '<td width="10%">&nbsp;</td>' +
             '<td width="90%">' +
             '<a class="vqa" href="javascript:parent.ChangeAnswer(' + answers[i].id + ',' + valID + ')">' +
             '<img src="' + GetImage('arrow.gif') + '" border="0">&nbsp;' + valName +
             '</a>' +
             '</td>' +
             '</tr>\n' ;
        if ( ( answers[i].type == solutionType ) || ( answers[i].type == navigationType ) || ( answers[i].type == numericType ) || ( answers[i].type == dateType ) )
          break ;
      }
      s += '</table></div>\n' ;
    }
  }
  return ( s ) ;
}

// Return the audit HTML for insertion onto the question pages
function BuildAuditHTML()
{
  var s = '' ;
  if ( answers.length > 0 )
  {
    s = '  <table border="0" cellspacing="0" cellpadding="0" width="100%">' +
        '    <tr class="greenhead">' +
        '      <td width="70%">&nbsp;' + Text_Audit + '</td>' +
        '      <td class="smallhead">' + Text_AuditChange + '</td>' +
        '    </tr>' +
        '  </table>' ;
    s = WrapTable ( "../../attarImages/ghead" , "../../attarImages/tiny.gif" , 4 , s ) +
        GetPadTable ( 8 ) +
        GetAnswerHTML() ;
  }      
  return ( s ) ;
}

function BuildCopyHTML() 
{
  var s ;
  s = '<table border="0" cellspacing="0" cellpadding="0" width="100%">' +
        '<tr class="yellowhead">' +
          '<td width="70%" id="copyHeader1"></td>' +
          '<td id="copyHeader2" class="smallhead" align="right">' +
            '<script language="javascript">' +
              'if (window.clipboardData)' +
                'document.write(\'<a href="javascript:parent.doTextCopy();">' +
                                 '<img src="../../attarImages/toolcopy.gif" alt="Copy to Clipboard" width="21" height="21" border="0"  /></a>\');' +
            '</script>' +
            '<a href="javascript:parent.ShowDiv(\'copydiv\')">' +
              '<img src="../../attarImages/toolclose.gif" alt="Close Text Window" width="21" height="21" border="0" valign="middle" />' +
            '</a>' +
            '<img src="../../attarImages/tiny.gif" border="0" height="1" width="5" />' +
          '</td>' +
        '</tr>' +
      '</table>' ;
  s = WrapTable ( "../../attarImages/yhead" , "../../attarImages/tiny.gif" , 4 , s ) +
      GetPadTable ( 8 ) +
      '<form><textarea id="clipdata" style="width:100%;" rows="4" ></textarea></form>' ;
  return(s);
}

// Pad a string with zeros
function PadZero ( sInput , len , dir ) 
{
  sInput += "" ;
  while ( sInput . length < len )
  {
    if (dir == "right" )
      sInput += "0" ;
    else
      sInput = "0" + sInput ;
  }
  return ( sInput ) ;
}

// Build the final HTML for passing back
function BuildEndHTML ()
{
  var s = '' ;

  s = '  <table border="0" cellspacing="0" cellpadding="0" width="100%">' +
      '    <tr class="yellowhead">' +
      '      <td width="70%">&nbsp;End of Demo</td>' +
      '      <td class="smallhead"></td>' +
      '    </tr>' +
      '  </table>' ;
    s = WrapTable ( "attarImages/yhead" , "attarImages/tiny.gif" , 4 , s ) ;
  s += '<br><b>Answers:</b><br>' ;

  var c , i ;
  for ( c = 0 ; c < answers.length ; c++ )
    if ( answers[c].backChainID == 0 ) // Remove this condition to allow back chains in the final list
    {
      if ( answers[c].type == numericType )
      {
        if ( answers[c].currentValue != NO_VALUE_NUMERIC )
          s += '&nbsp;&nbsp;' + answers[c].text + ' = ' + answers[c].currentValue ;
      }
      else if ( answers[c].type == dateType )
      {
        if ( answers[c].currentValue != NO_VALUE_NUMERIC )
          s += '&nbsp;&nbsp;' + answers[c].text + ' = ' + NumToDate ( answers[c].currentValue ) ;
      }
      else
      {
        i = GetAnswerValueIndex ( c ) ;
        if ( i >= 0 )
        {
          s += '&nbsp;&nbsp;' + answers[c].text + '&nbsp;=&nbsp;' + answers[c].values[i].text ;
          if ( answers[c].type == TSstepType )
            s += ' <font color="red">(Step)</font>' ;
          else if ( answers[c].type == solutionType )
            s += ' <font color="red">(Solution ' + answers[c].values[i].id + ')</font>' ;
          else if ( answers[c].type == navigationType )
            s += ' <font color="red">(Navigation)</font>' ;
          s += '<br>' ;
        }
      }
    }


  s = WrapTable ( "attarImages/txt" , "attarImages/tiny.gif" , 8 , s ) ;
  s = WrapTable ( "attarImages/bkgnd" , "attarImages/tiny.gif" , 8 , s ) ;

  return ( s ) ;
}

// Trouble shoot main page build
function BuildTroubleHTML(doc)
{
  var s = '' ;
  var ss = '' ;
  if( isInQuestionFrame( doc ) )
  {
    ss = Text_TroubleShoot ;
    if ( qText.length > 0 )
      ss = qText ;
    s = '<table border="0" width="100%" cellspacing="0" cellpadding="0">' +
        '<tr class="head" valign="middle"><td>&nbsp;' + ss + '</td>' ;

    if ( runMode != "" )  
      s += '<td align="right"><a class="srch" href="javascript:parent.SaveSession(0);">' + Text_SaveSession + '&nbsp;&nbsp;</a></td>' ;
    s += '</tr></table>' +
        GetPadTable ( 8 ) ;
  }
  else
  {
    s = '<div align="center" class="yellowhead2">Overview</div>' ;
    s = WrapTable ( "../../attarImages/yhead" , "../../attarImages/tiny.gif" , 4 , s ) +
        GetPadTable ( 4 ) ;
  }
  s += '<table id="TStable" width="100%" border="0" cellspacing="0" cellpadding="0"></table>' ;
  if( isInQuestionFrame( doc ) )
  {
    s +=  GetPadTable ( 8 ) ;

    if ( runMode == "2" )
      s += BuildExitBtn (); 
    else
      s += BuildExitBtn ( "javascript:parent.TS_EndString('" + qName + "'," + oID + ");" ) ;
    s += GetPadTable ( 8 ) ;
    if ( emailContext )
    {
      s += '<input type="button" value="' + Text_EmailBtn + '" onclick="javascript:alert(parent.TS_AsText());" />' +
           GetPadTable ( 8 ) ;
    }
  }
  return ( s ) ;
}

// Navigation main page build
function BuildNavigationHTML(doc)
{
  var s = '' ;
  var ss = '' ;
  if( isInQuestionFrame( doc ) )
  {
    ss = Text_Navigation ;
    if ( qText.length > 0 )
      ss = qText ;

    s = '<table border="0" width="100%" cellspacing="0" cellpadding="0">' ;
    s += '<tr class="head" valign="middle"><td>&nbsp;' + ss + '</td>' ;
    if ( runMode != "" )  
      s +='<td align="right"><a class="srch" href="javascript:parent.SaveSession(0);">' + Text_SaveSession + '&nbsp;&nbsp;</a></td>'
    s += '</tr></table>' +
      GetPadTable ( 8 ) ;
  }
  else
  {
    s = '<div align="center" class="yellowhead2">Overview</div>' ;
    s = WrapTable ( "../../attarImages/yhead" , "../../attarImages/tiny.gif" , 4 , s ) +
        GetPadTable ( 4 ) ;
  }
  s += '<table id="TStable" width="100%" border="0" cellspacing="0" cellpadding="0"></table>' ;
  if( isInQuestionFrame( doc ) )
  {
    s += GetPadTable ( 8 ) +
    BuildExitBtn ( ) +
    GetPadTable ( 8 ) ;
    if ( emailContext )
    {
      s += '<input type="button" value="' + Text_EmailBtn + '" onclick="javascript:alert(parent.Nav_AsText());" />' +
           GetPadTable ( 8 ) ;
    }
  }
  return ( s ) ;
}

// Navigation main page build
function BuildQAReport()
{
  var s , ss ;
  ss = Text_QAReport ;
  if ( qText.length > 0 )
    ss = qText ;
  s = '<table border="0" width="100%" cellspacing="0" cellpadding="0">' +
      '<tr class="head" valign="middle"><td>&nbsp;' + ss + '</td>' ;
  if ( runMode != "" )  
      s +='<td align="right"><a class="srch" href="javascript:parent.SaveSession(0);">' + Text_SaveSession + '&nbsp;&nbsp;</a></td>' ;
  s += '</tr></table>' +
      GetPadTable ( 8 ) +
      '<table id="QAtable" width="100%" border="0" cellspacing="0" cellpadding="0"></table>' +
      GetPadTable ( 8 ) +
      BuildExitBtn ( ) +
      GetPadTable ( 8 ) ;
  if ( emailContext )
  {
    s += '<input type="button" value="' + Text_EmailBtn + '" onclick="javascript:alert(parent.QA_AsText());" />' +
         GetPadTable ( 8 ) ;
  }
  return ( s ) ;
}
-->