<!--

var maximo_lineas = 10;
var importe = new Array(10);
var impuesto = new Array(10);
var importe_numero = new Array(10);
var importe_cadena = new Array(10);
var impuesto_numero = new Array(10);
var impuesto_cadena = new Array(10);

// ****************************
// ****************************
// Empieza la función redondear
// ****************************
// ****************************

function redondear(numero) {
 numero = Math.round(numero * 100) / 100;
 if (numero == Math.round(numero)) {
  return numero + ".00";
 } else if (numero * 10 == Math.round(numero * 10)) {
  return numero + "0";
 } else {
  return numero;
 }
}

// *****************************
// *****************************
// Empieza la función pasardatos
// *****************************
// *****************************

function pasardatos(dato1,dato2,dato3,dato4) {
 var referencia = dato1;
 var descripcion = dato2;
 var cantidad = 1;
 var precio = redondear(dato3);
 var iva = redondear(dato4);
 var importe = redondear(cantidad * precio);
 var impuesto = redondear(cantidad * iva);
 // Poner los datos en el formulario
 for (var i = 0;i < maximo_lineas;i++) {
  importe_numero[i] = parseFloat(document.getElementById("importe["+i+"]").value);
  importe_cadena[i] = importe_numero[i].toString();
  if (importe_cadena[i] == "NaN") {
   document.getElementById("referencia["+i+"]").value  = referencia;
   document.getElementById("descripcion["+i+"]").value = descripcion;
   document.getElementById("precio["+i+"]").value      = precio;
   document.getElementById("iva["+i+"]").value         = iva;
   document.getElementById("cantidad["+i+"]").value    = cantidad;
   document.getElementById("importe["+i+"]").value     = importe;
   document.getElementById("impuesto["+i+"]").value    = impuesto;
   calculartotal();
   return;
  } 
 }
 window.alert("No se pueden realizar pedidos con más de "+maximo_lineas+" líneas");
}

// ****************************
// ****************************
// Empieza la función comprobar
// ****************************
// ****************************

function comprobar() {
 var correcto = "S";
 var total         = document.getElementById("total").value;
 var f_nombrerazon = document.getElementById("fnombrerazon").value;
 var f_nifcif      = document.getElementById("fnifcif").value;
 var f_telefono    = document.getElementById("ftelefono").value;
 var f_domicilio   = document.getElementById("fdomicilio").value;
 var f_cp          = document.getElementById("fcp").value;
 var f_poblacion   = document.getElementById("fpoblacion").value;
 var f_provincia   = document.getElementById("fprovincia").value;
 var idcliente     = document.getElementById("idcliente").value;
 if (total == "") {
  window.alert("¡¡Ningún producto en el pedido!!");
  var correcto = "N"
 } else if (idcliente == "") {
  if (f_nombrerazon == "") {
   window.alert("Falta el nombre o la razón social y este dato es imprescindible para la factura");
   var correcto = "N"
  } else if (f_nifcif == "") {
   window.alert("Falta el NIF o el CIF y este dato es imprescindible para la factura");
   var correcto = "N"
  } else if (f_telefono == "") {
   window.alert("Falta el teléfono y este dato es imprescindible para la factura");
   var correcto = "N"
  } else if (f_domicilio == "") {
   window.alert("Falta el domicilio y este dato es imprescindible para la factura");
   var correcto = "N"
  } else if (f_cp == "") {
   window.alert("Falta el código postal y este dato es imprescindible para la factura");
   var correcto = "N"
  } else if (f_poblacion == "") {
   window.alert("Falta la población y este dato es imprescindible para la factura");
   var correcto = "N"
  } else if (f_provincia == "") {
   window.alert("Falta la provincia y este dato es imprescindible para la factura");
   var correcto = "N"
  }
 }
 enviar(correcto)
}

// *************************
// *************************
// Empieza la función enviar
// *************************
// *************************

function enviar(correcto) {
  if (correcto == "S") {
  document.pedido.submit();
  window.alert("Los datos de su pedido han sido enviados y procesados")
 } else {
  window.alert("Rellene el pedido correctamente")
 }
}

