<?
ini_set('error_reporting',1);
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = 'index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
$id_autoriza=32;
?>
<?
/*Sesiones*/
include("../conex.php");
include("../autoriza.php");
if ($editar==0)  
{
?>
		<script>
		alert ("No Autorizado");
		parent.location.href = 'index.php';
		</script>
	<? 
	exit();
}
$get_cod_nombres = $_GET["cp"];
$sql_bpro = "SELECT * FROM empresa WHERE id = $get_cod_nombres";
$res_bpro = mysql_query($sql_bpro,$enlace);
if (mysql_fetch_object ($res_bpro))
	{
	$sql_codigo = mysql_result($res_bpro, 0, "id");
	$sql_rut_empresa = mysql_result($res_bpro, 0, "rut_empresa");
	$sql_nombre = mysql_result($res_bpro, 0, "nombre");
	$sql_direccion = mysql_result($res_bpro, 0, "direccion");
	$sql_ciudad = mysql_result($res_bpro, 0, "ciudad");
	$sql_comuna = mysql_result($res_bpro, 0, "comuna");
	$id_comuna = $sql_comuna;
	$sql_actividad = mysql_result($res_bpro, 0, "actividad");
	$sql_rut_representante = mysql_result($res_bpro, 0, "rut_representante");
	$sql_nombre_representante = mysql_result($res_bpro, 0, "nombre_representante");
	$sql_idr = "SELECT * FROM comunas WHERE cod_comuna=$id_comuna";
	$res_idr = mysql_query($sql_idr,$enlace);
	if (mysql_fetch_object($res_idr))
		{
		$id_reg = mysql_result($res_idr, 0, "region");
		}
	}

else
	{
		$sql_codigo = $get_cod_nombres;

	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Nueva Empresa</title>
<style type="text/css">
<!--
body {
	background-color: #CCCCCC;
	margin-left: 0px;
	margin-top: 3px;
	margin-right: 0px;
	margin-bottom: 0px;
}
.Estilo7 {
	font-size: small;
	color: #000099;
}
.Estilo11 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: small; color: #000099; }
.Estilo13 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 9px;
	color: #000099;
	text-decoration: underline;
}
.Estilo14 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 9px;
	color: #000099;
}
.titulos2 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 13px;
	color: #003366;
	background-color: #CCCCCC;
	font-style: normal;
	font-weight: bold;
}
.botones {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: bold;
	height:26px;
}
-->
</style>

</head>

<table width="90%" border="0" align="center">
	<tr>
		<td><div align="center" class="titulos2">
		Editar Empresa
		</div></td>
	</tr>
