<?php
  ///////////////////////////////////////////////////
  //2007 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang.".php")) include "../lang/".$lang.".php";
     else include "../lang/ru.php";

  $title=$titlepage=$TEXT['support102'];
  require_once "../config.php";
  require_once "top.php";
  ?>
  <meta http-equiv='Refresh' content='300; URL=manager.php' />
  <title><?php echo $title; ?></title>
  <link rel="StyleSheet" type="text/css" href="../utils/reader.css" />
  </head>
  <body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" bottommargin="0" topmargin="0" >
  <?php
  if(!isset($button)) $button = $TEXT['support111'];
  if(!isset($action)) $action = "zayavka_ed.php";
  if(!isset($button2)) $button2 = $TEXT['support104'];
  if(!isset($action2)) $action2 = "zayavka_ed_frm.php";

  if($result2['user'] == 1) puterror2($TEXT['error-supp11']);
  if($result2['master'] == 1) puterror2($TEXT['error-supp11']);

  if(empty($_POST['zay_id'])) puterror2($TEXT['error-usr1']);
  if(strlen($_POST['zay_id'])>11) puterror2($TEXT['error-usr2']);
  $_POST['zay_id'] = rep($_POST['zay_id']);

  if(empty($_POST['tema_id'])) $_POST['tema_id'] = "";
  if(strlen($_POST['tema_id'])>11) puterror2($TEXT['error-supp1']);
  $_POST['tema_id'] = rep($_POST['tema_id']);

  if(strlen($_POST['tema_id2'])>11) puterror2($TEXT['error-supp1']);
  $_POST['tema_id2'] = rep($_POST['tema_id2']);

  if(empty($_POST['org_id'])) $_POST['org_id'] = "";
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org1']);
  $_POST['org_id'] = rep($_POST['org_id']);

  if(strlen($_POST['org_id2'])>11) puterror2($TEXT['error-org1']);
  $_POST['org_id2'] = rep($_POST['org_id2']);

  if($_POST['onorg'] == "on")
    {
   $_POST['org_id'] = $_POST['org_id'];
   $onchb = "checked";
    }
  else
  {
  if(!empty($_POST['org_id2'])) 
    {
    $_POST['org_id'] = $_POST['org_id2'];
    $onchb = "checked";
    }
    else
    {
    $_POST['org_id'] = "";
    $onchb = "";
    }
  }

  if($_POST['ontema'] == "on")
    {
   $_POST['tema_id'] = $_POST['tema_id'];
   $onchb2 = "checked";
    }
  else
  {
  if(!empty($_POST['tema_id2'])) 
    {
    $_POST['tema_id'] = $_POST['tema_id2'];
    $onchb2 = "checked";
    }
    else
    {
    $_POST['tema_id'] = "";
    $onchb2 = "";
    }
  }
       $query_zaya = "SELECT tbl_supp_zayavka.*, tbl_supp_tema.tema, tbl_supp_srochnost.srochnost 
	FROM tbl_supp_zayavka 
	JOIN tbl_supp_tema ON tbl_supp_zayavka.tema_id=tbl_supp_tema.tema_id 
	JOIN tbl_supp_srochnost ON tbl_supp_zayavka.sroch_id=tbl_supp_srochnost.sroch_id 
	WHERE tbl_supp_zayavka.zay_id = '".$_POST['zay_id']."'";
       $zaya = mysql_query($query_zaya);
         if(!$zaya) puterror2($TEXT['error-tbl26']);
            $zayavka = mysql_fetch_array($zaya,MYSQL_ASSOC);
            $tema2 = $zayavka['tema'];
            $zay_id_old = $_POST['zay_id'];
            $org_id = $zayavka['org_id'];
            $mast_id_old = $zayavka['mast_id'];
            $sost_id_old = $zayavka['sost_id'];
            $sroch_id_old = $zayavka['sroch_id'];

