<!--<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>-->
<style type="text/css">
#cat-sort tr td{
    
    width: 100%;
    border-bottom: 2px solid #ddd !important;
    cursor: pointer;
}
#cat-sort{
	border-collapse: collapse;
}
</style>
<?php

if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this form. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
}
?>
<!-- 
<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>

<script type="text/javascript">
tinyMCE.init({
        mode : "exact",
        elements : "formheader, formfooter",
        theme : "advanced",
        theme_advanced_toolbar_location : "top",
        plugins : "emotions,insertdatetime, ibrowser, heading ",
		heading_clear_tag : "p",
        theme_advanced_buttons3_add : "emotions,insertdate,inserttime",
		theme_advanced_buttons1_add_before : "h1,h2,h3,h4,h5,h6,separator",
        plugin_insertdate_dateFormat : "%m/%d/%y",
        plugin_insertdate_timeFormat : "%I:%M:%S %p", 
        theme_advanced_buttons1 : "fontselect,fontsizeselect,bold,italic,underline,strikethrough,forecolor,backcolor,separator,link,unlink",
        theme_advanced_buttons2 : "bullist,numlist,separator,justifyleft, justifycenter,justifyright,justifyfull,separator,outdent,indent,separator,undo,redo,separator,hr,removeformat,visualaid,separator,sub,sup,separator,charmap",
        theme_advanced_buttons3 : "cut,copy,paste,image,code,insertdate,inserttime,ibrowser"
});
</script>

-->

<script type="text/javascript">
	function updateTextareaNumRows(inObj) {
		var id = inObj.data('id');
		var newVal = inObj.val();

		$.ajax({
			type: 'POST',
			url: 'update_textarea_num_rows.php',
			data: {id: id, value: newVal}
		}).done(function(response) {
			if (response != '1') {
				alert('Error updating number of rows.');
			}
		});
	}

	$(document).ready(function() {
		$('.textarea-num-rows').change(function() {
			updateTextareaNumRows($(this));
		});
	});
</script>

<?php
if (!empty($_GET["move"])){
	include('order_formfields.php');
}
$action          = $_GET["a"];
$addfield          = $_GET["add"];
$editform          = mysql_real_escape_string($_POST["form"]);

if(empty($editform)){		//if form key passed through URL, use that key to select form
	$editform          = $_GET["key"];
}

if(empty($editform)){	//no form still selected for editing, select  most recently added form
	$query= "Select max(recordKey) as recordKey from formData where formID =0";	//pick last form record entered
	$rt=mysql_query($query);          // query executed
	while($nt=mysql_fetch_array($rt)){
		$editform=$nt[recordKey];
	}
	mysql_close;
}


 include('../domain.php');
 
//update any required or not required fields selected for change
if(!empty($_GET["required"])){
	$update = "update formData set required = 1 where recordKey='" . $_GET["required"]. "'";
	mysql_query($update);
}

if(!empty($_GET["notrequired"])){
	$update = "update formData set required = 0 where recordKey='" . $_GET["notrequired"]. "'";
	mysql_query($update);
}


if(!empty($_GET["remove"])){
	$removefield=$_GET["remove"];
}

$formtype = $_POST["formtype"];

//echo 'newoption=' . $_POST['newoption'];
//echo 'addoption='.$addoption;

if($_GET['addoption']==='1'){
	if(!empty($_POST['newoption'])){
		$qry = "Select max(orderNum) as ordernum from formData where parentID = '".$_GET['edit']."'";
		$rtq=mysql_query($qry);          // query executed
		while($ntq=mysql_fetch_array($rtq)){
			$nextnum = $ntq[ordernum] + 1;
		}
		mysql_close;
		$addoption = "Insert into formData (formID, caption, parentID, orderNum) values ('".$editform."', '".$_POST['newoption']."', '".$_GET['edit']."', '".$nextnum."')";
		//echo 'addoption='.$addoption;
		mysql_query($addoption);
	}
}

if($_GET['addp_text']==='1'){
	if(!empty($_POST['newptext'])){
		$qry = "Select max(orderNum) as ordernum from formData where parentID = '".$_GET['edit']."'";
		$rtq=mysql_query($qry);          // query executed
		while($ntq=mysql_fetch_array($rtq)){
			$nextnum = $ntq[ordernum] + 1;
		}
		mysql_close;
		$addp_text = "Insert into formData (formID, caption, parentID, orderNum) values ('".$editform."', '".mysql_real_escape_string($_POST['newptext'])."', '".$_GET['edit']."', '".$nextnum."')";
		//echo 'addp_text='.$addp_text;
		mysql_query($addp_text);
	}
}

