<?php
if ($_POST['codigo'] == "*") unset($_POST['codigo']);

$sql_tipo_combo="SELECT ctg_tpr_id, ctg_tpr_desc FROM ctg_tipo_productos ".$_SESSION['ADMproductos']." AND ctg_tpr_on='1' ORDER BY ctg_tpr_id";

$_POST['button_graba'] = " class=button ";	

if (!isset($_POST['codigo'])) {//agregar
        $arr_label['graba'] = $arr_label['agrega'];
        $_POST['descripcion'] = $_POST['mensaje'];
} else {//aqui lee el codigo porque sino los botones no toman el atributo de habilitados
        
        $_POST['button_limpia'] = " class=button ";
        
        $sql="SELECT * FROM ".$arr_campo['tabla']." WHERE ctg_tpr_id = '".$_POST['ctg_tpr_id']."' AND ".$catalogo_prefijo."id = '".intval($_POST['codigo'])."'";
        //echo $sql;
        $result=$connection->Execute($sql);
        $error = $connection->ErrorMsg();
        if (!$result->EOF) {
                $operador = $result->fields;
                //$_POST['ctg_tpr_id'] = $operador["ctg_tpr_id"];
                $_POST['caso'] = trim($operador[ $catalogo_prefijo."cas"]);
                $_POST['descripcion'] = clean_string($operador[ $catalogo_prefijo."desc"]);
                $_POST['nombre'] = trim($operador[ $catalogo_prefijo."nomqui"]);
                $_POST['equi'] = $operador[$catalogo_prefijo."equi"];
                $_POST['admitida'] = $operador[$catalogo_prefijo."admitida"];
                $_POST['motivo'] = $operador[$catalogo_prefijo."motivo"];
                $_POST['sinonims'] = $operador[$catalogo_prefijo."sinonims"];

                $_POST['cater'] = $operador[$catalogo_prefijo."cater"];
                $_POST['con'] = $operador[$catalogo_prefijo."con"];
                $_POST['blq'] = $operador[$catalogo_prefijo."blq"];
                $_POST['obs'] = $operador[$catalogo_prefijo."obs"];

                if ($_POST['con']=='1'){
                    $id_selected1 =' selected';
                    $id_selected2 =' ';
                } else {
                    $id_selected1 =' ';
                    $id_selected2 =' selected ';
                }

                if ($_POST['blq']=='1'){
                    $blq_selected1 =' selected';
                    $blq_selected2 =' ';
                } else {
                    $blq_selected1 =' ';
                    $blq_selected2 =' selected ';
                }

                $_POST['modeofform'] = "u";
                $_POST['button_borra'] = " class=button ";

        } else {
                $_POST['modeofform'] = "i";
                $_POST['codigo'] = $arr_label['substancia_nueva'];
        }
}
?>

<form name='form_standar' id='form_standar' action="index.php" method="post" enctype="multipart/form-data">
<?php include_once("includes/form_ctg_botones.php");?>
<?php include_once("includes/modal.php");?>
<input type=hidden id="titulo_anterior" name="titulo_anterior" value="<?php echo $catalogo_titulo?>">

