<?

////////////////////////////////
$EnforceSession = true;
include "includes/includes.php";
////////////////////////////////

// If we are editing
if(request("update") == "true")
{
	// Update the scheme
	while (list($key, $val) = each($gblScheme)) {
	   
	   // Update it
	   $query = "UPDATE scheme SET value = '".SQLEncode(request($key))."' WHERE site = '" . $gblSiteName . "' AND name = '".SQLEncode($key)."'".
	   SqlExecute($query);	   
	}
	
	// Redirect
	header("Location: scheme.php?" . $GlobalQuery);
	
	// Done
	exit;
}

// Header
include "includes/header.php";


// Begin Template
$tp = new PhpTemplate;
for( $tp->StartFromFile( "templates/scheme.template" ); $tp->NextAndEchoOutput(); ) 
{

	CheckGlobalEscapes( $tp );

	if( $tp->IsEscape("SCHEME") )
	{
		
		// Connect
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);
		
		// Retrieve data
		$query = "SELECT * FROM scheme WHERE site = '" . $gblSiteName . "' ORDER BY name ASC";
		$result = mysql_query($query, $db);
						
		// Loop results
		for( $RowIndex = 0; $a = mysql_fetch_array($result); $RowIndex++ )
		{
			$Record = new PhpSqlRow;
			$Record->SetAsArray($a);

			$tpElem = new PhpTemplate;						
			for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
			{
			
				CheckGlobalEscapes($tpElem);
				
				if( $tpElem->IsEscape("NAME") ) echo $Record->GetValue("name");
				else if( $tpElem->IsEscape("VALUE") ) echo htmlentities($Record->GetValue("value"));
				else if( $tpElem->IsEscape("IF-ODD-ROW") && ($RowIndex % 2) == 1 ) echo $tpElem->GetEscapeArgument();								
			}
		}			
	}
}

// Footer
include "includes/footer.php";

?>
