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


static final String sFileName = "reply.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 sreplyErr = "";
String soriginalErr = "";

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) {}
}
if ( sForm.equals("reply") ) {
  sreplyErr = replyAction(request, response, session, out, sAction, sForm, conn, stat);
  if ( "sendRedirect".equals(sreplyErr)) return;
}

%>            
<html>
<head>
<title>Forum</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">
<% original_Show(request, response, session, out, soriginalErr, sForm, sAction, conn, stat); %>
    
   </td>
  </tr>
 </table>
 <table>
  <tr>
   <td valign="top">
<% reply_Show(request, response, session, out, sreplyErr, sForm, sAction, conn, stat); %>
    
   </td>
  </tr>
 </table>


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


  String replyAction(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sAction, String sForm, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
  
    String sreplyErr ="";
    try {

      if (sAction.equals("")) return "";

      String sSQL="";
      String transitParams = "";
      String primaryKeyParams = "";
      String sQueryString = "";
      String sPage = "";
      String sParams = "";
      String sActionFileName = "index.jsp";
      String sWhere = " ";
      boolean bErr = false;
      long iCount = 0;

  
      String pPKmessage_id = "";

      final int iinsertAction = 1;
      final int iupdateAction = 2;
      final int ideleteAction = 3;
      int iAction = 0;

      if ( sAction.equalsIgnoreCase("insert") ) { iAction = iinsertAction; }
      if ( sAction.equalsIgnoreCase("update") ) { iAction = iupdateAction; }
      if ( sAction.equalsIgnoreCase("delete") ) { iAction = ideleteAction; }

      // Create WHERE statement


      String fldmid="";
      String fldtopic="";
      String fldauthor="";
      String fldmessage="";
      String flddate_entered="";
      String fldmessage_id="";

      // Load all form fields into variables
    
      fldmid = getParam( request, "Rqd_mid");
      fldtopic = getParam(request, "topic");
      fldauthor = getParam(request, "author");
      fldmessage = getParam(request, "message");
      flddate_entered = getParam(request, "date_entered");
      // Validate fields
      if ( iAction == iinsertAction || iAction == iupdateAction ) {
        if (sreplyErr.length() > 0 ) {
          return (sreplyErr);
        }
      }


      sSQL = "";
      // Create SQL statement

      switch (iAction) {
  
        case iinsertAction :
          
            sSQL = "insert into messages (" + 
                "message_parent_id," +
                "topic," +
                "author," +
                "message," +
                "date_entered)" +

                " values (" + 
                toSQL(fldmid, adNumber) + "," +
                toSQL(fldtopic, adText) + "," +
                toSQL(fldauthor, adText) + "," +
                toSQL(fldmessage, adText) + "," +
                toSQL(flddate_entered, adDate) + ")";
          break;
  
      }

      if ( sreplyErr.length() > 0 ) return sreplyErr;
      try {
        // Execute SQL statement
        stat.executeUpdate(sSQL);
      }
      catch(java.sql.SQLException e) {
        sreplyErr = e.toString(); return (sreplyErr);
      }
  
      try {
        if ( stat != null ) stat.close();
        if ( conn != null ) conn.close();
      }
      catch ( java.sql.SQLException ignore ) {}
      response.sendRedirect (sActionFileName);

      return "sendRedirect";
    }
    catch (Exception e) {out.println(e.toString()); }
    return (sreplyErr);
  }

  


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

      String sSQL="";
      String sQueryString = "";
      String sPage = "";
      String sWhere = "";
      String transitParams = "";
      String transitParamsHidden = "";
      String requiredParams = "";
      String primaryKeyParams ="";
      java.util.Hashtable rsHash = new java.util.Hashtable();
      
      String pmessage_id = "";

      String fldmessage_id="";
      String fldmid="";
      String fldtopic="";
      String fldauthor="";
      String fldmessage="";
      String flddate_entered="";


      boolean bPK = true;

      if ( "".equals(sreplyErr)) {
        // Load primary key and form parameters
        fldmessage_id = getParam( request, "message_id");
        fldmid = getParam( request, "mid");
        requiredParams += "<input type=\"hidden\" name=\"Rqd_mid\" value=\""+getParam( request, "mid")+"\">";
        pmessage_id = getParam( request, "message_id");
      }
      else {
        // Load primary key, form parameters and form fields
        fldmessage_id = getParam( request, "message_id");
        fldtopic = getParam( request, "topic");
        fldauthor = getParam( request, "author");
        fldmessage = getParam( request, "message");
        flddate_entered = getParam( request, "date_entered");
        requiredParams += "<input type=\"hidden\" name=\"Rqd_mid\" value=\""+getParam( request, "Rqd_mid")+"\">";
        pmessage_id = getParam( request, "PK_message_id");
      }

      
      if ( isEmpty(pmessage_id)) { bPK = false; }
      
      sWhere += "message_id=" + toSQL(pmessage_id, adNumber);
      primaryKeyParams += "<input type=\"hidden\" name=\"PK_message_id\" value=\""+pmessage_id+"\"/>";

      sSQL = "select * from messages where " + sWhere;


      out.println("    <table border=\"0\" cellspacing=\"1\" cellpadding=\"3\">");
      out.println("     <tr>\n      <td bgcolor=\"#c2c2c2\" colspan=\"2\"><font face=\"arial\" style=\"font:bold\" color=\"#0033cc\">你的回复</font></td>\n     </tr>");
      if ( ! sreplyErr.equals("")) {
        out.println("     <tr>\n      <td  colspan=\"2\"><font face=\"arial\" size=\"2\">"+sreplyErr+"</font></td>\n     </tr>");
      }
      sreplyErr="";
      out.println("     <form method=\"get\" action=\""+sFileName+"\" name=\"reply\">");

      java.sql.ResultSet rs = null;

      if ( bPK &&  ! (sAction.equals("insert") && "reply".equals(sForm))) {

        // Open recordset
        rs = openrs( stat, sSQL);
        rs.next();
        String[] aFields = getFieldsName( rs );
        getRecordToHash( rs, rsHash, aFields );
        rs.close();
        fldmessage_id = (String) rsHash.get("message_id");
        flddate_entered = (String) rsHash.get("date_entered");
        if ( "".equals(sreplyErr)) {
          // Load data from recordset when form displayed first time
          fldtopic = (String) rsHash.get("topic");
          fldauthor = (String) rsHash.get("author");
          fldmessage = (String) rsHash.get("message");
        }

        if (sAction.equals("") || ! "reply".equals(sForm)) {
      
          fldmessage_id = (String) rsHash.get("message_id");
          fldtopic = (String) rsHash.get("topic");
          fldauthor = (String) rsHash.get("author");
          fldmessage = (String) rsHash.get("message");
          flddate_entered = (String) rsHash.get("date_entered");
        }
        
      }
      else {
        if ( "".equals(sreplyErr)) {
          fldmessage_id = toHTML(getParam(request,"message_id"));
        }
flddate_entered = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new java.util.Date());
      }
      


      // Show form field
      
      out.print("     <tr>\n      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">文章标题</font></td><td >"); out.print("<input type=\"text\"  name=\"topic\" maxlength=\"50\" value=\""+toHTML(fldtopic)+"\" size=\"50\">");

      out.println("</td>\n     </tr>");
      
      out.print("     <tr>\n      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">作者</font></td><td >"); out.print("<input type=\"text\"  name=\"author\" maxlength=\"20\" value=\""+toHTML(fldauthor)+"\" size=\"20\">");

      out.println("</td>\n     </tr>");
      
      out.print("     <tr>\n      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">回复内容</font></td><td >"); out.print("<textarea name=\"message\" cols=\"80\" rows=\"12\">"+toHTML(fldmessage)+"</textarea>");

      out.println("</td>\n     </tr>");
      
      out.print("     <tr>\n      <td colspan=\"2\" align=\"right\">");
      

      if ( bPK && ! (sAction.equals("insert") && "reply".equals(sForm))) {
        
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"reply\"><input type=\"hidden\" value=\"\" name=\"FormAction\">");
      }
      
      else {
        out.print("<input type=\"submit\" value=\"Reply\" onclick=\"document.reply.FormAction.value = 'insert';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"reply\"><input type=\"hidden\" value=\"insert\" name=\"FormAction\">");
      }out.print("<input type=\"hidden\" name=\"message_id\" value=\""+toHTML(fldmessage_id)+"\">");out.print("<input type=\"hidden\" name=\"date_entered\" value=\""+toHTML(flddate_entered)+"\">");
      out.print(transitParamsHidden+requiredParams+primaryKeyParams);
      out.println("</td>\n     </tr>\n     </form>\n    </table>");
      



    }
    catch (Exception e) { out.println(e.toString()); }
  } 

  void original_Show (javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String soriginalErr, 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 pmid="";


    formParams = "mid=" + toURL(getParam( request, "mid")) + "&"; 
    // Build WHERE statement
        
    //-- Check mid parameter and create a valid sql for where clause
  
    pmid = getParam( request, "mid");
    if ( ! isNumber (pmid)) {
      pmid = "";
    }
    
    if (pmid != null && ! pmid.equals("")) {
            
        hasParam = true;
        sWhere += "m.message_id=" + pmid;
    }
    
    if (hasParam) { sWhere = " WHERE (" + sWhere + ")"; }

  // 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=\"2\" ><font face=\"arial\" size=\"2\">No records</font></td>\n     </tr>";


  String tableHeader = "";
    
  
  try {
    out.println("    <table border=\"0\" cellspacing=\"1\" cellpadding=\"3\">");
    out.println("     <tr>\n      <td bgcolor=\"#c2c2c2\" colspan=\"2\"><a name=\"original\"><font face=\"arial\" style=\"font:bold\" color=\"#0033cc\">原始文章</font></a></td>\n     </tr>");
    out.println(tableHeader);

  }
  catch (Exception e) {}

  
  try {
    java.sql.ResultSet rs = null;
    // Open recordset
    rs = openrs( stat, sSQL);
    iCounter = 0;
    
    java.util.Hashtable rsHash = new java.util.Hashtable();
    String[] aFields = getFieldsName( rs );

    // Show main table based on recordset
    while ( rs.next() ) {

      getRecordToHash( rs, rsHash, aFields );
      String fldauthor = (String) rsHash.get("m_author");
      String flddate_entered = (String) rsHash.get("m_date_entered");
      String fldmessage = (String) rsHash.get("m_message");
      String fldtopic = (String) rsHash.get("m_topic");
fldmessage = replace(fldmessage,CRLF,"<br>");

      out.print("     <tr>\n      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">作者</font> </td><td >"); out.print("<font face=\"arial\" size=\"2\">"+toHTML(fldauthor)+"&nbsp;</font>");
      out.println("</td>\n     </tr>");
      out.print("     <tr>\n      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">文章标题</font> </td><td >"); out.print("<font face=\"arial\" size=\"2\">"+toHTML(fldtopic)+"&nbsp;</font>");
      out.println("</td>\n     </tr>");
      out.print("     <tr>\n      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">发表日期</font> </td><td >"); out.print("<font face=\"arial\" size=\"2\">"+toHTML(flddate_entered)+"&nbsp;</font>");
      out.println("</td>\n     </tr>");
      out.print("     <tr>\n      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">文章内容</font> </td><td >"); out.print("<font face=\"arial\" size=\"2\">"+fldmessage+"&nbsp;</font>");
      out.println("</td>\n     </tr>");
      out.println("     <tr>\n      <td colspan=\"2\" >&nbsp;</td>\n     </tr>");
    
      iCounter++;
    }
    if (iCounter == 0) {
      // Recordset is empty
      out.println(sNoRecords);
    
      iCounter = RecordsPerPage+1;
      bIsScroll = false;
    }

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