<?php

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

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

// Make sure everything is defined properly
ensure_defined_REQUEST('filter');
ensure_defined_REQUEST('sort');
ensure_defined_REQUEST('val');
ensure_defined_REQUEST('col');
ensure_defined_REQUEST('page', 1);
ensure_defined_REQUEST('first', 1);

$clause_enc = '';
$ars = '';
$ges = '';
$trs = '';
$als = '';
$tis = '';
$prev_val = '';

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

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

}  elseif ( (substr($_REQUEST['do'],0,4) == 'list') || (substr($_REQUEST['do'],0,6) == 'search') ) {
########################################
# LIST OR SEARCH TRACKS
########################################

   if (strlen($_REQUEST['col']) < 1) $_REQUEST['col'] = 'tr.id';

   $valurl = rawurlencode($_REQUEST['val']);

   if (substr($_REQUEST['do'],0,5) == "list.") {

      // TRACK LISTING

      if (strlen($_REQUEST['val']) < 1) $_REQUEST['val'] = '1';

      $clause = $_REQUEST['col']." = '".$_REQUEST['val']."'";

   } else {

      if ($_REQUEST['do'] == 'search.adv') {

      // ADVANCED SEARCH

         if (!isset($_REQUEST['clause'])) {

         // QUERY FROM ADVANCED SEARCH FORM

            $clause_array = array();

            // LIKE STATEMENTS

            if (strlen($_REQUEST['tr_name']) > 0) {
               $clause_array[] = "upper(tr.name) like '%".strtoupper(raw_to_db($_REQUEST['tr_name']))."%'";
            }

            if (strlen($_REQUEST['ar_name']) > 0) {
               $clause_array[] = "upper(ar.name) like '%".strtoupper(raw_to_db($_REQUEST['ar_name']))."%'";
            }

            if (strlen($_REQUEST['al_name']) > 0) {
               $clause_array[] = "upper(al.name) like '%".strtoupper(raw_to_db($_REQUEST['al_name']))."%'";
            }

            if (strlen($_REQUEST['ge_name']) > 0) {
               $clause_array[] = "upper(ge.name) like '%".strtoupper(raw_to_db($_REQUEST['ge_name']))."%'";
            }

            if (strlen($_REQUEST['tr_location']) > 0) {
               $clause_array[] = "upper(tr.location) like '%".strtoupper(specialUrlEncode($_REQUEST['tr_location']))."%'";
            }

            if (strlen($_REQUEST['tr_comments']) > 0) {
               $clause_array[] = "upper(tr.comments) like '%".rawurlencode(strtoupper($_REQUEST['tr_comments']))."%'";
            }

            if (strlen($_REQUEST['tr_lyrics']) > 0) {
               $clause_array[] = "upper(tr.lyrics) like '%".rawurlencode(strtoupper($_REQUEST['tr_lyrics']))."%'";
            }

            if (strlen($_REQUEST['tr_license']) > 0) {
               $clause_array[] = "upper(tr.license) like '%".rawurlencode(strtoupper($_REQUEST['tr_license']))."%'";
            }

            // EQUAL STATEMENTS

            if (strlen($_REQUEST['tr_kind']) > 0) {
               $clause_array[] = "tr.kind = '".$_REQUEST['tr_kind']."'";
            }

            // DYNAMIC OPERATOR STATEMENTS

            if ( (strlen($_REQUEST['tr_time']) > 0) && (is_numeric($_REQUEST['tr_time'])) ) {
               $clause_array[] = "tr.time ".$_REQUEST['tr_time_op']." ".$_REQUEST['tr_time'];
            }

            if ( (strlen($_REQUEST['tr_track_number']) > 0) && (is_numeric($_REQUEST['tr_track_number'])) ) {
               $clause_array[] = "tr.track_number ".$_REQUEST['tr_track_number_op']." ".$_REQUEST['tr_track_number'];
            }

            if ( (strlen($_REQUEST['tr_bit_rate']) > 0) && (is_numeric($_REQUEST['tr_bit_rate'])) ) {
               $clause_array[] = "tr.bit_rate ".$_REQUEST['tr_bit_rate_op']." ".$_REQUEST['tr_bit_rate'];
            }

            if ( (strlen($_REQUEST['tr_sample_rate']) > 0) && (is_numeric($_REQUEST['tr_sample_rate'])) ) {
               $clause_array[] = "tr.sample_rate ".$_REQUEST['tr_sample_rate_op']." ".$_REQUEST['tr_sample_rate'];
            }

            if ( (strlen($_REQUEST['tr_size']) > 0) && (is_numeric($_REQUEST['tr_size'])) ) {
               $clause_array[] = "tr.size ".$_REQUEST['tr_size_op']." ".$_REQUEST['tr_size'];
            }

            if ( (strlen($_REQUEST['tr_dl_cnt']) > 0) && (is_numeric($_REQUEST['tr_dl_cnt'])) ) {
               $clause_array[] = "tr.dl_cnt ".$_REQUEST['tr_dl_cnt_op']." ".$_REQUEST['tr_dl_cnt'];
            }

            if ( (strlen($_REQUEST['tr_year']) > 0) && (is_numeric($_REQUEST['tr_year'])) ) {
               $clause_array[] = "tr.year ".$_REQUEST['tr_year_op']." ".$_REQUEST['tr_year'];
            }

            if ( (strlen($_REQUEST['tr_added']) > 0) ) {
               $clause_array[] = "tr.date ".$_REQUEST['tr_added_op']." ".strtotime($_REQUEST['tr_added']);
            }

            // BUILD SQL CLAUSE OR EXIT WITH ERROR

            if (count($clause_array) > 0) {

               $clause = '( '.implode(' '.$_REQUEST['condition'].' ',$clause_array).' )';
               // echo $clause;

               $clause_enc = obfuscate_apply($clause);

            } else {

               alert(SRCH_NOSTR);
               exit;

            }

         } else {

         // QUERY FROM LINK (SORT, FILTER, ETC.)

            // DECODE SQL CLAUSE OR EXIT WITH ERROR

            if (strlen($_REQUEST['clause']) > 0) {

               $clause = obfuscate_undo($_REQUEST['clause']);
               // echo $clause;

               $clause_enc = $_REQUEST['clause'];

            } else {

               alert(SRCH_NOSTR);
               exit;

            }

         }

      } else {

      // SIMPLE SEARCH

         $prev_val = db_to_raw($_REQUEST['val']);

         if (strlen($_REQUEST['val']) > 0) {

           if ($_REQUEST['col'] == 'tr.time') {

             if (substr($_REQUEST['val'],0,1) == '>') {
                 $clause = $_REQUEST['col']." > ".substr($_REQUEST['val'],1);
             } else if (substr($_REQUEST['val'],0,1) == '<') {
                 $clause = $_REQUEST['col']." < ".substr($_REQUEST['val'],1);
             } else {
                 $_REQUEST['val'] = abs($_REQUEST['val']);
                 $clause = $_REQUEST['col']." >= ".($_REQUEST['val'] - 5)." and ".$_REQUEST['col']." <= ".($_REQUEST['val'] + 5);
             }

             if ($_REQUEST['sort'] == '') $_REQUEST['sort'] = "ti";

           } else {

             // HANDLE URL ENCODED FIELDS
             if ($_REQUEST['col'] == 'tr.lyrics' || $_REQUEST['col'] == 'tr.license' || $_REQUEST['col'] == 'tr.comments')
              $_REQUEST['val'] = rawurlencode ($_REQUEST['val']);

             $upper_val = strtoupper($_REQUEST['val']);

             $clause = "UPPER(".$_REQUEST['col'].") like '%".raw_to_db($upper_val)."%'";

             if ($_REQUEST['sort'] == '') $_REQUEST['sort'] = substr($_REQUEST['col'],0,2);

           }

           $str = strtolower($_REQUEST['val']);

         } else {

           alert(SRCH_NOSTR);
           exit;

         }

      }

   }

   if (strlen($_REQUEST['filter']) > 0) $and = ' and '.rawurldecode($_REQUEST['filter']);
   else $and = '';

   $yes = $cos = $lys = $lis = "";
   if (substr($_REQUEST['col'],0,2) == "ar") $ars = 'SELECTED';
   if (substr($_REQUEST['col'],0,2) == "tr") $trs = 'SELECTED';
   if (substr($_REQUEST['col'],0,2) == "al") $als = 'SELECTED';
   if (substr($_REQUEST['col'],0,2) == "ge") $ges = 'SELECTED';
   if (substr($_REQUEST['col'],3,2) == "ti") $tis = 'SELECTED';
   if (substr($_REQUEST['col'],3,2) == "ye") $yes = 'SELECTED';
   if (substr($_REQUEST['col'],3,2) == "co") $cos = 'SELECTED';
   if (substr($_REQUEST['col'],3,2) == "ly") $lys = 'SELECTED';
   if (substr($_REQUEST['col'],3,2) == "li") $lis = 'SELECTED';

   if ($NETJUKE_SESSION_VARS["email"] != "") {
     $plist_opt1 = "<A HREF=\"Javascript:Plist('addto');\" title=\"".SRCH_TB_ADDTO_HELP."\">".SRCH_TB_ADDTO."</A> &raquo;";
     $plist_opt2 = plistSelect($NETJUKE_SESSION_VARS["email"],$NETJUKE_SESSION_VARS["default_pl"]) . " <INPUT TYPE='submit' NAME='submit' VALUE='".SRCH_TB_PLIST_BTN."' class='btn_content'>";
   } else {
     if (abs(substr(SECURITY_MODE,2,1)) != 2) {
       $plist_opt1 = SRCH_TB_ADDTO . " &raquo;";
     }
     $plist_opt2 = "<A HREF='login.php'><b>".SRCH_TB_LOGIN."</b></A>";
     if (abs(substr(SECURITY_MODE,2,1)) == 0) {
       $plist_opt2 .= " / <A HREF='account.php?do=new'><b>".SRCH_TB_REGISTER."</b></A>";
     }
   }

   $columns = array ( SRCH_COLS_TR
                    , SRCH_COLS_TI
                    , SRCH_COLS_AR
                    , SRCH_COLS_AL
                    , SRCH_COLS_TN
                    , SRCH_COLS_GE );

   if ( ($_REQUEST['sort'] == '') ||  ($_REQUEST['sort'] == "al")) {

     # SORT BY ALBUM NAME
     $order_by = 'order by upper(al.name), tr.track_number, upper(ar.name), tr.location';
     $selected = 3;

   } elseif ($_REQUEST['sort'] == "ar") {

     # SORT BY ARTIST NAME
     $order_by = 'order by upper(ar.name), upper(al.name), tr.track_number, tr.location';
     $selected = 2;

   } elseif ($_REQUEST['sort'] == "tr") {

     # SORT BY TRACK NAME
     $order_by = 'order by upper(tr.name), upper(ar.name), upper(al.name), tr.track_number, tr.location';
     $selected = 0;

   } elseif ($_REQUEST['sort'] == "ge") {

     # SORT BY GENRE
     $order_by = 'order by upper(ge.name), upper(al.name), tr.track_number, tr.location';
     $selected = 5;

   } elseif ($_REQUEST['sort'] == "ti") {

     # SORT BY TIME
     $order_by = 'order by tr.time';
     $selected = 1;

   } else {

     # SORT BY TRACK NUMBER
     $order_by = 'order by tr.track_number,tr.location';
     $selected = 4;

   }

   $cnt = 0;

   while ($cnt < 6) {
     if ($cnt == $selected) $columns[$cnt] = strtoupper ($columns[$cnt]);
     $cnt++;
   }

   $from = <<<___END_SQL
      from netjuke_tracks tr, netjuke_artists ar, netjuke_albums al,
           netjuke_genres ge
___END_SQL;

   $where = <<<___END_SQL
      where $clause $and
        and ar.id = tr.ar_id
        and al.id = tr.al_id
        and ge.id = tr.ge_id
___END_SQL;

   $sql = "select count(tr.id), sum(tr.time), sum(tr.size) $from $where";
   $dbrs = $dbconn->Execute($sql);

   $total_pages = $dbrs->fields[0];
   $total_time = $dbrs->fields[1];
   $total_size = $dbrs->fields[2];

   if (isset($_REQUEST['page_nav'])) {
     list($_REQUEST['first'],$_REQUEST['page']) = split(",",$_REQUEST['page_nav']);
   }

   if ($_REQUEST['first'] < 1) $_REQUEST['first'] = 1;
   $plus = abs(RES_PER_PAGE);
   if ($plus < 1) $plus = 25;
   $last = $_REQUEST['first'] + $plus;
   $prev_first = $_REQUEST['first'] - $plus;

   if ($_REQUEST['page'] < 1) $_REQUEST['page'] = 1;

   $select = "
      SELECT tr.name, ar.name, al.name, ge.name, tr.location,
             tr.id, ar.id, al.id, ge.id,
             tr.time, tr.track_number,
             tr.img_src, ar.img_src, al.img_src,
             tr.size
   ";

   $sql = "$select $from $where $order_by";
   $dbrs = $dbconn->SelectLimit($sql,($last-1));

	 if (! $dbrs) {
	 	 echo "<pre>QUERY FAILED!<br>";
		 print_r ($dbrs);
		 die;
	 }
   $page_cnt = 1;

   $rows = $dbrs->RecordCount();

   $html = "";
   $ids = "";
   while (!$dbrs->EOF) {

     if ($page_cnt == $last) {

        break;

     } else {

       if ($page_cnt >= $_REQUEST['first']) {

          $fields = $dbrs->fields;
          $edit_al = '';
          $edit_ar = '';
          $edit_ge = '';

          if (ENABLE_XML == 't') {
            if ($ids != '') $ids .= ',';
            $ids .= $fields[5];
          }

          $select = "<input type=checkbox class=\"nomargin\" name='val[]' value='".$fields[5]."'title=\"".SRCH_CHCK_HELP."\">";
          $play = buildButton(WEB_PATH."/play.php?do=play&val=".$fields[5], SRCH_PLAY_HELP, 'play');
          $play_ar = buildButton(WEB_PATH."/play.php?do=play_all&type=ar&id=".$fields[6], SRCH_PLAY_AR_HELP, 'play');
          $play_ar_rand = buildButton(WEB_PATH."/play.php?do=play_all&random=1&type=ar&id=".$fields[6], SRCH_PLAY_AR_RAND_HELP, 'randplay');
          $filter_ar = buildButton(WEB_PATH."/filter.php?do=list.albums&search_do=list.tracks&col=ar_id&val=".$fields[6],
            SRCH_FILTER_AR_HELP, 'info', "NetjukeRemote");
          $play_al = buildButton(WEB_PATH."/play.php?do=play_all&type=al&id=".$fields[7], SRCH_PLAY_AL_HELP, 'play');
          $play_al_rand = buildButton(WEB_PATH."/play.php?do=play_all&random=1&type=al&id=".$fields[7], SRCH_PLAY_AL_RAND_HELP, 'randplay');
          $filter_al = buildButton(WEB_PATH."/filter.php?do=list.artists&search_do=list.tracks&col=al_id&val=".$fields[7],
            SRCH_FILTER_AL_HELP, 'info', "NetjukeRemote");
          $play_ge = buildButton(WEB_PATH."/play.php?do=play_all&type=ge&id=".$fields[8], SRCH_PLAY_GE_HELP, 'play');
          $play_ge_rand = buildButton(WEB_PATH."/play.php?do=play_all&random=1&type=ge&id=".$fields[8], SRCH_PLAY_GE_RAND_HELP, 'randplay');

          if ($NETJUKE_SESSION_VARS['gr_id'] <= 2) {
            $get_info = buildButton(WEB_PATH."/admin/tr-edit.php?do=edit&tr_id=".$fields[5], SRCH_EDIT_HELP, 'edit', "NetJukeGetInfo");
            $edit_al = buildButton(WEB_PATH."/admin/md-edit.php?do=edit&type=al&id=".$fields[7], SRCH_EDIT_AL_HELP, 'edit', "NetJukeGetInfo");
            $edit_ar = buildButton(WEB_PATH."/admin/md-edit.php?do=edit&type=ar&id=".$fields[6], SRCH_EDIT_AR_HELP, 'edit', "NetJukeGetInfo");
            $edit_ge = buildButton(WEB_PATH."/admin/md-edit.php?do=edit&type=ge&id=".$fields[8], SRCH_EDIT_GE_HELP, 'edit', "NetJukeGetInfo");
          }
          else
            $get_info = buildButton(WEB_PATH."/tr-info.php?id=".$fields[5], SRCH_INFO_HELP, 'info', "NetJukeGetInfo");

          if (ENABLE_DOWNLOAD == 't') {

            $tr_location = $fields[4];
            if (substr_count($tr_location,"://") < 1) $tr_location = STREAM_SRVR."/".$tr_location;
            if (substr_count($tr_location,"://") < 1) $tr_location  = WEB_PATH."/".$tr_location;
            $dload = buildButton($tr_location, SRCH_DLOAD_HELP, 'dload');

          } else {

            $dload = "";

          }

          $time = myTimeFormat($fields[9]);

          $fields[0] = format_for_display($fields[0]);
          $fields[1] = format_for_display($fields[1]);
          $fields[2] = format_for_display($fields[2]);
          $fields[3] = format_for_display($fields[3]);

          $tr_img = image_icon($fields[11]);
          $ar_img = image_icon($fields[12]);
          $al_img = image_icon($fields[13]);

          if ($NETJUKE_SESSION_VARS["explore"] == 't')
            $ge_link = "<A HREF=\"".WEB_PATH."/explore.php?do=list.artists&ge_id=".$fields[8]."\" title=\"".SRCH_GE_HELP."\">$fields[3]</A>";
          else
            $ge_link = "<A HREF=\"".$_SERVER['PHP_SELF']."?do=list.tracks&col=ge.id&val=$fields[8]&sort=ge\" title=\"".SRCH_GE_HELP."\">$fields[3]</A>";

          $class = "line".($page_cnt%2+1);
          $html .= "
          <tr valign=top>
            <td class='$class' nowrap>$select $play $dload $get_info</td>
            <td class='$class'>$fields[0]&nbsp;".$tr_img."</td>
            <td class='$class' align=right>$time&nbsp;</td>
            <td class='$class'>$play_ar $play_ar_rand $filter_ar $edit_ar <A HREF=\"".$_SERVER['PHP_SELF']."?do=list.tracks&col=ar.id&val=$fields[6]&sort=ar\" title=\"".SRCH_AR_HELP."\">$fields[1]</A>&nbsp;".$ar_img."</td>
            <td class='$class'>$play_al $play_al_rand $filter_al $edit_al <A HREF=\"".$_SERVER['PHP_SELF']."?do=list.tracks&col=al.id&val=$fields[7]&sort=al\" title=\"".SRCH_AL_HELP."\">$fields[2]</A>&nbsp;".$al_img."</td>
            <td class='$class' align=center>$fields[10]</td>
            <td class='$class'>$play_ge $play_ge_rand $edit_ge $ge_link &nbsp;</td>
          </tr>
          ";

       }

       $page_cnt++;

       $dbrs->MoveNext();

     }

   }

   $dbrs->Close();

   if ($prev_first > 0) {
     if ($_REQUEST['do'] == 'search.adv') {
       $prev_btn = "<b>&laquo; <a href='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&clause=".$clause_enc."&sort=".$_REQUEST['sort']."&filter=".$_REQUEST['filter']."&first=$prev_first&page=".($_REQUEST['page'] - 1)."' title=\"".SRCH_TB_PREV_HELP."\">".SRCH_TB_PREV."</a> &nbsp;";
     } else {
       $prev_btn = "<b>&laquo; <a href='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&col=".$_REQUEST['col']."&val=".$_REQUEST['val']."&sort=".$_REQUEST['sort']."&filter=".$_REQUEST['filter']."&first=$prev_first&page=".($_REQUEST['page'] - 1)."' title=\"".SRCH_TB_PREV_HELP."\">".SRCH_TB_PREV."</a> &nbsp;";
     }
   } else {
     $prev_btn = "&nbsp";
   }

   if ($page_cnt <= $total_pages) {
     if ($_REQUEST['do'] == 'search.adv') {
       $next_btn = "<b><a href='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&clause=".$clause_enc."&sort=".$_REQUEST['sort']."&filter=".$_REQUEST['filter']."&first=$last&page=".($_REQUEST['page'] + 1)."' title=\"".SRCH_TB_NEXT_HELP."\">".SRCH_TB_NEXT."</a> &raquo;</b> &nbsp;";
     } else {
       $next_btn = "<b><a href='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&col=".$_REQUEST['col']."&val=".$_REQUEST['val']."&sort=".$_REQUEST['sort']."&filter=".$_REQUEST['filter']."&first=$last&page=".($_REQUEST['page'] + 1)."' title=\"".SRCH_TB_NEXT_HELP."\">".SRCH_TB_NEXT."</a> &raquo;</b> &nbsp;";
     }
   } else {
     $next_btn = "&nbsp";
   }

   $page_cnt = 0;

   $this_page  = 1;

   $this_prev_first = 1;

   $page_nav_html = "";
   while ($page_cnt < $total_pages) {

     if ($page_cnt % $plus == 0) {

       $this_first = $this_prev_first;

       if ($this_page == $_REQUEST['page']) {
         $page_selected = "selected";
       } else {
         $page_selected = "";
       }
       $page_nav_html .= "<option value='$this_first,$this_page' $page_selected>".SRCH_TB_PAGE." $this_page</option>";

       $this_prev_first = $this_first + $plus;

        $this_page++;

     }

     $page_cnt++;

   }

   # HTML header
   $section = "browse";
   include (INTERFACE_HEADER);

}