if($_POST['save_p_text'] && isset($_POST['p_text'])){
	$update = "update formData set caption='".mysql_real_escape_string($_POST['p_text'])."' where recordKey ='".$_POST['recordkey']."'";
	//echo  $update;
	mysql_query($update);
			
}		
	
if($_GET['addh3_text']==='1'){
	if(!empty($_POST['newh3text'])){
		$qry = "Select max(orderNum) as ordernum from formData where parentID = '".$_GET['edit']."'";
		$rtq=mysql_query($qry);          // query executed
		while($ntq=mysql_fetch_array($rtq)){
			$nextnum = $ntq[ordernum] + 1;
		}
		mysql_close;
		$addh3_text = "Insert into formData (formID, caption, parentID, orderNum) values ('".$editform."', '".mysql_real_escape_string($_POST['newh3text'])."', '".$_GET['edit']."', '".$nextnum."')";
		//echo 'addh3_text='.$addh3_text;
		mysql_query($addh3_text);
	}
}

if($_POST['save_h3_text'] && isset($_POST['h3_text'])){
	$update = "update formData set caption='".mysql_real_escape_string($_POST['h3_text'])."' where recordKey ='".$_POST['recordkey']."'";
	//echo  $update;
	mysql_query($update);
			
}	

if (!empty($_POST["chkDelete"])) {
     $query = "delete from formData WHERE recordKey = '" . $_GET['key'] ."'";
     mysql_query($query);
     echo "<font color=red>form deleted successfully.</font>";
     unset($action);
}
	//echo 'Action = ' . $action; (for testing)
	
if (!empty($removefield)) {
	$query = "Delete from formData where recordKey='" . $removefield . "' or parentID ='" . $removefield . "'";
	//echo $query;
	mysql_query($query);
}
if ($action == "add") {
         
         $query = "Insert into formData (formName, success_msg) values('" . mysql_real_escape_string($_POST["newform"]) . "', 'Thank you for your inquiry. We will respond to you within 24-48 hours.')";
         mysql_query($query);
         echo "<font color=red>form added successfully.</font>";
		 //select  most recently added form to display
		$query= "Select max(recordKey) as recordKey from formData where formID =0";	//pick last form record entered
		$rt=mysql_query($query);          // query executed
		while($nt=mysql_fetch_array($rt)){
			$editform=$nt[recordKey];
		}
		mysql_close;

}


                      //******************make any updates
if ($action == "update") {
		$query = "UPDATE formData SET formName = '" . mysql_real_escape_string($_POST['formname']) . "'";
		$query .= ", success_msg = '" . mysql_real_escape_string($_POST['successmsg']) . "'";
	//echo $query;
    if(!empty($_POST['chkActive'])){
      $query .= ", active ='1'";
      }else{
        //if($_POST['formname']!=="HOME"){  //can only turn to inactive if not the main home form
            $query .= ", active ='0'";
      //}
    }
		if(!empty($_POST['chkCaptcha'])){
			$query .= ", captcha ='1'";
		}else{       
            $query .= ", captcha ='0'";
		}
		
			if(!empty($_POST['toemail'])){
				$query .= ", to_email ='".mysql_real_escape_string($_POST['toemail']) ."'";
			}else{
				$query .= ", to_email =''";
			}
	
			if(!empty($_POST['fromemail'])){
				$query .= ", from_email ='".mysql_real_escape_string($_POST['fromemail']) ."'";
			}else{
				$query .= ", from_email =''";
			}
		
			if(!empty($_POST['formheader'])){
				$query .= ", header ='".mysql_real_escape_string($_POST['formheader']) ."'";
			}else{
				$query .= ", header =''";
			}
			
			if(!empty($_POST['formfooter'])){
				$query .= ", footer ='".mysql_real_escape_string($_POST['formfooter']) ."'";
			}else{
				$query .= ", footer =''";
			}
			
		$query = $query . " WHERE recordKey = '" . $_GET['key'] ."'" ;
		//echo $query;
		mysql_query($query) or die(mysql_error());   //execute query

		//confirmation message that update worked...
		echo "<font color=red>form content updated successfully.</font>";

	}	 //*******************end of update query

	//ADD FIELDS TO FORM
	if ($addfield == "basic") {
         $query = "Delete  from formData where formID = '" . $editform . "'";
		 mysql_query($query);
         $query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', 'text', 'Name', '1')";
         mysql_query($query);
		 $query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', 'text', 'Email', '2')";
         mysql_query($query);
		 $query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', 'text', 'Phone', '3')";
         mysql_query($query);
         //echo "<font color=red>form added successfully.</font>";
	}
	if ($addfield == "jobapp"){
		//first remove any existing fields from form	
		$query = "Delete  from formData where formID = '" . $editform . "'";
		mysql_query($query);	
		include("jobapp_form.php");
	}
	if ($addfield == "name") {
		//check if exists first
		$query = "select * from formData where formID = '" . $editform . "' and caption='Name'";
		$result=mysql_query($query);
		if(mysql_num_rows($result)===0){	//record not found, ok to create
			//get highest orderNum for fields on this form
			$query = "select max(orderNum) as highnum from formData where formID = '" . $editform . "'";
			$rst = mysql_query($query);
			 while($rnt=mysql_fetch_array($rst)){
				$highnum =  $rnt[highnum];
			 }
			mysql_close;
			//now insert new control
			$query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', 'text', 'Name', '".($highnum +1)."')";
			mysql_query($query);
		}
		 mysql_close;
	}
