<?php
session_start();
include("../../connection.php");

if(isset($_GET['saveAnItem'])){

	$description = $_GET['description'];
	$description = str_replace("'","&#039;",$_GET['description']);
	#$description = mysql_real_escape_string($description);	// If you have support for mysql_real_escape_string

	if(isset($_GET['newItem'])){	// This is a new item 
		// Save the item to the server
		$sql = "insert into MINICLICK.CALENDAR(Description,Color,StartDate,EndDate,User_Id)
		values('".$description."','".$_GET['bgColorCode']."','".date("Y-m-d H:i:s",strtotime($_GET['eventStartDate']))."','".date("Y-m-d H:i:s",strtotime($_GET['eventEndDate']))."','".$_SESSION['user_id']."')";
		$db = mysql_query($sql);
		$id = mysql_insert_id();
		echo $id;	// The id is sent back to ajax so that it could update the id of the entry, i.e. update it next time instead of saving another new item.
	}else{
		$sql = "update MINICLICK.CALENDAR set Description='".$description."',Color='".$_GET['bgColorCode']."',
		StartDate='".date("Y-m-d H:i:s",strtotime($_GET['eventStartDate']))."',
		EndDate='".date("Y-m-d H:i:s",strtotime($_GET['eventEndDate']))."' where Calendar_Id='".$_GET['id']."'";
		mysql_query($sql);
	}	
}
?>