<?php
include("dbconnection.php");
function login(){?>
<h1 style="float: left; color: red; margin: 5px 5px 0px 5px; height: 30px; font: 12px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; padding: 0px 0px 0px 10px; height: 20px; width: 100%; text-align: left;">User Login</h1>
<table id="user_login_table" style="float: left; margin: 5px 5px 0px 5px; padding: 0px 0px 0px 0px; width: 100%; overflow: hidden; display: block;">
	<tr>
		<td style="float: left; text-align: left; width: 80px; font: 9pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;">
			<label for="navbar_username">User Name:</label>
		</td>
		<td style="float: left; width: 50px; font: 9pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;">
			<input type="text" name="<?php //vb_login_username?>username" id="username" style="float: left; border: 1px solid #7C7C7C; font: 9px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; width: 100px;" value="User Name" onBlur="if (this.value == '') this.value = 'User Name';" onFocus="if (this.value == 'User Name') this.value = '';" />
		</td>
<?php
/*
<td  style="font: 9px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;" nowrap="nowrap">
<label for="cb_cookieuser_navbar">
<input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />		
Remember Me?
</label>
</td>
*/
?>
    </tr>
    <tr>
		<td style="float: left; text-align: left; width: 80px; font: 9pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;">
			<label for="navbar_password">Password:</label>
		</td>
		<td style="float: left; width: 50px;">
			<input type="password" style="float: left; border: 1px solid #7C7C7C; font: 9pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; width: 100px;" <?php //name="pass"?> name="<?php //vb_login_username?>pass" id="pass" />
		</td>
	</tr>
	<tr>
		<td style="float: left; text-align: left; width: 80px; font: 9pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;">
			<a id="a_wgad" href="wheelofgodUsers/registration.php?<?php echo "user=register"; ?>" style="text-decoration: none;" onMouseOver="document.getElementById('a_wgad').style.textDecoration='underline';" onMouseOut="document.getElementById('a_wgad').style.textDecoration='none';">Register</a>
		</td>
		<td style="float: left; width: 50px;">
<?php
//<input type="button" style="float: left; border: 1px solid #7C7C7C; background-color: #7C7C7C; color: #ffffff; font: 9pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; font-weight: bold;" name="submit" value="Login"onclick="login();" />
?>
			<input type="submit" style="float: left; border: 1px solid #7C7C7C; background-color: #7C7C7C; color: #ffffff; font: 9pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; font-weight: bold;" name="submit" value="Login" />
<?php //<input type="hidden" name="game" id="game_id" value="" />?>
		</td>
	</tr>
</table>
<?php
}
/*}


	<table cellpadding="0" cellspacing="3" border="0">
		<tr>
			<td class="smallfont">
				<label for="navbar_username">
					User Name
				</label>
			</td>
			<td>
				<input type="text" class="bginput" style="font-size: 11px" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onblur="if (this.value == '') this.value = 'User Name';" onfocus="if (this.value == 'User Name') this.value = '';" />
			</td>
			<td class="smallfont" colspan="2" nowrap="nowrap">
				<label for="cb_cookieuser_navbar">
					<input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />
					Remember Me?
				</label>
			</td>
		</tr>
		<tr>
			<td class="smallfont">
				<label for="navbar_password">Password</label>
			</td>
			<td>
				<input type="password" style="font: 10pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;" style="font-size: 11px" name="vb_login_password" id="navbar_password" size="10" tabindex="102" />
			</td>
			<td>
				<input type="submit" style="font: 9px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;" value="Log in" tabindex="104" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
			</td>
		</tr>
	</table>
*/

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site'])){
//if there is, it logs you in and directes you to the members page
	$username = $_COOKIE['ID_my_site'];
	$pass = $_COOKIE['Key_my_site'];
	$sql = "SELECT * FROM ".$dbTable." WHERE username = '".mysql_real_escape_string($username)."'";
	
	$check = mysql_query($sql)or die(mysql_error());
	while($info = mysql_fetch_array( $check )){
		if ($pass != $info['password']){
		}else{
			header("Location: ");
		}
	}
}

//if the login form is submitted
if (isset($_POST['submit'])){ // if form has been submitted
	// makes sure they filled it in
	if(!$_POST['username'] | !$_POST['pass']){
		echo $sql;
		echo "You did not fill in a required field.<br />";
		login();
		//die('You did not fill in a required field.<br />');
		
	}
	// checks it against the database
	if(!get_magic_quotes_gpc()){
		$_POST['email'] = addslashes($_POST['email']);
	}
	$check = mysql_query("SELECT * FROM ".$dbTable." WHERE username = '".$_POST['username']."'")or die(mysql_error());
	//Gives error if user dosen't exist
	$check2 = mysql_num_rows($check);
	if ($check2 == 0){
		die("That user does not exist in our database. <a href=\"wheelofgodUsers/registration.php\">Click Here to Register</a>");
	}
	while($info = mysql_fetch_array( $check )){
		$_POST['pass'] = stripslashes($_POST['pass']);
		$info['password'] = stripslashes($info['password']);
		$_POST['pass'] = md5($_POST['pass']);
	
		//gives error if the password is wrong
		if ($_POST['pass'] != $info['password']){
			die('Incorrect password, please try again.');
		}else{
			// if login is ok then we add a cookie
			$_POST['username'] = stripslashes($_POST['username']);
			$hour = time() + 3600;
			setcookie(ID_my_site, $_POST['username'], $hour);
			setcookie(Key_my_site, $_POST['pass'], $hour);
			//then redirect them to the members area
			header("Location: ");
		}
		echo "<span style=\"float: left; text-align: left; margin: 10px 10px 0px 10px; width: 100%;\">Welcome ".$_POST['username']."!</span><br />";
		echo "<span style=\"float: left; text-align: left; margin: 10px 10px 0px 10px; width: 100%;\"><a href=\"wheelofgodUsers/member.php\">User CP</a></span>";
		echo "<span style=\"float: left; text-align: left; margin: 10px 10px 0px 10px; width: 100%;\"><a href=\"logout.php\">Logout</a></span>";
	}
}
login();
?>