########################################

?>

        <table width='100%' border=0 cellspacing=1 cellpadding=3 class='border'>
         <tr>
           <td class="header" nowrap colspan=5>
             <B><?php echo  SRCH_HEADER ?></B>
           </td>
         </tr>
        <form action='<?php echo $_SERVER['PHP_SELF']?>' method=get name='searchForm'>
        <input type=hidden name='do' value='search'>
         <tr>
           <td class="content" align=center nowrap>
               <?php echo  $total_pages." ".SRCH_ROWCNT_1.". " ?>
           </td>
           <td class="content" align=center nowrap>
               <?php echo  " ".SRCH_ROWCNT_2." ".$_REQUEST['page']." ".SRCH_ROWCNT_3." ". ($this_page - 1) ?>
           </td>
           <td class="content" align=center nowrap>
               <?php echo  myTimeFormat($total_time)." ".SRCH_ROWCNT_4." ". myFilesizeFormat($total_size) ?>
           </td>
		   <?php if (BATCH_DOWNLOAD == 't') { ?>
		   <td class="content" align=center nowrap>
		       <B><A HREF="Javascript:Plist('batch');" title="<?php echo  SRCH_TB_BATCH_HELP ?>"><?php echo  SRCH_TB_BATCH ?></A></B>
           </td>
		   <?php } ?>
           <td class="content" align=center nowrap colspan=<?php echo (BATCH_DOWNLOAD == 't') ? 1 : 2 ?>>
             <select name='col' class=input_content>
               <option value='ar.name' <?php echo $ars?>><?php echo  SRCH_QS_AR ?></option>
               <option value='tr.name' <?php echo $trs?>><?php echo  SRCH_QS_TR ?></option>
               <option value='al.name' <?php echo $als?>><?php echo  SRCH_QS_AL ?></option>
               <option value='ge.name' <?php echo $ges?>><?php echo  SRCH_QS_GE ?></option>
               <option value='tr.time' <?php echo $tis?>><?php echo  SRCH_QS_TI ?></option>
               <option value='tr.year' <?php echo $yes?>><?php echo  SRCH_QS_YE ?></option>
               <option value='tr.comments' <?php echo $cos?>><?php echo  SRCH_QS_CO ?></option>
               <option value='tr.lyrics' <?php echo $lys?>><?php echo  SRCH_QS_LY ?></option>
               <option value='tr.license' <?php echo $lis?>><?php echo  SRCH_QS_LI ?></option>
             </select>
             <input type=text name='val' size='15' maxlength=50 value='<?php echo $prev_val?>' class=input_content>
             <input type=submit value='<?php echo  SRCH_QS_BTN ?>' class='btn_content'>
           </td>
        </tr>
        </form>
        <form action='pl-edit.php' method=get name='viewForm' onSubmit="return(Plist('view'));">
        <input type=hidden name='do' value='edit'>
         <tr>
           <td class="content" align=center nowrap><B><A HREF="Javascript:SelectAll();" title="<?php echo  SRCH_TB_SELECT_HELP ?>"><?php echo  SRCH_TB_SELECT ?></A></B></td>
           <td class="content" align=center nowrap><B><A HREF="Javascript:ResetAll();" title="<?php echo  SRCH_TB_RESET_HELP ?>"><?php echo  SRCH_TB_RESET ?></A></B></td>
           <td class="content" align=center nowrap><B><A HREF="Javascript:Plist('play');" title="<?php echo  SRCH_TB_PLAYSEL_HELP ?>"><?php echo  SRCH_TB_PLAYSEL ?></A></B><input type="checkbox" name='random' align='center' title="<?php echo SRCH_RANDOMIZE_HELP ?>"><?php echo SRCH_RANDOMIZE ?></td>
		       <td class="content" align=center nowrap><B><?php echo $plist_opt1?></B></td>
           <td class="content" align=center nowrap><?php echo $plist_opt2?></td>
        </tr>
        </form>
        </table>

        <?php echo  SpecialEditTB() ?>

        <BR>

        <?php searchPageNav('top', $clause_enc, $prev_btn, $next_btn, $page_nav_html); ?>

        <BR>

        <table width='100%' border=0 cellspacing=1 cellpadding=3 class='border'>
        <form name='playForm'>

           <?php
             if ($_REQUEST['do'] == 'search.adv') {
               advancedHeader($columns, $clause_enc);
             } else {
               simpleHeader($columns, $valurl);
             }
           ?>

