<?php
// this file needs to be utf8 without BOM. Check accentuated chars : é à è ù
header("content-type: application/xml");
$folder="";
$not_access=0;
$pagename="modules/p_calendar.php";

//includes
require_once('includes.php');

echo '<'.'?xml version="1.0" encoding="UTF-8"?'.'>';

$calid=isset($_POST["calid"])?$_POST["calid"]:0;
$calid = is_numeric($calid)?$calid:0;
$userid=isset($_SESSION['user_id'])?$_SESSION['user_id']:0;
$shared=isset($_POST['shared'])?$_POST['shared']:0;
$sharedmd5key=isset($_POST['sharedmd5key'])?$_POST['sharedmd5key']:'';
$shared = is_numeric($shared)?$shared:0;
$widgetid = isset($_POST['widgetid'])?$_POST['widgetid']:0;
$icalurl = isset($_POST["icalurl"])?str_replace("ical://","http://",$_POST["icalurl"]):"";
$shareditemkey = isset($_POST['shareditemkey'])?$_POST['shareditemkey']:"";
$shareditemkey = (empty($shareditemkey) && isset($_POST['sharedkey']))?$_POST['sharedkey']:$shareditemkey;
$acceptwriteshare = (empty($acceptwriteshare) && isset($_POST['acceptwriteshare']))?$_POST['acceptwriteshare']:'no';

$ownerallowwriting = (empty($ownerallowwriting) && isset($_POST['ownerallowwriting']))?$_POST['ownerallowwriting']:'no';
?>
<Module>
<UserPref name="calid" display_name="." datatype="hidden" default_value="0" />
<UserPref name="sharedkey" display_name="keysharing" datatype="hidden" default_value="" />
<UserPref name="sharedmd5key" display_name="." datatype="hidden" default_value="" />
<UserPref name="shareditemkey" display_name="." datatype="hidden" default_value="" />
<UserPref name="shared" display_name="." datatype="hidden" default_value="" />
<?php 
	if (isset($_SESSION['user_id']) && $_SESSION['type'] == 'A')
	{
?>
<UserPref name="ownerallowwriting" datatype="enum" default_value="no" display_name="ownerallowwriting" >
    <EnumValue value="yes" display_value="yes" />
    <EnumValue value="no" display_value="no" />
</UserPref>  
<UserPref name="acceptwriteshare" datatype="enum" default_value="no" display_name="acceptwriteshare" >
    <EnumValue value="yes" display_value="yes" />
    <EnumValue value="no" display_value="no" />
</UserPref> 
<?php
	} else {
?>
<UserPref name="ownerallowwriting" datatype="hidden" default_value="no" display_name="ownerallowwriting" />
<UserPref name="acceptwriteshare" datatype="hidden" default_value="no" display_name="acceptwriteshare" />
<?php
	}
