<?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['marsh-edit'];
  $helppage = $TEXT['supp-mar-help2'];

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

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

  if(empty($_POST['mar_id'])) puterror2($TEXT['error-marsh16']);
  if(strlen($_POST['mar_id'])>11) puterror2($TEXT['error-marsh13']);
  if(empty($_POST['marsh'])) puterror2($TEXT['error-marsh2']);
  if(strlen($_POST['marsh'])>255) puterror2($TEXT['error-marsh3']);
  if(empty($_POST['position'])) puterror2($TEXT['error-marsh4']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-marsh5']);

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

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

  $_POST['marsh'] = rep($_POST['marsh']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['mar_id'] = rep($_POST['mar_id']);

  $query10 = "SELECT `mar_name`, `position` FROM `tbl_marsh` WHERE `mar_id`='".$_POST['mar_id']."' ";
  $result10 = mysql_query($query10);
  if(!$result10) puterror2($TEXT['error-tbl42']);
      $result_lib = mysql_fetch_array($result10);
      if($_POST['marsh']!=$result_lib['mar_name']) {
          $query9 = "SELECT `mar_name` FROM `tbl_marsh` WHERE `mar_name`='".$_POST['marsh']."' ";
            $result9 = mysql_query($query9);
              if(!$result9) puterror2($TEXT['error-tbl42']);
                 if(mysql_num_rows($result9)>0) puterror2($TEXT['global-est']);
                   else $name_marsh = "`mar_name` = '".$_POST['marsh']."', ";
        }
       else $name_marsh = "";

  //*************************************
  if(empty($_POST['marsh'])) $_POST['marsh'] = "";
  if($result_lib['mar_name'] !== $_POST['marsh']) $log_marsh = $TEXT['log-marsh7']."&laquo;".$result_lib['mar_name']."&raquo;".$TEXT['log-na']."&laquo;".$_POST['marsh']."&raquo;"."<br />";
    else $log_marsh = "";

  if(empty($_POST['position'])) $_POST['position'] = "";
  if($result_lib['position'] !== $_POST['position']) $log_pos = $TEXT['log-marsh8']."&laquo;".$result_lib['position']."&raquo;".$TEXT['log-na']."&laquo;".$_POST['position']."&raquo;"."<br />";
    else $log_pos = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-marsh5'];
  $what = $log_marsh.$log_pos.$TEXT['log-id'].$_POST['mar_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_marsh` SET $name_marsh `position` = '".$_POST['position']."' WHERE `mar_id`='".$_POST['mar_id']."' ";
  if(mysql_query($query))
  {
    $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-tbl42']);
  require_once "../util/bottom.php";
?>