<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = 'index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
?>
<?
include("../conex.php");
include("../css/mascara_fecha.js");

?>
<script type="text/javascript" src="../css/validarut.js"></script>
<script language="JavaScript" src="../css/cal.js" type="text/JavaScript"></script>

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Facturar Guia</title>
</head>
<?

$cant = "1";
$flag=0;
$id_nv=$_POST["txt_nv"];
/********************************** Datos Empresa ******************************************************/
$sql_empresa = "SELECT * FROM empresa, comunas WHERE comunas.cod_comuna = empresa.comuna AND id=$id_empresa";
	$res_empresa = mysql_query($sql_empresa,$enlace);
	$row=0;
	while (mysql_fetch_object ($res_empresa))
		{
		$empresa_rut=mysql_result($res_empresa, $row, "rut_empresa");
		$empresa_nombre=mysql_result($res_empresa, $row, "nombre");
		$empresa_direccion=mysql_result($res_empresa, $row, "direccion");
		$empresa_ciudad=mysql_result($res_empresa, $row, "ciudad");
		$row=$row+1;
		}
		


//********************* Sacar Datos Nota Venta ******************************************/		
$sql_nv = "SELECT * FROM salida, tipo_dcto WHERE tipo_dcto.id = salida.tipo_dcto AND tipo_dcto.descripcion ='GUIA' AND num_dcto=$id_nv AND salida.empresa = $id_empresa AND tipo_dcto.empresa = $id_empresa";
$res_nv= mysql_query($sql_nv,$enlace);
$nv_cont = 0;
$sqlnv_ivar='';
while ( mysql_fetch_object($res_nv))
	{
	$sqlnv_id=mysql_result($res_nv, $nv_cont, "salida.id");
	$id_nota_venta=$sqlnv_id;
	$sqlnv_id_sucursal=mysql_result($res_nv, $nv_cont, "salida.id_sucursal");
	$sqlnv_fecha=mysql_result($res_nv, $nv_cont, "fecha_emision");
	$sqlnv_fecha_ve=mysql_result($res_nv, $nv_cont, "fecha_vencimiento");
	$sqlnv_rut=mysql_result($res_nv, $nv_cont, "rut_cliente");
	$sqlnv_td=mysql_result($res_nv, $nv_cont, "tipo_dcto");
	$sqlnv_nv=mysql_result($res_nv, $nv_cont, "num_dcto");
	$lstpago=mysql_result($res_nv, $nv_cont, "tipo_pago");
	$id_vendedor=mysql_result($res_nv, $nv_cont, "vendedor");
	$id_bodega=mysql_result($res_nv, $nv_cont, "bodega");
	$id_zona=mysql_result($res_nv, $nv_cont, "zona");
	$sql_neto=mysql_result($res_nv, $nv_cont, "neto");
	$sql_iva=mysql_result($res_nv, $nv_cont, "iva");
	$sql_total=mysql_result($res_nv, $nv_cont, "total");
	$sqlnv_ivar=mysql_result($res_nv, $nv_cont, "iva_retenido");
	$nv_cont += 1;
	}
	if ($nv_cont==0){
	?>
	<script language="javascript">
	alert("No existe nota de venta relacionada al numero ingresado");
	window.top.open('','_parent','');window.top.close();
	</script>
	<?
	}
	//********************************* Sacar numero factura *********************************************/
	$sql_ultima_bol = "SELECT correlativo FROM tipo_dcto_num_bodega  WHERE id_bodega = $id_bodega AND id_dcto=2";
	$res_ultima_bol = mysql_query($sql_ultima_bol,$enlace);
	$sql_num_bol=0;
	if(mysql_fetch_object($res_ultima_bol))	$sql_num_bol = mysql_result($res_ultima_bol,0 ,"correlativo");
	$sql_num_factura = $sql_num_bol + 1;
	
	
	
	$res_ven = mysql_query("SELECT * FROM vendedor WHERE id=$id_vendedor",$enlace);
	if(mysql_fetch_object($res_ven)) $txtvendedor = mysql_result($res_ven, 0, "nombres");
    $res_ven = mysql_query("SELECT * FROM tipo_pago WHERE id=$lstpago",$enlace);
	if(mysql_fetch_object($res_ven)) $forma_pago = mysql_result($res_ven, 0, "descripcion");
	$res_ven = mysql_query("SELECT * FROM zona WHERE id=$id_zona",$enlace);
	if(mysql_fetch_object($res_ven)) $txtzona = mysql_result($res_ven, 0, "descripcion");
	$res_ven = mysql_query("SELECT * FROM bodega WHERE id=$id_bodega",$enlace);
	if(mysql_fetch_object($res_ven)) $txtbodega = mysql_result($res_ven, 0, "descripcion");
	
	$res_cli = mysql_query("SELECT * FROM sucursales, nombres WHERE nombres.rut=sucursales.rut AND id=$sqlnv_id_sucursal",$enlace);
	if(mysql_fetch_object($res_cli)) 
		{
		$id_sucursal = mysql_result($res_cli, 0, "sucursales.id");
		$sucursal = mysql_result($res_cli, 0, "sucursales.sucursal");
		$txtrsoc = mysql_result($res_cli, 0, "sucursales.razon_social");
		$txtactividad = mysql_result($res_cli, 0, "sucursales.rubro_giro");
		$txtemail = mysql_result($res_cli, 0, "sucursales.email");
		$txtfono = mysql_result($res_cli, 0, "sucursales.fono");
		$txtfax = mysql_result($res_cli, 0, "sucursales.fax");
		$id_region = mysql_result($res_cli, 0, "sucursales.region");
		$id_comuna = mysql_result($res_cli, 0, "sucursales.comuna");
		$txtciudad = mysql_result($res_cli, 0, "sucursales.ciudad");
		$txtdireccion = mysql_result($res_cli, 0, "sucursales.direccion");
		$txtmontocredito = mysql_result($res_cli, 0, "monto_credito");
		$txtdias_plazo = mysql_result($res_cli, 0, "nombres.dias_plazo");
		}
		
	$unixtime = time();
		$settime = $unixtime;
		$dia = date("d",$settime);
		$mes = date("m",$settime);
		$ano = date("Y",$settime); 
		$fecha= "$dia/$mes/$ano";    //  hoy
	
	  $unixtime = time();
		$settime = $unixtime  + ( 86400* $txtdias_plazo);
		$dia = date("d",$settime);
		$mes = date("m",$settime);
		$ano = date("Y",$settime); 
	
	$fecha_ve = "$dia/$mes/$ano";	// plazo
	
		
