<?
ob_start();
include("../../../framework/framework.php");
include("../functions.php");

// Define page specific text for template
$PAGE[PAGE_PICTITLE]="ico_calendar_med.gif";
$module="calendar";


if (isset ($_GET[day]) && is_numeric($_GET[day]))
{
	$curday=$_GET[day];
}else $curday=(date("d"));


if (isset ($_GET[month]) && is_numeric($_GET[month]))
{
	$curmonth=$_GET[month];
}else $curmonth=date("m");

if (isset ($_GET[year]) && is_numeric($_GET[year]))
{
	$curyear=$_GET[year];
} else $curyear=date("Y");





template_init();






$thisform= load_fwobject("form","calendar",0);

?>
<?

if (isset($_GET[id]))
{
	$rs=$DB->Execute("SELECT * FROM $module WHERE id=".$_GET[id]);
	$resultz=$rs->FetchRow();
	$PAGE_ELEMENT[PAGE][1][0][param]=$resultz[ref_prat];

}

if (isset($_GET[id]) && $_POST[form_id]!= $thisform["name"])
{
	

	$PAGE[PAGE_INTITLE]=CALENDAR_UPD_APP;
	$PAGE[TXT_TITLE]=CALENDAR_UPD_APP;

	if (check_perm_obj($module,$resultz[ref_prat],"w"))
	{ 
		$result=$resultz;

	
		$thisform[onpost]=str_replace("type::add","type::upd",$thisform[onpost]);
		$thisform[Fields][send][content]="submit||".CALENDAR_UPD_APP."||";
		$response[title]=CALENDAR_UPD_DONE;
		$response[text]= CONTACT_UPD_DONE."<br><br>".make_button("app_view.php?form_id=listcont&form_page=1&ref_prat[text]=&ref_prat[realval][]=".$resultz[ref_prat],CALENDAR_BACK_LIST);

	}  else {
		$response[title]=FW_ERROR_NO_PERM;
		$response[text]=FW_ERROR_NO_PERM_TXT;
		$iserror=1;
		print draw_response($response);
	}

	
} elseif (isset($_GET[id]))
{
	$PAGE[PAGE_INTITLE]=CALENDAR_UPD_APP;
	$PAGE[TXT_TITLE]=CALENDAR_UPD_APP;

	if (check_perm_obj($module,$resultz[ref_prat],"w"))
	{ 
		$ad="upd";

		$result=$_POST;
		$response[title]=CALENDAR_UPD_DONE;
		$response[text]= CALENDAR_UPD_DONE."<br><br>".make_button("app_view.php?form_id=listcont&form_page=1&ref_prat[text]=&ref_prat[realval][]=".$resultz[ref_prat],CALENDAR_BACK_LIST);
		$thisform[onpost]=str_replace("type::add","type::upd",$thisform[onpost]);
		$thisform[Fields][send][content]="submit||".CALENDAR_UPD_APP."||";
	} else {
		$response[title]=FW_ERROR_NO_PERM;
		$response[text]=FW_ERROR_NO_PERM_TXT;
		$iserror=1;
		print draw_response($response);
	}


} else {

	$PAGE[PAGE_INTITLE]=CALENDAR_ADD_APP;
	$PAGE[TXT_TITLE]=CALENDAR_ADD_APP;
	if (check_perm_mod($module,"c")==1)
	{
		$ad="add";
		if ($_POST[form_id]== $thisform["name"]) $result=$_POST;
		else {
			$result[permessi]="U".$_SESSION[fw_userid]."=33330";
			if (isset($_GET[ref_id])) {
				$result[ref_prat]=$_GET[ref_id];
				$PAGE_ELEMENT[PAGE][1][0][param]=$_GET[ref_id];
			}
			if (isset($_GET[month]) &&  isset($_GET[year]) &&  isset($_GET[day])  ) {$result[day]=$_GET[year].'-'.$_GET[month].'-'.$_GET[day];}
			if (isset($_GET[hour]) && isset($_GET["min"])) {$result["time"]=$_GET[hour].':'.str_replace("0","00",$_GET[min]);}
		}
		$response[title]=CALENDAR_ADD_DONE;

	} else {
		$response[title]=FW_ERROR_NO_PERM;
		$response[text]=FW_ERROR_NO_PERM_TXT;
		$iserror=1;
		print draw_response($response);
	}

}


if ($iserror!=1){

	if ($_POST[form_id]==$thisform["name"])
	{	if(isset($_POST[form_page])) {$page=$_POST[form_page];}
		else $page=1;
		if (!isset($_POST[type])) 
		{
			$_POST[type]=1;
			$_POST[time]="00:00";
		}
		$error=check_form($thisform,$_POST,$page);
		if($error==1) {
		
			$manage=manage_post($thisform,$error,$_POST,$_GET[id]);
		
		} else print draw_form($thisform,$module,$error,$result);
		if ($manage==1) {
			$page=($_POST[form_page]+1);
			print draw_form($thisform,$module,$error,$result,$page);
		}
		elseif ($manage > 1)
		{
			
			header("location: app_view.php?actdone=$ad&form_id=listcont&form_page=1&ref_prat[text]=&ref_prat[realval][]=".$_POST[ref_prat][realval][0]);
			die();
			if (!isset($_GET[id]))
			{
						$response[text]= CALENDAR_ADD_DONE."<br><br>".make_button("app_view.php?form_id=listcont&form_page=1&ref_prat[text]=&ref_prat[realval][]=".$_POST[ref_prat][realval][0],CALENDAR_BACK_LIST);
			}
			
			print draw_response($response);
		}
		
	} else { 
		
		print draw_form($thisform,$module,"",$result);
	}
}





$PAGE[PAGE_CONTENT] = ob_get_contents();
ob_end_clean();
template_define_elements();

final_render();

?>