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;
	jF("TxtCod");
}
//
function TxtCod_OnKeyUp(){
	if(document.getElementById("ChN_BAR").checked==false){
		document.getElementById('TxtHora').value=GetHora();
	}else{
		document.getElementById('TxtHora').value=0;
	}
}
//
function fPartida(){
	var Alm=gv("CboAlm");
	var stru="aj.php?Cmd=R05&P1="+Alm;
	try{
	ajax=CreateAJAX();
	ajax.open("GET",stru);
	ajax.onreadystatechange=function() {
	   if (ajax.readyState==4) {
		   var Rpt = (ajax.responseText);
		   document.getElementById("TxtPar").value=Rpt;		   
	   }
	}
    ajax.send(null);
	}
	catch(err){
	}
}
//
function VUELTO(){
	var StrTC=document.getElementById("TxtTC").value;
	var StrMon=document.getElementById("CboMon").value;
	var StrTot=document.getElementById("T_TOH").value;
	var StrPag=document.getElementById("TxtPagarCon").value;
	if(StrPag.trim().length==0){
		StrPag="0";
	}
	StrTC=parseFloat(StrTC);
	StrTot=parseFloat(StrTot);
	StrPag=parseFloat(StrPag);
	if(StrMon=="DOLARES"){
		StrPag=StrPag * StrTC;
		StrPag=StrPag.toFixed(2);
	}
	var Vuelto=parseFloat("0");
	if(StrTot>StrPag){
		document.getElementById("TxtVuelto").value="0.0";
		return;
	}else{
		Vuelto=StrPag - StrTot;
	}
	Vuelto=Vuelto.toFixed(2);
	document.getElementById("TxtVuelto").value=Vuelto;
}
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 TDP(){
	var StrTdp=document.getElementById("CboTdp").value;
	document.getElementById("CboMon").value="SOLES"
	if(StrTdp=="CONTADO"){
document.getElementById("TxtPagarCon").readOnly=false;
document.getElementById("TxtPagarCon").value=document.getElementById("T_TO").value;		
document.getElementById("TxtVuelto").value="0.0";
document.getElementById("CboMon").disabled=false;
	}else{
		document.getElementById("TxtPagarCon").readOnly=true;
		document.getElementById("TxtPagarCon").value="0.0";
		document.getElementById("CboMon").disabled=true;
	}
	
}
function AutoNumeracion(estado){
	document.getElementById("TxtSer").value="";
	document.getElementById("TxtNum").value="";
	var Plom=document.getElementById("TxtTC").style.backgroundColor;
	var Blan=document.getElementById("TxtCod").style.backgroundColor;
	if(estado>0){
		document.getElementById("TxtSer").readOnly=true;
		document.getElementById("TxtNum").readOnly=true;
		document.getElementById("TxtSer").style.backgroundColor=Plom;
		document.getElementById("TxtNum").style.backgroundColor=Plom;
		if(estado==1){
		document.getElementById("TxtFec").focus();
		}
	}else{
		document.getElementById("TxtSer").readOnly=false;
		document.getElementById("TxtNum").readOnly=false;
		document.getElementById("TxtSer").style.backgroundColor=Blan;
		document.getElementById("TxtNum").style.backgroundColor=Blan;
		document.getElementById("TxtSer").focus();
	}
}
//**************************************************************************
function LoadRef(){
	var dr=gv("CboComR");
	var ser=gv("TxtSerR");
	var num=gv("TxtNumR");
	if(jL(dr)==0){
		alert("Seleccione el tipo de documento de referencia");
		document.getElementById("CboComR").focus();
		return;
	}
	if(ser.trim().length==0){
		alert("Ingrese la serie");
		document.getElementById("TxtSerR").focus();
		return;
	}
	if(num.trim().length==0){
		alert("Ingrese el numero");
		document.getElementById("TxtNumR").focus();
		return;
	}
	document.getElementById("TxtAc").value="LOAD_REF";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function CboRef(){
	var Num=document.getElementById("TxtNumR").value;
	var dr=document.getElementById("CboComR").value;
	
	var Plom=document.getElementById("TxtTC").style.backgroundColor;
	var Blan=document.getElementById("TxtCod").style.backgroundColor;
	if(dr.trim().length==0){
		document.getElementById("TxtSerR").style.backgroundColor=Plom;
		document.getElementById("TxtSerR").readOnly=true;		
		document.getElementById("TxtNumR").style.backgroundColor=Plom;
		document.getElementById("TxtNumR").readOnly=true;		
		return;
	}
	if(dr=="TRANSFERENCIA"){
		document.getElementById("TxtSerR").style.backgroundColor=Plom;
		document.getElementById("TxtSerR").value="000";
		document.getElementById("TxtSerR").readOnly=true;
		document.getElementById("TxtNumR").readOnly=false;
		document.getElementById("TxtNumR").style.backgroundColor=Blan;
	}else{
		document.getElementById("TxtNumR").style.backgroundColor=Blan;
		document.getElementById("TxtNumR").readOnly=false;
		document.getElementById("TxtSerR").style.backgroundColor=Blan;
		document.getElementById("TxtSerR").readOnly=false;
	}	
}
function fCls(){
	document.getElementById("TxtIdCliente").value="0";
	document.getElementById("TxtCliente").value="";
	document.getElementById("TxtRuc").value="";
}
//**************************************************************************
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("TxtMarcas");
	var Alm=gv("CboAlm");
	//
	Cod=Cod.replace("+", "@");
	Des=Des.replace("+", "@");
	//
	if(jL(Des)>0 || jL(Mar)>0 || jL(Cod)>0){
		DivDatos = document.getElementById("TAB_AJAX");
		DivCaption = document.getElementById("TAB_CAPTION");
		ajax=CreateAJAX();
		var StrU="ajax_funciones.php?Cmd=FILTRAR_GUIA&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){
							StrDatos = replaceAll(StrDatos,"@", "<tr class=\"zz\" ");
							StrDatos = replaceAll(StrDatos,"#", "</tr>");
							StrDatos = replaceAll(StrDatos,";", "</td>");
							StrDatos = replaceAll(StrDatos,"%", "<td>");
							StrDatos = replaceAll(StrDatos,":", "ondblclick=");	
							//
							DivDatos.innerHTML = StrDatos;
							DivCaption.style.visibility="visible";
							DivDatos.style.visibility="visible";
							document.getElementById("TAB_AJAX").scrollTop=0;
							//
							PINTAR_ZZ();
						}else{
							DivCaption.style.visibility="hidden";
							DivDatos.style.visibility="hidden";	
						}
			   }
		}
		ajax.send(null);
	}
}