?>
<form enctype='multipart/form-data' action=<?php echo $action2; ?> method=post>
<table align="center" class="table" border="0" cellpadding="0" cellspacing="0" width="97%">
<tr><td>
<fieldset class="filterr">
<legend align="left" title="<?php echo $TEXT['support104']; ?>"><?php echo $TEXT['support104']; ?></legend>
<table align="center" class="table" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr class="support_ob3"><td width="30%"><?php echo $TEXT['support89']; ?></td><td><?php echo $TEXT['support13']; ?></td><td width="2%">&nbsp;</td></tr>
<tr><td><input type=checkbox name=onorg <?php echo $onchb; ?> /><select class=input name="org_id">
    <?php
        $query4 = "SELECT * FROM `tbl_organizaciya` ORDER BY position";
        $organizaciya_tmp = mysql_query($query4);
          if(!$organizaciya_tmp) puterror2($TEXT['error-tbl7']);
              while($organizaciya = mysql_fetch_array($organizaciya_tmp,MYSQL_ASSOC))
                {
                 if($organizaciya['org_id'] == $_POST['org_id'])
                   {
                   $chk = "<option selected value=".$organizaciya['org_id'].">".$organizaciya['organizaciya']."";
                   }
                 else $chk = "<option value=".$organizaciya['org_id'].">".$organizaciya['organizaciya']."";
                 echo $chk;
                }
    ?>
    </select></td>
    <td><input type=checkbox name=ontema <?php echo $onchb2; ?> /><select class=input name="tema_id">
    <?php
        $query_tema = "SELECT `tema`, `tema_id` FROM `tbl_supp_tema` ORDER BY position";
        $tema_tmp = mysql_query($query_tema);
          if(!$tema_tmp) puterror2($TEXT['error-tbl22']);
              while($tema = mysql_fetch_array($tema_tmp,MYSQL_ASSOC))
                {
                 if($tema['tema_id'] == $_POST['tema_id'])
                   {
                   $chk_tema = "<option selected value=".$tema['tema_id'].">".$tema['tema']."";
                   }
                 else $chk_tema = "<option value=".$tema['tema_id'].">".$tema['tema']."";
                 echo $chk_tema;
                }
    ?>
    </select></td>
	<td width="5%">
	<input class=button type=submit value='<?php echo $button2; ?>' />
	<input type=hidden name=zay_id value='<?php echo $zay_id_old; ?>' />
	</td>
</tr>
</table>
</fieldset>
</td></tr>
<tr><td>
<hr width="100%"/>
</form>
     <table align='center' border='0' width='100%' cellpadding='0' cellspacing='0'><tr>
      <td>
         <fieldset class="zayavka" >
         <legend align="left"><?php echo $TEXT['support1']; ?> <?php echo $_POST['zay_id']; ?></legend>
          <table align='center' border='0' width='100%' cellpadding='0' cellspacing='0'>
        <form enctype='multipart/form-data' action=<?php echo $action; ?> method=post>
             <tr><td>&nbsp;</td>
                 <td width="70%" class="support_ob3" align="right"><?php echo $TEXT['support109']; ?><input type=checkbox name=ochistka /></td>
             </tr>
             <tr>
               <td align="right" width="20%" class="support">&nbsp;<?php echo $TEXT['support42']; ?></td>
                <?php
                   $query_man2 = "SELECT * FROM `tbl_logins` WHERE `login`='".$_COOKIE['login']."'";
                    $man_tmp = mysql_query($query_man2);
                     if(!$man_tmp) puterror2($TEXT['error-tbl20']);
                      $man = mysql_fetch_array($man_tmp,MYSQL_ASSOC);
                        $manager = $man['fullname'];
                        $man_id = $man['login_id'];
                   ?>
               <td class="support_ob4" width="80%" align="left">&nbsp;&nbsp;<?php echo $manager; ?></td>
             </tr>
             <tr><td align="right" width="0%" class="support">&nbsp;<?php echo $TEXT['support41']; ?></td>
                 <td width="70%" class="support_ob" align="left">&nbsp;&nbsp;<?php echo $zayavka['srochnost']; ?></td>
             </tr>
             <tr><td align="right" width="20%" class="support">&nbsp;<?php echo $TEXT['support13']; ?></td>
                 <td width="70%" class="support_ob" align="left">&nbsp;&nbsp;<?php echo $zayavka['tema']; ?></td>
             </tr>
             <tr>
               <td align="right" width="20%" class="support">&nbsp;<?php echo $TEXT['support14']; ?></td>
               <td class="support_ob3" width="80%" align="left">&nbsp;&nbsp;<?= nl2br($zayavka['problema']); ?></td>
             </tr>
             <tr>
               <td align="right" width="20%" class="support">&nbsp;</td>
               <td class="support" width="80%" align="right"><input class=button type=submit value='<?php echo $button; ?>' /></td>
	     </tr>
               <input type=hidden name=zay_id value='<?php echo $zay_id_old; ?>' />
               <input type=hidden name=org_id value='<?php echo $org_id; ?>' />
               <input type=hidden name=manager value='<?php echo $manager; ?>' />
               <input type=hidden name=man_id value='<?php echo $man_id; ?>' />
               <input type=hidden name=mast_id value='<?php echo $mast_id_old; ?>' />
               <input type=hidden name=sost_id value='<?php echo $sost_id_old; ?>' />
               <input type=hidden name=sroch_id value='<?php echo $sroch_id_old; ?>' />
              </table>
       </fieldset>
     </td>
     </tr>
     </table>
      <hr />
