//variables que pueden cambiar
var IVA_PENINSULA_BALEARES = 0.16;
var IVA_EXENTO = 0;
var IVA_IGIC2 = 0.02;
var IVA_IGIC5 = 0.05;
var IVA_IPSI_MELILLA = 0.04;
var IVA_IPSI_CEUTA = 0.03;
var ue_con_iva = new Array('ALE','AUS','BEL','BUL','CHE','CIP','DIN','EEB','ESK','ESL','FIN','FRA','GBA','GRE','HUN','IRL','ITA','LUX','LVT','LTN','MON','MTA','POL','POR','RUM','SUE','PBS');
//Si cambian los servicios adicionales de Postal Expres Nacional, hay que revisar la función cambiaOrigenAdmision

//funciones

function PopUp (nombre,url,alto,ancho) {
	X = (screen.width - ancho) / 2;
	Y = (screen.height - alto) / 2;
	nombre = window.open(url, nombre, "width=" + ancho + ",height=" + alto + ",top=" + Y + ",left=" + X + ",status=no,menubar=no,scrollbars=yes,resizable=no,location=no,toolbar=no,directories=no,fullsreen=no");
	if (document.images) nombre.focus();
}

function validaTexto(campo) {
	var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú ";
	var allValid = true;

	for (i = 0; i < campo.length; i++) {
		ch = campo.charAt(i);
		for (j = 0; j < checkOK.length; j++)		
			if (ch == checkOK.charAt(j))
				break;

		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid)
		campo.focus();
	return allValid;
}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

// comprueba que el valor introducido sea un número
function valNumero(campo,importe,idioma) {
	/*if (importe.contains('.');
		return false;*/
	if (importe)
		cadena = "0123456789,";
		//cadena = "0123456789,."; @@cambiado para no permitir los ptos en el importe, los decimales se ponen con comas
	else
		cadena = "0123456789";
	for (var i=0; i < campo.value.length; i++) {
		if (cadena.indexOf(campo.value.charAt(i)) == -1 ) { 
			switch (idioma){
			case (idioma='ESP'):
				alert("El campo " + campo.id + " sólo admite números")
				break
			case (idioma='CAT'):
				alert("El camp " + campo.id + " només admet xifres")
				break				
			case (idioma='VAL'):
				alert("El camp " + campo.id + " només admet xifres")
				break				
			case (idioma='GAL'):
				alert("O campo " + campo.id + " só admite números")
				break				
			case (idioma='EUK'):
				alert(campo.id + " eremuak zenbakiak bakarrik onartzen ditu")
				break		
			case (idioma='ENG'):
				alert("The field " + campo.id + " only allows figures")
				break										
			}		
			campo.focus();			
			return false;
		} 
	}
	return true;
}

function validaEntero(campo,min,max,idioma) {

	if (valNumero(campo,false,idioma)) {
		
		valor = typeof(parseInt(campo.value))=="number"?parseInt(campo.value):0; 
		min = typeof(parseInt(min))=="number"?parseInt(min):0; 
		max = typeof(parseInt(max))=="number"?parseInt(max):0; 

		// validar que sea mayor que el mínimo
		if ((valor < min)) {
			switch (idioma){
			case (idioma='ESP'):
				alert("El " + campo.id + " debe ser mayor o igual de " + min)
				break
			case (idioma='CAT'):
				alert("El " + campo.id + " ha de ser més gran o igual a " + min)
				break				
			case (idioma='VAL'):
				alert("El " + campo.id + " ha de ser més gran o igual a " + min)
				break				
			case (idioma='GAL'):
				alert("O " + campo.id + " debe ser maior ou igual de " + min)
				break				
			case (idioma='EUK'):
				alert(campo.id + " " +  min + " hurrengoa baino handiagoa edo berdina izan behar du:")
				break		
			case (idioma='ENG'):
				alert("The " + campo.id + " must be greater than or equal to " + min)
				break										
			}									
			limpia(campo.id);
			return false;
		}

		// validar que sea menor que el máximo
		if ((valor > max)) {
			switch (idioma){
			case (idioma='ESP'):
				alert("El " + campo.id + " debe ser menor o igual de " + max)
				break
			case (idioma='CAT'):
				alert("El " + campo.id + " ha de ser més petit o igual a " + max)
				break				
			case (idioma='VAL'):
				alert("El " + campo.id + " ha de ser menor o igual a " + max)
				break				
			case (idioma='GAL'):
				alert("O " + campo.id + " debe ser menor ou igual de " + max)
				break				
			case (idioma='EUK'):
				alert(campo.id + " " + max + " hurrengoa baino txikiagoa edo berdina izan behar du:" )
				break		
			case (idioma='ENG'):
				alert("The " + campo.id + " must be smaller than or equal to " + max)
				break										
			}	
			limpia(campo.id);							

			return false;
		}
	}
	return true;
}

function validaNormalizado(idioma) {			
	// validar que sea mayor que el mínimo
	if ((document.tarificador.S1.value > 20 )) {			
		switch (idioma){
		case (idioma='ESP'):
			alert("El peso debe ser menor o igual a 20 gramos si el envío es normalizado")
			break
		case (idioma='CAT'):
			alert("El pes ha de ser menor o igual a 20 grams si l'enviament és normalitzat")
			break				
		case (idioma='VAL'):
			alert("El pes ha de ser menor o igual a 20 grams si l'enviament és normalitzat")
			break				
		case (idioma='GAL'):
			alert("O peso debe ser menor ou igual a 20 gramos se o envío é normalizado")
			break				
		case (idioma='EUK'):
			alert("Bidalketa normalizatua bada, pisuak 20 gramokoa edo txikiagoa izan behar du")
			break		
		case (idioma='ENG'):
			alert("When standardized items, the weight cannot exceed 20 grammes")
			break										
		}			
		document.tarificador.S1.focus();
		return false;
	} else
	return true;
}

