<?
// TIME DINGY
function getmicrotime(){ 
    list($usec, $sec) = explode(" ",microtime()); 
    return ((float)$usec + (float)$sec); 
} 
$time_start = getmicrotime();

include "config.php";
include "function.php";

html_header();

	$connection=connectdb();


//Add the selected songs to the playlist
if ($playlistadd){
	while (list ($key, $value) = each($HTTP_POST_VARS)){
		$keyt=trim($key);
		$keye=explode ("_", $keyt);
	
	
		if ($keye[0] == "add"){
			$id=$keye[1];
			
			//checking if the song is already in the database in case someone refreshes the page
			$querycheck1="select filename from files where id='$id'";
			$result=mysql_query($querycheck1, $connection);
			$row=mysql_fetch_array($result);
			
			if ($row[filename]){
				$querycheck2="select * from playlist where filename='".addslashes($row[filename])."'";
				$result2=mysql_query($querycheck2, $connection);
				$row2=mysql_fetch_array($result2);
				
				if ($row[filename] == $row2[filename]){
					echo "<center><font color=red><b>$row2[filename] is already in the playlist</b></font></center><br>";
				}
				if (!$row[filename]){
					echo "the filename does not exist";
				}
				else {
					$insertquery="insert into playlist (filename) values ('".addslashes($row[filename])."')";
					$result=mysql_query($insertquery, $connection);
					if (!$result){ echo"<center><font color=red><b>Did NOT perform the insert action something caused me to NACK</center></font></b> ";}
				}
			}
		} 
	}
	if (!$id){
		echo "<center><font color=\"red\"><b>No valid selection to add<b></font></center>";
	}
}


?>

<form method="post" action="">
<!--  <table width="100%" border="0">
     <tr> 
      <td background="bg.jpg" height="38">&nbsp;</td>
    </tr>
   <tr> 
      <td background="bg.jpg" height="42"> 
        <div align="center"> -->
<? start_maintable(); ?>
			  	<!-- search table -->
                <table class="searchtable" cellspacing="<? echo $skininfo['cellspacing']['searchtable']; ?>">
                  <tr> 
                    <th width="25%" class="searchtable"> 
                      <img src="<? echo $skindir.$skin; ?>/header-searchartist.jpg">
                    </th>
                    <th width="25%" class="searchtable"> 
                      <img src="<? echo $skindir.$skin; ?>/header-searchsong.jpg">
                    </th>
                    <th width="25%" class="searchtable"> 
                      <img src="<? echo $skindir.$skin; ?>/header-searchall.jpg">
                    </th>
                    <th width="25%" class="searchtable"> 
                      <img src="<? echo $skindir.$skin; ?>/header-searchgenre.jpg">
                    </th>
                  </tr>
                  <tr> 
                    <td class="searchtable"> 
                        <input type="text" name="artist" value="<? echo "".stripslashes($artist).""; ?>">
                    </td>
                    <td class="searchtable"> 
                        <input type="text" name="song" value="<? echo  "".stripslashes($song).""; ?>">
                    </td>
                    <td class="searchtable"> 
                        <input type="text" name="all" value="<? echo "".stripslashes($all).""; ?>">
                    </td>
                    <td class="searchtable"> 
                        <select name="genre">
                        <option value="select">search in all genres</option>
<?
$sql1="select * from genres";
$res1=mysql_query($sql1, $connection);

while ($r=mysql_fetch_array($res1)) {
	$genres[$r[id]]=$r[description];
}

$sql="select DISTINCT genreid from files";
$res=mysql_query($sql, $connection);

while ($r=mysql_fetch_array($res)) {
	if ($r[genreid] == "0"){continue;}
	if ($r[genreid] == $genre) { $selected=" selected"; } else { $selected=''; }
echo"<option value=\"$r[genreid]\"$selected>".$genres[$r[genreid]]."</option>\n";
}
?>						
					</select>
                      </div>
                    </td>
                  </tr>
                </table>
				<!-- /search table -->
                  <p align="center"><input type="submit" name="Submit" value="Search"></p>
<?
if ($Submit || $playlistadd){

	//string maken voor het zoeken
	$initq="select * from files";
	if ($artist){$qsearch[] = "artist LIKE '%$artist%'";}
	if ($genre && $genre!="select"){ $qsearch[] = "genreid='$genre'";}
	if ($song){$qsearch[] = "title LIKE '%$song%'";}
	if ($all) {$qall = "select * from files where album LIKE '%$all%' || title LIKE '%$all' || artist LIKE '%$all%' || filename LIKE '%$all%'";}

	if (!$qsearch && !$qall){
		echo "<br><b><font color=\"red\">nothing to search for</font>";

	} else {
		if (@is_array($qsearch)) {
			$merged = implode (" AND ", $qsearch);
			if ($merged){
				$queryw="WHERE $merged ORDER BY artist";
				$query="$initq $queryw";
			}
		}

		// If there is a search for all fields, ignore the seperate fields
		if ($qall) {
			$query=$qall;
		}

		//tabel op scherm poepen
		if ($query){
			echo zoektabel($query);
		}
	}

}
echo 
end_maintable(); ?>
</form>
<?
html_footer();
$time_end = getmicrotime();
$time = $time_end - $time_start;

echo "<!-- Did it in $time seconds -->";
?>