<?php
require '../2_Projects/FileOperations.php';
$con = mysql_connect($website,$userid,$pw);
if (!$con) {
	die('Could not connect: ' . mysql_error());
}

$curDB = "../CurrentlyOpenedDBName.tmp";
if(file_exists ($curDB)){
	$f = fopen($curDB, "r");
	$dbase   = trim(fgets($f)); // linked database name
	fclose($f);
	mysql_select_db($dbase, $con);
}else{
	return;
}
echo ("<head><link rel='stylesheet' type='text/css' href='../CSS/Font.css'>

<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js'></script>
<script>
$(document).ready(function(){
    $('button').click(function(){
        $('p').toggle();
    });
});
</script>
</head>");

if($debug)echo ("<script>  document.write(document.URL); </script>");
$data_freq = "no";
if($_GET[data_freq] == "yes")$data_freq = "yes";
$output_id = $_GET[output_id];
$frequency_id = $_GET[frequency_id];

if($_GET[act] == "yes"){
	if(isset($_POST[NotUpdateTable])){
	}elseif(isset($_POST[UpdateTable])){
		$start_time_day = $_POST[start_hour] . ":" . $_POST[start_minute];
		$stop_time_day = $_POST[stop_hour] . ":" . $_POST[stop_minute];
		if(count($_POST[day_of_week]) > 0)foreach($_POST[day_of_week]  as $str)$dow .= $str . ",";
		if($start_time_day == ":")$start_time_day = "";
		if($stop_time_day == ":")$stop_time_day = "";
		if($dow == ",")$dow = "";
		$dom = $_POST[day_of_month];

		if($frequency_id == "add"){
			mysql_query("INSERT INTO Frequency(name,period,times_per_period,start,stop,sta_tm_day,end_tm_day,day_o_week,day_o_mnth)
							VALUES('{$_POST[nameField]}','{$_POST[periodField]}','{$_POST[TPPField]}','{$_POST[startField]}','{$_POST[stopField]}','{$start_time_day}','{$stop_time_day}','{$dow}','{$dom}')",$con);
		}else{
			mysql_query("UPDATE Frequency SET 	name = '{$_POST[nameField]}',
												period = '{$_POST[periodField]}',
												times_per_period = '{$_POST[TPPField]}',
												start = '{$_POST[startField]}',
												stop = '{$_POST[stopField]}',
												sta_tm_day = '{$start_time_day}',
												end_tm_day = '{$stop_time_day}',
												day_o_week = '{$dow}',
												day_o_mnth = '{$dom}'
												WHERE frequency_id = {$frequency_id}",$con);
		}
	}

	echo ("<script>	window.opener.document.location.href ='Frequency.php?output_id={$output_id}&frequency_id={$frequency_id}&data_freq={$data_freq}';</script>");
	echo("<script> window.close() </script>");
}

if($frequency_id != "add"){ // User selected to edit init field values on the form
	$res = mysql_query("SELECT name,period,times_per_period,start,stop,sta_tm_day,end_tm_day,day_o_week,day_o_mnth FROM Frequency WHERE frequency_id={$frequency_id}",$con);
	while($row = mysql_fetch_array($res)){
		$name = $row[0];
		$period = $row[1];
		$times_per_period = $row[2];
		$start = $row[3];
		$stop = $row[4];
		$start_time_day = $row[5];
		$stop_time_day = $row[6];
		$day_of_week = $row[7];
		$day_of_month = $row[8];
	}
}


$sel_min = "";
$sel_hr = "";
$sel_day= "";
$sel_wk = "";
$sel_mnth = "";
if($period == "MINUTE"){
	$sel_min = "selected";
}elseif($period == "HOURLY"){
	$sel_hr = "selected";
}elseif($period == "DAILY"){
	$sel_day= "selected";
}elseif($period == "WEEKLY"){
	$sel_wk = "selected";
}elseif($period == "MONTHLY"){
	$sel_mnth = "selected";
}elseif($period == "" || $period = NULL){

}else{
	echo ("<h3> old format frequency defined please delete and redefine (now press cancel)</h3>");
	$redefine = true;
}

echo ("<form action='AddEditFrequency.php?act=yes&output_id={$output_id}&frequency_id={$frequency_id}&data_freq={$data_freq}' method='post' id='main_form'>");
if(!$redefine){


	echo ("<table><br/>");

	// Name field
	echo ("<tr>");
	echo ("<td> Name </td>");
	echo 	("<td><input 	type='text'  autocomplete='off'
							name='nameField'
							value='{$name}'
							maxlength = '{$smaStr}'
							size = '60' />
			</td>");
	echo ("</tr>");

	echo ("<tr>");
	echo ("<td> Every </td>");
	echo 	("<td><input 	type='text'  autocomplete='off'
							name='TPPField'
							value='{$times_per_period}'
							maxlength = '{$smaStr}'
							size = '20' />
			");

	echo 	("	<select name='periodField'>
				<option value = 'MINUTE'  {$sel_min}>Minute(s)</option>
				<option value = 'HOURLY'  {$sel_hr}>Hour(s)</option>
				<option value = 'DAILY'   {$sel_day}>Day(s)</option>
				<option value = 'WEEKLY'  {$sel_wk}>Week(s)</option>
				<option value = 'MONTHLY' {$sel_mnth}>Month(s)</option>
				</select>
			</td>");
	echo ("</tr>");

	if($data_freq != "yes"){
		echo ("<tr>");
		echo ("<td> Start </td>");
		echo 	("<td><input 	type='text'  autocomplete='off'
								name='startField'
								value='{$start}'
								maxlength = '{$smaStr}'
								placeholder='YYYY-MM-DD [hh:mm:ss]'
								size = '60' />
				</td>");
		echo ("</tr>");
	/*
	echo ("<tr>");
	echo ("<td> Stop </td>");
	echo 	("<td><input 	type='text'  autocomplete='off'
							name='stopField'
							value='{$stop}'
							maxlength = '{$smaStr}'
							placeholder='YYYY-MM-DD [hh:mm:ss]'
							size = '60' />
			</td>");
	echo ("</tr>");
	*/
	}

	echo ("</table><br/>");
	echo ("<button type='submit' name='UpdateTable' value='OK' >OK</button>&nbsp");
}
echo ("<button type='submit' name='NotUpdateTable' value='Cancel' >Cancel</button>");
echo ("</form><br/>");

// data processing frequency does not have advanced options
if($GeekMode && $data_freq != "yes")echo_advanced_frequency_options("main_form",$start_time_day,$stop_time_day,$day_of_week,$day_of_month,$oper_sys);
?>
