String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
//**************************************************************************
function fFiltrar(){
	document.getElementById("TxtAc").value="F";
	document.getElementById("Frm").submit();
}
//
function ChN_BAR_OnChange(){
	document.getElementById('TxtHora').value=0;
	jFocus("TxtCod");
}
//
function TxtCod_OnKeyUp(){
	if(document.getElementById("ChN_BAR").checked==false){
		document.getElementById('TxtHora').value=GetHora();
	}else{
		document.getElementById('TxtHora').value=0;
	}
}
function CalcularIMPORTE(){
	var Importe=document.getElementById("T_Importe").value;
	var stt=0;
	var igv=0;
	var tot=0;	
	var uno=0;
	if(Importe.trim().length==0) Importe="0";
	Importe=parseFloat(Importe);
	if(document.getElementById("ChN_INC").checked){
		stt=Importe;
		igv=stt * 0.18;
		igv=igv.toFixed(2);
		tot=parseFloat(stt) + parseFloat(igv);
	}else{
		uno=Importe / 118;
		//uno=uno.toFixed(2);
		igv=uno * 18;
		igv=igv.toFixed(2);
		stt=parseFloat(Importe) - parseFloat(igv);
		tot=parseFloat(stt)+parseFloat(igv);
	}
	document.getElementById("T_ST").value=formato_numero(stt,2,".");
	document.getElementById("T_IG").value=formato_numero(igv,2,".");
	document.getElementById("T_TO").value=formato_numero(tot,2,".");	
}
//**************************************************************************
function fImporte(){
	var Pre=document.getElementById("TxtPre").value;
	var Can=document.getElementById("TxtCant").value;
	var Importe=parseFloat("0");
	if(Pre.trim().length==0)Pre="0";
	if(Can.trim().length==0)Can="0";
	Pre=parseFloat(Pre);
	Can=parseFloat(Can);
	Importe=Pre * Can;
	Importe=Importe.toFixed(2);
	document.getElementById("TxtImp").value=Importe;
}
//**************************************************************************
function fKiller(lpIndex){
	document.getElementById("Pro_TxtId").value=lpIndex;
	document.getElementById("TxtAc").value="KILLER";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function fGetTabla(){
var Cod = gv("TxtCod");
var Des = gv("TxtDes");
var Mar = "";//gv("TxtMarca");
var Alm = gv("CboAlm");
//
Cod=Cod.replace("+", "@");
Des=Des.replace("+", "@");
//
if(Des.trim().length>0 || Cod.trim().length>0 || Mar.trim().length>0){
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	ajax=CreateAJAX();
var StrU="ajax_funciones.php?Cmd=FILTRAR_COMPRAS&TxtPar="+Des+"&TxtPar2="+Mar+"&TxtPar3="+Cod+"&TxtPar4="+Alm;
	ajax.open("GET", StrU);	
	ajax.onreadystatechange=function() {
	   if (ajax.readyState==4) {
			var StrDatos=ajax.responseText;
			if(StrDatos.trim().length>0){			  
			   DivDatos.innerHTML = StrDatos;
			   DivCaption.style.visibility="visible";
			   DivDatos.style.visibility="visible";	
			}else{
				DivCaption.style.visibility="hidden";
				DivDatos.style.visibility="hidden";	
			}
	   }
	}
	ajax.send(null);
}
}
function CerrarDIV(){
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	DivCaption.style.visibility="hidden";
	DivDatos.style.visibility="hidden";	
	document.getElementById("TxtDes").focus();
}
//**************************************************************************
function PP(Pre){
	var Mon=document.getElementById("CboMon").value;
	var Tca=document.getElementById("TxtTC").value;
	Mon=Mon.trim();Pre=Pre.trim();Tca=Tca.trim();
	if(Mon.length==0)Mon="SOLES";
	if(Pre.length==0)Pre="0";
	if(Tca.length==0)Tca="1";
	Pre=parseFloat(Pre);Tca=parseFloat(Tca);
	if(Mon=="DOLARES"){
		Pre=Pre / Tca;Pre=Pre.toFixed(2);		
	}	
	return Pre;
}
//**************************************************************************
function fGetProdDatos_Id(lpStrId){
	if(lpStrId.trim().length>0){
	DivDatos = document.getElementById("DivDatosProd");
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=COMPRAS_GET_PROD_DATOS_X_ID&TxtPar="+lpStrId);	
	ajax.onreadystatechange=function() {
		   if (ajax.readyState==4) {			  
				   DivDatos.innerHTML = ajax.responseText
				   var Des=document.getElementById("Pro_TxtDes").value;
				   if(Des.trim().length==0){
   				   document.getElementById("Pro_TxtId").value="";
				   document.getElementById("Pro_TxtDes").value="";
				   document.getElementById("Pro_TxtCod").value="";
				   document.getElementById("Pro_TxtMarca").value="";
				   document.getElementById("Pro_TxtPre").value="";
   				   document.getElementById("TxtDes").value="";
  				   document.getElementById("TxtPre").value="";				   
				   document.getElementById("TxtCant").value="";
				   document.getElementById("TxtImp").value="";
				   document.getElementById("TxtCod").value="";				   
				   //document.getElementById("TxtMarca").value="";				   
				   alert("Codigo no registrado");
				   document.getElementById("TxtCod").focus();
				   }else{
document.getElementById("TxtCod").value=document.getElementById("Pro_TxtCod").value;
//document.getElementById("TxtMarca").value=document.getElementById("Pro_TxtMarca").value;
document.getElementById("TxtDes").value=document.getElementById("Pro_TxtDes").value;
document.getElementById("TxtPre").value=PP(document.getElementById("Pro_TxtPre").value);
document.getElementById("TxtCant").value="1";
document.getElementById("TxtImp").value=document.getElementById("TxtPre").value;
document.getElementById("TxtPre").focus();				   
				   }
				   DivDatos = document.getElementById("TAB_AJAX");
				   DivCaption = document.getElementById("TAB_CAPTION");
				   DivCaption.style.visibility="hidden";
				   DivDatos.style.visibility="hidden";		
				   		   
		   }
	}
    ajax.send(null);
	}
}
//--------------------------------------------------------------------------
function z(lpStrId){
	if(lpStrId.trim().length>0){
	DivDatos = document.getElementById("DivDatosProd");
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=COMPRAS_GET_PROD_DATOS_X_ID&TxtPar="+lpStrId);	
	ajax.onreadystatechange=function() {
		   if (ajax.readyState==4) {			  
				   DivDatos.innerHTML = ajax.responseText
				   var Des=document.getElementById("Pro_TxtDes").value;
				   if(Des.trim().length==0){
   				   document.getElementById("Pro_TxtId").value="";
				   document.getElementById("Pro_TxtDes").value="";
				   document.getElementById("Pro_TxtCod").value="";
				   document.getElementById("Pro_TxtMarca").value="";
				   document.getElementById("Pro_TxtPre").value="";
   				   document.getElementById("TxtDes").value="";
  				   document.getElementById("TxtPre").value="";				   
				   document.getElementById("TxtCant").value="";
				   document.getElementById("TxtImp").value="";
				   document.getElementById("TxtCod").value="";				   
				   //document.getElementById("TxtMarca").value="";				   
				   alert("Codigo no registrado");
				   document.getElementById("TxtCod").focus();
				   }else{
				   		SetValores();
				   }
				   DivDatos = document.getElementById("TAB_AJAX");
				   DivCaption = document.getElementById("TAB_CAPTION");
				   DivCaption.style.visibility="hidden";
				   DivDatos.style.visibility="hidden";		
				   		   
		   }
	}
    ajax.send(null);
	}
}
function SetValores(){
	document.getElementById("TxtCod").value=document.getElementById("Pro_TxtCod").value;
	//document.getElementById("TxtMarca").value=document.getElementById("Pro_TxtMarca").value;
	document.getElementById("TxtDes").value=document.getElementById("Pro_TxtDes").value;
	document.getElementById("TxtPre").value=PP(document.getElementById("Pro_TxtPre").value);
	document.getElementById("TxtCant").value="1";
	document.getElementById("TxtImp").value=document.getElementById("TxtPre").value;
	document.getElementById("TxtPre").focus();
}
//**************************************************************************
function fGetProdDatos(lpStrCod){
	if(lpStrCod.trim().length>0){
	DivDatos = document.getElementById("DivDatosProd");
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=GET_PROD_DATOS_C&TxtPar="+lpStrCod);	
	ajax.onreadystatechange=function() {
		   if (ajax.readyState==4) {			  
				   DivDatos.innerHTML = ajax.responseText
				   var Des=document.getElementById("Pro_TxtDes").value;
				   if(Des.trim().length==0){
   				   document.getElementById("Pro_TxtId").value="";
				   document.getElementById("Pro_TxtDes").value="";
				   document.getElementById("Pro_TxtPre").value="";
				   document.getElementById("Pro_TxtMarca").value="";
   				   document.getElementById("TxtDes").value="";
  				   document.getElementById("TxtPre").value="";				   
				   document.getElementById("TxtCant").value="";
				   document.getElementById("TxtImp").value="";
				   document.getElementById("TxtCod").value="";	
				   //document.getElementById("TxtMarca").value="";				   
				   alert("Codigo no registrado");
				   document.getElementById("TxtCod").focus();
				   }else{
var Mon=document.getElementById("CboMon").value;
var Pre=document.getElementById("Pro_TxtPre").value;
var Tca=document.getElementById("TxtTC").value;
document.getElementById("TxtDes").value=document.getElementById("Pro_TxtDes").value;
//document.getElementById("TxtMarca").value=document.getElementById("Pro_TxtMarca").value;
document.getElementById("TxtPre").value=PP(document.getElementById("Pro_TxtPre").value);
document.getElementById("TxtCant").value="1";
document.getElementById("TxtImp").value=document.getElementById("TxtPre").value;
document.getElementById("TxtPre").focus();
				   }
				   
		   }
	}
    ajax.send(null);
	}
}
//**************************************************************************
function fGetRuc(lpStrPro,Obj){
	var StrProveedor=lpStrPro;
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=GET_RUC&TxtPar="+StrProveedor);	
	ajax.onreadystatechange=function() {
		   if (ajax.readyState==4) {			   	   
				   document.getElementById(Obj).value=ajax.responseText;
				   Obj=Obj+"_";
				   document.getElementById(Obj).value=ajax.responseText;
		   }
	}
    ajax.send(null);
}
//**************************************************************************
function fGetProveedor(lpStrRuc,Obj){
	var StrRucH=document.getElementById("TxtRuc_").value;
	var StrRuc=lpStrRuc;
	if(StrRuc!=StrRucH){
		ajax=CreateAJAX();
		ajax.open("GET", "ajax_funciones.php?Cmd=GET_PROVEEDOR&TxtPar="+StrRuc);	
		ajax.onreadystatechange=function() {
			   if (ajax.readyState==4) {
					   var StrPro=ajax.responseText;
					   if(StrPro.trim().length==0){
						   	alert("Numero de ruc no registrado");
						   	document.getElementById(Obj).value="";
						   	document.getElementById(Obj).focus();
					   }else{
							document.getElementById(Obj).value=StrPro;
					   }
			   }
		}
		ajax.send(null);
	}

}
//**************************************************************************
function fCancelarPHP(){
	document.getElementById("TxtAc").value="C";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function fAgregar(){
	var Pro_Id=document.getElementById("Pro_TxtId").value;
	var Pro_Cod=document.getElementById("Pro_TxtCod").value;
	var Pro_Des=document.getElementById("Pro_TxtDes").value;
	
	var Cod=document.getElementById("TxtCod").value;
	var Des=document.getElementById("TxtDes").value;
	var Pre=document.getElementById("TxtPre").value;
	var Can=document.getElementById("TxtCant").value;
	
	Pro_Id=Pro_Id.trim().toUpperCase();
	Pro_Cod=Pro_Cod.trim().toUpperCase();
	Pro_Des=Pro_Des.trim().toUpperCase();
	
	Cod=Cod.trim().toUpperCase();
	Des=Des.trim().toUpperCase();
	
	if(Cod!=Pro_Cod){
		alert("El codigo del producto ha sido alterado");
		document.getElementById("TxtCod").focus();
		return;
	}
	if(Des!=Pro_Des){
		alert("La descripcion del producto ha sido alterada");
		document.getElementById("TxtDes").focus();
		return;
	}
	document.getElementById("TxtAc").value="ADD";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function fGuardar(){
	var Alm=document.getElementById("CboAlm").value;
	var Fec=document.getElementById("TxtFec").value;
	var Com=document.getElementById("CboCom").value;
	var Num=document.getElementById("TxtNum").value;
	var Mon=document.getElementById("CboMon").value;
	var Pro=document.getElementById("CboPro").value;
	
	if(Alm.trim().length==0){
		alert("Seleccione un almacen");
		document.getElementById("CboAlm").focus();
		return;
	}
	if(Fec.trim().length==0){
		alert("Ingrese una fecha");
		document.getElementById("TxtFec").focus();
		return;
	}
	if(EsFecha(Fec)==false){
		alert("Fecha no valida, la fecha debe ser ingresada segun el formato");
		document.getElementById("TxtFec").focus();
		return;
	}
	if(Com.trim().length==0){
		alert("Seleccione un comprobante");
		document.getElementById("CboCom").focus();
		return;
	}
	if(Num.trim().length==0){
		alert("Ingrese el numero de comprobante");
		document.getElementById("TxtNum").focus();
		return;
	}
	if(Mon.trim().length==0){
		alert("Seleccione la moneda correspondiente");
		document.getElementById("CboMon").focus();
		return;
	}
	if(Pro.trim().length==0){
		alert("Seleccione un proveedor");
		document.getElementById("CboPro").focus();
		return;
	}
	if(document.getElementById("CmdG").value=="Guardar"){
		var Rpta = confirm("Esta seguro que desea Guardar?")		
		if (Rpta==false){
			return;
		}
		document.getElementById("TxtAc").value="G";
	}else{
		var Rpta = confirm("Esta seguro que desea modificar?")		
		if (Rpta==false){
			return;
		}
		document.getElementById("TxtAc").value="M";
	}
	document.getElementById("Frm").submit();
}
/**************************************************************************
	onkeypress="return pulsar(event);"
**************************************************************************/
function pulsar(e) { 	
	var tecla = (document.all) ? e.keyCode :e.which; 
	var Obj=event.srcElement.name;	
	if(Obj=="TxtNom"){
		if(tecla==13){			
			document.getElementById('CmdG').click();			
		}
	}	
	return (tecla!=13); 
} 
//**************************************************************************
function LSoloTextoEx(e,Sig){	
	var k;
	k=(document.all) ? e.keyCode :e.which; 
	if(k==219){
		return false;
	}else{
		if(k==13){
			if(document.getElementById("ChN_BAR").checked==false){
				document.getElementById(Sig).focus();
			}else{
				if(jLen(document.getElementById("TxtCod").value)==0){
					document.getElementById(Sig).focus();
				}else{
					var R=AjBuscar();
					if(R=="NR"){						
						alert("Codigo de producto no registrado");
						jFocus("TxtCod");
						document.getElementById("TxtCod").select();
					}else{
						document.getElementById("DivDatosProd").innerHTML=R;
						SetValores();
						if(document.getElementById("ChN_AUT").checked==true){
							document.getElementById("CmdAdd").click();
							
						}
					}
				}
			}
			return true;
		}
		return true;
	}
}
//
function AjBuscar(){
	var cod=document.getElementById("TxtCod").value;
	cod=cod.replace("+", "@");		
	ajax=CreateAJAX();
	var Z="";
	Z="ajbar.php?C=C&P1="+cod;
	//alert(Z);
	ajax.open("GET",Z,false);	
	ajax.send(null);
	var StrHtml=ajax.responseText;
	return StrHtml.trim();
}
//