if ($addfield == "email") {
		//check if exists first
		$query = "select * from formData where formID = '" . $editform . "' and caption='Email'";
		$result=mysql_query($query);
		if(mysql_num_rows($result)===0){	//record not found, ok to create
			//get highest orderNum for fields on this form
			$query = "select max(orderNum) as highnum from formData where formID = '" . $editform . "'";
			$rst = mysql_query($query);
			 while($rnt=mysql_fetch_array($rst)){
				$highnum =  $rnt[highnum];
			 }
			mysql_close;
			//now insert new control
			$query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', 'text', 'Email', '".($highnum +1)."')";
			mysql_query($query);
		}
		 mysql_close;
	}	
if ($addfield == "phone") {
		//check if exists first
		$query = "select * from formData where formID = '" . $editform . "' and caption='Phone'";
		$result=mysql_query($query);
		if(mysql_num_rows($result)===0){	//record not found, ok to create
			//get highest orderNum for fields on this form
			$query = "select max(orderNum) as highnum from formData where formID = '" . $editform . "'";
			$rst = mysql_query($query);
			 while($rnt=mysql_fetch_array($rst)){
				$highnum =  $rnt[highnum];
			 }
			mysql_close;
			//now insert new control
			$query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', 'text', 'Phone', '".($highnum +1)."')";
			mysql_query($query);
		}
		 mysql_close;
	}	
if ($addfield == "address") {
		//check if exists first
		$query = "select * from formData where formID = '" . $editform . "' and caption='Address'";
		$result=mysql_query($query);
		if(mysql_num_rows($result)===0){	//record not found, ok to create
			//get highest orderNum for fields on this form
			$query = "select max(orderNum) as highnum from formData where formID = '" . $editform . "'";
			$rst = mysql_query($query);
			 while($rnt=mysql_fetch_array($rst)){
				$highnum =  $rnt[highnum];
			 }
			mysql_close;
			//now insert new control
			$query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', 'text', 'Address', '".($highnum +1)."')";
			mysql_query($query);
		}
		 mysql_close;
	}	
if ($addfield == "citystatezip") {
		//check if exists first
		$query = "select * from formData where formID = '" . $editform . "' and caption='City'";
		$result=mysql_query($query);
		if(mysql_num_rows($result)===0){	//record not found, ok to create
			//get highest orderNum for fields on this form
			$query = "select max(orderNum) as highnum from formData where formID = '" . $editform . "'";
			$rst = mysql_query($query);
			 while($rnt=mysql_fetch_array($rst)){
				$highnum =  $rnt[highnum];
			 }
			mysql_close;
			//now insert new control
			$query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', 'text', 'City', '".($highnum +1)."')";
			mysql_query($query);
		}
		 mysql_close;
		 		//check if exists first
		$query = "select * from formData where formID = '" . $editform . "' and caption='State'";
		$result=mysql_query($query);
		if(mysql_num_rows($result)===0){	//record not found, ok to create
			//get highest orderNum for fields on this form
			$query = "select max(orderNum) as highnum from formData where formID = '" . $editform . "'";
			$rst = mysql_query($query);
			 while($rnt=mysql_fetch_array($rst)){
				$highnum =  $rnt[highnum];
			 }
			mysql_close;
			//now insert new control
			$query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', 'text', 'State', '".($highnum +1)."')";
			mysql_query($query);
		}
		 mysql_close;
		 		//check if exists first
		$query = "select * from formData where formID = '" . $editform . "' and caption='Zip'";
		$result=mysql_query($query);
		if(mysql_num_rows($result)===0){	//record not found, ok to create
			//get highest orderNum for fields on this form
			$query = "select max(orderNum) as highnum from formData where formID = '" . $editform . "'";
			$rst = mysql_query($query);
			 while($rnt=mysql_fetch_array($rst)){
				$highnum =  $rnt[highnum];
			 }
			mysql_close;
			//now insert new control
			$query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', 'text', 'Zip', '".($highnum +1)."')";
			mysql_query($query);
		}
		 mysql_close;
	}	
