<?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')

	{

		

	}

	

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

	{

		$clicked_date = $_POST['DateCheckDB'];

		$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;

				//echo $clicked_date; exit;

			}

			//echo $pos; 

		}

		//echo $clicked_date;

		$IsUserExistQuery = "Select * from `day_managment` Where `userid`=$User_ID AND `date`='$clicked_date'";

//		echo $IsUserExistQuery; exit;

		$IsUserExistExecute = mysql_query($IsUserExistQuery);

		$IsUserExistNumRows = mysql_num_rows($IsUserExistExecute);

		

		if($IsUserExistNumRows > 0)

		{

			$Rows = mysql_fetch_array($IsUserExistExecute);

			echo $Rows['id']; exit;

		}

		

	}

	

	

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

	{

		$clicked_date = $_POST['DateGet'];

		$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;

				//echo $clicked_date; exit;

			}

			//echo $pos; 

		}

		echo $clicked_date; exit;

	}

	

	

	$where = "";

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

	{

		$id = $_POST['UpdateRecord'];

		$where .= ' Where `day_managment`.`id` ='.$id;

	}else {

		//$where = '';

		$where .= ' Where `day_managment`.`userid` ='.$User_ID;

    }

	$json_obj = array();

	$temp = array();

	$sql = "Select `day_managment`.*,users.username 

			from `day_managment`

			Inner Join users ON day_managment.userid = users.id

			$where";

			//echo $sql; exit;

	$results = mysql_query($sql);

	while($rows = mysql_fetch_array($results))

	{

		$AllDayYN = $rows['allday'];

		$OnLeaveYN = $rows['onleave'];
		
		$hours = $rows['hours'];
		$allDayDisplay = '';
		$HoursDisplay = '';
                $OnLeaveDisplay = '';
		if($hours != 0)
		{
			$HoursDisplay = 'Hours: '.$rows['hours'];
		}
		if($AllDayYN == 1)

		{
			$AllDayYN = 'Yes';
			$allDayDisplay = ' AllDay: '.$AllDayYN;

		}else{

		$AllDayYN = 'No';}

		if($OnLeaveYN == 1){

		$OnLeaveYN = "Yes";
                $OnLeaveDisplay = ' OnLeave: '.$OnLeaveYN;
                }else{

		$OnLeaveYN = "No";}

		

		$id = $rows['id'];

		$start = $rows['date'];

		$end   = $rows['date'];

		$dateform=date('d-m-Y', strtotime($rows['date']));

		$json_obj[] = array(

			'id'   => $id,

			//'title'   => $rows['username']." event ".$id,

			'title'   => $HoursDisplay.$allDayDisplay.$OnLeaveDisplay,

			'date'  =>$dateform,

			'hours' => $rows['hours'],

			'allday' => $rows['allday'],

			'onleave' => $rows['onleave'],

			'start' => strtotime($start),

			'end' => strtotime($end)

		);

	}

		echo json_encode($json_obj);

	

	?>