var precio_internet = 0.0, precio_real = 0.0;
//LEER XML PARA EL IVA DE LOS PORTES
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","ivaportes.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var ivaTip0, ivaTip1, ivaTip2;
ivaTip0 = xmlDoc.getElementsByTagName("tipo0")[0].childNodes[0].nodeValue;
ivaTip1 = xmlDoc.getElementsByTagName("tipo1")[0].childNodes[0].nodeValue;
ivaTip2 = xmlDoc.getElementsByTagName("tipo2")[0].childNodes[0].nodeValue;

//COMPRUEBA SI ES UNA DIRECCION DE CORREO ELECTRÓNICO CORRECTA
function validaEmail(elEmail){
	/*Con expresiones regulares
	NOTA: No se contemplan extensiones de dominio de más de 4 caracteres (.museum, .store) son mut poco frecuentes
	      No se contemplan las direcciones de correo con formato IP
	1.- \w+							---> que tenga al menos una palabra
	2.- ((-\w+)|(\.\w+)|(\_\w+))*	---> que tenga cero o más palabras precedidas de un (.), un (-) o un (_)
	3.- \@							---> una y sólo una @
	4.- [A-Za-z0-9]					---> sólo caracteres normales y números
	5.- ((.|-)[A-Za-z0-9]+)*		---> solo caracteres normales y números precedidos de un (.) o un (-)
	6.- \.							---> el último punto
	7.- [A-Za-z]{2,4}				---> de 2 a 4 caracteres después del punto. (4 por .info, .name)
	*/
	//	return (elEmail.search( /\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,4}/ ) != -1);
	// ¿Soporta expresiones regulares?
	var soporta = false;
	if (window.RegExp) {
		var aux = "a";
		var auxReg = new RegExp(aux);
		if (auxReg.test(aux)) soporta = true;
	}
	if (!soporta) return (elEmail.indexOf(".") > 2) && (elEmail.indexOf("@") > 0);
	var exp1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)");
	var exp2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
	return (!exp1.test(elEmail) && exp2.test(elEmail));
}

//COMPRUEBA QUE NO SE TECLEAN ESPACIOS, GUIONES, ETC
function quitaEspacios(cad){
	var malos = new String(' -.:_,');
	var nueva_cad = '';
	for (i=0;i<cad.length;i++){
		s=cad.substring(i, i+1);
		if (malos.indexOf(s)==-1) nueva_cad = nueva_cad + s;
	}
	return nueva_cad;
}

function sinEspacios(campo){
	var correcta = quitaEspacios(campo.value);
	if (correcta != campo.value){
		alert('Por favor, cumplimente este campo sin espacios, puntos, guiones, etc');
		campo.value = correcta;
		campo.focus();
		campo.select();
		return false;
	}else
		return true;
}

//CAMBIA EL NOMBRE DEL CAMPO PARA EL MENSAJE DE ERRORES
function nombreCampo(nomcam){
	switch (nomcam){
	   	case 'Nombre': 
			return 'Nombre o Persona de contacto';
			break;
	   	case 'Apellidos': 
			return 'Apellidos o Razón social';
			break;
	   	case 'NumCliente': 
			return 'Nº Licencia';
			break;
	   	case 'Direccion': 
			return 'Dirección';
			break;
	   	case 'Poblacion':
			return 'Población';
			break;
	   	case 'CodigoPostal':
			return 'Código Postal';
			break;
	   	case 'TelNumero':
			return 'Teléfono';
			break;
	   	case 'FaxNumero':
			return 'Fax';
			break;
	   	case 'NIF':
			return 'N.I.F./C.I.F.';
			break;
	   	case 'CorreoElectronico':
			return 'Correo electrónico';
			break;
	   	case 'fecha_entrega':
			return 'Fecha de entrega';
			break;
	   	default: return nomcam;
	}
}

//CONVIERTE UNA CADENA EN UN OBJETO x EL NOMBRE
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

//COMPRUEBA SI LO CAMPOS CUMPLIMENTADOS SON CORRECTOS
function validaPedido(){
	var i,p,q,nm,test,num,min,max,errors='',args=validaPedido.arguments;
	for (i=0; i<(args.length-2); i+=3){
		test=args[i+2];
		val=MM_findObj(args[i]);
		if (val){ 
			nm=nombreCampo(val.name);
			if ((val=val.value)!=""){
				if (test.indexOf('isEmail')!=-1){
					p=validaEmail(val);
					if (!p) errors+='- '+nm+' debe contener una dirección de correo electrónico correcta.\n';
				}else if (test!='R'){
					if (isNaN(val)) errors+='- '+nm+' debe contener un número.\n';
					if (test.indexOf('inRange') != -1) {
						p=test.indexOf(':');
						min=test.substring(8,p);
						max=test.substring(p+1);
						if (val<min || max<val) errors+='- '+nm+' debe contener un número entre '+min+' y '+max+'.\n';
					}
				}
			}else if (test.charAt(0) == 'R') errors += '- '+nm+' es obligatorio.\n';
		}
 	}
	val=MM_findObj('tot_pedido');
	if (parseFloat(val.value) <= 0) errors+='- Debe introducir el nº de unidades en algún producto.\n';
	lista =document.Pedido.Provincia;  
	laProv=lista.options[lista.selectedIndex].value;
	if (laProv.substring(0,1)=="2" && document.Pedido.FormaPago[0].checked && document.Pedido.FormaPago[0].value == "Contra reembolso") errors+='- La Forma de Pago para esta provincia NO puede ser Contra reembolso.\n';
	if (document.Pedido.Condiciones.checked==false) errors+='- Debe leer y aceptar las condiciones del pedido.\n';
	if (document.Pedido.Cliente)
		if (document.Pedido.Cliente[0].checked==true && document.Pedido.NumCliente.value=="") errors+='- Si es cliente, Nº de licencia es obligatorio.\n';
	if (document.Pedido.Provincia.value=="" || document.Pedido.Provincia.value=="000") errors+='- Provincia es obligatorio.\n';
	if (document.Pedido.Profesion){
		if (document.Pedido.Profesion.value=="") errors+='- Profesión es obligatorio.\n';
		if (document.Pedido.Profesion.value=="Otra" && document.Pedido.Otra.value=="") errors+='- Profesión es obligatorio.\n';
	}
//	if (document.Pedido.pastilla)
//	   if (!(document.Pedido.pastilla[0].checked || document.Pedido.pastilla[1].checked) && document.Pedido.Cliente[1].checked==true)
//	      errors+='- Debe seleccionar el tipo de dispositivo de llave electrónica .\n';
	if (errors) alert('Se han producido los siguientes errores:\n'+errors);

	document.MM_returnValue = (errors == '');
}