if ($addfield == "comments") {
		//check if exists first
		$query = "select * from formData where formID = '" . $editform . "' and caption='Comments'";
		$result=mysql_query($query);
		if(mysql_num_rows($result)===0){	//record not found, ok to create
			//get highest orderNum for fields on this form
			$query = "select max(orderNum) as highnum from formData where formID = '" . $editform . "'";
			$rst = mysql_query($query);
			 while($rnt=mysql_fetch_array($rst)){
				$highnum =  $rnt[highnum];
			 }
			mysql_close;
			//now insert new control
			$query = "Insert into formData (formID, controlType, caption, orderNum, text_rows) values('" . $editform . "', 'textarea', 'Comments', '".($highnum +1)."', '3')";
			mysql_query($query);
		}
		 mysql_close;
	}		
if ($addfield == "custom") {
	$fieldname=mysql_real_escape_string($_POST[fieldname]);
	$fieldtype=$_POST[controltype];
	//echo "fieldname = " . $fieldname . " and fieldtype = " . $fieldtype . "<br>";
	If((empty($fieldname))||(empty($fieldtype))){
		$custom_err=1;
		
	} else {
				//check if exists first
		$query = "select * from formData where formID = '" . $editform . "' and caption='".$fieldname."'";
		$result=mysql_query($query);
		if(mysql_num_rows($result)===0){	//record not found, ok to create
			//get highest orderNum for fields on this form
			$query = "select max(orderNum) as highnum from formData where formID = '" . $editform . "'";
			$rst = mysql_query($query);
			 while($rnt=mysql_fetch_array($rst)){
				$highnum =  $rnt[highnum];
			 }
			mysql_close;
			//now insert new control
			$query = "Insert into formData (formID, controlType, caption, orderNum) values('" . $editform . "', '".$fieldtype."', '".$fieldname."', '".($highnum +1)."')";
			mysql_query($query);
		}
		 mysql_close;
	}
}
// echo '<br><div align="center"><a href="index.php"><font color="blue">Back to Main Admin Page</font></a></div>';

//add new form
echo '<div id="add-new-wrapper_gallery"><form action="' . $_SERVER['PHP_SELF'] . '?a=add&admin=forms" method="post">Add new form:<br /><input type="text" name="newform" style="width:240px;">

		<input type="submit" name="save" id="save-button" value="Add form">
		</form></div>';
		
 //create selector to choose form to edit
 echo '<form action="' . $_SERVER['PHP_SELF'] . '?admin=forms" method="post">';
 echo'<div>Choose form to edit:<br /><select style="width:213px;" name="form">';
 $pages="Select recordKey, formName from formData where formID=0";
 $pg=mysql_query($pages);
  while($pnt=mysql_fetch_array($pg)){
   echo '<option value='. $pnt[recordKey];
   if($pnt[recordKey]==$editform){echo ' selected="selected"';}
   echo'>form ID#'.$pnt[recordKey];
   if (!empty($pnt[formName])){echo " - " .$pnt[formName];} else { echo " - Untitled";} 
   echo '</option>';
}

mysql_close;
echo '</select> <input type="submit" name="select" id="save-button" value="Edit"></form>&nbsp; &nbsp; &nbsp; - OR -</div><br>
	<center><hr />
	<br />
	<u><strong>General Form Settings</strong></u></center>';
//end of selector

if(!empty($editform)){	//determine which record to display
	$query = "SELECT * from formData where recordKey='" .$editform . "'";
	$rt=mysql_query($query);          // query executed 
	//echo mysql_error();                    // if any error is there that will be printed to the screen
	//echo "$editform not empty...". $editform;
} else{	//if $editform variable is empty...
	//echo "$editform is empty...". $editform;
	/*$query= "Select max(recordKey) as recordKey from formData where formID =0";	//pick last form record entered
	$rt=mysql_query($query);          // query executed
	while($nt=mysql_fetch_array($rt)){
		$editform=$nt[recordKey];
	}
	mysql_close;
	*/
		//echo "<br>formadata qry=".$query;
		//echo "<br>$editform now populated...". $editform;
	$query = "SELECT * from formData where recordKey='".$editform . "'";
	$rt=mysql_query($query);          // query executed 
}		
		
