<!---------- JavaScript
var caracteres = "abcdefghijklmnopqrstuvwxyzáéíóúñüABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚÑ0123456789-çÇ .,ºª/()";
var caracteres1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.@";
var caracteres2 = "0123456789";
var cadenacampos='';
var onoff = false;
var totpeso;
var FormaDePago;
// ***********************************************************
// Función lectura y escritura de valores totales de las cokies
// ***********************************************************
function alterError(value) {
	if (value<=0.99) {
	newPounds = '0';
	} else {
	newPounds = parseInt(value);
	}
newPence = parseInt((value+.0008 - newPounds)* 100);
	if (eval(newPence) <= 9) newPence='0'+newPence;
	newString = newPounds + '.' + newPence;
	return (newString);
	}

function showItems() {
index = document.cookie.indexOf("TheBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
	if (countend == -1) {
        countend = document.cookie.length;
        }
fulllist = document.cookie.substring(countbegin, countend);
totprice = 0;
itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
		itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
		itemend = i;
		thequantity = fulllist.substring(itemstart, itemend);
		itemtotal = 0;
		itemtotal = (eval(theprice*thequantity));
		temptotal = itemtotal * 100;
		var tax = itemtotal / 100 * (0 - 0);
    		tax = Math.floor(tax * 100)/100
		totprice = totprice + itemtotal + tax;
		itemlist=itemlist+1;
		document.writeln('<INPUT TYPE="hidden" NAME="Producto'+itemlist+'" VALUE="'+theitem+'" SIZE="40">');
		document.writeln('<INPUT TYPE="hidden" NAME="Cantidad'+itemlist+'" VALUE="'+thequantity+'" SIZE="5">');
		document.writeln('<INPUT TYPE="hidden" NAME="Importe'+itemlist+'" VALUE="'+alterError(itemtotal)+'" SIZE="10">');
		} else if (fulllist.substring(i,i+1) == ',') {
		theitem = fulllist.substring(itemstart, i);
		itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '#') {
		theprice = fulllist.substring(itemstart, i);
		itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ':') {
		thepeso = fulllist.substring(itemstart, i);
		itemstart = i+1;
		}
	}

document.writeln('<INPUT TYPE="hidden" NAME="PesoTot" VALUE="'+totpeso+'" SIZE="3">');
document.writeln('<INPUT TYPE="hidden" NAME="NumItems" VALUE="'+Math.round(alterError(itemlist))+'" SIZE="2">');
}

// ***********************************************************
// Función lectura valores de cokies
// ***********************************************************
function lectura() {
index = document.cookie.indexOf("TheBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
        if (countend == -1) {
        countend = document.cookie.length;
        }
fulllist = document.cookie.substring(countbegin, countend);
totprice = 0;
itemlist = 0;
totpeso = 0;
v_itemprod = 0;		 // para conocer el numero de productos
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;
			var tax = itemtotal / 100 * (0 - 0);
    			tax = Math.floor(tax * 100)/100
			totprice = totprice + itemtotal + tax;
			itemlist=itemlist+1;
			totpeso = totpeso + (eval(thequantity * thepeso / 1000));
			v_itemprod = v_itemprod + eval(thequantity*1);	 // para conocer el numero de productos
		} else if (fulllist.substring(i,i+1) == ',') {
			theitem = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '#') {
			theprice = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ':') {
			thepeso = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
	}
return(totprice);
}


