{extends "manage/manage.tpl.html"}

{function name="rank_icon" direction="down" href=""}
  {include file="include/rank_icon.tpl.html" direction=$direction href=$href}
{/function}

{block "manage_content"}
  <script type="text/javascript">
  <!--
  var url = '{$core.rel_url}manage/faq.php';
  var id = {$smarty.get.id|intval|default:''};

  function populateComboBox()
  {
      if (id == '') {
          url += '?prj_id=' + Eventum.getField('project').val();
      } else {
          url += '?cat=edit&id=' + id + '&prj_id=' + getSelectedOption(f, 'project');
      }
      window.location.href = url;
  }
  function validateForm()
  {
      if (Eventum.getField('project').val() == -1) {
          Validation.selectField('project');
          alert('{t escape=js}Please choose the project for this FAQ entry.{/t}');
          return false;
      }
      var field = Eventum.getField('support_levels[]');
      if (field.length > 0) {
          if (!Validation.hasOneSelected('support_levels[]')) {
              Validation.selectField('support_levels[]');
              alert('{t escape=js}Please assign the appropriate support levels for this FAQ entry.{/t}');
              return false;
          }
      }
      var rank = Eventum.getField("rank").val();
      if (Validation.isWhitespace(rank)) {
          Validation.selectField('rank');
          alert('{t escape=js}Please enter the rank of this FAQ entry.{/t}');
          return false;
      }
      if (!Validation.isNumberOnly(rank)) {
          Validation.selectField('rank');
          alert('{t escape=js}Please enter a number for the rank of this FAQ entry.{/t}');
          return false;
      }
      if (Validation.isFieldWhitespace('title')) {
          Validation.selectField('title');
          alert('{t escape=js}Please enter the title of this FAQ entry.{/t}');
          return false;
      }
      if (Validation.isFieldWhitespace('message')) {
          Validation.selectField('message');
          alert('{t escape=js}Please enter the content of this FAQ entry.{/t}');
          return false;
      }
      return true;
  }
  function checkDelete(f)
  {
      if (!Validation.hasOneChecked('items[]')) {
          alert('{t escape=js}Please select at least one of the FAQ entries.{/t}');
          return false;
      }
      if (!confirm('{t escape=js}This action will permanently remove the selected FAQ entries.{/t}')) {
          return false;
      } else {
          return true;
      }
  }
  $().ready(function() {
      $('#faq_form').submit(validateForm);
      $('.select_all').click(function() { Eventum.toggleCheckAll('items[]'); });

      $('select[name=project]').change(populateComboBox);

      $('#delete_form').submit(checkDelete);
  });
  //-->
  </script>

  <form id="faq_form" method="post">
  {if $smarty.get.cat|default:'' == 'edit'}
  <input type="hidden" name="cat" value="update">
  <input type="hidden" name="id" value="{$smarty.get.id|intval}">
  {else}
  <input type="hidden" name="cat" value="new">
  {/if}
    <table class="bordered">
      <tr class="title">
        <th colspan="2">
          {t}Manage Internal FAQ{/t}
        </th>
      </tr>
      <tr>
        <th width="150">
          {t}Project{/t}
        </th>
        <td width="80%">
          <select name="project">
            <option value="-1">{t}Please choose an option{/t}</option>
            {html_options options=$project_list|default:'' selected=$info.faq_prj_id|default:''}
          </select>
          {include file="error_icon.tpl.html" field="project"}
        </td>
      </tr>
      {if $support_levels|default:'' != ''}
      <tr>
        <th width="140">
          <nobr>{t}Assigned Support{/t}<br />{t}Levels{/t}</nobr>
        </th>
        <td>
          <select name="support_levels[]" size="4" multiple>
            {html_options options=$support_levels selected=$info.support_levels}
          </select>
          {include file="error_icon.tpl.html" field="support_levels[]"}
        </td>
      </tr>
      {/if}
      <tr>
        <th width="140">
          {t}Rank{/t}
        </th>
        <td>
          <input type="text" size="5" name="rank" value="{$info.faq_rank|default:''}">
          {include file="error_icon.tpl.html" field="rank"}
        </td>
      </tr>
      <tr>
        <th width="140">
          {t}Title{/t}
        </th>
        <td>
          <input type="text" name="title" size="40" value="{$info.faq_title|escape:"html"|default:''}">
          {include file="error_icon.tpl.html" field="title"}
        </td>
      </tr>
      <tr>
        <th width="140">
          {t}Message{/t}
        </th>
        <td>
          {include file="include/textarea.tpl.html"
            content=$info.faq_message|default:''
            id="message"
            rows="10"
            cols="50"
          }
        </td>
      </tr>
      <tr class="buttons">
        <td colspan="2">
          {if $smarty.get.cat|default:'' == 'edit'}
          <input type="submit" value="{t}Update FAQ Entry{/t}">
          {else}
          <input type="submit" value="{t}Create FAQ Entry{/t}">
          {/if}
          <input type="reset" value="{t}Reset{/t}">
        </td>
      </tr>
    </table>
  </form>

  <form id="delete_form" method="post">
  <input type="hidden" name="cat" value="delete">
  <table class="bordered grid">
      <tr class="title">
        <th colspan="4">
          {t}Existing Internal FAQ Entries{/t}
        </th>
      </tr>
        <tr>
          <th width="4" nowrap><input type="button" value="{t}All{/t}" class="select_all"></th>
          <th align="center">&nbsp;{t}Rank{/t}&nbsp;</th>
          <th width="{if $backend_uses_support_levels|default:''}50%{else}100%{/if}">&nbsp;{t}Title{/t}</th>
          {if $backend_uses_support_levels|default:''}
          <th width="50%">&nbsp;{t}Support Levels{/t}</th>
          {/if}
        </tr>
        {section name="i" loop=$list}
        <tr class="{cycle values="odd,even"}">
          <td width="4" nowrap align="center"><input type="checkbox" name="items[]" value="{$list[i].faq_id}"></td>
          <td align="center" nowrap>
            {rank_icon href="{$core.rel_url}manage/faq.php?cat=change_rank&id={$list[i].faq_id}&rank=desc" direction="down"}
            {$list[i].faq_rank}
            {rank_icon href="{$core.rel_url}manage/faq.php?cat=change_rank&id={$list[i].faq_id}&rank=asc" direction="up"}
          </td>
          <td width="50%">
            &nbsp;<a href="{$core.rel_url}manage/faq.php?cat=edit&id={$list[i].faq_id}" title="{t}update this entry{/t}">{$list[i].faq_title|escape:"html"}</a>
          </td>
          {if $backend_uses_support_levels|default:''}
          <td width="50%">
            &nbsp;{$list[i].support_levels|escape:"html"}
          </td>
          {/if}
        </tr>
        {sectionelse}
        <tr class="no_results">
          <td colspan="{if $backend_uses_support_levels|default:''}4{else}3{/if}">
            {t}No FAQ entries could be found.{/t}
          </td>
        </tr>
        {/section}
        <tr class="buttons">
          <td width="4" align="center">
            <input type="button" value="{t}All{/t}" class="select_all">
          </td>
          <td colspan="{if $backend_uses_support_levels|default:''}3{else}2{/if}">
            <input type="submit" value="{t}Delete{/t}">
          </td>
        </tr>
    </table>
    </form>
{/block}
