<?php 

	include("includes/config.php");

	include("includes/func.php");

	include("classes/safeSQL.class.php");

	

	if (($_SESSION['id'] == ""))

	{

		redirect("login");

	}

	

	include("templates/header.php");

	

	$safeSQL 			= new safeSQL;

	$User_ID			= $_SESSION['id'];

	if($_SESSION['usertype'] != 'Admin')

	{

		

    ?>

    <div id="mainPanel">

    <?php

		$_SESSION['msg'];

		if($_SESSION['msg'] != "")

		{

			echo "<span style='color:green'>".$_SESSION['msg']."</span>";

			unset($_SESSION['msg']);

			//echo '<div id="msg_show" style="display:none">'.$msg.'</div>';

		}

    ?>

    <div style="margin-top:15px;">

	 <div id='calendar'></div>

     </div>

     <?php

	 

	 $globel_check = "";

	 // Update Code

	 $update_id = "";

	 $updated_return_row = array();

	 if(isset($_POST['Cal_Event']))

	 {

		// $_SESSION['U_ID'] = $_POST['Cal_Event'];

		 $update_id = $_POST['Cal_Event'];

		 $sql = "Select * from day_managment WHERE id=$update_id";

		// echo $sql;exit;

		 $results = mysql_query($sql);

		 $updated_return_row = mysql_fetch_array($results);

	 }

	// echo $updated_return_row["date"]; exit;

	

	 $clicked_date = "";

	 if(isset($_POST['c_date']))

	{

		$clicked_date = $_POST['c_date'];

		//echo $clicked_date; exit; 

	}//Wed Jan 02 2013 00:00:00 GMT+0500

	//$clicked_date = "Wed Jan 02 2013 00:00:00 GMT+0500 (West Asia Standard Time)";

	$month_array = array(

			"Jan" => '01', "Feb" => '02', "Mar" => '03', "Apr" => '04', "May" => '05', "Jun" => '06', "Jul" => '07', "Aug" => '08', 

			"Sep" => '09', "Oct" => '10', "Nov" => '11', "Dec" => '12'

	);

//	echo $clicked_date; 

	foreach($month_array as $month => $values)

	{

		//echo $clicked_date;

		$pos = strpos($clicked_date, $month);

		if($pos != false)

		{

			$month_number = $values;

			$day_number = substr($clicked_date, $pos+4, 2);

			$year_number = substr($clicked_date, $pos+7, 4);

			$clicked_date = $year_number."-".$month_number."-".$day_number;

			$_SESSION['C_DATE'] = $clicked_date;

			//echo $clicked_date; exit;

		}

		//echo $pos; 

	}

	//echo $clicked_date; 

     ?>

     <div id="test" style="display:none;

     						position:fixed;

                            left:40%;

                            top:40%;

                            background:#0C89C9;

                            padding:0 20px 8px;

                            padding-top:0px;

                            width:222px;

                            border:3px solid #ccc;

                            box-shadow: 4px 4px 3px rgba(103, 115, 130, 1);

                            z-index:999">

     <?php

	 $msg_type = "";

	 if(isset($_POST['deletebtn']))

	 {

		 $delete_id = $_POST['update_record_id'];

		 $DeleteMonthlyHoursQuery = "DELETE FROM `day_managment` WHERE `id`= $delete_id";

		 $msg_type = "Deleted!";

		 if(mysql_query($DeleteMonthlyHoursQuery))

		{

			$_SESSION['msg'] = "Successfully ".$msg_type;

			echo '<script type="text/javascript">window.parent.location.href="day_manage.php";</script>';

		}

	

	 }

	 

	 if(isset($_POST['submit']))

	{

		if($_POST['allday'] != 1)

		{

			$_POST['allday'] = 0;

		}

		if($_POST['onleave'] != 1)

		{

			$_POST['onleave'] = 0;

		}

	//	$record = array(

		//	'userid'  => $_SESSION['id'],

			//'date'    => $_POST['cdate'],

	//		'hours'   => $_POST['hours'],

	//		'allday'  => $_POST['allday'],

	//		'onleave' => $_POST['onleave']

	///	);

	$hour = $_POST['hours'];

	$allday = $_POST['allday'];

	$onleave = $_POST['onleave'];

	$clicked_date = $_SESSION['C_DATE'];

	$update_row = $_POST['update_record_id'];

		 

	//echo $update_row; exit;

	$sql;

	if($update_row == "")

	{

		if($onleave == 1)

		{

			$hour = 0; $allday = 0;

		}

	$sql = "Insert into day_managment(`date`, `hours`, `allday`, `onleave`, `userid`)

				Values('$clicked_date',$hour,$allday,$onleave, $User_ID)";

				$msg_type = "Added!";

				//echo $sql;exit;

	}else{

		if($onleave == 1)

		{

			$hour = 0; $allday = 0;

		}

		$sql = "Update day_managment Set `hours` = $hour, `allday` = $allday, `onleave` = $onleave, `userid` = $User_ID

				Where id = $update_row";

				$msg_type = "Updated!";

				//echo $sql;exit;

	}

			//	echo $sql;

	if(mysql_query($sql))

	{

		$_SESSION['msg'] = "Successfully ".$msg_type;

		echo '<script type="text/javascript">window.parent.location.href="day_manage.php";</script>';

	}

	}

	

	 $popUpForm = "";

	 $hour_array = array(0, 1, 2, 3, 4, 5, 6);

	 $div_display_none = "document.getElementById('test').style.display = 'none'";

	 $popUpForm .= '<div style="width:100%">

	 				<div style="float:left; width:90%;"><h2 id="header_title" style="font-weight: lighter; margin-top: 4px;color:#FFFFFF;

					background:none !important;

	border:none !important;

	height:28px !important;

	font-size:25px !important;

	text-shadow: none !important;

	color:#FFFFFF !important;

	line-height:normal !important;

	font-family: Arial, Helvetica, sans-serif !important;

	text-indent:15px !important;

	padding:0 !important;

	letter-spacing:1px !important;

	margin: 10px 0px !important;"></h2></div>

	 				<div style="float:left; width:10%;"><a style="left:right;margin-bottom: 15px;" 

					onclick="'.$div_display_none.'" href="Javascript:void(0);"><img src="images/close.png" style="margin-left:13px; margin-top:4px;"/></a></div>

					</div>';

     $popUpForm .= '<div style="clear:both;">';

	 //	$popUpForm .= '<a style="left:right;margin-bottom: 15px;" onclick="window.close();" href=""><img align="right" src="images/close.png" style="margin-right: -20px; margin-top: -20px"/></a>';

		$popUpForm .= '<form action="" name="popup" id="popup" method="POST" style="left:5px; position:relative; marrin-left:-7px;">';

		$popUpForm .= '<table id="tabling1" cellpadding="0" cellspacing="0">';

		$popUpForm .= '<tr><td>Data</td>';

		$popUpForm .= '<td><label name="date" id="date"></label></td></tr>';

		$popUpForm .= '<tr><td>Horas Extra</td>';

		$popUpForm .= '<td><select name="hours" id="hours">';
	//	print_r($hour_array);

		foreach($hour_array as $hour)

		$popUpForm .= '<option value="'.$hour.'" >'.$hour.'</option>';

		$popUpForm .= '</select></td></tr>';

		$popUpForm .= '<tr><td>Dia inteiro</td>';

		$popUpForm .= '<td><input type="checkbox" name="allday" id="allday" value="1" /></td></tr>';

		$popUpForm .= '<tr><td>Falta</td>';

		$popUpForm .= '<td><input type="checkbox" name="onleave" id="onleave" value="1" /></td></tr>';

		$popUpForm .= '<input type="hidden" name="update_record_id" id="update_record_id" value=""/>';

		$popUpForm .= '<tr><td><input type="submit" name="deletebtn" id="deletebtn" class="form_button" value="Apagar" style="display:none;"/></td>';

		//$popUpForm .= '<td></td>';

		$popUpForm .= '<td><input type="submit" name="submit" id="submit" class="form_button" value="Guardar"/></td>';

		$popUpForm .= '</tr>';

		$popUpForm .= '</table>';

		$popUpForm .= '</form>';

		echo $popUpForm;

	}

        ?>

     </div>

     </body>

</html>