// ***********************************************************************************************
// Función lectura_2 valores de cokies PARA DETERMINAR IMPORTE TOTAL SIN TENER EN CUENTA LOS PACKS
// ***********************************************************************************************
function lectura_dto() {
index_dto = document.cookie.indexOf("TheBasket");
countbegin_dto = (document.cookie.indexOf("=", index_dto) + 1);
countend_dto = document.cookie.indexOf(";", index_dto);
        if (countend_dto == -1) {
        countend_dto = document.cookie.length;
        }
fulllist_dto = document.cookie.substring(countbegin_dto, countend_dto);
totprice_dto = 0;
itemlist_dto = 0;
totpeso_dto = 0;
v_itemprod_dto = 0;		 // para conocer el numero de productos

	for (var i = 0; i <= fulllist_dto.length; i++) {
		if (fulllist_dto.substring(i,i+1) == '[') {
			itemstart_dto = i+1;
		} else if (fulllist_dto.substring(i,i+1) == ']') {
			itemend_dto = i;
			thequantity_dto = fulllist.substring(itemstart_dto, itemend_dto);
			itemtotal_dto = 0;
			itemtotal_dto = (eval(theprice_dto*thequantity_dto));
			temptotal_dto = itemtotal_dto * 100;
			var tax_dto = itemtotal_dto / 100 * (0 - 0);
    			tax_dto = Math.floor(tax_dto * 100)/100
			totprice_dto = totprice_dto + itemtotal_dto + tax_dto;
			itemlist_dto = itemlist_dto + 1;
		} else if (fulllist.substring(i,i+1) == ',') {
			theitem_dto = fulllist.substring(itemstart_dto, i);
			
			var result_dto = new Array();
			result_dto = theitem_dto.match("PACK"); // Busca la palabra PACK dentro de la descripcion y si aparece no contabilizara
				if(result_dto != null){
				suma_dto = 0;
				}else{
				suma_dto = 1;
				}
			
			itemstart_dto = i+1;
		} else if (fulllist_dto.substring(i,i+1) == '#') {
			theprice_dto = 0;
				if (suma_dto > 0){	// no contabliza los PACKS a nivel de aplicar decuento al importe de compra
				theprice_dto = fulllist_dto.substring(itemstart_dto, i);
				}
			itemstart_dto = i+1;
		} else if (fulllist_dto.substring(i,i+1) == ':') {
			itemstart_dto = i+1;
		}
	}
	
return(totprice_dto);
}


// ***********************************************************
// Función para convertir el precio a cadena con punto de miles, coma decimales y añadir "€"
// ***********************************************************
function cadena(num)
{
var c, e, d, lon, lone, cad;
	if (num == 0){
	cad = "0 €";
	return (cad)}
c = Math.round(num * 100);
c = c+" ";
lon = c.length;
e = c.substr(0, lon - 3);
d = c.substr(lon - 3, 2 );
lone = e.length;
	if (lone < 4) {
  	cad = e+","+d+" €";
	}
	if (lone > 3 && lone < 5) {
  	cad = e.substr(0,1)+"."+e.substr(1,3)+","+d+" €";
  	}
	if (lone > 4 && lone < 6) {
  	cad = e.substr(0,2)+"."+e.substr(2,3)+","+d+" €";
	}
	if (lone > 5) {
  	cad = e.substr(0,3)+"."+e.substr(3,3)+","+d+" €";
	}
return (cad)
}