</td></tr>
<tr><td>
<table align="center" class="table" border="1" cellpadding="4" cellspacing="0" width="100%">
<?php
   if(!empty($_POST['org_id'])) $organizaciya = "AND tbl_supp_zayavka.org_id='".$_POST['org_id']."' ";
    else $organizaciya = "";

   if(!empty($_POST['tema_id'])) $tema_zapr = "AND tbl_supp_zayavka.tema_id='".$_POST['tema_id']."' ";
    else $tema_zapr = "";

     $query_zayavka = "SELECT tbl_supp_zayavka.zay_id, tbl_supp_zayavka.ob_type, tbl_supp_zayavka.problema, tbl_supp_tema.tema, tbl_organizaciya.organizaciya, tbl_organizaciya.position, tbl_ob_zay.par_zay_id 
	FROM tbl_supp_zayavka 
	JOIN tbl_supp_tema ON tbl_supp_zayavka.tema_id=tbl_supp_tema.tema_id 
	LEFT JOIN tbl_organizaciya ON tbl_supp_zayavka.org_id=tbl_organizaciya.org_id
	LEFT JOIN tbl_ob_zay ON tbl_supp_zayavka.zay_id=tbl_ob_zay.par_zay_id 
	WHERE (tbl_supp_zayavka.ob_type != '1' OR '5')
	AND tbl_supp_zayavka.zay_id != '".$_POST['zay_id']."' 
	AND tbl_ob_zay.root_zay_id = '".$_POST['zay_id']."' OR (tbl_supp_zayavka.zay_id NOT IN (SELECT root_zay_id FROM tbl_ob_zay) AND tbl_supp_zayavka.zay_id NOT IN (SELECT par_zay_id FROM tbl_ob_zay)) $organizaciya $tema_zapr 
	ORDER BY tbl_organizaciya.position, tbl_supp_zayavka.zay_id ";
     $result_zayavka1 = mysql_query($query_zayavka);
     if(!$result_zayavka1) puterror2($TEXT['error-tbl12']);
       if(mysql_num_rows($result_zayavka1)>0);
         {
           echo "<tr class='tableheadercat'>
            <td>".$TEXT['support1']."</td>
            <td>".$TEXT['support105']."</td>
            <td>".$TEXT['support89']."</td>
            <td>".$TEXT['support13']."</td>
            <td>".$TEXT['support14']."</td>
            <td>".$TEXT['btn_add']."</td>
                 </tr>";
             while($result_zayavka = mysql_fetch_array($result_zayavka1,MYSQL_ASSOC))
              {
               $res_org = $result_zayavka['organizaciya'];
	       $chzay_tmp = $result_zayavka['zay_id'];
               $ob_type = $result_zayavka['ob_type'];
               $problem = $result_zayavka['problema'];

               if($ob_type == 0) {$ob_type_vis="<img alt='picture' src='../img/ob_free.png' height='16' width='16' align='top' />"; $viz = "";}
                elseif ($ob_type == 1) {$ob_type_vis="<img alt='picture' src='../img/ob_root.png' height='16' width='16' align='top' />"; $viz = "";}
                elseif ($ob_type == 2) {$ob_type_vis="<img alt='picture' src='../img/ob_par.png' height='16' width='16' align='top' />"; $viz = "checked";}
                elseif ($ob_type == 3) {$ob_type_vis="<img alt='picture' src='../img/ob_parall.png' height='16' width='16' align='top' />"; $viz = "";}
                elseif ($ob_type == 4) {$ob_type="<img alt='picture' src='../img/ob_man.png' height='16' width='16' align='top' />"; $viz = "";}
                elseif ($ob_type == 5) {$ob_type="<img alt='picture' src='../img/onway.png' height='16' width='16' align='top' />"; $viz = "";}
                ?>
                    <tr class='tabledata' onmouseover="this.style.backgroundColor='#cccccc';" onmouseout="this.style.backgroundColor='#ffffff';">
                      <td><?php echo $result_zayavka['zay_id']; ?></td>
                      <td><?php echo $ob_type_vis; ?></td>
                      <td><?php echo $res_org; ?></td>
                      <td><?php echo $result_zayavka['tema']; ?></td>
                      <td><?php echo nl2br($result_zayavka['problema']); ?></td>
                      <td><input type=checkbox name=chzay[<?php echo $chzay_tmp; ?>] <?php echo $viz; ?> /></td>
                    </tr>
                <?php
              }
         }
?>
</table>
</form>
</td></tr>
</table>
</body>
</html>