// JavaScript Document
var enProceso = false; // lo usamos para ver si hay un proceso activo
var http = getHTTPObject(); // Creamos el objeto XMLHttpRequest
var xmlhttp2;
var objeto = null;
var tipo = ""
var aid = null;

function handleHttpResponse() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			if (http.responseText.indexOf('invalid') == -1) {
			// Armamos un array, usando la coma para separar elementos
			results = http.responseText;
			if(tipo==1)
			{
				document.getElementById(objeto).innerHTML = results[0];
			}else if(tipo==2)
			{
				document.getElementById(objeto).innerHTML = results[0];
				//alert(results[0]);
			}else if(tipo==3)
			{
				document.getElementById(objeto).innerHTML = results[0];
				//alert(results[0]);
			}else if(tipo==4)
			{
				if(results== "-" || results== null)
				{
					document.getElementById(objeto).innerHTML = "<span class='CMG_text'>The item is not exist.</span>";
				}else
				{
					var rsv = results;
					document.getElementById(objeto).innerHTML = "<span class='CMG_text'>" + results.replace(/,/g,"<br>") + "</span>";
				}
			}else if(tipo==5)
			{
				document.getElementById(objeto).value = results;
				//alert(results[0]);
			}else if(tipo==6)
			{
				if(results== "-" || results== null)
				{
					document.getElementById(objeto).innerHTML = "<span class='CMG_text'>The item is not exist.</span>";
				}else
				{
					var rsv = results;
					document.getElementById(objeto).innerHTML = "<span class='CMG_text'>" + results.replace(/,/g,"<br>") + "</span>";
				}
			}else if(tipo==10)
			{
				document.getElementById(objeto).innerHTML = ""
				document.getElementById(objeto).style.color = "#000000"	
				document.getElementById(objeto).innerHTML = "<b>" + results + "</b>";
			}else if(tipo==11)
			{
				//document.getElementById(objeto).innerHTML = "<img src='images/s_idinvoice.gif' alt='" + results + "' width='8' height='7' />"
				var n = "";var a ="";
				var nn = "";
				var f = "";				
				var num = objeto.toString().split("div")[1];
				n  = results.toString().split(",")[0];
				nn = results.toString().split(",")[0];
				f = results.toString().split(",")[1];
				a = n + "<input type='hidden' id='div"+num+"nn' name='div"+num+"nn' value='"+nn+"'/><input type='hidden' id='div"+num+"h' name='div"+num+"h' value='"+f+"'/>"
				//document.getElementById(objeto+"n").innerHTML = a;
				alert("Invoice saved.")
			}else if(tipo==12)
			{
				document.getElementById(objeto).innerHTML = results
			}else if(tipo==13)
			{
				document.getElementById(objeto).value= results
			}else if(tipo==14)
			{
				//document.getElementById(objeto).innerHTML = "<img src='images/s_idinvoice.gif' alt='" + results + "' width='8' height='7' />"
				var n = "";var a ="";
				var n = "";	
				var num = objeto.toString().split("div")[1];
				n  = results.toString().split(",")[0];
				nn = results.toString().split(",")[0];
				f = results.toString().split(",")[1];
				a = n + "<input type='hidden' id='div"+num+"nn' name='div"+num+"nn' value='"+nn+"'/><input type='hidden' id='div"+num+"h' name='div"+num+"h' value='"+f+"'/>"
				//document.getElementById(objeto+"n").innerHTML = a;
				alert("Bill of Lading saved.")
			}else if(tipo==15)
			{
				document.getElementById(objeto).value= results
			}else if(tipo==16)
			{
				alert("Liquidation Saved");
				var avg=(results.toString().split("*")[0] / results.toString().split("*")[2]).toFixed(2);
				var bal=(results.toString().split("*")[0] - results.toString().split("*")[3]).toFixed(2);
				document.getElementById(objeto).value= (results.toString().split("*")[0]-0).toFixed(2);
				document.getElementById('avg' + results.toString().split("*")[4]).innerHTML = "<b>" + avg + '</b>';
				document.getElementById('bal' + results.toString().split("*")[4]).innerHTML = "<b>" + bal + '&nbsp;</b>';
			}else
			{
				document.getElementById(objeto).innerHTML = "Error En b";
			}
			enProceso = false;
			}
		}
	}
}

function getDropDown(div,id_operacion, objeto1, op_get) {
	if (!enProceso && http)
	{
		if(op_get==1)
		{
			var valor = escape(objeto1.value);
		}else if(op_get==2)
		{
			var valor = escape(objeto1.selectedIndex);
		}else if(op_get==3)
		{
			var valor = escape(objeto1.options[objeto1.selectedIndex].text);
		}
		objeto = escape(div.toString());
		tipo = parseInt(id_operacion.toString().split(",")[0]);
		var url = "DGTFuncionesAjax.asp?valor="+ valor + "," + id_operacion.toString().split(",")[1] + "&id="+tipo;
		http.open("GET", url, true);
		http.onreadystatechange = handleHttpResponse;
		enProceso = true;
		http.send(null);
	}
}

