Location: PHPKode > projects > Silicon Fodder > SiliconFodder/DocumentRoot/diary/addDiary.php
<? require_once("../common/nocache.php"); ?>
<? require_once("../common/appobjects.php"); ?>
<? require_once("../common/db.php"); ?>
<? require_once("../common/checkIsLoggedIn.php"); ?>
<? require_once("../util/general.php"); ?>
<? require_once("../diary/util.php"); ?>
<? require_once("../admin/util.php"); ?>
<?
if($HTTP_GET_VARS["meetingaction"] != "") {
	$linkedId = $HTTP_GET_VARS["linkedId"];
	$meetingaction = 0;
	if($HTTP_GET_VARS["meetingaction"] == "confirm") $meetingaction = 1;
	if($HTTP_GET_VARS["meetingaction"] == "decline") $meetingaction = 2;
	$mySQL = "UPDATE diary SET meetingstatus=".$meetingaction." WHERE linkedId=".$linkedId." AND userid=".$HTTP_SESSION_VARS["mwoid"];
	gendb_query($mySQL, $myDb) or die(gendb_error());
	?>
	<html>
	<head>
	<script language="javascript">
		if(opener) {
			if(opener.parent.diaryMonths)
				opener.parent.diaryMonths.location='months.php';
			else
				opener.location.reload();
		}
		self.close();
	</script>
	</head>
	</html>
	<?
	exit;
}

