<?php

// defines if this script requires to be logged in
define( "PRIVATE", true );

require_once('../lib/inc-admin.php');
require_once(FS_PATH."/etc/locale/".LANG_PACK."/inc-admin_prefs-edit.php");

$pref_file = FS_PATH.'/etc/inc-prefs.php';
ensure_defined_REQUEST('do');

$NETJUKE_THEME_LIST = load_themes ();

$themename = DEFAULT_THEME;

$theme = $NETJUKE_THEME_LIST[$themename];
if (! is_object ($theme))
  $theme = $NETJUKE_THEME_LIST['artekopia'];

$theme->readUserSettings($themename, '');

if ($_REQUEST['do'] == 'save') {

  if (isset($_REQUEST['reset'])) {

    if ($_REQUEST['themeField'] == $themename) {
      $theme->resetSettings();
      $theme->saveUserSettings($themename);
    }
    header("Location: ".$_SERVER['PHP_SELF']);

  } else {

    ensure_defined_REQUEST('user_themesField', 'f');
    ensure_defined_REQUEST('inv_icnField', 'f');

    // only update colors/fonts if theme hasn't changed
    if ($_REQUEST['themeField'] == $themename) {

      foreach ($theme->getDefaultSettings() as $id => $option) {
        ensure_defined_REQUEST($id.'Field');
        $theme->setSetting($id, $_REQUEST[$id.'Field']);
      }

      $theme->saveUserSettings($themename);
    }

    // save all the other settings to the preferences file
    $data = array(   'LANG_PACK' => $_REQUEST['lang_packField']
                   , 'INTERFACE_HEADER' => FS_PATH."/lib/inc-header.php"
                   , 'INTERFACE_FOOTER' => FS_PATH."/lib/inc-footer.php"
                   , 'CUSTOM_HEADER' => separatorCleanup($_REQUEST['custom_headerField'])
                   , 'CUSTOM_FOOTER' => separatorCleanup($_REQUEST['custom_footerField'])
                   , 'USER_THEMES' => $_REQUEST['user_themesField']
                   , 'INV_ICN' => $_REQUEST['inv_icnField']
                   , 'DEFAULT_ICON_SET' => $_REQUEST['icon_setField']
                   , 'DEFAULT_THEME' => $_REQUEST['themeField']);

    ensure_defined_REQUEST('save_to_fileField', 0);
    SavePrefFile($pref_file, $_REQUEST['save_to_fileField'], $data);
    header ("Location: ".$PHP_SELF);
  }
}
else {

  $section = "sysadmin";
  include (INTERFACE_HEADER);

  PrefsTabs ('appearance');

  $user_themesChecked = '';
  $inv_icnChecked = '';

  if (@is_writable($pref_file)) $write_check = 'checked';
  else $write_check = '';

  if (strtolower(substr(USER_THEMES,0,1)) == 't') $user_themesChecked = 'CHECKED';

  if (strtolower(substr(INV_ICN,0,1)) == 't') $inv_icnChecked = 'CHECKED';

?>

<script language='Javascript'>

  function security_note() {

     var myalert = "";

     myalert += "<?php echo  ADMPREF_FORMS_SAVETOFILE_HELP_1 ?>";

     alert(myalert);

  }

  function checkForm () {

    var msg = '';

    <?php
      foreach ($NETJUKE_THEME->getDefaultSettings() as $id => $option) {
        if ($option['type']=='color') {
          echo "\t\tif (document.prefsForm.".$id."Field.value.length < 6) {\n";
          echo "\t\t  msg = msg + '- ".ADMPREF_CHECKCOLOR." ".$option['title']."'";
          echo "\t\t}\n";
        }
      }
    ?>

    if (msg == '') {

      return (true);

    } else {

      alert(msg);

      return (false);

    }

  }

  function theme_info () {
    var msg = '';

    <?php $mytheme = $NETJUKE_THEME_LIST[DEFAULT_THEME]; ?>
    msg = '<?php echo $mytheme->getMetadata ('name')." ".$mytheme->getMetadata ('version').'\n'.
          $theme->getMetadata ('author').'\n'.$mytheme->getMetadata ('description') ?>';

    alert (msg);
  }
</script>

<div align=center>

<form action='<?php echo $_SERVER['PHP_SELF']?>' method=post name='prefsForm' target="_self" onSubmit='return checkForm ();'>
<input type=hidden name="do" value="save">

<table width='550' border="0" cellspacing="1" cellpadding="3" class="border">
<tr>
  <td class='header' nowrap><B><?php echo  ADMPREF_HEADER_4 ?></B></td>
</tr>
<tr>
  <td class='content' nowrap align=center>
    <table border="0" cellspacing="0" cellpadding="2">
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td width="30%" align=right nowrap><?php echo  ADMPREF_FORMS_SAVETOFILE ?></td>
      <td width="70%" align=left nowrap>
        <input type="checkbox" name="save_to_fileField" value="1" <?php echo $write_check?>> <?php echo  ADMPREF_FORMS_CAPT_ENABLED ?>
        &nbsp; (<a href="javascript:security_note();"><?php echo  ADMPREF_FORMS_SAVETOFILE_HELP_2 ?></a>)
      </td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_LANGPACK ?></td>
      <td align=left nowrap><?php echo  lang_pack_menu("lang_packField", LANG_PACK) ?></td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_HTMLHEAD ?></td>
      <td align=left nowrap><input type=text name='custom_headerField' size='50' maxlength='256' value='<?php echo CUSTOM_HEADER?>' class=input_content title="/path/file.html  -or-  http://your.host.dom/path/file.html"></td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_HTMLFOOT ?></td>
      <td align=left nowrap><input type=text name='custom_footerField' size='50' maxlength='256' value='<?php echo CUSTOM_FOOTER?>' class=input_content title="/path/file.html  -or-  http://your.host.dom/path/file.html"></td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2">
        <table border="0"  cellspacing="0" cellpadding="2">
              <tr>
                <td class="subheader" colspan="3">
                  <div class="subheader"><?php echo ADMPREF_FORMS_GENERAL ?></div>
                </td>
              </tr>

              <tr>
                <td align="right" nowrap><?php echo  ADMPREF_FORMS_THEME ?></td>
                <td align="left" colspan="2" nowrap >
                  <?php echo themes_menu ("themeField", $themename, '', ''); ?>
                  [<b><a onclick="javascript:theme_info();" title="<?php echo ADMPREFS_FORMS_THEME_INFO ?>">?</a></b>]
                </td>
              </tr>

	            <tr>
                <td align="right" nowrap><?php echo  ADMPREF_FORMS_THEMES ?></td>
                <td align="left" colspan="2" nowrap>
          	      <input type=checkbox name='user_themesField' value='t' <?php echo $user_themesChecked?> title="<?php echo  ADMPREF_FORMS_THEMES_HELP ?>"> <?php echo  ADMPREF_FORMS_CAPT_ENABLED ?>
                </td>
              </tr>

              <tr>
                <td align="right" nowrap><?php echo ADMPREF_FORMS_ICONS ?></td>
                <td align="left" nowrap><?php echo icon_set_menu ("icon_setField", DEFAULT_ICON_SET, ''); ?></td>
                <td align="center" nowrap colspan=5>
                    <TABLE BORDER="1" WIDTH="100" CELLSPACING="0" CELLPADDING="4"><TR><TD><?php echo show_example_icons (); ?></TD></TR></TABLE>
                </td>
              </tr>

              <tr>
                <td width="40%" align="right" nowrap><?php echo  ADMPREF_FORMS_INVICN ?></td>
                <td width="60%" align="left" valign="top" colspan="2">
                	<input type=checkbox name='inv_icnField' value='t' <?php echo $inv_icnChecked?> title="<?php echo ADMPREF_FORMS_INVICN_HELP ?>">
                </td>
              </tr>

              <?php

                // does the current theme provide any settings?
                if ($theme->getDefaultSettingsCount() > 0) {
              ?>

              <tr>
                <td class="subheader" colspan="3">
                  <div class="subheader"><?php echo ADMPREF_FORMS_DETAILS ?></div>
                </td>
              </tr>

              <?php

                foreach ($theme->getDefaultSettings() as $id => $option) {
                  echo "<tr>\n";

                  echo '<td align="right" nowrap>'.$option['title']."</td>";

                  ?><?php
                  switch ($option['type']) {

                    case 'text' :
                      ?><td align="left" nowrap colspan="2">
                        <input type="text" name="<?php echo $id ?>Field" size="<?php echo $option['size']?>" value="<?php echo $theme->settings[$id]?>" class="input_content">
                      </td><?php
                      break;

                    case 'color' :
                      ?>
                      <td align=left nowrap>
                        <input type="text" name="<?php echo $id ?>Field" size="8" maxlength="6" value="<?php echo $theme->settings[$id]?>" class="input_content" onKeyUp="javascript:set_cell_color('<?php echo $id ?>Sample', document.prefsForm.<?php echo $id ?>Field.value);">
                      </td>
                      <td>
                        <table border="1" width="40" cellspacing="0" cellpadding="0">
                          <tr>
                            <td id="<?php echo $id ?>Sample" bgcolor="<?php echo $theme->settings[$id]?>">&nbsp;</td>
                          </tr>
                        </table>
                      </td>

                      <?php
                      break;
                  }

                  echo "</tr>\n";
                }

              ?>
              <tr>
                <td align=center wrap="virtual" colspan=3>
                  <A HREF="<?php echo  WEB_PATH ?>/palette.php" target="NetJukePalette" onClick="window.open('','NetJukePalette','width=640,height=575,top=0,left=0,menubar=no,scrollbars=yes,resizable=yes');"><B><?php echo ADMPREF_PALETTE ?></B></A>
                </td>
              </tr>
              <?php

            } // if ($NETJUKE_THEME->getDefaultSettingsCount() > 0)
            ?>

        </table>
      </td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td align=center nowrap colspan=2>
        <input type=submit value='<?php echo  ADMPREF_FORMS_BTN_SAVE ?>' class='btn_content'>
        <input type=submit name="reset" value='<?php echo  ADMPREF_FORMS_BTN_RESET ?>' class='btn_content'>
      </td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    </table>

</tr>
</table>

</form>

<?php

  include (INTERFACE_FOOTER);

}

exit;

?>
