<?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['otd-title'];

  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['otdel'])) puterror2($TEXT['error-otd2']);
  if(strlen($_POST['otdel'])>250) puterror2($TEXT['error-otd3']);
  if(empty($_POST['position'])) puterror2($TEXT['error-otd4']);
  if(strlen($_POST['position'])>5) puterror2($TEXT['error-otd5']);
  if(strlen($_POST['phone_int'])>20) puterror2($TEXT['error-user21']);

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

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

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

  $_POST['otdel'] = rep($_POST['otdel']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['phone_int'] = rep($_POST['phone_int']);

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

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