if($HTTP_POST_VARS["posted"] == "1") {
	$did          = $HTTP_POST_VARS["did"];
	$myDay1       = $HTTP_POST_VARS["ad1"];
	$myMonth1     = $HTTP_POST_VARS["am1"];
	$myYear1      = $HTTP_POST_VARS["ay1"];
	$myDay2       = $HTTP_POST_VARS["ad2"];
	$myMonth2     = $HTTP_POST_VARS["am2"];
	$myYear2      = $HTTP_POST_VARS["ay2"];
	$pageaction   = $HTTP_POST_VARS["pageaction"];
	$eventType    = $HTTP_POST_VARS["eventType"];
	$oldEventType = $HTTP_POST_VARS["oldEventType"];
	$p2           = $HTTP_POST_VARS["p2"];
	$recurring    = $HTTP_POST_VARS["recurring"];
	$clientId     = $HTTP_POST_VARS["client"];
	$project	  = $HTTP_POST_VARS["proj"];
	$notTime      = $HTTP_POST_VARS["notTime"];
	$mulUsersCsv  = $HTTP_POST_VARS["mulUsersCsv"];
	$linkedId     = $HTTP_POST_VARS["linkedId"];
	$smartdb      = $HTTP_POST_VARS["sdb"];
	$auth         = $HTTP_POST_VARS["auth"];
	$diaryEvent   = $HTTP_POST_VARS["diaryEvent"];
	$taskuser	  = $HTTP_POST_VARS["taskuser"];
	$private	  = $HTTP_POST_VARS["private"];

	if($smartdb != "") mysql_select_db($smartdb, $myDb);

	if($recurring == "") $recurring = "-1";
	if($clientId == "") $clientId ="null";
	if($eventType == "") $eventType = "0";
	if($notTime == "") $notTime = "-1";
	if($project == "") $project = "0";
	if($private == "") $private = "0";
	
	// is someone adding holiday, and holidays need authorising?
	if($eventType == $holidayid && $oldEventType != $holidayid && getApplicationObject("webofficeauthhols") == "yes" && !isInGroup("Holiday Admin", true)) {
		$eventType = 993;
	} elseif($auth != "") {
		// we are authorising (or not) a holiday request
		if($auth == "true") {
			$eventType = $holidayid;
		} else {
			$eventType = 0;
			$diaryEvent = "HOLIDAY WAS NOT AUTHORISED - ".$diaryEvent;
		}
	}

	$myDate1  = strtotime($arrMonths[$myMonth1]." ".$myDay1.", ".$myYear1);
	$myDate2  = strtotime($arrMonths[$myMonth2]." ".$myDay2.", ".$myYear2);

	$startTime  = $HTTP_POST_VARS["startTime"];
	$endTime    = $HTTP_POST_VARS["endTime"];

	$dateTime1  = getDateForDb($myDate1, $startTime);
	$dateTime2  = getDateForDb($myDate2, $endTime);

	$diaryEvent = db_make_safe($diaryEvent);

	if($mulUsersCsv == "all" && $eventType != 998) {            // special case of all (if not a meeting!)
		$userId = 0;
		$mulUsersCsv = "";
	} elseif($mulUsersCsv == "all" && $eventType == 998) {
		// as it's a meeting, all must be a list of all users...
		$mulUsersCsv = "";
		$mySQL = "SELECT id FROM users WHERE owneruserid=0 AND isDeleted=0 AND isClientContact=0 AND isSuperUser=0 AND isBusCentreAdmin=0 AND companyId=".$myCompId;
		$rows = gendb_query($mySQL, $myDb) or die(gendb_error());
		while($row = gendb_fetch_assoc($rows)) {
			$mulUsersCsv.= $row["id"].",";
		}
		gendb_free_result($rows);
		$mulUsersCsv = ereg_replace(",$", "", $mulUsersCsv);
	} elseif(!ereg(",", $mulUsersCsv)) {   // just one user selected if there's no comma in mulUsersCsv
		$userId = $mulUsersCsv;
		$mulUsersCsv = "";
		if($taskuser != "") $userId = $taskuser;
	}

	$nextNotificationDate = getNextNotificationDate($dateTime1, $notTime, $recurring);

	if($pageaction == "add" && $mulUsersCsv == "") {
		// either adding event for me, someone else, or for 'all' (i.e. not 'linked')
		clearBusyDaysCacheForUser($userId);
		$mySQL = "INSERT INTO diary (userid, starttime, endtime, event, eventType, isPrivate, addedBy, notificationTime, clientid, recurring, nextNotificationDate, companyId, projectId) VALUES (".$userId.",'".$dateTime1."','".$dateTime2."','".$diaryEvent."',".$eventType.",".$private.",".$HTTP_SESSION_VARS["mwoid"].",".$notTime.",".$clientId.",".$recurring.",".$nextNotificationDate.",".$myCompId.",".$project.")";
		gendb_query($mySQL, $myDb) or die(gendb_error());

	} elseif($pageaction == "add" && $mulUsersCsv != "") {
		// adding for multiple people - first, find largest linkedId in DB
		$maxLinkedId = getSingleValueFromSQL("SELECT MAX(linkedId) AS maxLinkedId FROM diary", "maxLinkedId");
		if($maxLinkedId == "") {
			$maxLinkedId = "1";
		} else {
			$maxLinkedId++;
		}
		
		// add a record for each user
		$mulUserIds = split(",", $mulUsersCsv);
		foreach($mulUserIds as $singleUserId) {
			clearBusyDaysCacheForUser($singleUserId);
			$mySQL = "INSERT INTO diary (userid, starttime, endtime, event, eventType, addedBy, notificationTime, clientid, recurring, nextNotificationDate, linkedId, companyId) VALUES (".$singleUserId.",'".$dateTime1."','".$dateTime2."','".$diaryEvent."',".$eventType.",".$HTTP_SESSION_VARS["mwoid"].",".$notTime.",".$clientId.",".$recurring.",".$nextNotificationDate.",".$maxLinkedId.",".$myCompId.")";
			gendb_query($mySQL, $myDb) or die(gendb_error());
		}

	} elseif($pageaction == "delete") {
		if($linkedId > 0) {
			$mySQL = "SELECT id FROM diary WHERE isDeleted=0 AND linkedId=".$linkedId;
			$rows = gendb_query($mySQL, $myDb) or die(gendb_error());
			while($row = gendb_fetch_assoc($rows)) {
				$loopDid = $row["id"];
				clearBusyDaysCacheFromDiaryId($loopDid);
				$mySQL2 = "UPDATE diary SET isDeleted=1 WHERE id=".$loopDid;
				gendb_query($mySQL2, $myDb) or die(gendb_error());
			}
			gendb_free_result($rows);

		} else {
			clearBusyDaysCacheFromDiaryId($HTTP_POST_VARS["did"]);
			$mySQL = "UPDATE diary SET isDeleted=1 WHERE id=".$did;
			gendb_query($mySQL, $myDb) or die(gendb_error());
		}

	} else {
		// editing...
		if($linkedId > 0) { // a record that was linked... (could still be, don't know yet)
			if($mulUsersCsv != "") { // record still is linked (i.e. still multiple)
				// list of multiple users may have changed... need to delete existing ones first...
				$mySQL = "SELECT id FROM diary WHERE isDeleted=0 AND linkedId=".$linkedId;
				$rows = gendb_query($mySQL, $myDb) or die(gendb_error());
				while($row = gendb_fetch_assoc($rows)) {
					$loopDid = $row["id"];
					clearBusyDaysCacheFromDiaryId($loopDid);
					$mySQL2 = "UPDATE diary SET isDeleted=1 WHERE id=".$loopDid;
					gendb_query($mySQL2, $myDb) or die(gendb_error());
				}
				gendb_free_result($rows);

				// then do a fresh add!
				$mulUserIds = split(",", $mulUsersCsv);
				foreach($mulUserIds as $singleUserId) {
					clearBusyDaysCacheForUser($singleUserId);
					$mySQL = "INSERT INTO diary (userid, starttime, endtime, event, eventType, addedBy, notificationTime, clientid, recurring, nextNotificationDate, linkedId, companyId) VALUES (".$singleUserId.",'".$dateTime1."','".$dateTime2."','".$diaryEvent."',".$eventType.",".$HTTP_SESSION_VARS["mwoid"].",".$notTime.",".$clientId.",".$recurring.",".$nextNotificationDate.",".$linkedId.",".$myCompId.")";
					gendb_query($mySQL, $myDb) or die(gendb_error());
				}
			} else { // record was linked, now isn't... so need to delete linked ones, and add new one
				$mySQL = "SELECT id FROM diary WHERE isDeleted=0 AND linkedId=".$linkedId;
				$rows = gendb_query($mySQL, $myDb) or die(gendb_error());
				while($row = gendb_fetch_assoc($rows)) {
					$loopDid = $row["id"];
					clearBusyDaysCacheFromDiaryId($loopDid);
					$mySQL2 = "UPDATE diary SET isDeleted=1 WHERE id=".$loopDid;
					gendb_query($mySQL2, $myDb) or die(gendb_error());
				}
				gendb_free_result($rows);

				// add new event
				clearBusyDaysCacheForUser($userId);
				$mySQL = "INSERT INTO diary (userid, starttime, endtime, event, eventType, addedBy, notificationTime, clientid, recurring, nextNotificationDate, companyId) VALUES (".$userId.",'".$dateTime1."','".$dateTime2."','".$diaryEvent."',".$eventType.",".$HTTP_SESSION_VARS["mwoid"].",".$notTime.",".$clientId.",".$recurring.",".$nextNotificationDate.",".$myCompId.")";
				gendb_query($mySQL, $myDb) or die(gendb_error());
			}

		} else { // the record wasn't linked... but is it now?
			if($mulUsersCsv != "") { // record is now linked
				//  need to delete old record...
				clearBusyDaysCacheFromDiaryId($did);
				$mySQL = "UPDATE diary SET isDeleted=1 WHERE id=".$did;
				gendb_query($mySQL, $myDb) or die(gendb_error());
				
				// then add linked records - get biggest linkedid first...
				$maxLinkedId = getSingleValueFromSQL("SELECT MAX(linkedId) AS maxLinkedId FROM diary", "maxLinkedId");
				if($maxLinkedId == "") {
					$maxLinkedId = "1";
				} else {
					$maxLinkedId++;
				}
				
				// then add multiple
				$mulUserIds = split(",", $mulUsersCsv);
				foreach($mulUserIds as $singleUserId) {
					clearBusyDaysCacheForUser($singleUserId);
					$mySQL = "INSERT INTO diary (userid, starttime, endtime, event, eventType, addedBy, notificationTime, clientid, recurring, nextNotificationDate, linkedId, companyId) VALUES (".$singleUserId.",'".$dateTime1."','".$dateTime2."','".$diaryEvent."',".$eventType.",".$HTTP_SESSION_VARS["mwoid"].",".$notTime.",".$clientId.",".$recurring.",".$nextNotificationDate.",".$maxLinkedId.",".$myCompId.")";
					gendb_query($mySQL, $myDb) or die(gendb_error());
				}
				
			} else { // was for one user (or 'all', of course), and still is for one user
				// clear cache for old event, and also for userid (in case userid has changed)
				clearBusyDaysCacheFromDiaryId($HTTP_POST_VARS["did"]);
				clearBusyDaysCacheForUser($userId);
				$mySQL = "UPDATE diary SET userid=".$userId.", isPrivate=".$private.", event='".$diaryEvent."', starttime='".$dateTime1."', endtime='".$dateTime2."', eventType=".$eventType.", notificationTime=".$notTime.", recurring=".$recurring.", nextNotificationDate=".$nextNotificationDate." WHERE id=".$did;
				gendb_query($mySQL, $myDb) or die(gendb_error());
			}
		}
	}

	// if we have added a meeting, send out messages
	if($eventType == "998" && $pageaction == "add") {
		$mulUserIds = split(",", $mulUsersCsv);
		foreach($mulUserIds as $singleUserId) {
			if($singleUserId != $HTTP_SESSION_VARS["mwoid"]) {
				$mySQL = "INSERT INTO messages (userid, senderid, message, dtmSent, meetingId) VALUES (".$singleUserId.", ".$HTTP_SESSION_VARS["mwoid"].", '".db_make_safe($HTTP_SESSION_VARS["mwofirstname"]." ".$HTTP_SESSION_VARS["mwolastname"]." has requested a meeting:<br><br>").$diaryEvent."', NOW(), ".$maxLinkedId.")";
				gendb_query($mySQL, $myDb) or die(gendb_error());
			}
		}
	}

	if($clientId != "null" && $pageaction == "add") {
		// adding a diary event linked to a client - need to go back to client page
		?>
		<html>
		<head>
		<script language="javascript">
			opener.top.main.location='/clients/index.php?userID=<?=$clientId?>';
			self.close();
		</script>
		</head>
		</html>
		<?
		exit;
	} else {
		// adding/amending event, need to check whether to refresh main page below
		?>
		<html>
		<head>
		<script language="javascript">
			if(opener) {
				if(opener.parent.diaryMonths)
					opener.parent.diaryMonths.location='months.php';
				else if(opener.location.toString().indexOf('clients/userItem.php') > -1)
					opener.location.reload();
				else if(opener.location.toString().indexOf('search/index.php') > -1)
					opener.location.reload();
				else if(opener.location.toString().indexOf('welcomeServ.php') > -1)
					opener.location.reload();
				else if(opener.location.toString().indexOf('mytasks') > -1)
					opener.location.reload();
			}
			self.close();
		</script>
		</head>
		</html>
		<?
		exit;
	}
}