function validaPrepagado(idioma) {			
	// validar que sea mayor que el mínimo
	if ((document.tarificador.S1.value > 500 )) {			
		switch (idioma){
		case (idioma='ESP'):
			alert("Para optar por la opción de sobre prepagado el peso debe ser menor o igual a 500 gramos")
			break
		case (idioma='CAT'):
			alert("Per a optar per l'opció de sobre prepagat el pes ha de ser menor o igual a 500 grams")
			break				
		case (idioma='VAL'):
			alert("Per a optar per l'opció de sobre prepagat el pes ha de ser menor o igual a 500 grams")
			break				
		case (idioma='GAL'):
			alert("Para optar pola opción de sobre prepagado o peso debe ser menor ou igual a 500 gramos")
			break				
		case (idioma='EUK'):
			alert("Gutunazal frankeatuaren aukera hautetsi baduzu pisuak 500 gramokoa edo txikiagoa izan behar du")
			break		
		case (idioma='ENG'):
			alert("To choose the prepayed envelope option, the weight cannot exceed 500 grammes")
			break										
		}			
		document.tarificador.S1.focus();
		return false;
	} else
		return true;
}

function activaSA(sa,valor) {		
	for (i=0;i<document.tarificador.length;i++) {
		var tempobj = document.tarificador.elements[i];		
		if (tempobj.name == sa) {			
			tempobj.checked = true;
			tempobj.value = valor;			
			break;
		}
	}
}

function validaDecimal(campo,min,max,idioma) {

	if (valNumero(campo,true,idioma)) {

		// remplazamos la coma española por el punto americano (necesario en javascript)
		// así nos da igual como venga y siempre realizaremos las validaciones bien.
		var valor = campo.value.replace(',','.');
		min = min.replace(',','.');
		max = max.replace(',','.');
		
		if ((document.tarificador.IDP.value=="021") && (document.tarificador.S200.options[document.tarificador.S200.selectedIndex].value=="PKN")){
		   max = 705;
		}
		
		// los pasamos a decimal
		valor = typeof(parseFloat(valor))=="number"?parseFloat(valor):0.0; 
		min = typeof(parseFloat(min))=="number"?parseFloat(min):0.0; 
		max = typeof(parseFloat(max))=="number"?parseFloat(max):0.0; 

		// validar que sea mayor que el mínimo
		if ((valor < min)) {
			switch (idioma){
			case (idioma='ESP'):
				alert("El Importe debe ser mayor o igual a cero")
				break
			case (idioma='CAT'):
				alert("L'import ha de ser major o igual a zero")
				break				
			case (idioma='VAL'):
				alert("L'Import ha de ser major o igual a zero")
				break				
			case (idioma='GAL'):
				alert("O Importe debe ser maior ou igual a cero")
				break				
			case (idioma='EUK'):
				alert("Zenbatekoak zero edo gehiago izan behar du")
				break		
			case (idioma='ENG'):
				alert("The total amount must be over zero or zero")			
				break										
			}			
			return false;
		}

		// validar que sea menor que el máximo
		if ((valor > max)) {
			switch (idioma){
			case (idioma='ESP'):
				alert("El " + campo.id + " debe ser menor o igual de " + max)
				break
			case (idioma='CAT'):
				alert("El " + campo.id + " ha de ser més petit o igual a " + max)
				break				
			case (idioma='VAL'):
				alert("El " + campo.id + " ha de ser menor o igual a " + max)
				break				
			case (idioma='GAL'):
				alert("O " + campo.id + " debe ser menor ou igual de " + max)
				break				
			case (idioma='EUK'):
				alert(campo.id + " " + max + " hurrengoa baino txikiagoa edo berdina izan behar du" )
				break		
			case (idioma='ENG'):
				alert("The " + campo.id + " must be smaller than or equal to " + max)
				break										
			}			
											
			return false;
		}
	}
	return true;
}

