<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang."/".$lang.".php")) include "../lang/".$lang."/".$lang.".php";
     else include "../lang/ru/ru.php";

  $title=$titlepage = $TEXT['kat-title3'];
  $helppage = $TEXT['kat-help3'];

  require_once "../config.php";
  require_once "../util/top_layer1.php";

  if($result2['user'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['master'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror2($TEXT['error-loginform4']);

  if(!isset($title)) $title = $TEXT['kat-title3'];
  if(!isset($button)) $button = $TEXT['btn_edit'];
  if(!isset($action)) $action = "edit.php";

  if(strlen($_GET['zh_str_id'])>11) puterror2($TEXT['error-kat9']);
  $_GET['zh_str_id'] = rep($_GET['zh_str_id']);

  $old_zh_str_id = $_GET['zh_str_id'];

  $query_kat = "SELECT * FROM `tbl_zhnaniy_strukt` WHERE `zh_str_id`='".$_GET['zh_str_id']."' ";
  $result_kat = mysql_query($query_kat);
  if(!$result_kat) puterror2($TEXT['error-tbl45']);
   $result_kat = mysql_fetch_array($result_kat);
         $strukt_name = $result_kat['strukt_name'];
         $position = $result_kat['position'];
         $parent = $result_kat['parent'];
?>
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post>
<table align="center"  width="40%">
<tr><td class="useraddedit"><?php echo $TEXT['menu-btn33']; ?></td><td class="useraddedit"><input size=30 maxlength=255 class=inputmandatory type=text name=strukt_name value='<?php echo $strukt_name; ?>' /></td></tr>
<tr><td class="useraddedit"><?php echo $TEXT['post-pos']; ?></td><td class="useraddedit"><input size=5 maxlength=6 class="inputmandatory" type=text name=position value='<?php echo $position; ?>' /></td></tr>
<tr><td class="useraddedit"><?php echo $TEXT['kat-parent']; ?></td><td class="useraddedit">
<?php
  function ShowTree($ParentID,$Level){
    $Level++;

    $query_tree="SELECT * FROM tbl_zhnaniy_strukt WHERE parent=".$ParentID." AND `zh_str_id` != '".$_GET['zh_str_id']."' ORDER BY position, strukt_name";
     $result4=mysql_query($query_tree);
      $num_rows = mysql_num_rows($result4);
       $kurent = 1;
       if($num_rows > 0)
       {
        while($row = mysql_fetch_array($result4,MYSQL_ASSOC))
        {
        $ID1 = $row['zh_str_id'];
        if($ID1 == $_GET['zh_str_id']) $id1chk = "selected";
         else $id1chk = "";

        //echo ("kurent=".$kurent."num_rows=".$num_rows);
         $query_check=mysql_query("SELECT COUNT(zh_str_id) FROM tbl_zhnaniy_strukt WHERE `parent`=".$ID1."");
          $result_check=mysql_result($query_check, 0);
            if($result_check > 0)
            {
             if($Level == 1) $otstup = "&nbsp;&nbsp;";
              elseif($Level == 2) $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;";
               elseif($Level == 3) $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                elseif($Level == 4) $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                 elseif($Level == 5) $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                  elseif($Level == 6) $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                   else $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
             if($kurent == $num_rows)
             {
             echo("<option ".$id1chk." value=".$ID1.">".$otstup.$row['strukt_name']."");
             }
              else
              {
              echo("<option ".$id1chk." value=".$ID1.">".$otstup.$row['strukt_name']."");
              }
            ShowTree($ID1, $Level);
            }
            else
            {
             if($Level == 1) $otstup = "&nbsp;&nbsp;";
              elseif($Level == 2) $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;";
               elseif($Level == 3) $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                elseif($Level == 4) $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                 elseif($Level == 5) $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                  elseif($Level == 6) $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                   else $otstup = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
              if($kurent == $num_rows)
             {
             echo("<option ".$id1chk." value=".$ID1.">".$otstup.$row['strukt_name']."");
             }
              else
              {
              echo("<option ".$id1chk." value=".$ID1.">".$otstup.$row['strukt_name']."");
              }
            }
            $kurent++;
       }
       $Level--;
     }
   }

echo("<select name='parent'>");
echo("<option value=0>".$TEXT['obj-move-koren']."");
     $query = "SELECT * FROM `tbl_zhnaniy_strukt` WHERE `parent` = 0 AND `zh_str_id` != '".$_GET['zh_str_id']."' ORDER BY position, strukt_name";
     $result = mysql_query($query);
     if(!$result) puterror2($TEXT['error-tbl40']);
     $num_row = mysql_num_rows($result);
      $kurent_row = 1;
         if($num_row > 0){
           while($result_kat = mysql_fetch_array($result,MYSQL_ASSOC))
           {
             $ID = $result_kat['zh_str_id'];
             if($ID == $_GET['zh_str_id']) $idchk = "selected";
              else $idchk = "";

             $query_chk=mysql_query("SELECT COUNT(zh_str_id) FROM tbl_zhnaniy_strukt WHERE `parent`= ".$ID."");
             $result_chk=mysql_result($query_chk, 0);
            if($result_chk > 0)
            {
             if($kurent_row == $num_row)
             {
             echo("<option ".$idchk." value=".$ID.">".$result_kat['strukt_name']."");
             }
              else
              {
              echo("<option ".$idchk." value=".$ID.">".$result_kat['strukt_name']."");
              }

             ShowTree($ID, $Level);
            }
             else
             {
              if($kurent_row == $num_row)
              {
              echo("<option ".$idchk." value=".$ID.">".$result_kat['strukt_name']."");
              }
              else
               {
               echo("<option ".$idchk." value=".$ID.">".$result_kat['strukt_name']."");
               }
            }
             $kurent_row++;
           }
         }
   echo("</select>");
   ?>
<input type=checkbox name="movechb" /></td></tr>
<tr><td></td><td>
<input class="button" type=submit value='<?php echo $button; ?>' />
<input type=hidden name=zh_str_id value='<?php echo $old_zh_str_id; ?>' />
</td></tr>
</table>
</form>
<?php
  require_once "../util/bottom.php";
?>