<script type="text/javascript" src="includes/common.js"></script>
<table border=0 align=center cellpadding=3 cellspacing=1 style="margin-top:20px" width=80% class="marco_table ui-corner-all">
<?php include_once("includes/form_titulo.php");?>
<?php
if (!isset($_POST['codigo'])){  //MUESTRA TABLA DE DESCRIPCIONES
//LISTADO ***************************************************************************************************************************
?>

<tr><td>

    <input type=hidden id="ctg_tpr_id"     name="ctg_tpr_id"      value="<?php echo $_POST['ctg_tpr_id']?>">

	<table border=0 align=center width=100% cellpadding=3 cellspacing=1 class="ui-corner-all" style='background:#333333;' >
	<tr>
	<td class=form_standar_cap><?php echo $arr_label['tipo_producto']?></td>
	<td>
              <select id=tipo name=tipo onchange="buscar_mensajes('');">
              <!--<option value=""><?php echo $arr_label['seleccione']?></option>-->
              <?php creacombo($sql_tipo_combo,$_POST['tipo']); ?>
              </select>
	</td>
	<td width=30></td>
	<td class=form_standar_cap><?php echo $arr_label['buscar']?></td>
	<td><input type="text" class="text-input" name="mensaje"  id="mensaje" onkeyup="buscar_mensajes(this.value)" value="<?php echo $_POST['descripcion']?>" size=35></td>
	<td width=50px height=22 id=mensaje_loading style='padding:0px'></td>
	<td width=60px>
        <ul id="icons" class="ui-widget ui-helper-clearfix">
        <li class="ui-state-default ui-corner-all" title="<?php echo $arr_label['imprimir']?>"><a href="javascript:popuponclick('class_pdf.php')"><span class="ui-icon ui-icon-print"></span></a></li>
        <li class="ui-state-default ui-corner-all" title="<?php echo $arr_label['exportar']?>"><a href="javascript:popuponclick('class_csv.php')"><span class="ui-icon ui-icon-extlink"></span></a></li>
        </ul>   	
	</td>
	</tr>
	</table>


</td></tr>
<tr><td>
	
	        <iframe id="frame_tabla" name="frame_tabla" width="99%" onload="resize_iframe(this.id,320)" frameborder=0 class="marco_table ui-corner-all" src="#" ></iframe>
</table>


<script type="text/javascript">
function checkform_standar( form ) {
		document.getElementById('ctg_tpr_id').value = document.getElementById('tipo').value;
		document.getElementById('codigo').value="";
		document.getElementById('form_standar').target= '';
		document.getElementById('form_standar').action= 'index.php';
		document.forms.form_standar.submit();
		
    return false;
}

function result_tabla_substan(ctg_tpr_id,codigo){//de tablas
		document.getElementById('ctg_tpr_id').value = ctg_tpr_id;
		document.getElementById('codigo').value = codigo;
		document.getElementById('form_standar').target= '';
		document.getElementById('form_standar').action= 'index.php';
		document.forms.form_standar.submit();
}


document.getElementById('tipo').value = '<?php echo $_POST['ctg_tpr_id']?>';
document.getElementById('mensaje').focus();

buscar_mensajes('<?php echo $_POST['descripcion']?>');
</script>

<?php } else { //AQUI INGRESA UN NUEVO *************************************************************************************************
?>

<script type="text/javascript">

function limpiar_datos(flag){   /*SAQUI SALI DE FORMA Y ENTRA A LISTADO*/
	if (flag == 'limpia') {//viene de botones
		document.getElementById('codigo').value = "*";//para que salga al listado
		document.getElementById('form_standar').target= '';
		document.getElementById('form_standar').action= 'index.php';
		document.getElementById('titulo_anterior').value = '';
		document.getElementById('modeofform').value = '';
		document.forms.form_standar.submit();
	} else {//solo desabilita graba aqui viene de graba op
		document.getElementById('graba').className = "buttonDis";
		document.getElementById('graba').disabled = true;
		document.getElementById('limpia').className = "button";		
		document.getElementById('limpia').disabled = false;
	}
        return false;
}

/*
function limpiar_datos(nivel){
      document.getElementById('caso').value = '';
      document.getElementById('descripcion').value = '';
      document.getElementById('nombre').value = '';
      document.getElementById('motivo').value = '';

      document.getElementById('tipo').selectedIndex = 0;
      document.getElementById('admitida').selectedIndex = 0;
} */

function checkform_standar( form ) {
      var errorCount = 0;

      var campo = document.getElementById('enfoco');

      if( form.ctg_tpr_id.selectedIndex == 0){errorCount++;if (errorCount == 1) {etiqueta = '<?php echo $arr_label['tipo_producto']?>';campo.value = 'ctg_tpr_id';}}
      //if( form.caso.value == ''){errorCount++;if (errorCount == 1) {etiqueta = '<?php echo $arr_label['caso_no']?>';campo.value = 'caso';}}
      if( form.descripcion.value == ''){errorCount++;if (errorCount == 1) {etiqueta = '<?php echo $arr_label['nom_subtancia']?>';campo.value = 'descripcion';}}
      if( form.nombre.value == ''){errorCount++;if (errorCount == 1) {etiqueta = '<?php echo $arr_label['nombre_quimico']?>';campo.value = 'nombre';}}
      
      
      if( form.equi.value == ''){errorCount++;if (errorCount == 1) {etiqueta = '<?php echo $arr_label['tipo_substancia']?>';campo.value = 'equi';}}
//      if( form.admitida.value == ''){errorCount++;if (errorCount == 1) {etiqueta = '<?php echo $arr_label['admitida']?>';campo.value = 'admitida';}}

      if( form.con.value == ''){errorCount++;if (errorCount == 1) {etiqueta = '<?php echo $arr_label['substancia_controlada']?>';campo.value = 'con';}}
      if( form.blq.value == ''){errorCount++;if (errorCount == 1) {etiqueta = '<?php echo $arr_label['substancia_bloqueada']?>';campo.value = 'blq';}}

      //if( form.cater.value == ''){errorCount++;if (errorCount == 1) {etiqueta = '<?php echo $arr_label['cater']?>';campo.value = 'cater';}}
/*      
      if( form.admitida.value == '1')
	      if( form.motivo.value == ''){errorCount++;if (errorCount == 1) {etiqueta = '<?php echo $arr_label['motivo_control']?>';campo.value = 'motivo';}}
*/


      if (errorCount) {
          call_dialog_msg(etiqueta);
          return false;
      }

			grabar_load();

        return false
}
</script>

<tr><td>

      <table border=0 align=center cellpadding=3 cellspacing=1>
      <tr><td class=form_standar_cap><?php echo $arr_label['tipo_producto']?></td>
          <td colspan=5>
          <select id="ctg_tpr_id" name="ctg_tpr_id" onClick="" onChange="cambia_select()">
          <option value=""><?php echo $arr_label['seleccione']?></option>
          <?php creacombo($sql_tipo_combo,$_POST['ctg_tpr_id']); ?>
          </select></td><td width=30></tr>

      <tr>
          <td class=form_standar_cap><?php echo $arr_label['codigo']?></td>
          <td><span id="codigo_desc" name="codigo_desc" class='fake_field'><?php echo $_POST['codigo']?></span></td>
          <td class=form_standar_cap><?php echo $arr_label['caso_no']?></td>
          <td colspan=3><input type=text  id=caso name=caso maxlength=25  size=25 class="text-input" onblur="toupper(this);"  onKeyPress="return acceptAlfaNumeric(event);" value="<?php echo $_POST['caso']?>"></td>
      </tr>


      <tr>
          <td class=form_standar_cap><?php echo $arr_label['nom_subtancia']?></td>
          <td colspan=5>         
          <textarea id="descripcion" name="descripcion" maxlength="500" onblur="toupper(this);" onkeyup="return ismaxlength(this)" rows=1 cols=49 onKeyPress="return acceptStandar(event)"><?php echo $_POST['descripcion']?></textarea>
      </tr>

 

      <tr>
          <td class=form_standar_cap><?php echo $arr_label['nombre_quimico']?></td>
          <td colspan=5><textarea id="nombre" name="nombre" maxlength="500" onblur="toupper(this);" onkeyup="return ismaxlength(this)" rows=1 cols=49 onKeyPress="return acceptStandar(event)" ><?php echo $_POST['nombre']?></textarea>
      </tr>

	<tr>
		<td class=form_standar_cap><?php echo $arr_label['sinonims']?></td>
    <td colspan=5><table>
			<tr>
				<td><textarea id="sinonims" name="sinonims" onblur="toupper(this);" onkeyup="return ismaxlength(this)" rows=2 cols=49 onKeyPress="return acceptStandar(event)"><?php echo $_POST['sinonims']?></textarea>
				<td valign=top><ul id="icons" class="ui-widget ui-helper-clearfix">    
					<li class="ui-state-default ui-corner-all" title="<?php echo $arr_label['textarea_ttt'].' '.$arr_label['sinonims']?>"><span onClick="check_window('sinonims','<?php echo $arr_label['sinonims']?>')" class="ui-icon ui-icon-newwin" style=''></span></li>
				</ul></td>
			</tr>
		</table></td>
	</tr>



      <tr>
      <td class=form_standar_cap><?php echo $arr_label['tipo_substancia']?></td>
          <td>
          <select id=equi name=equi>
          <option value='0'><?php echo $arr_label['no']?></option>
          <option value='1'><?php echo $arr_label['si']?></option>
          <!--
          <option value='A'><?php echo $arr_label['principio_activo']?></option>
          <option value='E'><?php echo $arr_label['excipiente']?></option>
          <option value='O'><?php echo $arr_label['otro']?></option>
          -->
          </select>
      </td>
<!--
      <td class=form_standar_cap><?php echo $arr_label['admitida']?></td>
          <td>
          <select id=admitida name=admitida>
          <option value='2'><?php echo $arr_label['no']?></option>
          <option value='1'><?php echo $arr_label['si']?></option>
          </select>
      </td>
-->      
<td class=form_standar_cap><?php echo $arr_label['substancia_controlada']?></td>
          <td>
          <select id=con name=con>
          <option value='2' <?php echo $id_selected2?> ><?php echo $arr_label['no']?></option>
          <option value='1' <?php echo $id_selected1?> ><?php echo $arr_label['si']?></option>
          </select>
      </td>
      
      </tr>

    <tr>
        <td class=form_standar_cap><?php echo $arr_label['substancia_bloqueada']?></td>
            <td>
            <select id=blq name=blq>
            <option value='2' <?php echo $blq_selected2?> ><?php echo $arr_label['no']?></option>
            <option value='1' <?php echo $blq_selected1?>  ><?php echo $arr_label['si']?></option>
            </select>
        </td>
                
    </tr>




<!--
    <tr>
    <td class=form_standar_cap><?php echo $arr_label['cater']?></td>
    <td colspan=5>
    <table>
    <tr>
    <td>    
    <textarea id="cater" name="cater" rows=2 cols=49><?php echo $_POST['cater']?></textarea>
    <td valign=top>    
    <ul id="icons" class="ui-widget ui-helper-clearfix">    
    <li class="ui-state-default ui-corner-all" title="<?php echo $arr_label['textarea_ttt'].' '.$arr_label['cater']?>"><span onClick="check_window('motivo','<?php echo $arr_label['cater']?>')" class="ui-icon ui-icon-newwin" style=''></span></li>
    </ul>
    </table>          
    </td>
    </tr>


    <tr>
    <td class=form_standar_cap><?php echo $arr_label['motivo_control']?></td>
    <td colspan=5>
    <table>
    <tr>
    <td>    
    <textarea id="motivo" name="motivo" rows=2 cols=49><?php echo $_POST['motivo']?></textarea>
    <td valign=top>    
    <ul id="icons" class="ui-widget ui-helper-clearfix">    
    <li class="ui-state-default ui-corner-all" title="<?php echo $arr_label['textarea_ttt'].' '.$arr_label['motivo']?>"><span onClick="check_window('motivo','<?php echo $arr_label['motivo']?>')" class="ui-icon ui-icon-newwin" style=''></span></li>
    </ul>
    </table>          
    </td>
    </tr>

-->


    <tr>
    <td class=form_standar_cap><?php echo $arr_label['obs']?></td>
    <td colspan=5>
    <table>
    <tr>
    <td>    
    <textarea id="obs" name="obs" rows=2 cols=49><?php echo $_POST['obs']?></textarea>
    <td valign=top>    
    <ul id="icons" class="ui-widget ui-helper-clearfix">    
    <li class="ui-state-default ui-corner-all" title="<?php echo $arr_label['textarea_ttt'].' '.$arr_label['obs']?>"><span onClick="check_window('motivo','<?php echo $arr_label['obs']?>')" class="ui-icon ui-icon-newwin" style=''></span></li>
    </ul>
    </table>          
    </td>
    </tr>
    
    
    </table>

</td></tr>
</table>

<script type="text/javascript">
    document.getElementById('ctg_tpr_id').value = '<?php echo $_POST['ctg_tpr_id']?>';
    document.getElementById('equi').value = '<?php echo $_POST['equi']?>';
    document.getElementById('admitida').value = '<?php echo $_POST['admitida']?>';
    document.getElementById('con').value = '<?php echo $_POST['con']?>';

    enfoco = document.getElementById('enfoco').value;
    if (document.getElementById(enfoco))
        document.getElementById(enfoco).focus();
    
    else
        if (document.getElementById('ctg_tpr_id').value == '')            
            document.getElementById('ctg_tpr_id').focus();
        else
            document.getElementById('caso').focus();            
</script>

<?php } ?>

<input type=hidden id="codigo"     name="codigo"      value="<?php echo $_POST['codigo']?>">

</form>


