String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
//**************************************************************************
function fFiltrar(){
	SM("F");
}
//
function ChN_BAR_OnChange(){
	document.getElementById('TxtHora').value=0;
}
//
function TxtCod_OnKeyUp(){
	if(document.getElementById("ChN_BAR").checked==false){
		document.getElementById('TxtHora').value=GetHora();
	}else{
		document.getElementById('TxtHora').value=0;
	}
}
//
function fCls(){
	sv("TxtCod","");
	sv("TxtDes","");
	sv("TxtPre","");
	sv("TxtCant","");
	sv("TxtImp","");
	jF("TxtCod");
}
//
function CalcularIMPORTE(){
	var Importe=jVal(gv("T_Importe"));
	var stt=0;
	var igv=0;
	var tot=0;	
	var uno=0;
	//	
	if(document.getElementById("ChN_INC").checked){
		stt=Importe;
		igv=stt * 0.18;
		igv=jVal(igv.toFixed(2));
		tot=stt +igv;
	}else{
		uno=Importe / 118;
		igv=uno * 18;
		igv=jVal(igv.toFixed(2));
		stt=Importe - igv;
		tot=stt+igv;
	}
	sv("T_ST",formato_numero(stt,2,"."));
	sv("T_IG",formato_numero(igv,2,"."));
	sv("T_TO",formato_numero(tot,2,"."));
	jFocus("ChN_BAR");
}
//**************************************************************************
function fImporte(){
	var Pre=jVal(gv("TxtPre"));
	var Can=jVal(gv("TxtCant"));
	var Importe=0;
	Importe=Pre * Can;
	Importe=Importe.toFixed(2);
	sv("TxtImp",Importe);
}
//**************************************************************************
function fKiller(lpIndex){
	sv("Pro_TxtId",lpIndex);
	SM("KILLER");
}
//**************************************************************************
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){
					
					//alert(StrDatos);
					
					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 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=gv("CboMon");
	var Tca=gv("TxtTC");
	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=jVal(Pre);Tca=jVal(Tca);
	if(Mon=="DOLARES"){
		Pre=Pre / Tca;
		Pre=Pre.toFixed(2);		
	}	
	return Pre;
}
//--------------------------------------------------------------------------
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=gv("Pro_TxtDes");
				   if(jL(Des)==0){
					   svv("Pro_TxtId,Pro_TxtDes,Pro_TxtCod,Pro_TxtMarca,Pro_TxtPre");
					   svv("TxtDes,TxtPre,TxtCant,TxtImp,TxtCod");
					   alert("Codigo no registrado");
					   jF("TxtCod");
				   }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(){
	sv("TxtCod",gv("Pro_TxtCod"));
	sv("TxtDes",gv("Pro_TxtDes"));	
	sv("TxtPre",PP(gv("Pro_TxtPre")));
	sv("TxtCant","1");
	sv("TxtImp",gv("TxtPre"));
	jF("TxtPre");
}
//**************************************************************************
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(){
	SM("C");
}
//**************************************************************************
function fAgregar(){
	var Pro_Id=gv("Pro_TxtId");
	var Pro_Cod=gv("Pro_TxtCod");
	var Pro_Des=gv("Pro_TxtDes");	
	var Cod=gv("TxtCod");
	var Des=gv("TxtDes");
	var Pre=gv("TxtPre");
	var Can=gv("TxtCant");	
	Pro_Id=Pro_Id.toUpperCase();
	Pro_Cod=Pro_Cod.toUpperCase();
	Pro_Des=Pro_Des.toUpperCase();	
	Cod=Cod.toUpperCase();
	Des=Des.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;
	}
	SM("ADD");
}
//**************************************************************************
function fGuardar(){
	var Alm=gv("CboAlm");
	var Fec=gv("TxtFec");
	var Com=gv("CboCom");
	var Num=gv("TxtNum");
	var Mon=gv("CboMon");
	var Pro=gv("CboPro");
	var CboTdp=gv("CboTdp");
	//
	if(jL(Alm)==0){
		alert("Seleccione un almacen");
		document.getElementById("CboAlm").focus();
		return;
	}
	if(jL(Fec)==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(jL(Com)==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(CboTdp.trim().length==0){
		alert("Seleccione Tipo de pago");
		document.getElementById("CboTdp").focus();
		return;
	}
	//
	if(gv("CmdG")=="Guardar"){
		var Rpta = confirm("Esta seguro que desea Guardar?")		
		if (Rpta==false){
			return;
		}
		SM("G");
	}else{
		var Rpta = confirm("Esta seguro que desea modificar?")		
		if (Rpta==false){
			return;
		}
		SM("M");
	}
}
/**************************************************************************
	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();
}
//**************************************************************************
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("TxtPre");		
		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());
	}
}
//
function InsPro(){
	Ow("proveedores_p.php",470*ZO(),220*ZO());
}
//
function fProveedores(){
	R=fLista("pro_nombre","proveedores","id_proveedor>=0");
	FCBO('CboPro',R);
	jF("CboPro");
}
