String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
//**************************************************************************
function fFiltrar(){
	document.getElementById("TxtAc").value="F";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function fKiller(lpIndex){
	document.getElementById("Pro_TxtId").value=lpIndex;
	document.getElementById("TxtAc").value="KILLER";
	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 fGetTabla(){
var Cod= gv("TxtCod");
var Des= gv("TxtDes");
var Mar= gv("TxtMarca");
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_TRASFERENCIA&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 CerrarDIV(){
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	DivCaption.style.visibility="hidden";
	DivDatos.style.visibility="hidden";	
	document.getElementById("TxtDes").focus();
}

//**************************************************************************
function z(lpStrId){
	fGetProdDatos_Id(lpStrId);
}
//**************************************************************************
function fGetProdDatos_Id(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("Pro_TxtPre").value="";
   				   document.getElementById("TxtDes").value="";  				  	   
				   document.getElementById("TxtCant").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("TxtDes").value=document.getElementById("Pro_TxtDes").value;
	document.getElementById("TxtCod").value=document.getElementById("Pro_TxtCod").value;
	document.getElementById("TxtMarca").value=document.getElementById("Pro_TxtMarca").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=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_TxtPre").value="";
				   document.getElementById("Pro_TxtMarca").value="";
   				   document.getElementById("TxtDes").value="";
				   document.getElementById("TxtCant").value="";
				   document.getElementById("TxtCod").value="";				   
				   document.getElementById("TxtMarca").value="";				   
				   alert("Codigo no registrado");
				   document.getElementById("TxtCod").focus();
				   }else{
document.getElementById("TxtMarca").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 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 AlmD=document.getElementById("CboAlmD").value;
	var Fec=document.getElementById("TxtFec").value;
	var Num=document.getElementById("TxtNum").value;

	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(Alm.trim().length==0){
		alert("Seleccione el almacen de origen");
		document.getElementById("CboAlm").focus();
		return;
	}

	if(AlmD.trim().length==0){
		alert("Seleccione el almacen de destino");
		document.getElementById("CboAlmD").focus();
		return;
	}	
	if(Alm==AlmD){
		alert("El almacen de destino debe ser distinto al almacen de origen");
		document.getElementById("CboAlmD").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=T&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("TxtMarca");		
		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());
	}
}
//**************************************************************************

$(document).keydown(function(e){
	var AT=gv("TxtAT");
	
	if(e.which==71 && e.altKey){		
		if(AT=="1"){	
		document.getElementById("CmdG").click();
		return;
		}
	}
	
	if(e.which==78 && e.altKey){
		if(AT=="1"){
		document.getElementById("CmdC").click();
		return;
		}
	}
	
	if(e.which==75 && e.altKey){
		if(AT=="1"){	
		document.getElementById("CmdE").click();
		return;
		}
	}

	if(e.which==66 && e.altKey){
		if(AT=="1"){	
		document.getElementById("CmdB").click();
		return;
		}
	}

	if(e.which==77 && e.altKey){
		if(AT=="1"){
		document.getElementById("CmdZ").click();
		return;
		}
	}
  
});