function getGeneral(objeto_contenedor,id_operacion, valor, tabindex){
	if (!enProceso && http)
	{
		objeto = escape(objeto_contenedor.toString());
		tipo = parseInt(id_operacion.toString());
		var url = "DGTFuncionesAjax.asp?id=" + tipo + "&valor=" + valor + "&tabindex=" + tabindex;
		http.open("GET", url, true);
		http.onreadystatechange = handleHttpResponse;
		enProceso = true;
		http.send(null);
	}
}

function getValue(objeto_contenedor,id_operacion,valor){
	if (!enProceso && http)
	{
		tipo = parseInt(id_operacion.toString());
		objeto = escape(objeto_contenedor.toString());
		var url = "DGTFuncionesAjax.asp?id=" + tipo + "&valor=" + valor;
		http.open("GET", url, true);
		http.onreadystatechange = handleHttpResponse;
		enProceso = true;
		http.send(null);
	}
}

function getDiv(objeto_contenedor,id_operacion, valor){
	if (!enProceso && http)
	{
		objeto = escape(objeto_contenedor.toString());
		tipo = parseInt(id_operacion.toString());
		var url = "DGTFuncionesAjax.asp?id=" + tipo + "&valor=" + valor;
		http.open("GET", url, true);
		http.onreadystatechange = handleHttpResponse;
		enProceso = true;
		http.send(null);
	}
}

function set001(pagoId, id, div, mensaje) {
	if (!enProceso && http)
	{
		var valor = pagoId;
		objeto = div;
		var url = "DGTFuncionesAjax.asp?pagoId="+ pagoId +"&id="+id+"&valor=notnull&msj="+mensaje;
		http.open("POST", url, true);		
		http.onreadystatechange = handleHttpResponse;
		enProceso = true;
		http.send(null);
	}
}

function set002(objeto_contenedor,id_operacion, valor) {
	if (!enProceso && http)
	{
		objeto = escape(objeto_contenedor.toString());
		tipo = parseInt(id_operacion.toString());
		var url = "DGTFuncionesAjax.asp?id=" + tipo + "&valor=" + valor;
		http.open("GET", url, true);		
		http.onreadystatechange = handleHttpResponse;
		enProceso = true;
		http.send(null);
	}
}

function set003(objeto_contenedor,id_operacion, valor) {
		xmlhttp4=GetXmlHttpObject();
		tipo = parseInt(id_operacion.toString());
		var url = "GetTransInfo.asp?valor=" + valor +"&tipo=" + tipo;
		xmlhttp4.onreadystatechange=stateChanged3;
		xmlhttp4.open("GET",url,true);
		xmlhttp4.send(null);
}

function set004(objeto_contenedor,id_operacion, valor)
{
		xmlhttp2=GetXmlHttpObject();
		tipo = parseInt(id_operacion.toString());
		var url = "GetTransInfo.asp?valor=" + valor +"&tipo=" + tipo;
		xmlhttp2.onreadystatechange=stateChanged;
		xmlhttp2.open("GET",url,true);
		xmlhttp2.send(null);
}

function set005(objeto_contenedor,id_operacion, valor) {
	if (!enProceso && http)
	{
		objeto = escape(objeto_contenedor.toString());
		tipo = parseInt(id_operacion.toString());
		var url = "DGTFuncionesAjax.asp?id=" + tipo + "&valor=" + valor;
		http.open("GET", url, true);		
		http.onreadystatechange = handleHttpResponse;
		//enProceso = true;
		http.send(null);
	}
}

function stateChanged()
{
if (xmlhttp2.readyState==4)
  {
  document.getElementById("hidBill").value=xmlhttp2.responseText;
  }
}

function stateChanged3()
{
if (xmlhttp4.readyState==4)
  {
  document.getElementById("hidInv").value=xmlhttp4.responseText;
  }
}

function LoadDropdowns(objeto_contenedor,id_operacion, valor)
{
		xmlhttp3=GetXmlHttpObject();
		tipo = parseInt(id_operacion.toString());
		var url = "LoadDD.asp?valor="+valor;
		xmlhttp3.onreadystatechange=stateChanged2;
		xmlhttp3.open("GET",url,true);
		xmlhttp3.send(null);
}

function stateChanged2()
{
if (xmlhttp3.readyState==4)
  {
  document.getElementById("destinoDiv").innerHTML=xmlhttp3.responseText;
  }
}

function LoadFarms(objeto_contenedor,id_operacion, valor)
{
		xmlhttp5=GetXmlHttpObject();
		tipo = parseInt(id_operacion.toString());
		var url = "LoadFarms.asp?valor="+valor;
		xmlhttp5.onreadystatechange=stateChanged4;
		xmlhttp5.open("GET",url,true);
		xmlhttp5.send(null);
}

function stateChanged4()
{
if (xmlhttp5.readyState==4)
  {
  document.getElementById("farmDiv").innerHTML=xmlhttp5.responseText;
  }
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) { xmlhttp = false; }
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) { xmlhttp = false; }
}
return xmlhttp;
}


