<%@ page contentType="text/html;charset=gb2312"%>
<%@ include file="Common.jsp" %><%!
//
//   Filename: searches.jsp


static final String sFileName = "searches.jsp";
              
static final String PageBODY = "link=\"#000000\" vlink=\"#0033cc\" alink=\"red\"";
static final String FormTABLE = "border=\"0\" cellspacing=\"1\" cellpadding=\"3\"";
static final String FormHeaderTD = "bgcolor=\"#c2c2c2\"";
static final String FormHeaderFONT = "face=\"arial\" style=\"font:bold\" color=\"#0033cc\"";
static final String FieldCaptionTD = "bgcolor=\"#f2f2f2\"";
static final String FieldCaptionFONT = "face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\"";
static final String DataTD = "";
static final String DataFONT = "face=\"arial\" size=\"2\"";
static final String ColumnFONT = "face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\"";
static final String ColumnTD = "bgcolor=\"#f2f2f2\"";
%><%

boolean bDebug = false;

String sAction = getParam( request, "FormAction");
String sForm = getParam( request, "FormName");
String sSearchErr = "";
String sMessagesErr = "";

java.sql.Connection conn = null;
java.sql.Statement stat = null;
String sErr = loadDriver();
conn = cn();
stat = conn.createStatement();
if ( ! sErr.equals("") ) {
 try {
   out.println(sErr);
 }
 catch (Exception e) {}
}

%>            
<html>
<head>
<title>SearchRes</title>
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="expires" content="0"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body link="#000000" vlink="#0033cc" alink="red">
<jsp:include page="header.jsp" flush="true"/>
 <table>
  <tr>
   
   <td valign="top">
<% Search_Show(request, response, session, out, sSearchErr, sForm, sAction, conn, stat); %>
    
   </td>
  </tr>
 </table>
 <table>
  <tr>
   <td valign="top">
<% Messages_Show(request, response, session, out, sMessagesErr, sForm, sAction, conn, stat); %>
    
   </td>
  </tr>
 </table>