// ***********************************************************
// Función de cálculo del formulario
// ***********************************************************
function calculo(form, formapago){
v_dto=0.00;
v_total=0.00;
v_cantid=0;
v_portes=0.00;
v_reemb=0.00;
v_impdto=0.00;
v_pesaprox=0.00;
v_pocendto="";

		if (isEmpty(formapago)){
			for (i=0; i < document.forms[0].Forma_de_pago.length; i++){
				if (document.forms[0].Forma_de_pago[0].checked == true) {
				FormaDePago = "fp1";
				}else{
				FormaDePago = "fp2";
				}
			}
		}else{
		FormaDePago = formapago;
		}

	with (document.forms[0]){
		
		if (FormaDePago == "fp1" && Pais.value != "España"){	//Envio contrareembolso
		alert (ErrFPag.value);Suma.focus();
		Forma_de_pago[1].checked=true;
		FormaDePago = "fp2";
		}
	
	v_total = lectura();
	v_total_dto = lectura_dto();		// añadido por PACKS para determinar la cantidad a la que aplicar descuentos
	Suma.value = cadena(v_total);
	v_pesaprox = totpeso;
	v_plusgtosenvio = 0;
	
	if (v_itemprod > 3){
		if (v_total>=40 && v_total<70){ // calcula el plus por cantidad de productos
		v_plusgtosenvio = 0.5;
		}
		if (v_total>=70 && v_total<150){
		v_plusgtosenvio = 1;
		}
		if (v_total>=150 && v_total<250){
		v_plusgtosenvio = 1.5;
		}
		if (v_total>=250){
		v_plusgtosenvio = Math.round(v_itemprod/10)
		}
	}

		if (v_total_dto < 42){						//Descuentos 6.655 pts.
		v_dto = 0*v_total_dto;
		v_pocendto=" ";
		}
		if (v_total_dto >= 42 && v_total_dto < 70){     //6650 a 11650 pts.
		v_d = Math.round(v_total_dto*5);
		v_dto = v_d/100;		
		v_pocendto="5%";
		}
		if (v_total_dto >= 70 && v_total_dto < 150){    //11650 a 25000 pts.
		v_d = Math.round(v_total_dto*10);
		v_dto = v_d/100;	
		v_pocendto="10%";
		}
		if (v_total_dto >= 150 && v_total_dto < 250){    //25000 a 41600 pts.
		v_d = Math.round(v_total_dto*15);
		v_dto = v_d/100;	
		v_pocendto="15%";
		}
		if (v_total_dto >= 250){ 					//41600 pts.
		v_d = Math.round(v_total_dto*20);
		v_dto = v_d/100;	
		v_pocendto="20%";
		}

	PorcDto.value = v_pocendto

		if (v_dto==0){
		Descuento.value = cadena(v_dto);
		}else{
		Descuento.value = "-"+cadena(v_dto);
		}

		if (Pais.value == "España"){	// España
			if (FormaDePago == "fp1"){  //Envio contrareembolso
				if (v_pesaprox<0.01){
				v_portes=0;
				v_reemb=0;
				}
				if (v_pesaprox>=0.01 && v_pesaprox<1){
				v_portes=6.45;
				v_r = Math.round((v_total-v_dto)*5);
				v_reemb=v_r/100;
					if (v_reemb < 3.00){
					v_reemb = 3.00;}
				}
				if (v_pesaprox>=1 && v_pesaprox<2){
				v_portes=6.95;
				v_r = Math.round((v_total-v_dto)*5);
				v_reemb=v_r/100;
					if (v_reemb < 3.00){
					v_reemb = 3.00;}
				}
				if (v_pesaprox>=2 && v_pesaprox<3){
				v_portes=7.95;
				v_r = Math.round((v_total-v_dto)*5);
				v_reemb=v_r/100;
					if (v_reemb < 3.00){
					v_reemb = 3.00;}
				}
				if (v_pesaprox>=3 && v_pesaprox<5){
				v_portes=9.50;
				v_r = Math.round((v_total-v_dto)*5);
				v_reemb=v_r/100;
					if (v_reemb < 3.00){
					v_reemb = 3.00;}
				}
				if (v_pesaprox>=5 && v_pesaprox<7.5){
				v_portes=14.00;
				v_r = Math.round((v_total-v_dto)*5);
				v_reemb=v_r/100;
					if (v_reemb < 3.00){
					v_reemb = 3.00;}
				}
				if (v_pesaprox>=7.5 && v_pesaprox<10){
				v_portes=17.00;
				v_r = Math.round((v_total-v_dto)*5);
				v_reemb=v_r/100;
					if (v_reemb < 3.00){
					v_reemb = 3.00;}
				}
				if (v_pesaprox>=10 && v_pesaprox<15){
				v_portes=25.00;
				v_r = Math.round((v_total-v_dto)*5);
				v_reemb=v_r/100;
					if (v_reemb < 3.00){
					v_reemb = 3.00;}
				}
				if (v_pesaprox>=15 && v_pesaprox<20){
				v_portes=30.00;
				v_r = Math.round((v_total-v_dto)*5);
				v_reemb=v_r/100;
					if (v_reemb < 3.00){
					v_reemb = 3.00;}
				}
				if (v_pesaprox>=20){
				v_portes=40.00;
				v_r = Math.round((v_total-v_dto)*5);
				v_reemb=v_r/100;
					if (v_reemb < 3.00){
					v_reemb = 3.00;}
				}
			}else{
				if (v_pesaprox<0.01){
				v_portes=0;
				v_reemb=0;
				}
				if (v_pesaprox>=0.01 && v_pesaprox<1){
				v_portes=6.45;
				v_reemb=0;
				}
				if (v_pesaprox>=1 && v_pesaprox<2){
				v_portes=6.95;
				v_reemb=0;
				}
				if (v_pesaprox>=2 && v_pesaprox<3){
				v_portes=7.95;
				v_reemb=0;
				}
				if (v_pesaprox>=3 && v_pesaprox<5){
				v_portes=9.50;
				v_reemb=0;
				}
				if (v_pesaprox>=5 && v_pesaprox<7.5){
				v_portes=14.00;
				v_reemb=0;
				}
				if (v_pesaprox>=7.5 && v_pesaprox<10){
				v_portes=17.00;
				v_reemb=0;
				}
				if (v_pesaprox>=10 && v_pesaprox<15){
				v_portes=25.00;
				v_reemb=0;
				}
				if (v_pesaprox>=15 && v_pesaprox<20){
				v_portes=30.00;
				v_reemb=0;
				}
				if (v_pesaprox>=20){
				v_portes=40.00;
				v_reemb=0;
				}
			}
			v_plusgtosenvio = v_plusgtosenvio*1;
// Zona A y B

			}else if (Pais.value == "Belgium" || Pais.value == "France" || Pais.value == "Germany" || Pais.value == "Italia" || Pais.value == "Monaco" || Pais.value == "Portugal" || Pais.value == "United Kingdom" || Pais.value == "Luxembourg" || Pais.value == "Netherlands" || Pais.value == "Austria" || Pais.value == "Denmark" || Pais.value == "Finland" || Pais.value == "Greece" || Pais.value == "Ireland" || Pais.value == "Liechtenstein" || Pais.value == "Sweden" || Pais.value == "Switzerland" || Pais.value == "Liechtenstein"){   
				if (v_pesaprox<0.01){
				v_portes=0;
				v_reemb=0;
				}
				if (v_pesaprox>=0.01 && v_pesaprox<1){
				v_portes=18.50;
				v_reemb=0;
				}
				if (v_pesaprox>=1 && v_pesaprox<2){
				v_portes=24.00;
				v_reemb=0;
				}
				if (v_pesaprox>=2 && v_pesaprox<3){
				v_portes=30.00;
				v_reemb=0;
				}
				if (v_pesaprox>=3 && v_pesaprox<4){
				v_portes=35.00;
				v_reemb=0;
				}
				if (v_pesaprox>=4 && v_pesaprox<5){
				v_portes=39.00;
				v_reemb=0;
				}
				if (v_pesaprox>=5 && v_pesaprox<7){
				v_portes=44.00;
				v_reemb=0;
				}
				if (v_pesaprox>=7 && v_pesaprox<10){
				v_portes=55.00;
				v_reemb=0;
				}
				if (v_pesaprox>=10 && v_pesaprox<16){
				v_portes=80.00;
				v_reemb=0;
				}
				if (v_pesaprox>=16){
				v_portes=120.00;
				v_reemb=0;
				}
			v_plusgtosenvio = v_plusgtosenvio*2;
//Zona E
			}else if (Pais.value == "Canada" || Pais.value == "USA" || Pais.value == "Mexico"){   
				if (v_pesaprox<0.01){
				v_portes=0;
				v_reemb=0;
				}
				if (v_pesaprox>=0.01 && v_pesaprox<1){
				v_portes=20.00;
				v_reemb=0;
				}
				if (v_pesaprox>=1 && v_pesaprox<2){
				v_portes=28.00;
				v_reemb=0;
				}
				if (v_pesaprox>=2 && v_pesaprox<3){
				v_portes=36.00;
				v_reemb=0;
				}
				if (v_pesaprox>=3 && v_pesaprox<4){
				v_portes=44.00;
				v_reemb=0;
				}
				if (v_pesaprox>=4 && v_pesaprox<5){
				v_portes=52.00;
				v_reemb=0;
				}
				if (v_pesaprox>=5 && v_pesaprox<7){
				v_portes=68.00;
				v_reemb=0;
				}
				if (v_pesaprox>=7 && v_pesaprox<10){
				v_portes=95.00;
				v_reemb=0;
				}
				if (v_pesaprox>=10 && v_pesaprox<16){
				v_portes=120.00;
				v_reemb=0;
				}
				if (v_pesaprox>=16){
				v_portes=180.00;
				v_reemb=0;
				}
			v_plusgtosenvio = v_plusgtosenvio*3;
//Zona C y D
			}else{   
				if (v_pesaprox<0.01){
				v_portes=0;
				v_reemb=0;
				}
				if (v_pesaprox>=0.01 && v_pesaprox<1){
				v_portes=20.00;
				v_reemb=0;
				}
				if (v_pesaprox>=1 && v_pesaprox<2){
				v_portes=30.00;
				v_reemb=0;
				}
				if (v_pesaprox>=2 && v_pesaprox<3){
				v_portes=39.00;
				v_reemb=0;
				}
				if (v_pesaprox>=3 && v_pesaprox<4){
				v_portes=48.00;
				v_reemb=0;
				}
				if (v_pesaprox>=4 && v_pesaprox<5){
				v_portes=56.00;
				v_reemb=0;
				}
				if (v_pesaprox>=5 && v_pesaprox<7){
				v_portes=75.00;
				v_reemb=0;
				}
				if (v_pesaprox>=7 && v_pesaprox<10){
				v_portes=110.00;
				v_reemb=0;
				}
				if (v_pesaprox>=10 && v_pesaprox<16){
				v_portes=150.00;
				v_reemb=0;
				}
				if (v_pesaprox>=16){
				v_portes=200.00;
				v_reemb=0;
				}
			v_plusgtosenvio = v_plusgtosenvio*3;
			}

	v_portes = v_portes + v_plusgtosenvio; // añade el plus a los gastos de transporte
	Portes.value = cadena(v_portes)
	Reembolso.value = cadena(v_reemb)
	Importe_total.value = cadena(v_total - v_dto + v_reemb + v_portes);
	}

}