echo '<div align="center"><table border=0 bordercolor="red" cellpadding=0 cellspacing=4>';
while($nt=mysql_fetch_array($rt)){

	echo '<tr><td><input type="hidden" name="recordKey" value="' . $nt[recordKey] . '"><form action="' . $_SERVER['PHP_SELF'] . '?a=update&admin=forms&key=' . $nt[recordKey] . '" method="post">';
	echo '<tr><td valign="top"><div align=left>Form header: </div><td valign="top"><div align=left><textarea name="formheader" cols=60>' .  $nt[header] . '</textarea></div>';

	echo '<tr><td><div align=left>Form Name: </div><td valign="top"><div align=left><input type="text" name="formname" value="' .  $nt[formName] . '"></div>';
    echo '<tr><td valign="top"><div align=left>Success message: </div><td valign="top"><div align=left><textarea name="successmsg" cols=60>' .  $nt[success_msg] . '</textarea></div>';
	if(!empty($nt[to_email])){	//if email blank, fill in default email from site prefs
		echo '<tr><td><div align=left>Send inquiry to: </div><td valign="top"><div align=left><input type="text" name="toemail" value="' .  $nt[to_email] . '"size=58></div>';
	} else{
		echo '<tr><td><div align=left>Send inquiry to: </div><td valign="top"><div align=left><input type="text" name="toemail" value="';
		include('../email.php');
		echo '"size=30></div>';
	}
	echo '<tr><td valign="top"><div align=left>Send from: </div><td valign="top"><div align=left><input type="text" name="fromemail" value="' .  $nt[from_email] . '" size=58>
	<br><i><font color="red">(an email address)</font></i></div>';
echo '<tr><td valign="top"><div align=left>Form footer: </div><td valign="top"><div align=left><textarea name="formfooter" cols=60>' .  $nt[footer] . '</textarea></div>';


		echo '<tr><td valign="top"><div align=left>Use Captcha: </div><td valign="top"><div align=left><input type="checkbox" name="chkCaptcha"';
    if(!empty($nt[captcha])) {
      echo ' checked=checked';
    }
    echo '"> (this helps protect you from getting too much spam.)</div>';
	
	echo '<tr><td valign="top"><div align=left>Active: </div><td valign="top"><div align=left><input type="checkbox" name="chkActive"';
    if(!empty($nt[active])) {
      echo ' checked=checked';
    }
    echo '"></div>';

  //add save button
	echo '<tr><td><td valign=top><div align=left><input type="submit" name="save" id="save-button" value="Save"></div>';

	//show delete checkbox
    echo '<tr><td valign="top"><div align=left>Delete: </div><td valign="top"><div align=left><input type="checkbox" name="chkDelete"><font color=red> <i><br>Be sure you mean to delete this form if you check this box<br> before saving. You will not be prompted again.</i></font></div>';
  
  
  echo '</form>';
echo '</table><!-- END FIRST TABLE WITH GENERAL FORM SETTINGS-->
<a name="controls"></a><div id="form-field-controls-wrapper"><table id="field-control-wrapper" width="100%" border=0 bordercolor="red" cellpadding=0 cellspacing=4><!-- START NEW TABLE FOR CONTROLS -->';
  echo '<tr><td colspan=2 style="padding-top:9px;padding-bottom:7px;"><center><strong>MANAGE FIELD INPUT CONTROLS:</strong></center>';
  //add links to add new controls
  
  //show existing controls on this form
  echo'<tr><td width="75%" valign="top"><table id="form-controls" border=0 bordercolor="black" cellspacing=0 cellpadding=0 width=100%>
  <tr><td style="padding-top:9px;padding-bottom:7px;">';
  echo'<div align="left"><u><center>Input Controls on this form:</center></u></div>';
  echo '<center><table cellspacing=3 cellpadding=0>';
  echo "<tr><td><strong>Name &nbsp;</strong><td><strong>Style </strong><td width=30><strong>Del. </strong><td width=30><strong>Opts. </strong><td><strong>Req.&nbsp;</strong><td><strong>Rows </strong>";
  echo '<tbody  id="cat-sort">';
  //get list of controls
  //$query2="Select * from formData where formID ='" .$nt[recordKey]. "' AND controlType>'' order by orderNum"; 
  $query2="Select * from formData where formID ='$editform' AND controlType>'' order by orderNum";
  //echo $query2."<br>";
  $rt2=mysql_query($query2);          // query executed
  $x=0; //datepicker counter to create dynamic js
	while($nt2=mysql_fetch_array($rt2)){
		if($nt2[controlType]==="text"){

		echo "<tr data-id=".$nt2[recordKey]."
		class='drag'><td><div align='left'><img class='handle' src='http://www.pilotwebs.com/images/icon-drag.png' style='margin-right:10px;margin-top:10px'></a>".$nt2[caption].": <td><div align='left'><input type='text' name='".$nt2[caption]."' size='8'>";
		echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&remove=" . $nt2[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a><td>";
		if($nt2[required]==='1'){
			echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&notrequired=" . $nt2[recordKey] . "&#controls'><font color='red'>Yes</font></a>";
		} else {
			echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&required=" . $nt2[recordKey] . "&#controls'><font color='black'>No</font></a>";
		}
		
		}
		if($nt2[controlType]==="datepicker"){
			$x++;
			echo "<tr data-id=".$nt2[recordKey]." class='drag'><td><div align='left'><img class='handle' src='http://www.pilotwebs.com/images/icon-drag.png' style='margin-right:10px;margin-top:10px'>".$nt2[caption].": <td><div align='left'><input type='text' name='".$nt2[caption]."' id='datepicker".$x."' size='8'>
			
			<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&remove=" . $nt2[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a><td>";
		if($nt2[required]==='1'){
			echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&notrequired=" . $nt2[recordKey] . "&#controls'><font color='red'>Yes</font></a>";
		} else {
			echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&required=" . $nt2[recordKey] . "&#controls'><font color='black'>No</font></a>";
		}	
		}
		if($nt2[controlType]==="textarea"){
			echo "<tr data-id=".$nt2[recordKey]." class='drag'><td><div align='left'><img class='handle' src='http://www.pilotwebs.com/images/icon-drag.png' style='margin-right:10px;margin-top:10px'>".$nt2[caption] . ": <td><div align='left'><textarea name='" . $nt2[caption] ."'></textarea>
			
			<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&remove=" . $nt2[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a><td>";
			if($nt2[required]==='1'){
				echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&notrequired=" . $nt2[recordKey] . "&#controls'><font color='red'>Yes</font></a>";
			} else {
				echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&required=" . $nt2[recordKey] . "&#controls'><font color='black'>No</font></a>";
			}
			echo "<td><select class='textarea-num-rows' name='num_rows[".$nt2['recordKey']."]' data-id='".$nt2['recordKey']."'>";
			$rowct=0;
			while ($rowct<15){
				echo "<option value='$rowct'";
				if($rowct==$nt2[text_rows]){
					echo " selected='selected'";
				}
				echo ">$rowct</option>";	
				$rowct++;				
			}
			echo "</select>";
		}
		if($nt2[controlType]==="p"){
			echo "<tr data-id=".$nt2[recordKey]." class='drag'><td colspan='2'><div align='left'><img class='handle' src='http://www.pilotwebs.com/images/icon-drag.png' style='margin-right:10px;margin-top:10px'>".$nt2[caption] . " (text copy)<div align='left'>
			
			<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&remove=" . $nt2[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a><td>";
			echo " - <a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&edit=" . $nt2[recordKey] . "&#controls' title='Edit choices'><font style='font-size:.9em;' color='blue'>Edit</font></a>";
			if($_GET['edit']===$nt2[recordKey]){	//This record set for editing - show text for this field or allow to add if doesn't exist yet

				$textdata = "Select * from formData where parentID = " . $nt2[recordKey] . " order by orderNum";
				$rtc=mysql_query($textdata); 
				if(mysql_num_rows($rtc)>0){				
					//show edit option if field already exists
					while($ntc=mysql_fetch_array($rtc)){
						echo "<tr><td valign='top' colspan=2><div align='left'>
						<form action='".$_SERVER['PHP_SELF']."?admin=forms&addp_text=1&key=" . $nt[recordKey] . "&edit=" . $nt2[recordKey] . "&#controls' method='post'>						
						<textarea name='p_text' rows='3' cols='40'>". $ntc[caption] . "</textarea><input type='hidden' name='recordkey' value='".$ntc[recordKey]."'><input type='submit' name = 'save_p_text' value='Save'></form></div>
						<!--<td valign='top'><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&remove=" . $ntc[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a>-->";
					}
					mysql_close;
				} else {
					//if it doesn't exist, allow it to be added here
					echo "<tr><td valign='top' colspan=2><form action='".$_SERVER['PHP_SELF']."?admin=forms&addp_text=1&key=" . $nt[recordKey] . "&edit=" . $nt2[recordKey] . "&#controls' method='post'><textarea name='newptext'  rows='3' cols='40'></textarea><input type='submit' value='Add Text'></form></div>";
				}
			
			}
		}
		if($nt2[controlType]==="h3"){
			echo "<tr data-id=".$nt2[recordKey]." class='drag'><td colspan='2'><div align='left'><img class='handle' src='http://www.pilotwebs.com/images/icon-drag.png' style='margin-right:10px;margin-top:10px'>".$nt2[caption] . " (title copy)<div align='left'>
			<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&remove=" . $nt2[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a><td>";
			echo " - <a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&edit=" . $nt2[recordKey] . "&#controls' title='Edit choices'><font style='font-size:.9em;' color='blue'>Edit</font></a>";
			if($_GET['edit']===$nt2[recordKey]){	//This record set for editing - show text for this field or allow to add if doesn't exist yet

				$textdata = "Select * from formData where parentID = " . $nt2[recordKey] . " order by orderNum";
				$rtc=mysql_query($textdata); 
				if(mysql_num_rows($rtc)>0){				
					//show edit option if field already exists
					while($ntc=mysql_fetch_array($rtc)){
						echo "<tr><td valign='top' colspan=2><div align='left'>
						<form action='".$_SERVER['PHP_SELF']."?admin=forms&addp_text=1&key=" . $nt[recordKey] . "&edit=" . $nt2[recordKey] . "&#controls' method='post'>						
						<textarea name='h3_text' rows='3' cols='40'>". $ntc[caption] . "</textarea><input type='hidden' name='recordkey' value='".$ntc[recordKey]."'><input type='submit' name = 'save_h3_text' value='Save'></form></div>
						<!--<td valign='top'><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&remove=" . $ntc[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a>-->";
					}
					mysql_close;
				} else {
					//if it doesn't exist, allow it to be added here
					echo "<tr><td valign='top' colspan=2><form action='".$_SERVER['PHP_SELF']."?admin=forms&addh3_text=1&key=" . $nt[recordKey] . "&edit=" . $nt2[recordKey] . "&#controls' method='post'><textarea name='newh3text' rows='3' cols='40'></textarea><input type='submit' value='Add Text'></form></div>";
				}
			
			}	
		}
		if($nt2[controlType]==="radio"){
			echo "<tr data-id=".$nt2[recordKey]."class='drag'><td><div align='left'><img class='handle' src='http://www.pilotwebs.com/images/icon-drag.png' style='margin-right:10px;margin-top:10px'>".$nt2[caption] . ": <td><div align='left'><!--<input type='radio' name='" . $nt2[caption] ."'>-->
			
			<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&remove=" . $nt2[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a>
				<td> - <a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&edit=" . $nt2[recordKey] . "&#controls' title='Edit choices'><font style='font-size:.9em;' color='blue'>Edit</font></a>";
			if($nt2[required]==='1'){
				echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&notrequired=" . $nt2[recordKey] . "&#controls'><font color='red'>Yes</font></a>";
			} else {
				echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&required=" . $nt2[recordKey] . "&#controls'><font color='black'>No</font></a>";
			}
			if($_GET['edit']===$nt2[recordKey]){	//This record set for editing - show any choices and option to add new
				//get any existing options for this dropdown
				$choices = "Select * from formData where parentID = " . $nt2[recordKey] . " order by orderNum";
				$rtc=mysql_query($choices);          // query executed
				while($ntc=mysql_fetch_array($rtc)){
					echo "<tr><td valign='top' colspan=2><div align='left'>" . $ntc[caption] . "<input type='radio' name='" . $nt2[caption] . "' value='" . $ntc[caption] . "'></div>
					
					<td valign='top'><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&remove=" . $ntc[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a>";
				}
				mysql_close;
				echo "<tr><td valign='top' colspan=2><form action='".$_SERVER['PHP_SELF']."?admin=forms&addoption=1&key=" . $nt[recordKey] . "&edit=" . $nt2[recordKey] . "&#controls' method='post'><input type='text' name='newoption'><input type='submit' value='Add option'></form></div>";
			
			
			}
		}
		if($nt2[controlType]==="checkbox"){
			echo "<tr data-id=".$nt2[recordKey]." class='drag'><td><div align='left'><img class='handle' src='http://www.pilotwebs.com/images/icon-drag.png' style='margin-right:10px;margin-top:10px'>".$nt2[caption] . ": <td><div align='left'><input type='checkbox' name='" . $nt2[caption] ."'>
			
			<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&remove=" . $nt2[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a><td>";
		if($nt2[required]==='1'){
			echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&notrequired=" . $nt2[recordKey] . "&#controls'><font color='red'>Yes</font></a>";
		} else {
			echo "<td><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&required=" . $nt2[recordKey] . "&#controls'><font color='black'>No</font></a>";
		}
				
		}
		if($nt2[controlType]==="dropdown"){
			echo "<tr data-id=".$nt2[recordKey]." class='drag'><td><div align='left'><img class='handle' src='http://www.pilotwebs.com/images/icon-drag.png' style='margin-right:10px;margin-top:10px'>".$nt2[caption] . ": <td><div align='left'><select style='width:150px;' name='" . $nt2[caption] ."'>";
			//get any options for this dropdown...add to list
			$choices = "Select * from formData where parentID = " . $nt2[recordKey] . " order by orderNum";
			$rtc=mysql_query($choices);          // query executed
			while($ntc=mysql_fetch_array($rtc)){
				echo "<option value='" . $ntc[caption] . "'>" . $ntc[caption] . "</option>";
			}
			mysql_close;
			echo "</select>";
			echo "<td valign='top'><a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&remove=" . $nt2[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a>
			<td valign='top'> - <a href='".$_SERVER['PHP_SELF']."?admin=forms&key=" . $nt[recordKey] . "&edit=" . $nt2[recordKey] . "&#controls' title='Edit choices'><font style='font-size:.9em;' color='blue'>Edit</font></a>";
			if($nt2[required]==='1'){
				echo "<td valign='top'><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&notrequired=" . $nt2[recordKey] . "&#controls'><font color='red'>Yes</font></a>";
			} else {
				echo "<td valign='top'><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&required=" . $nt2[recordKey] . "&#controls'><font color='black'>No</font></a>";
			}
			if($_GET['edit']===$nt2[recordKey]){	//This record set for editing - show any choices and option to add new
				//get any existing options for this dropdown
				$choices = "Select * from formData where parentID = " . $nt2[recordKey] . " order by orderNum";
				$rtc=mysql_query($choices);          // query executed
				while($ntc=mysql_fetch_array($rtc)){
					echo "<tr><td></td><td valign='top'><div align='left'><input type='text' value='" . $ntc[caption] . "'></div>
					
					<td valign='top'><a href='".$_SERVER['PHP_SELF']."?admin=forms&edit=" . $nt2[recordKey] . "&key=" . $nt[recordKey] . "&remove=" . $ntc[recordKey] . "&#controls' title='Remove'><font color='red'>(X)</font></a>";
				}
				mysql_close;
				echo "<tr><td valign='top' colspan=2><form action='".$_SERVER['PHP_SELF']."?admin=forms&addoption=1&key=" . $nt[recordKey] . "&edit=" . $nt2[recordKey] . "&#controls' method='post'><input type='text' name='newoption'> <input id='save-button' type='submit' value='Add to dropdown'></form></div>";
			
			
			}
			
		}
		
	}	//end of controls for this form
	mysql_close;
	echo'</div></tbody></table></center><!--END ALIGNMENT TABLE-->';
  echo'</table>';
  
  //show input control options
 echo '<td width="25%" valign="top" style="padding-left:8px;"><table border=0 bordercolor="black" cellspacing=0 width=100%>
	<td><u>Create Quick form:</u>
	<br><a href="'.$_SERVER['PHP_SELF'].'?admin=forms&key=' . $editform . '&add=basic&#controls"><font color="blue">Basic contact form</font></a><br>
	<a href="'.$_SERVER['PHP_SELF'].'?admin=forms&key=' . $editform . '&add=jobapp&#controls"><font color="blue">Job Application</font></a><br>
	(removes existing fields)
	<br><br>
	<hr><!-- BREAK BETWEEN SECTIONS -->
	<u>Add common field:</u>
	<br><a href="'.$_SERVER['PHP_SELF'].'?admin=forms&key=' . $editform . '&add=name&#controls"><font color="blue">Name</font></a><br>
	<a href="'.$_SERVER['PHP_SELF'].'?admin=forms&key=' . $editform . '&add=email&#controls"><font color="blue">Email</font></a><br>
	<a href="'.$_SERVER['PHP_SELF'].'?admin=forms&key=' . $editform . '&add=phone&#controls"><font color="blue">Phone</font></a><br>
	<a href="'.$_SERVER['PHP_SELF'].'?admin=forms&key=' . $editform . '&add=address&#controls"><font color="blue">Address</font></a><br>
	<a href="'.$_SERVER['PHP_SELF'].'?admin=forms&key=' . $editform . '&add=citystatezip&#controls"><font color="blue">City/State/Zip</font></a><br>
	<a href="'.$_SERVER['PHP_SELF'].'?admin=forms&key=' . $editform . '&add=comments&#controls"><font color="blue">Comments</font></a><br>
	<br>
	<hr><!-- BREAK BETWEEN SECTIONS -->
	<u>Add custom field:</u>
	<br>';
	if($custom_err>""){
		echo'<font color="red">Enter a name and choose the type of field.</font>';
	}
	echo'<form action="'.$_SERVER['PHP_SELF'].'?admin=forms&key=' . $editform . '&add=custom&#controls" method="post">
	Enter a name for the field:<br>
	<input type="text" name="fieldname" maxlength="30"><br>
	Select the type of input control:
	<select name="controltype">
	<option value="text">Standard text box</option>
	<option value="dropdown">Dropdown selector</option>
	<option value="textarea">Large text box</option>
	<option value="checkbox">Checkbox</option>
	<option value="radio">Radio selector</option>
	<option value="datepicker">Date Selector</option>
	<option value="p">Text copy (non-input)</option>
	<option value="h3">Title copy (non-input)</option>
	</select>
	<input id="save-button" type="submit" name="custom" value="Add Control"></form>
	
 </table>';
echo '';
//}
}
echo '</table></div><br>';

	echo '</div>';
	
$j=0;
while ($x>=$j){
?>

<script>
  $(document).ready(function() {
    $("#datepicker<?php echo $j;?>").datepicker();
    
   });
  	
</script>
<?php	
$j++;
}
?>
<script src="js/sortable.js"></script>