<?php
if ((isset($_GET['m']) && !empty($_GET['m'])) && (isset($_GET['y']) && !empty($_GET['y'])) && (isset($_GET['d']) && !empty($_GET['d'])) && (is_numeric($_GET['m']) && is_numeric($_GET['y']) && is_numeric($_GET['d'])))
{
if (file_exists("embedded/functions/site_functions.php"))
	{
	include_once("embedded/functions/site_functions.php");
	}
else
	{
include_once("../../embedded/functions/site_functions.php");
	}
date_default_timezone_set("Europe/Athens");
$msg="";
$c="";
$device_id=$_SESSION['device_id'];
$language=$_SESSION['language'];
if ($language==1)
	{
	$head1="Ελέγξτε ξανά <b>την ώρα έναρξης και ολοκλήρωσης!</b>";
	$head2="Περασμένη σημερινή ώρα. Παρακαλώ προσπαθήστε ξανά";
	$head3="Επιτυχής!";
	$head4="Υπάρχει ήδη προγραμματισμένο σενάριο. Παρακαλώ προσπαθήστε ξανά";
	$head5="Υπάρχει ήδη προγραμματισμός για ";
	$head6="Παρακαλώ προσπαθήστε ξανά";
	$head7="Πρόβλημα με την <b>συχνότητα</b>. Παρακαλώ προσπαθήστε ξανά.";
	$head8="Πρόβλημα με την <b>ώρα έναρξης</b>. Παρακαλώ προσπαθήστε ξανά.";
	$head9="Πρόβλημα με την <b>ώρα ολοκλήρωσης</b>. Παρακαλώ προσπαθήστε ξανά.";
	$head10="Πρόβλημα με το <b>όνομα σεναρίου</b>. Παρακαλώ προσπαθήστε ξανά.";
	}
else
	{
	$head1="Check again <b>start and end time</b> filling!";
	$head2="Time passed for today. Please re-schedule!";
	$head3="Done!";
	$head4="Another scenario is programmed this time.Please re-schedule!";
	$head5="Another scenario is programmed in "; 
	$head6="Please re-schedule!";
	$head7="Problem with <b>frequency</b>. Please check again!";
	$head8="Problem with <b>start time</b>. Please check again!";
	$head9="Problem with <b>end time</b>. Please check again!";
	$head10="Problem with <b>scenario name</b>. Please check again!";
	}
db_open();
$m= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["m"]))));
$y= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["y"]))));
$d= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["d"]))));
date_default_timezone_set("Europe/Athens");
	if (isset($_GET["s"]) && !empty($_GET["s"]))
	{
	$scenario= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["s"]))));
	if (!preg_match("/[^_A-Za-z0-9\' ']/", $scenario))
		{
		if ((isset($_GET["s_h"]) && !empty($_GET["s_h"]) && is_numeric($_GET["s_h"]) && $_GET["s_h"]<=23 ) && (isset($_GET["s_m"]) && !empty($_GET["s_m"]) && is_numeric($_GET["s_m"]) && ($_GET["s_m"]<=55) || $_GET["s_m"]==90) )
			{
			$s_h= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["s_h"]))));
			$s_m= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["s_m"]))));
			if($s_m==90)
				{
				$s_m="00";
				}
			$start_time=$s_h.":".$s_m.":00";
			$check_time=strtotime($start_time);
			$now=time();
			if ((isset($_GET["e_h"]) && !empty($_GET["e_h"]) && is_numeric($_GET["e_h"]) && $_GET["e_h"]<=23) && (isset($_GET["e_m"]) && !empty($_GET["e_m"]) && is_numeric($_GET["e_m"]) && ($_GET["e_m"]<=55) || $_GET["e_m"]==90) )
				{
				$e_h= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["e_h"]))));
				$e_m= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["e_m"]))));
				if($e_m==90)
					{
					$e_m="00";
					}
				if (($e_h<$s_h) || ($e_h==$s_h && $e_m<=$s_m))
					{
					$msg="$head1";
					$c="invalid";
					}
				else
					{
					$end_time=$e_h.":".$e_m.":00";
					if ((isset($_GET["f"]) && !empty($_GET["f"]) && is_numeric($_GET["f"]) && $_GET["f"]<=6) && (isset($_GET["f_s"]) && !empty($_GET["f_s"]) && is_numeric($_GET["f_s"]) && $_GET["f_s"]<=4))
						{
						$frequency= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["f"]))));
						$frequency_stop= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET["f_s"]))));
						$date=$y."-".$m."-".$d;
						$check_date=strtotime($date);
						$today=strtotime(date("Y-m-d"));
						if($check_time<=$now && $check_date<=$today)
							{
							$msg="$head2";
							$c="invalid";
							}
						else
							{
							if ($frequency==6)
								{
								
								$sql1 = "SELECT * FROM schedule_scenarios WHERE device_id='1' AND status='1' AND day='$date' 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')) ";
								$res=mysql_query($sql1);
								$resulta = mysql_fetch_array($res);
								if ($resulta=="")
									{
									
									$a=schedule_scenario($scenario,$device_id,$date,$start_time,$end_time,$option=1);
									if ($a==5)
										{
										$msg="$head3";
										$c="valid";
										}
									}
								else
									{
									$msg="$head4";
									$c="invalid";
									}
								
								}
							else
								{
								if ($frequency_stop ==1)
									{
									$e="+ 1 week";
									}
								else if ($frequency_stop==2)
									{
									$e="+ 15 days";
									}
								else if ($frequency_stop==3)
									{
									$e="+ 1 month";
									}
								else
									{
									$e="+ 1 year";
									}
								if ($frequency ==1)
									{
									$a="+ 1 day";
									}
								else if ($frequency==2)
									{
									$a="+ 2 days";
									}
								else if ($frequency==3)
									{
									$a="+ 3 days";
									}
								else if ($frequency==4)
									{
									$a="+ 1 week";
									}
								else
									{
									$a="+ 15 days";
									}	
								$da = mktime(0,0,0,$m,$d,$y);
								$end_date1 =date("Y-m-d",strtotime($e,$da));
								$end_date=strtotime($end_date1);
								$c_date=strtotime($date);
								$i=0;
								$time_array=array();
								while ($c_date<=$end_date)
									{
									$time_array[$i]=$date;
									$next_date=date("Y-m-d",strtotime($a,$da));
									$b=explode("-",$next_date);
									$y=$b[0];
									$m=$b[1];
									$d=$b[2];
									$da = mktime(0,0,0,$m,$d,$y);
									$date=$next_date;
									$c_date=strtotime($date);
									$i++;
									}
								$i=$i-1;	
								$a=0;
								for ($a=0;$a<=$i;$a++)
									{
									$date=$time_array[$a];
									
									$sql1 = "SELECT * FROM schedule_scenarios WHERE device_id='1' AND status='1' AND day='$date' 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')) ";
									$res=mysql_query($sql1);
									$resulta = mysql_fetch_array($res);
									if ($resulta!="")
										{
										$msg="$head5"."<b>".$date."</b>"."$head6";
										$c="invalid";
										break;
										}
									
									}
									if ($msg=="")
										{
										for ($a=0;$a<=$i;$a++)
											{
											$date=$time_array[$a];
											$insert=schedule_scenario($scenario,$device_id,$date,$start_time,$end_time,$option=1);
											if ($insert==5)
												{
												$msg="$head3";
												}
											}
										}
									
								}
							}
						}
					else
						{
						$msg="$head7";
						$c="invalid";
						}
					}
				
				}
			else
				{
				$msg="$hea9";
				$c="invalid";
				}
				
			}
		else
			{
			$msg="$head8";
			$c="invalid";
			}
		}
	}
	else
	{
	$msg="$head10";
	$c="invalid";
	}
db_close();
	}

echo<<<a
<span class="$c">$msg</span>
a;
?>