?>


<script type="text/javascript" src="../css/validarut.js"></script>
<body>
<form id="form1" name="form1" method="post" action="">
<input name="txtvalser" type="text" id="txtvalser" style="visibility:hidden" size="1" />
<table width="640" align="center" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="60px"><div align="center"><img src="../Images/logo.gif" width="60px" height="60px"></div></td>
	<td width="580px"><div align="left"><font size="1">
		<?=$empresa_nombre?><br>
		<?=$empresa_direccion?><br>
		<?=$empresa_ciudad;?>

		</font></div>
	</td>
     <td width="280px" align="left"><br>
      <input type="text" name="txttitulo" id="txttitulo" align="middle" style="height:50px;width:300px;border-width:0; text-align:left; font-size:18px" value="FACTURA Nº <?=$sql_num_factura;?>"/>
    </td>
    <td width="20%">&nbsp;</td>
  </tr>
</table>
<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
		<td width="94">Fecha:</td>
		<td width="148">
			<input name="txtfecha" type="text" style="border-width:0;" value="<?=$fecha?>" size="23" readonly="1">	  </td>
		<td width="26"><a  href="javascript:show_calendar('form1.txtfecha');"><img name="foto2" id="foto2" align="top" border="0" src="../Images/CALENDAR.gif" style="cursor:pointer" title="Presione aqui y seleccione la fecha" /></a></td>
		<td width="86"> Vencimiento</td>
		<td width="286">
			<input name="fecha_ve" type="text" id="fecha_ve" style="border-width:0;" value="<?=$fecha_ve?>" size="12" readonly="1" />	
			<a  href="javascript:show_calendar('form1.fechaven');"><img name="foto2" id="foto2" align="top" border="0" src="../Images/CALENDAR.gif" style="cursor:pointer" title="Presione aqui y seleccione la fecha" /></a>	  </td>
	</tr>
    
	<tr>
		<td width="94">R.U.T.</td>
		<td width="148">
			<input name="txtrut" type="text" id="txtrut" value="<?=$sqlnv_rut?>"  onBlur="compruebasuc('form1')"  onKeyPress="return valida(event)" size="23" maxlength="12"/>	  </td>
		<td width="26" align="left"><a onClick="JavaScript:PopWindow('buscacliente.php')">
			<IMG align="middle" border="0" src="../Images/modificar.gif" style="cursor:pointer" title="Presione aqui para buscar cliente" width="16" height="16"></a>		</td>
	  <td width="86" align="right"><div align="left">Sucursal: &nbsp;</div></td>
      
		<td width="286"><select name="lstsuc" disabled="disabled" style="width:278px" onChange="comprueba()">
        <option value="null"><?=$sucursal?> </option>
      </select></td>   
              
  </tr>
