<?php
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
	}

$move 			= $_GET["move"];
$fieldkey 			= $_GET["fieldkey"];
$formid = $_GET["key"];
$parentfield = $_GET["parent"];
//if(empty($pageid)){
//	$pageid=1;
//}
	

//perform and moving/renumbering queries first
if(!empty($move)){  //page being moved up or down
//echo "move is selected<br>";
        unset($previouskey);
        unset($nextkey);
		if(!empty($parentfield)){
			$query = "SELECT orderNum from formData WHERE recordKey ='". $fieldkey."' AND parentID ='". $parentfield."'";  //get current ordernumber for field being moved
		} else {
			$query = "SELECT orderNum from formData WHERE recordKey ='". $fieldkey."'";  //get current ordernumber for field being moved
		}
          //echo "<br><font color='red'>qry=" . $query . "</font>";
		  $rt=mysql_query($query);
          while($nt=mysql_fetch_array($rt)){
          $ordernumber = $nt[orderNum];    //this is current order# for this field on this page
		  //$fieldkey = $nt[fieldkey];
			//echo "<br><font color='red'>recordkey=". $fieldkey."<br>current ordernumber = " . $ordernumber . "</font>";
        }
        mysql_close;
			
			//now step through remaining fields and get records before and after the current field
			if(!empty($parentfield)){
				$query = "SELECT * from formData WHERE formID ='". $formid ."' and parentID = '".$parentfield."' and orderNum>0 order by orderNum";
			} else {
				$query = "SELECT * from formData WHERE formID ='". $formid ."' and orderNum>0 order by orderNum";  
			}
			//echo "<br><font color='red'>qry=" . $query . "</font>";
          $rt=mysql_query($query);
			while($nt=mysql_fetch_array($rt)){
			//echo 'itemkey: ' .$nt[itemKey] . ' - ord#: ' . $nt[$ordercolumn]. '<br>';
            if($nt[orderNum]<$ordernumber){ //if current record is lower than record selected to be moved, save itemkey as previous record
                 $previouskey = $nt[recordKey];  //category key just before the one being moved
                 $previousnum = $nt[orderNum];   //ordernumber of the item before the one being moved
					//echo 'checkpoint 1<br>';
					
            }
            if($nt[orderNum]>$ordernumber){    //if current record is higher than record selected to be moved, save itemkey as next higher record
                  $nextkey = $nt[recordKey];   //category key just after the one being moved
                  $nextnum = $nt[orderNum];   //ordernumber of the item after the one being moved
                  //echo 'checkpoint 2<br>';
                  break;  //go to end of loop, stop looking thru records
            }
					//echo '<br><font color="red">previouskey ' . $nt[recordKey] . '</font>';
					//echo '<br><font color="red">previous order#' . $nt[orderNum] . '</font>';
			
			}     //end of loop to check through item numbers

   if($move=="dn"){
     //echo 'checkpoint 3<br>';
           if(!empty($nextkey)){
             //echo 'checkpoint 4<br>';
                 $query="Update formData set orderNum ='" . $nextnum . "' where recordKey = '" . $fieldkey . "'";
                 mysql_query($query);
                 echo mysql_error();
					//echo "<br><font color='red'>". $query . '</font><br>';
                 $query="Update formData set orderNum ='" . $ordernumber . "' where recordKey = '" . $nextkey . "'" ;
                 mysql_query($query);
                 echo mysql_error();
					//echo "<br><font color='red'>".$query . '</font><br>';
           }
   }
   if($move=="up"){
         if(!empty($previouskey)){
                 $query="Update formData set orderNum ='" . $previousnum . "' where recordKey = '" . $fieldkey."'" ;
                 mysql_query($query);
                 echo mysql_error();
					//echo $query . '<br>';
                 $query="Update formData set orderNum ='" . $ordernumber . "' where recordKey = '" . $previouskey ."'";
                 mysql_query($query);
                 echo mysql_error();
					//echo $query . '<br>';
           }
   }

}



	

	

?>