<!-- tbody style="overflow: auto; height: 330px;" -->

           <?php echo  $html ?>

<!-- /tbody -->

        </form>
        </table>
        <?php
          if (ENABLE_XML == 't') {
            $xml_button = buildButton (WEB_PATH."/xml.php?do=show.tracks&id=".$ids, COMMON_XML_HELP, 'xml');
            echo '<br><center>'.$xml_button.'</center>';
           }
        ?>

        <BR>

        <?php searchPageNav('bottom', $clause_enc, $prev_btn, $next_btn, $page_nav_html); ?>

<?php

   # HTML footer
   include (INTERFACE_FOOTER);

########################################

function searchPageNav($type, $clause_enc, $prev_btn, $next_btn, $page_nav_html) {

  if ($type == 'top') {
    $scrolling = "<a href='#PageBot' title='". SRCH_TB_PAGEBOT_HELP ."'><b>". SRCH_TB_PAGEBOT ."</b></a>";
  } else {
    $scrolling = "<a href='#PageTop' title='". SRCH_TB_PAGETOP_HELP ."'><b>". SRCH_TB_PAGETOP ."</b></a>";
  }

  if ($_REQUEST['do'] == 'search.adv') {

    $hidden_fields = "
        <input type=hidden name='do' value='".$_REQUEST['do']."'>
        <input type=hidden name='clause' value='".$clause_enc."'>
        <input type=hidden name='sort' value='".$_REQUEST['sort']."'>
        <input type=hidden name='filter' value='".$_REQUEST['filter']."'>
    ";

  } else {

    $hidden_fields = "
        <input type=hidden name='do' value='".$_REQUEST['do']."'>
        <input type=hidden name='col' value='".$_REQUEST['col']."'>
        <input type=hidden name='val' value='".$_REQUEST['val']."'>
        <input type=hidden name='sort' value='".$_REQUEST['sort']."'>
        <input type=hidden name='filter' value='".$_REQUEST['filter']."'>
    ";

  }

  echo "
        <table width='100%' border=0 cellspacing=1 cellpadding=3 class='border'>
        <form action='".$_SERVER['PHP_SELF']."' method=get name='pageForm'>
        ".$hidden_fields."
         <tr>
           <td width='25%' class='content' align=center nowrap>
             ".$prev_btn."
           </td>
           <td width='25%' class='content' align=center nowrap>
             ".$scrolling."
           </td>
           <td width='25%' class='content' align=center>
             <select name='page_nav' class=input_content>
               ".$page_nav_html."
             </select>
             <input type=submit value='". SRCH_TB_PAGE_BTN ."' class='btn_content'>
           </td>
           <td width='25%' class='content' align=center nowrap>
             ".$next_btn."
           </td>
        </tr>
        </form>
        </table>
  ";

}

