/*****************************************************************************
 * Function : PrintPage
 *
 *    Notes : Opens a new window with relevant content for printing a page.
 *
 * Assumptions :
 *
 *   - The function assumes that the file contains the strings
 *     <div id="contentHeader">, <div id="content">, and
 *     <!-- InstanceBeginEditable name=\"ContentHeader\" -->.
 *     The last string must be within the content header.
 *
 * Example Usage :
 *
 * <div id="contentHeader"><a href='javascript:PrintPage("<?php 
 *
 * echo preg_replace( "/[\n|\r]/",
 *                    "\\n",
 *                    addslashes(file_get_contents("../includes.inc")) );
 *
 * ?>", "<?php echo $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; ?>", "<?php
 *
 * echo preg_replace( "/[\n|\r]/",
 *                    "\\n",
 *                    addslashes(file_get_contents("../footer.inc")));
 *
 * ?>")'> [ more stuff ] </div>
 *
 *****************************************************************************/
function PrintPage(includes, url, footer)
  {
  var header = document.getElementById("contentHeader").innerHTML;
  var contentStr = document.getElementById("content").innerHTML;
  var pos =
    header.indexOf("<!-- InstanceBeginEditable name=\"ContentHeader\" -->");

  // -------------------------------------------------------------------------
  // make the width of the footer's table 100%
  // -------------------------------------------------------------------------

  var rExp = new RegExp("(<table[^\>]+width=[\"'])[^\"']+", "gi");
  footer = footer.replace(rExp, "$1100%");

  // -------------------------------------------------------------------------
  // replace all anchor tags
  //
  // NOTE :
  // - Anchor tag replacement fails if between the '<' and the '>' there
  //   is another '>'.  During replacement, the actual '>' for the
  //   anchor will be displayed.
  // -------------------------------------------------------------------------

/*
  // attempt to handle calls to functions that may have strings with tags
  rExp = /<a[^>]+\"[^\"]*\([^\"]*\)\"[^>\)]*>/gi;
  contentStr = contentStr.replace(rExp, "<b class=\"print_content\">");
  footer = footer.replace(rExp, "<b class=\"print_footer\">");

  rExp = /<a[^>]+'[^']*\([^']*\)'[^>\)]*>/gi;
  contentStr= contentStr.replace(rExp, "<b class=\"print_content\">");
  footer = footer.replace(rExp, "<b class=\"print_footer\">");
*/

  rExp = new RegExp("<a[^>]+>", "gi");
  contentStr = contentStr.replace(rExp, "<b class=\"print_content\">");
  footer = footer.replace(rExp, "<b class=\"print_footer\">");

  rExp = new RegExp("<a[^>]+>", "gi");
  contentStr = contentStr.replace(rExp, "<b class=\"print_content\">");
  footer = footer.replace(rExp, "<b class=\"print_footer\">");


  printStr =
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" +
    "<html>\n" +
    "  <head>\n" +
    "    <title>College of Engineering</title>\n" +
    "    <meta http-equiv=\"Content-Type\" " +
      "content=\"text/html; charset=iso-8859-1\">\n" +
      includes +
    "  </head>\n" +
    "  <body>\n" +
    "\n" +
    "<table width=\"100%\" border=\"0\" " +
      "cellspacing=\"0\" cellpadding=\"0\">\n" +
    "  <tr>\n" +
    "    <td><img src=\"../images/vt_logo_black.gif\"></td>\n" +
    "    <td valign=\"middle\"><div class=\"subheader\" " +
      "align=\"right\">College of Engineering</div></td>\n" +
    "  </tr>\n" +
    "  <tr>\n" +
    "    <td>&nbsp;</td>\n" +
    "    <td valign=\"middle\"><div align=\"right\">" + 
      url + "</div></td>\n" +
    "  </tr>\n" +
    "</table>\n" +
    "<br />\n";

  // -------------------------------------------------------------------------
  // NOTE : In Safari, comments are replaced with spaces when retrieving the
  //        the innerHTML for a div tag.  Other browsers just give all the
  //        text within a div tag.  For this reason, the heaer is not
  //        displayed in Safari since it modifies the innerHTML.
  // -------------------------------------------------------------------------
 
  if ( pos != -1 )
    {
    header = header.substr(pos);

/*
    // attempt to handle calls to functions that may have strings with tags
    rExp = /<a[^>]+\"[^\"]*\([^\"]*\)\"[^>\)]*>/gi;
    header = header.replace(rExp, "<b class=\"print_contentHeader\">");

    rExp = /<a[^>]+'[^']*\([^']*\)'[^>\)]*>/gi;
    header = header.replace(rExp, "<b class=\"print_contentHeader\">");
*/

	rExp = new RegExp("<a[^>]+>", "gi");
    header = header.replace(rExp, "<b class=\"print_contentHeader\">");

	printStr +=
      "<div id=\"contentHeader\">" + header + "</div>\n";
    }

  printStr +=
	"<div id=\"content\">\n" + contentStr + "\n" +
    "</div>\n" +
    footer +
    "  </body>\n" +
    "</html>\n";

  rExp = new RegExp("</a>", "gi");
  printStr = printStr.replace(rExp, "</b>");

  // -------------------------------------------------------------------------
  // display the print window
  // -------------------------------------------------------------------------

  print_window = window.open
    (
	"",
	"print_window",
	"toolbar=no,menubar=yes,scrollbars=yes"
    );
  
  print_window.document.write(printStr);

  print_window.document.close();

  } // end function PrintPage