?>
<Content><![CDATA[
<head>
<script>
var id__MODULE_ID__ = <?php echo $calid;?>;
var date__MODULE_ID__ = new Date();
var mod__MODULE_ID__ = 0;
var events__MODULE_ID__ = [];
var ical__MODULE_ID__ = new Array();
var icalData__MODULE_ID__ = new Array();
var icalDayData__MODULE_ID__ = new Array();
var shared__MODULE_ID__ = <?php echo $shared;?>;
var shareditemkey__MODULE_ID__ ='<?php echo $shareditemkey;?>';
var ownerallowwriting__MODULE_ID__ ='<?php echo $ownerallowwriting;?>';
var acceptwriteshare__MODULE_ID__ = '<?php echo $acceptwriteshare;?>';
add__MODULE_ID__=function(v_form){
	var stime=v_form.hour__MODULE_ID__.value+":"+v_form.minute__MODULE_ID__.value;
	var etime=v_form.hourf__MODULE_ID__.value+":"+v_form.minutef__MODULE_ID__.value;
	if (etime<stime){alert(lg("endTimeMustBeHigher"));return;}
	var ndate=$p.date.getDbFormat(v_form.day__MODULE_ID__.value,v_form.mon__MODULE_ID__.value,v_form.yea__MODULE_ID__.value);
	var oId=v_form.tit__MODULE_ID__.value;
	var oCom=v_form.com__MODULE_ID__.value;
			
	$p.ajax.call("../modules/wid_calendar.php",
				{
		            'type':'load',
                    'method':'post',
					'variables':
						"modid="+id__MODULE_ID__
						+ "&t="+oId
						+ "&act=add"
						+ "&c="+oCom
						+ "&d="+ndate
						+ "&h="+stime
						+ "&end="+etime
						+ '&ownerallowwriting='+ownerallowwriting__MODULE_ID__
						+ '&acceptwriteshare='+acceptwriteshare__MODULE_ID__
                        + '&shareditemkey='+shareditemkey__MODULE_ID__
					,
                    'callback':
                    {
                        'function':get__MODULE_ID__,
                        'variables':
                        {
                            'calid':id__MODULE_ID__
                        }
                    }
                }
		); 
}
get__MODULE_ID__=function(response,vars) {

	if(response) {
		var v_id = $p.ajax.getVal(response,"calid","str",false,0);

	    id__MODULE_ID__ = v_id;
	    tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("calid",v_id);   
	    var sharedkey = $p.ajax.getVal(response,"sharedkey","str",false,'');  
	    var ownerallowwriting = $p.string.getVar(tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].vars,"ownerallowwriting");
		var acceptwriteshare = $p.string.getVar(tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].vars,"acceptwriteshare");
	    if (ownerallowwriting == 'yes') {
	        tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("sharedkey",sharedkey);
	    }
		if (acceptwriteshare == 'yes') {
			tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("acceptwriteshare","yes");
		} else {
			tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("acceptwriteshare","no");
		}
		tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].refresh();
	} else {
		if("<?php echo $icalurl; ?>" != "" ) {
			getXml("../portal/xml_icalendar.php",
				getIcalEvent__MODULE_ID__,"","xml",
				"url="+"<?php echo $icalurl; ?>"+"&sharedmd5key=<?php echo $sharedmd5key; ?>&widgetid=<?php echo $widgetid; ?>",
				"POST");
		}
		if (v_id != indef){
			var l_id = v_id.split("_");
			if (l_id[0] != id__MODULE_ID__){
				id__MODULE_ID__ = l_id[0];
				tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("calid",id__MODULE_ID__);
			}
		}
		if (mod__MODULE_ID__==0){
	        $p.print("ev__MODULE_ID__",lg("lblLoading"));
			getXml("../modules/wid_calendar.php",
				displayMonth__MODULE_ID__,
				"",
				"xml",
				"modid="+id__MODULE_ID__
					+"&act=month&m="+(date__MODULE_ID__.getMonth()+1)
					+"&y="+date__MODULE_ID__.getFullYear()
					+"&sharedmd5key=<?php echo $sharedmd5key; ?>"
					+"&widgetid=<?php echo $widgetid; ?>"
					+"&calid=<?php echo $calid; ?>"
					+"&icalurl=<?php echo $icalurl; ?>"
					+"&shareditemkey=<?php echo $shareditemkey; ?>"
					+"&shared=<?php echo $shared; ?>"
					+"&ownerallowwriting=<?php echo $ownerallowwriting; ?>"
					+"&acceptwriteshare=<?php echo $acceptwriteshare; ?>"
					,"POST");
					var shared__MODULE_ID__ = <?php echo $shared;?>;
		} else {
			var gdate=$p.date.getDbFormat(date__MODULE_ID__.getDate(),(date__MODULE_ID__.getMonth()+1),date__MODULE_ID__.getFullYear());
			getXml("../modules/wid_calendar.php",display__MODULE_ID__,"","xml","modid="+id__MODULE_ID__+"&act=get&d="+gdate+"&sharedmd5key=<?php echo $sharedmd5key; ?>&widgetid=<?php echo $widgetid; ?>&shareditemkey=<?php echo $shareditemkey; ?>","POST");
		}
	
	}
}
getIcalEvent__MODULE_ID__=function(response,vars)
{
	var k,m = 0;
	ical__MODULE_ID__[0] = new Array();
	var ical_month = 0
	var ical_monthEnd = 0
	var icalData = "";
	var dateIcal = "";
	var dateIcalEnd = "";

	if (response.getElementsByTagName("ical")[0] && icalData__MODULE_ID__.length == 0){
		var l_event = response.getElementsByTagName("ical")[0];
		var data = $p.ajax.getVal(l_event,"cal","str",false,"--");
		if(data.search(/\n\r.+/) != -1) {
			icalData = $p.ajax.getVal(l_event,"cal","str",false,"--").split("\n\r");
		} else {
			icalData = $p.ajax.getVal(l_event,"cal","str",false,"--").split("\n");
		}
			for(k=0;k<icalData.length;k++) {
				icalV = icalData[k].split(":");
				if(icalV[0].indexOf("DTSTART",0)>=0) {
					ical__MODULE_ID__[m]["DTSTART"] = (icalV[1]==undefined)?"":icalV[1].substring(0,8);
					dateIcal = ical__MODULE_ID__[m]["DTSTART"];
					ical_month = parseInt(dateIcal.substring(4,6),10);
				} else {
					if(icalV[0].indexOf("DTEND",0)>=0) {
						ical__MODULE_ID__[m]["DTEND"] = (icalV[1]==undefined)?"":icalV[1].substring(0,8);
					} else {
						if(icalV[0].indexOf("SUMMARY",0)>=0) {
							ical__MODULE_ID__[m]["SUMMARY"] = icalV[1];
						} else {
							if(icalV[0].indexOf("UID",0)>=0) {
								ical__MODULE_ID__[m]["UID"] = icalV[1];
							} else {
								if(icalV[0].indexOf("DTSTAMP",0)>=0) {
									ical__MODULE_ID__[m]["DTSTAMP"] = icalV[1];
								} else {
									if(icalV[0].indexOf("RRULE",0)>=0) {
										ical__MODULE_ID__[m]["DTSTAMP"] = icalV[1];
									} else {
										if(icalV[0].indexOf("DESCRIPTION",0)>=0) {
											ical__MODULE_ID__[m]["DESCRIPTION"] = icalV[1];
										}
									}
								}
							}
						}
					}
				}
				/*
				switch(icalV[0]) {
					case "DTSTART;VALUE=DATE": 
								ical__MODULE_ID__[m]["DTSTART"] = (icalV[1]==undefined)?"":icalV[1];
								dateIcal = ical__MODULE_ID__[m]["DTSTART"];
								ical_month = parseInt(dateIcal.substring(4,6),10);
								break;
					case "DTEND;VALUE=DATE": ical__MODULE_ID__[m]["DTEND"] = (icalV[1]==undefined)?"":icalV[1]; break;
					case "SUMMARY": ical__MODULE_ID__[m]["SUMMARY"] = icalV[1]; break;
					case "UID": ical__MODULE_ID__[m]["UID"] = icalV[1]; break;
					case "DTSTAMP": ical__MODULE_ID__[m]["DTSTAMP"] = icalV[1]; break;
					case "RRULE": ical__MODULE_ID__[m]["DTSTAMP"] = icalV[1]; break;
					case "DESCRIPTION": ical__MODULE_ID__[m]["DESCRIPTION"] = icalV[1]; break;
					default : break;
				}
				*/
				if(icalV[0] == "END") {
						dateIcalEnd =  -1;
						if( ical__MODULE_ID__[m]["DTEND"] != undefined ) {
							dateIcalEnd = ical__MODULE_ID__[m]["DTEND"];
							ical_monthEnd = parseInt(dateIcalEnd.substring(4,6),10);
							dateIcalEnd = parseInt(dateIcalEnd.substring(0,8),10);
							if(!icalData__MODULE_ID__[ical_monthEnd]) {
								icalData__MODULE_ID__[ical_monthEnd] = new Array();
							}
							icalData__MODULE_ID__[ical_monthEnd].push(
																new Array(
																	-1,
																	ical__MODULE_ID__[m]["SUMMARY"],
																	ical__MODULE_ID__[m]["UID"],
																	ical__MODULE_ID__[m]["DTSTAMP"],
																	ical__MODULE_ID__[m]["RRULE"],
																	ical__MODULE_ID__[m]["DESCRIPTION"],
																	ical__MODULE_ID__[m]["DTEND"]
																	)
																);
						}
						
					if(!icalData__MODULE_ID__[ical_month]) {
						m=0;
						icalData__MODULE_ID__[ical_month] = new Array();
						var temp = ical__MODULE_ID__[m]["DTSTART"];
						temp = (temp!=null)?temp.substring(0,8):-1;
						if( temp>0 ) {
							icalData__MODULE_ID__[ical_month].push(
																		new Array(
																			ical__MODULE_ID__[m]["DTSTART"],
																			ical__MODULE_ID__[m]["SUMMARY"],
																			ical__MODULE_ID__[m]["UID"],
																			ical__MODULE_ID__[m]["DTSTAMP"],
																			ical__MODULE_ID__[m]["RRULE"],
																			ical__MODULE_ID__[m]["DESCRIPTION"],
																			ical__MODULE_ID__[m]["DTEND"]
																		)
																);
						}
					} else {
						if(ical__MODULE_ID__[m]["DTSTART"]>0) {
							icalData__MODULE_ID__[ical_month].push(
																		new Array(
																			ical__MODULE_ID__[m]["DTSTART"],
																			ical__MODULE_ID__[m]["SUMMARY"],
																			ical__MODULE_ID__[m]["UID"],
																			ical__MODULE_ID__[m]["DTSTAMP"],
																			ical__MODULE_ID__[m]["RRULE"],
																			ical__MODULE_ID__[m]["DESCRIPTION"],
																			ical__MODULE_ID__[m]["DTEND"]
																		)
																	);
						}
					}
					m++;
					ical__MODULE_ID__[m] = new Array();
				}
			}
		
	}
}
getIcalEventByDay__MODULE_ID__=function()
{
	var l_month = date__MODULE_ID__.getMonth()+1;
		l_imonth = l_month;
		l_month = ( l_month<10?"0"+l_month:l_month);
	var ical_day = date__MODULE_ID__.getDate();
		ical_day = ( ical_day<10?"0"+ical_day:ical_day);
	var ical_yea = date__MODULE_ID__.getFullYear();
	var icalDateEv = ical_yea+""+l_month+""+ical_day;
	var m = 0;
	if("<?php echo $icalurl; ?>" != "" ) {
		for(k=0;k<icalData__MODULE_ID__[l_imonth].length;k++) {
			ical_month = "";
			ical_day = "";
			ical_yea = "";
			ical_title = "";
			dateIcal = icalData__MODULE_ID__[l_imonth][k][0];
			dateIcal = (dateIcal!=undefined && dateIcal!=-1)?dateIcal.substring(0,8):"";
			dateIcalEnd = icalData__MODULE_ID__[l_imonth][k][6];
			dateIcalEnd = (dateIcalEnd!=undefined)?dateIcalEnd.substring(0,8):"";

			if( (dateIcal == icalDateEv) ||  (dateIcalEnd == icalDateEv) ) {
				icalDayData__MODULE_ID__[m] = [];
				icalDayData__MODULE_ID__[m][0] = icalData__MODULE_ID__[l_imonth][k][1];
				icalDayData__MODULE_ID__[m][1] = (icalData__MODULE_ID__[l_imonth][k][5]!=null)?icalData__MODULE_ID__[l_imonth][k][5]:"--";
				icalDayData__MODULE_ID__[m][2] = "";
				if(icalData__MODULE_ID__[l_imonth][k][0] == -1 ) {
					dateIcalEnd = icalData__MODULE_ID__[l_imonth][k][6];
					ical_HEnd =  parseInt(dateIcalEnd.substring(9,11),10);
					ical_MEnd = parseInt(dateIcalEnd.substring(11,13),10);
					icalDayData__MODULE_ID__[m][2] = "&laquo;"+ical_HEnd+":"+ical_MEnd ;
				} else if(dateIcal == dateIcalEnd) {
					dateIcalEnd = icalData__MODULE_ID__[l_imonth][k][6];
					ical_HEnd =  parseInt(dateIcalEnd.substring(9,11),10);
					ical_MEnd = parseInt(dateIcalEnd.substring(11,13),10);
					dateIcal = icalData__MODULE_ID__[l_imonth][k][0];
					ical_H =  parseInt(dateIcal.substring(9,11),10);
					ical_M = parseInt(dateIcal.substring(11,13),10);
					if(isNaN(ical_HEnd)) {
						ical_HEnd = "--";
						ical_MEnd = "--";
					}
					if(isNaN(ical_H)) {
						ical_H = "--";
						ical_M = "--";
					}
					icalDayData__MODULE_ID__[m][2] = ical_H+":"+ical_M+"&raquo;"+ical_HEnd+":"+ical_MEnd ;
				} else {
					dateIcal = icalData__MODULE_ID__[l_imonth][k][0];
					ical_H =  parseInt(dateIcal.substring(9,11),10);
					ical_M = parseInt(dateIcal.substring(11,13),10);
					if(isNaN(ical_H)) {
						ical_H = "--";
						ical_M = "--";
					}
					icalDayData__MODULE_ID__[m][2] = ical_H+":"+ical_M+"&raquo;";
				}
				m++;
				
			}

		}
	}
}
loadMonth__MODULE_ID__=function (year,month)
{
	year = document.f__MODULE_ID__.yea__MODULE_ID__.value;
	month = document.f__MODULE_ID__.mon__MODULE_ID__.value;
	target = new Date(year, month, 0);
	nbDay = target.getDate();
	days = document.f__MODULE_ID__.day__MODULE_ID__;
	for (i=1;i<=31;i++)
	{
		j = document.createElement("OPTION");
		j.text = i;
		j.value = i;
		if (nbDay>=i)
			days.options[i-1] = j;
		else
			days.length = nbDay;
	}
}
display__MODULE_ID__=function(response,vars)
{
	noopt__MODULE_ID__();
	l_s = "<a href='#' onclick='displayMonthEvents__MODULE_ID__();return false;'>&laquo; "+lg("returnWholeMonth")+"</a><br /><br />";

	if (response.getElementsByTagName("event")[0] || icalDayData__MODULE_ID__.length>0 ){
		var i=0;
		l_s += "<table cellpadding='1' cellspacing='0' width='100%'>";
		while (response.getElementsByTagName("event")[i])
        {
			var l_event = response.getElementsByTagName("event")[i];
			l_s += "<tr>"
                + "<td width='72'>"
                + $p.ajax.getVal(l_event,"time","str",false,"--")
                + " - "
                + $p.ajax.getVal(l_event,"endtime","str",false,"--")
                + "</td>"
                + "<td onmouseover=\"mouseBox('"+($p.ajax.getVal(l_event,"comment","str",false,"--").replace(/'/g," "))+"',event)\" onmouseout=\"mouseBox('')\">"
                + $p.ajax.getVal(l_event,"title","str",false,"--")
                + "</td>"
                + "<td width='15'>"
                + "<a href='#' onclick='sup__MODULE_ID__("+$p.ajax.getVal(l_event,"id","id",false,0)+")'><img src='../images/ico_close.gif' /></a>"
                + "</td>"
                + "</tr>";
			i++;
		}
		// icalendar comments
		for( k=0;k<icalDayData__MODULE_ID__.length;k++ ) {
				l_s += "<tr>"
                + "<td width='72'>"
                + icalDayData__MODULE_ID__[k][2]
                + "</td>"
                + "<td onmouseover=\"mouseBox('"+(icalDayData__MODULE_ID__[k][1].replace(/'/g," "))+"',event)\" onmouseout=\"mouseBox('')\">"
                + icalDayData__MODULE_ID__[k][0];
                + "</td>"
                + "<td width='15'>"
                + ""
                + "</td>"
                + "</tr>";
		}
		icalDayData__MODULE_ID__ = [];
		
		l_s += "</table>";
	} else {l_s += lg("lblNoEvent");}
    
	$p.print("ev_list__MODULE_ID__",l_s);
}
displayMonth__MODULE_ID__=function(response,vars)
{
	noopt__MODULE_ID__();
    
    events__MODULE_ID__.length = 0;
    
	var l_s = '<table cellpadding="0" cellspacing="0" style="border: 1px solid #0E679A;">'
        + '<tr style="background: #E9EDF2;">'
        + '<td style="text-align: center;height: 20px;">'
        + '<a href="#" onclick="return prev__MODULE_ID__()"><img src="../images/ico_previous.gif" /></a>'
        + '</td>'
        + "<td colspan='5' id='currdate__MODULE_ID__' style='text-align:center'>"
        + showdate__MODULE_ID__()
        + '</td>'
        + '<td style="text-align:center">'
        + '<a href="#" onclick="return next__MODULE_ID__();"><img src="../images/ico_next.gif" /></a>'
        + '</td>'
        + '</tr>'
    	+ '<tr style="background: #E9EDF2;">'
        + "<td style='width: 20px;height: 20px;text-align: center;'>" + lg("monday2")+ "</td>"
        + "<td style='width: 20px;height: 20px;text-align: center;'>"+lg("tuesday2")+"</td>"
        + "<td style='width: 20px;height: 20px;text-align: center;'>"+lg("wednesday2")+"</td>"
        + "<td style='width: 20px;height: 20px;text-align: center;'>"+lg("thirsday2")+"</td>"
        + "<td style='width: 20px;height: 20px;text-align: center;'>"+lg("friday2")+"</td>"
        + "<td style='width: 20px;height: 20px;text-align: center;'>"+lg("saturday2")+"</td>"
        + "<td style='width: 20px;height: 20px;text-align: center;'>"+lg("sunday2")+"</td>"
        + "</tr>";

    var current_month = new Date().getMonth()+1;
	var l_date = new Date(date__MODULE_ID__.getFullYear(),date__MODULE_ID__.getMonth(),1),
        l_today = new Date,
        inc = 0,
        l_day,
        l_month = l_date.getMonth()+1,
        l_yea = l_date.getFullYear(),
        xmlinc = 0,
        l_comment;

	var l_start = (l_date.getDay() == 0 ? 7 : l_date.getDay());
	for (var i = 1;i < l_start;i++)
    {
		if (i == 1)
            l_s += "<tr>";
		l_s += "<td style='width: 20px;height: 20px;text-align: center;'></td>";
		inc++;
	}
	while ((l_date.getMonth()) == date__MODULE_ID__.getMonth())
    {
		if (inc % 7 == 0) l_s += "<tr>";
		l_day = l_date.getDate();
		l_comment = "";
		if (response.getElementsByTagName("event")[xmlinc])
        {
			while(response.getElementsByTagName("event")[xmlinc] && $p.ajax.getVal(response.getElementsByTagName("event")[xmlinc],"day","int",false,0)==l_day)
            {
				var l_event = response.getElementsByTagName("event")[xmlinc];
                var l_title = $p.ajax.getVal(l_event,"time","str",false,"--")+"&raquo;"+$p.ajax.getVal(l_event,"endtime","str",false,"--")+" "+($p.ajax.getVal(l_event,"title","str",false,"--").replace(/'/g," "));
				l_comment += '' + l_title;
				
                // Only 24 events
                if ( events__MODULE_ID__.length < 24 && (l_month>current_month || ( l_month==current_month &&  l_day >= l_today.getDate() ) ) )
                    events__MODULE_ID__.push(l_day+'/'+l_month+'/'+l_yea+' '+l_title);
                
				xmlinc++;
			}
		}
	
		// icalendar
		if( (l_month>current_month || ( l_month==current_month &&  l_day >= l_today.getDate() ) ) && icalData__MODULE_ID__[l_month] ) {
			var dateIcal = "";
			var dateIcalEnd = "";

			for(k=0;k<icalData__MODULE_ID__[l_month].length;k++) {

				ical_month = "";
				ical_day = "";
				ical_yea = "";
				ical_H = "";
				ical_M = "";
				ical_S = "";
				
				ical_monthEnd = "";
				ical_dayEnd = "";
				ical_yeaEnd = "";
				ical_HEnd = "";
				ical_MEnd = "";
				ical_SEnd = "";

				dateIcal = "";
				dateIcalEnd = "";
				
				ical_title = "";

				var dateStart = icalData__MODULE_ID__[l_month][k][0];
				if(icalData__MODULE_ID__[l_month][k][0] != -1) {
					dateStart = (dateStart!=undefined)?dateStart.substring(0,8):"";
				}
				if(dateStart>0) {
					dateIcal = icalData__MODULE_ID__[l_month][k][0];
					
					ical_month = parseInt(dateIcal.substring(4,6),10);
					ical_yea = parseInt(dateIcal.substring(0,4),10);
					ical_day = parseInt(dateIcal.substring(6,8),10);
					
					ical_H =  parseInt(dateIcal.substring(9,11),10);
					ical_M = parseInt(dateIcal.substring(11,13),10);
					ical_S = parseInt(dateIcal.substring(13,15),10);
					
					dateIcalEnd = icalData__MODULE_ID__[l_month][k][6];
					
					if(dateIcalEnd != null) {
						ical_monthEnd = parseInt(dateIcalEnd.substring(4,6),10);
						ical_yeaEnd = parseInt(dateIcalEnd.substring(0,4),10);
						ical_dayEnd = parseInt(dateIcalEnd.substring(6,8),10);
						
						ical_HEnd =  parseInt(dateIcalEnd.substring(9,11),10);
						ical_MEnd = parseInt(dateIcalEnd.substring(11,13),10);
						ical_SEnd = parseInt(dateIcalEnd.substring(13,15),10);
					}
					if( l_day==ical_day && ical_month == l_month && ical_yea == l_yea && dateIcal!="" ) {
						if(icalData__MODULE_ID__[l_month][k][1] != undefined && icalData__MODULE_ID__[l_month][k][1] != "") {
							var mkStart = new Date(ical_yea,ical_month,ical_day);
							mkStart = mkStart.getTime();
							
							var mkEnd = 0;
							if(ical_yeaEnd>0) {
								mkEnd = new Date(ical_yeaEnd,ical_monthEnd,ical_dayEnd);
								mkEnd = mkEnd.getTime();
							}
							
							var HStart = ((ical_H>0)?(ical_H+":"+ical_M):"--");
							var HEnd = "";
							if( ical_H>0 && (mkStart==mkEnd) ) {
								HEnd = "&raquo;"+ical_HEnd+":"+ical_MEnd;
							}else if( ical_H>0 && (mkStart<mkEnd) ) {
								HEnd = "&raquo;";
							}
							ical_title = icalData__MODULE_ID__[l_month][k][1];
							ical_title  = HStart+"&raquo;"+HEnd+" "+ical_title.replace(/'/g," ");	
							
							l_comment += ical_title;
						} else {
							l_comment += "--" + '' + l_title;
						}
						l_comment = l_comment.replace(/'/g," ");
						events__MODULE_ID__.push(ical_day+'/'+ical_month+'/'+ical_yea+" : "+ical_title);
					}
					
				}

				if( dateStart == -1 ) {
					dateIcal = icalData__MODULE_ID__[l_month][k][0];
					
					ical_month = "";
					ical_yea = "";
					ical_day = "";

					ical_H =  "";
					ical_M = "";
					ical_S = "";
					
					dateIcalEnd = icalData__MODULE_ID__[l_month][k][6];
					ical_monthEnd = parseInt(dateIcalEnd.substring(4,6),10);
					ical_yeaEnd = parseInt(dateIcalEnd.substring(0,4),10);
					ical_dayEnd = parseInt(dateIcalEnd.substring(6,8),10);
						
					ical_HEnd =  parseInt(dateIcalEnd.substring(9,11),10);
					ical_MEnd = parseInt(dateIcalEnd.substring(11,13),10);
					ical_SEnd = parseInt(dateIcalEnd.substring(13,15),10);
					if( l_day==ical_dayEnd && ical_monthEnd == l_month && ical_yeaEnd == l_yea && dateIcalEnd!="" ) {
		                					
						
						if(icalData__MODULE_ID__[l_month][k][1] != undefined && icalData__MODULE_ID__[l_month][k][1] != "") {

							var mkEnd = 0;
							if(ical_yeaEnd>0) {
								mkEnd = new Date(ical_yeaEnd,ical_monthEnd,ical_dayEnd);
								mkEnd = mkEnd.getTime();
							}
							
							var HEnd = "";
							if( ical_HEnd>0) {
								HEnd = "&laquo;"+ical_HEnd+":"+ical_MEnd;
							} else {
								HEnd = "&laquo;"+"--";
							}
							ical_title = icalData__MODULE_ID__[l_month][k][1];
							ical_title  = HEnd+" "+" "+ical_title.replace(/'/g," ");	
							l_comment += ical_title;
						} else {
							l_comment += "--" + '' + l_title;
						}
						events__MODULE_ID__.push(ical_dayEnd+'/'+ical_monthEnd+'/'+ical_yeaEnd+" : "+ical_title);	
						l_comment = l_comment.replace(/'/g," ");
					}
				}

			}
			
		}
		
		if (l_comment == "")
        {
			l_s+="<td style='width: 20px;height: 20px;text-align: center;' bgcolor='#ffffff' onmouseover=\"mouseBox('')\">"+l_day+"</td>";
		} else {
			l_s+="<td style='width: 20px;height: 20px;text-align: center;' bgcolor='#efefef' style='cursor:pointer;cursor:hand;font-weight:bold;' onclick='selDay__MODULE_ID__("+l_day+")' onmouseover=\"mouseBox('"+l_comment+"',event)\" onmouseout=\"mouseBox('')\">"+l_day+"</td>";
		}

		l_date.setDate(l_date.getDate()+1);
		inc++;
		if (inc % 7 == 0) l_s += "</tr>";
	}
	$p.print("ev__MODULE_ID__",l_s);
    
    displayMonthEvents__MODULE_ID__();
}
selDay__MODULE_ID__=function(day)
{
	date__MODULE_ID__ = new Date(date__MODULE_ID__.getFullYear(),date__MODULE_ID__.getMonth(),day);
	// ical event
	getIcalEventByDay__MODULE_ID__();	
	mod__MODULE_ID__ = 1;
	get__MODULE_ID__();

}
selMonth__MODULE_ID__=function(){
	mod__MODULE_ID__ = 0;
	get__MODULE_ID__();
}
sup__MODULE_ID__=function(id,newL){
	response = confirm(lg("msgAreYouSureSupElement"));
	if (response == 1)
    {
        //executescr("../modules/wid_calendar.php","modid="+id__MODULE_ID__+"&act=sup&calid="+id,false,true,get__MODULE_ID__);
         $p.ajax.call("../modules/wid_calendar.php",
                {
                    'type':'load',
                    'method':'post',
                    'variables':
                        "modid="+id__MODULE_ID__
                        + "&calid="+id
                        + "&act=sup"
                        + '&acceptwriteshare='+acceptwriteshare__MODULE_ID__
                        + '&shareditemkey='+shareditemkey__MODULE_ID__
                    ,
                    'callback':
                    {
                        'function':get__MODULE_ID__,
                        'variables':
                        {
                            'calid':id__MODULE_ID__
                        }
                    }
                }
            );   
    }
}
opt__MODULE_ID__=function(obj)
{
	var l_s = "";
    var hour = ["","","","","","","","","","","","","","","","","","","","","","","",""];
	hour[new Date().getHours()+1] = "selected";
	if (<?php echo $userid;?>==0){alert(lg("msgNeedToBeConnected"));}
	else {
		l_s+="<form name='f__MODULE_ID__' action='' onsubmit='add__MODULE_ID__(this);return false;'><table cellpadding='1' cellspacing='0'><tr><td>"+lg("lblTitle")+"</td><td><input type='text' name='tit__MODULE_ID__' maxlength='64' size='32' /></td></tr><tr><td>"+lg("Comment")+"</td><td><input type='text' name='com__MODULE_ID__' maxlength='250' size='35' /></td></tr>";
		l_s+="<tr><td>"+lg("lblDate")+"</td>";
		l_s+="<td><select name='day__MODULE_ID__'><option value='1'>01</option><option value='2'>02</option><option value='3'>03</option><option value='4'>04</option><option value='5'>05</option><option value='6'>06</option><option value='7'>07</option><option value='8'>08</option><option value='9'>09</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option><option value='13'>13</option><option value='14'>14</option><option value='15'>15</option><option value='16'>16</option><option value='17'>17</option><option value='18'>18</option><option value='19'>19</option><option value='20'>20</option><option value='21'>21</option><option value='22'>22</option><option value='23'>23</option><option value='24'>24</option><option value='25'>25</option><option value='26'>26</option><option value='27'>27</option><option value='28'>28</option><option value='29'>29</option><option value='30'>30</option><option value='31'>31</option></select>";
		l_s+="<select onchange='loadMonth__MODULE_ID__();' name='mon__MODULE_ID__'><option value='1'>"+lg("month1")+"</option><option value='2'>"+lg("month2")+"</option><option value='3'>"+lg("month3")+"</option><option value='4'>"+lg("month4")+"</option><option value='5'>"+lg("month5")+"</option><option value='6'>"+lg("month6")+"</option><option value='7'>"+lg("month7")+"</option><option value='8'>"+lg("month8")+"</option><option value='9'>"+lg("month9")+"</option><option value='10'>"+lg("month10")+"</option><option value='11'>"+lg("month11")+"</option><option value='12'>"+lg("month12")+"</option></select>";
		l_s+="<select name='yea__MODULE_ID__'><option value='2006'>2006</option><option value='2007'>2007</option><option value='2008'>2008</option><option value='2009'>2009</option><option value='2010'>2010</option><option value='2011'>2011</option><option value='2012'>2012</option></select></td></tr>";
		//l_s+="<tr><td>"+lg("time")+"</td><td><input type='text' name='time__MODULE_ID__' size='5' maxlength='5' value='hh:mm' /></td></tr><tr><td></td><td><input class='btn' type='submit' value='"+lg("lblAdd")+"' /> <a href='#' onclick='noopt__MODULE_ID__();return false;'><img src='../images/ico_close.gif' /></a></td></tr></table></form>";
		l_s+="<tr><td>"+lg("lblFrom")+"</td><td><select name='hour__MODULE_ID__'><option value='00' "+hour[0]+">00 h</option><option selected value='01' "+hour[1]+">01 h</option><option value='02' "+hour[2]+">02 h</option><option value='03' "+hour[3]+">03 h</option><option value='04' "+hour[4]+">04 h</option><option value='05' "+hour[5]+">05 h</option><option value='06' "+hour[6]+">06 h</option><option value='07' "+hour[7]+">07 h</option><option value='08' "+hour[8]+">08 h</option><option value='09' "+hour[9]+">09 h</option><option value='10' "+hour[10]+">10 h</option><option value='11' "+hour[11]+">11h</option><option value='12' "+hour[12]+">12 h</option><option value='13' "+hour[13]+">13 h</option><option value='14' "+hour[14]+">14h</option><option value='15' "+hour[15]+">15 h</option><option value='16' "+hour[16]+">16 h</option><option value='17' "+hour[17]+">17 h</option><option value='18' "+hour[18]+">18 h</option><option value='19' "+hour[19]+">19 h</option><option value='20' "+hour[20]+">20 h</option><option value='21' "+hour[21]+">21 h</option><option value='22' "+hour[22]+">22 h</option><option value='23' "+hour[23]+">23 h</option></select> ";
		l_s+="<select name='minute__MODULE_ID__'><option value='00'>00 mn</option><option value='05'>05 mn</option><option value='10'>10 mn</option><option value='15'>15 mn</option><option value='20'>20 mn</option><option value='25'>25 mn</option><option value='30'>30 mn</option><option value='35'>35 mn</option><option value='40'>40 mn</option><option value='45'>45 mn</option><option value='50'>50 mn</option><option value='55'>55 mn</option></select></td></tr>";
		l_s+="<tr><td>"+lg("until")+"</td><td><select name='hourf__MODULE_ID__'><option value='00'>00 h</option><option value='01'>01 h</option><option value='02'>02 h</option><option value='03'>03 h</option><option value='04'>04 h</option><option value='05'>05 h</option><option value='06'>06 h</option><option value='07'>07 h</option><option value='08'>08 h</option><option value='09'>09 h</option><option value='10'>10 h</option><option value='11'>11h</option><option value='12'>12 h</option><option value='13'>13 h</option><option value='14'>14h</option><option value='15'>15 h</option><option value='16'>16 h</option><option value='17'>17 h</option><option value='18'>18 h</option><option value='19'>19 h</option><option value='20'>20 h</option><option value='21'>21 h</option><option value='22'>22 h</option><option value='23'>23 h</option></select> ";
		l_s+="<select name='minutef__MODULE_ID__'><option value='00'>00 mn</option><option value='05'>05 mn</option><option value='10'>10 mn</option><option value='15'>15 mn</option><option value='20'>20 mn</option><option value='25'>25 mn</option><option value='30'>30 mn</option><option value='35'>35 mn</option><option value='40'>40 mn</option><option value='45'>45 mn</option><option value='50'>50 mn</option><option value='55'>55 mn</option></select></td></tr>";
		l_s+="<tr><td></td><td><input class='btn' type='submit' value='"+lg("lblAdd")+"' /> <a href='#' onclick='noopt__MODULE_ID__();return false;'><img src='../images/ico_close.gif' /></a></td></tr></table></form>";
		obj.parentNode.innerHTML=l_s;
		
		l_form=document.forms["f__MODULE_ID__"];
		l_form.yea__MODULE_ID__.value=date__MODULE_ID__.getFullYear();
		l_form.mon__MODULE_ID__.value=(date__MODULE_ID__.getMonth()+1);
		l_form.day__MODULE_ID__.value=date__MODULE_ID__.getDate();
	}
}
noopt__MODULE_ID__=function(){
	if (shared__MODULE_ID__==1) return false;
	var obj = $p.get("addb__MODULE_ID__");
	obj.innerHTML = "<span id='acceptwriteshare__MODULE_ID__' style='color: #C0C0C0;float: right;'></span> <a href='#' onclick='opt__MODULE_ID__(this);return false;'><img src='../images/ico_add.gif' /> "+lg("lblAddEvent")+"</a>";
	// write if widget is shared or not
	acceptShareState__MODULE_ID__();
}
// display sharing question
initiateSharingMessage__MODULE_ID__ = function() {
	var l_s = lg('doYouWantToShareWidget')
		+ ' ?<br />'
		+ '<center>'
		+ '<input type="button" onclick="setWidgetAsShared__MODULE_ID__(true);" value="' + lg('yes') + '" /> '
		+ '<input type="button" onclick="setWidgetAsShared__MODULE_ID__(false);" value="' + lg('no') + '" /> '
		+ '</center>';

	$p.print("ev__MODULE_ID__",l_s);
}
// Define the widget as shared
setWidgetAsShared__MODULE_ID__ = function(v_shared) {
	var l_shared = (v_shared ? 'yes' : 'no');
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("acceptwriteshare",l_shared);
	acceptwriteshare__MODULE_ID__ = l_shared;
	createWidgetItemInDb__MODULE_ID__();
}
// initialize the widget in the DB - create the task item
createWidgetItemInDb__MODULE_ID__ = function(){
	$p.ajax.call("../modules/wid_calendar.php",
		{
			'type':'load',
			'method':'post',
			'variables':'act=init',
			'callback':
			{
				'function' : createWidgetItemInDbCallback__MODULE_ID__
			}
		}
	); 
}
createWidgetItemInDbCallback__MODULE_ID__ = function(response,vars) {	
	var l_id = $p.ajax.getVal(response,"calid","str",false,0);
	var l_sharedkey = $p.ajax.getVal(response,"sharedkey","str",false,0);
	
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("calid",l_id);
	id__MODULE_ID__ = l_id;
	
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("shareditemkey",l_sharedkey);
	shareditemkey__MODULE_ID__ = l_sharedkey;
	
	// refresh the widget content
	init__MODULE_ID__();
}


init__MODULE_ID__=function(){
	if (id__MODULE_ID__ == 0 && $p.app.user.id == -1)
	{
		initiateSharingMessage__MODULE_ID__();
	}
    else
	{
		get__MODULE_ID__();
	}
}
next__MODULE_ID__=function(){
	if (mod__MODULE_ID__==0){date__MODULE_ID__.setMonth(date__MODULE_ID__.getMonth()+1);}
	else {date__MODULE_ID__=new Date(date__MODULE_ID__.getTime()+86400000);}
	get__MODULE_ID__();
	return false;
}
prev__MODULE_ID__=function(){
	if (mod__MODULE_ID__==0){date__MODULE_ID__.setMonth(date__MODULE_ID__.getMonth()-1);}
	else {date__MODULE_ID__=new Date(date__MODULE_ID__.getTime()-86400000);}
	get__MODULE_ID__();
	return false;
}
showdate__MODULE_ID__=function(){
	var mon = date__MODULE_ID__.getMonth(); 
	var dat = date__MODULE_ID__.getDate(); 
	var yea  = date__MODULE_ID__.getFullYear();
	return "<b>"
        + (mod__MODULE_ID__ == 0 ? "" : dat+" ")
        + lg("month"+(mon+1))
        + " "
        + yea
        + "</b>";
}
displayMonthEvents__MODULE_ID__=function()
{
    mod__MODULE_ID__ = 0;

    var l_s = '<b>'+lg('nextEventsForTheMonth')+' :</b><br /><br />';
    
    if (events__MODULE_ID__.length == 0 && ical__MODULE_ID__.length == 0)
    {
        l_s += lg('lblNoEvent');
    }
    else
    {
        l_s += '<ul>';
        for (var i = 0;i < events__MODULE_ID__.length;i++)
        {
            l_s += '<li>'
                + events__MODULE_ID__[i]
                + (__useNotebook ? ' <a href="#" onclick="shareEvent__MODULE_ID__('+i+');return false;">'+$p.img('ico_share_s.gif',13,10)+'</a>' : '')
                + '</li>';
            
        }
        l_s += '</ul>';
    }
    
    $p.print("ev_list__MODULE_ID__",l_s);
}
shareEvent__MODULE_ID__ = function(v_id)
{
    $p.friends.menu(6,0,{'title':events__MODULE_ID__[v_id],'description':''});
}
function acceptShareState__MODULE_ID__ () {

        var shareState = {'yes':lg('widgetWorldUpdating'),'no':'','Oui':lg('widgetWorldUpdating'),'Non':''};
        var l_s = shareState[acceptwriteshare__MODULE_ID__];
        var obj = $p.get("acceptwriteshare__MODULE_ID__");
        obj.innerHTML = l_s;
}
noopt__MODULE_ID__();
init__MODULE_ID__();

</script>
</head>
<body>
<div id="ev__MODULE_ID__" style="padding: 6px;float: left;"></div>
<div id="ev_list__MODULE_ID__" style="padding: 6px;float: left;"></div>
<hr class="float_correction" />
<div id="addb__MODULE_ID__" style="padding: 3px 0 5px 4px;"></div>
</body>
]]></Content>
</Module>