<?php
  ///////////////////////////////////////////////////
  //2007 (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['otd-title3'];
  $helppage = "";

  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(strlen($_POST['otd_id'])>11) puterror2($TEXT['error-otd9']);
  if(empty($_POST['otdel'])) puterror2($TEXT['error-otd2']);
  if(strlen($_POST['otdel'])>250) puterror2($TEXT['error-otd3']);
  if(empty($_POST['position'])) puterror2($TEXT['error-otd4']);
  if(strlen($_POST['position'])>5) puterror2($TEXT['error-otd5']);
  if(empty($_POST['phone_int'])) $_POST['phone_int'] = "";
  if(strlen($_POST['phone_int'])>20) puterror2($TEXT['error-user21']);

  if(isset($_POST['otdel'])) $otdel=trim($_POST['otdel']);
    else $otdel="";
     if (empty($otdel)) puterror2($TEXT['error-otd6']);

  if(isset($_POST['position'])) $position=trim($_POST['position']);
    else $position="";
     if (empty($position)) puterror2($TEXT['error-otd7']);

  $_POST['otdel'] = rep($_POST['otdel']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['otd_id'] = rep($_POST['otd_id']);
  $_POST['phone_int'] = rep($_POST['phone_int']);

  $query10 = "SELECT `otdel` FROM `tbl_otdel` WHERE `otd_id`='".$_POST['otd_id']."' ";
  $result10 = mysql_query($query10);
  if (!$result10) puterror2($TEXT['error-tbl8']);
      $result_lib = mysql_fetch_array($result10);
      if($_POST['otdel']!=$result_lib['otdel']) {
          $query9 = "SELECT `otdel` FROM `tbl_otdel` WHERE `otdel`='".$_POST['otdel']."' ";
            $result9 = mysql_query($query9);
              if (!$result9) puterror2($TEXT['error-tbl8']);
                 if (mysql_num_rows($result9)>0) puterror2($TEXT['global-est']);
                   else $name_otdel = "`otdel` = '".$_POST['otdel']."', ";
        }
       else $name_otdel = "";

  //*************************************
  $query1 = "SELECT * FROM `tbl_otdel` WHERE `otd_id`='".$_POST['otd_id']."' ";
  $result = mysql_query($query1);
  if (!$result) puterror2($TEXT['error-tbl8']);
   $result2 = mysql_fetch_array($result);

  if($result2['otdel'] !== $_POST['otdel']) $log_otd = $TEXT['log-otd5'].$result2['otdel'].$TEXT['log-nd8'].$_POST['otdel']."<br />";
    else $log_otd = "";

  if($result2['otdel'] !== $_POST['otdel']) $log_otd_visible = $_POST['otdel'];
    else $log_otd_visible = $result2['otdel'];

  if($result2['position'] !== $_POST['position']) $log_pos = $TEXT['log-otd6'].$log_otd_visible.$TEXT['log-nd12'].$result2['position'].$TEXT['log-nd8'].$_POST['position']."<br />";
    else $log_pos = "";

  if($result2['phone_int'] !== $_POST['phone_int']) $log_phi = $TEXT['log-otd6'].$log_otd_visible.$TEXT['log-nd12'].$result2['phone_int'].$TEXT['log-nd8'].$_POST['phone_int']."<br />";
    else $log_phi = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-otd4'];
  $what = $log_otd." ".$log_pos." ".$log_phi.$TEXT['log-id'].$_POST['otd_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_otdel` SET $name_otdel `position` = '".$_POST['position']."', `phone`='".$_POST['phone_int']."' WHERE `otd_id`='".$_POST['otd_id']."' ";
  if(mysql_query($query))
  {
   $query3 = "UPDATE `tbl_users` SET `otd_pos` = '".$_POST['position']."' WHERE `otd_id` = '".$_POST['otd_id']."' ";
    if(mysql_query($query3))
     {
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
     } else puterror2($TEXT['error-user30']);
  } else puterror2($TEXT['error-otd11']);
  require_once "../util/bottom.php";
?>
