<?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['org-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(empty($_POST['org_id'])) puterror2($TEXT['error-org10']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org1']);
  if(empty($_POST['organizaciya'])) puterror2($TEXT['error-org3']);
  if(strlen($_POST['organizaciya'])>300) puterror2($TEXT['error-org4']);
  if(empty($_POST['position'])) puterror2($TEXT['error-org5']);
  if(strlen($_POST['position'])>5) puterror2($TEXT['error-org6']);

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

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

  $_POST['organizaciya'] = rep($_POST['organizaciya']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['org_id'] = rep($_POST['org_id']);

  $query10 = "SELECT `organizaciya` FROM `tbl_organizaciya` WHERE `org_id`='".$_POST['org_id']."' ";
  $result10 = mysql_query($query10);
  if (!$result10) puterror2($TEXT['error-tbl7']);
      $result_lib = mysql_fetch_array($result10);
      if($_POST['organizaciya']!=$result_lib['organizaciya']) {
          $query9 = "SELECT `organizaciya` FROM `tbl_organizaciya` WHERE `organizaciya`='".$_POST['organizaciya']."' ";
            $result9 = mysql_query($query9);
              if (!$result9) puterror2($TEXT['error-tbl7']);
                 if (mysql_num_rows($result9)>0) puterror2($TEXT['global-est']);
                   else $name_organizaciya = "`organizaciya` = '".$_POST['organizaciya']."', ";
        }
       else $name_organizaciya = "";

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-org3'];
  $what = $result_lib['organizaciya'].$TEXT['log-na'].$_POST['organizaciya'].$TEXT['log-id'].$_POST['org_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_organizaciya` SET $name_organizaciya `position` = '".$_POST['position']."' WHERE `org_id`='".$_POST['org_id']."' ";
  if(mysql_query($query))
  {
   $query1 = "UPDATE `tbl_users` SET `org_pos` = '".$_POST['position']."' WHERE `org_id` = '".$_POST['org_id']."' ";
    if(mysql_query($query1))
     {
    $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-org12']);
  require_once "../util/bottom.php";
?>
