<?  require('system/login.php');  ?>
<?php require_once('../Connections/JS2.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {

switch ($_POST['ck1']) {
  case 0:  $updateSQL = sprintf("UPDATE jscookmenu SET cid=%s, title=%s, link=%s, link_tag=%s, idx=%s WHERE mid=%s",
                       GetSQLValueString($_POST['cid'], "int"),
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['link'], "text"),
                       GetSQLValueString($_POST['link_tag'], "text"),
					   GetSQLValueString($_POST['idx'], "int"),
                       GetSQLValueString($_POST['mid'], "int"));

          mysql_select_db($database_JS2, $JS2);
          $Result1 = mysql_query($updateSQL, $JS2) or die(mysql_error());
  
          break;
   case 1:$updateSQL = sprintf("INSERT INTO jscookmenu (cid,title,link,link_tag,idx) VALUES ( %s , %s, %s, %s, %s)",
              GetSQLValueString(0, "int"),
              GetSQLValueString($_POST['title'], "text"),
              GetSQLValueString($_POST['link'], "text"),
              GetSQLValueString($_POST['link_tag'], "text"),
			  GetSQLValueString($_POST['idx'], "int"));

              mysql_select_db($database_JS2, $JS2);
              $Result1 = mysql_query($updateSQL, $JS2) or die(mysql_error());
  
      break;
    case 2:$updateSQL = sprintf("INSERT INTO jscookmenu (cid,title,link,link_tag,idx) VALUES ( %s , %s, %s, %s, %s)",
              GetSQLValueString($_POST['cid'], "int"),
              GetSQLValueString($_POST['title'], "text"),
              GetSQLValueString($_POST['link'], "text"),
              GetSQLValueString($_POST['link_tag'], "text"),
			  GetSQLValueString($_POST['idx'], "int"));

              mysql_select_db($database_JS2, $JS2);
              $Result1 = mysql_query($updateSQL, $JS2) or die(mysql_error());
  
      break; 
    case 3:$updateSQL = sprintf("INSERT INTO jscookmenu (cid,title,link,link_tag,idx) VALUES ( %s , %s, %s, %s, %s)",
              GetSQLValueString($_POST['mid'], "int"),
              GetSQLValueString($_POST['title'], "text"),
              GetSQLValueString($_POST['link'], "text"),
              GetSQLValueString($_POST['link_tag'], "text"),
			  GetSQLValueString($_POST['idx'], "int") );

              mysql_select_db($database_JS2, $JS2);
              $Result1 = mysql_query($updateSQL, $JS2) or die(mysql_error());
  
      break;   
   case 4: $updateSQL = sprintf("DELETE FROM jscookmenu WHERE mid = %s ",
              GetSQLValueString($_POST['mid'], "int"));
              mysql_select_db($database_JS2, $JS2);
              $Result1 = mysql_query($updateSQL, $JS2) or die(mysql_error());
  
      break;  
   }
}




function sel($cid){
require('../Connections/JS2.php');
mysql_select_db($database_JS2, $JS2);
$query_R2 = "SELECT * FROM jscookmenu WHERE cid = $cid ORDER BY idx ASC";
$R2 = mysql_query($query_R2, $JS2) or die(mysql_error());
$row_R2 = mysql_fetch_assoc($R2);
$totalRows_R2 = mysql_num_rows($R2);
$arr1=array();
$i=1;
do {
 $arr1[$i][0]=$row_R2['mid'];
 $arr1[$i][1]=$row_R2['cid']; 
 $arr1[$i][2]="(".$row_R2['idx'].")".$row_R2['title'];
 $arr1[$i][3]=$_SERVER['PHP_SELF']."?mid=".$row_R2['mid'];//$row_R2['link'];
 $arr1[$i][4]="_self";//$row_R2['link_tag']; 
 $i++;
  } while ($row_R2 = mysql_fetch_assoc($R2));
 if (count($arr1)<1 ){return 0; }else{return $arr1;} 
 
}

