<?
function printlogin()
{

?>

<script type="text/javascript">
function check()
{
 if(document.forms["formLogin"].login.value == "")
 {
 	alert("Write your login");
	document.forms["formLogin"].login.focus();
	return false;
	}
	else if(document.forms["formLogin"].password.value == "")
	{
	 alert("Write your password");
	 document.forms["formLogin"].password.focus();
	 return false;
	}
	else
	{
	 return true;
	}
}
</script>

<form name="formLogin" method="post" action="functions/login.php">
<table id="identificacion">
<tr>
<td colspan="2" align="center"><h4>Identification</h4></td>
</tr>
<tr>
<td align="right">Login: </td><td><input type="text" name="login" /></td>
</tr>
<tr>
<td align="right">Pass: </td><td><input type="password" name="password" /></td>
</tr>
<tr>

<td colspan="2" align="center"><input type="submit" value="Access" onClick="return check()"/>
</tr>
</table>
</form>
<?
	if(isset($_GET["error"]))
	{
	 ?>
	 <script type="text/javascript">
	 				 alert("Data is not correct");
	 </script>
	 <?
	}


}


function printuser(){

?>
<h2> 

<?php 
echo $_SESSION['login'];
?>

</h2>
Cash:
<?php
$query = "SELECT * FROM User where login='".$_SESSION['login']."'";
$answer = mysql_query($query);
$row = mysql_fetch_array($answer);
echo $row["cash"]; 
$id = $row["id"];
echo "<br>";

//print the user's stocks
$query = "SELECT Ownership.id AS ownerid, ticker, volume, stock, price FROM Ownership, Stock where user=".$id." and Ownership.stock = Stock.id";
$answer = mysql_query($query);
echo "<table border='1'>";
echo "<tr><td colspan='4'><b>Stocks</b></td></tr>";
echo "<tr><td>Ticker</td><td>Volume</td><td>Price</td></tr>";
 while($row = mysql_fetch_array($answer)){
    echo "<tr>";
	echo "<td>";
	echo "<a href='details.php?idticker=".$row["stock"]."&ticker=".$row["ticker"]."'>".$row["ticker"]."</a>"; 	
	echo "</td>";
	echo "<td>";
	echo $row["volume"];
	echo "</td>";		
	echo "<td>";
	echo $row["price"];
	echo "</td>";
	echo "<td>";	
	echo "<a href='/functions/sell.php?ownerid=".$row["ownerid"]."&ticker=".$row["ticker"]."&volume=".$row["volume"]."&stock=".$row["stock"]."'>Sell</a>"; 
	echo "</td>";
	echo "</tr>";
} 
echo "</table>";
}


function printsellbuyuser(){

$query = "SELECT * FROM User where login='".$_SESSION['login']."'";
$answer = mysql_query($query);
$row = mysql_fetch_array($answer);
$id = $row["id"];

//print the sell intentions of this user
echo "<table border='1'>";
echo "<tr><td colspan='4'><b>Orders</b></td></tr>";
echo "<tr><td colspan='4'>Sells</td></tr>";
echo "<tr><td>Stock</td><td>Price</td><td>Volume</td></tr>";
$query = "SELECT Sell.id AS idsell, ticker, user, price, volume, stock, price FROM Sell, Stock where user=".$id." AND Stock.id = Sell.stock";
$answer = mysql_query($query);
while($row = mysql_fetch_array($answer)){
	echo "<tr>";
	echo "<td>";	
	echo "<a href='details.php?idticker=".$row["stock"]."&ticker=".$row["ticker"]."'>".$row["ticker"]."</a>"; 
	echo "</td>";
	echo "<td>";	
	echo $row["price"];
	echo "</td>";
	echo "<td>";	
	echo $row["volume"];
	echo "</td>";
	echo "<td>";	
	echo "<a href='/functions/removesells.php?idsell=".$row['idsell']."'>Remove</a>";
	echo "</td>";
	echo "</tr>";
}
//print the buy intentions of this user
echo "<tr><td colspan='4'>Buys</td></tr>";
echo "<tr><td>Stock</td><td>Price</td><td>Volume</td></tr>";
$query = "SELECT Buy.id AS idbuy, ticker, user, price, volume, stock FROM Buy, Stock where user=".$id." AND Stock.id = Buy.stock";
$answer = mysql_query($query);
while($row = mysql_fetch_array($answer)){
	echo "<tr>";
	echo "<td>";	
	echo "<a href='details.php?idticker=".$row["stock"]."&ticker=".$row["ticker"]."'>".$row["ticker"]."</a>"; 
	echo "</td>";
	echo "<td>";	
	echo $row["price"];
	echo "</td>";
	echo "<td>";	
	echo $row["volume"];
	echo "</td>";
	echo "<td>";	
	echo "<a href='/functions/removebuy.php?idbuy=".$row['idbuy']."'>Remove</a>";
	echo "</td>";
	echo "</tr>";
}
echo "</table>";
}


