<?php
if (isset($_GET['c']) && !empty($_GET['c']) && is_numeric($_GET['c']))
{
if (file_exists("embedded/functions/site_functions.php"))
	{
	include_once("embedded/functions/site_functions.php");
	}
else
	{
include_once("../../embedded/functions/site_functions.php");
	}
db_open();	
$device_id=$_SESSION['device_id'];
$language=$_SESSION['language'];
if ($language==1)
	{
	$head1="το σενάριο;";
	$head4=$head3="πρόκειται";
	$head2="ο";
	$head5="α";
	$head6="τα σενάρια";
	$head7="Ενεργοποιημένα στις ";
	$head9="Σενάρι";
	$head8="να ξεκινήσουν ή έχουν ξεκινήσει! Θέλετε να <span>απενεργοποιήσετε</span>";
	$head11="να ξεκινήσει ή έχει ξεκινήσει! Θέλετε να <span>απενεργοποιήσετε</span>";
	$yes="Ναι";
	$no="Όχι";
	$problem="Σφάλμα βάσης δεδομένων!";
	$min1="λεπτό";
	$min2="λεπτά";
	$head10="Κανένα σενάριο δεν είναι ενεργοποιημένο ή πρόκειται να ενεργοποιηθεί!Το <span>Manual</span> θα ενεργοποιηθεί για <span>";
	$head12="Προγραμματισμός ανανεώθηκε! Το <span>Manual</span> ενεργοποιήθηκε για ";
	$head13="Προγραμματισμός μένει <span>ενεργής</span>. Καμία αλλαγή!";
	}
else
	{
	$head1="this scenario?";
	$head3="is";
	$head4="are";
	$head2="";
	$head5="s";
	$head6="these scenarios?";
	$head7="Activated in ";
	$head9="Scenario";
	$head11=$head8="programmed to be activated soon or running! Do you want to <span>de-activate</span>";
	$yes="Υes";
	$no="No";
	$problem="Database Troubleshoot!";
	$min1="minute";
	$min2="minutes";
	$head10="No scenario is activated soon! <span>Manual</span> activated for the next <span>";
	$head12="Automation updated! <span>Manual</span> activated for the next ";
	$head13="Automation still <span>active</span>. No changes made!";
	}
$c= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['c']))));
if ($c==1)//apla tsekarei an paizei allo senario
	{
		if (isset($_GET['q']) && !empty($_GET['q']) && is_numeric($_GET['q']) && $_GET['d']==0 && $_GET['e']==0)
		{
		$q=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['q']))));
		$aa=$q+1;
		$start_timee=array();
		$end_timee=array();
		$todayy=array();
		$start_timee[0][0]=date("H:i:00",strtotime("+1 minutes"));
		$end_timee[0][0]=date("H:i:00",strtotime("+$aa minutes"));
		$start_time_final=date("Hi00",strtotime("+1 minutes"));
		$end_time_final=date("Hi00",strtotime("+$aa minutes"));
		if($end_time_final[0]==0)
			{
			$end_time_final[0]=9;
			}
		if ($start_time_final[0]==0)
			{
			$start_time_final[0]=9;
			}
		$limitt=strtotime($start_timee[0][0],strtotime("+ $aa minutes"));
		$limit=strtotime("23:59:00");
		$name="";
		for($i=0;$i<$aa;$i++)
			{
			if(strtotime(date("H:i:00",strtotime("+$i minutes")))==$limit)
				{
				break;
				}
			}
		if($i==$aa)
			{
			$counter=0;
			}
		else
			{
			$counter=1;
			$ee=$i+1;
			$start_timee[1][0]=date("H:i:00",strtotime("+ $ee minutes"));
			$end_timee[1][0]=date("H:i:00",strtotime("+ $aa minutes"));
			$todayy[1]=date("Y-m-d",strtotime("+ 1 day"));
			}
		$end_timee[0][0]=date("H:i:00",strtotime("+$i minutes"));
		$todayy[0]=date("Y-m-d");
		
		$rr=1;
		for ($n=0;$n<=$counter;$n++)
		{
			$start_time=$start_timee[$n][0];
			$end_time=$end_timee[$n][0];
			$today=$todayy[$n];
			$sql1 = "SELECT * FROM schedule_scenarios WHERE device_id='$device_id' AND status='1' AND day='$today' AND ((start_time<='$start_time' AND end_time>='$start_time') OR (end_time<='$end_time' AND start_time>='$start_time') OR (end_time>='$end_time' AND start_time>='$start_time' AND start_time<='$end_time')) order by start_time";
			@ $result = mysql_query($sql1);
			$resulta= mysql_numrows($result);
			if ($resulta!=0) 
				{
				while($row=mysql_fetch_array($result))
					{
						$s=$row['scenario_id'];
						$sql11 = "SELECT scenario_name FROM scenarios WHERE scenario_id='$s'";
						@ $resultaa = mysql_query($sql11);
							$rowe= mysql_fetch_array($resultaa);
							if($name=="")
								{
								$pl="$head3";
								$s="$head2";
								$pl1="$head1";
								$exp="$head11";
								}
							else
								{
								$pl="$head4";
								$s="$head5";
								$pl1="$head6";
								$exp="$head8";
								}
						$name=$name.'<tr><td title="'.$head7.$row['day'].'">'.$rr.")".$rowe["scenario_name"].'</td></tr><tr><td><span class="d_important">(<i>'.$row['start_time'].' - '.$row['end_time'].'</i>)</span></td></tr>';
						$rr++;
					}
				}
		}
		if ($name!="")
			{
			$name='<table id="msgga">'.$name.'</table>';
			$p1=$head9.$s.": <span>$name</span>,  $pl $exp  $pl1";
			$msg='<div class="p1">'.$p1.'</div><div class="n_buttons"><input type="button" value="'.$yes.'" onclick="scenario_check(2,'.$start_time_final.','.$end_time_final.','.$q.');" /><input type="button" value="'.$no.'" onclick="scenario_check(3,1,1,1);" /></div>';
			$class="decision";
			}
		else
			{
			for($n=0;$n<=$counter;$n++)
				{
				$start_time=$start_timee[$n][0];
				$end_time=$end_timee[$n][0];
				$today=$todayy[$n];
				$a=schedule_scenario($scenario_name="manual",$device_id,$today,$start_time,$end_time,$option="1");
				if ($a==5)
					{
					if ($q==1)
						{
						$minute="$min1";
						}
					else
						{
						$minute="$min2";
						}
					$msg='<div id="x">'.$head10.$q.'</span> '.$minute.'.</div>';
					$class="okkk";
					}
				else
					{
					$msg='<div id="x">'.$problem.'</div>';
					$class="decision";
					}
				}
			}
		}
		
	}