<tr>
		<td width="94">E-Mail</td>
		<td width="148">
			<input name="txtemail" id="txtemail" type="text" value="<?=$txtemail?>" size="23" > </td>
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">Señor: &nbsp;</div></td>
		<td width="286"><input name="txtrsoc" id="txtrsoc" type="text" value="<?=$txtrsoc?>" size="42" /></td>
	</tr>
<tr>
		<td width="94">Fono: </td>
	  <td width="148"><input name="txtfono" id="txtfono" type="text" value="<?=$txtfono;?>" size="23" onKeyPress="validaenter(event,6);" /></td>
		<td width="26">&nbsp;</td>
	  <td width="86" align="right"><div align="left">Actividad:&nbsp;      </div></td>
		<td width="286"><input name="txtactividad" id="txtactividad" type="text" value="<?=$txtactividad?>" size="42" onKeyPress="validaenter(event,3);" /></td>
</tr>
	<tr>
		<td width="94">Fax:</td>
	  <td width="148"><input name="txtfax" id="txtfax" type="text"  value="<?=$txtfax?>" size="23" onKeyPress="validaenter(event,7);" /></td>
		<td width="26">&nbsp;</td>
	  <td width="86" align="right"><div align="left">Regi&oacute;n: &nbsp;</div></td>
		<td width="286"><select name="lstreg" onChange="rellenaCombo('form1');" style="width:278px">
          <option value="null" selected>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");
			
			?>
         <option value="<?=$ides?>"  <? if ($id_region==$ides){?> selected="selected"<? }?>><? echo $nombres;?></option>           <?
  				$cont = $cont + 1;
 			
			}
			?>
              </select>	</td>
	</tr>
	<tr>
		<td width="94">Iva Retenido:</td>
	  <td width="148"><input name="txtivar" id="txtivar" type="text" size="23" onKeyPress="return validan(event);" /></td>
		<td width="26">&nbsp;</td>
	  <td width="86" align="right"><div align="left">Comuna:&nbsp;</div></td>
	 <td width="286"><select name="lstcom" style="width:278px">
          <option value="null" selected>SELECCIONE COMUNA
            <?
			$cons_region = mysql_query("SELECT * FROM comunas;", $enlace);
 			$cont=0;
			while ( mysql_fetch_object ($cons_region) ) 
 			{
 				$ides=mysql_result($cons_region, $cont, "cod_comuna");
  				$nombres=mysql_result($cons_region, $cont, "comuna");
			
			?>
         <option value="<?=$ides?>"  <? if ($id_comuna==$ides){?> selected="selected"<? }?>><? echo $nombres;?></option>           <?
  				$cont = $cont + 1;
 			
			}
			?>
              </select>	</td>
	</tr>
		<tr>
		<td width="94">N&ordm; Dcto </td>
		<td width="148">
		  <input type="text" name="txtndcto" size="23" readonly="1" onKeyPress="return validan(event);" value="<?=$sql_num_factura;?>" onBlur="fun_vndcto()" />		</td>
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">Ciudad:&nbsp;</div></td>
		<td width="286"><input name="txtciudad" id="txtciudad" type="text" value="<?=$txtciudad?>" size="42" onKeyPress="validaenter(event,4);" /></td>
	</tr>
	
	<tr>
		<td width="94">Zona</td>
		<td width="148">
		  <?
			$sql_umed = "SELECT * FROM zona ORDER BY descripcion";
			$res_umed = mysql_query($sql_umed,$enlace);
			$contb=0;
			?>
              <select name="lstzona" id="lstzona" style="width:166px;">
			  <option value="0" selected="selected">SELECCIONE ZONA</option>
			  <?
			  while (mysql_fetch_object ($res_umed))
				{
				$sql_umed_cod = mysql_result($res_umed, $contb, "id");
				$sql_umed_nom = mysql_result($res_umed, $contb, "descripcion");
			?>
                <option value="<?=$sql_umed_cod?>"  <? if ($id_zona==$sql_umed_cod){?> selected="selected"<? }?>><? echo $sql_umed_nom;?></option>               
                
			<?
				$contb = $contb+1;
			}
			?>
              </select>		</td>
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">Direcci&oacute;n:</div></td>
		<td width="286"><input name="txtdireccion" id="txtdireccion" type="text" value="<?=$txtdireccion?>" size="42" onKeyPress="validaenter(event,5);" /></td>
	</tr>
	<tr>
		<td width="94">Bodega</td>
		<td width="148">
		  <input type="text" name="txtbodega" id="txtbodega" size="23" readonly="1" value="<?= $txtbodega;?>"/>		</td>
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">Vendedor:</div></td>
		<td width="286"><?
			$sql_umed ="SELECT id , nombres AS descripcion FROM vendedor ORDER BY nombres ASC";
			$res_umed = mysql_query($sql_umed,$enlace);
			$contb=0;
			?>
              <select name="lstvendedor" id="lstvendedor" style="width:278px;">
			  <option value="0" selected="selected">SELECCIONE VENDEDOR</option>
			  <?
			  while (mysql_fetch_object ($res_umed))
				{
				$sql_umed_cod = mysql_result($res_umed, $contb, "id");
				$sql_umed_nom = mysql_result($res_umed, $contb, "descripcion");
			?>
                <option value="<?=$sql_umed_cod?>" <? if ($id_vendedor==$sql_umed_cod){?> selected="selected"<? }?>> <? echo $sql_umed_nom." (".$sql_umed_cod.")";?></option>
			<?
				$contb = $contb+1;
			}
			?>
              </select>		</td>
	</tr>
     <tr>
    <td>Credito</td>
    <td><input name="txtmontocredito" type="text" id="txtmontocredito" style="border-width:0;text-align:right;font-size:14px;" value="<?=$txtmontocredito;?>" size="14" readonly="1" /></td>
    <td>&nbsp;</td>
    <td>Tipo Pago:</td>
    <td><select name="lstpago" id="lstpago" style="width:200px" onChange="validactacte();">
     
      <?
			$cons_pago = mysql_query("SELECT * FROM tipo_pago WHERE descripcion ='CONTADO' OR descripcion = 'CREDITO'", $enlace);
 			$cont=0;
			while ( mysql_fetch_object ($cons_pago) ) 
 			{
 				$ides=mysql_result($cons_pago, $cont, "id");
  				$nombres=mysql_result($cons_pago, $cont, "descripcion");
  				if($lstpago==$ides){$sel="selected";}
				else {$sel="";}
				echo "              <option value=\"".$ides."\"$sel>".$nombres."\n";
				
  				$cont = $cont + 1;
 			}
			?>
    </select>
   </td>
       
  </tr>
    	<tr>
		<td width="94">Guia</td>
		<td width="148"><input name="txtnotaventa" id="txtnotaventa" type="text" value="<?=$sqlnv_nv?>" size="12" /></td>
		<td width="26">&nbsp;</td>
		<td width="86" align="right"><div align="left">O.C.</div></td>
		<td width="286"><input name="txtnumoc" id="txtnumoc" type="text" value="<?=$txtnumoc?>" size="12"/></td>
	</tr>