// ***********************************
// ***********************************
// Empieza la función borrarformulario
// ***********************************
// ***********************************

function borrarformulario() {
 for (var i=0;i < maximo_lineas;i++) {
  document.getElementById("referencia["+i+"]").value  = "";
  document.getElementById("descripcion["+i+"]").value = "";
  document.getElementById("cantidad["+i+"]").value    = "";
  document.getElementById("precio["+i+"]").value      = "";
  document.getElementById("iva["+i+"]").value         = "";
  document.getElementById("importe["+i+"]").value     = "";
  document.getElementById("impuesto["+i+"]").value    = "";
 }
 document.getElementById("subtotal").value = "";
 document.getElementById("ivatotal").value = "";
 document.getElementById("total").value    = "";
}

// ******************************
// ******************************
// Empieza la función cantidad
// ******************************
// ******************************

function cantidad(fila,opcion) {
 var precio          = parseFloat(document.getElementById("precio["+fila+"]").value);
 var iva             = parseFloat(document.getElementById("iva["+fila+"]").value);
 var cantidad_numero = parseInt(document.getElementById("cantidad["+fila+"]").value);
 var cantidad_cadena = cantidad_numero.toString();
 if (cantidad_cadena == "NaN") {
  window.alert("Ningún producto en esta línea")
 } else {
  if (opcion == "sumar") {
   var cantidad = cantidad_numero + 1;
  } else if (cantidad_numero == 1) {
    borrarproducto(fila);
    return;
   } else {
   var cantidad = cantidad_numero - 1;   
  }
  var importe = redondear(cantidad * precio);
  var impuesto = redondear(cantidad * iva);
  document.getElementById("cantidad["+fila+"]").value = cantidad;
  document.getElementById("importe["+fila+"]").value  = importe;
  document.getElementById("impuesto["+fila+"]").value = impuesto;
  calculartotal();
 }
}

// *********************************
// *********************************
// Empieza la función borrarproducto
// *********************************
// *********************************

function borrarproducto(fila) {
 var referencia  = "";
 var descripcion = "";
 var cantidad    = "";
 var precio      = "";
 var iva         = "";
 var importe     = "";
 var impuesto    = "";
 document.getElementById("referencia["+fila+"]").value  = referencia;
 document.getElementById("descripcion["+fila+"]").value = descripcion;
 document.getElementById("cantidad["+fila+"]").value    = cantidad;
 document.getElementById("precio["+fila+"]").value      = precio;
 document.getElementById("iva["+fila+"]").value         = iva;
 document.getElementById("importe["+fila+"]").value     = importe;
 document.getElementById("impuesto["+fila+"]").value    = impuesto;
 calculartotal();
}

// ********************************
// ********************************
// Empieza la función calculartotal
// ********************************
// ********************************

function calculartotal() {
 var subtotal = 0;
 var ivatotal = 0;
 var total = 0;
 for (var i = 0;i < maximo_lineas;i++) {
  importe_numero[i]  = parseFloat(document.getElementById("importe["+i+"]").value);
  impuesto_numero[i] = parseFloat(document.getElementById("impuesto["+i+"]").value);
  importe_cadena[i]  = importe_numero[i].toString();
  impuesto_cadena[i] = impuesto_numero[i].toString();
  if (importe_cadena[i] == "NaN") {
   importe[i] = 0;
   impuesto[i] = 0;
  } else {
   importe[i] = importe_numero[i];
   impuesto[i] = impuesto_numero[i];
   subtotal = redondear(parseFloat(subtotal) + parseFloat(importe[i]));
   ivatotal = redondear(parseFloat(ivatotal) + parseFloat(impuesto[i]));
  }
 }
 if (subtotal == 0) {
  subtotal = "";
  ivatotal = "";
  total    = "";
 } else {
  var total = redondear(parseFloat(subtotal) + parseFloat(ivatotal));
 }
 // Refrescar los objetos del formulario
 document.getElementById("subtotal").value = subtotal;
 document.getElementById("ivatotal").value = ivatotal;
 document.getElementById("total").value    = total;
}

// -->