<?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-title'];
  $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['organizaciya'])) puterror2($TEXT['error-org3']);
  if(strlen($_POST['organizaciya'])>255) puterror2($TEXT['error-org4']);
  if(empty($_POST['position'])) puterror2($TEXT['error-org5']);
  if(strlen($_POST['position'])>5) puterror2($TEXT['error-org6']);

  if($_POST['visible'] == "on") $visible = "show";
    else $visible = "hide";

  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']);

  $query_chk = "SELECT `organizaciya` FROM `tbl_organizaciya` WHERE `organizaciya`='".$_POST['organizaciya']."' ";
     $result_chk = mysql_query($query_chk);
     if (!$result_chk) puterror2($TEXT['error-tbl7']);
       if (mysql_num_rows($result_chk)>0) puterror2($TEXT['global-est']);

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-org1'];
  $what = $_POST['organizaciya'];
  $who = $result2['fullname'];
  //*************************************
  $query = "INSERT INTO `tbl_organizaciya` ( `org_id` , `organizaciya`, `position`, `visible` ) VALUES ( NULL , '".$_POST['organizaciya']."', '".$_POST['position']."', '$visible') ";
  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-org9']);
  require_once "../util/bottom.php";
?>
