	<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>
	    function validate(form) {
		    var companyName = form.companyName.value;
		    if(companyName == "") {
			    inlineMsg("companyName","Company Name Harus Diisi.",2);
			    return false;
		    } else {
			    var targetdiv = document.getElementById("companyName"); 
			    targetdiv.style.backgroundColor = "white";
		    }
		    return true;
	    }	
	</script>	   
    <?php
	$idEvent = "ConfirmProduct";
    $addProduct = (isset($_POST["addProduct"]) ? $_POST["addProduct"] : null);
    if($addProduct == "addProduct") {
        $productName = (isset($_POST["productName"]) ? $_POST["productName"] : null);
        if ($productName) {
            $query = 
            " SELECT DISTINCT elementSubID, elementSubDescription ".
            " FROM form_element_sub, form_element_link ".
            " WHERE linkElementSubID = elementSubID ".
            " AND LOWER(elementSubDescription)=LOWER('$productName') ".
            " AND linkDesignID = '41' ";
            //echo $query;
            if($result=mysqli_query($conn, $query)) {
                if (mysqli_num_rows($result) > 0) {
                    while ($row = mysqli_fetch_array($result)) {
                        echo '<p><strong>Record sudah ada.</strong></p>';
                        echo '
                        <form name="formSubmit" id="formSubmit" class="form" action="'.$thisPage.'adm1n/'.$curPage1.'" method="post">
                            <input type="hidden" name="elementSubID" id="elementSubID" value="'.$row["elementSubID"].'" />
                            <input type="submit" name="btnSubmit" id="btnSubmit" class="button" value="Click to Continue">
                        </form>';
                    }
                }                 
            } else {   
                function clean($string) {
                    $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
                    return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
                }	     
                $elementSubNames = $productName;
                $elementSubNames = clean($elementSubNames);
                $elementSubNames = explode(' ',$elementSubNames);
                $elementSubName  = '';
                foreach ($elementSubNames as $url) {
                    if ($elementSubName != "") {
                        $$elementSubName .= "-";
                    }	
                    $elementSubName .= strtolower($url);
                }        
                $sql1 = 
                " INSERT INTO form_element_sub ".
                " VALUES (null,'".$elementSubName."','single','".$productName."')";				
                //echo "<br>".$sql1;                
                if (mysqli_query($conn, $sql1)) {
                    $elementSubID = mysqli_insert_id($conn);
                    $sql2 = 
                    " INSERT INTO form_element_link ".
                    " VALUES (null,'41','".$elementSubID."')";				
                    if (mysqli_query($conn, $sql2)) {
                        echo '
                        <form name="formAddProduct" id="formAddProduct" class="form" action="'.$thisPage.'adm1n/'.$curPage1.'" method="post">
                            <input type="hidden" name="elementSubID" id="elementSubID" value="'.$elementSubID.'" />
                        </form>';
                        echo '
                        <script type="text/javascript">
                            document.getElementById(\'formAddProduct\').submit(); 
                        </script>';
                    }                    
                }
            }
        }            
    } else {
        $sql = 
        " select * from form_label ".
        " where labelType='$idEvent'";
        //echo "<br> sql".$sql; 
        if ($hasil = mysqli_query($conn, $sql)) {		
            ?>
            <form name="form" id="form" class="form" action="<?php echo $thisPage.'adm1n/'.$curPage1;?>" onsubmit="return validate(this)" method="post">
            <?php
            while ($rec = mysqli_fetch_array($hasil)) {
                echo "<h2 class='h2-teasers'>".$rec["labelNameTitle"]."</h2>";
                $sql2 = 
                " SELECT * FROM form_design, form_element ".
                " WHERE elementID = element AND labelParentID = ".$rec["labelID"].
                " ORDER BY designORDER asc";
                // echo $sql2;
                if ($hasil2 = mysqli_query($conn, $sql2)) {
                    echo '<table width="100%" cellpadding="10" cellspacing="1" border="0" bgcolor="#999999">';
                    while ($rec2 = mysqli_fetch_array($hasil2)) {
                        // Link ke Element
                        //echo $elementSubStat;
                        if ($rec2["labelName"] == "Product Name") {
                            if ($rec2["elementName"] != "hidden") {
                                //echo "$designName : ".$$designName;
                                echo '<tr valign="middle" bgcolor="#e4e4e4">';
                                echo '	<td width=25% style="background-color:#e4e4e4;">
                                            <strong>
                                                '.$rec2["labelName"];
                                                if ($rec2["labelNameEng"]) {
                                                    echo '
                                                    (<i>'.$rec2["labelNameEng"].'</i>)';
                                                }
                                            echo '	
                                            </strong>													
                                        </td>';
                                echo '	<td style="background-color:#e4e4e4;">';
                                            echo '<input style="width:96%; height:30px; padding: 8px; font-size:18px" ';
                                            echo 'type="'.$rec2["elementName"].'" 
                                                  name="'.$rec2["designName"].'" 
                                                  id="'.$rec2["designName"].'" 
                                                  value="'.(isset(${$rec2["designName"]}) ? ${$rec2["designName"]} : null).'">';
                                echo '	</td>';
                                echo '</tr>';
                            }
                        }
                    }
                    echo '</table>';
                }
            }
            ?>
            <table width="100%" cellpadding="15" cellspacing="1" border="0">
                <tr>
                    <td align="center">
                        <input type="hidden" name="action" id="action" value="Add Product" />
                        <input type="hidden" name="addProduct"  id="addProduct"  value="addProduct" />
                        <input type="submit" name="submit" id="submit" class="button" value="Add Data">
                    </td>
                </tr>
            </table>
            </form>
            <?php
        }
    }    
	?>