/**
   * Prints the javascripts which will be used to control the new value given to the named field.
   * The error messages may be adapted to a local language. The FieldTemplate gives the field type
   * and constraints. The FieldTemplate gives the local labeld too. Never throws an Exception but
   * log a silvertrace and writes an empty string when :
   *
   * <UL>
   *   <LI>the fieldName is unknown by the template.
   *   <LI>the field type is not a managed type.
   * </UL>
   *
   * @param out
   * @param template
   * @param PagesContext
   * @throws java.io.IOException
   */
  @Override
  public void displayScripts(PrintWriter out, FieldTemplate template, PagesContext PagesContext)
      throws java.io.IOException {
    String language = PagesContext.getLanguage();
    if (!TextField.TYPE.equals(template.getTypeName())) {
      SilverTrace.info(
          "form",
          "TimeFieldDisplayer.displayScripts",
          "form.INFO_NOT_CORRECT_TYPE",
          TextField.TYPE);
    }

    out.println(
        "var "
            + template.getFieldName()
            + "Empty = isWhitespace(stripInitialWhitespace(field.value));");

    if (template.isMandatory() && PagesContext.useMandatory()) {
      out.println("	if (" + template.getFieldName() + "Empty) {");
      out.println(
          "		errorMsg+=\"  - '"
              + template.getLabel(language)
              + "' "
              + Util.getString("GML.MustBeFilled", language)
              + "\\n \";");
      out.println("		errorNb++;");
      out.println("	}");
    }

    out.println(" if (!" + template.getFieldName() + "Empty) {");
    out.println("var reg=new RegExp(\"^([01][0-9]|2[0-3]):([0-5][0-9])$\",\"g\");");
    out.println("if (!reg.test(field.value)) {");
    out.println(
        "		errorMsg+=\"  - '"
            + template.getLabel(language)
            + "' "
            + Util.getString("GML.MustContainsCorrectHour", language)
            + "\\n \";");
    out.println("		errorNb++;");
    out.println("}");
    out.println("}");

    Util.getJavascriptChecker(template.getFieldName(), PagesContext, out);
  }
  @Override
  public void displayScripts(
      final PrintWriter out, final FieldTemplate template, final PagesContext pageContext)
      throws IOException {
    checkFieldType(template.getTypeName(), "VideoFieldDisplayer.displayScripts");
    String language = pageContext.getLanguage();
    String fieldName = template.getFieldName();
    if (template.isMandatory() && pageContext.useMandatory()) {
      out.append("	if (isWhitespace(stripInitialWhitespace(field.value))) {\n")
          .append("		var ")
          .append(fieldName)
          .append("Value = document.getElementById('")
          .append(fieldName)
          .append(Field.FILE_PARAM_NAME_SUFFIX)
          .append("').value;\n")
          .append("   var ")
          .append(fieldName)
          .append("Operation = document.")
          .append(pageContext.getFormName())
          .append(".")
          .append(fieldName)
          .append(OPERATION_KEY)
          .append(".value;\n")
          .append("		if (")
          .append(fieldName)
          .append("Value=='' || ")
          .append(fieldName)
          .append("Operation=='")
          .append(Operation.DELETION.name())
          .append("') {\n")
          .append("			errorMsg+=\"  - '")
          .append(EncodeHelper.javaStringToJsString(template.getLabel(language)))
          .append("' ")
          .append(Util.getString("GML.MustBeFilled", language))
          .append("\\n \";\n")
          .append("			errorNb++;\n")
          .append("		}\n")
          .append("	}\n");
    }

    Util.includeFileNameLengthChecker(template, pageContext, out);
    Util.getJavascriptChecker(template.getFieldName(), pageContext, out);
  }
  /**
   * Prints the HTML layout of the dataRecord using the RecordTemplate to extract labels and extra
   * informations. The value formats may be adapted to a local language. Never throws an Exception
   * but log a silvertrace and writes an empty string when :
   *
   * <UL>
   *   <LI>a field is unknown by the template.
   *   <LI>a field has not the required type.
   * </UL>
   *
   * @param pagesContext
   * @param record
   * @return
   */
  @Override
  public String toString(PagesContext pagesContext, DataRecord record) {
    SilverTrace.info("form", "XmlSearchForm.toString", "root.MSG_GEN_ENTER_METHOD");
    StringWriter sw = new StringWriter();
    String language = pagesContext.getLanguage();
    PrintWriter out = new PrintWriter(sw, true);

    if (pagesContext.getPrintTitle() && title != null && title.length() > 0) {
      out.println("<table CELLPADDING=0 CELLSPACING=2 BORDER=0 WIDTH=\"98%\" CLASS=intfdcolor>");
      out.println("<tr>");
      out.println("<td CLASS=intfdcolor4 NOWRAP>");
      out.println("<table CELLPADDING=0 CELLSPACING=0 BORDER=0 WIDTH=\"100%\">");
      out.println("<tr>");
      out.println("<td class=\"intfdcolor\" nowrap width=\"100%\">");
      out.println(
          "<img border=\"0\" src=\""
              + Util.getIcon("px")
              + "\" width=5><span class=txtNav>"
              + title
              + "</span>");
      out.println("</td>");
      out.println("</tr>");
      out.println("</table>");
      out.println("</td>");
      out.println("</tr>");
      out.println("</table>");
    }
    if (fieldTemplates != null && !fieldTemplates.isEmpty()) {
      Iterator<FieldTemplate> itFields = this.fieldTemplates.iterator();
      if (pagesContext.isBorderPrinted()) {
        out.println(
            "<table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=intfdcolor4>");
        out.println("<tr>");
        out.println("<td nowrap>");
        out.println(
            "<table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" class=\"contourintfdcolor\" width=\"100%\">");
      } else {
        out.println("<table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">");
      }
      out.print("<input TYPE=\"hidden\" NAME=id VALUE=\"");
      out.print(record.getId());
      out.println("\">");
      PagesContext pc = new PagesContext(pagesContext);
      pc.setNbFields(fieldTemplates.size());
      pc.incCurrentFieldIndex(1);

      // calcul lastFieldIndex
      int lastFieldIndex = -1;
      lastFieldIndex += Integer.parseInt(pc.getCurrentFieldIndex());
      FieldDisplayer fieldDisplayer = null;

      while (itFields.hasNext()) {
        FieldTemplate fieldTemplate = itFields.next();
        if (fieldTemplate != null) {
          String fieldType = fieldTemplate.getTypeName();
          String fieldDisplayerName = fieldTemplate.getDisplayerName();
          try {
            if (!StringUtil.isDefined(fieldDisplayerName)) {
              fieldDisplayerName = getTypeManager().getDisplayerName(fieldType);
            }

            fieldDisplayer = getTypeManager().getDisplayer(fieldType, fieldDisplayerName);
            if (fieldDisplayer != null) {
              lastFieldIndex += fieldDisplayer.getNbHtmlObjectsDisplayed(fieldTemplate, pc);
            }
          } catch (FormException fe) {
            SilverTrace.error(
                "form", "XmlSearchForm.toString", "form.EXP_UNKNOWN_DISPLAYER", null, fe);
          }
        }
      }
      pc.setLastFieldIndex(lastFieldIndex);

      itFields = this.fieldTemplates.iterator();
      while (itFields.hasNext()) {
        FieldTemplate fieldTemplate = itFields.next();
        if (fieldTemplate != null) {
          String fieldName = fieldTemplate.getFieldName();
          String fieldLabel = fieldTemplate.getLabel(language);
          String fieldType = fieldTemplate.getTypeName();
          String fieldDisplayerName = fieldTemplate.getDisplayerName();
          try {
            if (!StringUtil.isDefined(fieldDisplayerName)) {
              fieldDisplayerName = getTypeManager().getDisplayerName(fieldType);
            }

            fieldDisplayer = getTypeManager().getDisplayer(fieldType, fieldDisplayerName);
          } catch (FormException fe) {
            SilverTrace.error(
                "form", "XmlSearchForm.toString", "form.EXP_UNKNOWN_DISPLAYER", null, fe);
          }
          if (fieldDisplayer != null) {
            out.println("<tr align=center>");
            if (StringUtil.isDefined(fieldLabel)) {
              out.println("<td class=\"txtlibform\" align=left width=\"200\">");
              out.println(fieldLabel);
              out.println("</td>");
            }
            out.println("<td valign=\"baseline\" align=left>");
            try {
              fieldDisplayer.display(out, record.getField(fieldName), fieldTemplate, pc);
            } catch (FormException fe) {
              SilverTrace.error(
                  "form", "XmlSearchForm.toString", "form.EX_CANT_GET_FORM", null, fe);
            }
            out.println("</td>");
            out.println("</tr>");
            ;
            pc.incCurrentFieldIndex(fieldDisplayer.getNbHtmlObjectsDisplayed(fieldTemplate, pc));
          }
        }
      }
      if (pagesContext.isBorderPrinted()) {
        out.println("</TABLE>");
        out.println("</TD>");
        out.println("</TR>");
      }
      out.println("</TABLE>");
    }
    return sw.getBuffer().toString();
  }