<?php
 include ("emenux.php");
 require ("conecta.php");
 $sw=$_POST['hdsw'];
 $sw2=$_POST['hdsw'];
 $rut=strtoupper($_POST['rut']);
 $nombre="";
 $direccion="";
 $codcomuna="";
 $telefono1="";
 $telefono2="";
 $fax="";
 $contacto="";
 $email="";
 $codcpago="";
 $cont=0;
 $error2= "0";
 $recomendacion2="0";
 if ($sw=="buscar"){
  $sqlb="select * from m01001 where rut = '$rut' ;";
  $resb=mysql_query($sqlb,$con);
  if (mysql_num_rows($resb)>0){ 
   $sw="encontrado";
   $sw2="encontrado";
   while ($filab = mysql_fetch_row($resb)){
    $nombre=$filab[1];
    $direccion=$filab[2];
    $codcomuna=$filab[3];   
    $telefono1=$filab[4];
    $telefono2=$filab[5];
    $fax=$filab[6];
    $contacto=$filab[7];
    $email=$filab[8];
    $codcpago=$filab[9];     
    }//while
   }else{
    $error2= "Rut no encontrado.";
    $recomendacion2="->Agregar.";
   }//fin si   
  }//fin de buscar
  if ($sw=="grabar"){
   $rut=strtoupper($_POST['rut']);
   $nombre=$_POST['nombre'];
   $direccion=$_POST['direccion'];
   $codcomuna=$_POST['hdcom'];   
   $telefono1=$_POST['telefono1'];
   $telefono2=$_POST['telefono2'];
   $fax=$_POST['fax'];
   $contacto=$_POST['contacto'];
   $email=$_POST['email'];
   $codcpago=$_POST['hdcodpag'];//hdcodpag
   $sqlg="replace into m01001 
   (rut,nombre,direccio,codcomun,telefon1,telefon2,fax,contacto,email,codconpa)
   values ('$rut','$nombre','$direccion','$codcomuna','$telefono1','$telefono2','$fax','$contacto','$email','$codcpago');";
   $resg=mysql_query($sqlg,$con) ; 
   $rut="";    
   $nombre="";
   $direccion="";
   $codcomuna="";
   $telefono1="";
   $telefono2="";
   $fax="";
   $contacto="";
   $email="";
   $codcpago="";
   $sw=""; 
 }//fin grabar 
  
 if ($sw=="eliminar"){
  $sqle="delete from m01001 where rut='$rut';";
  mysql_query($sqle,$con);
  $rut="";    
  $nombre="";
  $direccion="";
  $codcomuna="";
  $telefono1="";
  $telefono2="";
  $fax="";
  $contacto="";
  $email="";
  $codcpago="";
  $sw="";
 }//fin si de eliminar
 if (strlen($codcomuna)==0){$codcomuna="5406";}
 if (strlen($codcpago)==0){$codcpago="001";}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE>Maestro Proveedores</TITLE>
<script language="javascript" src="fstandar.js"></script>
<script language="javascript">
function ponecomuna(){
 document.form1.hdcom.value=document.form1.cmbcom.value;
 }
function ponecpago(){
 document.form1.hdcodpag.value=document.form1.cmbcondpag.value;
 //window.alert(document.form1.hdcodpag.value);
 } 
function valida(){ 
 if (document.form1.rut.value.length > 0){
  err=0;
  document.form1.hdsw.value="grabar";
  if (modulo11(document.form1.rut.value)==-1  ){
   window.alert("El rut ingresado es incorrecto" );
   err=1;
   document.form1.rut.style.backgroundColor="#FFFFCC";
   document.form1.rut.focus();  
   }
  if (verificaMail(document.form1.email,"")==false){
   err=1;
   document.form1.email.style.backgroundColor="#FFFFCC";
   document.form1.email.focus();
   }
  if (document.form1.nombre.value.length==0){
   err=1;
   window.alert("debe ingresar nombre");
   document.form1.nombre.style.backgroundColor="#FFFFCC";
   document.form1.nombre.focus();
   } 
  if (document.form1.direccion.value.length==0){
   err=1;
   window.alert("debe ingresar direccion");
   document.form1.direccion.style.backgroundColor="#FFFFCC";
   document.form1.direccion.focus();
   }  
  if (err==0){
   document.form1.action="M0101001.php"
   document.form1.method="post";
   document.form1.submit();
   }  
 }//fin si  
}  
function buscarut(){
 if (modulo11(document.form1.rut.value)==0){
  document.form1.rut.style.backgroundColor="#FFFFFF";
  document.form1.hdsw.value="buscar";
  document.form1.action="M0101001.php";
  document.form1.method="post";
  document.form1.submit();
 // window.alert("si");
  }else{
   window.alert("El rut ingresado es incorrecto" );
   document.form1.rut.style.backgroundColor="#FFFFCC";
   document.form1.rut.focus();  
  }//si 
 } 
 function limpiar(){
  document.form1.rut.value="";
  document.form1.hdrut.value="";
  document.form1.nombre.value="";  
  document.form1.direccion.value="";
  document.form1.comuna.value="";
  document.form1.hdcom.value="";
  document.form1.telefono1.value="";
  document.form1.telefono2.value="";
  document.form1.fax.value="";
  document.form1.contacto.value="";
  document.form1.email.value="";
  document.form1.cmbcondpag.value="";
  document.form1.hdcodcpag.value="";
 }//fin limpiar
 function eliminar(){
 if(window.confirm("Seguro desea eliminar?")==true){
  document.form1.hdsw.value="eliminar";
  document.form1.action="M0101001.php";
  document.form1.method="post";
  document.form1.submit();
  }
 }//fin eliminar 