</table>
<body vlink="#2293ff" alink="#2293ff" link="#2293ff" onLoad="validacodigo();">
<form name="form1" method="post" action="guardae_empresas.php">
<div align="left" class="titulos2" style="font-size:12px">
<input name="txtid" type="text" id="txtid" value="<?=$sql_codigo?>" readonly="1" size="40" style="visibility:hidden">
  <table width="90%" border="1" align="center">
    <tr>
      <td>Rut Empresa </td>
      <td>
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr>
           <td><input name="txtrut_empresa" type="text" id="txtrut_empresa" value="<?=$sql_rut_empresa?>" size="40" onblur="comprueba2()"  onKeyPress="return valida(event)"></td>
           <td>&nbsp;</td>
          </tr>
        </table></td>
    </tr>
	 <tr>
      <td>Nombre</td>
      <td>
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td><input name="txtnombre" type="text" id="txtnombre" size="40" value="<?=$sql_nombre?>"></td>
            <td>&nbsp;</td>
          </tr>
        </table></td>
    </tr>
    <tr>
      <td>Regi&oacute;n</td>
      <td>
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="40%">
<select name="lstreg" onChange="rellenaCombo('form1');" style="width:267px; font-family:Arial, Helvetica, sans-serif">
	  <?
	  	$cons_comuna = mysql_query("SELECT region FROM comunas WHERE cod_comuna=$id_comuna;", $enlace);
		$cont=0;
	  	while ( mysql_fetch_object ($cons_comuna) ) 
 			{
 				$id_reg=mysql_result($cons_comuna, $cont, "region");
			}
	  ?>
        <option value="null">SELECCIONE REGION
          	<?
			$cons_region = mysql_query("SELECT * FROM regiones;", $enlace);
 			$cont=0;
			while ( mysql_fetch_object ($cons_region) ) 
 			{
 				$ides=mysql_result($cons_region, $cont, "Cod_Region");
  				$nombres=mysql_result($cons_region, $cont, "Region");
				if ( $ides == $id_reg)
				{
					echo "              <option value=\"".$ides."\" selected>".$nombres."\n";
				}
				else
				{
  					echo "              <option value=\"".$ides."\">".$nombres."\n";
				}
  				$cont = $cont + 1;
 			}
			?>
      </select>            </td>
            <td width="60%">&nbsp;&nbsp;<a onClick="JavaScript:PopWindow('<? echo "1";?>')"></a></td>
          </tr>
        </table></td>
    </tr>
	  <td>Comuna</td>
        <td height="1px">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="40%">
              <select name="lstcom" style="width:267px;" onChange="rellenaciudad('form1')";>
            <option value="null" selected >SELECCIONE COMUNA
			<?
			$cons_comuna = mysql_query("SELECT * FROM comunas WHERE region=$id_reg;", $enlace);
 			$cont=0;
			if ($id_reg !='')
				{
			while ( mysql_fetch_object ($cons_comuna) ) 
 			{
 				$ides=mysql_result($cons_comuna, $cont, "cod_comuna");
  				$nombres=mysql_result($cons_comuna, $cont, "comuna");
				if ( $ides == $id_comuna)
				{
					echo "              <option value=\"".$ides."\" selected>".$nombres."\n";
					if ($sql_ciudad=='')$sql_ciudad=$nombres;
				}
				else
				{
  					echo "              <option value=\"".$ides."\">".$nombres."\n";
				}
  				$cont = $cont + 1;
 			}
			}
			?>
      </select>            </td>
            <td width="60%">&nbsp;&nbsp;<a onClick="JavaScript:PopWindow('<? echo "1";?>')"></a></td>
          </tr>
        </table></td>
    </tr>
    <tr>
      <td>Ciudad</td>
      <td height="1px">
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td><input name="txtciudad" type="text" id="txtciudad" size="40"  value="<?=$sql_ciudad?>"></td>
            <td>&nbsp;</td>
          </tr>
        </table></td>
    </tr>
	        <td>Direcci&oacute;n</td>
              <td height="1px">
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td><input name="txtdireccion" type="text" id="txtdireccion" size="40" value="<?=$sql_direccion?>"></td>
            <td>&nbsp;</td>
          </tr>
        </table></td>
    </tr>
          <td>Actividad</td>
          <td>
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="40%"><input name="txtactividad" type="text" id="txtactividad" size="40"  value="<?=$sql_actividad?>"></td>
          <td width="60%">&nbsp;&nbsp;<a onClick="JavaScript:PopWindow('<? echo "2";?>')"></a></td>
        </tr>
      </table></td>
    </tr>
    <tr>
      <td>Rut Representante </td>
      <td>
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="40%"><input name="txtrut" type="text" id="txtrut" size="40"  value="<?=$sql_rut_representante?>" maxlength="12" onblur="comprueba()"  onKeyPress="return valida(event)"></td>
            <td width="60%">&nbsp;&nbsp;<a onClick="JavaScript:PopWindow('<? echo "3";?>')"></a></td>
          </tr>
        </table></td>
    </tr>
	<tr>
      <td>Nombre Representante </td>
      <td>
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="40%"><input name="txtnombre_representante" type="text" id="txtnombre_representante" size="40"  value="<?=$sql_nombre_representante?>"></td>
            <td width="60%">&nbsp;&nbsp;<a onClick="JavaScript:PopWindow('<? echo "3";?>')"></a></td>
          </tr>
        </table></td>
    </tr>
  </table>
    <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td>&nbsp;</td>
      <td><div align="center">
        <label>
        <input type="button" name="Submit" value="Guardar" onClick="enviar();">
        </label>
      </div></td>
      <td>&nbsp;</td>
    </tr>
  </table>
  </div>
</form>
<script language="javascript">
function rellenaCombo(formulario)
      {
        with (document.forms[formulario]) 
        {
          var region = lstreg[lstreg.selectedIndex].value; 
          var n = lstcom.length;  
          lstcom.disabled = false;  
          
          for (var i = 0; i < n; ++i)
            lstcom.remove(lstcom.options[i]); 
          
          lstcom[0] = new Option("SELECCIONE COMUNA", 'null'); 
          
          if (region != 'null')  
          {
   <?php
   
    $cons_region = mysql_query("SELECT * FROM regiones;", $enlace);
    $cont=0;
    while ( mysql_fetch_object ($cons_region) )
    {
     $reg = mysql_result($cons_region, $cont, "Cod_Region");
   ?>
            if (region == '<?php echo $reg;?>')
            {
    <?php
    
      $cons_comuna = mysql_query("SELECT * FROM comunas WHERE region = ".$reg." ORDER BY comuna;", $enlace);
      $cont2=0;
      while ( mysql_fetch_object ($cons_comuna) )
      {
       $id_comuna = mysql_result($cons_comuna, $cont2, "cod_comuna");
       $comuna = mysql_result($cons_comuna, $cont2, "comuna");
    ?>
              lstcom[lstcom.length] = new Option("<?php echo $comuna;?>", '<?php echo $id_comuna;?>');
    <?php
    
	   	$cont2 = $cont2 + 1;
      }
    ?>
            }
   <?php
   
   		$cont = $cont + 1;
    }
   ?>
            lstcom.focus();  
          }
          else  
          {
            lstcom.disabled = true;  
            lstreg.focus(); 
          }
          
          lstcom.selectedIndex = 0;  
        }
      }