function VerificaPesoVolumetrico(idp,idioma) {
	if (idp=='901') {
		if (parseInt(document.tarificador.C20.value) > 80){
			switch (idioma){
			case (idioma='ESP'):
				alert("El largo puede ser como máximo de 80cms");
				break
			case (idioma='CAT'):
				alert("El largo puede ser como máximo de 80cms");
				break				
			case (idioma='VAL'):
				alert("El largo puede ser como máximo de 80cms");
				break				
			case (idioma='GAL'):
				alert("El largo puede ser como máximo de 80cms");
				break				
			case (idioma='EUK'):
				alert("El largo puede ser como máximo de 80cms");
				break		
			case (idioma='ENG'):
				alert("El largo puede ser como máximo de 80cms");
				break										
			}															
			return false;		
		}
		if (parseInt(document.tarificador.C21.value) > 50){
			switch (idioma){
			case (idioma='ESP'):
				alert("El ancho puede ser como máximo de 50cms");
				break
			case (idioma='CAT'):
				alert("El ancho puede ser como máximo de 50cms");
				break				
			case (idioma='VAL'):
				alert("El ancho puede ser como máximo de 50cms");
				break				
			case (idioma='GAL'):
				alert("El ancho puede ser como máximo de 50cms");
				break				
			case (idioma='EUK'):
				alert("El ancho puede ser como máximo de 50cms");
				break		
			case (idioma='ENG'):
				alert("El ancho puede ser como máximo de 50cms");
				break										
			}															
			return false;		
		}
		if (parseInt(document.tarificador.C22.value) > 50){
			switch (idioma){
			case (idioma='ESP'):
				alert("El alto puede ser como máximo de 50cms");
				break
			case (idioma='CAT'):
				alert("El alto puede ser como máximo de 50cms");
				break				
			case (idioma='VAL'):
				alert("El alto puede ser como máximo de 50cms");
				break				
			case (idioma='GAL'):
				alert("El alto puede ser como máximo de 50cms");
				break				
			case (idioma='EUK'):
				alert("El alto puede ser como máximo de 50cms");
				break		
			case (idioma='ENG'):
				alert("El alto puede ser como máximo de 50cms");
				break										
			}															
			return false;		
	  }
	}
	if (idp=='300' || idp=='303' || idp =='307') {
		Largo=parseInt(document.tarificador.C20.value);
		Ancho=parseInt(document.tarificador.C21.value);
		Alto=parseInt(document.tarificador.C22.value);
		total=Largo+Ancho+Alto;

		if ((Ancho > 100)&&(Alto > 100)&&(Largo>100)){
			alert("Los campos Largo, Ancho y Alto no pueden ser superiores a 100 cm.");
		} else if ((Ancho > 100)&&(Alto > 100)){
			alert("Los campos Ancho y Alto no pueden ser superiores a 100 cm.");
		} else if ((Ancho > 100)&&(Largo > 100)){
			alert("Los campos Largo y Ancho no pueden ser superiores a 100 cm."); /* La suma total de las dimensiones no puede ser superior a 200 cm.*/
		} else if ((Alto > 100)&&(Largo > 100)){
			alert("Los campos Largo y Alto no pueden ser superiores a 100 cm."); /* La suma total de las dimensiones no puede ser superior a 200 cm.*/
		} else if (Ancho > 100){
			alert("El campo Ancho no puede ser superior a 100 cm");
		} else if (Alto > 100){
			alert("El campo Alto no puede ser superior a 100 cm");
		} else if (Largo > 100){
			alert("El campo Largo no puede ser superior a 100 cm");
		}					 
		if (total <= 200) {
			if (Largo > 100 || Alto >100 || Ancho >100) {
				//alert("La suma de los campos Largo, Ancho y Alto no puede ser superior a 200 cm.");
				return false;
			}	
		}					 
		else if (total > 200){
			alert("La suma de los campos Largo, Ancho y Alto no puede ser superior a 200 cm");
			return false;
		}
	}
	if (idp=='305' || idp=='301' || idp=='302') {
		Largo=parseInt(document.tarificador.C20.value);
		Ancho=parseInt(document.tarificador.C21.value);
		Alto=parseInt(document.tarificador.C22.value);
		total=Largo+Ancho+Alto;
		
		if ((Ancho > 105)&&(Alto > 105)&&(Largo>105)){
		  alert("Los campos Largo, Ancho y Alto no pueden ser superiores a 105 cm.");
		} else if ((Ancho > 105)&&(Alto > 105)){
		  alert("Los campos Ancho y Alto no pueden ser superiores a 105 cm.");
		} else if ((Ancho > 105)&&(Largo > 105)){
		  alert("Los campos Largo y Ancho no pueden ser superiores a 105 cm."); /* La suma total de las dimensiones no puede ser superior a 200 cm.*/
		} else if ((Alto > 105)&&(Largo > 105)){
		  alert("Los campos Largo y Alto no pueden ser superiores a 105 cm."); /* La suma total de las dimensiones no puede ser superior a 200 cm.*/
		} else if (Ancho > 105){
		  alert("El campo Ancho no puede ser superior a 105 cm");
		} else if (Alto > 105){
		  alert("El campo Alto no puede ser superior a 105 cm");
		} else if (Largo > 105){
		  alert("El campo Largo no puede ser superior a 105 cm");
		}					 				 
		if (total <= 200) {
		  if (Largo > 105 || Alto >105 || Ancho >105) {
			//alert("La suma de los campos Largo, Ancho y Alto no puede ser superior a 200 cm.");
			return false;
		  }	
		 }					 
		 else if (total > 200){
		  alert("La suma de los campos Largo, Ancho y Alto no puede ser superior a 200 cm");
		  return false;
		}
	}	
	return true;
}