</script>
<link rel="stylesheet" href="styles.css">
<style type="text/css">
<!--
.Estilo1 {color: #0000FF}
.Estilo2 {color: #FF0000}
-->
</style>
</HEAD>
<BODY <?php
 if ($sw2=="buscar"){
  echo "onload='javascript:(document.form1.nombre.focus())'";
  }
 if ($sw2=="grabar" || $sw2=="eliminar"){
  echo "onload='javascript:(document.form1.rut.focus())'";
  } 
 if ($sw2=="encontrado" ){
  echo "onload='javascript:(document.form1.cmdgra.focus())'";
  }  
  ?>>
<TABLE height="100%" cellSpacing=0 cellPadding=0 width="100%" align=center 
border=0>
  <TBODY>
  <TR>
    <TD vAlign=center align=middle height="100%">
      <TABLE height=400 cellSpacing=0 cellPadding=0 width=718 border=0>
        <TBODY>
        <TR>
          <TD width="718" height=22>
            <TABLE height=22 cellSpacing=0 cellPadding=0 width="100%" 
              border=0><TBODY>
              <TR>
                <TD vAlign=top align=left width=24><A 
                  href="index.php"><IMG src="images/xc_left.gif" alt="Inicio" width=24 
                  height=22 
                  border=0></A></TD>
                <TD vAlign=top align=left width="100%"><IMG height=22 
                  src="images/xc_center.gif" width="100%" 
                border=0></TD>
              <TD vAlign=top align=right width=24><IMG height=22 
                  src="images/xc_right.gif" width=24 
              border=0></TD></TR></TBODY></TABLE></TD></TR>
        <TR>
          <TD class=BottomTable vAlign=top 
          background=images/xc_stripes.gif height="100%">
            <TABLE class=IconsTable height=33 cellSpacing=0 cellPadding=0 
            width="100%" border=0>
              <TBODY>
              <TR>
                <TD height="32"><div align="right" class="larger">
                  <div align="right">Vemol</div>
                </div></TD>
                </TR></TBODY></TABLE>
            <TABLE cellSpacing=0 cellPadding=0 width="715" 
              border=0>
              <TBODY>
              <TR>
                <TD width=23 height=18>&nbsp;</TD>
                <TD vAlign=top align=left width=180 height=18>&nbsp;</TD>
                <TD width=12 height=18>&nbsp;</TD>
                <TD width="461" height=18 class=MainMenuHead>Proveedores:</TD>
                <TD width=20 height=18>&nbsp;</TD></TR>
              <TR>
                <TD width=23 height="100%">&nbsp;</TD>
                <TD vAlign=top align=left width=180 height="100%">
                  <TABLE cellSpacing=0 cellPadding=0 width="150" border=0>
                    <TBODY>
                    <TR>
                      <TD width=10 valign="top"><IMG height=12 
                        src="images/xc_dotgrey.gif" width=10></TD>
                      <TD width=4>&nbsp;</TD>
                      <TD class=LeftMenu noWrap>Ingreso de Proveedores.<br />
                        -&gt;Agregar.<br />
-&gt;Modificar.<br />
-&gt;Eliminar.</TD>
                    </TR>
                    <tr>
                      <td valign="top"><img 
                        src="images/xc_dotgrey.gif" width="10" height="12" /></td>
                      <td>&nbsp;</td>
                      <td class="LeftMenu" nowrap="nowrap">Alertas:<span class="Estilo2"><br />
                            <?php echo $error2 ?></span></td>
                    </tr>
                    <tr>
                      <td valign="top"><img 
                        src="images/xc_dotgrey.gif" width="10" height="12" /></td>
                      <td>&nbsp;</td>
                      <TD class=LeftMenu noWrap>Se recomienda:<span class="Estilo1"><br />
                          <?php echo $recomendacion2 ?></span></TD>
                    </TR></TBODY></TABLE></TD>
                <TD width=12 height="100%">&nbsp;</TD>
                <TD class=contentcell vAlign=top align=middle width=461 
                height="100%">
                  <TABLE width="100%">
                    <TBODY>
                    <TR>
                      <TD>
                        <form name="form1" method="post" action="">
                          <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <th scope="col"><div align="right" class="Emphasize">Rut:</div></th>
                              <th scope="col"><div align="left">
                                <input name="rut" type="text" id="rut" onblur="buscarut()" value="<?php echo $rut ?>" size="10" maxlength="9" />
                                <input name="hdsw" type="hidden" id="hdsw" value="nada" />
                              </div></th>
                            </tr>
                            <tr>
                              <th scope="row"><div align="right" class="Emphasize">Nombre:</div></th>
                              <td><input name="nombre" type="text" id="nombre"  value="<?php echo $nombre ?>" size="50" maxlength="100"/></td>
                            </tr>
                            <tr>
                              <th scope="row"><div align="right" class="Emphasize">Direccion:</div></th>
                              <td><input name="direccion" type="text" id="direccion" value="<?php echo $direccion ?>" size="50" maxlength="100"/></td>
                            </tr>
                            <tr>
                              <th scope="row"><div align="right" class="Emphasize">Comuna:</div></th>
                              <td>
							  <div align="left">
							  	<?php
									$sqlcom="select * from m01003 order by 2;";
									$rescom=mysql_query($sqlcom,$con) ;				
								?>
								<select name="cmbcom" id="cmbcom" onchange="ponecomuna()">
								<?php 
									while ($fila=mysql_fetch_row($rescom)){
									if ($fila[0]==$codcomuna){
				                  	echo "<option value=$fila[0] selected='selected'>$fila[1]</option>";
									}else{
									 echo "<option value=$fila[0]>$fila[1]</option>";
									 }//si
									}
								?>
								</select>
								<input name="hdcom" type="hidden" id="hdcom" value="<?php echo $codcomuna ?>"/>				
                              </div></td>
                            </tr>
                            <tr>
                              <th scope="row"><label>
                                <div align="right" class="Emphasize">Tel&eacute;fono1:</div>
                              </label></th>
                              <td><input name="telefono1" type="text" id="telefono1" value="<?php echo $telefono1 ?>" size="16" maxlength="15" /></td>
                            </tr>
                            <tr>
                              <th scope="row"><label>
                                <div align="right"><span class="Emphasize">Teléfono2</span>:</div>
                              </label></th>
                              <td><input name="telefono2" type="text" id="telefono2" value="<?php echo $telefono2 ?>" size="16" maxlength="15" /></td>
                            </tr>
                            <tr>
                              <th scope="row"><label>
                                <div align="right"><span class="Emphasize">Fax</span>:</div>
                              </label>                                </th>
                              <td><input name="fax" type="text" id="fax" value="<?php echo $fax ?>" size="16" maxlength="15" /></td>
                            </tr>
                            <tr>
                              <th scope="row"><label>
                                <div align="right"><span class="Emphasize">Contacto</span>:</div>
                              </label></th>
                              <td><input name="contacto" type="text" id="contacto" value="<?php echo $contacto ?>" size="50" maxlength="100"/></td>
                            </tr>
                            <tr>
                              <th scope="row"><div align="right"><span class="Emphasize">Email</span>:</div></th>
                              <td><input name="email" type="text" id="email" value="<?php echo $email ?>" size="50" maxlength="100" /></td>
                            </tr>
                            <tr>
                              <th scope="row"><div align="right" class="Emphasize">Condici&oacute;n de Pago:</div></th>
                              <td>
							  <?php
							  		$sqlcodpag="select * from m01002 order by 2;";
									$rescodpag=mysql_query($sqlcodpag,$con) ;				
							  ?>
								<select name="cmbcondpag" id="cmbcondpag" onchange="ponecpago()">
								<?php 
									while ($fila=mysql_fetch_row($rescodpag)){
									if ($fila[0]==$codcpago){
										echo "<option value=$fila[0] selected='selected'>$fila[1]</option>";
									}else{
										echo "<option value=$fila[0]>$fila[1]</option>";
										}
									}
								?>
								</select>
								<input name="hdcodpag" type="hidden" id="hdcodpag" value="<?php echo $codcpago ?>" />                              </td>
                            </tr>
                            <tr>
                              <th scope="row">&nbsp;</th>
                              <td>&nbsp;</td>
                            </tr>
                            <tr>
                              <th colspan="2" scope="row"><input name="cmdgra" type="button" id="cmdgra" value="Grabar" onclick="valida()" />
                                <label></label>
                                &nbsp;&nbsp;
                                <label></label>
                                <label></label>                                
                                <label></label>
                                &nbsp;&nbsp;
								<input name="cmdel" type="button" id="cmdel" value="Eliminar" onclick="eliminar()"/>								</th>
                              </tr>
                          </table>
                                </form>						</TD>
                    </TR></TBODY></TABLE></TD>
                <TD width=20 height="100%">&nbsp;</TD></TR>
              <TR>
                <TD width=23 height=8>&nbsp;</TD>
                <TD width=180 height=8>&nbsp;</TD>
                <TD width=12 height=8>&nbsp;</TD>
                <TD width="461" height=8>&nbsp;</TD>
        <TD width=20 
      height=8>&nbsp;</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY>
<!-- /Footer -->
<script language="JavaScript" src="menu.js"></script>
<script language="JavaScript" src="menu_tpl.js"></script>
<script language="JavaScript">
	new menu (MENU_ITEMS, MENU_POS);
</script>
	  </TABLE>
	</BODY>
<?php
	mysql_close($con);
?>
</HTML>