    <?php
	echo '
	<script type="text/javascript" src="'.$thisPage.'js/messages.js"></script>
	<link rel="stylesheet" type="text/css" href="'.$thisPage.'css/messages.css" />
	<link rel="stylesheet" type="text/css" href="'.$thisPage.'css/captcha.css" />';
	require_once ($thisUrl.'functions/form_creator.php');
	$idEvent = "CompletingForm";
	
	echo "
	<script>
		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;
		}
	</script>";
	
	if ($kd_header_id) {
		/** Edit Begin**/
		$sql = "SELECT * FROM kontak_dagang_header WHERE kd_header_id = '$kd_header_id' and tgl_transaksi = '$tglNAMA' ";
		if ($hasil = mysql_query($sql)) {
			if (mysql_num_rows($hasil) > 0) {
				while ($data = mysql_fetch_array($hasil)) {
				}
			}
		}
		/** Edit End**/
	} 
	
	$labelNameTitle = "";	
	if ($kd_header_id) {
		$labelNameTitle = "Update Existing Data";
	} else {
		$labelNameTitle = "Insert New Data";
	}
	
	$ctr = 0;
	if (isset($_POST["manage_form_action"])) {
		$manage_form_action = $_POST["manage_form_action"];
	}
	if ($manage_form_action) {		
		$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 = '';
		$nmDB = "kontak_dagang_header";
		if ($hasil1 = mysql_query($sql1)) {
			while ($data1 = mysql_fetch_array($hasil1)) {
				$labelName = $data1["labelName"];
				$designName = $data1["designName"];
				$$designName = $_POST[$designName];

				if (!$sql7) {
					if ($manage_form_action == "Save Data") {
						$sql7 = "INSERT INTO ".$nmDB." values(null, '".$exhibitorID."', '".$tglNAMA."' ";
					} else if ($manage_form_action == "Update Data") {
						$sql7 = "UPDATE ".$nmDB." set ";
					}
				} else {
					if ($manage_form_action == "Update Data") {
						$sql7 .= ", ";
					}
				}
				
				if ($manage_form_action == "Save Data") {
					if ($$designName) {
						$sql7 .= ", '".$$designName."'";
					} else {
						$sql7 .= ", 0";
					}
				} else if ($manage_form_action == "Update Data") {
					if ($$designName) {
						$sql7 .= "$designName = '".$$designName."' ";
					} else {
						$sql7 .= "$designName = 0 ";
					}
				}
			}
			if ($manage_form_action == "Save Data") {
				$sql7 .= ")";
			} else if ($manage_form_action == "Update Data") {
				$sql7 .= "WHERE kd_header_id = '".$kd_header_id."' ";
			}
		}
		//echo "<br>action: ".$action;
		//echo "<br><br>sql7: ".$sql7."<br>";

		if ($result = mysql_query($sql7)) {
			echo '<script>';
			echo 'location.href="'.$thisPage.'adm1n/'.$curPage1.'"';
			echo '</script>';
		} else {
			die('<br>Invalid query: ' . mysql_error());
		}
	} else {		
		?>
        <br />
        <form name="form" id="form" method="post" class="form" action="" 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)) {
					echo "<h4 class='h4-teasers'>".$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)) {
							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").'
								</td>
							</tr>';
						}
						if ($kd_header_id) {
							$btnAction = "Update Data";
						} else {
							$btnAction = "Save Data";
						}
					    echo '
							<tr valign="top" bgcolor="#e4e4e4">
								<td align="center" colspan="2">
									<div style="margin:20px 0px" align="center">
										<input type="hidden" name="action" id="idproduct" value="'.$action.'" />
										<input type="hidden" name="idproduct" id="idproduct" value="'.$idproduct.'" />
										<input type="submit" name="manage_form_action" id="manage_form_action" class="button" value="'.$btnAction.'">
									</div>
								</td>
							</tr>
						</table>
						<br><br>';
					}
                }
            }									
            ?>
        </form>
        <?php
	}
	?>