// esta función comprueba que todos los campos que cuyo nombre comience por S estén rellenos
function checkrequired(formulario,idioma) {	
	
	//FML: Comprobación peso mínimo - Añadido: 13:27 26/12/2007
	try {
		if (validaEntero(formulario.S1, 1, 99999999, idioma) == false ) {
			return false;
		}
	} catch (e) {}

	// comprobar para Cartas y Tarjetas Postales, Carta Certificada Urgente Nacional, Publibuzón Nacional, Paquete Postal, Paquete Azul, y Cartas Certificadas y Libros y periodicos nacional que no haya incongruencia de Zonas
	elproducto=formulario.IDP.value;
	if ((elproducto=='010') || (elproducto=='050') || (elproducto=='203') || (elproducto=='410') || (elproducto=='300') || (elproducto=='040') || (elproducto=='020') || (elproducto=='050') || (elproducto=='209') || (elproducto=='210') || (elproducto=='111')){
	   origen = formulario.S100.options[formulario.S100.selectedIndex].value
	   destino = formulario.S200.options[formulario.S200.selectedIndex].value;

	   if ((origen=='157' || origen=='158') && (destino=='175' || destino=='174')){
	      alert("Desde Ceuta o Melilla no se pueden realizar envíos locales a Canarias o Península y Baleares");
	      return false;
	   }
	   if ((origen=='157' || origen=='158') && (destino=='177' || destino=='176')){
	      alert("Con origen Ceuta o Melilla debe elegir CANARIAS como tipo de destino.\nPor favor, verifique");
	      return false;
	   }
	   if (origen=='156' && destino=='174'){
	      alert("Desde Canarias los envíos a Península y Baleres son interurbanos.\nPor favor, verifique");
	      return false;
	   }
	   if (origen=='156' && destino=='95'){
	      alert("Debe especificar tipo de destino: local, misma isla ó distinta isla.\nPor favor, verifique");
	      return false;
	   }
	   if (origen=='155' && (destino=='175' || destino=='176' || destino=='177')){
	      alert("Con origen Península y Baleares debe elegir CANARIAS como tipo de destino.\nPor favor, verifique");
	      return false;		  
	  }
	}	
	//comprobaciones de zonas incompatibles para publicorreo nacional y publicorreo tempo
	if ((elproducto=='200') || (elproducto=='204')){
	     org=document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text;
	     dest=document.tarificador.S18.options[document.tarificador.S18.selectedIndex].value;
	     zonaAoB=document.tarificador.S3.options[document.tarificador.S3.selectedIndex].value;
		 if (org=='PENÍNSULA Y BALEARES' && (dest=='176' || dest=='177')){
			alert("Con origen Península y Baleares no puede elegir ámbito CANARIAS MISMA ISLA o CANARIAS DISTINTA ISLA.\nPor favor, verifique");
			return false;
		 }
		 if (org=='CANARIAS' && dest=='156'){
			alert("Con origen CANARIAS no puede elegir ámbito CANARIAS.\nPor favor, verifique");
			return false;
		 }
		 if (org=='CEUTA' && dest=='157' && zonaAoB=='62'){
			alert("Con origen CEUTA no puede elegir zona B.\nPor favor, verifique");
			return false;
		 }
		 if ((org=='CEUTA' || org=='MELILLA') && (dest=='176' || dest=='177')){
			alert("Con origen CEUTA o MELILLA no puede elegir ámbito CANARIAS MISMA ISLA o CANARIAS DISTINTA ISLA.\nPor favor, verifique");
			return false;
		 }
		 if (org=='MELILLA' && dest=='158' && zonaAoB=='62'){
			alert("Con origen MELILLA no puede elegir zona B.\nPor favor, verifique");
			return false;
		 }		 		 		 
	}
	
	
	if (elproducto=='501'){
	  tmpNumPalab = formulario.S6.value;
	  if (tmpNumPalab<7){
	    alert("Le recordamos que la tarifa del telegrama se tarifa con un mínimo de percepción por importe de siete (7) palabras");
	  }
	}

	if (!VerificaPesoVolumetrico(elproducto,idioma)){
	  return false;
	}

	// activamos el iva.
	var origen = '';
	var destino = '';
	try { origen = formulario.S100.options[formulario.S100.selectedIndex].value; } catch (e) {}
	try { destino = formulario.S200.options[formulario.S200.selectedIndex].value;} catch (e) {}

	activaIVA(formulario.IDP.value,origen,destino);	
		
	var pass=true;
	if (document.images) {
		for (i=0;i<formulario.length;i++) {
			var tempobj=formulario.elements[i];
 
			// valida si no es normalizado que no pese más de 20 gramos
			if ((tempobj.name.substring(0,2)=="S5") && (tempobj.checked == true)){
				if (!validaNormalizado(idioma)) return false;
			}
 			 			
 			// valida si se ha elegido sobre prepagado que no pese más de 500 gramos. CUI
			if ((tempobj.name.substring(0,3)=="S17") && (tempobj.checked == true)){
				if (!validaPrepagado(idioma)) return false;
			}
 			
 			// valida si hay remesas que el numero del envío de la remesa sea igual a la cantidad
			if (tempobj.name.substring(0,3)=="S14"){								
				// Colocamos su valor en cantidad.
				document.tarificador2.cantidad.value = tempobj.value;
				//document.tarificador2.cantidad.enabled = false;
			}
 					
			if (tempobj.name.substring(0,1)=="S") {
				if (( (tempobj.type=="text"||tempobj.type=="textarea"||tempobj.type=="password" ) && tempobj.value=='')
						||(tempobj.type.toString().charAt(0)=="s" && tempobj.selectedIndex==0)
						||(tempobj.type=="radio" && tempobj.value==''))
				{
					tempobj.focus();
					pass=false;
					break;
				} else { // si se ha marcado un servicio adicional y debo comprobar si ha rellenado la casilla correspondiente		
						if (formulario.elements[i].name.substring(0,2)=='SA') {
							// si están seleccionados y no tienen valor.
							if ((formulario.elements[i].checked) && (formulario.elements[i].value=='')){
									tempobj.focus();
									pass=false;
									break;
							}
						}
				}	
		    
			}
		} // for

	} // if

	if (!pass) {
			
		shortFieldName=tempobj.id;
		switch (idioma){
			case (idioma='ESP'):
				alert("Por favor, el campo " + shortFieldName + " es obligatorio")
				break
			case (idioma='CAT'):
				alert("Si us plau, el camp " + shortFieldName + " és obligatori")
				break				
			case (idioma='VAL'):
				alert("Per favor, el camp " + shortFieldName + " és obligatori")
				break				
			case (idioma='GAL'):
				alert("Por favor, o campo " + shortFieldName + " é obrigatorio")
				break				
			case (idioma='EUK'):
				alert("Mesedez, " + shortFieldName + " eremua nahitaezkoa da")
				break		
			case (idioma='ENG'):
				alert("Please note that the field " + shortFieldName + " is compulsory")
				break										
			}																	
		return false;
	} else {

		// ahora comprobamos las restricciones	
			try { // las restricciones son siempre por un destino (pais) codificado como 200, no lo haremos para el mismo y lo haremos para todos los demás.					
				for (i=0;i<formulario.length;i++) {
					var tempobj=formulario.elements[i];
					if (tempobj.name.substring(0,1)=="S") {
						if ((tempobj.name!='S200') && (tempobj.name.substring(0,2)!='SA')) {													
							// podemos tener la restricción por pais (todo esto no debería a fuego)
							pass = restriccionesProductoC(tempobj.name,tempobj.value,document.tarificador.S200.value);
							// DANIEL.MOSSBERG
							if (pass == false) return pass;
						}	
					}				
				}
			} catch	(e)	{
				try {
				// en este caso, la restricción es por un atributo (todo esto no debería a fuego)
				pass = restriccionesProductoC(tempobj.name,tempobj.value,document.tarificador.S16.value);
				} catch	(e)	{ }
			}			
			return pass;	
	}
}