</table>


<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
  	<td width="10%"><div align="right">Observacion:</div></td>
    <td width="90%"><div align="left"> <input type="text" name="txtobs" id="txtobs" size="60" value="<?= $txtobs;?>"/></div></td>
    
  </tr>
</table>

<table width="640" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
	<tr bgcolor="#000000"> 
		<td width="8%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Cant.</font></strong></div></td>
		<td width="12%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">C&oacute;digo</font></strong></div></td>
		<td width="35%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Detalle</font></strong></div></td>
		
		<td width="12%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Precio</font></strong></div></td>
		<td width="12%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Total $</font></strong></div></td>
    </tr>
<?
$sql_pnv = "SELECT * FROM movimiento WHERE numero_dcto = $sqlnv_nv AND tipo_dcto = $sqlnv_td AND empresa = $id_empresa";
$res_pnv= mysql_query($sql_pnv,$enlace);
$pnv_cont = 0;
$descuentos=0;
$txtsubtotal=0;
$cant_real=1;
while ( mysql_fetch_object($res_pnv))
	{
	$cod_producto=mysql_result($res_pnv, $pnv_cont, "cod_producto");
	$cantidad=mysql_result($res_pnv, $pnv_cont, "cantidad");
	$precio=mysql_result($res_pnv, $pnv_cont, "precio_unitario");
	$descuento=mysql_result($res_pnv, $pnv_cont, "descuentos");
	$descuentos+=$descuento;
	$ltotal=$precio*$cantidad;
	$txtsubtotal+=$ltotal;
	$sql_pro = "SELECT * FROM productos WHERE codigo=$cod_producto";
     $res_pro= mysql_query($sql_pro,$enlace);
	   $detalle=mysql_result($res_pro, 0, "descripcion");

		?>
		
		
		
		<tr> 
			<td><div align="left">
			    <input name="cantidad[<? echo "$cant_real";?>]" id="cantidad[<? echo "$cant_real";?>]" type="text" value="<? echo "$cantidad";?>" readonly="1" style="border-width:0;width:100%" border="0">
			</div></td>
			<td><div align="left">
				  <input name="codigo[<? echo "$cant_real";?>]" id="codigo[<? echo "$cant_real";?>]" type="text" value="<? echo "$cod_producto";?>" readonly="1" style="border-width:0;width:100%" border="0">
			</div></td>
			<td><div align="left">  
				<input name="detalle[<? echo "$cant_real";?>]" id="detalle[<? echo "$cant_real";?>]" type="text" value="<? echo "$detalle";?>" readonly="1" style="border-width:0;width:100%" border="0">
			</div></td>
			
			  
			<td><div align="right">
				<input name="precio[<? echo "$cant_real";?>]" id="precio[<? echo "$cant_real";?>]" type="text" value="<? echo "$precio";?>" readonly="1" style="border-width:0;text-align:right;width:100%" border="0">
			</div></td>
				
			<td><div align="right">
				<input name="total[<? echo "$cant_real";?>]" id="total[<? echo "$cant_real";?>]" type="text" value="<? echo "$ltotal";?>" readonly="1" style="border-width:0;text-align:right;width:100%" border="0">
			</div></td>
		</tr>
		<?  
		
	$pnv_cont += 1;
	$cant_real += 1;
	}
	?>
	
