<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
<title>Celtic CRM</title>
</head>
<body>
<?
require_once("../../config.php");
$sql=mysql_connect($sugar_config['dbconfig']['db_host_name'],$sugar_config['dbconfig']['db_user_name'],$sugar_config['dbconfig']['db_password']);
mysql_select_db($sugar_config['dbconfig']['db_name']);
session_start();
mysql_query("set names utf8");

if($_POST['update1']==1)
{
	if(count($_POST['check']))
	{
	foreach($_POST['check'] as $check)
	{
		if($check)
		{
			$time_start=date("H:i:s",strtotime($_REQUEST['date'][6].$_REQUEST['date'][7].$_REQUEST['date'][8].$_REQUEST['date'][9]."-".$_REQUEST['date'][3].$_REQUEST['date'][4]."-".$_REQUEST['date'][0].$_REQUEST['date'][1]." ".$_REQUEST['hour'].":".$_REQUEST['minute'].":00")-7200);
			mysql_query("update calls set date_start='".$_REQUEST['date'][6].$_REQUEST['date'][7].$_REQUEST['date'][8].$_REQUEST['date'][9]."-".$_REQUEST['date'][3].$_REQUEST['date'][4]."-".$_REQUEST['date'][0].$_REQUEST['date'][1]."',time_start='".$time_start."' where id='".$check."'");
			mysql_query("update meetings set date_start='".$_REQUEST['date'][6].$_REQUEST['date'][7].$_REQUEST['date'][8].$_REQUEST['date'][9]."-".$_REQUEST['date'][3].$_REQUEST['date'][4]."-".$_REQUEST['date'][0].$_REQUEST['date'][1]."',time_start='".$time_start."' where id='".$check."'");
			mysql_query("update tasks set date_start='".$_REQUEST['date'][6].$_REQUEST['date'][7].$_REQUEST['date'][8].$_REQUEST['date'][9]."-".$_REQUEST['date'][3].$_REQUEST['date'][4]."-".$_REQUEST['date'][0].$_REQUEST['date'][1]."',time_start='".$time_start."' where id='".$check."'");
		}
	}
	}
}
if($_POST['closeselected1']==1)
{
	if(count($_POST['check']))
	{
	foreach($_POST['check'] as $check)
	{
		if($check)
		{
			mysql_query("update calls set status='Held',reminder_time='-1' where id='".$check."'");
			mysql_query("update meetings set status='Held',reminder_time='-1' where id='".$check."'");
			mysql_query("update tasks set status='Completed' where id='".$check."'");
		}
	}
	}
}
if($_POST['discard1']==1)
{
	if(count($_POST['check']))
	{
	foreach($_POST['check'] as $check)
	{
		if($check)
		{
			mysql_query("update calls set reminder_time='-1' where id='".$check."'");
			mysql_query("update meetings set reminder_time='-1' where id='".$check."'");
			//mysql_query("update tasks set reminder_time='-1' where id='".$check."'");
		}
	}
	}
}
if($_POST['updatesec1']==1)
{
	if(count($_POST['check']))
	{
	foreach($_POST['check'] as $check)
	{
		if($check)
		{
			/*
			$r=mysql_fetch_array(mysql_query("select reminder_time,date_start,time_start from calls where id='".$check."'"));
			if($r['date_start'] && $r['time_start'] && $r['reminder_time'])
			{
				$rt=$r['reminder_time'];
				$new_date=gmdate("Y-m-d H:i:s",time()+$_POST['sec']+$rt);
				$ndate=explode(" ",$new_date);
				mysql_query("update calls set date_start='".$ndate[0]."',time_start='".$ndate[1]."' where id='".$check."'");
			}
			$r=mysql_fetch_array(mysql_query("select reminder_time,date_start,time_start from meetings where id='".$check."'"));
			if($r['date_start'] && $r['time_start'] && $r['reminder_time'])
			{
				$rt=$r['reminder_time'];
				$new_date=gmdate("Y-m-d H:i:s",time()+$_POST['sec']+$rt);
				$ndate=explode(" ",$new_date);
				mysql_query("update meetings set date_start='".$ndate[0]."',time_start='".$ndate[1]."' where id='".$check."'");
			}
			$r=mysql_fetch_array(mysql_query("select date_start,time_start from tasks where id='".$check."'"));
			if($r['date_start'] && $r['time_start'])
			{
				$rt=$r['reminder_time'];
				$new_date=gmdate("Y-m-d H:i:s",time()+$_POST['sec']+$rt);
				$ndate=explode(" ",$new_date);
				mysql_query("update tasks set date_start='".$ndate[0]."',time_start='".$ndate[1]."' where id='".$check."'");
			}
			*/
			$r=mysql_fetch_array(mysql_query("select reminder_time,date_start,time_start from calls where id='".$check."'"));
			if($r['date_start'] && $r['time_start'] && $r['reminder_time'])
			{
				$ts=explode(":",$r['time_start']);
				$ds=explode("-",$r['date_start']);
				$new_date=gmdate("Y-m-d H:i:s",mktime($ts[0],$ts[1],$ts[2],$ds[1],$ds[2],$ds[0])+$_POST['rsec']+7200);
				$ndate=explode(" ",$new_date);
				mysql_query("update calls set date_start='".$ndate[0]."',time_start='".$ndate[1]."' where id='".$check."'");
			}
			$r=mysql_fetch_array(mysql_query("select reminder_time,date_start,time_start from meetings where id='".$check."'"));
			if($r['date_start'] && $r['time_start'] && $r['reminder_time'])
			{
				$ts=explode(":",$r['time_start']);
				$ds=explode("-",$r['date_start']);
				$new_date=gmdate("Y-m-d H:i:s",mktime($ts[0],$ts[1],$ts[2],$ds[1],$ds[2],$ds[0])+$_POST['rsec']+7200);
				$ndate=explode(" ",$new_date);
				mysql_query("update meetings set date_start='".$ndate[0]."',time_start='".$ndate[1]."' where id='".$check."'");
			}
			$r=mysql_fetch_array(mysql_query("select date_start,time_start from tasks where id='".$check."'"));
			if($r['date_start'] && $r['time_start'])
			{
				$ts=explode(":",$r['time_start']);
				$ds=explode("-",$r['date_start']);
				$new_date=gmdate("Y-m-d H:i:s",mktime($ts[0],$ts[1],$ts[2],$ds[1],$ds[2],$ds[0])+$_POST['rsec']+7200);
				$ndate=explode(" ",$new_date);
				mysql_query("update tasks set date_start='".$ndate[0]."',time_start='".$ndate[1]."' where id='".$check."'");
			}
		}
	}
	}
}
if($_POST['reminder1']==1)
{
	if(count($_POST['check']))
	{
	foreach($_POST['check'] as $check)
	{
		if($check)
		{
			$r=mysql_fetch_array(mysql_query("select reminder_time from calls where id='".$check."'"));
			$rtime=$r['reminder_time']-$_REQUEST['reminder_minutes']+$_REQUEST['reminder_hours'];
			if($rtime<0)$rtime=60;
			mysql_query("update calls set reminder_time='".$rtime."' where id='".$check."'");
			
			$r=mysql_fetch_array(mysql_query("select reminder_time from meetings where id='".$check."'"));
			$rtime=$r['reminder_time']-$_REQUEST['reminder_minutes']+$_REQUEST['reminder_hours'];
			if($rtime<0)$rtime=60;
			mysql_query("update meetings set reminder_time='".$rtime."' where id='".$check."'");
		}
	}
	}
}
?>
<script type="text/javascript" src="include/javascript/cookie.js?s=4.5.0g&c="></script>
<script>jscal_today = 1.213179783E+012; if(typeof app_strings == "undefined") app_strings = new Array();</script>
<script type="text/javascript" src="../../jscalendar/calendar.js?s=4.5.0g&c="></script>
<script type="text/javascript" src="../../jscalendar/lang/calendar-en.js?s=4.5.0g&c="></script>
<script type="text/javascript" src="../../jscalendar/calendar-setup_3.js?s=4.5.0g&c="></script>
<script src="../../include/javascript/yui/YAHOO.js?s=4.5.0g&c="></script>
<script src="../../include/javascript/yui/log.js?s=4.5.0g&c="></script>
<script src="../../include/javascript/yui/dom.js?s=4.5.0g&c="></script>
<script src="../../include/javascript/yui/event.js?s=4.5.0g&c="></script>
<script src="../../include/javascript/yui/animation.js?s=4.5.0g&c="></script>
<script src="../../include/javascript/yui/connection.js?s=4.5.0g&c="></script>
<script src="../../include/javascript/yui/dragdrop.js?s=4.5.0g&c="></script>
<script src="../../include/javascript/yui/ygDDList.js?s=4.5.0g&c="></script>
<script src="mintajax.js"></script>
<script type="text/javascript" src="../../include/javascript/sugar_3.js?s=4.5.0g&c="></script>
<script type="text/javascript">
	var time_reg_format = '([0-9]{1,2}):([0-9]{1,2})';
	var date_reg_format = '([0-9]{1,2}).([0-9]{1,2}).([0-9]{4})';
	var date_reg_positions = {'d': 1,'m': 2,'Y': 3};
	var time_separator = ':';
	var cal_date_format = '%d.%m.%Y';
	var time_offset = 0;