function valParteEntera(campo,e){
	if (campo.indexOf(',') == -1) { // no hay decimales, no debe ser mayor de e dígitos
		if (campo.length > e) return false;
	} else { // hay que validar los decimales
		if (campo.indexOf(",") > e) return false;
	}
	return true;
}

function valParteDecimal(valor,d){
	if (valor.indexOf(',') == -1) { 
		// no hay decimales ok
	} else { // hay que validar los decimales
		if (valor.length - valor.indexOf(",") -1 > d) return false;
	}			
	return true;
}

function valImporte(nombre,campo,idioma){
	
	if (campo == '') return true;	
	if (!valNumero(nombre,campo,true,idioma)) return false;

	var nValor=typeof(parseFloat(campo))=="number"?parseFloat(campo):0.0; 
	if (!(nValor >= 0)) {
		switch (idioma){
			case (idioma='ESP'):
				alert("El Importe debe ser mayor o igual a cero")
				break
			case (idioma='CAT'):
				alert("L'import ha de ser major o igual a zero")
				break				
			case (idioma='VAL'):
				alert("L'Import ha de ser major o igual a zero")
				break				
			case (idioma='GAL'):
				alert("O Importe debe ser maior ou igual a cero")
				break				
			case (idioma='EUK'):
				alert("Zenbatekoak zero edo gehiago izan behar du")
				break		
			case (idioma='ENG'):
				alert("The total amount must be over zero or zero")
				break										
			}																			
		return false;
	}

	// hay que validar que su longitud no sea mayor de 12,2
	if (!valParteEntera(campo,12)){
		switch (idioma) {
			case (idioma='ESP'):
				alert("La parte entera del campo " + nombre + " no puede ser mayor de 12 cifras")
				break
			case (idioma='CAT'):
				alert("La part sencera del camp " + nombre + " no pot ser major de 12 xifres")
				break				
			case (idioma='VAL'):
				alert("La part sencera del camp " + nombre + " no pot ser major de 12 xifres")
				break				
			case (idioma='GAL'):
				alert("A parte enteira do campo " + nombre + " non pode ser maior de 12 cifras")
				break				
			case (idioma='EUK'):
				alert( nombre + "eremuak guztira 12 zifra baino gutxiago eduki behar ditu")
				break		
			case (idioma='ENG'):
				alert("The whole component of the field " + nombre + " cannot exceed 12 figures")
				break										
			}																					
		return false;
	}
	if (!valParteDecimal(campo,2)) {	
		switch (idioma){
			case (idioma='ESP'):
				alert("La parte decimal del campo " + nombre + " no puede ser mayor de 2 cifras")
				break
			case (idioma='CAT'):
				alert("La part decimal del camp " + nombre + " no pot ser major de 2 xifres")
				break				
			case (idioma='VAL'):
				alert("La part decimal del camp " + nombre + " pot ser major de 2 xifres")
				break				
			case (idioma='GAL'):
				alert("A parte decimal do campo  " + nombre + " non pode ser maior de 2 cifras")
				break				
			case (idioma='EUK'):
				alert(nombre + " eremuaren zati hamartarrak ezin ditu bi zifra baino gehiago eduki")
				break		
			case (idioma='ENG'):
				alert("The decimal component of the field " + nombre + " cannot exceed 2 figures")
				break										
			}				
		return false;
	}

	return true;
}

// muestra u oculta una tabla
function ShowHide(id, visible) {         
    if (id!="") {		
		var visibility;    
				
		if (visible) 
			visibility = 'block';
		else
			visibility = 'none';
	
		obj = document.getElementsByTagName("div"); 
		obj['DIV' + id].style.display = visibility;
		
		// si ocultamos debemos de desmarcar el seleccionado.		
		for (i=0;i<document.tarificador.length;i++) {			
			var tempobj=document.tarificador.elements[i]; 	
			if (tempobj.name=="SA" + id) 
				tempobj.checked = false
		}		
	}
} 

function limpia(id) {           		
	// limpiamos.	
	for (i=0;i<tarificador.length;i++) {			
		var tempobj=tarificador.elements[i]; 	
		if (tempobj.id == id) 
			tempobj.value ='';
	}			
}