mysql_select_db($database_JS2, $JS2);
$query_R1 = "SELECT * FROM jscookmenu WHERE cid = 0 ORDER BY idx ASC";
$R1 = mysql_query($query_R1, $JS2) or die(mysql_error());
$row_R1 = mysql_fetch_assoc($R1);
$totalRows_R1 = mysql_num_rows($R1);


$arr2=array();
$mycc=0;
$c=0;
$menutxt='';
$menutxt .= '<script language="JavaScript" type="text/javascript">
var myMenu =
[';

 do {
   $c++;
   $chkc=$c;
   $str1=$_SERVER['PHP_SELF']."?mid=".$row_R1['mid'];//$row_R1['link']<>''?"'".$row_R1['link']."'" :'null';
   $str2="_self";//$row_R1['link_tag']<>''?"'".$row_R1['link_tag']."'" :'null';
   //if ($c<>0){ $menutxt .= '], _cmSplit,'."\n"; }
   $str0=$row_R1['pic']<>''?'\'<img name="" src="'.$row_R1['pic'].'" width="36" height="20" alt="" />\'':'null';
   $menutxt .= "[".$str0.", '(".$row_R1['idx'].")".$row_R1['title']."', '".$str1 ."','". $str2 ."', null ";  
   $re2=($row_R1['mid']<>"")?sel($row_R1['mid']) :0;
   if ((count($re2)>0 )&&($re2[1][0]<>"")){
     $menutxt .=","; 
     for($i=1;$i<=count($re2);$i++){
	   if($re2[$i][0]<>""){  
	   $c++;
	   $str1=$re2[$i][3]<>''?"'".$re2[$i][3]."'" :'null';
	   $str2=$re2[$i][4]<>''?"'".$re2[$i][4]."'" :'null';
       $menutxt .= "[null, '".$re2[$i][2]."', ". $str1 .",". $str2 .", null"; 
       $re3=sel($re2[$i][0]);
	   if ((count($re3)>0 )&&($re3[1][0]<>"")){
	    $menutxt .=","; 
	    for($j=1;$j<=count($re3);$j++){
	      if($re3[$j][0]<>""){
	       
		   $str1=$re3[$j][3]<>''?"'".$re3[$j][3]."'" :'null';
	       $str2=$re3[$j][4]<>''?"'".$re3[$j][4]."'" :'null';		  
		   
		   $menutxt .= "[null, '".$re3[$j][2]."',". $str1 .",". $str2 .", null "; 
		   $re4=sel($re3[$j][0]) ;
		   if ((count($re4)>0 )&&($re4[1][0]<>"")){
		   $menutxt .=","; 
		    for($k=1;$k<=count($re4);$k++){
		      if($re4[$k][0]<>""){
			   $str1=$re4[$k][3]<>''?"'".$re4[$k][3]."'" :'null';
	           $str2=$re4[$k][4]<>''?"'".$re4[$k][4]."'" :'null';	
			  
               $menutxt .= "[null, '".$re4[$k][2]."',". $str1 .",". $str2 .", null "; 
		        }
		      $menutxt .="],\n";
			  }
		    }
			$menutxt .="],\n";
		  }
	   }
	   
	   }
	  $menutxt .="],\n";
	    
	 }
    }
   }

  $menutxt .= "], _cmSplit, \n"; 
 } while ($row_R1 = mysql_fetch_assoc($R1));

$colname_Rt3 = "-1";
if (isset($_GET['mid'])) {
  $colname_Rt3 = $_GET['mid'];
}
mysql_select_db($database_JS2, $JS2);
$query_Rt3 = sprintf("SELECT * FROM jscookmenu WHERE mid = %s", GetSQLValueString($colname_Rt3, "int"));
$Rt3 = mysql_query($query_Rt3, $JS2) or die(mysql_error());
$row_Rt3 = mysql_fetch_assoc($Rt3);
$totalRows_Rt3 = mysql_num_rows($Rt3);




mysql_free_result($R1);


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
<link href="12x_font.css" rel="stylesheet" type="text/css" />
</head>
<script language="JavaScript" src="images/jscookmenu.js" type="text/javascript"></script>
<link rel="stylesheet" href="images/theme.css" type="text/css" />
<script language="JavaScript" src="images/theme.js" type="text/javascript"></script>
<body>

<table width="644" height="300" border="0" align="center">
  <tr>
    <td width="177" valign="top"><div ID=myMenuID></div></td>
    <td width="457" valign="top">
    <form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
      <table width="456" border="0">
        <tr>
          <td width="49" class="font_blank_12x">位置</td>
          <td colspan="2" class="font_blank_12x">
            <label><?php echo $row_Rt3['title']; ?></label></td>
        </tr>
        <tr>
          <td width="49" class="font_blank_12x">說明</td>
          <td colspan="2" class="font_blank_12x"><input name="title" type="text" id="title" value="<?php echo $row_Rt3['title']; ?>" /></td>
        </tr>
        <tr>
          <td class="font_blank_12x">連結</td>
          <td colspan="2" class="font_blank_12x"><label>
            <input name="link" type="text" id="link" value="<?php echo $row_Rt3['link']; ?>" size="50" />
          </label></td>
        </tr>
        <tr>
          <td class="font_blank_12x">&nbsp;</td>
          <td colspan="2" class="font_blank_12x"><label>
            <select name="link_tag" id="link_tag">
              <option value="_blank" <?php if (!(strcmp("_blank", $row_Rt3['link_tag']))) {echo "selected=\"selected\"";} ?>>開新視窗</option>
              <option value="_self" <?php if (!(strcmp("_self", $row_Rt3['link_tag']))) {echo "selected=\"selected\"";} ?>>無</option>
            </select>
            <input name="mid" type="hidden" id="mid" value="<?php echo $row_Rt3['mid']; ?>" />
            <input name="cid" type="hidden" id="cid" value="<?php echo $row_Rt3['cid']; ?>" />
          </label></td>
        </tr>
        <tr>
          <td class="font_blank_12x">&nbsp;</td>
          <td width="159" class="font_blank_12x"><label>
            <input name="ck1" type="radio" id="radio" value="0" checked="checked" />
            修正此結點
            </label></td>
          <td width="234" class="font_blank_12x"><input type="radio" name="ck1" id="radio2" value="1" />
新增為根結點 </td>
        </tr>
        <tr>
          <td class="font_blank_12x">&nbsp;</td>
          <td class="font_blank_12x"><input name="ck1" type="radio" id="radio3" value="2" />
            新增為平行結點            </td>
          <td class="font_blank_12x"><input type="radio" name="ck1" id="radio4" value="3" />
新增為子結點</td>
        </tr>
        <tr>
          <td class="font_blank_12x">&nbsp;</td>
          <td colspan="2" class="font_blank_12x"><input type="radio" name="ck1" id="radio5" value="4" />
            刪除此結點 </td>
        </tr>
        <tr>
          <td class="font_blank_12x">排列</td>
          <td colspan="2" class="font_blank_12x"><label>
            <input name="idx" type="text" id="idx" value="<?php echo $row_Rt3['idx']; ?>" size="8" maxlength="3" />
            排列順序的編號
          </label></td>
        </tr>
        <tr>
          <td class="font_blank_12x">&nbsp;</td>
          <td colspan="2" class="font_blank_12x"><label>
            <div align="center">
              <input type="submit" name="button" id="button" value="送出" />
            </div>
            </label></td>
        </tr>
      </table>
      <input type="hidden" name="MM_update" value="form1" />
    </form>
    </td>
  </tr>
</table>
<?
//echo count($arr2)."<br>";
echo $menutxt .=" ] ;";
echo "cmDraw ('myMenuID', myMenu, 'vbr', cmThemeOffice, 'ThemeOffice');</script>";
?>


</body>
</html>
<?
mysql_free_result($Rt3);
?>