String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
//**************************************************************************
function fFiltrar(){
	document.getElementById("TxtAc").value="F";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function fCancelar(){
	document.getElementById("TxtCod").value="";
	document.getElementById("TxtDes").value="";
	document.getElementById("TxtMar").value="";
	document.getElementById("TxtCan").value="";
	document.getElementById("CmdE").disabled =true;
	document.getElementById("CmdG").value="Guardar";
	document.getElementById("TxtId").value="";
	document.getElementById("TxtAc").value="";
	document.getElementById("CboAlm").focus();
	
	TAB_A = document.getElementById("TAB_AJAX");
	TAB_C = document.getElementById("TAB_CAPTION");	
	TAB_C.style.visibility="hidden";
	TAB_A.style.visibility="hidden";
}

function fGuardar(){
	var T_ID=document.getElementById("T_ID").value;
	var Cod=document.getElementById("TxtCod").value;
	var Des=document.getElementById("TxtDes").value;
	var Can=document.getElementById("TxtCan").value;
	//
	if(jLen(Cod)==0 && jLen(Des)==0){
		alert("Seleccione un Producto");
		document.getElementById("TxtCod").focus();
		return;
	}
	/*if(jLen(T_ID)==0){
		alert("No se puede determinar el id del producto seleccionado.  Seleccione un producto");
		document.getElementById("TxtCod").focus();
		return;
	}*/
	if(Can.trim().length==0){
		alert("Ingrese Stock Minimo");
		document.getElementById("TxtCan").focus();
		return;
	}

	if(document.getElementById("CmdG").value=="Guardar"){
		document.getElementById("TxtAc").value="G";
	}else{
		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 fGetTabla(){
var Cod = gv("TxtCod");
var Des = gv("TxtDes");
var Mar = gv("TxtMar");
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=FIL_STOCK_MIN&TxtPar="+Alm+"&TxtPar2="+Cod+"&TxtPar3="+Des+"&TxtPar4="+Mar;	
	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 fGetTabla(){
	var Cod = gv("TxtCod");
	var Des = gv("TxtDes");
	var Mar = gv("TxtMar");
	var Alm = gv("CboAlm");

	if(Cod.trim().length>0 || Des.trim().length>0){		
		TAB_A = document.getElementById("TAB_AJAX");
		TAB_C = document.getElementById("TAB_CAPTION");	
		
		Cad=AX("F_STOCK_M",Alm,Cod,Des,Mar);
		
		TR=Cad.split("|");
		
			var	R="<table width=\"929\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#fff\" class=\"estilo777\" style=\"table-layout:fixed\">";

			R=R+"<tr>";				
			R=R+"<td width=\"100\"></td>";
			R=R+"<td width=\"100\"></td>";
			R=R+"<td width=\"100\"></td>";
			R=R+"<td width=\"400\"></td>";
			R=R+"<td width=\"100\"></td>";
			R=R+"<td width=\"40\"></td>";
			R=R+"</tr>";	
		
		if(jL(Cad)>0){
			for(i=0; i<=TR.length-1; i++){	
				TD=TR[i].split(",");
				
				//TD[5]=(TD[4]/TCa).toFixed(2);
				//
				R=R+"<tr onclick=\"TrClas(this)\" id=\""+i+"\" style=\"font-size:12px\" ondblclick=\"Z('"+TD[1]+"')\">";				
				R=R+"<td >"+TD[1]+"</td>";
				R=R+"<td >"+TD[2]+"</td>";
				R=R+"<td >"+TD[3]+"</td>";
				R=R+"<td >"+TD[4]+"</td>";
				R=R+"<td >"+TD[5]+"</td>";
				R=R+"<td ></td>";
				R=R+"</tr>";
			}
		}
		R=R+"</table>";
				
		var StrDatos=R;
		
		if(jL(R)>0){			  
			TAB_A.innerHTML = StrDatos;
			TAB_C.style.visibility="visible";
			TAB_A.style.visibility="visible";
			
				   
		}else{
			TAB_C.style.visibility="hidden";
			TAB_A.style.visibility="hidden";
		}
	}
}

//-------------------------------------------------------------------------
function Z(Cod){	
	svv("TxtCod,TxtDes,TxtMar");	
	R=AX("Z_STOCK_M",Cod);
	
	S=R.split("|");	
	
	Cod=S[0];
	Des=S[1];
	Mar=S[2];
	//
	sv("TxtCod",Cod);
	sv("TxtDes",Des);
	sv("TxtMar",Mar);
	//
	CerrarDIV()
}
//**************************************************************************

function CerrarDIV(){
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	DivCaption.style.visibility="hidden";
	DivDatos.style.visibility="hidden";	
	document.getElementById("TxtCan").focus();
}
//**************************************************************************
/*function z(lpStrId){
	if(lpStrId.trim().length>0){
	DivDatos = document.getElementById("DivDatosProd");
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=FIL_STOCK_MIN_ID&TxtPar="+lpStrId);	
	ajax.onreadystatechange=function() {
	   if (ajax.readyState==4) {			  
		   DivDatos.innerHTML = ajax.responseText
		   var Cod=document.getElementById("TCod").value;
		   var Des=document.getElementById("TDes").value;
		   var Mar=document.getElementById("TMar").value;
		   if(jLen(Cod)==0 && jLen(Des)==0 && jLen(Mar)==0 ){
			   document.getElementById("TCod").value="";
			   document.getElementById("TDes").value="";
			   document.getElementById("TMar").value="";
			   alert("Codigo no registrado");
			   document.getElementById("TxtCod").focus();
		   }else{
			   document.getElementById("TxtCod").value=document.getElementById("TCod").value;
			   document.getElementById("TxtDes").value=document.getElementById("TDes").value;
			   document.getElementById("TxtMar").value=document.getElementById("TMar").value;
			   document.getElementById("TxtCan").focus();				   
		   }
		   DivDatos = document.getElementById("TAB_AJAX");
		   DivCaption = document.getElementById("TAB_CAPTION");
		   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 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=="0"){
			jF("CboTdp");		
		}else{
			jF("TxtMarca");
		}
		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());
	}
}


function TrClas(x){
	if (document.getElementsByClassName("trselected").length > 0) {
		var element = document.getElementsByClassName("trselected");
		element[0].className = "impar";			
	}
	//
	document.activeElement.blur();
	x.className="trselected";
}

//-------------------------------------------------------------------------
function TrFila(evnt){
var ev = (evnt) ? evnt : event;
	var code=(ev.which) ? ev.which : event.keyCode;
	var element = document.getElementsByClassName("trselected");
	//
	TAB_A = document.getElementById("TAB_AJAX");
	TAB_C = document.getElementById("TAB_CAPTION");   
  	//
	if(TAB_A.style.visibility!="hidden" && document.getElementById(0)){
	   if (code == 40) {
			if (document.getElementsByClassName("trselected").length > 0) {							
				var num = (parseInt(element[0].id) + 1).toString();				
				if (document.getElementById(num)) {
					TrClas(document.getElementById(num));
				}
			}else{
				TrClas(document.getElementById(0));			
			}
	   }
	   //
	   if (code == 38) {
			if (document.getElementsByClassName("trselected").length > 0) {
				var num = (parseInt(element[0].id) - 1).toString();
				if (document.getElementById(num)) {
					TrClas(document.getElementById(num))
				}
			}
		}
		//
		if (code == 13 ){
			if (document.getElementsByClassName("trselected").length > 0) {
				Z(element[0].cells[0].innerText);
			}
		}
		//
		if (code == 27 ){
			CerrarDIV();
			//
			document.getElementById("LDet").focus();
		}
	}
}
//
if (window.document.addEventListener) {
   window.document.addEventListener("keydown", TrFila, false);
} else {
   window.document.attachEvent("onkeydown", TrFila);
}
