<?php

include 'db.php';
include 'error.php';
include 'auth.php';

$backgroundID=$_GET["backgroundID"];

$backgroundHeader=$_POST["backgroundHeader"];
$backgroundDetail=$_POST["backgroundDetail"];
$modify=$_POST["modify"];
$modifyID=$_POST["modifyID"];


if (empty($backgroundHeader) || empty($backgroundDetail))
{


	if(empty($backgroundID))
	{
	//new background detail
	include 'header.php';
		?>

				<form name="addBackground" action="background_mod.php" method="post">
				<input type="hidden" name="modify" value="0">
				Background Header:<br>
				<input type="text" name="backgroundHeader" size="60">
				<br>
				Background Detail:<br>
				<textarea name="backgroundDetail" rows="10" cols="45"></textarea>
				<br>
				<input type="submit" value="submit"></form>

		<?php
		include 'footer.php';
	}else
	{
		//Event Id exists so need to modify

		if(!($connection=@mysql_connect($hostName, $dbUsername, $dbPassword)))
			die("Could not connect");

		if(!(mysql_select_db ($databaseName, $connection)))
			showerror();


		$viewQuery="SELECT * FROM background WHERE background_id=$backgroundID";
		//echo($insertQuery);

		if(!($result = @mysql_query($viewQuery,$connection)))
			showerror();

		mysql_close($connection);

		$row=mysql_fetch_array($result);

		include 'header.php';
		?>
				<form name="addBackground" action="background_mod.php" method="post">
				<input type="hidden" name="modify" value="1"><input type="hidden" name="modifyID" value="<?php echo($backgroundID); ?>">
				Background Header:<br>
				<input type="text" name="backgroundHeader" size="60" value="<?php echo($row["background_header"]); ?>">
				<br>
				Background Detail:<br>
				<textarea name="backgroundDetail" rows="10" cols="45"><?php echo($row["background_detail"]); ?></textarea>
				<br>
				<input type="submit" value="submit"></form>

		<?php
		include 'footer.php';
	}





}else
{

	
	$backgroundHeader=clean($backgroundHeader,100);
	$backgroundDetail=clean($backgroundDetail,3000);

	if(!($connection=@mysql_connect($hostName, $dbUsername, $dbPassword)))
		die("Could not connect");

	if(!(mysql_select_db ($databaseName, $connection)))
		showerror();

	if ($modify=="0")
	{
		$insertQuery="INSERT INTO background VALUES(NULL,".
												"\"".$backgroundHeader."\",".
												"\"".$backgroundDetail."\")";
	}else{
		$insertQuery="UPDATE background SET background_header=\"".$backgroundHeader."\", background_detail=\"".$backgroundDetail."\" WHERE background_id=$modifyID";
	}
	
	
	//echo($insertQuery);

	if((@mysql_query($insertQuery,$connection)) && mysql_affected_rows()==1)
		{
			if($modify=="0")
			{
				$modifyID=mysql_insert_id($connection);
				$modifyID.="&isNew=T";
			}				
		//print_r($modifyID);
		header("Location:background_mod_success.php?"."backgroundId=".$modifyID."&status=T");
		}
	else
		header("Location:background_mod_success.php?"."&status=F");




	mysql_close($connection);



}
?>

