<?php session_start();?>
<!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=utf-8" />
<title>Cesto</title>
<script type="text/javascript" src="../js/jquery-1.11.1.js"></script>
<script type="text/javascript">
function _agregar(idx)
{
	var cant=document.getElementById('item_'+idx).value;
	document.getElementById('err_msg').innerHTML='';
	
	var arr=document.getElementsByClassName('txt_cant').length-1;
	var cantx,pvpx,subx;
	var subx_tmp=0;
	var ivax=0;
	var iva_tmp;
	var iva_acum=0;
	for(i=0;i<=arr;i++)
	{
		cantx=parseInt(document.getElementsByClassName('txt_cant').item(i).value);
		cantx=cantx*100000000;
		pvpx=parseFloat(document.getElementsByClassName('txt_pvp').item(i).value);
		pvpx=pvpx*100000000;
		
		ivax=parseFloat(document.getElementsByClassName('txt_iva').item(i).value);
		console.log(ivax);
		subx=cantx*pvpx;
		subx=Math.round(subx);
		subx_tmp+=subx;
		iva_tmp=subx*(ivax/100);
		
		iva_acum+=iva_tmp;
		iva_tmp=iva_tmp/10000000000000000;
		
		subx=subx/10000000000000000;
		
		document.getElementsByClassName('txt_sub').item(i).value=subx.toFixed(2);
	}
	subx_tmp=subx_tmp/10000000000000000;
	iva_acum=iva_acum/10000000000000000;
	document.getElementById('subtotal').innerHTML=subx_tmp.toFixed(2);
	document.getElementById('iva').innerHTML=iva_acum.toFixed(2);

$.ajax({
		url:'ces2.php',
		type:'POST', 
		async:false,
		data:{idx:idx,cant:cant},
		success:function(dato){
			console.log(dato);
			document.body.style.cursor='auto';
			document.getElementById('err_msg').innerHTML=dato;
		}, 

		error:function(jqXHR,textStatus,errorThrown )
		{
           if (jqXHR.status === 0) {
              alert('Not connect: Verify Network..');
           } else if (jqXHR.status == 404) {
               console.log('Requested page not found [404]');
           } else if (jqXHR.status == 500) {
               console.log('Internal Server Error [500].');
           } else if (exception === 'parsererror') {
               console.log('Requested JSON parse failed.');
           } else if (exception === 'timeout') {
               //console.log('Time out error.');
			   alert('Servidor no Responde');
           } else if (exception === 'abort') {
               console.log('Ajax request aborted.');
           } else {
               console.log('Uncaught Error: ' + jqXHR.responseText);
           }

		}, 

	    complete:function(jqXHR, textStatus){
			if(textStatus==='success'){setTimeout(_limpia, 5000);}
			},
		timeout:30000
	});

}
function _limpia()
{
	document.getElementById('err_msg').innerHTML='';
}

function _adr_dato(idx){

$.ajax({
		cache:false,
		url:'op_hoja_addr.php',
		datatype: "text",
	    async:true,
		type:'POST',
		data:{idx:idx},
		beforeSend: function(jqXHR, textStatus){}, 
		success:function(dato, textStatus, jqXHR)
		{		
			console.log(dato);
			document.getElementById('nom_lbl').innerHTML=$(dato).find("nombre").text();
			document.getElementById('dir_lbl').innerHTML=$(dato).find("direccion").text();
			document.getElementById('ciu_lbl').innerHTML=$(dato).find("ciudad").text();
			document.getElementById('tel_lbl').innerHTML=$(dato).find("telefono").text();
			document.getElementById('ema_lbl').innerHTML=$(dato).find("email").text();
			document.getElementById('con_lbl').innerHTML=$(dato).find("contacto").text();
			
		},
		error:function(jqXHR,textStatus,errorThrown )
		{
           if (jqXHR.status === 0) {
              alert('Sin Conexion: Verifique Red.');
           } else if (jqXHR.status == 404) {
               console.log('Requested page not found [404]');
           } else if (jqXHR.status == 500) {
               console.log('Internal Server Error [500].');
           } else if (exception === 'parsererror') {
               console.log('Requested JSON parse failed.');
           } else if (exception === 'timeout') {
               alert('Servidor no Responde');
			   location.href='../salir.php';
           } else if (exception === 'abort') {
               console.log('Ajax request aborted.');
           } else {
               console.log('Uncaught Error: ' + jqXHR.responseText);
           }

		}, 
		 complete:function(jqXHR, textStatus){if(textStatus==='success'){}},
			timeout:90000
		});



}