else
	{
	if (is_numeric($_GET['q']) && is_numeric($_GET['d']) && is_numeric($_GET['e']))
		{
		if ($c==2 )//edw ginetai to activate apenergopoiontas ta alla..
			{
			$q= $_GET['q'];
			$d= $_GET['d'];
			$e= $_GET['e'];
			if ($q[0]==9)
				{
				$q[0]=0;
				}
			if ($d[0]==9)
				{
				$d[0]=0;
				}
			$start_timee=array();
			$end_timee=array();
			$todayy=array();
			$todayy[0]=date("Y-m-d");
			$start_timee[0]=$q[0].$q[1].":".$q[2].$q[3].":"."00";
			$end_timee[0]=$d[0].$d[1].":".$d[2].$d[3].":"."00";
			$start_check=strtotime($start_timee[0]);
			$end_check=strtotime($end_timee[0]);
			$dif="";
			if($end_check<$start_check)
				{
				$end_timee[1]=$end_timee[0];
				$start_timee[1]=date("00:00:00");
				$end_timee[0]="23:59:00";
				$todayy[1]=date("Y-m-d",strtotime("+1 day"));
				}
			
			$msg="";
			$rt=count($start_timee)-1;//metavliti gia to poses tha kanei fores update
			for($i=0;$i<=$rt;$i++)
			{
			$today=$todayy[$i];
			$start_time=$start_timee[$i];
			$end_time=$end_timee[$i];
			$sql1 = "UPDATE schedule_scenarios SET status='0' WHERE device_id='$device_id' AND status='1' AND day='$today' AND ((start_time<='$start_time' AND end_time>='$start_time') OR (end_time<='$end_time' AND start_time>='$start_time'))";
			@ $result = mysql_query($sql1);
			if ($result =="1")
				{
				$a=schedule_scenario($scenario_name="manual",$device_id,$today,$start_time,$end_time,$option="1");
				if ($a!=5)
					{
					$msg=$msg."problem";
					}
				}
			else
				{
				$msg=$msg."problem";
				}
			}
			
			if ($msg=="")
					{
					if($e==1)
						{
						$min="$min1";
						}
					else
						{
						$min="$min2";
						}
					$msg='<div id="x">'.$head12.'<span>'.$e.'</span> '.$min.'.</div>';
					$class="okkk";
					}
				else
					{
					$msg='<div id="x">'.$problem.'</span></div>';
					$class="decision";
					}
			
			}
		else 
			{
			$msg='<div id="x">'.$head13.'</div>';
			$class="decision";
			}
		}
	}
db_close();	
echo<<<d
<div id ="scenario_notification" class="$class" >
$msg
</div>
d;
}
?>
		