if (document.images){
  var obligado=new Image();
  obligado.src="http://www.cype.es/principal/imagen/formularios/obligacionP.gif";
  var NOobligado=new Image();
  NOobligado.src="http://www.cype.es/principal/transparent.gif";
}

//PONER MARCA DE CAMPO OBLIGATORIO
function ponerObli(nombreImg,campo){
  if (document.images){
	objto=MM_findObj(campo);
    document[nombreImg].src=obligado.src;
	objto.disabled=0;

	var tb = document.getElementById('tbNOCliente');
	if (campo == 'NumCliente' && tb){
		tb.style.display = "block";
		var nc = document.getElementById('NumCliente');
		nc.focus();
	}

	tb = document.getElementById('tbPredimensionadores');
	if (campo == 'NumCliente' && tb) tb.style.display = "block";

	tb = document.getElementById('ofertaGenPres');
	if (campo == 'NumCliente' && tb){
		var contenido = "";
		contenido += "<b>* &iexcl; OFERTA DE LANZAMIENTO !</b> <br>";
		contenido += "<li>Si dispone de Predimensionadores:<br>"
		contenido += "1&ordm; Generador de Presupuestos a 108 &euro;, 2&ordm; a 75,60 &euro;<br>"
		contenido += "y siguientes a 54 &euro;. Precios v&aacute;lidos para licencias monopuesto</li><br>"
		contenido += "<li>Si no dispone de Predimensionadores:<br>"
		contenido += "1&ordm; Generador de Presupuestos a 189 &euro;, 2&ordm; a 132,30 &euro;<br>"
		contenido += "y siguientes a 94,50 &euro;. Precios v&aacute;lidos para licencias monopuesto</li>"
		tb.innerHTML = contenido;
	}
	
	tb = document.getElementById('predA1');
	var capaPred;
	if (campo == 'NumCliente' && tb){
		for (i=1;i<7;i++){
			if (document.getElementById('chP'+i).checked){
				capaPred = document.getElementById('chA'+i);
				capaPred.checked = true;
				capaPred = document.getElementById('predA'+i);
				capaPred.style.display = "block";
			}
		}
		sumaP02();
	}
  }
}
//QUITAR MARCA DE CAMPO OBLIGATORIO
function quitarObli(nombreImg,campo){
  if (document.images){
	objto=MM_findObj(campo);
    document[nombreImg].src=NOobligado.src;
	objto.value="";
	objto.disabled=1;
	var tb = document.getElementById('tbNOCliente');
	if (campo == 'NumCliente' && tb) tb.style.display = "none";

	tb = document.getElementById('NumPromocion');
	if (campo == 'NumCliente' && tb){
		tb.value = "";
	}

	tb = document.getElementById('linDto');
	if (campo == 'NumCliente' && tb){
		tb.style.display = "none";
		document.getElementById('descuento').value = "0,00";
		document.getElementById("descuentoPor").value = "";
		suma();
	}

	tb = document.getElementById('tbPredimensionadores');
	if (campo == 'NumCliente' && tb) tb.style.display = "none";

	tb = document.getElementById('ofertaGenPres');
	if (campo == 'NumCliente' && tb){
		var cm = document.getElementById('mejoras0');
		if (cm){
			if (cm.checked){
				cm.checked = false;
				document.getElementById('mejoras1').checked=true;
				document.getElementById("lineaMejoras").style.display = "none";
			}
		}
		var contenido = "";
		contenido += "<b>* &iexcl; OFERTA DE LANZAMIENTO !</b> <br>";
		contenido += "<li>1&ordm; Generador de Presupuestos a 210 &euro;, 2&ordm; a 147 &euro;<br>"
		contenido += "y siguientes a 105 &euro;. Precios v&aacute;lidos para licencias monopuesto</li>"
		tb.innerHTML = contenido;
		var genPres = document.getElementById("sumPredimensionadores");
		if (genPres) {
			for (i=1;i<=6;i++){
				chA = document.getElementById("chA"+i);
				chA.checked = false;
			}
			genPres.value="0,00";
			document.getElementById("dtoPredimensionadores").value="0,00";
			sumaP0();
		}
	}
	tb = document.getElementById('predA1');
	var capaPred;
	if (campo == 'NumCliente' && tb){
		for (i=1;i<7;i++){
			capaPred = document.getElementById('chA'+i);
			capaPred.checked = false;
			capaPred = document.getElementById('predA'+i);
			capaPred.style.display = "none";
		}
		sumaP02();
	}
  }
}

//SI SE ELIJE COMO PROFESION "OTRA" SE HACE CAMPO OBLIGATORIO
function esOtra(campo){
  if (campo.value == "Otra"){
      ponerObli('obliotra','Otra');
  } else if (campo.value == "#"){
  	        document.Pedido.Profesion.value="Otra";
      		ponerObli('obliotra','Otra');
	     } else {
            quitarObli('obliotra','Otra');
         }
}

//COMPRUEBA FORMA DE PAGO EN CANARIAS
function comprobar(){
  lista =document.Pedido.Provincia;  
  laProv=lista.options[lista.selectedIndex].value;
  if (laProv.substring(0,1)=="2"){
       canarias=0; 
     }else{
       canarias=1;    
     };
  if (document.Pedido.FormaPago[0].checked && canarias==0){
      alert("La Forma de Pago para esta Provincia debe ser Tarjeta o Transferencia Bancaria");
      document.Pedido.FormaPago[1].checked=true;
  }
}

//ABRE UNA VENTANA NUEVA DEL NAVEGADOR
function AbreVentana(pagina, nombre, ancho, alto, barra_scroll,resiz) {
	var winl = (screen.width - ancho) / 2;
	var wint = (screen.height - alto) / 2;
	winprops = 'height='+alto+',width='+ancho+',top='+wint+',left='+winl+',scrollbars='+barra_scroll+',resizable='+resiz;
	win = window.open(pagina, nombre, winprops);
	if (parseInt(navigator.appVersion) >= 4) win.focus();
}

//FORMATEA UN DATO NUMERICO PARA PONER LOS PUNTOS DE LOS MILES
function PonPuntos(numero) {
 if (numero == NaN) numero = "0";
 numero = ""+numero; 
 if (numero.length > 3) {
   mod = numero.length % 3;
   resultado = (mod > 0 ? (numero.substring(0,mod)) : "");
   for (i=0 ; i < Math.floor(numero.length / 3); i++) {
    if ((mod == 0) && (i == 0))
      resultado += numero.substring(mod+ 3 * i, mod + 3 * i + 3);
    else
      resultado+= "." + numero.substring(mod + 3 * i, mod + 3 * i + 3);
   }
   return (resultado);
 }
 else return numero;
}

