<?php

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

# Call common libraries
require_once('./lib/inc-common.php');
require_once(FS_PATH."/etc/locale/".LANG_PACK."/inc-prefs.php");

$NETJUKE_THEME_LIST = load_themes ();

if (USER_THEMES == 'f' && BATCH_DOWNLOAD == 'f') {
  header ("Location: ".WEB_PATH."/index.php\n\n");
  exit;
}

if (!$_REQUEST['do']) {

  header ("Location: ".WEB_PATH."/index.php\n\n");

  exit;

}  elseif ($_REQUEST['do'] == "edit") {

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

     $theme = $NETJUKE_THEME_LIST[$_REQUEST['themeField']];
     $theme->resetSettings();
     $theme->saveUserSettings($_REQUEST['themeField'], $NETJUKE_SESSION_VARS["email"]);

  } else

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

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

    $sql = "UPDATE netjuke_userprefs SET ";

    if (USER_THEMES == 't') {

          $theme = $NETJUKE_THEME_LIST[$_REQUEST['themeField']];

          // if new theme selected set suggested icon_set/inv settings
          if ($_REQUEST['themeField'] != $NETJUKE_SESSION_VARS["theme"]) {

            if ($theme->getMetaData('icon_invert') != '') {
              $_REQUEST['inv_icnField'] = $theme->getMetaData('icon_invert');
            }

            if ($theme->getMetaData('icon_set') != '') {
              $_REQUEST['icon_setField'] = $theme->getMetaData('icon_set');
            }

          // if the theme is old / update changed theme-settings
          } else {

            foreach ($theme->getDefaultSettings() as $id => $option) {

              ensure_defined_REQUEST($id.'Field');
              $theme->setSetting($id, $_REQUEST[$id.'Field']);

            }
            $theme->saveUserSettings($_REQUEST['themeField'], $NETJUKE_SESSION_VARS["email"]);

          }

          $sql .= " inv_icn = '".$_REQUEST['inv_icnField']."' "
               ." , icon_set = '".$_REQUEST['icon_setField']."' "
               ." , theme = '".$_REQUEST['themeField']."' ";

    }

    // Pull in the language selection
    $sql .= " , language = '".$_REQUEST['languageField']."' ";

    $sql .= " WHERE us_email = '".$NETJUKE_SESSION_VARS["email"]."'";

    if ($dbconn->Execute($sql) === false) {

      javascript ("alert('".PREFS_DENIED_1."');");

    } else {

      if (USER_THEMES == 't') {

          $NETJUKE_SESSION_VARS["inv_icn"] = $_REQUEST['inv_icnField'];
          $NETJUKE_SESSION_VARS["icon_set"] = $_REQUEST['icon_setField'];
          $NETJUKE_SESSION_VARS["theme"] = $_REQUEST['themeField'];

      }

      if (BATCH_DOWNLOAD == 't') {

        $NETJUKE_SESSION_VARS["batch_type"] = $_REQUEST['batch_typeField'];

      }

      $NETJUKE_SESSION_VARS["language"] = $_REQUEST['languageField'];

      netjuke_session('update');

    }

  }

  if (strtolower(substr($NETJUKE_SESSION_VARS["inv_icn"],0,1)) == 't') $inv_icnChecked = 'CHECKED';
  else $inv_icnChecked = '';

  $section = "prefs";
  include (INTERFACE_HEADER);
  if (USER_THEMES == 't')
    $current_theme = $NETJUKE_SESSION_VARS["theme"];
#  else
#   $current_theme = DEFAULT_THEME;

?>

          <script language='Javascript'>
            function checkForm () {
              var msg = '';

              <?php if (USER_THEMES == 't') {

                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 + '- ".PREFS_CHECKCOLOR." ".$option['title']."'";
                    echo "\t\t}\n";
                  }
                }
              } // if (USER_THEMES == 't') ?>
              if (msg == '') {
                return (true);
              } else {
                alert(msg);
                return (false);
              }
            }

            function theme_info () {
              var msg = '';

              msg = '<?php echo $NETJUKE_THEME->getMetadata ('name')." ".$NETJUKE_THEME->getMetadata ('version').'\n'.
                    $NETJUKE_THEME->getMetadata ('author').'\n'.$NETJUKE_THEME->getMetadata ('description') ?>';

              alert (msg);
            }
          </script>
          <div align=center>
          <table width="400" border="0" cellspacing="1" cellpadding="3" class="border">
          <form action="prefs.php" method=post name='prefsForm' onSubmit='return checkForm ();'>
          <input type="hidden" name="do" value="edit">
          <input type="hidden" name="save" value="1">
          <tr>
            <td class="header" nowrap><B><?php echo  PREFS_HEADER ?></B></td>
          </tr>
        <tr>
            <td class="content" nowrap align=center>
              <table border=0 cellspacing=0 cellpadding=2>
              <tr>
                <td align=center wrap="virtual" colspan=3>
                  <?php echo PREFS_CAPTION ?>
                </td>
              </tr>
              <tr><td>&nbsp;</td></tr>

             <?php if (USER_THEMES == 't') { ?>

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

              <tr>
                <td align="right" nowrap><?php echo  PREFS_FORMS_THEMES ?></td>
                <td align="left" colspan="2" nowrap >
                  <?php echo themes_menu ("themeField", $current_theme, "javascript:prefsForm.submit();", ''); ?>
                  [<b><a onclick="javascript:theme_info();" title="<?php echo PREFS_FORMS_THEME_INFO ?>">?</a></b>]
                </td>
              </tr>

              <tr>
                <td align="right" nowrap><?php echo PREFS_FORMS_ICONS ?></td>
                <td align="left" nowrap><?php echo icon_set_menu ("icon_setField", $NETJUKE_SESSION_VARS["icon_set"], "javascript:prefsForm.submit();"); ?></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  PREFS_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 PREFS_FORMS_INVICN_HELP ?>"> <?php echo  PREFS_FORMS_INVICN_ENABLED ?>
                </td>
              </tr>

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

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

              <?php

                foreach ($NETJUKE_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 $NETJUKE_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 $NETJUKE_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 $NETJUKE_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  PREFS_PALETTE ?></B></A>
                </td>
              </tr>
              <?php

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

          } // if (USER_THEMES == 't')

	      if (BATCH_DOWNLOAD == 'XXXX') { ?>
				<tr>
          <td class="subheader" colspan="3">
            <div class="subheader"><?php echo PREFS_FORMS_ADDITIONAL ?></div>
          </td>
        </tr>	      
	      <?php } ?>
	      <tr>
      		<td align=left nowrap colspan=2>&nbsp;</td>
    	      </tr>
              <tr>
                <td align="center" nowrap colspan="3">
                  <input type="submit" value="<?php echo  PREFS_FORMS_BTN_SAVE ?>" class="btn_content">
                  <input type="submit" name="reset" value="<?php echo  PREFS_FORMS_BTN_RESET ?>" class="btn_content">
                  </td>
              </tr>
              </table>
            </td>
          </tr>
          </form>
          </table>
          </div>

<?php

  include (INTERFACE_FOOTER);

  exit;

} else {

  header ("Location: ".WEB_PATH."/index.php\n\n");

  exit;

}

?>
