<?php
require('../cfg/res.php');
require('../../_lib/select.php');
//$res_item=mysql_query($sql_item,$link_vcrx);
?>
<script type="text/javascript">
var item_cod_m='';
var item_tipo_s='N';
<?php require('../../_js/busca_exsiste.js');?>
<?php include('../../_js/busca_maes3.js');?>
<?php include('../../_js/lista_maes3.js');?>
<?php include('../../_js/fecha_format.js');?>



function busca_mm()
{
	document.getElementById('marca_descri').innerHTML='';
	document.getElementById('modelo_descri').innerHTML='';
	var mark=document.getElementById('marca').value;
	if(mark.length==0)
	{
		return false;
	}
	
	var mode=document.getElementById('modelo').value;
	
	var bu='item_mrca="'+mark+'" and item_mdlo="'+mode+'"';
	
	
	var br=busca_exsiste('item0',bu);	
	if(br!='0')
	{
		document.getElementById('marca').innerHTML='*';
		document.getElementById('marca_descri').innerHTML='Marca y modelo ya existen';
	}

}

function nombre()
{
	var n1 = ''
            n1 = document.getElementById('descri').value;
	
	    n1 = n1.trim();
	
	if(n1.length < 3 || n1 == 'GENE/')
	{
		//document.getElementById('dper_err_n1').innerHTML='*Obligatorio';
		document.getElementById('descri_descri').value=document.getElementById('marca').value+'/'+document.getElementById('modelo').value;		
		if(n1=='' || n1==' ' || n1=='-' || n1=='NUEVO' || n1=='GENE/')
		{	
			
			document.getElementById('descri_descri').value='GENE'+'/'+document.getElementById('modelo').value;
		}
		
	}
}
function guarda_item(codxe,opcion)
{ 
	document.getElementById('item_error').innerHTML='';
	document.getElementById('marca_descri').innerHTML='';
	document.getElementById('ean_descri').innerHTML='';

  	var n1=document.getElementById('descri').value;
	var descri=document.getElementById('descri').value;
	var marca=document.getElementById('marca').value;
	var modelo=document.getElementById('modelo').value;
	var parte=document.getElementById('parte').value;
	var medida=document.getElementById('medida').value;
	var talla=document.getElementById('talla').value;
	var tamanio=document.getElementById('tamanio').value;
	var klidad=document.getElementById('klidad').value;
	var color=document.getElementById('color').value;
	var umedida=document.getElementById('umedida').value;
	var upack=document.getElementById('upack').value;
	var cxunidad=document.getElementById('cxunidad').value;
	var cgenerico=document.getElementById('cgenerico').value;
	var pais=document.getElementById('pais').value;
	var cinterno=document.getElementById('cinterno').value;
	var cean=document.getElementById('ean').value;
	var creten=document.getElementById('creten').value;
	var lsub=document.getElementById('lsub').value;
	var proveedor=document.getElementById('proveedor').value;
	var pcp=document.getElementById('pcp').value;
	var puni=document.getElementById('puni').value;
	var ice=document.getElementById('ice').value;
	var iva=document.getElementById('iva').value;
	var irbp=document.getElementById('irbp').value;
	var pvp=document.getElementById('pvp').value;

	


	
	//descri=descri.trim();
	if(descri.length==0)
	{
		document.getElementById('descri_descri').focus();
		document.getElementById('item_error').innerHTML='Descripcion del Item Obligatoria';
		return false;
	}

	//marca=marca.trim();
	if(marca=='-' || marca=='' || marca==' ' || marca=='NUEVO')
	{
		marca='GENE';
	}

	if(item_tipo_s=='N')
	{
		var bu='item_mrca="'+marca+'" and item_mdlo="'+modelo+'"';
		var br=busca_exsiste('item0',bu);
	
		if(br!='0')
		{
			document.getElementById('modelo_descri').innerHTML='*';
			document.getElementById('item_error').innerHTML='Marca y modelo ya existen';
			return false;
		}
	
	
		if(cean.length!=0)
		{
			bu='item_ean="'+cean+'"';
			br=busca_exsiste('item0',bu);
			
			if(br!='0')
			{
				document.getElementById('ean_descri').innerHTML='*';
				document.getElementById('item_error').innerHTML='EAN ya existe';
				return false;
			}
		}
		if(parte.length!=0)
		{
			bu='item_numpart="'+parte+'"';
			br=busca_exsiste('item0',bu);
			
			if(br!='0')
			{
				document.getElementById('parte_descri').innerHTML='*';
				document.getElementById('item_error').innerHTML='parte ya existe';
				return false;
			}
		}



	}


$.ajax({
		cache:false,
		url:'../lib/ditem1.php',
		datatype: "text",
	    async:false,
		type:'POST',
		data:{descri:descri,marca:marca,modelo:modelo,parte:parte,medida:medida,talla:talla,tamanio:tamanio,klidad:klidad,color:color,umedida:umedida,upack:upack,cxunidad:cxunidad,cgenerico:cgenerico,pais:pais,cinterno:cinterno,cean:cean,creten:creten,lsub:lsub,proveedor:proveedor,pcp:pcp,puni:puni,ice:ice,iva:iva,irbp:irbp,pvp:pvp,opcion:opcion,codigo:codxe}, 
    	beforeSend: function(jqXHR, textStatus)
		{
	
		}, 
		
		success:function(dato, textStatus, jqXHR)
		{
			console.log(dato);
			if(opcion=='N')
			{
				if(dato==='0')
				{
					alert('Ocurrio un error al registrar');
				}
				else
				{
					document.getElementById('codigo').value=dato;
					lista_item();
				}
			}
			else
			{
				if(dato==='0')
				{
					alert('Actualizo satisfactoriamente');
					lista_item();
				}
				else
				{
					alert('Error al actualizar');
				}
			
			}
			
						
		},
		error:function(jqXHR,textStatus,errorThrown )
		{
           if (jqXHR.status === 0) {
              document.getElementById('item_error').innerHTML='Not connect: Verify Network.';
           } else if (jqXHR.status == 404) {
               document.getElementById('item_error').innerHTML='Requested page not found [404]';
           } else if (jqXHR.status == 500) {
               document.getElementById('item_error').innerHTML='Internal Server Error [500].';
           } else if (exception === 'parsererror') {
               document.getElementById('item_error').innerHTML='Requested JSON parse failed.';
           } else if (exception === 'timeout') {
               //console.log('Time out error.');
			   document.getElementById('item_error').innerHTML='Servidor no Responde';
           } else if (exception === 'abort') {
               document.getElementById('item_error').innerHTML='Ajax request aborted.';
           } else {
               document.getElementById('item_error').innerHTML='Uncaught Error: ' + jqXHR.responseText;
           }

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



		
}


function edit_i(cod)
{
	item_cod_m=cod;
	item_tipo_s='M';
	limpia();
	$.ajax({
		url:'ditem2.php',
		async:false,
		type:'POST', 
		data:{codigo:cod},
		beforeSend: function(){},
		success: function(data){
				console.log(data);
				document.getElementById('item_lista_h').innerHTML=data;
				document.getElementById('codigo').value=document.getElementById('hdd_item_codigo').value;
				document.getElementById('proveedor').value=document.getElementById('hdd_item_provee').value;
				document.getElementById('descri').value=document.getElementById('hdd_item_descri').value;
				document.getElementById('marca').value=document.getElementById('hdd_item_marca').value;
				document.getElementById('modelo').value=document.getElementById('hdd_item_modelo').value;
				document.getElementById('parte').value=document.getElementById('hdd_item_numpart').value;
				document.getElementById('medida').value=document.getElementById('hdd_item_umdd').value;
				document.getElementById('talla').value=document.getElementById('hdd_item_tlla').value;
				document.getElementById('tamanio').value=document.getElementById('hdd_item_tmno').value;
				document.getElementById('klidad').value=document.getElementById('hdd_item_kldd').value;
				document.getElementById('color').value=document.getElementById('hdd_item_clor').value;
				document.getElementById('umedida').value=document.getElementById('hdd_item_umdd').value;
				document.getElementById('upack').value=document.getElementById('hdd_item_unixpack').value;
				document.getElementById('cxunidad').value=document.getElementById('hdd_item_codxunid').value;
				document.getElementById('cgenerico').value=document.getElementById('hdd_item_codigov').value;
				document.getElementById('pais').value=document.getElementById('hdd_item_pais').value;
				document.getElementById('cinterno').value=document.getElementById('hdd_item_at10').value;
				document.getElementById('lsub').value=document.getElementById('hdd_item_lsub').value;				
				
				document.getElementById('pcp').value=document.getElementById('hdd_item_pcp1').value;
				document.getElementById('puni').value=document.getElementById('hdd_item_puni').value;
				document.getElementById('ice').value=document.getElementById('hdd_item_ice').value;
				document.getElementById('iva').value=document.getElementById('hdd_item_iva').value;
				document.getElementById('irbp').value=document.getElementById('hdd_item_irbp').value;
				document.getElementById('pvp').value=document.getElementById('hdd_item_pvp1').value;
				
				
			},
		error: function(err_e){ console.log(err_e);},
		timeout: 60000
		
		
		
		});

}




function lista_item()
{
	
	
	
	$.ajax({
		cache:false,
		url:'ditem3.php',
		datatype: "text",
	    async:true,
		type:'POST',
		data:{}, 
    	beforeSend: function(jqXHR, textStatus)
		{
				document.getElementById('item_lista_n').innerHTML='Actualizando Datos...';
		}, 
		
		success:function(dato, textStatus, jqXHR)
		{
		
						document.getElementById('item_lista_n').innerHTML=dato;
		},
		error:function(jqXHR,textStatus,errorThrown )
		{
           if (jqXHR.status === 0) {
              document.getElementById('item_lista_n').innerHTML='Not connect: Verify Network.';
           } else if (jqXHR.status == 404) {
               document.getElementById('item_lista_n').innerHTML='Requested page not found [404]';
           } else if (jqXHR.status == 500) {
               document.getElementById('item_lista_n').innerHTML='Internal Server Error [500].';
           } else if (exception === 'parsererror') {
               document.getElementById('item_lista_n').innerHTML='Requested JSON parse failed.';
           } else if (exception === 'timeout') {
               document.getElementById('item_lista_n').innerHTML='Servidor no Responde';
           } else if (exception === 'abort') {
               document.getElementById('item_lista_n').innerHTML='Ajax request aborted.';
           } else {
               document.getElementById('item_lista_n').innerHTML='Uncaught Error: ' + jqXHR.responseText;
           }

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

}


function cancela_item()
{
	item_cod_m='';
	item_tipo_s='M';

/*
	document.getElementById('dper_txt_ci').disabled=false;
	document.getElementById('dper_btn_m').style.display='none';
	document.getElementById('dper_btn_c').style.display='none';
	document.getElementById('dper_btn_n').style.display='block';
*/
	limpia();

}
function limpia()
{
	var cas=document.getElementsByClassName('dper_txt').length;
	for(i=0;i<cas;i++)
	{
		document.getElementsByClassName('dper_txt').item(i).value='';
	}
}


function _marca(e)
{if(e.keyCode==13){busca_maes2('mrca',document.getElementById('marca').value,'marca','marca_descri','');}}

function _pais(e)
{if(e.keyCode==13){busca_maes2('pais',document.getElementById('pais').value,'pais','pais_descri','');}}

function _modelo(e)
{if(e.keyCode==13){busca_maes2('mdlo',document.getElementById('modelo').value,'modelo','modelo_descri','');}}

function _klidad(e)
{if(e.keyCode==13){busca_maes2('kldd',document.getElementById('klidad').value,'klidad','klidad_descri','');}}

function _color(e)
{if(e.keyCode==13){busca_maes2('clor',document.getElementById('color').value,'color','color_descri','');}}

function _proveedor(e)
{if(e.keyCode==13){busca_maes2('enti',document.getElementById('proveedor').value,'proveedor','proveedor_descri','');}}

function _lsub(e)
{if(e.keyCode==13){busca_maes2('lsub',document.getElementById('lsub').value,'lsub','lsub_descri','');}}

</script>
<div align="left">
  <table border="0" width="100%" cellpadding="0" cellspacing="0" style="font-size:12px;" >
<tr valign="top">
<td>
<!--+++++ -->
<table border="0" cellpadding="2" cellspacing="0" style=" font-size:12px; font-weight:bolder;">
<tr>
	<td><input type="button" value="CODIGO:" /></td>
    <td><input type="text" id="codigo" class="dper_txt" maxlength="45"   disabled="disabled" style="background:#FFF;"    /></td>
    <td><span id="codigo_descri"></span></td>
</tr>

<tr align="left">
	<td><input type="button" value="DESCRIPCION:" /></td>
    <td><input type="text" id="descri" class="dper_txt" maxlength="50"  onblur="nombre();"  /></td>
    <td width="200"><span id="descri_descri"></span></td>
</tr>
<tr>
	<td><input type="button" value="MARCA:" onClick="list_carga('mrca','marca','marca_descri');"  /></td>
    <td><input type="text" id="marca" onKeyDown="_marca(event);" onblur="busca_mm();nombre();" placeholder="marca"  class="dper_txt"  maxlength="20" /></td>
    <td><span id="marca_descri" class="dper_err"></span></td>
</tr>
<tr>
	<td><input type="button" value="MODELO:" onClick="list_carga('mdlo','modelo','modelo_descri');"  /></td>
    <td><input type="text" id="modelo" class="dper_txt" maxlength="20" onblur="busca_mm();nombre();"  onkeydown="_modelo(event);"  /></td>
    <td><span id="modelo_descri" class="dper_err"></span></td>
</tr>
<tr>
	<td><input type="button" value="No.PARTE:" /></td>
    <td><input type="text" id="parte" class="dper_txt" maxlength="20"   /></td>
    <td><span id="parte_descri" class="dper_err"></span></td>
</tr>

<tr>
	<td><input type="button" value="MEDIDAS:" /></td>
    <td><input type="text" id="medida" class="dper_txt" maxlength="20"   /></td>
    <td><span id="medida_descri" class="dper_err"></span></td>
</tr>

<tr>
	<td><input type="button" value="TALLA:" onClick="list_carga('tlla','talla','talla_descri');" /></td>
    <td><input type="text" id="talla" onKeyDown="_talla(event);" placeholder="talla" class="dper_txt" /> </td>
	<td><span id="talla_descri" class="dper_err"></span></td>
</tr>

<tr>
  <td><input type="button" value="TAMAÑO:" /></td>
  <td><input type="text" id="tamanio" class="dper_txt" maxlength="20"   /></td>
  <td><span id="tamanio_descri"></span></td>
</tr>
<tr>
  <td><input type="button" value="K-LIDAD:" onClick="list_carga('kldd','klidad','kldd_descri');" /></td>
  <td><input type="text" id="klidad" onKeyDown="_klidad(event);" placeholder="klidad"  /></td>
  <td><span id="klidad_descri"></span></td>
</tr>
<tr>
  <td><input type="button" value="COLOR:" onClick="list_carga('clor','color','color_descri');" /></td>
  <td><input type="text" id="color" onKeyDown="_color(event);" placeholder="color" class="dper_txt"  /></td>
  <td><span id="color_descri"></span></td>
</tr>
<tr>
  <td><input type="button" value="UNI. MEDIDA:" /></td>
  <td><input type="text" id="umedida" class="dper_txt" maxlength="20"   /></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td><input type="button" value="UNI.XPACK" /></td>
  <td><input type="text" id="upack" class="dper_txt" maxlength="20"   /></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td><input type="button" value="COD.XUNIDAD"></td>
  <td><input type="text" id="cxunidad" class="dper_txt" maxlength="20"   /></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td><input type="button" value="COD.GENERICO" /></td>
  <td><input type="text" id="cgenerico" class="dper_txt" maxlength="20"   /></td>
  <td>&nbsp;</td>
</tr>




</table>
<!--+++++ -->
</td>
<td>
<!--+++++ -->

<table border="0" cellpadding="2" cellspacing="0" style=" font-size:12px; font-weight:bolder;">

<tr>
	<td><input type="button" value="PAIS:" onClick="list_carga('pais','pais','pais_descri');" /></td>
    <td><input type="text" id="pais"  onkeypress="_pais(event);"  placeholder="pais" class="dper_txt" /></td>
    <td width="200"><span id="pais_descri" class="dper_err"></span></td>
</tr>
<tr>
	<td><input type="button" value="COD.EAN:" /></td>
    <td><input type="text" id="ean" class="dper_txt" maxlength="13"   /></td>
    <td><span id="ean_descri" class="dper_err"></span></td>
</tr>

<tr>
	<td><input type="button" value="COD.INTERNO:" /></td>
    <td><input type="text" id="cinterno" class="dper_txt" maxlength="20"   /></td>
    <td></td>
</tr>

<tr>
	<td><input type="button" value="COD.RETEN:" /></td>
    <td><input type="text" id="creten" class="dper_txt" maxlength="20"    /></td>
    <td width="50"><span id="creten_descri" class="dper_err"></span></td>
</tr>
<tr>
	<td><input type="button" value="LINEA.SUBL:" onClick="list_carga('lsub','lsub','lsub_descri');" /></td>
    <td><input type="text" id="lsub" class="dper_txt" maxlength="20" onkeydown="_lsub(event);"   /></td>
    <td><span id="lsub_descri" class="dper_err"></span></td>
</tr>

<tr>
	<td><input type="button" value="PROVEEDOR:" onClick="list_carga('enti','proveedor','proveedor_descri');" /></td>
    <td><input type="text" id="proveedor" class="dper_txt" maxlength="20"  onkeydown="_proveedor(event);"   /></td>
    <td><span id="proveedor_descri" class="dper_err"></span></td>
</tr>

<tr>
	<td><input type="button" value="P.C.P.:" /></td>
    <td><input type="text" id="pcp" class="dper_txt" maxlength="12" size="12"  style="text-align:right;" value="0.00"    /></td>
    <td><span id="pcp_descri" class="dper_err"></span></td>
</tr>

<tr>
	<td><input type="button" value="P.UNIT:" /></td>
    <td><input type="text" id="puni" class="dper_txt" maxlength="12" size="12"  style="text-align:right;" value="0.00"   /></td>
    <td><span id="puni_descri" class="dper_err"></span></td>
</tr>

<tr>
	<td><input type="button" value="I.C.E %" /></td>
    <td><input type="text" id="ice" class="dper_txt" maxlength="5" size="5"  style="text-align:right;" value="0.00"    /></td>
    <td><span id="ice_descri" class="dper_err"></span></td>
</tr>
<tr>
  <td><input type="button" value="I.V.A %" /></td>
  <td><input type="text" id="iva" class="dper_txt" maxlength="5" size="5"  style="text-align:right;" value="12.00"   /></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td><input type="button" value="I.R.B.P" /></td>
  <td><input type="text" id="irbp" class="dper_txt" maxlength="5" size="5"  style="text-align:right;" value="0.00"   /></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td><input type="button" value="P.V.P." /></td>
  <td><input type="text" id="pvp" class="dper_txt" maxlength="12" size="12"  style="text-align:right;" value="0.00"   /></td>
  <td>&nbsp;</td>
</tr>


</table>


<!--+++++ -->



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

<input type="button" value="Guardar" onClick="guarda_item(item_cod_m,item_tipo_s);" id="item_btn"/>
<input type="button" value="Limpiar" onClick="limpia();" id="limpia_btn"/>
<div id="item_error"></div>
<div id="item_lista_n"><?php //require('../../_lib/ditem3.php');?></div>
<div id="item_lista_h"></div>

<div id="lista_temporal_contenedor" style=" display:none; padding:0px; margin:0px; background: #666; opacity:0.5; width:100%; min-height: 100%; height:auto !important;position: fixed;top:0;left:0; z-index:1;" ></div>
<div id="lista_cargador" style=" display:none; background:#FFF;position: fixed; top:50px;z-index:10;" ><img src="../img/ajax-loader.gif" /></div>    
<div id="lista_temporal" style=" display:none; background:#FFF;position: fixed; top:50px;z-index:10; overflow-y:scroll; min-height:50px; max-height:400px; width:100%;" ><img src="../img/ajax-loader.gif" /></div>    