//FORMATEA UN DATO NUMERICO PARA PONERLO EN EUROS
function PonEuros(numeuro) {
//Redondeo a dos decimales el valor en Euros
   euros_red=""+(Math.round(numeuro*100)/100);
//Cambio el punto decimal por una coma
   punto = euros_red.indexOf(".");
   if (punto >= 0){ 
     aux = euros_red.substring(0,punto);
     aux=PonPuntos(aux)+",";
     decimales = ""+euros_red.substring(punto+1,euros_red.length);
     if (decimales.length == 1)
        decimales+= "0";
     aux += decimales;
   }else{
     aux=PonPuntos(euros_red)+",00";
   }
   return (aux);
}
//CÁLCULO DEL CARACTER DEL CONTROL DEL CIF
function controlCIF(n){
	if (!isNaN(n)){
		var digitos = new Array();
		for (i=0;i<7;i++) digitos[i]=parseInt(n.substring(i,i+1));
		var A = digitos[1] + digitos[3] + digitos[5];
		var B = 0;
		var impar = "";
		for (i=0;i<7;i+=2){
			impar = ""+(digitos[i] * 2);
			if (impar.length == 2) B = B + (parseInt(impar.substring(0,1))+parseInt(impar.substring(1,2)))
			else B = B + parseInt(impar);
		}
		var C = A + B;
		C = ""+C;
		var l = C.length;
		var uniC = 10 - parseInt(C.substring(l-1,l));
		var letras = "JABCDEFGHI";
		return(uniC+letras.charAt(uniC));
	}else return '  ';
}
//CÁLCULO DEL CARACTER DEL CONTROL DEL NIF
function controlNIF(n){
	if (!isNaN(n)){
		var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
		var resto = n % 23;
		return(letras.charAt(resto));
	}else return ' ';
}

