


var peticion = false;
var  testPasado = false;
try {
  peticion = new XMLHttpRequest();
  } catch (trymicrosoft) {
  try {
  peticion = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
  try {
  peticion = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (failed) {
  peticion = false;
  }
  }
}
if (!peticion)
alert("ERROR AL INICIALIZAR!");

var peticion2 = false;
try {
  peticion2 = new XMLHttpRequest();
  } catch (trymicrosoft) {
  try {
  peticion2 = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
  try {
  peticion2 = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (failed) {
  peticion2 = false;
  }
  }
}
if (!peticion2)
alert("ERROR AL INICIALIZAR!");

var peticion3 = false;
try {
  peticion3= new XMLHttpRequest();
  } catch (trymicrosoft) {
  try {
  peticion3 = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
  try {
  peticion3 = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (failed) {
  peticion3 = false;
  }
  }
}
if (!peticion3)
alert("ERROR AL INICIALIZAR!");

function crear_peticion_ () {
	var peticion3 = false;
	try {
	  peticion3= new XMLHttpRequest();
	} catch (trymicrosoft) {
	  try {
	     peticion3 = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (othermicrosoft) {
	     try {
	        peticion3 = new ActiveXObject("Microsoft.XMLHTTP");
	     } catch (failed) {
	        peticion3 = false;
	     }
	  }
	}
	if (!peticion3)
	  alert("ERROR AL INICIALIZAR!");
	return peticion3;
}

function cargarPHP (url, idvalor, params) {
        
    if (idvalor != '') var valor= document.getElementById(idvalor);
    else var valor= '';
    
    var fragment_url = url+'?id='+valor;

    if (params != '') fragment_url = fragment_url + params;           

    peticion.open("GET", fragment_url);
    peticion.send(null);
}

function cargarPHP2 (url) {
                
    //alert(url);
    peticion.open("GET", url);
    //open(url,"_blank");
    peticion.send(null);
    
}

function cargarCombo (url, comboAnterior, element_id) {
    //Obtenemos el contenido del div
    //donde se cargaran los resultados
    var element =  document.getElementById(element_id);
    //Obtenemos el valor seleccionado del combo anterior
    var valordepende = document.getElementById(comboAnterior)
    var x = valordepende.value
    //construimos la url definitiva
    //pasando como parametro el valor seleccionado
    var fragment_url = url+'?Id='+x;
    element.innerHTML = 'Cargando...';
    //abrimos la url
    peticion.open("GET", fragment_url);
    peticion.onreadystatechange = function() {
        if (peticion.readyState == 4) {
//escribimos la respuesta
element.innerHTML = peticion.responseText;
        }
    }
   peticion.send(null);
}

function cargarComboPar (url, parametros, comboAnterior, element_id) {
    //Obtenemos el contenido del div
    //donde se cargaran los resultados
    var element =  document.getElementById(element_id);
    //Obtenemos el valor seleccionado del combo anterior
    var valordepende = document.getElementById(comboAnterior)
    var x = valordepende.value
    //construimos la url definitiva
    //pasando como parametro el valor seleccionado
    var fragment_url = url+'?Id='+x+parametros;
    element.innerHTML = 'Cargando...';
    //abrimos la url
    peticion.open("GET", fragment_url);
    peticion.onreadystatechange = function() {
        if (peticion.readyState == 4) {
//escribimos la respuesta
element.innerHTML = peticion.responseText;
        }
    }
   peticion.send(null);
}

function recalcularTotal() {
   var importe = document.getElementsByName("importe")[0].value;
   var cantidad = document.getElementsByName("cantidad")[0].value;
   var result = importe*cantidad;
   document.getElementsByName("total")[0].innerHTML = result.toFixed(2)+"&euro;";
}

// Recalcula el total de la suma de N filas
// Cuando modificamos el valor de una de las lineas recalcula el valor del total
 
function recalcularTotal2(filas) {
   var total=0.0;
   for(i=0;i<filas;i++) {
      total += parseFloat(document.getElementById("id1_"+i).value);  
       
   }
   document.getElementById("aux_total_facturacion").value = total.toFixed(2);
   document.getElementById("ttotal").innerHTML = "<b>"+total.toFixed(2)+" Kg</b>";
}

function recalcularTotal3(filas) {
   var total=0.0;
   for(i=0;i<filas;i++) {
      total += parseFloat(document.getElementById("id2_"+i).value);         
   }
   document.getElementById("dtotal").innerHTML = "<b>"+total.toFixed(2)+" Kg</b>";
}

function recalcularTotal4(filas) {
   var total=0.0;
   for(i=0;i<filas;i++) {
      total += parseFloat(document.getElementById("id1_"+i).value);   
   }
   document.getElementsByName("ttotal2")[0].innerHTML = "<b>"+total.toFixed(2)+" Kg</b>";
}


function recalcularTotallinea() {
   var limporte = document.getElementsByName("limporte")[0].value;
   var lcantidad = document.getElementsByName("lcantidad")[0].value;
   var result = limporte*lcantidad;
   document.getElementsByName("ltotal")[0].innerHTML = result.toFixed(2)+"&euro;";
}


function cargarCombo_2 (url, comboAnterior, element_id) {  
  cargarCombo(url,comboAnterior,element_id);
  recalcularTotal();
}

function cargarCombo_2linea (url, comboAnterior, element_id) {
  cargarCombo(url,comboAnterior,element_id);
  recalcularTotallinea();
}