//**************************************************************************
function PINTAR_ZZ(){
	$('.zz').focus(function() {	
		this.style.backgroundColor="#FFCC00";
	}).blur(function() {
	   this.style.backgroundColor="white";
	});
	$('.zz').keydown(function(e) {
		if(e.which == 40) {
			var r=this.id;
			r=r.replace("z","");
			r=parseInt(r)+1;
			try {
				document.getElementById("z"+r).focus();
				e.preventDefault();
			} catch (e) {
				
			}
		}
		if(e.which == 38) {
			var r=this.id;
			r=r.replace("z","");
			r=parseInt(r)-1;
			if(r==0){
				r=1;
			}
			document.getElementById("z"+r).focus();
			e.preventDefault();
		}
		if(e.which == 13) {
			this.ondblclick();
			e.preventDefault();
		}
		if(e.which == 27) {
			CerrarDIV();			
			e.preventDefault();
			sv("TxtHora","0");
		}
	});	
}

//**************************************************************************
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("TxtMarcas").value="";
  				   document.getElementById("TxtPre").value="";				   
				   document.getElementById("TxtCant").value="";
				   document.getElementById("TxtImp").value="";
				   document.getElementById("TxtCod").value="";				   
				   alert("Codigo no registrado");
				   document.getElementById("TxtCod").focus();
				   }else{
document.getElementById("TxtCod").value=document.getElementById("Pro_TxtCod").value;
document.getElementById("TxtMarcas").value=document.getElementById("Pro_TxtMarca").value;
document.getElementById("TxtDes").value=document.getElementById("Pro_TxtDes").value;
document.getElementById("TxtPre").value=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>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("TxtDes").value="";
				   document.getElementById("TxtMarcas").value="";
				   document.getElementById("TxtCant").value="";
				   document.getElementById("TxtCod").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("TxtMarcas").value=document.getElementById("Pro_TxtMarca").value;
	document.getElementById("TxtDes").value=document.getElementById("Pro_TxtDes").value;
	document.getElementById("TxtCant").value="1";
	document.getElementById("TxtCant").focus();	
}
//
function fGetProdDatos(lpStrCod){
	if(lpStrCod.trim().length>0){
	DivDatos = document.getElementById("DivDatosProd");
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=VEN_GET_PROD_DATOS&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_TxtMarca").value="";
   				   document.getElementById("TxtDes").value="";
				   document.getElementById("TxtMarcas").value="";
				   document.getElementById("TxtCant").value="";
				   document.getElementById("TxtCod").value="";				   
				   alert("Codigo no registrado");
				   document.getElementById("TxtCod").focus();
				   }else{
document.getElementById("TxtMarcas").value=document.getElementById("Pro_TxtMarca").value;
document.getElementById("TxtDes").value=document.getElementById("Pro_TxtDes").value;
document.getElementById("TxtCant").value="1";
document.getElementById("TxtCant").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 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 Num=document.getElementById("TxtNum").value;
	var Ser=document.getElementById("TxtSer").value;
	var Par=document.getElementById("TxtPar").value;
	var DesRs=document.getElementById("TxtDesRs").value;
	var DesDir=document.getElementById("TxtDesDir").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(document.getElementById("ChN_AUTO").checked==false){
		if(Ser.trim().length==0){alert("Ingrese la serie del comprobante.");
			document.getElementById("TxtSer").focus();return;}
		if(Num.trim().length==0){alert("Ingrese el numero de comprobante");
			document.getElementById("TxtNum").focus();
			return;}
	}	
	if(Par.trim().length==0){alert("Ingrese el punto de partida");
		document.getElementById("TxtPar").focus();return;}
	if(DesRs.trim().length==0){alert("Ingrese la razon social del destinatario");
		document.getElementById("TxtDesRs").focus();return;}
	if(DesDir.trim().length==0){alert("Ingrese la direccion del destinatario");
		document.getElementById("TxtDesDir").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=V&P1="+cod;
	//alert(Z);
	ajax.open("GET",Z,false);	
	ajax.send(null);
	var StrHtml=ajax.responseText;
	return StrHtml.trim();
}

//**************************************************************************
function cDes(e){	
	var k;
	k=(document.all) ? e.keyCode :e.which;
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	if(k==40){
		if(DivDatos.style.visibility!="hidden"){
			document.getElementById("z1").focus();
			event.preventDefault();			
		}
		return true;
	}
	if(k==13){
		jF("TxtMarcas");		
		DivCaption.style.visibility="hidden";
		DivDatos.style.visibility="hidden";	
		event.preventDefault();
		return true;
	}
}
function cDesUP(e){	
	var k;
	k=(document.all) ? e.keyCode :e.which;	
	if(k==40 || k==27 || k==13){
		
	}else{
		sv('TxtHora',GetHora());
	}
}
//**************************************************************************