//VALIDA UN CIF o NIF SI ES ESPAÑOL
function validaNIFCIF (campo,quePais){
	var letrasCIF = new String('ABCDEFGHKLMNPQS');
	var nifcif = quitaEspacios(campo.value);
	var v = true;
	nifcif = nifcif.toUpperCase();
	campo.value = nifcif;
	if (document.Pedido.Pais)
		elPais = document.Pedido.Pais.value;  
	else
		elPais = "111";  
	if (elPais.substring(0,1)=="1"){
		digi1=nifcif.substring(0,1);
		if (letrasCIF.indexOf(digi1)!=-1){
			if (nifcif.length != 9) v = false
			else{
				digi2=nifcif.substring(1,8);
				digi3=nifcif.substring(8,9);
				cc = controlCIF(digi2);
				v = (digi3 == cc.substring(0,1) || digi3 == cc.substring(1,2));
			}
		}else{
			l=nifcif.length;
			digi1=nifcif.substring(0,l-1);
			digi3=nifcif.substring(l-1,l);
			v = (digi3 == controlNIF(digi1));
		}
		if (!v) alert('El valor tecleado NO coincide con\nlas reglas de validación del NIF/CIF.\nPor favor, compruebe que lo ha tecleado correctamente.');
	}
}
//CÁLCULO DEL TOTAL DEL PEDIDO INCLUIDOS LOS PORTES
function suma(){
	var pvp_eur=0.0, imp_eur = 0.0, iva_eur = 0, tot_eur = 0.0, tot_eur_prod = 0.0, iva=0, i=0, unidades=0;
	var tot_kg=0.0, imp_portes=0.0, pesodemas=0.0, preciodemas=0.0, iva_portes=0.0;
	var fpago=document.Pedido.FormaPago;
	var contrareembolso = (fpago[0].value == "Contra reembolso");
	var todos1 = 1, pvCjto = 0.0, tot_sinIVA = -1;
	var productoBase = false, esCliente=false, idProd = "", desProd = "", msgMod="", esModulo=false;
	var alguna_unidad = false;
	var tieneDto = document.getElementById("linDto");
	var queDto = document.getElementById("descuentoPor");
	var porDto = 0.0;

	if (tieneDto){
		if (queDto){
			if (queDto.value != "")
				porDto = parseFloat(document.getElementById("descuentoPor").value.replace(/%/,""))
			else
				porDto = 0.0;				
		}
	}

	lista =document.Pedido.Pais;  
	elPais=lista.options[lista.selectedIndex].value;
	lista =document.Pedido.Provincia;  
	laProv=lista.options[lista.selectedIndex].value;
	if (elPais.substring(0,1)=="0"){
		iva=0;   
	}else{ 
		if (laProv.substring(0,1)=="0" || laProv.substring(0,1)=="2"){
			iva=0; 
			if (laProv.substring(0,1)=="2" && fpago[0].checked && contrareembolso){
				document.Pedido.FormaPago[1].checked=true;
			}
		}else{
			iva=1;    
		}
	};  
// PRODUCTOS
	esCliente = document.Pedido.Cliente[0].checked;
	while (document.Pedido["uni"+i]){
		document.Pedido["imp"+i].value="";
		document.Pedido["iva"+i].value="";
		document.Pedido["tot"+i].value="";
		unidades = parseInt(document.Pedido["uni"+i].value);
		if (isNaN(unidades) || unidades < 0) unidades = 0;
		//COMPRUEBA SI SE HA MARCADO ALGUN MODULO SIN SER CLIENTE Y SIN HABER MARCADO EL PROGRAMA BASE
		idProd = document.Pedido["pro"+i].value;
		if (idProd=="MBT" || idProd=="IBT" || idProd=="M2D" || idProd=="M3D" || idProd=="MUC" || idProd=="MUP" || idProd=="ECO"){
			if (unidades > 0) productoBase = true
			else{
				if (desProd == "") desProd=document.Pedido["red"+i].value
				else desProd = desProd + " o " + document.Pedido["red"+i].value;
			}
		}
		esModulo = (idProd=="EBT" || idProd=="MEZ" || idProd=="MEE" || idProd=="MPA" || idProd=="MCD");
		if (esModulo==true && productoBase==false && esCliente==false && unidades > 0){
			msgMod = "Si quiere adquirir "+document.Pedido["red"+i].value + " debe tener, al menos, una licencia\n";
			msgMod = msgMod + "del programa " + desProd + " o marcar en\n";
			msgMod = msgMod + "este pedido el programa " + desProd + "\n";
			unidades = 0;
			alert(msgMod);			
		}

		document.Pedido["uni"+i].value=unidades;
		if (unidades > 1) todos1 = 0; 
		if ( unidades > 0){
			cad_aux=document.Pedido["pes"+i].value;
			cad_aux=cad_aux.replace(",",".");
			tot_kg = tot_kg + (parseFloat(cad_aux) * unidades);

			cad_aux=document.Pedido["pvi"+i].value;
			cad_aux=cad_aux.replace(",",".");
			pvp_eur = parseFloat(cad_aux);
			imp_eur=unidades * pvp_eur;
			tot_sinIVA = tot_sinIVA + imp_eur;
			document.Pedido["imp"+i].value=PonEuros(imp_eur);
			if (iva == 1) iva_eur=imp_eur*parseInt(document.Pedido["ivp"+i].value)/100;
			document.Pedido["iva"+i].value=PonEuros(iva_eur);
			tot_eur_prod = imp_eur + iva_eur;
			document.Pedido["tot"+i].value=PonEuros(tot_eur_prod);
			tot_eur = tot_eur + (Math.round(tot_eur_prod*100)/100);
		}
		if (document.Pedido["uni"+i].value != "0") alguna_unidad = true; //Para saber si calculo los portes
		i++;
	}
// SI LA SUMA DE LOS PRODUCTOS SUPERA EL PRECIO DEL CONJUNTO DE PROGRAMAS
//Si en algún producto se ha pedido más de 1 unidad ya no se calcula
	if (document.Pedido.uniCjto)
	  if (document.Pedido.uniCjto.value != "" && todos1){
		cad_aux=document.Pedido.pviCjto.value;
		cad_aux=cad_aux.replace(",",".");
		pvCjto = parseFloat(cad_aux);
		if (tot_sinIVA >= pvCjto){
			alert('Le sale más económico el conjunto de programas que esos programas sueltos');
			i = 0;
			while (document.Pedido["uni"+i]){
				cad_aux = document.Pedido.uniCjto.value;
				if (cad_aux == "uni"+i){
					document.Pedido["uni"+i].value="1";
					cad_aux=document.Pedido["pes"+i].value;
					cad_aux=cad_aux.replace(",",".");
					tot_kg = parseFloat(cad_aux);
					cad_aux=document.Pedido["pvi"+i].value;
					cad_aux=cad_aux.replace(",",".");
					pvp_eur = parseFloat(cad_aux);
					imp_eur = pvp_eur;
					document.Pedido["imp"+i].value=PonEuros(imp_eur);
					if (iva == 1) iva_eur=imp_eur*parseInt(document.Pedido["ivp"+i].value)/100;
					document.Pedido["iva"+i].value=PonEuros(iva_eur);
					tot_eur_prod = imp_eur + iva_eur;
					document.Pedido["tot"+i].value=PonEuros(tot_eur_prod);
					tot_eur = Math.round(tot_eur_prod*100)/100;
				}else{
					document.Pedido["imp"+i].value="";
					document.Pedido["iva"+i].value="";
					document.Pedido["tot"+i].value="";
					document.Pedido["uni"+i].value="0";
				}
				i++;
			}
		}
	  }

// DESCUENTO PROMOCION
	if (queDto){
		if (porDto > 0){
			var impDto = tot_eur * (porDto/100);
			document.Pedido.descuento.value = PonEuros(impDto);
			tot_eur = tot_eur - impDto;
			tieneDto.style.display = "block";
		}else{
			document.Pedido.descuento.value = "0,00";
			tieneDto.style.display = "none";
		}
	}
	
// PORTES
	document.Pedido.euros_Portes.value="";
	document.Pedido.iva_Portes.value="";
	document.Pedido.tot_portes.value="";
	if (alguna_unidad == true){
		cod_porte=laProv.substring(1,3);
		if (document.Pedido.nomfam){
			var laFamilia = document.getElementById("nomfam").value;
			if (laFamilia == "CYPECAD" || laFamilia == "CYPECAD LT"){
				cod_porte = "00";
			}
		}
		for (i=0;i<nportes;i++)
			if (cod_porte == portes[i][0]){
				if (tot_kg<=parseInt(portes[i][1])) imp_portes = parseFloat(portes[i][2])
				else{
					pesodemas = tot_kg - parseInt(portes[i][1]);
					pesodemas = pesodemas / parseInt(portes[i][3]);
					preciodemas = Math.ceil(pesodemas) * parseFloat(portes[i][4]);
					imp_portes = parseFloat(portes[i][2]) + preciodemas;
				}
			}
		document.Pedido.euros_Portes.value = PonEuros(imp_portes);
		if (iva == 1) iva_portes=imp_portes*(ivaTip1/100)
		else iva_portes=0.0;
		document.Pedido.iva_Portes.value = PonEuros(iva_portes);
		imp_portes = imp_portes + iva_portes;
		document.Pedido.tot_portes.value = PonEuros(imp_portes);
	}
	
// TOTAL PEDIDO
	tot_eur = tot_eur + imp_portes;	
	document.Pedido.tot_pedido.value=PonEuros(tot_eur);
	tot_pts=Math.round(tot_eur*166.386);
	document.Pedido.tot_pedido_pts.value=PonPuntos(tot_pts);
}

