<?php
include "header.php";

if ($_POST["submit"] == 1)
{
	$title = $_POST["title"];
	$startdate = $_POST["startdate"];
	$stopdate = $_POST["stopdate"];
	$venue = $_POST["venue"];
	$descrip = $_POST["descrip"];
	$ldesc = $_POST["ldesc"];
	
	$query = "insert into events(title, startdate, stopdate, venue, descrip, ldesc)";
	$query .= " values ('$title', '$startdate', '$stopdate', '$venue', '$descrip', '$ldesc')";
	//echo $query; exit;
	mysql_query($query) or die (mysql_error());
	
	$result = mysql_query("select last_insert_id() from events") or die (mysql_error());
	while ($row = mysql_fetch_array($result))
	{
		$eventid = $row[0];
	}
	
	require "upload.php";
	
	//upload images
	$path = "files/";
	$acceptable_file_types = "image/gif|image/jpeg|image/pjpeg";
	$default_extension = "";
	$mode = 2;
	$my_uploader = new uploader('en');
	$my_uploader->max_filesize(100000);
	$my_uploader->max_image_size(800, 800);
	
	$upload_file_name = "userfile";
	if ($my_uploader->upload($upload_file_name, $acceptable_file_types, $default_extension)) 
	{
		$my_uploader->save_file($path, $mode);
	}
		
	if ($my_uploader->error && $my_uploader->error != "No file was uploaded")
	{
		$err = "Uploading Picture" . $my_uploader->error;
		echo $err; exit;
	}
	elseif (! $my_uploader->error)
	{
		$imgpath = $path . $my_uploader->file['name'];
		mysql_query("update events set imgpath='$imgpath' where eventid='$eventid'") or die (mysql_error());
	}
	
	//upload papers
	$path = "files/";
	$acceptable_file_types = "application/pdf";
	$default_extension = "";
	$mode = 2;
	$my_uploader = new uploader('en');
	$my_uploader->max_filesize(1000000);
	
	$upload_file_name = "bro";
	if ($my_uploader->upload($upload_file_name, $acceptable_file_types, $default_extension)) 
	{
		$my_uploader->save_file($path, $mode);
	}
		
	if ($my_uploader->error && $my_uploader->error != "No file was uploaded")
	{
		$err = "Uploading Bordchure" . $my_uploader->error;
		echo $err; exit;
	}
	elseif (! $my_uploader->error)
	{
		$imgpath = $path . $my_uploader->file['name'];
		mysql_query("update events set bropath='$imgpath' where eventid='$eventid'") or die (mysql_error());
	}
	//exit;
	?>
	<script>window.location='index.php'</script>
	<?php
}
?>

<script language="javascript">
function checkform(form)
{
	if (form.title.value == "")
	{
		alert ("Please enter evnet title");
		form.title.focus();
		return false;
	}
	return true;
}
</script>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" onSubmit="return(checkform(this))" name="form1" enctype="multipart/form-data">
<table align="left" cellpadding="0" cellspacing="3" border="0">
  <tr>
    <td>Event Title</td>
    <td><input type="text" name="title" value="<?php echo $title; ?>" maxlength="150"></td>
  </tr>
  
  <tr>
    <td>Start Date</td>
    <td><input type="text" name="startdate" value="<?php echo $startdate; ?>" maxlength="10" size=12 readonly onfocus='form1.bt.click()'>
    <script language='javascript'>
<!--
if (!document.layers)
{
	document.write("<input type=button name='bt' onclick='popUpCalendar(this, form1.startdate, \"yyyy-mm-dd\")' value='select' style='font-size:11px'>")
}
//-->
</script>
</td>
  </tr>
  
  <tr>
    <td>End Date</td>
    <td><input type="text" name="stopdate" value="<?php echo $stopdate; ?>" maxlength="10" size=12 readonly onfocus='form1.bt1.click()'>
    <script language='javascript'>
<!--
if (!document.layers)
{
	document.write("<input type=button name='bt1' onclick='popUpCalendar(this, form1.stopdate, \"yyyy-mm-dd\")' value='select' style='font-size:11px'>")
}
//-->
</script>
    </td>
  </tr>
  
  <tr>
    <td>Venue</td>
    <td><input type="text" name="venue" value="<?php echo $venue; ?>" maxlength="250"></td>
  </tr>
  
  <tr>
    <td valign="top">Short Description</td>
    <td><textarea name="descrip" rows="5" cols="20" wrap="virtual" onKeyDown="textCounter(this, 250);" onKeyUp="textCounter(this, 250);"><?php echo $descrip; ?></textarea></td>
  </tr>
  
  <tr><td valign="top">Long Description</td><td><textarea name="ldesc" rows="5" cols="20" wrap="virtual" onKeyDown="textCounter(this, 5000);" onKeyUp="textCounter(this, 5000);"><?php echo $ldesc; ?></textarea></td></td>
  
  <tr>
    <td valign="top">Event Images</td>
    <td><input type="file" name="userfile"></td>
  </tr>

  <tr>
    <td valign="top">Event Brodchure</td>
    <td><input type="file" name="bro"></td>
  </tr>
  
  <tr><td colspan="2" align="center"><input type="hidden" name="submit" value="1"><input type="submit" value=" Add ">&nbsp;<input type="reset" value="Cancel"></td></tr>
</table>
</form>

<?php
include "footer.php";
?>