<?php
session_start();
ini_set('max_execution_time', 300000);
include("include/setsession.php");
require_once("include/connect.inc");
require_once("include/functions.php");
mysqli_query($link, "SET character_set_results=utf8");

$txt="";
$id=0;
$idtype			= 0;
$description	= "";
$timeedition	= 0;
$maxtimeup		= 0;
$days			= "";
$path			= "";
$name			= "";
if (isset($_GET['id']) && $_GET['id']!='') $id = $_GET['id'];

$txt.='<table align="left" cellspacing="6" width="100%">
		<tr>
			<td colspan="9" bgcolor="#515151">
				<div class="headerDet">EDIZIONI</div>
				<div id="btnClose" title="close" onclick="javascript:closeDet();"></div>
			</td>
		</tr>';
		if($id==0) {
			$txt.='<tr><td colspan="9">
				<form name="insert" id="insert" method="POST" enctype="multipart/form-data" action="sql_setEditions.php">
				<table align="left" cellspacing="6" width="1280">
				<tr style="font-weight:bold">
					<td width="30" align="left"></td>
					<td width="150" align="left">TIPO</td>
					<td width="100" align="left">ORA</td>
					<td width="100" align="left">LIMITE UPLOAD</td>
					<td width="230" align="left"></td>
					<td width="100" align="left">PATH</td>
					<td width="100" align="left">FILENAME</td>
					<td width="20"></td>
					<td width="20"></td>
				</tr>
				<tr>
				<td width="30"></td>
				<td width="150">
					<select name="idtype" id="idtype" class="sel200">
						<option value="0"></option>';
						$sql="SELECT * FROM ".$s_prefix."_editionstype ORDER BY description";
						$valb = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
						while ($rowb = mysqli_fetch_array($valb, MYSQLI_ASSOC)) {
							$txt.='<option value="'.$rowb['id'].'">'.$rowb['description'].'</option>';
						}
				$txt.='</select>
				</td>
				<td width="100"><input type="text" id="timeedition" name="timeedition" onkeypress="return checkTime(this,event);" onkeyup="formatTime(this,event);" onpaste="return false;" style="width:80px;" required /></td>
				<td width="100"><input type="text" id="maxtimeup" name="maxtimeup" onkeypress="return checkTime(this,event);" onkeyup="formatTime(this,event);" onpaste="return false;" style="width:80px;" required /></td>
				<td width="230">';
					// 0 sunday - 6 sabato  //$dayofweek = date('w', strtotime($date));						
				$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
					<b>Mon</b><br>';
					$txt.='<input type="checkbox" id="day_1" name="day_1" value="1"/>
					</div>';

				$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
					<b>Tue</b><br>';
					$txt.='<input type="checkbox" id="day_2" name="day_2" value="1"/>
					</div>';

				$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
					<b>Wed</b><br>';
					$txt.='<input type="checkbox" id="day_3" name="day_3" value="1"/>
					</div>';
				
				$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
					<b>Thu</b><br>';
					$txt.='<input type="checkbox" id="day_4" name="day_4" value="1"/>
					</div>';
				
				$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
					<b>Fri</b><br>';
					$txt.='<input type="checkbox" id="day_5" name="day_5" value="1"/>
					</div>';

				$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
					<b>Sat</b><br>';
					$txt.='<input type="checkbox" id="day_6" name="day_6" value="1"/>
					</div>';

				$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
					<b>Sun</b><br>';
					$txt.='<input type="checkbox" id="day_0" name="day_0" value="1"/>
					</div>';

				$txt.='</td>
				<td width="100"><input type="text" id="path" name="path" /></td>
				<td width="100"><input type="text" id="name" name="name" /></td>
				<td width="20">
					<input type="hidden" name="id" id="id" value="0" />
					<input id="salva" type="submit" value="Save" />
				</td>
				<td width="20"></td>
				</tr>
				</table>
				</form>
				</td></tr>
				<tr height="0"><td colspan="9"><hr /></td></tr>';
		}
		$txt.='<tr style="font-weight:bold">
			<td width="30" align="left">ID</td>
			<td width="150" align="left">TIPO</td>
			<td width="100" align="left">ORA</td>
			<td width="100" align="left">LIMITE UPLOAD</td>
			<td width="230" align="left">GIORNI</td>
			<td width="100" align="left">PATH</td>
			<td width="100" align="left">FILENAME</td>
			<td width="20"></td>
			<td width="20"></td>
		</tr>
		<tr height="0"><td colspan="9"><hr /></td></tr>';
		$sql="SELECT a.*, b.description FROM ".$s_prefix."_editions a LEFT JOIN ".$s_prefix."_editionstype b ON a.idtype=b.id ORDER BY b.description, a.timeedition";
		$val = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
		while ($row = mysqli_fetch_array($val, MYSQLI_ASSOC)) {
			if($id==$row['id']) {
				$txt.='<tr><td colspan="9">
						<form name="insert" id="insert" method="POST" enctype="multipart/form-data" action="sql_setEditions.php">
						<table align="left" cellspacing="6" width="1280">
						<tr>
						<td width="30">'.$row['id'].'</td>
						<td width="150">
							<select name="idtype" id="idtype" class="sel200">
								<option value="0"></option>';
								$sql="SELECT * FROM ".$s_prefix."_editionstype ORDER BY description";
								$valb = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
								while ($rowb = mysqli_fetch_array($valb, MYSQLI_ASSOC)) {
									$selected='selected="selected"';
									if($row['idtype']!=$rowb['id']) $selected='';
									$txt.='<option value="'.$rowb['id'].'" '.$selected.'>'.$rowb['description'].'</option>';
								}
						$txt.='</select>
						</td>
						<td width="100"><input type="text" id="timeedition" name="timeedition" onkeypress="return checkTime(this,event);" onkeyup="formatTime(this,event);" onpaste="return false;" style="width:80px;" value="'.formatHHMM($row['timeedition']/1000).'" required /></td>
						<td width="100"><input type="text" id="maxtimeup" name="maxtimeup" onkeypress="return checkTime(this,event);" onkeyup="formatTime(this,event);" onpaste="return false;" style="width:80px;" value="'.formatHHMM($row['maxtimeup']/1000).'" required /></td>
						<td width="230">';
							// 0 sunday - 6 sabato  //$dayofweek = date('w', strtotime($date));						
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Mon</b><br>';
							$ck="";
							if(substr($row['days'], 1, 1)==1) $ck='checked="checked"';
							$txt.='<input type="checkbox" id="day_1" name="day_1" value="1" '.$ck.'/>
							</div>';
	
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Tue</b><br>';
							$ck="";
							if(substr($row['days'], 2, 1)==1) $ck='checked="checked"';
							$txt.='<input type="checkbox" id="day_2" name="day_2" value="1" '.$ck.'/>
							</div>';
	
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Wed</b><br>';
							$ck="";
							if(substr($row['days'], 3, 1)==1) $ck='checked="checked"';
							$txt.='<input type="checkbox" id="day_3" name="day_3" value="1" '.$ck.'/>
							</div>';
						
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Thu</b><br>';
							$ck="";
							if(substr($row['days'], 4, 1)==1) $ck='checked="checked"';
							$txt.='<input type="checkbox" id="day_4" name="day_4" value="1" '.$ck.'/>
							</div>';
						
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Fri</b><br>';
							$ck="";
							if(substr($row['days'], 5, 1)==1) $ck='checked="checked"';
							$txt.='<input type="checkbox" id="day_5" name="day_5" value="1" '.$ck.'/>
							</div>';
	
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Sat</b><br>';
							$ck="";
							if(substr($row['days'], 6, 1)==1) $ck='checked="checked"';
							$txt.='<input type="checkbox" id="day_6" name="day_6" value="1" '.$ck.'/>
							</div>';

						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Sun</b><br>';
							$ck="";
							if(substr($row['days'], 0, 1)==1) $ck='checked="checked"';
							$txt.='<input type="checkbox" id="day_0" name="day_0" value="1" '.$ck.'/>
							</div>';

						$txt.='</td>
						<td width="100"><input type="text" id="path" name="path" value="'.$row['path'].'" /></td>
						<td width="100"><input type="text" id="name" name="name" value="'.$row['name'].'" /></td>
						<td width="20">
							<input type="hidden" name="id" id="id" value="'.$id.'" />
							<input id="salva" type="submit" value="Save" />
						</td>
						<td width="20"><div id="btnCancelSmall" title="delete" onclick="javascript:showEditions(0)"></div></td>
						</tr>
						</table>
						</form>
						</td></tr>
						<tr height="0"><td colspan="9"><hr /></td></tr>';
			} else {
				$txt.='<tr>
						<td>'.$row['id'].'</td>
						<td>'.$row['description'].'</td>
						<td>'.formatHHMM($row['timeedition']/1000).'</td>
						<td>'.formatHHMM($row['maxtimeup']/1000).'</td>
						<td>';
							// 0 sunday - 6 sabato  //$dayofweek = date('w', strtotime($date));
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Mon</b><br>';
							$ck="";
							if(substr($row['days'], 1, 1)==1) $txt.='<img src="img/icons/checked_checkbox.png" width="16" height="16" />';
							else $txt.='<img src="img/icons/unchecked_checkbox.png" width="16" height="16" />';
							$txt.='</div>';
	
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Tue</b><br>';
							$ck="";
							if(substr($row['days'], 2, 1)==1) $txt.='<img src="img/icons/checked_checkbox.png" width="16" height="16" />';
							else $txt.='<img src="img/icons/unchecked_checkbox.png" width="16" height="16" />';
							$txt.='</div>';
	
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Wed</b><br>';
							$ck="";
							if(substr($row['days'], 3, 1)==1) $txt.='<img src="img/icons/checked_checkbox.png" width="16" height="16" />';
							else $txt.='<img src="img/icons/unchecked_checkbox.png" width="16" height="16" />';
							$txt.='</div>';
						
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Thu</b><br>';
							$ck="";
							if(substr($row['days'], 4, 1)==1) $txt.='<img src="img/icons/checked_checkbox.png" width="16" height="16" />';
							else $txt.='<img src="img/icons/unchecked_checkbox.png" width="16" height="16" />';
							$txt.='</div>';
						
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Fri</b><br>';
							$ck="";
							if(substr($row['days'], 5, 1)==1) $txt.='<img src="img/icons/checked_checkbox.png" width="16" height="16" />';
							else $txt.='<img src="img/icons/unchecked_checkbox.png" width="16" height="16" />';
							$txt.='</div>';
	
						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Sat</b><br>';
							$ck="";
							if(substr($row['days'], 6, 1)==1) $txt.='<img src="img/icons/checked_checkbox.png" width="16" height="16" />';
							else $txt.='<img src="img/icons/unchecked_checkbox.png" width="16" height="16" />';
							$txt.='</div>';

						$txt.='<div style="float:left; align:center; padding-left:4px; padding-right:4px;">
							<b>Sun</b><br>';
							if(substr($row['days'], 0, 1)==1) $txt.='<img src="img/icons/checked_checkbox.png" width="16" height="16" />';
							else $txt.='<img src="img/icons/unchecked_checkbox.png" width="16" height="16" />';
							$txt.='</div>';

						$txt.='</td>
						<td>'.$row['path'].'</td>
						<td>'.$row['name'].'</td>
						<td><div id="btnEditSmall" title="Modifica" onclick="javascript:editEditions('.$row['id'].')"></div></td>
						<td><div id="btnDeleteSmall" title="Elimina" onclick="javascript:deleteEditions('.$row['id'].')"></div></td>
						</tr>
						<tr height="0"><td colspan="9"><hr /></td></tr>';
			}
		}
$txt.='</table>';
//$txt.='</td></tr></table>';

echo $txt;
?>