</table>
<br>
<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">SUB TOTAL</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<input name="sql_subtotal" id="sql_subtotal" type="text" size="9" value="<?=$txtsubtotal?>" readonly="1" style="border-width:0; text-align:right" border="0">
        </font></div></td>
    </tr>
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">DESCUENTO</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<input name="sql_descuento" id="sql_descuento" type="text" size="9" value="<?=$descuentos;?>" readonly="1" style="border-width:0; text-align:right" border="0">
        </font></div></td>
    </tr>
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">NETO</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<input name="sql_neto" id="sql_neto" type="text" size="9" value="<?=$sql_neto?>" readonly="1" style="border-width:0; text-align:right" border="0">
        </font></div></td>
    </tr>
	<tr>
		<td width="5%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">IVA</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
		  $<input name="sql_iva" id="sql_iva" type="text" size="9" value="<?=$sql_iva?>" readonly="1" style="border-width:0;text-align:right" border="0">
	    </font></div></td>
    </tr>
	<tr>
		<td width="5%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
	  	<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">TOTAL </font></strong></div></td>
	  	<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
	    $<input name="sql_total" id="sql_total" type="text" size="9" value="<?=$sql_total?>" readonly="1" style="border-width:0;text-align:right" border="0">
      </font></div></td>
    </tr>