$pageaction = "";
$smartdb = $HTTP_GET_VARS["sdb"];
if($smartdb != "") mysql_select_db($smartdb, $myDb);
$userids = $HTTP_GET_VARS["userids"]; // are we viewing a collection of users' diaries? (we use this to drive what's shown in the 'change users' popup)

if($HTTP_GET_VARS["did"] != "") {
	// Open existing diary event
	$pageaction = "edit";

	$mySQL = "SELECT diary.*, users.firstname AS addedFName, users.lastname AS addedLName, users_1.firstname, users_1.lastname, users_1.companyName FROM (diary LEFT OUTER JOIN users users_1 ON diary.clientid = users_1.id) LEFT OUTER JOIN users ON diary.addedBy = users.id WHERE diary.id = ".$HTTP_GET_VARS["did"];
	$rows = gendb_query($mySQL, $myDb) or die(gendb_error());
	$row = gendb_fetch_assoc($rows);
	$starttime     = $row["starttime"];
	$endtime       = $row["endtime"];
	$userId        = $row["userid"];
	$myEvent       = $row["event"];
	$eventType     = $row["eventType"];
	$addedBy       = $row["addedBy"];
	$notTime       = $row["notificationTime"];	// time before event to notify (in hours)
	$clientId      = $row["clientid"];
	$recurring     = $row["recurring"];
	$meetingstatus = $row["meetingstatus"];
	$addedByName   = $row["addedFName"]." ".$row["addedLName"];
	$clientName    = $row["firstname"]." ".$row["lastname"];
	$companyName   = $row["companyName"];
	$projectId	   = $row["projectId"];
	$isPrivate	   = $row["isPrivate"];
	if($companyName != "") {
		$clientName = $clientName.", ".$companyName;
	}
	$linkedId    = $row["linkedId"];
	gendb_free_result($rows);

	$myDay1       = substr($starttime, 8, 2);
	$myMonth1     = substr($starttime, 5, 2) - 1;
	$myYear1      = substr($starttime, 0, 4);

	$myDay2       = substr($endtime, 8, 2);
	$myMonth2     = substr($endtime, 5, 2) - 1;
	$myYear2      = substr($endtime, 0, 4);

	$myStartTime = substr($starttime, 11, 5);
	$myEndTime   = substr($endtime, 11, 5);

	$myDate1  = strtotime($arrMonths[$myMonth1]." ".$myDay1.", ".$myYear1);
	$myDate2  = strtotime($arrMonths[$myMonth2]." ".$myDay2.", ".$myYear2);
	
	if($linkedId == 0 && $userId != 0) {
		// If we're opening an event that's for just one person...
		$mulUsersCsv = $userId;
	} elseif($userId == 0) {
		// Event for 'all'
		$mulUsersCsv = "all";
	} else {
		// We're opening a linked item
		$mySQL = "SELECT userid FROM diary WHERE isDeleted=0 AND linkedId=".$linkedId;
		$rows = gendb_query($mySQL, $myDb) or die(gendb_error());
		while($row = gendb_fetch_assoc($rows)) {
			if($mulUsersCsv == "") {
				$mulUsersCsv.= $row["userid"];
			} else {
				$mulUsersCsv.= ",".$row["userid"];
			}
		}
		gendb_free_result($rows);
	}

	// is this a meeting that someone else has requested?
	if($eventType == 998 && $addedBy != $HTTP_SESSION_VARS["mwoid"] && $meetingstatus == 0) {
		$showAcceptDecline = true;
	}
	// is this un-authorised holiday request?
	if($eventType == 993 && isInGroup("Holiday Admin", true)) {
		$showAuthNotauth = true;
	}
	
} else {
	$pageaction = "add";

	if($HTTP_GET_VARS["client"] != "") {

		$mySQL = "SELECT firstname, lastname, users.companyName AS usercomp, companies.companyname AS compcomp FROM users LEFT JOIN companies ON users.companiesId=companies.id WHERE users.id=".$HTTP_GET_VARS["client"];
		$rows = gendb_query($mySQL, $myDb) or die(gendb_error());
		$row = gendb_fetch_assoc($rows);
		$clientName  = $row["firstname"]." ".$row["lastname"];
		$companyName = $row["compcomp"];
		if($companyName == "") $companyName = $row["usercomp"];
		if($companyName != "") {
			$clientName = $clientName.", ".$companyName;
		}
		gendb_free_result($rows);
	}

	$myDay1   = $HTTP_GET_VARS["ad"];
	$myMonth1 = $HTTP_GET_VARS["am"];
	$myYear1  = $HTTP_GET_VARS["ay"];

	$myStartTime  = $HTTP_GET_VARS["startTime"];
	$myEndTime    = $HTTP_GET_VARS["endTime"];

	$myDay2   = $myDay1;
	$myMonth2 = $myMonth1;
	$myYear2  = $myYear1;

	$myDate1  = strtotime($arrMonths[$myMonth1]." ".$myDay1.", ".$myYear1);
	$myDate2  = strtotime($arrMonths[$myMonth2]." ".$myDay2.", ".$myYear2);

	$p2       = $HTTP_GET_VARS["p2"];
	$p2n      = $HTTP_GET_VARS["p2n"];
	$clientId = $HTTP_GET_VARS["client"];
	$task     = $HTTP_GET_VARS["task"];
	$project  = $HTTP_GET_VARS["proj"];

	if($pageaction == "add" && isDiaryAdminUser() && $clientId != "") {
		$myEvent = "Appointment: ".$clientName;
	}

	if($task == "true") {
		$eventType = 997;
	}
	
	$recurring = -1;
	$notTime = -1;

	if($p2 != "") {
		$mulUsersCsv = $p2;
	} else {
		$mulUsersCsv = $HTTP_SESSION_VARS["mwoid"];
	}

}