</script>
<script type="text/javascript" src="../../cache/jsLanguage/en_us.js?s=4.5.0g&c=&j=4"></script>
<script type="text/javascript" src="../../cache/jsLanguage/Contacts/en_us.js?s=4.5.0g&c=&j=4"></script>
<script type="text/javascript" src="../../include/javascript/sugar_3.js?s=4.5.0g&c="></script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<style type="text/css">
@import url("../../themes/Sugar/style.css?s=4.5.0g&c=");
</style>
<link href="../../themes/Sugar/colors.sugar.css?s=4.5.0g&c=" rel="stylesheet" type="text/css" title="sugar" />
<link rel="stylesheet" type="text/css" media="all" href="../../themes/Sugar/calendar-win2k-cold-1.css?s=4.5.0g&c=">


<table width="618" cellpadding="0" cellspacing="0" border="0" class="h3Row">
<tr>
<td nowrap><h3>Remind List</h3></td>
</tr>
</table>


<form action="ReminderWindow.php" method="POST">
<table width="618" border="0" cellspacing="0" cellpadding="0" class="listView">
<tr>
<td scope="col" width="30" class="listViewThS1" NOWRAP><input type="checkbox" name="selectall" id="selectall" value="1" onclick="selectallitems();" /></td>
<td scope="col" width="20" class="listViewThS1" NOWRAP>&nbsp;</td>
<td scope="col" width="212" class="listViewThS1" nowrap="nowrap">Name&nbsp;</td>
<td scope="col" width="79" class="listViewThS1" nowrap="nowrap">Reminder&nbsp;</td>
<td scope="col" width="122" class="listViewThS1" nowrap="nowrap">Time&nbsp;</td>
<td scope="col" width="30" class="listViewThS1" NOWRAP>&nbsp;</td>
<td scope="col" width="30" class="listViewThS1" NOWRAP>&nbsp;</td>
</tr>
</table>
<div style="overflow:auto;height:300px;width:618px;">
<?
function getList($type)
{
	$i=0;
	if($type=="tasks")
	{
		$rem="";
		$status="Completed";
	}
	else
	{
		$rem=" and reminder_time!='-1'";
		$status="Held";
	}
	$w=mysql_query("select * from ".$type." where deleted='0' and assigned_user_id='".$_SESSION['authenticated_user_id']."' and status!='".$status."'".$rem." order by date_start desc, time_start asc");
	while($r=mysql_fetch_array($w))
	{
		$ts=explode(":",$r['time_start']);
		$ds=explode("-",$r['date_start']);
		if(mktime($ts[0],$ts[1],$ts[2],$ds[1],$ds[2],$ds[0])+7200<=mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"))+$r['reminder_time'])
		{
			$name[$i]['date_start']=$r['date_start'];
			$ts=explode(":",$r['time_start']);
			$ds=explode("-",$r['date_start']);
			$name[$i]['time_start']=date("H:i:s",@mktime($ts[0],$ts[1],$ts[2],$ds[1],$ds[2],$ds[0])+7200);
			$name[$i]['name']=$r['name'];
			$name[$i]['id']=$r['id'];
			$name[$i]['type']=$type;
			$name[$i]['reminder_time']=$r['reminder_time'];
			$i++;
		}
	}
	return $name;
}
$meetings=getList("meetings");
$calls=getList("calls");
$tasks=getList("tasks");
if(count($meetings)+count($calls)+count($tasks)==0)print '<script>window.close();</script>';

for($i=0;$i<count($meetings);$i++)$name[]=$meetings[$i];
for($i=0;$i<count($calls);$i++)$name[]=$calls[$i];
for($i=0;$i<count($tasks);$i++)$name[]=$tasks[$i];
//print_r($name);
rsort($name);
$o=0;
foreach($name as $value)
{
	if($value['type']=="meetings")
	{
		$img="Meetings";
		$status="Held";
	}
	if($value['type']=="calls")
	{
		$img="Calls";
		$status="Held";
	}
	if($value['type']=="tasks")
	{
		$img="Tasks";
		$status="Completed";
	}
	if($o==0)
	{
		$check=" checked";
		$sdate_start=$value['date_start'];
		$stime_start=$value['time_start'];
		$st=explode(":",$stime_start);
		$hour=$st[0];
		$minute=$st[1];
		$o++;
	}
	else $check="";
	$ts=explode(":",$value['time_start']);
	
	if($value['reminder_time'] != -1)
	{
		$ts=explode(":",$value['time_start']);
		$ds=explode("-",$value['date_start']);
		$rem=date("H:i:s",mktime($ts[0],$ts[1],$ts[2],$ds[1],$ds[2],$ds[0])-$value['reminder_time']);
	}
	else $rem="";
	print '<table cellspacing="0" cellpadding="0" border="0" width="600">
<tr><td valign="top" width="30" class="oddListRowS1" bgcolor="#ffffff"><input onclick="show(\''.$value['id'].'\',\''.$value['date_start'][8].$value['date_start'][9].'.'.$value['date_start'][5].$value['date_start'][6].'.'.$value['date_start'][0].$value['date_start'][1].$value['date_start'][2].$value['date_start'][3].'\',\''.$ts[0].'\',\''.$ts[1].'\');" type="checkbox" name="check[]" id="check'.$value['id'].'" value="'.$value['id'].'" '.$check.'></td>
<td valign="top" width="30" class="oddListRowS1" bgcolor="#ffffff"><img src="../../themes/Sugar/images/'.$img.'.gif" border="0"></td>
<td valign="top" width="220" class="oddListRowS1" bgcolor="#ffffff"><div style="overflow:hidden;width:220px;"><a href="#" class="listViewTdLinkS1" style="cursor:pointer;" onclick="window.open(\'../../index.php?module='.$img.'&action=DetailView&record='.$value['id'].'&close=1\',\'CRM\',\'height=780,width=1014, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, directories=yes, status=yes\',\'\').focus();">'.$value['name'].'</a></div></td>
<td valign="top" width="120" class="oddListRowS1" bgcolor="#ffffff">'.$rem.'</td>
<td valign="top" width="120" class="oddListRowS1" bgcolor="#ffffff"><div style="overflow:hidden;width:120px;">'.$value['date_start'][8].$value['date_start'][9].'.'.$value['date_start'][5].$value['date_start'][6].'.'.$value['date_start'][0].$value['date_start'][1].$value['date_start'][2].$value['date_start'][3].' '.$value['time_start'].'</div></td>
<td valign="top" width="30" class="oddListRowS1" bgcolor="#ffffff"><a style="cursor:pointer;" onclick="window.open(\'../../index.php?module='.$img.'&action=EditView&record='.$value['id'].'\',\'CRM\',\'\',\'\');"><img src="../../themes/Sugar/images/edit_inline.gif" border="0"></a></td>
<td valign="top" width="30" class="oddListRowS1" bgcolor="#ffffff"><a href="CloseReminder.php?type='.$value['type'].'&record='.$value['id'].'&status='.$status.'"><img src="../../themes/Sugar/images/close_inline.gif" border="0"></a></td>
</tr></table>';
}
?>
</div>
<br />
<div id="close" style="display:block;">
<br />
<!--<input type="hidden" name="discard1" value="0" /><input type="submit" name="discard" class="button" onclick="this.form.discard1.value='1';" value="Discard Selected" />-->
<input type="hidden" name="closeselected1" value="0" /><input type="submit" class="button" name="closeselected" value="Close Selected" onclick="this.form.closeselected1.value='1';" />
</div>
<br />
<ul class="tablist">
<li id="li-postponeby" class="active"><a class="current" id="a-postponeby" href="javascript:showdiv('postponeby');">Postpone By</a></li>
<li id="li-postponeto"><a id="a-postponeto" href="javascript:showdiv('postponeto');">Postpone To</a></li>
<li id="li-reminder"><a id="a-reminder" href="javascript:showdiv('reminder');">Reminder</a></li>
</ul>

<div id="postponeby" style="display:block;">
<br />
<select name='rsec' id='rsec'>
<OPTION value='300'>5 min</OPTION>
<OPTION value='600'>10 min</OPTION>
<OPTION value='900'>15 min</OPTION>
<OPTION value='1800'>30 min</OPTION>
<OPTION value='3600'>1 hour.</OPTION>
<OPTION value='7200'>2 hours</OPTION>
<OPTION value='14400'>4 hours</OPTION>
<OPTION value='28800'>8 hours</OPTION>
<OPTION value='43200'>12 hours</OPTION>
<OPTION value='86400'>1 day</OPTION>
<OPTION value='172800'>2 days</OPTION>
<OPTION value='259200'>3 days</OPTION>
<OPTION value='345600'>4 days</OPTION>
<OPTION value='604800'>1 week</OPTION>
<OPTION value='1209600'>2 weeks</OPTION>
</select>
<input type="hidden" name="updatesec1" value="0" /><input style="display:inline" type="submit" class="button" value="Update" name="updatesec" onclick="this.form.updatesec1.value='1';" />
</div>
<div id="postponeto" style="display:none;">
<br />
Date: <input name="date" id="date" onblur="parseDate(this, '%d.%m.%Y');" size="11" maxlength="10" type="text" value="<?print $sdate_start[8].$sdate_start[9].".".$sdate_start[5].$sdate_start[6].".".$sdate_start[0].$sdate_start[1].$sdate_start[2].$sdate_start[3];?>" /> <img src="../../themes/Sugar/images/jscalendar.gif" alt="%d.%m.%Y"  id="date_trigger" align="absmiddle">
<script type="text/javascript">
Calendar.setup ({
	inputField : "date", ifFormat : "%d.%m.%Y", showsTime : false, button : "date_trigger", singleClick : true, step : 1
});
</script>&nbsp;&nbsp;Time: 
<select name='hour' id='hour'>
<OPTION value='00' <?if($hour=="00")print "selected";?>>00</OPTION>
<OPTION value='01' <?if($hour=="01")print "selected";?>>01</OPTION>
<OPTION value='02' <?if($hour=="02")print "selected";?>>02</OPTION>
<OPTION value='03' <?if($hour=="03")print "selected";?>>03</OPTION>
<OPTION value='04' <?if($hour=="04")print "selected";?>>04</OPTION>
<OPTION value='05' <?if($hour=="05")print "selected";?>>05</OPTION>
<OPTION value='06' <?if($hour=="06")print "selected";?>>06</OPTION>
<OPTION value='07' <?if($hour=="07")print "selected";?>>07</OPTION>
<OPTION value='08' <?if($hour=="08")print "selected";?>>08</OPTION>
<OPTION value='09' <?if($hour=="09")print "selected";?>>09</OPTION>
<OPTION value='10' <?if($hour=="10")print "selected";?>>10</OPTION>
<OPTION value='11' <?if($hour=="11")print "selected";?>>11</OPTION>
<OPTION value='12' <?if($hour=="12")print "selected";?>>12</OPTION>
<OPTION value='13' <?if($hour=="13")print "selected";?>>13</OPTION>
<OPTION value='14' <?if($hour=="14")print "selected";?>>14</OPTION>
<OPTION value='15' <?if($hour=="15")print "selected";?>>15</OPTION>
<OPTION value='16' <?if($hour=="16")print "selected";?>>16</OPTION>
<OPTION value='17' <?if($hour=="17")print "selected";?>>17</OPTION>
<OPTION value='18' <?if($hour=="18")print "selected";?>>18</OPTION>
<OPTION value='19' <?if($hour=="19")print "selected";?>>19</OPTION>
<OPTION value='20' <?if($hour=="20")print "selected";?>>20</OPTION>
<OPTION value='21' <?if($hour=="21")print "selected";?>>21</OPTION>
<OPTION value='22' <?if($hour=="22")print "selected";?>>22</OPTION>
<OPTION value='23' <?if($hour=="23")print "selected";?>>23</OPTION>
</select>:
<select name='minute' id='minute'>
<OPTION value='0' <?if($minute=="0")print "selected";?>>00</OPTION>
<OPTION value='15' <?if($minute=="15")print "selected";?>>15</OPTION>
<OPTION value='30' <?if($minute=="30")print "selected";?>>30</OPTION>
<OPTION value='45' <?if($minute=="45")print "selected";?>>45</OPTION>
</select>&nbsp;&nbsp;<input type="hidden" name="update1" value="0" /><input style="display:inline" type="submit" class="button" value="Update" name="update" onclick="this.form.update1.value='1';" />
</div>
<div id="reminder" style="display:none;">
<br />
<?
include("../../modules/Calls/time_select.php");
print show_time_select("");
?>
<input type="hidden" name="reminder1" value="0" /><input style="display:inline" type="submit" class="button" value="Update" name="reminder" onclick="this.form.reminder1.value='1';" />
</div>
</form>
<script>
function selectallitems()
{
	<?
	foreach($name as $value)
	{
		print 'if(document.getElementById("selectall").checked==true){document.getElementById("check'.$value['id'].'").checked=true}else {document.getElementById("check'.$value['id'].'").checked=false} ;
		';
	}
	?>
}
function show(id,date,hour,minute)
{
	document.getElementById("date").value=date;
	document.getElementById("hour").value=hour;
	document.getElementById("minute").value=minute;
}
function showdiv(id)
{
	document.getElementById('postponeto').style.display="none";
	document.getElementById('postponeby').style.display="none";
	document.getElementById('reminder').style.display="none";
	document.getElementById('li-postponeto').className="";
	document.getElementById('li-postponeby').className="";
	document.getElementById('li-reminder').className="";
	document.getElementById('a-postponeto').className="";
	document.getElementById('a-postponeby').className="";
	document.getElementById('a-reminder').className="";
	document.getElementById(id).style.display="block";
	document.getElementById('li-'+id).className="active";
	document.getElementById('a-'+id).className="current";
}
</script>
<?
mysql_close($sql);
?>
</body>
</html>