<?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['supp-step-ed'];
  $helppage = $TEXT['supp-step-ed-help'];
  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['name'])) puterror2($TEXT['error-marsh8']);
  if(strlen($_POST['name'])>255) puterror2($TEXT['error-marsh9']);
  if(empty($_POST['position'])) puterror2($TEXT['error-marsh10']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-marsh11']);
  if(empty($_POST['mar_id'])) puterror2($TEXT['error-marsh16']);
  if(strlen($_POST['mar_id'])>11) puterror2($TEXT['error-marsh13']);
  if(empty($_POST['login_id'])) puterror2($TEXT['error-login15']);
  if(strlen($_POST['login_id'])>11) puterror2($TEXT['error-login14']);
  if(empty($_POST['logika'])) puterror2($TEXT['error-marsh21']);
  if(strlen($_POST['logika'])>11) puterror2($TEXT['error-marsh24']);
  if(empty($_POST['tema_id'])) puterror2($TEXT['error-tema5']);
  if(strlen($_POST['tema_id'])>11) puterror2($TEXT['error-tema4']);
  $_POST['mar_id'] = rep($_POST['mar_id']);
  $_POST['step_id'] = rep($_POST['step_id']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['login_id'] = rep($_POST['login_id']);
  $_POST['name'] = rep($_POST['name']);
  $_POST['logika'] = rep($_POST['logika']);
  $_POST['tema_id'] = rep($_POST['tema_id']);

  $query10 = "SELECT tbl_step.*, tbl_logins.login, tbl_logins.fullname, tbl_supp_tema.tema_id, tbl_supp_tema.tema
    FROM tbl_step
    JOIN tbl_logins ON tbl_step.login_id=tbl_logins.login_id
    LEFT JOIN tbl_supp_tema ON tbl_step.tema_id=tbl_supp_tema.tema_id
    WHERE `step_id`='".$_POST['step_id']."' ";
  $result10 = mysql_query($query10);
  if(!$result10) puterror2(mysql_error());
      $result_lib = mysql_fetch_array($result10,MYSQL_ASSOC);

  $query11 = "SELECT login, fullname FROM tbl_logins WHERE `login_id`='".$_POST['login_id']."' ";
  $result11 = mysql_query($query11);
  if(!$result11) puterror2($TEXT['error-tbl20']);
      $result_login = mysql_fetch_array($result11,MYSQL_ASSOC);

  $query12 = "SELECT `tema` FROM `tbl_supp_tema` WHERE `tema_id`='".$_POST['tema_id']."' ";
  $result12 = mysql_query($query12);
  if(!$result12) puterror2($TEXT['error-tbl22']);
      $result_tema = mysql_fetch_array($result12,MYSQL_ASSOC);

 if($result_lib['logika'] == "1") $logika = $TEXT['logic-marsh1'];
   elseif($result_lib['logika'] == "2") $logika = $TEXT['logic-marsh2'];
    else $logika = $TEXT['logic-marsh3'];

 if($_POST['logika'] == "1") $logika2 = $TEXT['logic-marsh1'];
   elseif($_POST['logika'] == "2") $logika2 = $TEXT['logic-marsh2'];
    else $logika2 = $TEXT['logic-marsh3'];
  //*************************************
  if(empty($_POST['name'])) $_POST['name'] = "";
  if($result_lib['step_name'] !== $_POST['name']) $log_step = $TEXT['log-marsh10']."&laquo;".$result_lib['step_name']."&raquo;".$TEXT['log-na']."&laquo;".$_POST['name']."&raquo;"."<br />";
    else $log_step = "";

  if(empty($_POST['login_id'])) $_POST['login_id'] = "";
  if($result_lib['login_id'] !== $_POST['login_id']) $log_login = $TEXT['log-marsh12']."&laquo;".$result_lib['fullname']."&#040;".$result_lib['login']."&#041;&raquo;".$TEXT['log-na']."&laquo;".$result_login['fullname']."&#040;".$result_login['login']."&#041;&raquo;"."<br />";
    else $log_login = "";

  if(empty($_POST['tema_id'])) $_POST['tema_id'] = "";
  if($result_lib['tema_id'] !== $_POST['tema_id']) $log_tema = $TEXT['log-marsh15']."&laquo;".$result_lib['tema']."&raquo;".$TEXT['log-na']."&laquo;".$result_tema['tema']."&raquo;"."<br />";
    else $log_tema = "";

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

  if($result_lib['logika'] !== $_POST['logika']) $log_logika = $TEXT['logic-marsh-tit']."&laquo;".$logika."&raquo;".$TEXT['log-na']."&laquo;".$logika2."&raquo;"."<br />";
    else $log_logika = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-marsh6'];
  $what = $log_step.$log_pos.$log_login.$log_logika.$log_tema.$TEXT['log-id'].$_POST['step_id'];
  $who = $result2['fullname'];
  //*************************************
    $query = "UPDATE `tbl_step` SET `login_id` = '".$_POST['login_id']."', `position` = '".$_POST['position']."', `step_name` = '".$_POST['name']."', `logika` = '".$_POST['logika']."', `tema_id` = '".$_POST['tema_id']."' WHERE `step_id`='".$_POST['step_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=marshindex.php?mar_id=".$_POST['mar_id']."' />
               </head></html>";
           } else puterror2($TEXT['error-log']);
         }
  else puterror2($TEXT['error-tbl43']);
  require_once "../util/bottom.php";
?>