$diaryViewName = "Diary";
if($eventType == 998) $diaryViewName = "Meeting";
if($task == "true") $diaryViewName = "Add task";
?>
<html>
<head>
<title>My Web Office - Popup</title>
<? require_once("../common/js.php"); ?>
<style>
<? require_once("../common/Lite.php"); ?>
body {
	background-color: #FFFFD5;
}
.month {
	background-color: #9EBEF5;
	font-weight: bold;
	height:40px;
}
.day {
	border-bottom: 1px solid #000000;
}
td,div {
	color: #000000;
	font-size: 8pt;
	font-family: Verdana, Arial, Helvetica;
	padding-left:15px;
	padding-right:15px;
}
.thisWeek {
	background-color: #FBE694;
}
.week {
	cursor: pointer;
}
a {
	color: <?=getApplicationObject("LINK_COLOUR")?>;
	text-decoration: none;
}
</style>
<script language="javascript">
var diaryEventHasBeenChanged = false;
function closeForm() {
	if(!diaryEventHasBeenChanged || confirm('You have amended this record - are you sure you want to close without saving?'))
		self.close();
	
}
function submitForm() {
<?if($pageaction != "add") { ?>
	if(!diaryEventHasBeenChanged) { self.close(); return; }
<? } ?>

	var sel1, day1, mon1, year1, sel2, day2, mon2, year2, event;

<?if($eventType==997 || $eventType==996) { // If task - set endtime date to end of today?>
	document.frm.ad2.value = document.frm.ad1.value;
	document.frm.am2.value = document.frm.am1.value;
	document.frm.ay2.value = document.frm.ay1.value;
	document.frm.endTime.value = "23:30";
	sel2 = document.frm.endTime.value;
<? } else { ?>
	sel2 = document.frm.endTime.selectedIndex;
<? } ?>
	
	sel1 = document.frm.startTime.selectedIndex;
	day1 = Number(document.frm.ad1.value);
	mon1 = Number(document.frm.am1.value);
	year1 = Number(document.frm.ay1.value);

	day2 = Number(document.frm.ad2.value);
	mon2 = Number(document.frm.am2.value);
	year2 = Number(document.frm.ay2.value);

	event = document.frm.diaryEvent.value;
	if(event == '') {
		alert('Please enter the diary event');
		return;
	}
	if((year2 < year1) || (year1==year2 && mon2 < mon1) || (year1==year2 && mon1==mon2 && day2 < day1) || (year1==year2 && mon1==mon2 && day2 == day1 && sel2 <= sel1)) {
		alert('End time must be after start time!');
		return;
	}
	if(document.frm.mulUsersCsv && document.frm.mulUsersCsv.value == "all" && document.frm.notTime && document.frm.notTime.selectedIndex>0) {
		alert('Sorry - notifications cannot be used when adding an event for all users');
		return;
	}
	if(document.frm.mulUsersCsv && document.frm.mulUsersCsv.value == "") {
		alert('Please select users');
		return;
	}
	if(document.frm.recurring && document.frm.recurring.selectedIndex>0 && (year1!=year2 || mon1!=mon2 || day1!=day2)) {
		alert('Sorry - reccurring events cannot span days');
		return;
	}
	if(document.frm.recurring && document.frm.recurring.selectedIndex>0 && document.frm.eventType.options && document.frm.eventType.options[document.frm.eventType.selectedIndex].value == 998) {
		alert('Sorry - invited meetings cannot also be reccurring');
		return;
	}
	if(document.frm.recurring && document.frm.recurring.selectedIndex>0 && document.frm.eventType.options && (document.frm.eventType.options[document.frm.eventType.selectedIndex].value == 997 || document.frm.eventType.options[document.frm.eventType.selectedIndex].value == 996)) {
		alert('Sorry - tasks cannot be reccurring');
		return;
	}
	if((document.frm.mulUsersCsv.value.toString().indexOf(",") > -1 || document.frm.mulUsersCsv.value == "all") && (document.frm.eventType.options[document.frm.eventType.selectedIndex].value == 997 || document.frm.eventType.options[document.frm.eventType.selectedIndex].value == 996)) {
		alert('Sorry - tasks be added for one person only');
		return;
	}
	document.frm.submit();
}
function setImgSrc(imageName, imageLocation) {
	if (document.images) document.images[imageName].src = imageLocation;
	return true;
}
function setDate(myDay, myMonth, myYear, dateField) {
	arrMonths = new Array(12);
	arrMonths[0] = "January";
	arrMonths[1] = "February";
	arrMonths[2] = "March";
	arrMonths[3] = "April";
	arrMonths[4] = "May";
	arrMonths[5] = "June";
	arrMonths[6] = "July";
	arrMonths[7] = "August";
	arrMonths[8] = "September";
	arrMonths[9] = "October";
	arrMonths[10] = "November";
	arrMonths[11] = "December";
	getElement("date" + dateField).innerHTML = myDay + "&nbsp;" + arrMonths[myMonth - 1] + "&nbsp;" + myYear;
	if(dateField == 1) {
		document.frm.ad1.value = myDay;
		document.frm.am1.value = myMonth - 1;
		document.frm.ay1.value = myYear;
	} else {
		document.frm.ad2.value = myDay;
		document.frm.am2.value = myMonth - 1;
		document.frm.ay2.value = myYear;
	}
	diaryEventHasBeenChanged = true;
}
function getElement(id) {
	var x;
	if (document.getElementById) {
		x = document.getElementById(id);
	}
	else if (document.all) {
		x = document.all[id];
	}
	else if (document.layers) {
		x = document.layers[id];
	}
	return x;
}
function popupCal(dateField) {
	var dd, mm, yy;
	if(dateField == 1) {
		dd = document.frm.ad1.value;
		mm = document.frm.am1.value;
		yy = document.frm.ay1.value;
	} else {
		dd = document.frm.ad2.value;
		mm = document.frm.am2.value;
		yy = document.frm.ay2.value;
	}
	popup('calPop.php?df=' + dateField + '&dd=' + dd + '&mm=' + ++mm + '&yy=' + yy,'cal',0,0,0,240,220);
}
function deleteEntry() {
	if(confirm('Delete this event from the diary?')) {
		document.frm.pageaction.value='delete';
		document.frm.submit();
	}
}
function showUser(userId) {
	opener.top.main.location='/phonebook/userItem.php?userID=' + userId;
	self.close();
}
function showClient(userId) {
	opener.top.main.location='/clients/index.php?userID=' + userId;
	self.close();
}
function showClaim(diaryId) {
	opener.top.main.location='/claims/diarylink.php?id=' + diaryId;
	self.close();
}
function lookAtSecondDropdown() {
	if((document.frm.ad1.value == document.frm.ad2.value) &&
		(document.frm.am1.value == document.frm.am2.value) &&
		(document.frm.ay1.value == document.frm.ay2.value)) {
		var sel1, sel2;
		sel1 = document.frm.startTime.selectedIndex;
		sel2 = document.frm.endTime.selectedIndex;
		if((sel1 > sel2) && (document.frm.endTime.options.length - sel1 > 2)) {
			document.frm.endTime.selectedIndex = sel1 + 2;
		}
	}
}
function popupMulti() {
	popup('mulUsers.php?userids=<?=$userids?>&mcsv=' + document.frm.mulUsersCsv.value + '&task=' + document.frm.eventType.value ,'mulusers','yes',0,0,300,350)
}
function popupAttendees() {
	popup('mulUsers.php?meeting=true&linkedId=' + document.frm.linkedId.value,'mulusers','yes',0,0,300,350)
}
function meetingconfirm(linkedId) {
	self.location = 'addDiary.php?linkedId=' + linkedId + '&meetingaction=confirm';
}
function meetingdecline(linkedId) {
	self.location = 'addDiary.php?linkedId=' + linkedId + '&meetingaction=decline';
}
</script>
</head>
<body style="padding:0px;margin:0px" onLoad="document.frm.diaryEvent.focus()">