// ***********************************************************
// Función de validación del formulario
// ***********************************************************
function validaDatos(form){
index = document.cookie.indexOf("TheBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
	if (countend == -1) {
        countend = document.cookie.length;
        }
fulllist = document.cookie.substring(countbegin, countend);
itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
		itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
		itemend = i;
		itemlist=itemlist+1;
		v_Producto = "Producto"+itemlist;
		v_Cantidad = "Cantidad"+itemlist;
		v_Importe = "Importe"+itemlist;
		} else if (fulllist.substring(i,i+1) == ',') {
		itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '#') {
		itemstart = i+1;
		}
	}
	with(document.forms[0]){
//	with (foroc){

		if (v_total < ImportMin.value){
			alert(ImportMin.value);Suma.focus();
		}else if (Nombre.value ==' ' || isEmpty(Nombre.value)){
			alert(SinNombre.value);Nombre.focus();
		}else if (Nombre.value.length < 3 || Nombre.value.length > 18){
			alert(ErrNombre.value);Nombre.focus();
		}else if (Apellidos.value ==' ' || isEmpty(Apellidos.value)){
			alert(SinApell.value);Apellidos.focus();
		}else if (Apellidos.value.length < 3 || Apellidos.value.length > 30){
			alert(ErrApell.value);Apellidos.focus();
		}else if (Direccion.value ==' ' || isEmpty(Direccion.value)){
			alert(SinDomic.value);Direccion.focus();
		}else if (Direccion.value.length < 5 || Direccion.value.length > 50){
			alert(ErrDomic.value);Direccion.focus();
		}else if (Ciudad.value ==' ' || isEmpty(Ciudad.value)){ 
			alert(SinCiud.value);Ciudad.focus();
		}else if (Provincia.value ==' ' || isEmpty(Provincia.value)){
			alert(SinProv.value);Provincia.focus();
		}else if (isEmpty(Cpostal.value)){ 
			alert(SinCPost.value);Cpostal.focus();
		}else if (Pais.value == "España" && (Cpostal.value.length !== 5 || !validaCampo2(Cpostal.value))){
			alert(ErrCPost.value);Cpostal.focus();
		}else if (Pais.value !== "España" && Cpostal.value.length < 5){
			alert(ErrCPost.value);Cpostal.focus();
		}else if (Pais.value !== "España" && FormaDePago == "fp1"){
			alert (ErrFPag.value);Suma.focus();
		}else if (Pais.value == "España" && FormaDePago == "fp1" && isEmpty(Telefono.value)){
			alert(SinTel.value);Telefono.focus();
		}else if (!isEmpty(Telefono.value) && (Telefono.value.length < 9 || !validaCampo2(Telefono.value))){
			alert(ErrTel.value);Telefono.focus();
		}else if (isEmpty(Email.value)){
			alert(SinMail.value);Email.focus();
		}else if (!validaEmail(Email.value)){
			alert(ErrMail.value);Email.focus();
		}else if (Comentarios.value.length > 300){
			alert(ErrComen.value);Comentarios.focus();
		}else{
		submit();
//		desactivar(this.form);
		}
	}
}

