<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?PHP
	session_start(); // Initialize session data
	if(! isset($_SESSION['useBT'])){ #If started in the middle go to 1st page
		header("Location: index.php");
	}
	
	if($_SERVER['REQUEST_METHOD'] == 'POST') {
		if(isset($_POST['fSitesUse'])){ //Result selected
			$siteTypes = $_POST['fSitesUse'];
			$_SESSION['fSitesUse'] = array_filter($siteTypes);
			header("Location: pg7.php");
		}
	}
	else{
		echo "<table style=\"width: 800px\" align=\"center\">
			<tr><td><h1 class=\"auto-style1\">BitTorrent Search Engine/Site Survey&nbsp;</h1></td></tr>
			<tr><td style=\"height: 21px\"></td></tr>";
		echo "<table style=\"width: 800px\" align=\"center\">
			<tr><td><p>If you were to perform <strong>20 searches for files, how many of them would go to each </strong>of the following sites (total should be 20)?<p>";

		echo "<form name=\"fSitesUseForm\" method=\"post\" action=\"pg6.php\" onsubmit=\"return checkForm()\">";
		echo "<table>";
		$num = count($_SESSION['sitesUse']); //No of elements in array
		for($i = 0; $i < $num; $i++){
			$tmpStr = "<tr><td>" . $_SESSION['sitesUse'][$i] . "</td><td><input name=\"fSitesUse[]";
			$tmpStr = $tmpStr . "\" type=\"text\" style=\"width:40px\" maxlength=\"2\" value=\"0\" onkeyup=\"calculate()\"/></td></tr>";
			echo $tmpStr;
		}

		echo "<tr><td><hr></td><td><hr></td></tr><tr><td><strong>Total</strong></td><td><input name=\"total\" type=\"text\" style=\"width:40px\" maxlength=\"2\" readonly=\"True\"/></td></tr></table><br />";
		echo "<div class=\"auto-style2\"><input name=\"Next\" type=\"submit\" value=\"Next >>\" /></div>
			</form>
			<p class=\"auto-style3\">Progress 6/8</p></td></tr>
			<tr><td>&nbsp;</td></tr>
			</table>";			
	}
?>


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

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link rel="stylesheet" type="text/css" href="css.css" />
<title>BitTorrent Search Engine Survey</title>
<style type="text/css">
.auto-style1 {
	text-align: center;
}
.auto-style2 {
	text-align: right;
}
.auto-style3 {
	text-align: center;
	font-size: small;
}
</style>


<script type="text/javascript">

function checkForm() //Check whether total is 20
{
	if (document.fSitesUseForm.elements["total"].value == 20)
	{
		return true;
	}
	alert('Total must be 20');
	return false;
}

function calculate() //Find total
{
	var multiSites = document.fSitesUseForm.elements["fSitesUse[]"];
	var count = multiSites.length;
	var total = 0;

	if (count == undefined) //Only 1 element
	{
		if (isNaN(document.fSitesUseForm["fSitesUse[]"].value))
		{
			document.fSitesUseForm["fSitesUse[]"].value = "";
		}
		else
		{
			if (document.fSitesUseForm["fSitesUse[]"].value != "")
			{
				total += parseInt(document.fSitesUseForm["fSitesUse[]"].value);
			}
		}
	}
	else
	{
		for(var i = 0; i < count; i++)
		{
			if (isNaN(multiSites[i].value))
			{
				multiSites[i].value = "";
			}
			else
			{
				if (multiSites[i].value == "")
				{
					continue;
				}
				total += parseInt(multiSites[i].value);
			}
		}
	}
	document.fSitesUseForm.elements["total"].value = total;
}
</script>
</head>

<body>
</body>

</html>