</body>
</html>
<%%>
<%
if ( stat != null ) stat.close();
if ( conn != null ) conn.close();
%>
<%!

  void Search_Show (javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sSearchErr, String sForm, String sAction, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
    try {
      

      String flds_topic="";


      String sSQL="";
      String transitParams = "";
      String sQueryString = "";
      String sPage = "";
      

      out.println("    <table border=\"0\" cellspacing=\"1\" cellpadding=\"3\">");
      
      out.println("     <form method=\"get\" action=\"searches.jsp\" name=\"Search\">\n     <tr>");
      // Set variables with search parameters
      
      flds_topic = getParam( request, "s_topic");

      // Show fields
      

      out.println("      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\"></font></td>");
      out.print("      <td >"); out.print("<input type=\"text\"  name=\"s_topic\" maxlength=\"50\" value=\""+toHTML(flds_topic)+"\" size=\"\">");
 out.println("</td>");
      
      out.println("      <td ><input type=\"submit\" value=\"Search\"/></td>");
      out.println("     </tr>\n     </form>\n    </table>");
      out.println("");
    }
    catch (Exception e) { out.println(e.toString()); }
  }


  void Messages_Show (javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sMessagesErr, String sForm, String sAction, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException  {
  
    String sWhere = "";
    int iCounter=0;
    int iPage = 0;
    boolean bIsScroll = true;
    boolean hasParam = false;
    String sOrder = "";
    String sSQL="";
    String transitParams = "";
    String sQueryString = "";
    String sPage = "";
    int RecordsPerPage = 20;
    String sSortParams = "";
    String formParams = "";

      String ps_topic="";


    formParams = "s_topic=" + toURL(getParam( request, "s_topic")) + "&"; 
    // Build WHERE statement
    // Check Messages parameter and create a valid sql for where clause 
   
    ps_topic = getParam( request, "s_topic");


    if ( ! ps_topic.equals("") ) { 
      hasParam = true;
      sWhere = "m.message like '%" + replace(ps_topic, "'", "''") + "%'" + " or " + "m.topic like '%" + replace(ps_topic, "'", "''") + "%'";}
    if (hasParam) { sWhere = " WHERE (" + sWhere + ")"; }
    // Build ORDER statement
    String sSort = getParam( request, "FormMessages_Sorting");
    String sSorted = getParam( request, "FormMessages_Sorted");
    String sDirection = "";
    String sForm_Sorting = "";
    int iSort = 0;
    try {
      iSort = Integer.parseInt(sSort);
    }
    catch (NumberFormatException e ) {
      sSort = "";
    }
    if ( iSort == 0 ) {
      sForm_Sorting = "";
    }
    else {
      if ( sSort.equals(sSorted)) { 
        sSorted="0";
        sForm_Sorting = "";
        sDirection = " DESC";
        sSortParams = "FormMessages_Sorting=" + sSort + "&FormMessages_Sorted=" + sSort + "&";
      }
      else {
        sSorted=sSort;
        sForm_Sorting = sSort;
        sDirection = " ASC";
        sSortParams = "FormMessages_Sorting=" + sSort + "&FormMessages_Sorted=" + "&";
      }
    
      if ( iSort == 1) { sOrder = " order by m.topic" + sDirection; }
      if ( iSort == 2) { sOrder = " order by m.author" + sDirection; }
      if ( iSort == 3) { sOrder = " order by m.date_entered" + sDirection; }
    }
  

  // Build full SQL statement
  
  sSQL = "select m.author as m_author, " +
    "m.date_entered as m_date_entered, " +
    "m.message as m_message, " +
    "m.message_id as m_message_id, " +
    "m.topic as m_topic " +
    " from messages m ";
  
  sSQL = sSQL + sWhere + sOrder;

  String sNoRecords = "     <tr>\n      <td colspan=\"4\" ><font face=\"arial\" size=\"2\">No records</font></td>\n     </tr>";


  String tableHeader = "";
      tableHeader = "     <tr>\n      <td bgcolor=\"#f2f2f2\"><a &\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\"></font></a></td>\n      <td bgcolor=\"#f2f2f2\"><a href=\""+sFileName+"?"+formParams+"FormMessages_Sorting=1&FormMessages_Sorted="+sSorted+"&\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">标题</font></a></td>\n      <td bgcolor=\"#f2f2f2\"><a href=\""+sFileName+"?"+formParams+"FormMessages_Sorting=2&FormMessages_Sorted="+sSorted+"&\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">作者</font></a></td>\n      <td bgcolor=\"#f2f2f2\"><a href=\""+sFileName+"?"+formParams+"FormMessages_Sorting=3&FormMessages_Sorted="+sSorted+"&\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">发表日期</font></a></td>\n     </tr>";
  
  
  try {
    out.println("    <table border=\"0\" cellspacing=\"1\" cellpadding=\"3\">");
    out.println("     <tr>\n      <td bgcolor=\"#c2c2c2\" colspan=\"4\"><a name=\"Messages\"><font face=\"arial\" style=\"font:bold\" color=\"#0033cc\">搜索结果</font></a></td>\n     </tr>");
    out.println(tableHeader);

  }
  catch (Exception e) {}

  
  try {
    // Select current page
    iPage = Integer.parseInt(getParam( request, "FormMessages_Page"));
  }
  catch (NumberFormatException e ) {
    iPage = 0;
  }

  if (iPage == 0) { iPage = 1; }
  RecordsPerPage = 20;
  try {
    java.sql.ResultSet rs = null;
    // Open recordset
    rs = openrs( stat, sSQL);
    iCounter = 0;
    absolute (rs, (iPage-1)*RecordsPerPage+1);
    java.util.Hashtable rsHash = new java.util.Hashtable();
    String[] aFields = getFieldsName( rs );

    // Show main table based on recordset
    while ( (iCounter < RecordsPerPage) && rs.next() ) {

      getRecordToHash( rs, rsHash, aFields );
      String fldauthor = (String) rsHash.get("m_author");
      String flddate_entered = (String) rsHash.get("m_date_entered");
      String fldtopic = (String) rsHash.get("m_topic");
      String fldField1= "<img src=images/Thread_small.gif>";

      out.println("     <tr>");
      
      out.print("      <td >"); out.print("<font face=\"arial\" size=\"2\">"+fldField1+"&nbsp;</font>");
      out.println("</td>");
      out.print("      <td >"); out.print("<a href=\"viewthread.jsp?"+transitParams+"mid="+toURL((String) rsHash.get("m_message_id"))+"&\"><font face=\"arial\" size=\"2\">"+toHTML(fldtopic)+"</font></a>");

      out.println("</td>");
      out.print("      <td >"); out.print("<font face=\"arial\" size=\"2\">"+toHTML(fldauthor)+"&nbsp;</font>");
      out.println("</td>");
      out.print("      <td >"); out.print("<font face=\"arial\" size=\"2\">"+toHTML(flddate_entered)+"&nbsp;</font>");
      out.println("</td>");
      out.println("     </tr>");
    
      iCounter++;
    }
    if (iCounter == 0) {
      // Recordset is empty
      out.println(sNoRecords);
    
      iCounter = RecordsPerPage+1;
      bIsScroll = false;
    }

    else {

  
    // Parse scroller
    boolean bInsert = false;
    boolean bNext = rs.next();
    if ( !bNext && iPage == 1 ) {
    
    }
    else {
      out.print("     <tr>\n      <td colspan=\"4\" bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">");
    
      if ( iPage == 1 ) {
        out.print("\n       <a href_=\"#\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">Previous</font></a>");
      }
      else {
        out.print("\n       <a href=\""+sFileName+"?"+formParams+sSortParams+"FormMessages_Page="+(iPage - 1)+"#Form\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">Previous</font></a>");
      }
  
      out.print("\n       [ "+iPage+" ]");
  
      if (!bNext) { 
        out.print("\n       <a href_=\"#\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">Next</font></a><br>");
      }
      else {
        out.print("\n       <a href=\""+sFileName+"?"+formParams+sSortParams+"FormMessages_Page="+(iPage + 1)+"#Form\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">Next</font></a><br>");
      }
    
      out.println("</td>\n     </tr>");
    }
  
    }

    if ( rs != null ) rs.close();
    out.println("    </table>");
    
  }
  catch (Exception e) { out.println(e.toString()); }
}
%>