function cambiaOrigenAdmision() {
	//para Postal Expres Nacional
	try {
		//obtener el valor de la lista de Modalidad
		var vModalidad = document.tarificador.S91.value;
		if (vModalidad == '314') {
			//ocultar todos los servicios adicionales porque Logística Inversa no los permite
			ShowHide('737',false);
			ShowHide('739',false);
			ShowHide('760',false);
			ShowHide('792',false);
			ShowHide('793',false);
			ShowHide('794',false);
			ShowHide('799',false);
		} else if (vModalidad == '311' || vModalidad == '312' || vModalidad == '313') {
			//mostrar todos los servicios adicionales
			if (document.getElementById('DIV737').style.display != 'block') document.getElementById('DIV737').style.display = 'block';
			if (document.getElementById('DIV739').style.display != 'block') document.getElementById('DIV739').style.display = 'block';
			if (document.getElementById('DIV760').style.display != 'block') document.getElementById('DIV760').style.display = 'block';
			if (document.getElementById('DIV792').style.display != 'block') document.getElementById('DIV792').style.display = 'block';
			if (document.getElementById('DIV793').style.display != 'block') document.getElementById('DIV793').style.display = 'block';
			if (document.getElementById('DIV794').style.display != 'block') document.getElementById('DIV794').style.display = 'block';
			if (document.getElementById('DIV799').style.display != 'block') document.getElementById('DIV799').style.display = 'block';
		}
	} catch (e) {}

	//quitar Colombia de Giro Internacional Modalidad Libranza
	try {
		var campoDestino = document.tarificador.S200.value;
		var campoModalidad = document.tarificador.S16.value;
		if (campoDestino == 'COL' && campoModalidad == '213') {
			alert("El país seleccionado no admite la modalidad de giro seleccionada.");
			document.tarificador.S200.selectedIndex = 0;
		}
	} catch (e) {}
	
	
	try {
		origen = document.tarificador.S100.options[document.tarificador.S100.selectedIndex].value; 				
		switch (origen){			
			case (origen='156'): // canarias
				if (document.tarificador.IDC_700.value=='203')
					document.tarificador2.IVA_SA.selectedIndex=1;
				else
					document.tarificador2.IVA_SA.selectedIndex=0;
				break;			
			case (origen='738'): // canarias
				document.tarificador2.IVA_SA.selectedIndex=0;
				break;
			case (origen='735'): // canarias
				document.tarificador2.IVA_SA.selectedIndex=0;
				break;				
			case (origen='763'): // canarias
				document.tarificador2.IVA_SA.selectedIndex=0;
				break;	
			case (origen='764'): // canarias
				document.tarificador2.IVA_SA.selectedIndex=0;
				break;	
			case (origen='760'): // canarias
				document.tarificador2.IVA_SA.selectedIndex=0;
				break;	
			case (origen='761'): // canarias
				document.tarificador2.IVA_SA.selectedIndex=0;
				break;	
			case (origen='762'): // canarias
				document.tarificador2.IVA_SA.selectedIndex=0;
				break;	
			case (origen='751'): // ceuta
				document.tarificador2.IVA_SA.selectedIndex=1;
				break;
			case (origen='157'): // ceuta
				if (document.tarificador.IDC_700.value=='203')
					document.tarificador2.IVA_SA.selectedIndex=2;
				else			
					document.tarificador2.IVA_SA.selectedIndex=1;
				break;				
			case (origen='752'): // melilla
				document.tarificador2.IVA_SA.selectedIndex=2;
				break;
			case (origen='158'): // melilla
				if (document.tarificador.IDC_700.value=='203')
					document.tarificador2.IVA_SA.selectedIndex=3;
				else			
					document.tarificador2.IVA_SA.selectedIndex=2;
				break;
			default: // península y baleares
				if (document.tarificador.IDC_700.value=='203')
					document.tarificador2.IVA_SA.selectedIndex=4;
				else					
					document.tarificador2.IVA_SA.selectedIndex=3;
		}				
	} catch (e) {}			
}