function marca_chA(actual){
	var i=1, numPredim = 0, totPredim = 0.0, imp_eur=0.0, a=0, imp_real = 0.0, totReal = 0.0;


while (document.Pedido["chA"+i]){
		if (document.Pedido["chA"+i].checked==true){
		    numPredim++;
			if (numPredim == 1){
				 imp_eur = precio_internet;
				 imp_real = precio_real;
			}else  if (numPredim == 2){
				     imp_eur = precio_internet - (precio_internet*0.3);
				     imp_real = precio_real - (precio_real*0.3);
				  }else{
					 imp_eur = precio_internet - (precio_internet*0.5);
				     imp_real = precio_real - (precio_real*0.5);
				  }
			document.Pedido["imp"+i].value=PonEuros(imp_eur);
			document.Pedido["pvp"+i].value=PonEuros(imp_real);
		}else{
			document.Pedido["imp"+i].value=PonEuros(0.0);
			document.Pedido["pvp"+i].value=PonEuros(0.0);
			imp_eur = 0.0;
			imp_real = 0.0;
		}
		totPredim += imp_eur;
		totReal += imp_real;
		i++;
	}
	if (totPredim <=0){
		alert("Debe marcar al menos un predimensionador");
		i--;
		document.Pedido["imp"+actual].value=PonEuros(90.0);
		document.Pedido["pvp"+actual].value=PonEuros(119.0);
		document.Pedido["chA"+actual].checked=true;
		totPredim = precio_internet;
		totReal = precio_real;
	}
	document.Pedido.imp_parcial.value = PonEuros(totPredim);
	document.Pedido.imp_real.value = PonEuros(totReal);
	sumaA0();
}
//CÁLCULO DEL TOTAL DEL PEDIDO [[SÓLO PREDIMENSIONADORES]] INCLUIDOS LOS PORTES
function sumaA0(){
	var pvp_eur=0.0, imp_eur = 0.0, iva_eur = 0, tot_eur = 0.0, tot_eur_prod = 0.0, iva=0, i=0, unidades=0;
	var tot_kg=0.0, imp_portes=0.0, pesodemas=0.0, preciodemas=0.0, iva_portes=0.0;
	var fpago=document.Pedido.FormaPago;
	var contrareembolso = (fpago[0].value == "Contra reembolso");
	var todos1 = 1, pvCjto = 0.0, tot_sinIVA = -1;
	var productoBase = false, esCliente=false, idProd = "", desProd = "", msgMod="", esModulo=false;
	var alguna_unidad = false;
	if (precio_internet == 0.0) precio_internet = parseFloat(document.Pedido.pvi1.value)
	if (precio_real == 0.0) precio_real = parseFloat(document.Pedido.pvp1.value);

	lista =document.Pedido.Pais;
	elPais=lista.options[lista.selectedIndex].value;
	lista =document.Pedido.Provincia;
	laProv=lista.options[lista.selectedIndex].value;
	if (elPais.substring(0,1)=="0"){
		iva=0;   
	}else{ 
		if (laProv.substring(0,1)=="0" || laProv.substring(0,1)=="2"){
			iva=0; 
			if (laProv.substring(0,1)=="2" && fpago[0].checked && contrareembolso){
				document.Pedido.FormaPago[1].checked=true;
			}
		}else{
			iva=1;    
		}
	};  
	while (document.Pedido["uni"+i]){
		if (document.Pedido["chA"+i].checked==true){
		  document.Pedido["uni"+i].value = document.Pedido.unidades.value;
		}
    	document.Pedido["pvi"+i].value = document.Pedido["imp"+i].value;
		i++;
	}
// PRODUCTOS
	esCliente = document.Pedido.Cliente[0].checked;
	document.Pedido.importe.value = document.Pedido.imp_parcial.value;
	unidades = parseInt(document.Pedido.unidades.value);
	if (isNaN(unidades) || unidades < 0) unidades = 0;
	cad_aux=document.Pedido.pes1.value;
	cad_aux=cad_aux.replace(",",".");
	tot_kg = tot_kg + (parseFloat(cad_aux) * unidades);
	cad_aux=document.Pedido.importe.value;
	cad_aux=cad_aux.replace(",",".");
	pvp_eur = parseFloat(cad_aux);
	imp_eur=unidades * pvp_eur;
	tot_sinIVA = tot_sinIVA + imp_eur;
	document.Pedido.baseimp.value=PonEuros(imp_eur);
	if (iva == 1) iva_eur=imp_eur*parseInt(document.Pedido.ivp1.value)/100;
	document.Pedido.iva.value=PonEuros(iva_eur);
	tot_eur_prod = imp_eur + iva_eur;
	document.Pedido.total.value=PonEuros(tot_eur_prod);
	tot_eur = tot_eur + (Math.round(tot_eur_prod*100)/100);
	if (document.Pedido.unidades.value != "0") alguna_unidad = true; //Para saber si calculo los portes
	
// PORTES
	document.Pedido.euros_Portes.value="";
	document.Pedido.iva_Portes.value="";
	document.Pedido.tot_portes.value="";
	if (alguna_unidad == true){
		cod_porte=laProv.substring(1,3);
		for (i=0;i<nportes;i++)
			if (cod_porte == portes[i][0]){
				if (tot_kg<=parseInt(portes[i][1])) imp_portes = parseFloat(portes[i][2])
				else{
					pesodemas = tot_kg - parseInt(portes[i][1]);
					pesodemas = pesodemas / parseInt(portes[i][3]);
					preciodemas = Math.ceil(pesodemas) * parseFloat(portes[i][4]);
					imp_portes = parseFloat(portes[i][2]) + preciodemas;
				}
			}
		document.Pedido.euros_Portes.value = PonEuros(imp_portes);
		if (iva == 1) iva_portes=imp_portes*(ivaTip1/100)
		else iva_portes=0.0;
		document.Pedido.iva_Portes.value = PonEuros(iva_portes);
		imp_portes = imp_portes + iva_portes;
		document.Pedido.tot_portes.value = PonEuros(imp_portes);
	}
// TOTAL PEDIDO
	tot_eur = tot_eur + imp_portes;	
	document.Pedido.tot_pedido.value=PonEuros(tot_eur);
	tot_pts=Math.round(tot_eur*166.386);
	document.Pedido.tot_pedido_pts.value=PonPuntos(tot_pts);
}


//CÁLCULO DEL TOTAL DEL PEDIDO INCLUIDOS LOS PORTES para los pedidos de AutoPEM
function getPVPautoPEM(npre){
	var pedUsuCOACV = new Array("119,00","83,30","59,50","59,50","59,50","59,50","238,00","0,00","294,00","294,00");
	return(pedUsuCOACV[npre-1]);
}
function getPVIautoPEM(ped, npre){
	var pedUsuCOACV = new Array("0,00","63,00","45,00","45,00","45,00","45,00","148,00","0,00","0,00","78,00");
	var pedCOACV = new Array("90,00","63,00","45,00","45,00","45,00","45,00","148,00","0,00","0,00","78,00");
	var pedUsuNOCOACV = new Array("0,00","83,30","59,50","59,50","59,50","59,50","238,00","0,00","0,00","119,00");
	switch(ped){
		case 1: return(pedUsuCOACV[npre-1]);
				break;
		case 2: return(pedCOACV[npre-1]);
				break;
		case 3: return(pedUsuNOCOACV[npre-1]);
				break;
	};
}