</table>
<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
	  	<td width="100%" align="center">
			<input type="button" name="Anular" id="Anular" value="Anular Guia" onClick="fun_anular();" />
			<input type="button" name="Imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();" />
	  	  <input type="button" name="guardar" id="guardar" value="Guardar/Facturar" onClick="fun_guardar();" />
	  	  <input type="button" name="salir" id="salir" value="Salir" onClick="fun_salir();" /></td>
	</tr>
</table>
<input name="txtcom" type="text" id="txtcom" style="visibility:hidden" size="1" />
<input name="txtcompser" type="text" id="txtcompser" style="visibility:hidden" size="1" />
<input name="txtguardado" type="text" id="txtguardado" style="visibility:hidden" size="1" value="0"/>
<input name="id_nota_venta" type="text" id="id_nota_venta" style="visibility:hidden" size="1" value="<?=$id_nota_venta?>"/>
<input name="txtnv" type="text" id="txtnv" style="visibility:hidden" size="1" value="<?=$id_nv;?>" />
<input name="id_bodega" type="text" id="id_bodega" style="visibility:hidden" size="1" value="<?=$id_bodega?>" />
<input name="id_vendedor" type="text" id="id_vendedor" style="visibility:hidden" size="1" value="<?=$id_vendedor?>" />
<input name="id_zona" type="text" id="id_zona" style="visibility:hidden" size="1" value="<?=$id_zona?>" />
<input name="id_sucursal" type="text" id="id_sucursal" style="visibility:hidden" size="1" value="<?=$id_sucursal?>" />

</form>

</body>
</html>
<script>
	var nav4 = window.Event ? true : false;
	<!-- Ingreso solo numeros y k K - . -->
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 validan(evt)
	{
	var key = nav4 ? evt.which : evt.keyCode;
	if (key==13) document.form1.txtrsoc.focus()
	return (key <= 13 || (key >= 48 && key <= 57) || (key == 46));
	}
function validaenter(evt, donde)
	{
	var key = nav4 ? evt.which : evt.keyCode;
	if (key==13&& donde==1) document.form1.txtemail.focus();
	if (key==13&& donde==2) document.form1.txtactividad.focus();
	if (key==13&& donde==3) document.form1.txtciudad.focus();
	if (key==13&& donde==4) document.form1.txtdireccion.focus();
	if (key==13&& donde==5) document.form1.txtfono.focus();
	if (key==13&& donde==6) document.form1.txtfax.focus();
	if (key==13&& donde==7) document.form1.menubol4.focus();
	}
function PopWindow(pagina)
	{
	if (pagina=='buscacliente.php')
		{
		window.open(pagina,'TEST','width=682,height=550,menubar=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no,top=70,left=300');
		}
	else
		{
		window.open(pagina,'TEST','width=1,height=1,menubar=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no,top=70,left=300');
		}
	}
function PopWindow2(cant)
	{
	window.open('buscachassis.php?id='+cant+'&cod='+document.getElementById("codigo["+cant+"]").value,'TEST','width=682,height=550,menubar=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no,top=70,left=300');
	}
function comprueba()
	{
	if (Rut(document.form1.txtrut.value)) PopWindow('compruebaf.php?rutc='+document.form1.txtrut.value);
	}
	
function fun_anular()
	{
	var answer = confirm ("¿Esta seguro de anular esta nota de venta?\n");
		if (answer)
			{
			document.form1.action="anular.php?cnt=<?=$id_nota_venta?>";
			document.form1.target="_self";
			document.form1.submit();
			}
		else
			{
			return false;
			}
	}