########################################

function simpleHeader($columns, $valurl) {

  $ar_filter = $al_filter = $ge_filter = "";

  if (strlen($_REQUEST['filter']) == 0) {
    $ar_filter = "&nbsp;".buildButton(WEB_PATH ."/filter.php?do=list.artists&search_do=".$_REQUEST['do']."&col=".
	$_REQUEST['col']."&val=".$valurl, SRCH_AR_FILTER_HELP, 'filter', 'NetjukeRemote');
  }

  if (strlen($_REQUEST['filter']) == 0) {
    $al_filter = "&nbsp;".buildButton(WEB_PATH ."/filter.php?do=list.albums&search_do=".$_REQUEST['do']."&col=".
	$_REQUEST['col']."&val=".$valurl, SRCH_AL_FILTER_HELP, 'filter', 'NetjukeRemote');
  }

  if (strlen($_REQUEST['filter']) == 0) {
    $ge_filter = "&nbsp;".buildButton(WEB_PATH ."/filter.php?do=list.genres&search_do=".$_REQUEST['do']."&col=".
	$_REQUEST['col']."&val=".$valurl, SRCH_GE_FILTER_HELP, 'filter', 'NetjukeRemote');
  }

  echo "
    <tr>
      <td class='header' width='1%' nowrap>
        <B>". SRCH_COLS_OP ."</B>
      </td>
      <td class='header' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&col=".$_REQUEST['col']."&val=".$valurl."&sort=tr&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_TR_HELP ."'>".$columns[0]."</A></B>
      </td>
      <td class='header' width='1%' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&col=".$_REQUEST['col']."&val=".$valurl."&sort=ti&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_TI_HELP ."'>".$columns[1]."</A></B>
      </td>
      <td class='header' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&col=".$_REQUEST['col']."&val=".$valurl."&sort=ar&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_AR_HELP ."'>".$columns[2]."</A></B>
        ". $ar_filter ."
      </td>
      <td class='header' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&col=".$_REQUEST['col']."&val=".$valurl."&sort=al&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_AL_HELP ."'>".$columns[3]."</A></B>
        ". $al_filter ."
      </td>
      <td class='header' width='1%' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&col=".$_REQUEST['col']."&val=".$valurl."&sort=tn&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_TN_HELP ."'>".$columns[4]."</A></B>
      </td>
      <td class='header' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&col=".$_REQUEST['col']."&val=".$valurl."&sort=ge&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_GE_HELP ."'>".$columns[5]."</A></B>
        ". $ge_filter ."
      </td>
    </tr>
  ";

}