</script>
<script type="text/javascript" src="../css/validarut.js"></script>
<script type="text/javascript" src="../css/validarut2.js"></script>
<script language="javascript">
var nav4 = window.Event ? true : false;
<!-- Ingreso solo letras -->
function valida(evt){
var key = nav4 ? evt.which : evt.keyCode;
	if (key==13) document.form1.txtrsoc.focus()
	return (key <= 13 || (key >= 48 && key <= 57) || (key == 107) || (key == 75) || (key == 45) || (key == 46));
	}


function comprueba()
	{
	if (Rut(document.form1.txtrut.value)) document.form1.txtnombre_representante.focus();
	}
function comprueba2()
	{
	if (Rut2(document.form1.txtrut_empresa.value)) document.form1.txtnombre.focus();
	}	  
function enviar()
	{
	if (document.form1.txtrut_empresa.value=='')
		{
		alert('Ingrese Rut');
		document.form1.txtrut_empresa.focus();
		return false;
		}
	if (document.form1.txtnombre.value=='')
		{
		alert('Ingrese Nombre');
		document.form1.txtnombre.focus();
		return false;
		}
	if (document.form1.lstreg.value=='null')
		{
		alert('Ingrese Region');
		document.form1.lstreg.focus();
		return false;
		}
	if (document.form1.lstcom.value=='null')
		{
		alert('Ingrese Comuna');
		document.form1.lstcom.focus();
		return false;
		}
	if (document.form1.txtciudad.value=='')
		{
		alert('Ingrese Ciudad');
		document.form1.txtciudad.focus();
		return false;
		}
	if (document.form1.txtdireccion.value=='')
		{
		alert('Ingrese Direccion');
		document.form1.txtdireccion.focus();
		return false;
		}
	if (document.form1.txtactividad.value=='')
		{
		alert('Ingrese Actividad');
		document.form1.txtactividad.focus();
		return false;
		}
	if (document.form1.txtrut.value=='')
		{
		alert('Ingrese Rut Representante');
		document.form1.txtrut.focus();
		return false;
		}
	if (document.form1.txtnombre_representante.value=='')
		{
		alert('Ingrese Nombre Representante');
		document.form1.txtnombre_representante.focus();
		return false;
		}
	document.form1.submit();
	}

</script>
<script language="javascript">
function rellenaciudad(formulario)
	{
    with (document.forms[formulario]) 
    	{	
        var region = lstreg[lstreg.selectedIndex].value;
		var comu = lstcom[lstcom.selectedIndex].value;          
        if (region != 'null')  
        	{
			<?php
			$cons_region = mysql_query("SELECT * FROM regiones;", $enlace);
			$cont=0;
    		while ( mysql_fetch_object ($cons_region) )
    			{
     			$reg = mysql_result($cons_region, $cont, "Cod_Region");
   				?>
            	if (region == '<?php echo $reg;?>')
            		{
    				<?php
				  	$cons_comuna = mysql_query("SELECT * FROM comunas WHERE region = ".$reg." ORDER BY comuna;", $enlace);
				  	$cont2=0;
					while ( mysql_fetch_object ($cons_comuna) )
				  		{
       					$id_comuna = mysql_result($cons_comuna, $cont2, "cod_comuna");
       					$comuna = mysql_result($cons_comuna, $cont2, "comuna");
    					?>
						if (comu == '<?php echo $id_comuna;?>')
							{
							<?php
							$cons_ciudad = mysql_query("SELECT comuna, region FROM comunas WHERE cod_comuna = ".$id_comuna." ORDER BY comuna;", $enlace);
							$row1 = mysql_fetch_row($cons_ciudad);
							$txtauxciudad = $row1[0];
							$txtauxnum = $row1[1];
							?>
							txtciudad.value="<?=$txtauxciudad?>";
							}
    					<?php
	   					$cont2 = $cont2 + 1;
      					}
    				?>
            		}
   				<?php
   				$cont = $cont + 1;
   				}
   			?>
          	}
		}
	}
function validacodigo()
	{
	if (document.form1.txtid.value=='0')
		{
		document.form1.txtrut_empresa.readOnly=0;
		}
	else
		{
		document.form1.txtrut_empresa.readOnly=1;
		}
	}
</script>