function muestraimagen(id, numero) {
	var nombre = 'productos/'+id+'-'+numero+'.jpg';
	document.getElementById('fotoproducto').src = nombre;
}

// FICHA PRODUCTO > CALCULO DE PRECIOS

function formateanumero(numero){

	resultado = parseFloat(numero).toFixed(2).toString();
	
	resultado = resultado.split(".");
	
	var cadena = ""; cont = 1;
	
	for(m=resultado[0].length-1; m>=0; m--){
	
		cadena = resultado[0].charAt(m) + cadena;
		
		cont%3 == 0 && m >0 ? cadena = "." + cadena : cadena = cadena;
		
		cont== 3 ? cont = 1 :cont++;
	}
	
	resp = cadena + "," + resultado[1];
	
	return resp;
}


function obtenmedida(){

	var medidasvalores = document.compra.medida;
	
	for (var i = 0; i< medidasvalores.length; i++) {
	
		if (medidasvalores[i].checked) {
		
			medida = medidasvalores[i].value;
			//alert(medida);
			
		}
	}
}

function obtenlavable(){

	var lavablevalores = document.compra.lavable;
	
	for (var i = 0; i< lavablevalores.length; i++) {
	
		if (lavablevalores[i].checked) {
		
			lavable = lavablevalores[i].value;
			//alert(lavable);
			
		}
	}
}

function calcula(operacion){

	if (operacion == 'mas'){
	
		document.getElementById('unidades').value ++;
		
	} else {
	
	if (operacion == 'menos')
	
		if (document.getElementById('unidades').value != 1){
		
			document.getElementById('unidades').value --;
		}
	}
	
	unidades = document.getElementById('unidades').value
	
	obtenmedida();
	
	obtenlavable();
		
	precionormal = unidades * precios[medida];
	
	//portes = 15
	portes = 15;
	preciolavable = (lavables[medida] * unidades)+portes;
	
	if (lavable == 1){
		preciototal = precionormal + preciolavable;
	} else {
		preciototal = precionormal;
	}
		
	
	for (var i in precios) {
		
		document.getElementById('strprecio'+i).innerHTML=formateanumero(precios[i]*unidades)+' &euro;';
			
	}
		
	document.getElementById('strpreciolavable').innerHTML='+ '+formateanumero(preciolavable)+' &euro;';
	
	document.getElementById('strpreciototal').innerHTML=formateanumero(preciototal)+' &euro;';
	
}


function calculanormal(operacion){

	if (operacion == 'mas'){
	
		document.getElementById('unidades').value ++;
		
	} else {
	
	if (operacion == 'menos')
	
		if (document.getElementById('unidades').value != 1){
		
			document.getElementById('unidades').value --;
		}
	}
	
	unidades = document.getElementById('unidades').value
	
	obtenmedida();
	
	//obtenlavable();
		
	precionormal = unidades * precios[medida];
	
	//portes = 15
	//portes = 15;
	//preciolavable = (lavables[medida] * unidades)+portes;
	
	//if (lavable == 1){
	//	preciototal = precionormal + preciolavable;
	//} else {
		preciototal = precionormal;
	//}
		
	
	for (var i in precios) {
		
		document.getElementById('strprecio'+i).innerHTML=formateanumero(precios[i]*unidades)+' &euro;';
			
	}
		
	//document.getElementById('strpreciolavable').innerHTML='+ '+formateanumero(preciolavable)+' &euro;';
	
	document.getElementById('strpreciototal').innerHTML=formateanumero(preciototal)+' &euro;';
	
}

// FIN CALCULO DE PRECIOS

// ELIMINAR PRODUCTO DE LA CESTA
function removecesta(i){
	if (document.cesta.cambiado.value=='si'){
			document.cesta.eliminar.value=i;
			document.cesta.submit();
	} else {
		document.location.href = "removecesta.php?id="+i;
	}
}


