<?
session_start();
include("conexion.php");
$query = "SELECT * FROM User WHERE login ='".$_SESSION['login']."'";
$result = mysql_query($query, $conexion);
$row = mysql_fetch_array($result);

$ownerid = $_GET["ownerid"];
$ticker = $_GET["ticker"];
$volume = $_GET["volume"];
$stock = $_GET["stock"];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sell</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../css/style.css" media="screen" />
</head>

<body>
<div id="title">
	<div id="header">
	<img src="../images/money.png" alt="Money" align="left"/>
	</div>
	<div id="virtualtrade">
	<h1>      
    	<a href="../index.php"><img src="../images/title.png" alt="Virtual Trade"/></a>
    </h1>
	</div>
	<div id="signin">
	<?
	if(isset($_SESSION['login'])) {
		echo "<a href='/infouser.php'>".$_SESSION['login']."</a>";
		echo "&nbsp;&nbsp;";
		echo "<a href='/functions/logout.php'>Sign out</a>";
	}
	else{
	?>
	<a href="/register.php">Register</a>
	<a href="/loginindex.php">Sign in</a>
	<?
	}
	?>
	<br/>
	</div>
</div>
<div id="sellphp">
You are going to sell:
<br/><br/>
<?
echo "Ticker:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;";
echo $ticker;
echo "<br/>";
//query to know the number of stocks that user has of this ticket
$query = "SELECT SUM(Volume) AS Volowner FROM Ownership WHERE stock = ".$stock." AND user=".$row["id"];
$resultowner = mysql_query($query, $conexion);
$rowowner = mysql_fetch_array($resultowner);
//query to know the stock's number that user wants to sell previously
$query = "SELECT SUM(Volume) AS Volsell FROM Sell WHERE stock = ".$stock." AND user=".$row["id"];
$resultsell = mysql_query($query, $conexion);
$rowsell = mysql_fetch_array($resultsell);

//number of stocks that user can sell
$total = $rowowner["Volowner"] - $rowsell["Volsell"];

?>
<script type="text/javascript">
function check()
{

 if(document.forms["formSell"].price.value == "")
 {
 	alert("Write the price");
	document.forms["formSell"].price.focus();
	return false;
	}
	else if(document.forms["formSell"].volume.value == "")
	{
	 alert("You have to write the number of stocks that you want to sell");
	 document.forms["formSell"].volume.focus();
	 return false;
	}
	else if(document.forms["formSell"].price.value < 0)
	{
	 alert("The price can't be negative");
	 document.forms["formSell"].price.focus();
	 return false;
	}
	else if(parseInt(document.forms["formSell"].volume.value) > <? echo $total; ?>)
	{
	 alert("You can't sell all of this stocks");
	 document.forms["formSell"].volume.focus();
	 return false;
	}
	
	else
	{
	 return true;
	}
}
</script>
<form name="formSell" method="post" action="sell2.php?ownerid=<? echo $ownerid; ?>&ticker=<? echo $ticker; ?>&stock=<? echo $stock; ?>">
<table id="wantsell">
<tr>
<td align="left">Price of each stock: </td><td><input type="text" name="price" /></td>
</tr>
<tr>
<td align="left">Volume of stocks:</td><td><input type="text" name="volume" /></td><td align="right">(you can sell no more than <? echo $total; ?>) </td>
</tr>
<tr>

</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Sell" onClick="return check()"/>
</tr>
</table>
<input type="hidden" name="ownerid" value="<? echo $ownerid; ?>"/>
<input type="hidden" name="stock" value="<? echo $stock; ?>"/>
</form>

</div>
</body>
</html>