	<script type="text/javascript" src="../functions/cleanhtml.js"></script>

	<?php	
	if ($menu == 17) {
		include ("ads_setup.php");
	} else {
		$regForm = $_REQUEST["regForm"];
		$idContent = $_REQUEST["id"];	
		$targetPage = "../images/content/adds/";
		if ($action=="add" || $action=="edit" || $action=="delete") {
			if ($regForm == "Submit Data") {
				?>
				<p></p>
				<div align="center">
					<img src="images/loading.gif" border="0"><br>
					<strong>Updating on progress</strong>. <br>
				</div>
				<p></p>
				<?php
				$title = $_POST["title"];
				$position = $_POST["position"];
				$links1 = $_POST["links1"];
				$exp_date = $_POST["thn"].'-'.$_POST["bln"].'-'.$_POST["tgl"];
	
				$gambar1 = $_FILES['gambar1']['name'];			
				$old_gambar1 = $_POST["old_gambar1"];
				
				$placement = $_POST["ads"];
				
				if ($action=="add") {
					$sql2 = "insert into adds_master values (null,'$title','$position','$links1','$gambar1','$exp_date') ";
					if ($hasil2 = mysql_query($sql2)) {
						$id_last = mysql_fetch_array(mysql_query("select id from adds_master order by id desc limit 0,1"));
						if (!$idContent) {
							$idContent = $id_last[0];
						}
						foreach ($placement as $arr) {
							$sql3 = "insert into adds_placement values (null,'$idContent','$arr') ";
							mysql_query($sql3);
						}
					}
					
				} else if ($action=="edit") {				
					$sql  = "update adds_master set title='$title', position='$position', links='$links1' "; 
					if ($gambar1) $sql .= ", gambar='$gambar1' "; 
					$sql .= ", exp_date='$exp_date' where id='$idContent' ";
					if ($hasil = mysql_query($sql)) {
						$sql2 = "delete from adds_placement where adds_id='$idContent' ";
						if ($hasil2 = mysql_query($sql2)) {
							foreach ($placement as $arr) {
								$sql3 = "insert into adds_placement values (null,'$idContent','$arr') ";
								mysql_query($sql3);
							}
						}
					}
				} else if ($action=="delete") {				
					$sql  = "delete from adds_master where id='$idContent' "; 
					if ($hasil = mysql_query($sql)) {
						$sql2 = "delete from adds_placement where adds_id='$idContent' ";
						if ($hasil2 = mysql_query($sql2)) {
						}
					}
				}
						
				if ($gambar1) {
					$uploaddir = $targetPage;
					chmod($uploaddir , 0755);
					if (move_uploaded_file($_FILES['gambar1']['tmp_name'], $uploaddir.$gambar1)){										
						if ($old_gambar1) {
							unlink ($uploaddir.$old_gambar1);
						}
					}
				}
	
				$alamat  = '<script language=JavaScript>';
				$alamat .= ' location.href = "?menu='.$menu.'&tipe='.$tipe;
				$alamat .= ' "; ';
				$alamat .= '</script>';
				echo $alamat;
			} 
			else {
				$sql = "select *, day(exp_date) as tgl, month(exp_date) as bln, year(exp_date) as thn from adds_master where id='$idContent'";
				if ($hasil = mysql_query($sql)) {
					// echo $sql;
					while ($data = mysql_fetch_array($hasil)) {
						$id = $data["id"];
						$title = $data["title"];
						$position = $data["position"];
						$links1 = $data["links"];
						$gambar1 = $data["gambar"];
						$exp_date = $data["exp_date"];
						$tgl = $data["tgl"];
						$bln = $data["bln"];
						$thn = $data["thn"];
					}
				}
				
				if ($action=="delete") {
					?>
					<table cellpadding="5" cellspacing="0" border="0">
						<tr><td><strong><?php echo strtoupper($nama_menu); ?> >> <?php echo strtoupper($action)." ".strtoupper($nama_menu); ?></strong></td></tr>
						<tr valign="top">
							<td>									
								<form method="post" action="<?php echo '?menu='.$menu.'&action='.$action;?>" enctype="multipart/form-data" name="form1" onSubmit="submitForm(); cleanHTML();">
									<div align="center">
										Anda Ingin Hapus Data Iklan Berikut?
										<div id="spasi1d"></div>
									</div>
									<table  width="100%" cellpadding="7" cellspacing="1" border="0" bgcolor="#999999">
										<tr bgcolor="#FFFFFF">
											<td align="center">
												<table cellpadding="7" cellspacing="0" border="0">
													<tr>
														<td align="right"><strong>Title</strong></td> 
														<td align="center">:</td>
														<td><?php echo $title;?></td>
													</tr>
													<tr>
														<td align="center" colspan="3">
															<input type="submit" name="regForm" value="Submit Data">
															<input type="hidden" name="id" value="<?php echo $idContent; ?>">
															<input type="hidden" name="tipe" value="<?php echo $tipe; ?>">
														</td>
													</tr>
												</table>											
											</td>
										</tr>
									</table>
								</form>
							</td>
						</tr>
					</table>																													
					<?php
				} else {
					?>
					<table width="100%" cellpadding="5" cellspacing="0" border="0">
						<tr><td><strong><?php echo strtoupper($nama_menu); ?> >> <?php echo strtoupper($action)." ".strtoupper($nama_menu); ?></strong></td></tr>
						<tr valign="top">
							<td>									
								<form method="post" action="<?php echo '?menu='.$menu.'&action='.$action;?>" enctype="multipart/form-data" name="form1" onSubmit="submitForm(); cleanHTML();">
									<table cellpadding="7" cellspacing="0" border="0" bgcolor="#999999">
										<tr valign="top" bgcolor="#f6f6f6"><td colspan="2"></td></tr>
										<tr bgcolor="#f6f6f6">
											<td width="180" align="right"><strong>Title</strong></td> 
											<td><input type="text" name="title" value="<?php echo $title;?>" size="70"></td>
										</tr>
										<tr bgcolor="#f6f6f6"><td colspan="2"><div id="grs-lurus"></div></td></tr>								
										<tr valign="top" bgcolor="#f6f6f6">
											<td align="right"><strong>Position</strong></td>
											<td>
											  <?php 
												$selectForm = "";
												$sql2 = "select * from adds_position ";
												if ($hasil2 = mysql_query($sql2)) {
													while ($data2 = mysql_fetch_array($hasil2)) {
														if ($selectForm == "") {
															$selectForm = '<select name="position" id="position">';
														}
														$selectForm .= '<option ';
														if ($position == $data2["id"])
															$selectForm .= 'selected ';
														
														$selectForm .= 'value="'.$data2["id"].'">'.$data2["position"].'</option>';
													}
													if ($selectForm)
														$selectForm .= "</select>";
												}
												echo $selectForm;
												?>
											</td>
										</tr>
										<tr bgcolor="#f6f6f6"><td colspan="2"><div id="grs-lurus"></div></td></tr>								
										<tr valign="top" bgcolor="#f6f6f6">
											<td align="right">
												<strong>Links</strong> <br>
												(http://www.domain.com)
											</td>
											<td>
												<input type="text" name="links1" id="links1" value="<?php echo $links1; ?>" size="70">
											</td>
										</tr>
										<tr bgcolor="#f6f6f6"><td colspan="2"><div id="grs-lurus"></div></td></tr>
										<tr bgcolor="#f6f6f6">
											<td align="right"><strong>Image</strong></td>
											<td>
												<?php
												if ($gambar1) {
													echo '<img width="350" src="'.$targetPage.$gambar1.'" border="0">';
												}
												?>									
												<input type="file" name="gambar1" size="76">
											</td>
										</tr>
										<tr bgcolor="#f6f6f6"><td colspan="2"><div id="grs-lurus"></div></td></tr>								
										<tr valign="top" bgcolor="#f6f6f6">
											<td align="right"><strong>Expiration Date</strong></td>
											<td>
												<select name="tgl">
													<?php
													for($i=1; $i<=31; $i++) {
														?>
														<option <?php if ($tgl==$i) echo " selected "; ?> value="<?php echo $i?>"><?php echo $i?></option>
														<?php
													}
													?>
												</select>
												<select name="bln">
													<?php
													$nmbulan = array (
														1 => 'Jan', 2 => 'Feb', 3 => 'Mar', 4 => 'Apr', 5 => 'Mei', 6 => 'Jun', 7 => 'Jul', 8 => 'Agt', 9 => 'Sep', 10 => 'Okt', 11 => 'Nov', 12 => 'Des',
													);
													for($i=1; $i<=12; $i++) {
														?>
														<option <?php if ($bln==$i) echo " selected "; ?> value="<?php echo $i?>"><?php echo $nmbulan[$i]?></option>
														<?php
													}
													?>
												</select>
												<select name="thn">
													<?php
													$startDate = date("Y");
													$endDate = $startDate + 5;
													for($i=$startDate; $i<=$endDate; $i++) {
														?>
														<option <?php if ($thn==$i) echo " selected "; ?> value="<?php echo $i?>"><?php echo $i?></option>
														<?php
													}
													?>
												</select>														
											</td>
										</tr>
										<tr bgcolor="#f6f6f6"><td colspan="2"><div id="grs-lurus"></div></td></tr>								
										<tr valign="top" bgcolor="#f6f6f6">
											<td align="right"><strong>Ads Placement</strong></td>
											<td>										
												<?php
												$i=0;
												$checks = "";
												$sql2 = "select * from menus_head where stats <> '6' ";
												if ($hasil2 = mysql_query($sql2)) {
													while ($data2 = mysql_fetch_array($hasil2)) {
														if ($checks == "")
															$checks = '<table width="100%"><tr>';
														
														if ($i % 4 == 0)	
															$checks .= '</tr><tr>';
															
														$checks .= '<td>';	
														$checks .= '	<input type="checkbox" ';
														
														$sql3 = "select * from adds_placement where adds_id='$id' and placement='".$data2["id"]."' ";
														if ($hasil3 = mysql_query($sql3)) {
															while ($data3 = mysql_fetch_array($hasil3)) {
																$checks .= ' checked ';	
															}
														} 
		
														$checks .= '	name="ads[]" value="'.$data2["id"].'"> ';
														$checks .= 		strtoupper ($data2["namaMenu"]);
														$checks .= '</td>';	
														$i++; 
													}
												}
												if ($checks)
													$checks .= '</tr></table>';
		
												echo $checks;
												?>
											</td>
										</tr>
										<tr bgcolor="#ffffff">
											<td align="center" colspan="2">
												<div id="spasi1"></div>
												<input type="submit" name="regForm" value="Submit Data">
												<input type="hidden" name="old_gambar1" value="<?php echo $gambar1; ?>">
												<input type="hidden" name="tanggal" value="<?php echo $tanggal; ?>">
												<input type="hidden" name="tahun" value="<?php echo $tahun; ?>">
												<input type="hidden" name="bulan" value="<?php echo $bulan; ?>">
												<input type="hidden" name="id" value="<?php echo $idContent; ?>">
												<input type="hidden" name="curtitle" value="<?php echo $curtitle; ?>">
												<input type="hidden" name="targetPage" value="<?php echo $targetPage; ?>">
												<input type="hidden" name="tipe" value="<?php echo $tipe; ?>">
												<div id="spasi1"></div>
											</td>
										</tr>
									</table>											
								</form>
							</td>
						</tr>
					</table>																													
					<?php
				}			
			}
		} 
		else {
			?>
			<table width="100%" cellpadding="5" cellspacing="0" border="0">
				<tr><td><strong>MENU SELECTED >> <?php echo strtoupper($nama_menu); ?></strong></td></tr>
				<tr valign="top">
					<td>
						<div align="right">
							<div id="box" style="width:107px;" onClick="location='?menu=<?php echo $menu; ?>&tipe=<?php echo $tipe;?>&action=add'">
								<strong>Add New Data</strong>
							</div>
						</div>
						<table width="100%" cellpadding="6" cellspacing="1" bgcolor="#CCCCCC">
							<tr bgcolor="#EEEEEE">
								<td width="7%" align="center"><strong>ID</strong></td>
								<td width="33%"><strong>Title</strong></td>
								<td width="15%"><strong>Position</strong></td>
								<td width="30%"><strong>Expiration Date</strong></td>
								<td width="15%" align="center"><strong>Action</strong></td>
							</tr>										
							<?php 
							if ($koneksi){
								$nomor = 0;
								$query="select *, a.id as a_id, b.position as b_position from adds_master as a, adds_position as b where a.position=b.id order by exp_date ";
								if ($hasil=mysql_query($query)) {
									while($data=mysql_fetch_array($hasil)){													
										$nomor=$nomor+1;
										$bgcolor = "#FFFFFF";
										if ($data["exp_date"] < date ("Y-m-d"))
											$bgcolor = "#fceeef";
										else 
											$bgcolor = "#f3fcee";	
										?>
										<tr bgcolor="<?php echo $bgcolor; ?>">
											<td align="center"><?php echo $nomor; ?> </td>
											<td><?php echo $data["title"]; ?></td>
											<td><?php echo $data["b_position"]; ?></td>
											<td><?php echo date("l, F  d,  Y", strtotime($data["exp_date"])); ?></td>
											<td align="center">
												<a href="<?php echo "?menu=".$menu."&id=".$data["a_id"]."&action=edit&tipe=".$tipe; ?>">Edit</a>
												&nbsp; | &nbsp; 
												<a href="<?php echo "?menu=".$menu."&id=".$data["a_id"]."&action=delete&tipe=".$tipe; ?>">Delete</a>
											</td>
										</tr>
										<?php
									}
								}
							}	
							?>
						</table>
					</td>
				</tr>
			</table>				
			<?php
		}
	}
	?>
