<!-- WRITTEN BY: VAISHNAVI KAKUMANI

	DEBUG BY:VAISHNAVI KAKUMANI -->
<?php session_start();
if(isset($_SESSION['email']))
{
echo "";
}
else
{
	header( "Location: login.php" ); 
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Auto Suggest</title>

<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script type="text/javascript">
	function lookup(ticker) {
		if(ticker.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post("rpc.php", {queryString: ""+ticker+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
	} // lookup
	
	function fill(thisValue) {
		
		$('#ticker').val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
	
		 //window.location = "http://localhost/stocks/ticker.php?tick="+thisValue;
		
	}
	
</script>


<style="font-size:2;font-family:arial;color:black;background-image:url('gray.jpg');background-size:100% 100%;
background-repeat:no-repeat;"></style>
<style type="text/css">
h1 {
	font-family: calibri;
	color: black;
	font-size: 24;
}
 
p {
	font-family: calibri;
	color: black;
	font-size: 20;
}
	body {
		font-family: Helvetica;
		font-size: 11px;
		color: #000;
	}
	
	h3 {
		margin: 0px;
		padding: 0px;	
	}

	.suggestionsBox {
		position: relative;
		left: 30px;
		margin: 10px 0px 0px 0px;
		width: 200px;
		background-color: #212427;
		-moz-border-radius: 7px;
		-webkit-border-radius: 7px;
		border: 2px solid #000;	
		color: #fff;
	}
	
	.suggestionList {
		margin: 0px;
		padding: 0px;
	}
	
	.suggestionList li {
		
		margin: 0px 0px 3px 0px;
		padding: 3px;
		cursor: pointer;
	}
	
	.suggestionList li:hover {
		background-color: #659CD8;
	}
	marquee{
font-family: rrial;
	color: red;
	font-size: 20;
}
</style>

</head>

<body>
<script type="text/javascript">

function tickersubmit()
{
	
	document.tradeStockForm.eventType.value="tickerChangeEvent";
	document.tradeStockForm.submit();
}
function displayTickerDetailsDiv(company,symbol,askPrice,bidPrice,tickerPrice,tickerChange,tickerVolume){
	document.getElementById('company').innerHTML=company;
	document.getElementById('tickerhide').value=company;
	document.getElementById('symbol').innerHTML=symbol;
	document.getElementById("chartDiv").innerHTML='<iframe src = "http://ichart.finance.yahoo.com/w?s='+symbol+'" height ="320" width="700" frameborder="0"/>';
	document.getElementById('askPrice').innerHTML=askPrice+'$';
	document.getElementById('bidPrice').innerHTML=bidPrice+'$';
	document.getElementById('tickerChange').innerHTML=tickerChange+'%';
	document.getElementById('tickerVolume').innerHTML=tickerVolume;
	document.getElementById('tickerDetailsDiv').style.display='inline';
	document.getElementById('ticker').value=symbol;

}
function show_confirm()
{

//checking whether time is between 9.30a.m and 4p.m
/*var todayDate = new Date();
var nineDate=new Date(todayDate.getFullYear(),todayDate.getMonth(),todayDate.getDate(), 9, 30, 0, 0);
var fiveDate = new Date(todayDate.getFullYear(),todayDate.getMonth(),todayDate.getDate(), 16, 0, 0, 0);
if(!((todayDate.getTime()<fiveDate.getTime())&&(todayDate.getTime()>nineDate.getTime())))
{
	alert("Market operates between 9.30a.m & 4.00p.m.Please come back tomorrow to make a transaction");
	return false;
}*/

//validating fields
var quantity=document.getElementById("quantity").value;
var otype="";
var lprices="";
var sprices="";
var buySellAction="";
var i;

//validating quantity
if (quantity==null || quantity=="")
{
  alert("Please enter the number of shares you want to trade with.");
  return false;
}
quantity = quantity.toString();
for (i = 0; i < quantity.length; i++)
{
	var c = quantity.charAt(i);
    if (isNaN(c)) 
	{
		alert("Entered value is not a valid number");
		return false;
	}
}

//validating ordertype
olen = document.tradeStockForm.otype.length;
for (i = 0; i <olen; i++) 
{
	if (document.tradeStockForm.otype[i].checked) 
	{
		otype = document.tradeStockForm.otype[i].value;
		
	}
}

if (otype == "") 
{
	alert("Please choose the type of order");
	return false;
}

//validating buysellaction
buysellen = document.tradeStockForm.buySellAction.length;
for (i = 0; i <buysellen; i++) 
{
	if (document.tradeStockForm.buySellAction[i].checked) 
	{
		buySellAction = document.tradeStockForm.buySellAction[i].value;
	}
}

if (buySellAction == "") 
{
	alert("Please choose the type of action");
	return false;
}
if (otype=="limit")
{
	sprices=document.tradeStockForm.sprice.value;
	if(!(sprices=="" || sprices==null))
	{
	alert("please enter the limit threshold only");
	return false;
	}
	lprices=document.tradeStockForm.lprice.value;
	//var limitflag=isNAN(lprice);
	if (lprices=="" || lprices==null)
	{
		alert("Please enter the threshold for limit order");
		return false;
	}
	else
	{
		lprices = lprices.toString();
		var lflag=0;
		var lfirst=lprices.charAt(0);
		var llast=lprices.charAt(lprices.length-1);
		var lfirstflag=isNaN(lfirst);
		var llastflag=isNaN(llast);
		var lsecondflag=true;
		for (i = 1; i <=lprices.length-2; i++)
		{
			var c = lprices.charAt(i);
			if (isNaN(c)) 
			{
				if(c!='.')
				{
				alert("Entered value is not a valid number");
				lsecondflag=false;
				}
				if(c=='.' && lflag==1)
				{
				lsecondflag=false;
				}
				if(c=='.' && lflag==0)
				{
					
					lflag=1;
					
				}
				
			}
		}
		if(!(!(lfirstflag) && lsecondflag && !(llastflag)))
		{
		alert("Enter a valid limit threshold");
		return false;
		}
	}
}
if (otype=="stop")
{
	lprices=document.tradeStockForm.lprice.value;
	if (!(lprices=="" ||  lprices==null))
	{
		alert("please enter the stop threshold only"); 
		return false;
	}
	sprices=document.tradeStockForm.sprice.value;
	//var stopflag=isNAN(sprice);
	if (sprices=="" ||  sprices==null)
	{
		alert("Please enter the threshold for stop order");
		return false;
	}
	else
	{
		sprices = sprices.toString();
		var sflag=0;
		var sfirst=sprices.charAt(0);
		var slast=sprices.charAt(sprices.length-1);
		var sfirstflag=isNaN(sfirst);
		var slastflag=isNaN(slast);
		var ssecondflag=true;
		for (i = 1; i <=sprices.length-2; i++)
		{
			var c = sprices.charAt(i);
			if (isNaN(c)) 
			{
				if(c!='.')
				{
				alert("Entered value is not a valid number");
				ssecondflag=false;
				}
				if(c=='.' && sflag==1)
				{
				ssecondflag=false;
				}
				if(c=='.' && sflag==0)
				{
					sflag=1;
				}
				
			}
		}
		if(!(!(sfirstflag) && ssecondflag && !(slastflag)))
		{
		alert("Enter a valid stop threshold");
		return false;
		}
	}
}


var ticker   = document.getElementById("ticker").value;

var r=confirm("Order Preview:\n\n Ticker Symbol:"+ticker+" \n\n quantity:"+quantity+"\n\n Action:"+buySellAction+"\n \norder-type:"+otype+"\n\n Submit Order?");

if (r==true)
  {
  alert("Your transaction is being processed.");
  document.tradeStockForm.eventType.value="submitEvent";
  document.tradeStockForm.action="transactionprocess.php";
  document.tradeStockForm.submit();
  }
else
  {
  alert("Order Cancelled");
  }
}
</script>
<table width="100%" height="0" border="0">
<tr>
<td colspan="2" style="background-color:#F6F6F6;text-align:left;">
<?php

//retrieving stock data and storing them in the database
$row = 1;
if (($handle = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=AAPL+ADBE+AMZN+APOL+CSCO+CTSH+DELL+ERIC+GOOG+INFY+INTC+MSFT+ORCL+PROV+QCOM+SNDK+SWIR+VRTU+YHOO+ACCL+ACIW+BBSI+BITS+BLKB+BCSI+CVLT+CNQR+CCUR+PROJ+ELRC+ERII+FFDF+FNGN+GSBC+GRNB+GCBC+HALL+HBNK+HBHC+IBOC+JXSB+JFBC+KINS
+LKFN+SIRI+RAX+VVUS+DNDN+EBAY+RMBS&f=nsabp5p2v", "r")) !== FALSE)
{
	$con = mysql_connect("localhost:3306","","");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("test", $con);
	mysql_query("delete from stockdata");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
	{
		$num = count($data);
		$row++;
		for ($c=0; $c < $num; $c++) 
		{
		   $var[$c]= $data[$c] ;
		}
		//inserting fields into stockdata database
		mysql_query("INSERT INTO stockdata (company,symbol,ask,bid,price,stockchange,stockvolume)
		VALUES ('$var[0]','$var[1]','$var[2]','$var[3]','$var[4]','$var[5]','$var[6]')");
	}
	$result=mysql_query("select symbol,ask,stockchange from stockdata order by symbol;");
	$count=mysql_num_rows($result);
		$i=0;
	while($row = mysql_fetch_array($result))
		  {
			$symbol[$i]=$row['symbol'];
			$ask[$i]=$row['ask'];
			$change[$i]=$row['stockchange'];
			$i++;
		  }
	echo "<marquee>";
	for($j=0;$j<$count;$j++)
	{
	echo "<b>";
	echo $symbol[$j].": ".$ask[$j]."$"."<font style='color:green'>(".$change[$j]."%)   </font>";
	echo "</b>";
	}
	echo "</marquee>";
	mysql_close($con);
    fclose($handle);
}
?>

	
		
				Select Company
				<br />
				<form name="tradeStockForm" action="tradestocks.php" method="post">
				<input type="text" size="30" value="" name="ticker" id="ticker" onkeyup="lookup(this.value);" onblur="fill();" />
				<a onclick= 'tickersubmit()'><CENTER><img border='0' src='plus.jpg' width='15'/></CENTER></a>
			
			
			<div class="suggestionsBox" id="suggestions" style="display: none;">
				<img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
				<div class="suggestionList" id="autoSuggestionsList">
					&nbsp;
				</div>
			</div>
		
	<input type="hidden" id="eventType" name="eventType"/>
	<div id="tickerDetailsDiv" style="display:none">
	<table>
		<tr>
		<td>
		<table align="left"> 
			<tr>
				<td >Company:</td>
				<td><p  id="company"></p></td>
				<td><p  id="tickercompany"></p><input type="hidden" id="tickerhide" name="tickerhide"></td>
			</tr>
			<tr>
				<td>Symbol:</td>
				<td><p  id="symbol"></p></td>
			</tr>
			<tr>
				<td >AskPrice:</td>
				<td><p  id="askPrice"></p></td>
			</tr>
			<tr>
				<td >BidPrice:</td>
				<td><p  id="bidPrice"></p></td>
			</tr>
			<tr>
				<td>Change:</td>
				<td><p  id="tickerChange"></p></td>
			</tr>
			<tr>
				<td>Volume:</td>
				<td><p  id="tickerVolume"></p></td>
			</tr>
		</table>
		</td>
		<td>
		<div id="chartDiv">
		</div>
		</td>
		</tr>
		<tr>
			<td>
		<table>
		<tr>
			<td>
				Quantity:
			</td>
			<td><input type="text" name="quantity" id="quantity"/>
			</td>
		</tr>
		<tr>
		<td>
	Action:</td>
	<td><input type="radio" name="buySellAction" value="buy" id="buySellAction" />Buy
			<input type="radio" name="buySellAction" value="sell"id="buySellAction" />Sell
	</td>
	</tr>
	</table>
	<tr>
	<td>
	</br>Order Type:
	</br></br><input type="radio" name="otype" value="market" id="otype" />Market</br></br>
				<input type="radio" name="otype" value="limit" id="otype" />Limit</br>
				<table><tr><td>Limit Price:</td><td><input type="text" name="lprice"  id="lprice"/></td></tr></table></br></br>
				<input type="radio" name="otype" value="stop" id="otype"/>Stop</br>
				<table><tr><td>Stop Price:</td><td><input type="text" name="sprice"  id="sprice"/></td></tr></table></br>
	</td>
	</tr>
	<tr>
	<td colspan=2>
				<input type="button" onclick="show_confirm()" value="Process" />
	</td>
	</tr>
	</td>
	</tr>
	</table>
	</div>
</form>

	<?php error_reporting (E_ALL ^ E_NOTICE); 
	$eventType = $_POST["eventType"];

	if($eventType == "tickerChangeEvent")
	{
		$ticker=$_POST["ticker"];

		//extracting the stockvalues of ticker symbol and displaying them
		$con= mysql_connect("localhost:3306","","");
		if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db("test", $con);
		
	
		$tickerresult=mysql_query("select * from stockdata where company='$ticker';");
		$rows = mysql_num_rows($tickerresult); 
		while($row = mysql_fetch_array($tickerresult))
		  {
			$tickerCompany = $row['company'];
			$tickerSymbol=$row['symbol'];
			$askPrice=$row['ask'];
			$bidPrice=$row['bid'];
			$tickerPrice=$row['price'];
			$tickerChange=$row['stockchange'];
			$tickerVolume=$row['stockvolume'];
			echo "<SCRIPT LANGUAGE='javascript'>displayTickerDetailsDiv('".$tickerCompany."','".$tickerSymbol."','".$askPrice."','".$bidPrice."','".$tickerPrice."','".$tickerChange."','".$tickerVolume."');</SCRIPT>";
		   }
		
			
		mysql_close($con);
	}
	else if($eventType == "submitEvent")
	{
		$ticker=$_POST["ticker"];
		$quantity=$_POST["quantity"];
		$buysell=$_POST["buySellAction"];
		$ordertype=$_POST["otype"];
		
	}
	?>

</body>
</html> 