<form name="frm" method="post" action="addDiary.php">
<table cellpadding="1" cellspacing="0" width="100%" border="0">
<tr><td class="month" colspan="3" style="background-image:url('/images/myofficeSml.jpg');background-repeat:no-repeat;background-position: right"><?=$diaryViewName?>
<?if($addedBy != "" && $addedBy != "0" && $addedBy != $HTTP_SESSION_VARS["mwoid"] && $HTTP_SESSION_VARS["is-smart"] != "true") echo " - added to system by <a href=\"javascript:showUser(".$addedBy.")\">".$addedByName."</a>"?>
<?if($pageaction == "edit" && $clientId > 0) echo "<br>(for client <a href=\"javascript:showClient(".$clientId.")\">".$clientName."</a>)"; ?>
<?if($pageaction == "add" && $p2n!="" && $p2!=$HTTP_SESSION_VARS["mwoid"]) echo(" - add event for ".$p2n)?>
<?if($pageaction == "edit" && $recurring == -1) echo "<a href=\"outlook.php?id=".$HTTP_GET_VARS["did"]."\"><img style=\"position:absolute;top:10px;right:10px\" alt=\"Add diary event to Outlook\" src=\"/images/outlook.jpg\" border=\"0\"></a>"?>
</td></tr>
<tr><td colspan="3">&nbsp;</td></tr>
<tr><td width="70" nowrap>Start&nbsp;time:</td>
<?if($showAcceptDecline) {?>
<td><strong><?=$myStartTime?></strong></td>
<td width="250" align="right"><select style="visibility:hidden"></select>&nbsp;&nbsp;&nbsp;</td>
<? } else { ?>
<td><select onChange="diaryEventHasBeenChanged=true; lookAtSecondDropdown()" name="startTime"><?displayTimes($myStartTime)?></select></td>
<td width="250" align="right"><a href="#" onClick="popupCal(1);return false;"><img src="/images/cal.gif" border="0" hspace="5"></a>&nbsp;&nbsp;&nbsp;</td>
<? } ?>
</tr>
<? if($eventType == 997 || $eventType == 996) { ?>
	<tr><td colspan="3"><input type="hidden" name="endTime"></td></tr>
<? } else { ?>
	<tr><td width="70" nowrap>End time:</td>
	<?if($showAcceptDecline) {?>
	<td><strong><?=$myEndTime?></strong></td>
	<td width="250" align="right"><select style="visibility:hidden"></select>&nbsp;&nbsp;&nbsp;</td>
	<? } else { ?>
	<td><select onChange="diaryEventHasBeenChanged=true" name="endTime"><?displayTimes($myEndTime)?></select></td>
	<td width="250" align="right"><a href="#" onClick="popupCal(2);return false;"><img src="/images/cal.gif" border="0" hspace="5"></a>&nbsp;&nbsp;&nbsp;</td>
	<? } ?>
	</tr>
<? } ?>