function sumaAutoPEM(ped){
	var iva=0, i=1, j=0, chPred=6, tot_kg=0.0, imp_portes=0, preciodemas=0.0;
	var numpred=0, sumaImp=0.0, impAux=0.0, unidades=0, baseImp=0.0, ivaImp=0.0, totImp=0.0;
	var fpago=document.Pedido.FormaPago;
	var contrareembolso = (fpago[0].value == "Contra reembolso");
	lista =document.Pedido.Pais;  
	elPais=lista.options[lista.selectedIndex].value;
	lista =document.Pedido.Provincia;  
	laProv=lista.options[lista.selectedIndex].value;
	if (elPais.substring(0,1)=="0"){
		iva=0;   
	}else{ 
		if (laProv.substring(0,1)=="0" || laProv.substring(0,1)=="2"){
			iva=0; 
			if (laProv.substring(0,1)=="2" && fpago[0].checked && contrareembolso){
				document.Pedido.FormaPago[1].checked=true;
			}
		}else{
			iva=1;    
		}
	};  
// PRODUCTOS
	i=1;
	numpred=1;
	chPred=6;
	while (i <= 10){
		cadAux = document.Pedido["pes"+i].value;
		tot_kg = parseFloat(cadAux.replace(/,/,"."));
		if (i > 6) j=i
		else j = numpred;
		document.Pedido["imp"+i].value="0,00";		
		document.Pedido["pvp"+i].value="0,00";
		if (document.Pedido["chA"+i].checked){
			document.Pedido["pvp"+i].value=getPVPautoPEM(j);
			document.Pedido["imp"+i].value=getPVIautoPEM(ped,j);
			cadAux = document.Pedido["imp"+i].value;
			sumaImp += parseFloat(cadAux.replace(/,/,"."));
			numpred++;
		}else{
			if (i<=6) chPred--;
		}
		i++;
	}
	if (chPred == 0){
		i=1;
		alert("Elija el Predimensionador que desea recibir gratuitamente");
		document.Pedido["chA"+i].checked = true;
		document.Pedido["pvp"+i].value=getPVPautoPEM(i);
		document.Pedido["imp"+i].value=getPVIautoPEM(ped,i);
		cadAux = document.Pedido["imp"+i].value;
		sumaImp += parseFloat(cadAux.replace(/,/,"."));
	}
	document.Pedido.importe.value = PonEuros(sumaImp);
	unidades = parseInt(document.Pedido["unidades"].value);
	if (isNaN(unidades) || unidades <= 0) unidades = 1;
	document.Pedido.unidades.value = unidades;
	baseImp = unidades * sumaImp;
	document.Pedido.baseimp.value = PonEuros(baseImp);
	if (iva == 1) ivaImp=sumaImp*parseInt(document.Pedido.ivp1.value)/100;
	document.Pedido.iva.value=PonEuros(ivaImp);
	totImp = sumaImp + ivaImp;
	document.Pedido.total.value=PonEuros(totImp);
	totImp = Math.round(totImp*100)/100;
	
	// PORTES
	document.Pedido.euros_Portes.value="";
	document.Pedido.iva_Portes.value="";
	document.Pedido.tot_portes.value="";
	cod_porte=laProv.substring(1,3);
	for (i=0;i<nportes;i++)
		if (cod_porte == portes[i][0]){
			if (tot_kg<=parseInt(portes[i][1])) imp_portes = parseFloat(portes[i][2])
			else{
				pesodemas = tot_kg - parseInt(portes[i][1]);
				pesodemas = pesodemas / parseInt(portes[i][3]);
				preciodemas = Math.ceil(pesodemas) * parseFloat(portes[i][4]);
				imp_portes = parseFloat(portes[i][2]) + preciodemas;
			}
		}
	document.Pedido.euros_Portes.value = PonEuros(imp_portes);
	if (iva == 1) iva_portes=imp_portes*(ivaTip1/100)
	else iva_portes=0.0;
	document.Pedido.iva_Portes.value = PonEuros(iva_portes);
	imp_portes = imp_portes + iva_portes;
	document.Pedido.tot_portes.value = PonEuros(imp_portes);
	// TOTAL PEDIDO
	totImp = totImp + imp_portes;	
	document.Pedido.tot_pedido.value=PonEuros(totImp);
	document.Pedido.tot_pedido_pts.value=PonPuntos(Math.round(totImp*166.386));
}

////////////GENERADORES DE PRESUPUESTOS///////////////////////

var pvpPredis = new Array(90,63,45,45,45,45,90,153,198,243,288,333);
var pvpPresus = new Array(120,84,60,60,60,60,210,147,105,105,105,105);
var sumdtoPredis = 0.0;
var numPredim=0, numPresus = 6;

function marca_chP(actual){
	var i=1, totPredim = 0.0, imp_eur=0.0, a=0, imp_real = 0.0, totReal = 0.0;

	numPresus=0;
	while (document.Pedido["chP"+i]){
		if (document.Pedido["chP"+i].checked==true){
		    numPresus++;
			if (numPresus == 1){
				 imp_eur = precio_internet;
				 imp_real = precio_real;
			}else  if (numPresus == 2){
				     imp_eur = precio_internet - (precio_internet*0.3);
				     imp_real = precio_real - (precio_real*0.3);
				  }else{
					 imp_eur = precio_internet - (precio_internet*0.5);
				     imp_real = precio_real - (precio_real*0.5);
				  }
			document.Pedido["imp"+i].value=PonEuros(imp_eur);
			document.Pedido["pvp"+i].value=PonEuros(imp_real);
		}else{
			document.Pedido["imp"+i].value=PonEuros(0.0);
			document.Pedido["pvp"+i].value=PonEuros(0.0);
			imp_eur = 0.0;
			imp_real = 0.0;
		}
		totPredim += imp_eur;
		totReal += imp_real;
		i++;
	}
	if (totPredim <=0){
		alert("Debe marcar al menos un Generador de Presupuestos");
		i--;
		document.Pedido["imp"+actual].value=PonEuros(210.0);
		document.Pedido["pvp"+actual].value=PonEuros(267.0);
		document.Pedido["chP"+actual].checked=true;
		numPresus=1;
		totPredim = precio_internet;
		totReal = precio_real;
	}
	document.Pedido.imp_parcial.value = PonEuros(totPredim);
	document.Pedido.imp_real.value = PonEuros(totReal);
	if (numPredim>0){
		var minimo = Math.min(numPredim, numPresus);
		sumdtoPredis = pvpPredis[5+minimo]*1.0;
		document.Pedido.dtoPredimensionadores.value = PonEuros(sumdtoPredis);
	}else{
		document.Pedido.dtoPredimensionadores.value = "0,00";
	}
	sumaP0();
}

function sumaPredi(){
	var numP = 0;
	var obj = null;
	sumdtoPredis = 0.0;
    numPredim=0;
	for (i=0;i<6;i++){
		obj = document.getElementById("chA"+(i+1));
		if (obj.checked){
			sumdtoPredis+=pvpPredis[numPredim];
		    numPredim++;
		}
	}
	document.Pedido.sumPredimensionadores.value = PonEuros(sumdtoPredis);

	if (numPredim>0){
		var minimo = Math.min(numPredim, numPresus);
		sumdtoPredis = pvpPredis[5+minimo]*1.0;
		document.Pedido.dtoPredimensionadores.value = PonEuros(sumdtoPredis);
	}else{
		document.Pedido.dtoPredimensionadores.value = "0,00";
	}
	sumaP0();
}

