// JavaScript Document
	var g_c_f = 1;
	var g_o;
	var lista_mostrada = false;
	var txt_mostrada = false;
	var globaldetalle = "";
	

	function redondear(cantidad, decimales) {
		var cantidad = parseFloat(cantidad);
		var decimales = parseFloat(decimales);
		decimales = (!decimales ? 2 : decimales);
		return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
	} 
	
	function enviar(ob,op,e)
	{
		if (e!=1)
		{
			e = e || window.event;    
			var Ucode = e.keyCode || e.which;
			//Evento = window.event;
			//key = (Evento.keyCode==0)? 13 : Evento.keyCode;
			//o.readOnly = false;
			if(Ucode==13)
			{
				g_o = ob;
				if(ob.value.toString() == ""){buscaPorNombre(1,"Producto NO Encontrado",op);}else{document.getElementById("td1").innerHTML = "Buscando . . .";setTimeout("ejecutaConsulta("+op+")",200);}
			}
		}
		else
		{
			g_o = ob;
			if(ob.value.toString() == ""){buscaPorNombre(1,"Producto NO Encontrado",op);}else{document.getElementById("td1").innerHTML = "Buscando . . .";setTimeout("ejecutaConsulta("+op+")",200);}
		}
	}
	
	function ejecutaConsulta(op)
	{
		var o = g_o;
		try
		{
		// la opcion 1 sirve para llenar ls datos de la factura		alert(o.value);
			getGeneral("txtallval"+g_c_f,op, o.value);
		}catch(e){}
		//{alert("Error asincronico 00010001 " + e)}
		
	}
	
	function calculaCantidad(o,e)
	{
		e = e || window.event;    
		var Ucode = e.keyCode || e.which;
		//Evento = window.event;
		//key = (Evento.keyCode==0)? 13 : Evento.keyCode;
		var g_c_f_a = o.name.toString().substring(7,9);

		if(Ucode==13)
		{
			var iva=document.getElementById("txtallval"+g_c_f_a).value.toString().split("<@>")[12];	
			if (iva=='True')
			{
				factoriva=1
			}
			else
			{
				factoriva=1
			}
			if(document.getElementById("txtallval1").value != "-")
			{
				var t = o.value * parseFloat(document.getElementById("txtallval"+g_c_f_a).value.toString().split("<@>")[3]);
				document.getElementById("txtvalt"+g_c_f_a).value = redondear(t/factoriva,2)
				document.getElementById("txtvalu"+g_c_f_a).focus();
				document.getElementById("txtvalu"+g_c_f_a).select();
				try
				{
					var ttemp = parseFloat(parseFloat(document.getElementById("t1").innerHTML.toString().replace(",",""))) + parseFloat(redondear(t/factoriva,2))
				}catch(e)
				{
					var ttemp =parseFloat(redondear(t/factoriva,2));
				}
				var html = "";
				//html = html + "<br>Total Orden:&nbsp;" + redondear(parseFloat(ttemp*1.12).toString(),2);
				//document.getElementById("idtotaltemp").innerHTML = html + "<img src='images/spacer.gif' />"
				
			}else
			{
				var t = o.value * 0;
				document.getElementById("txtvalt"+g_c_f_a).value = redondear(t/factoriva,2)
				document.getElementById("txtvalu"+g_c_f_a).focus();
				document.getElementById("txtvalu"+g_c_f_a).select();
			}
		}
	}
	
	function calculaCantidadPUR(o,e)
	{
		e = e || window.event;    
		var Ucode = e.keyCode || e.which;
		//Evento = window.event;
		//key = (Evento.keyCode==0)? 13 : Evento.keyCode;
		var g_c_f_a = o.name.toString().substring(7,9);

		if(Ucode==13)
		{
			if(document.getElementById("txtallval1").value != "-")
			{
				var t = o.value * parseFloat(document.getElementById("txtallval"+g_c_f_a).value.toString().split("<@>")[3]);
				document.getElementById("txtvalt"+g_c_f_a).value = redondear(t,2)
				document.getElementById("txtvalu"+g_c_f_a).focus();
				document.getElementById("txtvalu"+g_c_f_a).select();
				try
				{
					var ttemp = parseFloat(parseFloat(document.getElementById("t1").innerHTML.toString().replace(",",""))) + parseFloat(redondear(t,2))
				}catch(e)
				{
					var ttemp =parseFloat(redondear(t,2));
				}
				var html = "";
				//html = html + "<br>Total Orden:&nbsp;" + redondear(parseFloat(ttemp*1).toString(),2);
				//document.getElementById("idtotaltemp").innerHTML = html + "<img src='images/spacer.gif' />"
				
			}else
			{
				var t = o.value * 0;
				document.getElementById("txtvalt"+g_c_f_a).value = redondear(t,2)
				document.getElementById("txtvalu"+g_c_f_a).focus();
				document.getElementById("txtvalu"+g_c_f_a).select();
			}
		}
	}
	
	function calculaPrecio(o,e)
	{
		e = e || window.event;    
		var Ucode = e.keyCode || e.which;
		//Evento = window.event;
		//key = (Evento.keyCode==0)? 13 : Evento.keyCode;
		var g_c_f_a = o.name.toString().substring(7,9);

		if(Ucode==13)
		{
			var iva=document.getElementById("txtallval"+g_c_f_a).value.toString().split("<@>")[12];	
			if (iva=='True')
			{
				factoriva=1
			}
			else
			{
				factoriva=1
			}
			if(document.getElementById("txtallval1").value != "-")
			{
				var t = o.value * parseFloat(document.getElementById("txtcant"+g_c_f_a).value.toString());
				document.getElementById("txtvalt"+g_c_f_a).value = redondear(t/factoriva,2)
				document.getElementById("txtvald"+g_c_f_a).focus();
				document.getElementById("txtvald"+g_c_f_a).select();
				try
				{
					var ttemp = parseFloat(parseFloat(document.getElementById("t1").innerHTML.toString().replace(",",""))) + parseFloat(redondear(t/factoriva,2))
				}catch(e)
				{
					var ttemp =parseFloat(redondear(t/factoriva,2));
				}
				var html = "";
				//html = html + "<br>Total Orden:&nbsp;" + redondear(parseFloat(ttemp*1.12).toString(),2);
				//document.getElementById("idtotaltemp").innerHTML = html + "<img src='images/spacer.gif' />"
				
			}else
			{
				var t = o.value * 0;
				document.getElementById("txtvalt"+g_c_f_a).value = redondear(t/factoriva,2)
				document.getElementById("txtvald"+g_c_f_a).focus();
				document.getElementById("txtvald"+g_c_f_a).select();
			}
		}
	}
	
	function calculaCosto(o,e)
	{
		e = e || window.event;    
		var Ucode = e.keyCode || e.which;
		//Evento = window.event;
		//key = (Evento.keyCode==0)? 13 : Evento.keyCode;
		var g_c_f_a = o.name.toString().substring(7,9);

		if(Ucode==13)
		{
			if(document.getElementById("txtallval1").value != "-")
			{
				var t = o.value * parseFloat(document.getElementById("txtcant"+g_c_f_a).value.toString());
				document.getElementById("txtvalt"+g_c_f_a).value = redondear(t,2)
				document.getElementById("txtvalt"+g_c_f_a).focus();
				document.getElementById("txtvalt"+g_c_f_a).select();
				try
				{
					var ttemp = parseFloat(parseFloat(document.getElementById("t1").innerHTML.toString().replace(",",""))) + parseFloat(redondear(t,2))
				}catch(e)
				{
					var ttemp =parseFloat(redondear(t,2));
				}
				var html = "";
				//html = html + "<br>Total Orden:&nbsp;" + redondear(parseFloat(ttemp*1.12).toString(),2);
				//document.getElementById("idtotaltemp").innerHTML = html + "<img src='images/spacer.gif' />"
				
			}else
			{
				var t = o.value * 0;
				document.getElementById("txtvalt"+g_c_f_a).value = redondear(t,2)
				document.getElementById("txtvalt"+g_c_f_a).focus();
				document.getElementById("txtvalt"+g_c_f_a).select();
			}
		}
	}
	
	function calculaCostoPUR(o,e)
	{
		e = e || window.event;    
		var Ucode = e.keyCode || e.which;
		//Evento = window.event;
		//key = (Evento.keyCode==0)? 13 : Evento.keyCode;
		var g_c_f_a = o.name.toString().substring(7,9);

		if(Ucode==13)
		{
			if(document.getElementById("txtallval1").value != "-")
			{
				var t = o.value * parseFloat(document.getElementById("txtcant"+g_c_f_a).value.toString());
				document.getElementById("txtvalt"+g_c_f_a).value = redondear(t,2)
				document.getElementById("txtvalt"+g_c_f_a).focus();
				document.getElementById("txtvalt"+g_c_f_a).select();
				try
				{
					var ttemp = parseFloat(parseFloat(document.getElementById("t1").innerHTML.toString().replace(",",""))) + parseFloat(redondear(t,2))
				}catch(e)
				{
					var ttemp =parseFloat(redondear(t,2));
				}
				var html = "";
				//html = html + "<br>Total Orden:&nbsp;" + redondear(parseFloat(ttemp*1).toString(),2);
				//document.getElementById("idtotaltemp").innerHTML = html + "<img src='images/spacer.gif' />"
				
			}else
			{
				var t = o.value * 0;
				document.getElementById("txtvalt"+g_c_f_a).value = redondear(t,2)
				document.getElementById("txtvalt"+g_c_f_a).focus();
				document.getElementById("txtvalt"+g_c_f_a).select();
			}
		}
	}
	
	function calculaDescuento(o,e)
	{
		e = e || window.event;    
		var Ucode = e.keyCode || e.which;
		//Evento = window.event;
		//key = (Evento.keyCode==0)? 13 : Evento.keyCode;		
		var g_c_f_a = o.name.toString().substring(7,9);
		
		if(Ucode==13)
		{
			if (o.value.substring(0,1)=='$') //ingreso de descuento numérico
			{
				var valornum=o.value.substring(1);
				if(parseFloat(valornum) >= 0)
				{
					var iva=document.getElementById("txtallval"+g_c_f_a).value.toString().split("<@>")[12];	
					if (iva=='True')
					{
						factoriva=1
					}
					else
					{
						factoriva=1
					}	
					var t_s_d = document.getElementById("txtcant"+g_c_f_a).value * document.getElementById("txtvalu"+g_c_f_a).value
					var t = parseFloat(t_s_d)-parseFloat(valornum);
					if(t<0)
					{
						o.value = 0;
						o.select();
					}
					else
					{
						document.getElementById("txtvalt"+g_c_f_a).value = redondear(t/factoriva,2)
						document.getElementById("txtvalt"+g_c_f_a).focus();
						document.getElementById("txtvalt"+g_c_f_a).select();
						try
						{
							var ttemp = parseFloat(parseFloat(document.getElementById("t1").innerHTML.toString().replace(",",""))) + parseFloat(redondear(t/factoriva,2))
						}catch(e)
						{
							var ttemp =parseFloat(redondear(t/factoriva,2));
						}
						var html = "";
						//html = html + "<br>Total Orden:&nbsp;" + redondear(parseFloat(ttemp*1.12).toString(),2);
						//document.getElementById("idtotaltemp").innerHTML = html + "<img src='images/spacer.gif' />"
					}
				}else
				{
					o.value = 0;
					o.select();
				}
			}
			else
			{
				if(parseFloat(o.value) >= 0 && parseFloat(o.value) <= 100)
				{
					var iva=document.getElementById("txtallval"+g_c_f_a).value.toString().split("<@>")[12];	
					if (iva=='True')
					{
						factoriva=1
					}
					else
					{
						factoriva=1
					}	
					var t_s_d = document.getElementById("txtcant"+g_c_f_a).value * document.getElementById("txtvalu"+g_c_f_a).value
					var t = parseFloat(t_s_d)-(parseFloat(o.value/100) * parseFloat(t_s_d));
					document.getElementById("txtvalt"+g_c_f_a).value = redondear(t/factoriva,2)
					document.getElementById("txtvalt"+g_c_f_a).focus();
					document.getElementById("txtvalt"+g_c_f_a).select();
					try
					{
						var ttemp = parseFloat(parseFloat(document.getElementById("t1").innerHTML.toString().replace(",",""))) + parseFloat(redondear(t/factoriva,2))
					}catch(e)
					{
						var ttemp =parseFloat(redondear(t/factoriva,2));
					}
					var html = "";
					//html = html + "<br>Total Orden:&nbsp;" + redondear(parseFloat(ttemp*1.12).toString(),2);
					//document.getElementById("idtotaltemp").innerHTML = html + "<img src='images/spacer.gif' />"
				}else
				{
					o.value = 0;
					o.select();
				}
			}
		}
	}
	
	function calculaTotal(o)
	{
		psubmit();
	}
	
	function showHelpCont(d_m)
	{		
		if (document.getElementById("txtallval"+d_m).value.toString().split("<@>")[10]!=""){
			var html = ""
			html = html + "<table border='0' cellspacing='0' cellpadding='3'><tr>"
			html = html + "<td align='left' valign='top'><img src='uploads/"+document.getElementById("txtallval"+d_m).value.toString().split("<@>")[10]+"' /></td>"
			html = html + "<td align='left' valign='top' style='background:#efefef; font-family:Verdana; font-size:11px; vertical-align:top;text-align:left'>" + document.getElementById("txtallval"+d_m).value.toString().split("<@>")[2] + "</td>"
			html = html + "</tr></table>"
			document.getElementById("helpCont").innerHTML = html
		}else
		{
			var html = ""
			html = html + "<table border='0' cellspacing='0' cellpadding='3'><tr>"
			html = html + "<td align='left' valign='top'>&nbsp;</td>"
			html = html + "<td align='left' valign='top' style='background:#efefef; font-family:Verdana; font-size:11px; vertical-align:top;text-align:left'>" + document.getElementById("txtallval"+d_m).value.toString().split("<@>")[2] + "</td>"
			html = html + "</tr></table>"
			document.getElementById("helpCont").innerHTML = html;
		}
	}
	
	function showHelpContEst(pidi)
	{
		try
		{
		// la opcion 1 sirve para llenar ls datos de la factura
			document.getElementById("helpCont").innerHTML = "<span style='color:#666666; font-family:Verdana; font-size:14px;'>buscando...</span>"
			getGeneral("helpCont","3", pidi);
		}catch(e){}
		//{alert("Error asincronico 00010001 " + e)}
	}
	
	function emptyHelpCont()
	{
		document.getElementById("helpCont").innerHTML = ""
	}
	
	function psubmit()
	{
		//var a = confirm("Desea guaradar la orden?")
		var h = document.getElementById("txtallval1").value
		if(h!="-")
		{
			document.getElementById("totpos").value = g_c_f;
			document.forms[0].submit()
		}else
		{
			alert("Ningun producto seleccionado")
			document.getElementById("txtcod1").focus();
			document.getElementById("txtcod1").select();
		}
	}
	
	function buscaPorNombre(id,val,op)
	{
		// -- BETA DEV
		try{
		document.getElementById("td" + id).innerHTML = "<input onkeyup=\"muestra_lista(this,350,0,0,'',"+op+");\" class='formElement' name='txtnom' type='text' id='txtnom' size='50' maxlength='50' value='"+val+"' />"
		}catch(e){alert(e.message+"busca")}
		globaldetalle = val;
		txt_mostrada = true		
		document.getElementById("txtcant1").value = "1"
		document.getElementById("txtvalu1").value = ""
		try{
		document.getElementById("txtvalu1td").innerHTML = ""
		}catch(e){}
		document.getElementById("txtvald1").value = "0"
		document.getElementById("txtvalt1").value = ""
		var html = "Total Temporal<img src='images/spacer.gif' />";
		document.getElementById("idtotaltemp").innerHTML = html + "<img src='images/spacer.gif' />"
		document.getElementById("txtnom").focus()
		document.getElementById("txtnom").select()
		
	}
	
	function muestra_lista(sender, w, h, offSet, text, op)
	{
	try{
		if(sender.value.toString().length >= 3 )
		{
		//alert(op);
			getGeneral2("lista",4, sender.value,op)
			document.getElementById("lista").style.width = w
			document.getElementById("lista").style.height = h
			var posX = findPosX(sender);
			var posY = findPosY(sender);
			document.getElementById('lista').style.left = posX+offSet;
			document.getElementById('lista').style.top = posY+20;
			document.getElementById("lista").style.visibility = "visible"
			lista_mostrada = true;
		}
	}catch(e){alert(e.message+"muestra")}
	}
	
	function escojeListaDinamica(sender,idD,op,e)
	{	
		cerrar_lista();
		document.getElementById("td1").innerHTML = "Buscando . . ."
		document.getElementById("txtcod1").value = "p" + idD;
		//alert(document.getElementById("txtcod1").value);
		enviar(document.getElementById('txtcod1'),op,e);
	}
	
	function muestraDetD(sender,idD)
	{
		document.getElementById("detD").style.visibility="visible";
		document.getElementById("detD").style.left=findPosX(sender)+351;
		document.getElementById("detD").style.top=findPosY(sender);
		getGeneral("detD","3", idD);
	}
	
	function escondeDetD()
	{
		document.getElementById("detD").innerHTML = ""
		document.getElementById("detD").style.visibility="hidden";
	}
	
	function cerrar_lista(){ 
		document.getElementById("lista").style.visibility = "hidden"
	}

	function cerrarAmpliacion()
	{
		if(lista_mostrada == true)
		{
			escondeDetD()
			lista_mostrada = false;
			cerrar_lista()
		}
	}
	
	function borrarDetalle(didf, oidf, ordenf)
	{
		var c = confirm("Desea borrar el detalle?")
		if(c)
		{
			document.location = "main.asp?goto=FIPDeleteDetail.asp&detID=" + didf + "&oid=" + oidf + "&orden=" + ordenf
		}
	}
	
	function borrarDetallePUR(didf, tidf, ordenf)
	{
		var c = confirm("Desea borrar el detalle?")
		if(c)
		{
			document.location = "main.asp?goto=FIPDeleteKardex.asp&detID=" + didf + "&tid=" + tidf + "&orden=" + ordenf
		}
	}

	document.onclick = cerrarAmpliacion
