String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
//
function fFiltrar(){
	SN("F");
}
//
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(jL(Des) + jL(Mar) + jL(Cod)>2){
		DivDatos = document.getElementById("TAB_AJAX");
		DivCaption = document.getElementById("TAB_CAPTION");
		ajax=CreateAJAX();
		var StrU="ajax_funciones.php?Cmd=AJ_PROD_FILTRAR&TxtPar="+Des+"&TxtPar3="+Mar+"&TxtPar4="+Cod+"&TxtPar2="+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 CerrarDIV(){
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	DivCaption.style.visibility="hidden";
	DivDatos.style.visibility="hidden";	
	document.getElementById("TxtDes").focus();
}
//**************************************************************************
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 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_TxtPre,Pro_TxtMarca");
						svv("TxtDes,TxtCant,TxtMarca,TxtCod");
						alert("Codigo no registrado");
					jF("TxtCod");
					}else{
						sv("TxtMarca",gv("Pro_TxtMarca"));
						sv("TxtCod",gv("Pro_TxtCod"));
						sv("TxtDes",gv("Pro_TxtDes"));
						sv("TxtCant","1");					   
						jF("TxtCant");	
					}
				DivDatos = document.getElementById("TAB_AJAX");
				DivCaption = document.getElementById("TAB_CAPTION");
				DivCaption.style.visibility="hidden";
				DivDatos.style.visibility="hidden";	
			}
		}
	ajax.send(null);
	}
}
//
function fCancelarPHP(){
	document.getElementById("TxtAc").value="C";
	document.getElementById("Frm").submit();
}
//**************************************************************************
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 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 Acc=gv("CboAcc");
	var Fec=gv("TxtFec");
	var Num=gv("TxtNum");
	//
	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(Alm)==0){
		alert("Seleccione el almacen");
		document.getElementById("CboAlm").focus();
		return;
	}	
	
	if(jL(Acc)==0){
		alert("Seleccione la accion correspondiente");
		document.getElementById("CboAcc").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 cCod(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){
		var cod=gv("TxtCod");
		if(cod==""){
			jF("TxtDes");
			return;
		}
		event.preventDefault();
		return true;
	}
}
function cCodUP(e){	
	var k;
	k=(document.all) ? e.keyCode :e.which;	
	if(k==40 || k==27 || k==13){
		
	}else{
		sv('TxtHora',GetHora());
	}
}
//**************************************************************************
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());
	}
}
//**************************************************************************
$(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;
		}
	}
  
});
//
function fCls(){
	CerrarDIV();
	sv("TxtCod","");
	sv("TxtDes","");
	sv("TxtMarca","");
	sv("TxtCant","");
	jF("TxtCod");
}