</script>
<style type="text/css">
.txt_cant{ width:40px; border:#000 solid 1px; text-align:right;}
.txt_pvp{ width:40px; border:#000 solid 1px; text-align:right;}
.txt_sub{ width:40px; border:#000 solid 1px; text-align:right;}
</style>
</head>

<body>
<div align="center">
<?php require('../cfg/res.php');
$car1_sql='select *from tvir1 where enti="'.$_SESSION['user_vcrx_id'].'"';
$car1_res=mysql_query($car1_sql,$link_vcrx);
$car1_idn=mysql_result($car1_res,0,'idnum1');

$car2_sql='select *,(pvp1*cant)as sub from tvir2 where idnum2="'.$car1_idn.'"';
$car2_res=mysql_query($car2_sql,$link_vcrx);


?>
<table cellpadding="4">
<tr>
	<th></th>
    <th>IITEM</th>
    <th>CANT</th>
    <th>PRECIO</th>
    <th>SUB</th>
    <th></th>
</tr>
<?php while($car2_r=mysql_fetch_assoc($car2_res)){?>
	<tr>
    <td><input type="button" value="Borrar" /></td>
    <td><?php echo $car2_r['item'];?></td>
    <td><input type="text" class="txt_cant" value="<?php echo number_format($car2_r['cant'],0);?>" id="item_<?php echo $car2_r['iddet'];?>"  /></td>
    <td><input type="text" class="txt_pvp"  value="<?php echo number_format($car2_r['pvp1'],2);?>" /></td>
    <td><input type="text" class="txt_sub"  value="<?php echo number_format($car2_r['sub'],2);?>"  /></td>
    <td>
	    <input type="hidden" class="txt_iva"  value="<?php echo $car2_r['iva'];?>"  />
    	<input type="button" onclick="_agregar('<?php echo $car2_r['iddet'];?>')" value="Actualizar" />	
    </td>
	</tr>
<?php } 
$car2_sql='select sum(pvp1*cant)as total from tvir2 where idnum2="'.$car1_idn.'"';
$car2_res=mysql_query($car2_sql,$link_vcrx);
$car2_tot=mysql_result($car2_res,0,'total');


?>
<tr>
	<td></td>
	<td colspan="3" align="right">Subtotal</td>
    <td><span id="subtotal"><?php echo number_format($car2_tot,2);?></span></td>
    <td></td>
</tr>

<tr>
	<td></td>
	<td colspan="3" align="right">IVA</td>
    <td><span id="iva"><?php //echo number_format($car2_tot,2);?></span></td>
    <td></td>
</tr>

</table>
<div id="err_msg" style="height:30px;"></div>


 <?php $adr_sql='select *from adrs0 where adrs_enti="'.$_SESSION['user_vcrx_id'].'"  and adrs_status!="X" order by adrs_defa desc, adrs_cdad,adrs_calle1';
	  $adr_res=mysql_query($adr_sql,$link_vcrx);
	  if(mysql_num_rows($adr_res)!=0){ $adr_aux=0;?>
      <div align="left">Escoja Dirección de Envio :
<select id="adr_dir" onchange="_adr_dato(this.value);" >		
<?php while($adr_r=mysql_fetch_assoc($adr_res)){ $adr_aux++;?>
		<option value="<?php echo $adr_r['adrs_codigo'];?>" <?php if($adr_r['adrs_defa']=='1'){echo 'selected="selected"';} ?>   ><?php echo $adr_aux.'-'.$adr_r['adrs_calle1'];?></option>	
<?php }?>
	  </select></div>        
<?php } ?>        


<?php  
$dper_sql='select *from adrs00 where adrs_enti="'.$_SESSION['user_vcrx_id'].'"  and adrs_status!="X" and adrs_defa="1"';

$dper_res=mysql_query($dper_sql,$link_vcrx);
$dper_nom='N/A';
$dper_dir='N/A';
$dper_ciu='N/A';
$dper_pro='N/A';
$dper_tel='N/A';
$dper_ema='N/A';
$dper_con='N/A';

if(mysql_num_rows($dper_res)!=0)
{
$dper_nom=mysql_result($dper_res,0,'enti_descri');
$dper_dir=mysql_result($dper_res,0,'adrs_calle1').mysql_result($dper_res,0,'adrs_calle2').mysql_result($dper_res,0,'adrs_callen').mysql_result($dper_res,0,'adrs_calle3');
$dper_ciu=mysql_result($dper_res,0,'cdad_descri');
$dper_pro=mysql_result($dper_res,0,'prvc_descri');
$dper_tel=mysql_result($dper_res,0,'adrs_tlfn').'-'.mysql_result($dper_res,0,'adrs_ext');
$dper_ema=mysql_result($dper_res,0,'adrs_email');
$dper_con=mysql_result($dper_res,0,'adrs_contacto');

}
?>		
<table width="100%">
<tr align="left"><td>NOMBRE: </td><td><span id="nom_lbl" ><?php echo $dper_nom;?></span></td></tr>
<tr align="left"><td>DIRECCION: </td><td><span id="dir_lbl" ><?php echo $dper_dir;?></span></td></tr>
<tr align="left"><td>CIUDAD: </td><td><span id="ciu_lbl" ><?php echo $dper_pro.'-'.$dper_ciu;?></span></td></tr>
<tr align="left"><td>TELEFONO: </td><td><span id="tel_lbl" ><?php echo $dper_tel;?></span></td></tr>
<tr align="left"><td>EMAIL: </td><td><span id="ema_lbl" ><?php echo $dper_ema;?></span></td></tr>
<tr align="left"><td>CONTACTO: </td><td><span id="con_lbl" ><?php echo $dper_con;?></span></td></tr>
</table> 
<!-- <a href="../lib/dper1.php?idx=<?php echo $_SESSION['user_vcrx_id'];?>" target="_blank" style="color:#F00; text-decoration:none;">Si sus datos personales son incorrectos, Actualice sus Datos Aqui.!</a> -->
<a href="../lib/nenti.php?idx=<?php echo $_SESSION['user_vcrx_id'];?>" target="_blank" style="color:#F00; font-weight:bolder; text-decoration:none;">Si sus datos personales son incorrectos, Actualice sus Datos Aqui.!</a>
</div>



<div><a href="ces3.php"><span>Proceder Compra</span></a></div>
</div>
</body>
</html>