function sumaP0(){
	var pvp_eur=0.0, imp_eur = 0.0, iva_eur = 0, tot_eur = 0.0, tot_eur_prod = 0.0, iva=0, i=0, unidades=0;
	var tot_kg=0.0, imp_portes=0.0, pesodemas=0.0, preciodemas=0.0, iva_portes=0.0;
	var fpago=document.Pedido.FormaPago;
	var contrareembolso = (fpago[0].value == "Contra reembolso");
	var todos1 = 1, pvCjto = 0.0;
	var productoBase = false, esCliente=false, idProd = "", desProd = "", msgMod="", esModulo=false;
	var alguna_unidad = false;
	if (precio_internet == 0.0) precio_internet = parseFloat(document.Pedido.pvi1.value)
	if (precio_real == 0.0) precio_real = parseFloat(document.Pedido.pvp1.value);

	lista =document.Pedido.Pais;
	elPais=lista.options[lista.selectedIndex].value;
	lista =document.Pedido.Provincia;
	laProv=lista.options[lista.selectedIndex].value;
	if (elPais.substring(0,1)=="0"){
		iva=0;   
	}else{ 
		if (laProv.substring(0,1)=="0" || laProv.substring(0,1)=="2"){
			iva=0; 
			if (laProv.substring(0,1)=="2" && fpago[0].checked && contrareembolso){
				document.Pedido.FormaPago[1].checked=true;
			}
		}else{
			iva=1;    
		}
	};  
	while (document.Pedido["uni"+i]){
		if (document.Pedido["chP"+i].checked==true){
		  document.Pedido["uni"+i].value = document.Pedido.unidades.value;
		}
    	document.Pedido["pvi"+i].value = document.Pedido["imp"+i].value;
		i++;
	}
// PRODUCTOS
	esCliente = document.Pedido.Cliente[0].checked;
	document.Pedido.importe.value = document.Pedido.imp_parcial.value;
	unidades = parseInt(document.Pedido.unidades.value);
	if (isNaN(unidades) || unidades < 0) unidades = 0;
	cad_aux=document.Pedido.pes1.value;
	cad_aux=cad_aux.replace(",",".");
	tot_kg = tot_kg + (parseFloat(cad_aux) * unidades);
	cad_aux=document.Pedido.importe.value;
	cad_aux=cad_aux.replace(",",".");
	pvp_eur = parseFloat(cad_aux);
	imp_eur=(unidades * pvp_eur) - sumdtoPredis;
	document.Pedido.baseimp.value=PonEuros(imp_eur);
	if (iva == 1) iva_eur=imp_eur*parseInt(document.Pedido.ivp1.value)/100;
	document.Pedido.iva.value=PonEuros(iva_eur);
	tot_eur_prod = imp_eur + iva_eur;
	document.Pedido.total.value=PonEuros(tot_eur_prod);
	tot_eur = tot_eur + (Math.round(tot_eur_prod*100)/100);
	if (document.Pedido.unidades.value != "0") alguna_unidad = true; //Para saber si calculo los portes
	
// PORTES
	document.Pedido.euros_Portes.value="";
	document.Pedido.iva_Portes.value="";
	document.Pedido.tot_portes.value="";
	if (alguna_unidad == true){
		cod_porte=laProv.substring(1,3);
		for (i=0;i<nportes;i++)
			if (cod_porte == portes[i][0]){
				if (tot_kg<=parseInt(portes[i][1])) imp_portes = parseFloat(portes[i][2])
				else{
					pesodemas = tot_kg - parseInt(portes[i][1]);
					pesodemas = pesodemas / parseInt(portes[i][3]);
					preciodemas = Math.ceil(pesodemas) * parseFloat(portes[i][4]);
					imp_portes = parseFloat(portes[i][2]) + preciodemas;
				}
			}
		document.Pedido.euros_Portes.value = PonEuros(imp_portes);
		if (iva == 1) iva_portes=imp_portes*(ivaTip1/100)
		else iva_portes=0.0;
		document.Pedido.iva_Portes.value = PonEuros(iva_portes);
		imp_portes = imp_portes + iva_portes;
		document.Pedido.tot_portes.value = PonEuros(imp_portes);
	}
// TOTAL PEDIDO
	tot_eur = tot_eur + imp_portes;	
	document.Pedido.tot_pedido.value=PonEuros(tot_eur);
	tot_pts=Math.round(tot_eur*166.386);
	document.Pedido.tot_pedido_pts.value=PonPuntos(tot_pts);
}

////////////GENERADORES DE PRESUPUESTOS VERSIÓN 2 ///////////////////////

///PRECIOS ANTES DE julio 2006
//var pvpCON = new Array("148","103","74","74","74","74");
//var pvpSIN = new Array("267","186.3","133.5","133.5","133.5","133.5");
//var pviCON = new Array("120","84","60","60","60","60");
//var pviSIN = new Array("210","147","105","105","105","105");

var pvpCON = new Array("120","84","60","60","60","60");
var pvpSIN = new Array("240.3","167.67","120.15","120.15","120.15","120.15");
var pviCON = new Array("108","75.6","54","54","54","54");
var pviSIN = new Array("189","132.3","94.5","94.5","94.5","94.5");


function marca_chP2(gp,actual){
	var predAsoc = document.getElementById("predA"+actual);
	var esCli =  document.Pedido.Cliente[0].checked;
	if (gp.checked && esCli){
		predAsoc.style.display = "block";
		eval("document.Pedido.chA"+actual+"[0].checked=true");
		eval("document.Pedido.chA"+actual+"[1].checked=false");
	}else{
		predAsoc.style.display = "none";
		eval("document.Pedido.chA"+actual+"[0].checked=false");
		eval("document.Pedido.chA"+actual+"[1].checked=true");
	}
	sumaP02();
}