function activaIVA(idp,origen,destino) {        
	// verdadero si el producto es nacional.
	var nacional = IsNumeric(destino);		

	// forzamos el orgien de admisión al correspondiente.
	cambiaOrigenAdmision(); //***ver si hay que modificar por inclusión de islas en provincias

	// por defecto lo ponemos al valor de origen.
	if (nacional)
		document.tarificador2.IVA.value = document.tarificador2.IVA_SA.value;
	else
		document.tarificador2.IVA.value = IVA_EXENTO;

	// Para ceuta y melilla	
	if ((origen=='157') || (origen=='158') || (origen=='751') || (origen=='752')) { // para ceuta y melilla
		document.tarificador2.IVA.value=0;
			
		if ((origen==destino) || (destino=='167')) {
			if ((origen='157') || (origen='751')) //ceuta						
				document.tarificador2.IVA.value=IVA_IPSI_CEUTA;
			else
				document.tarificador2.IVA.value=IVA_IPSI_MELILLA;						
		}						

		try {			
			if (document.tarificador.S3.value =='61') { // zona A publicorreo es local, solo existe este caso especial.
				if ((origen='157') || (origen='751')) //ceuta						
					document.tarificador2.IVA.value=IVA_IPSI_CEUTA;
				else
					document.tarificador2.IVA.value=IVA_IPSI_MELILLA;			
			}
		} catch (e) {}
			
	} 
	//Modificaciones para la valija
	if (destino=='751' || origen=='751' || destino=='752' || origen=='752' || origen=='738' || destino=='738' || origen=='156' || destino=='156' || origen=='735' || destino=='735' || origen=='157' || destino=='157' || origen=='158' || destino=='158' || origen=='763' || destino=='763' || origen=='764' || destino=='764' || origen=='760' || destino=='760' || origen=='761' || destino=='761' || origen=='762' || destino=='762'){
		document.tarificador2.IVA.value=IVA_EXENTO;
	}

	// si es europeo el iva correspondiente.	
	for (i=0; i < ue_con_iva.length; i++) {						
		if (ue_con_iva[i]==destino)
			document.tarificador2.IVA.value = document.tarificador2.IVA_SA.value;			
	} 

	//Modificaciones para Paquete Prisma Internacional
	if ((idp=='902') && (origen=='156' || origen=='157' || origen=='158')) {
		document.tarificador2.IVA.value=IVA_EXENTO;
	}

	//Modificaciones para el Publicorreo Internacional
	if ((idp=='201') && (origen=='156' || origen=='157' || origen=='158')) {
		document.tarificador2.IVA.value=IVA_EXENTO;
	}

	//Modificaciones para el Paquete Prepagado Internacional
	if ((idp=='306') && (origen=='156' || origen=='157' || origen=='158')) {
		document.tarificador2.IVA.value=IVA_EXENTO;
	}

	//Modificaciones para el Postal Exprés Internacional
	if ((idp=='305') && (origen=='156' || origen=='157' || origen=='158')) {
		document.tarificador2.IVA.value=IVA_EXENTO;
	}

	//Modificaciones para el Paquete Internacional Económico
	if ((idp=='301') && (origen=='156' || origen=='157' || origen=='158')) {
		document.tarificador2.IVA.value=IVA_EXENTO;
	}

	//Modificaciones para el Paquete Internacional Prioritario
	if ((idp=='302') && (origen=='156' || origen=='157' || origen=='158')) {
		document.tarificador2.IVA.value=IVA_EXENTO;
	}

	// para carta nacional ordinaria, libros y periódicos nacional, publibuzón nacional, paquete postal, paquete azul, urgente y certificada, certificada urgente y  notificacion .
	if ( (idp=='010') || (idp=='209') || (idp=='210') || (idp=='203') || (idp=='410') || (idp=='300') || (idp=='040') || (idp=='020') || (idp=='050') || (idp=='111') ) { // les cobramos iva, sin pesan más de 50.			
	   switch (origen){
		  case (origen='155'): // península y baleares
			 document.tarificador2.IVA.value=IVA_PENINSULA_BALEARES;
			 break;
		  case (origen='156'): // canarias
			 document.tarificador2.IVA.value=IVA_EXENTO;
			 break;
		  case (origen='157'): // ceuta
			 if (destino=='167') // local con iva
				document.tarificador2.IVA.value=IVA_IPSI_CEUTA;				
			 else
				document.tarificador2.IVA.value=IVA_EXENTO;
			 break;
		  case (origen='158'): // melilla
			 if (destino=='167') //local con iva
				document.tarificador2.IVA.value=IVA_IPSI_MELILLA;
			 else
				document.tarificador2.IVA.value=IVA_EXENTO;
	   }
	   switch (destino){
		  case (destino='753'): // Andorra
				 document.tarificador2.IVA.value=IVA_EXENTO;
			 break;
		  case (destino='95'): // canarias (interurbano)
			 document.tarificador2.IVA.value=IVA_EXENTO;
			 break;
		  case (destino='175'): // canarias local
			 document.tarificador2.IVA.value=IVA_IGIC2;
			 document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].value=IVA_IGIC5;
			 break;
		  case (destino='176'): // canarias misma isla
			 document.tarificador2.IVA.value=IVA_IGIC2;
			 document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].value=IVA_IGIC5;
			 break;
		  case (destino='177'): // canarias distinta isla
			 document.tarificador2.IVA.value=IVA_EXENTO;
			 document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].value=IVA_EXENTO;
			 break;
		  case (destino='172'): // ceuta
			 if (origen=='157') // local con IPSI
				document.tarificador2.IVA.value=IVA_IPSI_CEUTA;				
			 else
				document.tarificador2.IVA.value=0;
			 break;
		  case (destino='173'): // melilla
			 if (origen=='158') //local con IPSI
				document.tarificador2.IVA.value=IVA_IPSI_MELILLA;
			 else
				document.tarificador2.IVA.value=0;
	   }
	}
			
	// cualquier tramo menor de 50 no lleva iva para estos productos de carta.
	if ( (idp=='010') || (idp=='020') || (idp=='011') || (idp=='021') || (idp=='111')) { 								
	   if (document.tarificador.S1.value <= 50)  // cualquier tramo menor de 50 no lleva iva.
		  if (destino==174 || destino==175){
			 document.tarificador2.IVA.value=-1;							
		  } else {
				 document.tarificador2.IVA.value=0;	
		  } 		
	}
							
	// PRISMA NACIONAL, VALIJA NACIONAL Y AL POSTAL EXPRÉS NACIONAL con destino a Canarias, Ceuta y Melilla.
	//AÑADIDO 'Reembolso Venta a Distancia'
	if  ((idp=='901') || (idp=='110') || (idp=='303') ||(idp =='307') || (idp=='308') ){	
	   if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CANARIAS"){
		  if ((destino=='751') || (destino=='752')){
			 document.tarificador2.IVA.value=IVA_EXENTO;
		  }
		  if (((origen=='763') || (origen=='735') || (origen=='764') || (origen=='762') || (origen=='761') || (origen=='760') || (origen=='738')) && (origen==destino)){
			 document.tarificador2.IVA.value=IVA_IGIC2;
		  } else {
			 document.tarificador2.IVA.value=IVA_EXENTO;
		  }
	   }
	   if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CEUTA"){
		  if (origen=='157' && destino=='751'){
			 document.tarificador2.IVA.value=IVA_IPSI_CEUTA;
		  }
	   }
		if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="MELILLA"){
		  if (origen=='157' && destino=='752'){
			 document.tarificador2.IVA.value=IVA_IPSI_MELILLA;
		  }
	   }
	}

	// PRISMA NACIONAL DE CEUTA A CEUTA
	if (idp=='901' && origen=='157' && destino=='751'){ 
		  document.tarificador2.IVA.value = IVA_IPSI_CEUTA;
	} 
	// PRISMA NACIONAL DE MELILLA A MELILLA
	if (idp=='901' && origen=='157' && destino=='752'){ 
		  document.tarificador2.IVA.value = IVA_IPSI_MELILLA;
	} 

		// IGIC DE SERVICIOS ADICIONALES
		if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CANARIAS"){
		  document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].value=IVA_IGIC5;
		}
		
		// PREFRANQUEADOS, GIRO ONLINE, BUROFAX NACIONAL, BUROFAX INTERNACIONAL, TELEGRAMA NACIONAL, TELEX NACIONAL, TELEX CON ESTACIONES MOVILES, FAX NACIONAL, NOTIFICACION TELEMATICA SEGURA y PACK POSTAL
		if (idp=='102' || idp=='921' || idp=='503' || idp=='504' || idp=='500' || idp=='507' || idp=='509' || idp=='511' || idp=='603' || idp=='804'){
		   if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CANARIAS"){
			 document.tarificador2.IVA.value=IVA_EXENTO;
		   }        
		}
		
		//Modificación para el Burofax Internacional y Fax Internacional
		if (idp=='504' || idp=='512'){
			 if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CANARIAS"){
			  document.tarificador2.IVA.value=IVA_EXENTO;
		   } else if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CEUTA"){
			  document.tarificador2.IVA.value=IVA_IPSI_CEUTA;
		   } else if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="MELILLA"){
			  document.tarificador2.IVA.value=IVA_IPSI_MELILLA;
		   } else {  //Se trata de península y baleares
			  document.tarificador2.IVA.value=IVA_PENINSULA_BALEARES;
		   }	
		}
		
		// TELEX INTERNACIONAL y TELEGRAMA URGENTE INTERNACIONAL
		if (idp=='508' || idp=='502'){
		   if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CANARIAS"){
			  document.tarificador2.IVA.value=IVA_EXENTO;
		   } else if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CEUTA"){
			  document.tarificador2.IVA.value=IVA_IPSI_CEUTA;
		   } else if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="MELILLA"){
			  document.tarificador2.IVA.value=IVA_IPSI_MELILLA;
		   } else {  //Se trata de península y baleares
			  document.tarificador2.IVA.value=IVA_PENINSULA_BALEARES;
		   }
		}

	// GIROS, que al ser aún un servicio reservado NO lleva IVA.
	if ((idp=='400') || (idp=='921')  || (idp=='401') || (idp=='402') || (idp=='403') || (idp=='404') || (idp=='407'))
		document.tarificador2.IVA.value=0;

	//LIBROS Y PERIODICOS INTERNACIONAL 
	// (no llevan impuestos si los envios no se realizan desde la península) 
	if ((idp=='213') && (origen=='156' || origen=='157' || origen=='158')){
		  document.tarificador2.IVA.value=0;
	}

		//Apartado de Correos, BD CodPostales, Cambio Dirección Nacional
		if (idp=='206' || idp=='207' || idp=='905'){ 
		  if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CANARIAS"){
			//alert("RAFA");
			document.tarificador2.IVA.value=IVA_IGIC5;
			document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].value=IVA_IGIC5;  
			//alert("RR " + document.tarificador2.IVA_SA.value);
		  }
		}
		
	// tampoco el certificado administrativo
	if (idp=='022') 		
		document.tarificador2.IVA.value=IVA_EXENTO;
		
	// IGIC Paquete Internacional Económico ni telegrama online ni radiotelegrama nacional
	if (idp=='301' || idp=='602' || idp=='505'){
	   if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CANARIAS"){
		  document.tarificador2.IVA.value=IVA_EXENTO;	
	   }
	}

	// origen canarias exento de iva, todos menos fotocopias y embalajes
	if (((origen=='156') || (origen=='735') || (origen=='738')) && ((idp!='802') || (idp!='803'))) // canarias		
		//document.tarificador2.IVA.value=IVA_EXENTO;		


	// ** IMPUESTOS DE LOS SERVICIOS ADICIONALES DE CANARIAS **
	if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CANARIAS"){
	  document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].value=IVA_IGIC5;
	}

		//TELEGRAMA INTERNACIONAL, TELEGRAMA URGENTE INTERNACIONAL, RADIOTELEGRAMA INTERNACIONAL
		if ((idp=='501' || idp=='502' || idp=='506')){
		   if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CANARIAS"){
			  document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].value=IVA_EXENTO; 
		   }
		}
		//PUBLICORREO NACIONAL Y PUBLICORREO TEMPO
		if (idp=='200' || idp=='204'){
		   tmpZona=document.tarificador.S3.options[document.tarificador.S3.selectedIndex].value; 
		   tmpAmbito= document.tarificador.S18.options[document.tarificador.S18.selectedIndex].value;
		   //CONTROLAMOS EL IMPUESTO QUE APLICAMOS DEPENDIENDO DEL ORIGEN LA ZONA Y EL AMBITO
		   if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="PENÍNSULA Y BALEARES" && tmpAmbito=="781"){
			  document.tarificador2.IVA.value=IVA_PENINSULA_BALEARES;
		   } 
		   else if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CANARIAS" && tmpAmbito=="176"){
			  document.tarificador2.IVA.value=IVA_IGIC2;
		   }
		   else if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="CEUTA" && tmpZona=="61" && tmpAmbito=="157"){
			  document.tarificador2.IVA.value=IVA_IPSI_CEUTA;
		   }
		   else if (document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].text=="MELILLA" && tmpZona=="61" && tmpAmbito=="158"){
			  document.tarificador2.IVA.value=IVA_IPSI_MELILLA;
		   } else {
			  document.tarificador2.IVA.value=IVA_EXENTO;
		   }           
		}
		
		// QUITAR IMPUESTOS DE EMBALAJES, PREFRANQUEADOS, SNTS
		if ((idp=='800') || (idp=='801') || (idp=='802') || (idp=='803') || (idp=='102') || (idp=='603')){
			document.tarificador2.IVA.value=IVA_EXENTO;
		}
		
		//POSTALES DE CORREO DIGITAL
		if ((idp=='601') || (idp=='607')){
			document.tarificador2.IVA.value=IVA_EXENTO;
		}        
		
		// CORRECCION IMPUESTOS CARTA Y TARJETA POSTAL INTERNACIONAL
		if (((idp=='011') || (idp=='021') || (idp=='041') || (idp=='051') || (idp=='071')) && ((origen=='156') || (origen=='157') || (origen=='158'))){
		   document.tarificador2.IVA.value=IVA_EXENTO;
		}   
		
	//alert("IVA Prod: " + document.tarificador2.IVA.value);
	//alert("IVA SA: " + document.tarificador2.IVA_SA.options[document.tarificador2.IVA_SA.selectedIndex].value);
}  



