<?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['pho-title5'];
  $helppage = "";

  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['mer_id'])) puterror2($TEXT['error-pho15']);
  if(strlen($_POST['mer_id'])>11) puterror2($TEXT['error-pho13']);
  if(empty($_POST['meropriyatie'])) puterror2($TEXT['error-pho1']);
  if(strlen($_POST['meropriyatie'])>800) puterror2($TEXT['error-pho2']);
  if(empty($_POST['position'])) puterror2($TEXT['error-pho3']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-pho4']);
  if(strlen($_POST['old_org_id'])>11) puterror2($TEXT['error-org1']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org1']);
  if(strlen($_POST['old_org'])>9) puterror2($TEXT['error-res13']);
  if(empty($_POST['org_id'])) puterror2($TEXT['error-org10']);

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

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

  $_POST['meropriyatie'] = rep($_POST['meropriyatie']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['mer_id'] = rep($_POST['mer_id']);
  $_POST['old_org_id'] = rep($_POST['old_org_id']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['old_org'] = rep($_POST['old_org']);

  $new_org = $_POST['org_id'];

  //*************************************
  $query = "SELECT * FROM `tbl_meropriyatiya` WHERE `mer_id`='".$_POST['mer_id']."' ";
  $result = mysql_query($query);
  if (!$result) puterror2($TEXT['error-tbl12']);
   $result2 = mysql_fetch_array($result);

     $query_org = "SELECT `organizaciya` FROM `tbl_organizaciya` WHERE `org_id`='".$_POST['old_org_id']."' ";
     $result_org = mysql_query($query_org);
       if (!$result_org) puterror2($TEXT['error-tbl7']);
           $result_org2 = mysql_fetch_array($result_org);
           $res_org = $result_org2['organizaciya'];

     $query_org2 = "SELECT `organizaciya` FROM `tbl_organizaciya` WHERE `org_id`='".$_POST['org_id']."' ";
     $result_org2 = mysql_query($query_org2);
       if (!$result_org2) puterror2($TEXT['error-tbl7']);
           $result_org3 = mysql_fetch_array($result_org2);
           $res_org2 = $result_org3['organizaciya'];

  if($new_org !== $_POST['old_org_id']) $log_org = $TEXT['log-res6'].$log_res_visible.$TEXT['log-org3'].$res_org.$TEXT['log-nd8'].$res_org2;
    else $log_org = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-pho6'];
  $what = $result2['meropriyatie']." ".$TEXT['log-na']." ".$_POST['meropriyatie']." ".$log_org." ".$TEXT['log-id']." ".$_POST['mer_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_meropriyatiya` SET `meropriyatie` = '".$_POST['meropriyatie']."', `position` = '".$_POST['position']."', `org_id` = '$new_org' WHERE `mer_id`='".$_POST['mer_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-pho20']);
  require_once "../util/bottom.php";
?>