function sumaP02(){
	var numPres=0;
	var chPresu, chPredi, txtPvp, txtPvi;
	var totPvp=0.0, totPvi=0.0, elPvp, elPvi;
	
	for (i=0;i<6;i++){
		chPresu = document.getElementById("chP"+(i+1));
		if (chPresu.checked){
			chPredi = eval("document.Pedido.chA"+(i+1)+"[0].checked");
			if (chPredi){
				elPvp = parseFloat(pvpCON[numPres]);
				elPvi = parseFloat(pviCON[numPres]);
			}else{
				elPvp = parseFloat(pvpSIN[numPres]);
				elPvi = parseFloat(pviSIN[numPres]);
			}
			numPres++;
		}else{
			elPvp = 0.0;
			elPvi = 0.0;
		}
		txtPvp = document.getElementById("pvp"+(i+1));
		txtPvp.value = PonEuros(elPvp);
		txtPvi = document.getElementById("imp"+(i+1));
		txtPvi.value = PonEuros(elPvi);
		totPvp += elPvp;
		totPvi += elPvi;
	}

	if (numPres==0){
		alert('Debe seleccionar al menos un Generador de Presupuestos');
		document.Pedido.chP1.checked=true;
		elPvp = parseFloat(pvpSIN[0]);
		elPvi = parseFloat(pviSIN[0]);
		numPres=1;
		txtPvp = document.getElementById("pvp1");
		txtPvp.value = PonEuros(elPvp);
		txtPvi = document.getElementById("imp1");
		txtPvi.value = PonEuros(elPvi);
		totPvp += elPvp;
		totPvi += elPvi;
			
	}
	document.getElementById("imp_real").value = PonEuros(totPvp);
	document.getElementById("imp_parcial").value = PonEuros(totPvi);

	var lista =document.Pedido.Pais;
	var elPais=lista.options[lista.selectedIndex].value;
	lista =document.Pedido.Provincia;
	var laProv=lista.options[lista.selectedIndex].value;
	var iva=0, laBase=0.0;
	var fpago=document.Pedido.FormaPago;
	var contrareembolso = (fpago[0].value == "Contra reembolso");

	if (elPais.substring(0,1)=="0"){
		iva=0;   
	}else{ 
		if (laProv.substring(0,1)=="0" || laProv.substring(0,1)=="2"){
			iva=0; 
			if (laProv.substring(0,1)=="2" && fpago[0].checked && contrareembolso){
				document.Pedido.FormaPago[1].checked=true;
			}
		}else{
			iva=1;    
		}
	};  
	
	i=1;
	while (document.Pedido["uni"+i]){
		if (document.Pedido["chP"+i].checked==true){
		  document.Pedido["uni"+i].value = document.Pedido.unidades.value;
		}
    	document.Pedido["pvi"+i].value = document.Pedido["imp"+i].value;
		i++;
	}
	
	var imp_eur = 0.0, iva_eur = 0, tot_eur=0.0;
	var tot_kg=0.0, imp_portes=0.0, pesodemas=0.0, preciodemas=0.0, iva_portes=0.0;
	
//CONTRATO MEJORAS
		var cm = document.getElementById('mejoras0');
		if (cm){
			if (cm.checked){
				totPvi = totPvi / 2;
				document.Pedido.impmejoras.value = PonEuros(totPvi);
			}else{
				document.Pedido.impmejoras.value = PonEuros(0.0);
			}
		}

	laBase = totPvi * parseInt(document.Pedido.unidades.value);
	document.Pedido.baseimp.value = PonEuros(laBase);
//IVA	
	if (iva == 1) iva_eur=laBase*parseInt(document.Pedido.ivp1.value)/100;
	document.Pedido.iva.value=PonEuros(iva_eur);
	tot_eur_prod = laBase + iva_eur;
	document.Pedido.total.value=PonEuros(tot_eur_prod);
	tot_eur = tot_eur + (Math.round(tot_eur_prod*100)/100);
	if (document.Pedido.unidades.value != "0") alguna_unidad = true; //Para saber si calculo los portes
	
// PORTES
	document.Pedido.euros_Portes.value="";
	document.Pedido.iva_Portes.value="";
	document.Pedido.tot_portes.value="";
	if (alguna_unidad == true){
		cod_porte=laProv.substring(1,3);
		for (i=0;i<nportes;i++)
			if (cod_porte == portes[i][0]){
				if (tot_kg<=parseInt(portes[i][1])) imp_portes = parseFloat(portes[i][2])
				else{
					pesodemas = tot_kg - parseInt(portes[i][1]);
					pesodemas = pesodemas / parseInt(portes[i][3]);
					preciodemas = Math.ceil(pesodemas) * parseFloat(portes[i][4]);
					imp_portes = parseFloat(portes[i][2]) + preciodemas;
				}
			}
		document.Pedido.euros_Portes.value = PonEuros(imp_portes);
		if (iva == 1) iva_portes=imp_portes*(ivaTip1/100)
		else iva_portes=0.0;
		document.Pedido.iva_Portes.value = PonEuros(iva_portes);
		imp_portes = imp_portes + iva_portes;
		document.Pedido.tot_portes.value = PonEuros(imp_portes);
	}
// TOTAL PEDIDO
	tot_eur = tot_eur + imp_portes;	
//	alert(tot_eur + "+" + imp_portes + "="+tot_eur);
	document.Pedido.tot_pedido.value=PonEuros(tot_eur);
	tot_pts=Math.round(tot_eur*166.386);
	document.Pedido.tot_pedido_pts.value=PonPuntos(tot_pts);
}

function contratoMejoras(campo){

	var tb = document.getElementById('ofertaGenPres');

	if (tb)
		if (campo.id == 'mejoras0'){
			var contenido = "";
			contenido += "<b>* &iexcl; OFERTA DE LANZAMIENTO + CONTRATO DE MEJORAS! (AL FINAL DEL PEDIDO SE APLICA EL 50% DESCUENTO)</b> <br>";
			contenido += "<li>Si dispone de Predimensionadores:<br>"
			contenido += "1&ordm; Generador de Presupuestos a 54 &euro;, 2&ordm; a 37,80 &euro;<br>"
			contenido += "y siguientes a 27 &euro;. Precios v&aacute;lidos para licencias monopuesto</li><br>"
			contenido += "<li>Si no dispone de Predimensionadores:<br>"
			contenido += "1&ordm; Generador de Presupuestos a 94,50 &euro;, 2&ordm; a 66,15 &euro;<br>"
			contenido += "y siguientes a 47,25 &euro;. Precios v&aacute;lidos para licencias monopuesto</li>"
			tb.innerHTML = contenido;
			document.getElementById("lineaMejoras").style.display = "block";
		}else{
			var contenido = "";
			contenido += "<b>* &iexcl; OFERTA DE LANZAMIENTO !</b> <br>";
			contenido += "<li>Si dispone de Predimensionadores:<br>"
			contenido += "1&ordm; Generador de Presupuestos a 108 &euro;, 2&ordm; a 75,60 &euro;<br>"
			contenido += "y siguientes a 54 &euro;. Precios v&aacute;lidos para licencias monopuesto</li><br>"
			contenido += "<li>Si no dispone de Predimensionadores:<br>"
			contenido += "1&ordm; Generador de Presupuestos a 189 &euro;, 2&ordm; a 132,30 &euro;<br>"
			contenido += "y siguientes a 94,50 &euro;. Precios v&aacute;lidos para licencias monopuesto</li>"
			tb.innerHTML = contenido;
			document.getElementById("lineaMejoras").style.display = "none";
		}
	sumaP02();
}



//////////////////////////////////////////////////
