// 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(o)
	{
		Evento = window.event;
		key = (Evento.keyCode==0)? 13 : Evento.keyCode;
		o.readOnly = false;
		if(key==13)
		{
			g_o = o;
			if(o.value.toString() == ""){buscaPorNombre(1,"Producto NO Encontrado");}else{document.getElementById("td1").innerHTML = "Buscando . . .";setTimeout("ejecutaConsulta()",200);}
		}
	}
	
	function ejecutaConsulta()
	{
		var o = g_o;
		try
		{
		// la opcion 1 sirve para llenar ls datos de la factura
			getGeneral("txtallval"+g_c_f,"1", o.value);
		}catch(e){alert("Error asincronico 00010001 " + e)}
	}
	
	function calculaCantidad(o)
	{
		Evento = window.event;
		key = (Evento.keyCode==0)? 13 : Evento.keyCode;
		var g_c_f_a = o.name.toString().substring(7,9);

		if(key==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/1.12,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/1.12,2))
				}catch(e)
				{
					var ttemp =parseFloat(redondear(t/1.12,2));
				}
				var html = "Total Temporal<img src='images/spacer.gif' />";
				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/1.12,2)
				document.getElementById("txtvald"+g_c_f_a).focus();
				document.getElementById("txtvald"+g_c_f_a).select();
			}
		}
	}
	
	function calculaDescuento(o)
	{
		Evento = window.event;
		key = (Evento.keyCode==0)? 13 : Evento.keyCode;		
		var g_c_f_a = o.name.toString().substring(7,9);
		
		if(key==13)
		{
			if(parseFloat(o.value) >= 0 && parseFloat(o.value) <= 100)
			{
				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/1.12,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/1.12,2))
				}catch(e)
				{
					var ttemp =parseFloat(redondear(t/1.12,2));
				}
				var html = "Total Temporal<img src='images/spacer.gif' />";
				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 00010000" + 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)
	{
		// -- BETA DEV
		document.getElementById("td" + id).innerHTML = "<input onkeyup=\"muestra_lista(this,350,0,0,'');\" class='formElement' name='txtnom' type='text' id='txtnom' size='50' maxlength='50' value='"+val+"' />"
		globaldetalle = val;
		txt_mostrada = true		
		document.getElementById("txtcant1").value = "1"
		document.getElementById("txtvalu1").value = ""
		document.getElementById("txtvalu1td").innerHTML = ""
		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)
	{
		if(sender.value.length >= 4 )
		{
			getGeneral("lista",4, sender.value)
			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;
		}
	}
	
	function escojeListaDinamica(sender,idD)
	{		
		cerrar_lista();
		document.getElementById("td1").innerHTML = "Buscando . . ."
		document.getElementById("txtCod1").value = "p" + idD;
		enviar(document.getElementById('txtCod1'));
	}
	
	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
		}
	}

	document.onclick = cerrarAmpliacion