<?php
  ///////////////////////////////////////////////////
  //перемещение строк вверх/вниз
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

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

  $title=$titlepage = $TEXT['zhurn-edit'];
  $helppage = $TEXT['zhurn-edit-help'];

  require_once "../config.php";
  require_once "top.php";
  ?>
  <title><?php echo $title; ?></title>
  <link rel="StyleSheet" type="text/css" href="../utils/grid.css" />
  </head>
  <body>
  <?php
  if($result2['administrator'] == 0) puterror2($TEXT['error-supp11']);

  if(empty($_GET['zh_str_id'])) puterror2($TEXT['error-zhurn1']);
  if(strlen($_GET['zh_str_id'])>11) puterror2($TEXT['error-zhurn2']);
  if(empty($_GET['zhurn_id'])) puterror2($TEXT['error-zhurn11']);
  if(strlen($_GET['zhurn_id'])>11) puterror2($TEXT['error-zhurn12']);
  if(empty($_GET['position'])) puterror2($TEXT['error-zhurn7']);
  if(strlen($_GET['position'])>6) puterror2($TEXT['error-zhurn8']);
  if(empty($_GET['moving'])) puterror2($TEXT['error-zhurn13']);
  if(strlen($_GET['moving'])>5) puterror2($TEXT['error-zhurn14']);
  if(strlen($_GET['k'])>8) puterror2($TEXT['error-zhurn15']);

  $_GET['position'] = rep($_GET['position']);
  $_GET['zh_str_id'] = rep($_GET['zh_str_id']);
  $_GET['zhurn_id'] = rep($_GET['zhurn_id']);
  $_GET['moving'] = rep($_GET['moving']);

  if($_GET['moving']=="up"){
  $query_move = "SELECT zhurn_id, position FROM `tbl_zhurnal` WHERE `position`<'".$_GET['position']."' AND `zh_str_id`='".$_GET['zh_str_id']."' ORDER BY `position` DESC LIMIT 1 ";
  $result_move = mysql_query($query_move);
  if(!$result_move) puterror2($TEXT['error-tbl41']);
   $result2_move = mysql_fetch_array($result_move,MYSQL_ASSOC);
   if($result2_move['position'] == 0) puterror2($TEXT['global-up-down']);

   $query = "UPDATE `tbl_zhurnal` SET `position`='".$result2_move['position']."' WHERE `zhurn_id`='".$_GET['zhurn_id']."' ";
   if(mysql_query($query))
   {
   $query = "UPDATE `tbl_zhurnal` SET `position`='".$_GET['position']."' WHERE `zhurn_id`='".$result2_move['zhurn_id']."' ";
     if(mysql_query($query))
      {
       echo "<html><head>
          <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=zhurnal.php?zh_str_id=".$_GET['zh_str_id']."&amp;zhurn_id=".$_GET['zhurn_id']."&amp;k=".$_GET['k']."' />
          </head></html>";
      } else puterror2($TEXT['error-tbl41']);
    } else puterror2($TEXT['error-tbl41']);
  }
  else
  {
  $query_move = "SELECT zhurn_id, position FROM `tbl_zhurnal` WHERE `position`>'".$_GET['position']."' AND `zh_str_id`='".$_GET['zh_str_id']."' ORDER BY `position` LIMIT 1";
  $result_move = mysql_query($query_move);
  if(!$result_move) puterror2($TEXT['error-tbl41']);
   $result2_move = mysql_fetch_array($result_move,MYSQL_ASSOC);
  if($result2_move['position'] == 0) puterror2($TEXT['global-up-down']);

  $query = "UPDATE `tbl_zhurnal` SET `position`='".$_GET['position']."' WHERE `zhurn_id`='".$result2_move['zhurn_id']."' ";
  if(mysql_query($query))
  {
   $query = "UPDATE `tbl_zhurnal` SET `position`='".$result2_move['position']."' WHERE `zhurn_id`='".$_GET['zhurn_id']."' ";
     if(mysql_query($query))
      {
       echo "<html><head>
          <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=zhurnal.php?zh_str_id=".$_GET['zh_str_id']."&amp;zhurn_id=".$_GET['zhurn_id']."&amp;k=".$_GET['k']."' />
          </head></html>";
      } else puterror2($TEXT['error-tbl41']);
    } else puterror2($TEXT['error-tbl41']);
  }
?>
</body>
</html>