########################################

function advancedHeader($columns, $clause_enc) {

  $ar_filter = $al_filter = $ge_filter = "";

  if (strlen($_REQUEST['filter']) == 0) {
    $ar_filter = "&nbsp;".buildButton(WEB_PATH."/filter.php?do=list.artists&search_do=".$_REQUEST['do']."&clause=".$clause_enc,
       SRCH_AR_FILTER_HELP, 'filter', 'NetjukeRemote');
  }

  if (strlen($_REQUEST['filter']) == 0) {
    $al_filter = "&nbsp;".buildButton(WEB_PATH."/filter.php?do=list.albums&search_do=".$_REQUEST['do']."&clause=".$clause_enc,
       SRCH_AL_FILTER_HELP, 'filter', 'NetjukeRemote');
  }

  if (strlen($_REQUEST['filter']) == 0) {
    $ge_filter = "&nbsp;".buildButton(WEB_PATH."/filter.php?do=list.genres&search_do=".$_REQUEST['do']."&clause=".$clause_enc,
       SRCH_GE_FILTER_HELP, 'filter', 'NetjukeRemote');
  }

  echo "
    <tr>
      <td class='header' width='5%' nowrap>
        <B>". SRCH_COLS_OP ."</B>
      </td>
      <td class='header' width='30%' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&clause=".$clause_enc."&sort=tr&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_TR_HELP ."'>".$columns[0]."</A></B>
      </td>
      <td class='header' width='5%' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&clause=".$clause_enc."&sort=ti&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_TI_HELP ."'>".$columns[1]."</A></B>
      </td>
      <td class='header' width='20%' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&clause=".$clause_enc."&sort=ar&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_AR_HELP ."'>".$columns[2]."</A></B>
        ". $ar_filter ."
      </td>
      <td class='header' width='28%' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&clause=".$clause_enc."&sort=al&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_AL_HELP ."'>".$columns[3]."</A></B>
        ". $al_filter ."
      </td>
      <td class='header' width='2%' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&clause=".$clause_enc."&sort=tn&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_TN_HELP ."'>".$columns[4]."</A></B>
      </td>
      <td class='header' width='10%' nowrap>
        <B><A CLASS='header' HREF='".$_SERVER['PHP_SELF']."?do=".$_REQUEST['do']."&clause=".$clause_enc."&sort=ge&filter=".$_REQUEST['filter']."' title='". SRCH_COLS_GE_HELP ."'>".$columns[5]."</A></B>
        ". $ge_filter ."
      </td>
    </tr>
  ";

}

########################################

?>