// FORMULARIO DATOS
function compruebafacturacion() {
	if(document.datos.fact_diferente[1].checked) {
		document.datos.fact_nombre.disabled = false;
		document.datos.fact_dninifcif.disabled = false;
		document.datos.fact_direccion.disabled = false;
		document.datos.fact_cp.disabled = false;
		document.datos.fact_poblacion.disabled = false;
		document.datos.fact_provincia.disabled = false;
	} else {
		document.datos.fact_nombre.disabled = true;
		document.datos.fact_dninifcif.disabled = true;
		document.datos.fact_direccion.disabled = true;
		document.datos.fact_cp.disabled = true;
		document.datos.fact_poblacion.disabled = true;
		document.datos.fact_provincia.disabled = true;
		
		if (document.datos.nombrefiscal.value != ''){
			document.datos.fact_nombre.value = document.datos.nombrefiscal.value;
		} else {
			document.datos.fact_nombre.value = document.datos.nombre.value + ' ' + document.datos.apellidos.value;
		}
		document.datos.fact_dninifcif.value = document.datos.dninifcif.value; 
		document.datos.fact_direccion.value = document.datos.direccion.value;
		document.datos.fact_cp.value = document.datos.cp.value;
		document.datos.fact_poblacion.value = document.datos.poblacion.value;
		document.datos.fact_provincia.value = document.datos.provincia.value;
	}
	compruebaenvio();
}

function compruebaenvio() {
	if (document.datos.env_diferente[2].checked) {
		document.datos.env_nombre.disabled = false;
		document.datos.env_direccion.disabled = false;
		document.datos.env_cp.disabled = false;
		document.datos.env_poblacion.disabled = false;
		document.datos.env_provincia.disabled = false;
	} else {
		document.datos.env_nombre.disabled = true;
		document.datos.env_direccion.disabled = true;
		document.datos.env_cp.disabled = true;
		document.datos.env_poblacion.disabled = true;
		document.datos.env_provincia.disabled = true;
		
		if (document.datos.env_diferente[0].checked) {
			if (document.datos.nombrefiscal.value != ''){
				document.datos.env_nombre.value = document.datos.nombrefiscal.value;
			} else {
				document.datos.env_nombre.value = document.datos.nombre.value + ' ' + document.datos.apellidos.value;
			}
			document.datos.env_direccion.value = document.datos.direccion.value;
			document.datos.env_cp.value = document.datos.cp.value;
			document.datos.env_poblacion.value = document.datos.poblacion.value;
			document.datos.env_provincia.value = document.datos.provincia.value;
		} else {
			document.datos.env_nombre.value = document.datos.fact_nombre.value;
			document.datos.env_direccion.value = document.datos.fact_direccion.value;
			document.datos.env_cp.value = document.datos.fact_cp.value;
			document.datos.env_poblacion.value = document.datos.fact_poblacion.value;
			document.datos.env_provincia.value = document.datos.fact_provincia.value;
		}
		
		
	}
	
}

// COMPRUEBA DATOS DE CESTA
function validardatoscesta() {
		var f = document.datos;
		if (f.nombre.value=='' || f.apellidos.value=='' || f.dninifcif.value=='' || f.direccion.value=='' || f.poblacion.value=='' || f.cp.value=='' || f.tel.value=='' || f.email.value==''){
			alert('Todos los campos marcados con (*) deben estar cumplimentados.');
			return false;
		}
		return true;
	}
	
// COMPRUEBA DATOS ALTA NEWSLETTER
function validaraltanewsletter() {
		var f = document.newsletter;
		if (f.newsemail.value==''){
			alert('Debes introducir un e-mail para poderte enviar el boletín.');
		} else {
		document.newsletter.submit();
		}
	}

// COMPRUEBA DATOS CONTACTO
function validardatoscontacto() {
		var f = document.datos;
		if (f.nombre.value=='' || f.apellidos.value=='' || f.email.value==''){
			alert('Todos los campos marcados con (*) deben estar cumplimentados.');
			return false;
		}
		return true;
	}
	
// CEPTA CONDICIONES DE VENTA EN UN PEDIDO
function aceptacondiciones() {
		var f = document.pedido;
		if (!f.condiciones.checked){
			alert('No podemos precesar el pedido si no acepta las CONDICIONES DE VENTA.');
			return false;
		}
		return true;
	}
	
// COMPRUEBA DATOS ILUSTRADOR
function validardatosilustrador() {
		var f = document.datos;
		if (f.nombre.value=='' || f.apellidos.value=='' || f.email.value==''){
			alert('Todos los campos marcados con (*) deben estar cumplimentados.');
			return false;
		}
		return true;
	}
