	<script type="text/javascript" src="<?php echo $thisPage; ?>js/messages.js"></script>
	<link rel="stylesheet" type="text/css" href="<?php echo $thisPage; ?>css/messages.css" />
	<link rel="stylesheet" type="text/css" href="<?php echo $thisPage; ?>css/captcha.css" />

	<script>
	jQuery(document).ready(function() {
		//alert(jQuery('#idproduct').val());
		var button = '';
		jQuery('#image-container').html('');   
		jQuery.post(
			'listImage.php',{
				'idproduct':jQuery('#idproduct').val(),
				'id':jQuery('#id').val()
			}, function(data) {
				jQuery.each(data, function(index, value) {
				//alert (index);
				jQuery('#'+index).append('<img id-"' + index + '" src="' + value + '" width="200" border="0"><br> ');
				jQuery('#button-'+index).click(function() {
					jQuery.post(
						'deleteImage.php',{
							'imageID':index
						}
					);
					$('#button-'+index).remove();
				});
			}
		);
		},'json');
		

	});
    </script>        

    <?php
	$idEvent = "ExhibitorAddProduct";
	echo "<script>";
		echo "function validate(form) {";	
		$sql = "select designName from form_label, form_design where labelType='".$idEvent."' and labelID=labelParentID and validasi='YES' order by designORDER asc";
		//echo "<br> sql : ".$sql; 
		if ($hasil = mysql_query($sql)) {
			while ($rec = mysql_fetch_array($hasil)) {
				if ($rec["designName"] != "website") {
					echo "var ".$rec["designName"]." = form.".$rec["designName"].".value;";
					echo '
						if('.$rec["designName"].' == "") {
							inlineMsg("'.$rec["designName"].'","Field can not be empty.",2);
							return false;
					  	} else {
							var targetdiv = document.getElementById("'.$rec["designName"].'"); 
					  		targetdiv.style.backgroundColor = "white";
					  	}
					';	
				}
			}
		}	
		echo "
				return true;
			}
		";

	echo "</script>";
	
	$addProductData = $_POST["addProductData"];
	//echo "<br>addProductData: 	".$addProductData;
	$ctr = 0;
	if ($addProductData == "Save Data" || $addProductData == "Update Data") {		
		$sql1 = "select designID, labelType, labelName, designName from 
				 form_label, form_design where 
				 labelType='".$idEvent."' and labelID=labelParentID 
				 order by designORDER asc";
		//echo "<br>sql: ".$sql1;
		$sql7 = '';
		if ($hasil1 = mysql_query($sql1)) {
			while ($data1 = mysql_fetch_array($hasil1)) {
				if (!$sql7) {
					if ($addProductData == "Save Data") {
						$sql7 = "INSERT INTO exhibitor_list_products values(null, '".$id."' ";
					} else if ($addProductData == "Update Data") {
						$sql7 = "UPDATE exhibitor_list_products set ";
					}
				} else {
					if ($addProductData == "Update Data") {
						$sql7 .= ", ";
					}
				}
				$labelName = $data1["labelName"];
				$designName = $data1["designName"];
				$$designName = $_POST[$designName];				
				
				$sql2 = "SELECT * FROM form_design_other WHERE labelParentID = '".$data1["designID"]."'";
				if ($hasil2 = mysql_query($sql2)) {
					if (mysql_num_rows($hasil2)) {
						while ($data2 = mysql_fetch_array($hasil2)) {
							$designName2 = $data2["designName"];
							$$designName2 = $_POST[$designName2];
							if ($$designName2 && $designName2 == "product_category_other") {
								$sql4 = "SELECT * FROM form_element_sub WHERE LOWER(elementSubName) = LOWER('".$$designName2."')";
								if ($hasil4 = mysql_query($sql4)) {
									if (!mysql_num_rows($hasil4)) {
										$sql8 = "INSERT INTO form_element_sub VALUES (null, 
												'".$$designName2."', 'single', '".$$designName2."')";
										//echo "<br> sql8: ".$sql8;
										$result = mysql_query($sql8);
										if (!$result) {
											die('Invalid query: ' . mysql_error());
										} else {
											$idNext = mysql_insert_id();
											$$designName = $idNext;
											$sql8 = "INSERT INTO form_element_link VALUES (null, 
													'".$data1["designID"]."', '".$$designName."')";
											//echo "<br> sql8: ".$sql8;
											$result1 = mysql_query($sql8);
											if (!$result1) {
												die('Invalid query: ' . mysql_error());
											}
										}
									}
								}
							} else if ($$designName2 && $designName2 == "product_sub_category_other") {
								//echo "<br>$designName : ".$$designName;
								if ($$designName) {
									$sql3 = "SELECT * FROM form_element_value WHERE valueID = '".$$designName."' ";
									//echo "<br>sql3: ".$sql3;
									if ($hasil3 = mysql_query($sql3)) {
										while ($data3 = mysql_fetch_array($hasil3)) {
											$sql4 = "SELECT * FROM form_element_value WHERE LOWER(valueName) = LOWER('".$$designName2."')";
											//echo "<br>sql4: ".$sql4;
											if ($hasil4 = mysql_query($sql4)) {
												if (!mysql_num_rows($hasil4)) {
													$sql8 = "INSERT INTO form_element_value VALUES (null, 
															'".$data3["elementSubParentID"]."', '".$$designName2."')";
													//echo "<br> sql8: ".$sql8;
													$result = mysql_query($sql8);
													if (!$result) {
														die('<br>Invalid query: ' . mysql_error());
													} else {
														$idNext2 = mysql_insert_id();
													}
													
												}
											}
										}
									}
								} else {
									if ($idNext) {
										$sql8 = "INSERT INTO form_element_value VALUES (null, 
												'".$idNext."', '".$$designName2."')";
										//echo "<br> sql8: ".$sql8;
										$result = mysql_query($sql8);
										if (!$result) {
											die('Invalid query: ' . mysql_error());
										} else {
											
											$idNext2 = mysql_insert_id();
											
											$sql8 = "INSERT INTO form_element_value VALUES (null, 
													'".$idNext."', 'Others, please specify (Lainnya, masukkan disini)')";
											$result2 = mysql_query($sql8);
											if (!$result2) {
												die('Invalid query: ' . mysql_error());
											}
										}
									}
								}
								if ($result) {									
									$$designName = $idNext2;
								}
							}
						}
					}
				}
				if ($$designName) {
					if ($addProductData == "Save Data") {
						$sql7 .= ", '".$$designName."'";
					} else if ($addProductData == "Update Data") {
						$sql7 .= "$designName = '".$$designName."' ";
					}
				}
				else {
					if ($addProductData == "Save Data") {
						$sql7 .= ", 0";
					} else if ($addProductData == "Update Data") {
						$sql7 .= "$designName = 0 ";
					}
				}

				$sql5 = "select designName, labelParentID from form_design_extention where labelParentID = '".$data1["designID"]."'";
				//echo "<br>sql5: ".$sql5;
				if ($hasil5 = mysql_query($sql5)) {
					while ($data5 = mysql_fetch_array($hasil5)) {
						$designName5 = $data5["designName"];
						$$designName5 = $_POST[$designName5];
						$temp = $$designName5;
						//echo "<br>temp: ".$temp;
						//echo "<br>$designName5: ".$$designName5;
						$sql6 = "SELECT designID, designName, labelParentID FROM form_design_other WHERE labelParentID = '".$data5["labelParentID"]."'";
						//echo "<br>sql6: ".$sql6;
						if ($hasil6 = mysql_query($sql6)) {
							while ($data6 = mysql_fetch_array($hasil6)) {
								$designName6 = $data6["designName"];
								$$designName6 = $_POST[$designName6];
								$sql4 = "SELECT * FROM form_element_value WHERE valueID = '".$$designName5."'";
								//echo "<br>sql4: ".$sql4;
								$elementSubParentID4 = '';
								if ($hasil4 = mysql_query($sql4)) {
									while ($data4 = mysql_fetch_array($hasil4)) {
										$elementSubParentID4 = $data4["elementSubParentID"];
									}
								}
								if ($elementSubParentID4 && $$designName6) {
									$sql4 = "SELECT * FROM form_element_value WHERE elementSubParentID = '".$elementSubParentID4."' 
											AND LOWER(valueName) = LOWER('".$$designName6."')";
									//echo "<br>".$sql4;
									if ($hasil4 = mysql_query($sql4)) {
										if (!mysql_num_rows($hasil4)) {
											$sql8 = "INSERT INTO form_element_value VALUES (null, 
													'".$elementSubParentID4."', '".$$designName6."')";
											//echo "<br>sql8: ".$sql8;
											$result = mysql_query($sql8);
											if (!$result) {
												die('Invalid query: ' . mysql_error());
											} else {
												$idNext3 = mysql_insert_id();
												$temp = $idNext3;
											}
										} else {
											while ($data4 = mysql_fetch_array($hasil4)) {
												$temp = $data4["valueID"];
											}
										}
									}
								}
							}
						}
						//echo "<br>$designName6: ".$$designName6;
						if ($temp) {
							if ($addProductData == "Save Data") {
								$sql7 .= ", '".$temp."'";
							} else if ($addProductData == "Update Data") {
								$sql7 .= ", $designName5 = '".$temp."' ";
							}
						} else {
							if ($addProductData == "Save Data") {
								$sql7 .= ", 0";	
							} else if ($addProductData == "Update Data") {
								$sql7 .= ", $designName5 = 0 ";
							}
						}
					}
				}
				
			}
			if ($addProductData == "Save Data") {
				$sql7 .= ")";
			} else if ($addProductData == "Update Data") {
				$sql7 .= "WHERE listProductID = '".$idproduct."' ";
			}
		}
		//echo "<br><br>".$sql7."<br>";

		$result = mysql_query($sql7);
		if (!$result) {
			die('<br>Invalid query: ' . mysql_error());
		} else {
			foreach ($_FILES["productImage"]["error"] as $key => $error) {
				if ($error == UPLOAD_ERR_OK) {
					$uploaddir = $thisUrl.'product_image/';
					chmod($uploaddir, 0775); 
					
					$uploaddir = $thisUrl.'product_image/'.$id.'/';
					if (!file_exists($uploaddir)) {
						mkdir($uploaddir);
					}
					chmod($uploaddir, 0775); 
					
					$uploaddir = $thisUrl.'product_image/'.$id.'/'.$idproduct.'/';
					if (!file_exists($uploaddir)) {
						mkdir($uploaddir);
					}
					
					chmod($uploaddir, 0775); 
					$tmp_name = $_FILES["productImage"]["tmp_name"][$key];
					$name = basename($_FILES["productImage"]["name"][$key]);
					chmod($name, 0644);
					
					if (move_uploaded_file($tmp_name, $uploaddir.$name)) {
						$sql8 = "INSERT INTO exhibitor_list_image VALUES (null, 
								'".$idproduct."', '".$name."')";
						$result2 = mysql_query($sql8);
						if (!$result2) {
							die('<br>Invalid query: ' . mysql_error());	
						}
					}
					$uploaddir = $thisUrl.'product_image/';
					chmod($uploaddir, 0755); 
				}
			}		
			echo '<script>';
			echo 'location.href="'.$thisPage.'adm1n/pages/product-services/?action=addProduct&id='.$id.'"';
			echo '</script>';
		}
	} else {		
		//echo "test";
		require_once ($thisUrl.'functions/form_creator.php');
		?>
        <br />
        <form name="form" id="form" method="post" class="form" action="?action=add" enctype="multipart/form-data" onsubmit="return validate(this)">
			<?php
			$sql = "select * from form_label where labelType='$idEvent'";
            //echo "<br> sql".$sql; 
            if ($hasil = mysql_query($sql)) {		
                while ($rec = mysql_fetch_array($hasil)) {
                    if ($rec["labelNameTitle"]) {
						echo "<h4 class='h4-teasers'>".$rec["labelNameTitle"]."</h4>";
					}
					$sql2 = "select * from form_design, form_element where elementID = element AND labelParentID = ".$rec["labelID"]." order by designORDER asc";
                    // echo $sql2;
                    if ($hasil2 = mysql_query($sql2)) {
                        echo '
						<table width="100%" cellpadding="10" cellspacing="1" border="0" bgcolor="#999999">';
                        while ($rec2 = mysql_fetch_array($hasil2)) {
							$sql3 = "SELECT * FROM exhibitor_list_products WHERE listProductID = '$idproduct' ";
							if ($hasil3 = mysql_query($sql3)) {		
								while ($data3 = mysql_fetch_array($hasil3)) {
									${$rec2["designName"]} = $data3[$rec2["designName"]];
									//echo "<br>".$rec2["designName"]." : ".${$rec2["designName"]};
								}
							}
							echo '
							<tr valign="top" bgcolor="#e4e4e4">
								<td width="28%" style="background-color:#e4e4e4;">
									<strong>'
										.$rec2["labelName"].'<br>'
										.($rec2["labelNameEng"] ? '(<i>'.$rec2["labelNameEng"].'</i>)' : '').'	
									</strong>
								</td>';
                                echo '
								<td style="background-color:#e4e4e4;">
									'.$rec2["elementName"]($rec["labelID"], $rec2["designID"], 0, "form_design", "form_element", "elementID", "element", "labelParentID", "designID").'
									'.extention($rec2["designID"]).'
									'.others($rec2["designID"]).'
								</td>
							</tr>';
						}
						if ($action == "editProduct") {
							$btnProductData = "Update Data";
						} else {
							$btnProductData = "Save Data";
						}
					    echo '
							<tr valign="top" bgcolor="#e4e4e4">
								<td><strong>Product Image</strong></td>
								<td>';
									$sql3 = "SELECT * FROM exhibitor_list_image WHERE productID = '".$idproduct."'";
									if ($hasil3 = mysql_query($sql3)) {
										while ($data3 = mysql_fetch_array($hasil3)) {
											echo ' 
											<spam id="button-image-'.$data3["imageID"].'">
												<input type="button" class="button-image" value="Delete"/></span>
												<br><span id="image-'.$data3["imageID"].'"/></span>
												<br><br>
											</spam>';
										}
									}
									echo '
									<div id="filesContainer"></div>
									<div>
										<input type="button" name="addFile" id="addFile" class="button" value="Add Image">
									</div>
								</td>
							</tr>
							<tr valign="top" bgcolor="#e4e4e4">
								<td align="center" colspan="2">
									<div style="margin:20px 0px" align="center">
										<input type="hidden" name="action" id="action" value="addProduct" />
										<input type="hidden" name="addProduct" id="addProduct" value="Manage Products">
										<input type="hidden" name="id" id="id" value="'.$id.'" />
										<input type="hidden" name="idproduct" id="idproduct" value="'.$idproduct.'" />
										<input type="submit" name="addProductData" id="addProductData" class="button" value="'.$btnProductData.'">
									</div>
								</td>
							</tr>
						</table>
						<br><br>';
					}
                }
            }									
            ?>
        </form>
        <?php
	}
	?>