<?php
  ///////////////////////////////////////////////////
  //перемещение строк вверх/вниз
  //2009 (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['org-title3'];

  require_once "../config.php";
  require_once "../util/top.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(empty($_GET['org_id'])) puterror2($TEXT['error-org10']);
  if(strlen($_GET['org_id'])>11) puterror2($TEXT['error-org1']);
  if(empty($_GET['position'])) puterror2($TEXT['error-org5']);
  if(strlen($_GET['position'])>5) puterror2($TEXT['error-org6']);
  if(empty($_GET['moving'])) puterror2($TEXT['error-otd4']);
  if(strlen($_GET['moving'])>5) puterror2($TEXT['error-otd5']);

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

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

   $query = "UPDATE `tbl_organizaciya` SET `position`='".$result2_move['position']."' WHERE `org_id`='".$_GET['org_id']."' ";
   if(mysql_query($query))
   {
   $query_usr = "UPDATE `tbl_users` SET `org_pos` = '".$result2_move['position']."' WHERE `org_id` = '".$_GET['org_id']."' ";
    if(mysql_query($query_usr))
     {
   $query = "UPDATE `tbl_organizaciya` SET `position`='".$_GET['position']."' WHERE `org_id`='".$result2_move['org_id']."' ";
     if(mysql_query($query))
      {
      $query_usr2 = "UPDATE `tbl_users` SET `org_pos` = '".$_GET['position']."' WHERE `org_id` = '".$result2_move['org_id']."' ";
       if(mysql_query($query_usr2))
        {
       echo "<html><head>
          <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
          </head></html>";
        } else puterror2($TEXT['error-user30']);
      } else puterror2($TEXT['error-org12']);
     } else puterror2($TEXT['error-user30']);
   } else puterror2($TEXT['error-org12']);
  }
  else
  {
  $query_move = "SELECT org_id, position FROM `tbl_organizaciya` WHERE `position`>'".$_GET['position']."' ORDER BY `position` LIMIT 1";
  $result_move = mysql_query($query_move);
  if (!$result_move) puterror2($TEXT['error-tbl7']);
   $result2_move = mysql_fetch_array($result_move);
  if($result2_move['position'] == 0) puterror2($TEXT['global-up-down']);

  $query = "UPDATE `tbl_organizaciya` SET `position`='".$_GET['position']."' WHERE `org_id`='".$result2_move['org_id']."' ";
  if(mysql_query($query))
  {
   $query_usr = "UPDATE `tbl_users` SET `org_pos` = '".$_GET['position']."' WHERE `org_id` = '".$result2_move['org_id']."' ";
    if(mysql_query($query_usr))
     {
   $query = "UPDATE `tbl_organizaciya` SET `position`='".$result2_move['position']."' WHERE `org_id`='".$_GET['org_id']."' ";
     if(mysql_query($query))
      {
      $query_usr2 = "UPDATE `tbl_users` SET `org_pos` = '".$result2_move['position']."' WHERE `org_id` = '".$_GET['org_id']."' ";
       if(mysql_query($query_usr2))
        {
        echo "<html><head>
          <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
          </head></html>";
        } else puterror2($TEXT['error-user30']);
      } else puterror2($TEXT['error-org12']);
     } else puterror2($TEXT['error-user30']);
    } else puterror2($TEXT['error-org12']);
  }
  require_once "../util/bottom.php";
?>
