<?php
include('config.php');
////////////////////Register//////////////////////////////
if(@$_POST['name']){
mysql_connect($dbloc, $dbuser, $dbpass);
mysql_select_db($db); 
$user=$_POST['user'];
$password=$_POST['password'];
$email=$_POST['email'];
$gender=$_POST['gender'];
$name=$_POST['name'];
$birthday=$_POST['month']. '/'. $_POST['day']. '/'. $_POST['year'];
$date=date("m.d.y"); 
$time=date('h:i:s A');
$game="Melero Video";
setcookie("username", $user, time()+530500);
//Insert new user into users db
mysql_query("INSERT INTO users(name, user, password, date, time, gender, birthday, email, pictureid, usertype) VALUES('$name', '$user', '$password', '$date', '$time', '$gender', '$birthday', '$email', 'nophoto.bmp','User')"); 

//Create Table with List of Playlists
$userdbname=$user.'playlist';

//Create Favorites Table
mysql_query("CREATE TABLE $userdbname( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), userid VARCHAR(100), title VARCHAR(100), date VARCHAR(100), views VARCHAR(100), nextid VARCHAR(100))"); 
//Get allgenres id
$result = mysql_query("SHOW TABLE STATUS LIKE '$userdbname'");
$row = mysql_fetch_array($result);
$nextid = $row['Auto_increment']; 
$userfavorites=$_COOKIE['username'].$nextid;
mysql_query("INSERT INTO $userdbname(userid, title, date, views, nextid) VALUES('admin', 'Favorites', 'September 29 2014', '0', '$nextid')"); 
mysql_query("CREATE TABLE $userfavorites( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), userid VARCHAR(100), allgenreid VARCHAR(100))"); 
header("Location:index.php");
}

/////////////////////Logout///////////////
$my=str_replace("%20"," ",$_SERVER["REQUEST_URI"]);
$strcut=strstr($my, '=');
$ok=str_replace("=","",$strcut);
if($ok=='logout'){
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
   foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
      setcookie($name, '', time()-1000);
       setcookie($name, '', time()-1000, '/');
   }
}



echo "<script type=text/javascript>
top.window.location = 'index.php';
</script>";
}else{}

////////////////////Login/////////////////
if(@$_POST['theuser']){
mysql_connect($dbloc, $dbuser, $dbpass);
mysql_select_db($db);  
// Connect to MySQL
$theuser=$_POST['theuser'];
$result = mysql_query("SELECT * FROM users WHERE user LIKE '$theuser' "); 
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
if($_POST['password']==$row['password']){
setcookie('username', $theuser,  time()+(10 * 365 * 24 * 60 * 60));
header("location:index.php");
}
else{
echo "Incorrect user name or password";
}}}
else{
}
include("headertop.php");
?><body><head>
<title>Membership registration - Melero video</title>
<P><table align=center width=80% bgcolor=white border=1 cellpadding=0 cellspacing=0 bordercolor=white>
<td width=90% bgcolor=white align=center>
<table width=100% align=center><CenteR><b>Sign in</b>
<form action="register.php" METHOD="post">
<td width=65% align=center><input type=text placeholder="User Name"  name=theuser></td>
<tr><td width=65% align=center><input type=text placeholder="Password" name=password></td>
<tR><td width=65% align=center><center><input type=submit value="Sign In"></td>
</form>
</table>
</td>
<tR><td width=90% align=center bgcolor=white><b>Or</b></td>

<tr><td width=90% align=center bgcolor=white>
<table width=100% align=center><form action="register.php" METHOD="post">
<b>Register Today!</b>
<tr><td width=65% align=center><input type=text placeholder="First & Last Name" name=name></td></tr>
<tr><td width=65% align=center><input type=text placeholder="User Name" name=user></td></tr>
<tr><td width=65% align=center><input type=text placeholder="Email" name=email></td></tr>
<tr><td width=65% align=center><input type=text placeholder="Password" name=password></td></tr>
<tr><td width=65% align=center>Gender: <select name=gender><option value="Female">Female</option><option value="Male">Male</option></select></td></tr>
<tr><td width=65% align=center><select name=month>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name=day>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>

</select>

<select name=year>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">200</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
</select>
</td></tr><tr><td width=65% align=center><input type=submit value="Register"></td></tr></form></table>
</td></table>
</table>
<?php include("headerbottom.php"); ?>