function fun_guardar()
	{
	if (document.form1.txtguardado.value==0)
		{
		
		if (document.form1.txtrut.value!='' && document.form1.txtrsoc.value!='' && document.form1.txtdireccion.value!='' && document.form1.txtciudad.value!='' && document.form1.lstcom.value!= 'null')
			{
			
				document.form1.action="guarda_factura.php?cnt=<?=$cnt?>";
				document.form1.target="_blank";
				document.form1.submit();
				document.form1.txtguardado.value=1;
				
			}
		else
			{
			alert('Ingrese todos los datos del cliente');
			return false;
			}
		}
	else
		{
		alert('Este documento ya ha sido guardado');
		return false;
		}
	}
function fun_salir()
	{
	if (document.form1.txtguardado.value==0)
		{
		var answer = confirm ("No ha guardado la nota de venta, ¿Desea guardarla?\n Si preciona cancelar saldra sin guardar");
		if (answer)
			{
			fun_guardar();
			}
		else
			{
			window.top.open('','_parent','');window.top.close();
			}
		}
	else
		{
		var answer = confirm ("¿Seguro desea salir?");
		if (answer)
			{
			window.top.open('','_parent','');window.top.close();
			}
		}
	}
</script>
<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 CIUDAD", '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;  
        }
	}
function valval()
	{
	var reg=document.form1.lstreg.value;
	rellenaCombo('form1');
	document.form1.lstcom.value=document.form1.txtcom.value;
	}

function fun_imprimir()
	{
	var a = fun_guardar();
	if (a!= false)
      {
	
	if (document.form1.txtrut.value!='' && document.form1.txtrsoc.value!='' && document.form1.txtdireccion.value!='' && document.form1.txtciudad.value!='' && document.form1.lstcom.value!= 'null')
		{
		
		 	document.form1.action="imprimir.php?cnt=<?=$cant_real?>";
			document.form1.target="_blank";
			document.form1.submit();
			setTimeout('funcion_salir();',1000);
		
		}
	else
		{
		alert('Ingrese todos los datos del cliente');
		}
	  }
	}
function funcion_salir()
	{
	window.top.open('','_parent','');window.top.close();
	}

function compruebasuc(formulario)
	{
	if (Rut(document.form1.txtrut.value))
		{
		with (document.forms[formulario]) 
			{
			  var rut = txtrut.value; 
			  var n = lstsuc.length;  
			  
			  lstsuc.disabled = false;  
			  
			  for (var i = 0; i < n; ++i)
				lstsuc.remove(lstsuc.options[i]); 
			  
			  lstsuc[0] = new Option("SELECCIONE SUCURSAL", 'null'); 
			  
			  if (rut != 'null')  
			  {
	   <?php
	   
		$cons_region = mysql_query("SELECT * FROM nombres;", $enlace);
		$cont=0;
		while ( mysql_fetch_object ($cons_region) )
		{
		 $reg = mysql_result($cons_region, $cont, "rut");
	   ?>
				if (rut == '<?php echo $reg;?>')
				{
				
				
				
		<?php
		
		  $cons_comuna = mysql_query("SELECT * FROM sucursales WHERE rut = '".$reg."' ORDER BY sucursal;", $enlace);
		  $cont2=0;
		  while ( mysql_fetch_object ($cons_comuna) )
		  {
		   $id_comuna = mysql_result($cons_comuna, $cont2, "rut");
		   $comuna = mysql_result($cons_comuna, $cont2, "sucursal");
		?>
				  lstsuc[lstsuc.length] = new Option("<?php echo $comuna;?>", '<?php echo $comuna;?>');
		<?php
		
			$cont2 = $cont2 + 1;
		  }
		?>



				}
	   <?php
	   
			$cont = $cont + 1;
		}
	   ?>
				lstsuc.focus();  
			  }
			  else  
			  {
				lstsuc.disabled = true;  
				txtrut.focus(); 
			  }
			  
			  lstsuc.selectedIndex = 0;  
			}
		}
	else
		{
		for (var i = 0; i < n; ++i)
			document.form1.lstsuc.remove(document.form1.lstsuc.options[i]); 
		document.form1.lstsuc[0] = new Option("SELECCIONE SUCURSAL", 'null'); 
		document.form1.lstsuc.selectedIndex = 0;
		document.form1.lstsuc.disabled = true;  
		}
	}		
	


</script>