<tr><td colspan="3"><br>
<?if($showAcceptDecline) {?>
<textarea readonly rows="8" cols="43" name="diaryEvent"><?=$myEvent?></textarea>
<? } else { ?>
	<? if($eventType == 997 || $eventType == 996) { ?>
		<textarea onChange="diaryEventHasBeenChanged=true" rows="9" cols="43" name="diaryEvent"><?=$myEvent?></textarea>
	<? } else { ?>
		<textarea onChange="diaryEventHasBeenChanged=true" rows="8" cols="43" name="diaryEvent"><?=$myEvent?></textarea>
	<? } ?>
<? } ?>
</td></tr>

<? if($showAcceptDecline) { ?>
<tr><td colspan="3" nowrap align="center">
	<strong>Please confirm whether you are able to attend the<br>meeting by clicking below.</strong>
	<br>
	<a href="#" style="position:relative;top:3px" onClick="popupAttendees(); return false;" target="_blank">View attendees &gt;&gt;</a>
	<br><br>
	<?=gen_button("btnConfirm", "meetingconfirm(".$linkedId.")", "", "", "Accept")?>
	&nbsp;&nbsp;&nbsp;&nbsp;
	<?=gen_button("btnDecline", "meetingdecline(".$linkedId.")", "", "", "Decline")?>
	&nbsp;&nbsp;&nbsp;&nbsp;
	<?=gen_button("btnClose", "self.close()", "", "", "Close")?>
</td></tr>
<? } elseif($task == "true") { ?>
<tr>
<td colspan="2">
<?
if($pageaction == "edit" && $userId == 0) {
	echo("&nbsp;");
} else {
	echo("<select onChange=\"diaryEventHasBeenChanged=true\" name=\"notTime\">");
	displayNotificationDropdown($notTime);
	echo("</select>");
}
?>
</td>
<td align="right">Type: <b>Task</b>&nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>

<tr>
<td colspan="2" nowrap><a href="#" style="position:relative;top:3px" onClick="popupMulti(); return false;" target="_blank">Please select user</a></td>
<td align="right">Private Task <input type="checkbox" name="private" value="true"></td>
</tr>

<tr><td colspan="3"><br>
	<?=displayAddButton("Add", "submitForm();")?>
	<img src="/images/spacer.gif" width="145" height="1">
	<?=displayCancelButton("Close", "closeForm();")?>
	<input type="hidden" name="eventType" value="997">
	<input type="hidden" name="task" value="true">
</td></tr>
<? } elseif($showAuthNotauth) { ?>
<tr><td colspan="3" nowrap align="center">
	<br>
	<strong>Authorise the holiday request?</strong>
	<br><br>
	<a href="#" onClick="document.frm.auth.value='true';document.frm.submit();return false;" class="liteButton">Authorise</a>
	&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="#" onClick="document.frm.auth.value='false';document.frm.submit();return false;" class="liteButton">Not authorise</a>
</td></tr>
<? } else { ?>
<tr>
<td colspan="2">
<?
if($pageaction == "edit" && $userId == 0) {
	echo("&nbsp;");
} else {
	echo("<select onChange=\"diaryEventHasBeenChanged=true\" name=\"notTime\">");
	displayNotificationDropdown($notTime);
	echo("</select>");
}
?>
</td>
<? if($eventType == 998 && $addedBy != $HTTP_SESSION_VARS["mwoid"]) { ?>
<td align="right">Meeting<input type="hidden" name="eventType" value="998">&nbsp;&nbsp;&nbsp;&nbsp;</td>
<? } elseif($HTTP_SESSION_VARS["is-smart"] == "true") { ?>
<td align="center" style="font-weight:bold">SMART EVENT<input type="hidden" name="eventType" value="995">&nbsp;&nbsp;&nbsp;&nbsp;</td>
<? } elseif($eventType == 997 || $eventType == 996) { ?>
<td align="right">Type: <b>Task</b><input type="hidden" name="eventType" value="<?=$eventType?>">&nbsp;&nbsp;&nbsp;&nbsp;</td>
<? } else { ?>
<td align="right">Type <select onChange="diaryEventHasBeenChanged=true" name="eventType"><?getOptionsForEvents($eventType)?></select>&nbsp;&nbsp;&nbsp;&nbsp;</td>
<? } ?>
</tr>

<tr>
<? if($pageaction == "add" && isDiaryAdminUser() && $HTTP_SESSION_VARS["is-smart"] != "true") { ?>

<? if($userids == "") { ?>
<td colspan="2" nowrap><a href="#" style="position:relative;top:3px" onClick="popupMulti(); return false;" target="_blank">Add event for multiple users?</a></td>
<? } else { ?>
<td colspan="2" nowrap><a href="#" style="position:relative;top:3px" onClick="popupMulti(); return false;" target="_blank">PLEASE SELECT USER</a></td>
<? } ?>

<? } elseif($pageaction == "edit" && $userId != 0  && $eventType == 998 && isDiaryAdminUser()) { ?>
<td colspan="2" nowrap><a href="#" style="position:relative;top:3px" onClick="popupAttendees(); return false;" target="_blank">Attendees...</a></td>
<? } elseif(($eventType == 997 || $eventType == 996) && $pageaction == "edit" && $userId != 0 && isDiaryAdminUser() && $HTTP_SESSION_VARS["is-smart"] != "true") { ?>
<td colspan="2" nowrap><a href="#" style="position:relative;top:3px" onClick="popupMulti(); return false;" target="_blank">Change task user...</a></td>
<? } elseif($pageaction == "edit" && $userId != 0 && isDiaryAdminUser() && $HTTP_SESSION_VARS["is-smart"] != "true") { ?>
<td colspan="2" nowrap><a href="#" style="position:relative;top:3px" onClick="popupMulti(); return false;" target="_blank">Change users...</a></td>
<? } else { ?>
<td colspan="2">&nbsp;</td>
<? } ?>
<? if($eventType == 998) { ?>
<td align="right"><input type="hidden" name="recurring" value="-1"></td>
<? } elseif($eventType == 997 || $eventType == 996) { ?>
<td align="right">Private Task <input type="checkbox" onChange="diaryEventHasBeenChanged=true" name="private" value="true" <? if($isPrivate == "1") { echo "CHECKED"; } ?>><input type="hidden" name="recurring" value="-1"></td>
<? } else { ?>
<td align="right">Recurring? <select onChange="diaryEventHasBeenChanged=true" name="recurring"><?displayRecurringDropdown($recurring)?></select>&nbsp;&nbsp;&nbsp;&nbsp;</td>
<? } ?>
</tr>

<? if($pageaction == "add") { ?>
<tr><td colspan="3"><br>
	<?=displayAddButton("Add", "submitForm();")?>
	<img src="/images/spacer.gif" width="145" height="1">
	<?=displayCloseButton("Close", "closeForm();")?>
</td></tr>
<? } elseif($userId==0 && !isDiaryAdminUser()) { ?>
<tr><td colspan="3" align="right"><br>
	<?=displayCloseButton("Close", "self.close();")?>
</td></tr>
<? } else { ?>
<tr><td colspan="3" nowrap><br>
	<?=displayAmendButton("Amend", "submitForm();")?>
	&nbsp;&nbsp;&nbsp;&nbsp;
	<?=displayDeleteButton("Delete", "deleteEntry();")?>
	&nbsp;&nbsp;&nbsp;&nbsp;
	<?=displayCloseButton("Close", "closeForm();")?>
</td></tr>
<? } ?>
<? } /* end of 'display accept/decline' if */ ?>
</table>
<input type="hidden" name="posted" value="1">
<input type="hidden" name="ad1" value="<?=$myDay1?>">
<input type="hidden" name="am1" value="<?=$myMonth1?>">
<input type="hidden" name="ay1" value="<?=$myYear1?>">
<input type="hidden" name="ad2" value="<?=$myDay2?>">
<input type="hidden" name="am2" value="<?=$myMonth2?>">
<input type="hidden" name="ay2" value="<?=$myYear2?>">
<input type="hidden" name="pageaction" value="<?=$pageaction?>">
<input type="hidden" name="did" value="<?=$HTTP_GET_VARS["did"]?>">
<input type="hidden" name="p2" value="<?=$p2?>">
<input type="hidden" name="client" value="<?=$clientId?>">
<input type="hidden" name="proj" value="<?=$project?>">
<input type="hidden" name="linkedId" value="<?=$linkedId?>">
<input type="hidden" name="mulUsersCsv" value="<?=$mulUsersCsv?>">
<input type="hidden" name="sdb" value="<?=$smartdb?>">
<input type="hidden" name="auth">
<input type="hidden" name="oldEventType" value="<?=$eventType?>">
</form>
<div id="date1" style="position:absolute;left:230px;top:60px;"><?=displayDate($myDate1)?></div>
<? if($eventType != 997 && $eventType != 996) { ?>
	<div id="date2" style="position:absolute;left:230px;top:84;"><?=displayDate($myDate2)?></div>
<? } ?>
</body>
</html>
Return current item: Silicon Fodder