//function desactivar(formulario) {
//formulario.boton.disabled = true;
//formulario.submit();
//}

// ***********************************************************
// Función de retorno de cadena vacía
// ***********************************************************
function isEmpty(s){
   return ((s == null) || (s.length == 0))
}

// ***********************************************************
// Función que comprueba si los caracteres introducidos están en la variable    
// ***********************************************************
function validaCampo(s){
var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        // si el caracter en que estoy no aparece en la variable "caracteres",
        // entonces retornar falso
        if (caracteres.indexOf(c) == -1) return false;
    }
    return true;
}

// ***********************************************************
// Función que comprueba si los caracteres introducidos están en la variable    
// ***********************************************************
function validaCampo1(s){
var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        // si el caracter en que estoy no aparece en la variable "caracteres1",
        // entonces retornar falso
        if (caracteres1.indexOf(c) == -1) return false;
    }
    return true;
}

// ***********************************************************
// Función que comprueba si los caracteres introducidos están en la variable    
// ***********************************************************
function validaCampo2(s){
var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        // si el caracter en que estoy no aparece en la variable "caracteres2",
        // entonces retornar falso
        if (caracteres2.indexOf(c) == -1) return false;
    }
    return true;
}

// ***********************************************************
// Función para validad E-mail   
// ***********************************************************
function validaEmail(s){
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
						// /^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (filter.test(s)){
	return true;
	}else{
	return false;
	}
}
// JavaScript fin ---------->