function printsells(){
?>
<h2>Sells</h2>
<? 
$currentuser = $_SESSION['login'];
$query = "SELECT *, Sell.id AS idbuy, User.login AS loginuser FROM Sell, User, Stock WHERE Sell.user = User.id and Stock.id = Sell.stock ORDER BY Sell.price";
$answer = mysql_query($query);
echo "<table border='1'>";
echo "<tr><td>Stock</td><td>Price</td><td>Volume</td><td>User</td></tr>";
while($row = mysql_fetch_array($answer)){
	//don't print the current user's sell intentions 
	if($row["loginuser"]!==$currentuser){
    	echo "<tr>";
		echo "<td>";	
		//User can see the ticker's details
		echo "<a href='details.php?idticker=".$row["stock"]."&ticker=".$row["ticker"]."'>".$row["ticker"]."</a>"; 
		echo "</td>";
		echo "<td>";
		echo $row["price"];
		echo "</td>";
		echo "<td>";	
		echo $row["volume"]; 
		echo "</td>";
		echo "<td>";	
		echo $row["login"]; 
		echo "</td>";
		echo "<td>";	
		//User can buy this
		echo "<a href='/functions/buy.php?idbuy=".$row["idbuy"]."&buyer=".$currentuser."'>Buy</a>"; 
		echo "</td>";	
		echo "</tr>";
	}
} 
echo "</table>";

}

function printbuys(){
?>
<h2>Buys</h2>
<? 
$query ="SELECT * FROM User WHERE cash <= 0";
$answer = mysql_query($query);
while($row = mysql_fetch_array($answer)){
	$query = "DELETE FROM Buy WHERE user=".$row['id'];
	mysql_query($query);
}
//Buys table in descendant order
$query = "SELECT * , User.login AS loginuser FROM Buy, User, Stock WHERE Buy.user = User.id and Stock.id = Buy.stock ORDER BY Buy.price DESC";
$answer = mysql_query($query);
echo "<table border='1'>";
echo "<tr><td>Stock</td><td>Price</td><td>Volume</td><td>User</td></tr>";
while($row = mysql_fetch_array($answer)){
	//don't print the current user's sell intentions 
	if($row["loginuser"]!==$_SESSION['login']){ 
    	echo "<tr>";
		echo "<td>";	
		//User can see the ticker's details
		echo "<a href='details.php?idticker=".$row["stock"]."&ticker=".$row["ticker"]."'>".$row["ticker"]."</a>"; 
		echo "</td>";
		echo "<td>";
		echo $row["price"];
		echo "</td>";
		echo "<td>";	
		echo $row["volume"]; 
		echo "</td>";
		echo "<td>";	
		echo $row["login"]; 
		echo "</td>";
		echo "</tr>";
	}
} 
echo "</table>";

}


function pricestocks(){

//companies array to don't repeat any company
$companies[] = array();
//query of get the history prices
$query = "SELECT History.id AS idhistory, History.stock, History.price, History.time, History.volume, Stock.ticker, Company.name FROM History, Stock, Company 
WHERE History.stock=Stock.id AND Stock.company=Company.id ORDER BY History.time DESC";
$answer = mysql_query($query);
//query to obtain the current date
$querytoday = "SELECT DISTINCT year(now()) AS year, month(now()) AS month, day(now()) AS day FROM History";
$answertoday = mysql_query($querytoday);
$rowtoday = mysql_fetch_array($answertoday);
$year = $rowtoday["year"];
$month = $rowtoday["month"];
$day = $rowtoday["day"];
$datetoday = $year."-".$month."-".$day." 00:00:00";
echo "<table border='1'>";
echo "<tr><td>Company</td><td>Ticker</td><td>Change%</td><td>Price</td><td>Volume</td><td>Change</td><td>Prev Close</td><td>Date</td></tr>";
 while($row = mysql_fetch_array($answer)){
 	//select the last price of the last day before
 	$querypricebefore = "SELECT History.price AS pricebefore FROM History, Stock WHERE History.stock=Stock.id AND time < '".$datetoday."' AND History.stock = ".$row["stock"]." ORDER BY History.time DESC";
	$answerpricebefore = mysql_query($querypricebefore);
 	while($rowpricebefore = mysql_fetch_array($answerpricebefore)){
		//if the company is not in $companies
 		if (!in_array($row["name"], $companies)){	
    		echo "<tr>";
			echo "<td>";
			echo $row["name"];
			echo "</td>";
			echo "<td>";
			//user can see the ticker's details and send the data by GET method
			echo "<a href='details.php?idticker=".$row["stock"]."&ticker=".$row["ticker"]."&name=".$row["name"]."&price=".$row["price"]."&pricebefore=".$rowpricebefore["pricebefore"]."&volume=".$row["volume"]."&time=".$row["time"]."'>".$row["ticker"]."</a>"; 
			echo "</td>";
			echo "<td>";
			//change%, compare the current price with last price in last day
			$number = 100*($row["price"]-$rowpricebefore["pricebefore"])/$rowpricebefore["pricebefore"];
			//only 2 decimals
			echo round($number,2)."%"; 
			echo "</td>";
			echo "<td>";
			echo "£".$row["price"];
			echo "</td>";
			echo "<td>";
			echo $row["volume"];
			echo "</td>";
			echo "<td>";
            //subtraction of the current price and the last price in last day
			echo "£".($row["price"]-$rowpricebefore["pricebefore"]);
			echo "</td>";
			echo "<td>";
			echo "£".$rowpricebefore["pricebefore"];
			echo "</td>";
			echo "<td>";
			echo $row["time"];
			echo "</td>";
			echo "</tr>";
			//introduce the company in $companies
			array_push($companies, $row["name"]);
		}
	}
} 
echo "</table>";

}

?>