import javax.servlet.http.*;

public class ErrorPage extends Object {
  public static final int UNKNOWN = 0;
  public static final int QUOTA_EXCEEDED = 1;
  public static final String[] errMsg = { "Unknown error.",
					  "You have exceeded your quota." };

  public static StringBuffer doPage(HttpServletRequest req) {
    String errNum;
    int errCode;
    try {
      errNum = Util.getQueryValue(req, "errorCode");
      errCode = Integer.parseInt(errNum);
    }
    catch (InternalException g) {
      return printPage(UNKNOWN);
    }
    catch (NumberFormatException nf) {
      return printPage(UNKNOWN);
    }
    return printPage(errCode);
  }

  public static StringBuffer doPage(String msg) {
    return printPage(msg);
  }

  public static StringBuffer doPage(int ecode) {
    return printPage(ecode);
  }

  protected static StringBuffer printPage(int ecode) {
    try {
      return printPage(errMsg[ecode]);
    }
    catch (ArrayIndexOutOfBoundsException aioob) {
      return printPage(errMsg[UNKNOWN]);
    }
  }

  protected static StringBuffer printPage(String errorMsg) {
    StringBuffer retbuf = new StringBuffer();

    retbuf.append("<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">\n");
    retbuf.append("<html>\n");
    retbuf.append("<head>\n");
    retbuf.append("<title>MIT Sloan . Search</title>\n");
    retbuf.append("</head>\n");
    retbuf.append("\n");
    retbuf.append("<body bgcolor=\"#000000\" text=\"#000000\" link=\"#990000\" alink=\"#660000\" vlink=\"#330000\" marginwidth=\"0\" marginheight=\"0\" topmargin=\"0\" leftmargin=\"0\">\n");
    retbuf.append("\n");
    retbuf.append("<table border=0 cellpadding=\"0\" cellspacing=\"0\" width=\"638\" height=\"100%\">\n");
    retbuf.append("<tr valign=\"top\">\n");
    retbuf.append("        <td width=\"97\" bgcolor=\"#ffffff\">\n");
    retbuf.append("<!-- begin left navigation -->\n");
    retbuf.append("        <a href=\"../../index.html\" target=\"_top\"><img src=\"../../images/nav/logo.gif\" alt=\"MIT Sloan\" width=\"97\" height=\"92\" border=\"0\"></a><br>\n");
    retbuf.append("        <a href=\"../../visit/main.html\"><img src=\"../../images/nav/visitors.gif\" alt=\"Visitors\' Center\" width=\"97\" height=\"32\" border=\"0\"></a><br>\n");
    retbuf.append("        <a href=\"../../news/main.html\"><img src=\"../../images/nav/news.gif\" alt=\"News Room\" width=\"97\" height=\"31\" border=\"0\"></a><br>\n");
    retbuf.append("        <a href=\"../../worldwide/main.html\"><img src=\"../../images/nav/sloanworld.gif\" alt=\"Sloan Worldwide\" width=\"97\" height=\"31\" border=\"0\"></a><br>\n");
    retbuf.append("        <a href=\"../../research/main.html\"><img src=\"../../images/nav/research.gif\" alt=\"Research Center\" width=\"97\" height=\"31\" border=\"0\"></a><br>\n");
    retbuf.append("        <a href=\"../../cdo/main.html\"><img src=\"../../images/nav/career.gif\" alt=\"Career Center\" width=\"97\" height=\"31\" border=\"0\"></a><br>\n");
    retbuf.append("        <a href=\"../../virtual/main.html\"><img src=\"../../images/nav/virtual.gif\" alt=\"Virtual Campus\" width=\"97\" height=\"31\" border=\"0\"></a><br>\n");
    retbuf.append("        <a href=\"../../programs/main.html\"><img src=\"../../images/nav/academic.gif\" alt=\"Academic Programs\" width=\"97\" height=\"31\" border=\"0\"></a><br>\n");
    retbuf.append("        <a href=\"../../application/main.html\"><img src=\"../../images/nav/application.gif\" alt=\"Application Center\" width=\"97\" height=\"31\" border=\"0\"></a><br>\n");
    retbuf.append("        <a href=\"\"><img src=\"../../images/nav/events.gif\" alt=\"Events Calendar\" width=\"97\" height=\"31\" border=\"0\"></a><br>\n");
    retbuf.append("        <a href=\"http://www.mit.edu\" target=\"_top\"><img src=\"../../images/nav/mit.gif\" alt=\"MIT\" width=\"97\" height=\"31\" border=\"0\"></a><br>\n");
    retbuf.append("        <img src=\"../../images/nav/bottom.gif\" width=\"97\" height=\"23\" border=\"0\"><br>\n");
    retbuf.append("<!-- end left navigation -->\n");
    retbuf.append("        </td>\n");
    retbuf.append("        <td width=\"1\" bgcolor=\"#ffffff\"><img src=\"../../images/kdot.gif\" width=\"1\" height=\"1\" border=\"0\"></td>\n");
    retbuf.append("        <td width=\"516\" bgcolor=\"#ffffff\">\n");
    retbuf.append("<!-- begin main content channel -->\n");

    retbuf.append("<b>The Search Servlet has encountered an error:</b>\n");
    retbuf.append("<p>" + errorMsg + "\n");

    retbuf.append("<!-- end main content channel -->\n");
    retbuf.append("        </td>\n");
    retbuf.append("        <td width=\"1\" bgcolor=\"#000000\"><img src=\"../../images/kdot.gif\" width=\"1\" height=\"1\" border=\"0\"></td>\n");
    retbuf.append("        <td bgcolor=\"#999999\" width=\"23\">\n");
    retbuf.append("<!-- begin sidebar -->\n");
    retbuf.append("        <img src=\"../../images/spacer.gif\" width=\"23\" height=\"1\" border=\"0\">\n");
    retbuf.append("<!-- end sidebar -->\n");
    retbuf.append("        </td>\n");
    retbuf.append("</tr>\n");
    retbuf.append("</table>\n");
    retbuf.append("\n");
    retbuf.append("                \n");
    retbuf.append("</body>\n");
    retbuf.append("</html>\n");

    return retbuf;
  }
}
