
// Funcion despliegue información adicional para detalle de producto

function despliega(seccion,contenido){
	
	if ( document.getElementById(contenido).style.display == 'block' ) {
          document.getElementById(contenido).style.display = 'none';
		  document.getElementById(seccion).className = 'cerrado';
      } else {
          document.getElementById(contenido).style.display = 'block';
	      document.getElementById(seccion).className = 'abierto';
      }
	
}


// Funcion despliegue información adicional para detalle de producto

function muestramas(boton,contenido){
	
          document.getElementById(contenido).style.display = 'block';
		  document.getElementById(boton).style.display = 'none';
}




// cambia la imagen y el pie a mostrar
function showImagen(cont){
	//obtenemos el número seleccionado anterior (current_selected) para quitarle el class=selected
	var current_selected = document.getElementById("current_selected").value;
	document.getElementById("contador_"+current_selected).className = "";
	//ponemos en current_selected el valor del actual seleccionado
	document.getElementById("contador_"+cont).className = "selected";
	document.getElementById("current_selected").value = cont;
	//cambiamos la imagen y el pie
	document.getElementById("imagen_seleccionada").src = "http://www.lomospain.com/tienda/images/detalles/"+document.getElementById("imagen_"+cont).value;
	document.getElementById("pie_seleccionado").innerHTML = document.getElementById("pie_"+cont).value;
}


// recomienda
function recomienda(id_producto){
	var nombre = document.getElementById("nombre").value;
	var email = document.getElementById("email").value;
	var nombre_recomendado = document.getElementById("nombre_recomendado").value;
	var email_recomendado = document.getElementById("email_recomendado").value;
	var texto_recomendado = document.getElementById("texto_recomendado").value;
	
	if (nombre=="" || email==""  ||	nombre_recomendado == "" ||	email_recomendado ==  "") {
			showRecomiendaError("Los datos marcados con asterisco * son obligatorios, tienes que completar tus datos para poder enviar tu recomendación.");
		} else if (!validarEmail(email) || !validarEmail(email_recomendado)){
			showRecomiendaError("Hay un error en las direcciones de correo, comprueba que ambas son correctas.");
		} else {
	
			
		    var url = "ajax_recomienda.php?nombre="+nombre+
		    									"&email="+email+
		    									"&nombre_recomendado="+nombre_recomendado+
		    									"&email_recomendado="+email_recomendado+
		    									"&texto_recomendado="+texto_recomendado+
		    									"&id_producto="+id_producto;
		    initRequest(url);
		    req.onreadystatechange = processRequest;
		    req.open("GET", url, true); 
		    req.send(null);
		}
}

//vuelve a mostrar el formulario de recomendación
function otraRecomendacion(){
	//limpiamos el formulario
	document.getElementById("form1").reset();
	document.getElementById("formulario").style.display='';
	document.getElementById("formulario").style.visibility='visible';
	document.getElementById("feedback").style.display='none';
}

//  muestra error de recomienda
function showRecomiendaError(texto){
	document.getElementById("formulario").style.display='none';
	document.getElementById("message_error").innerHTML=texto;
	document.getElementById("error_formulario").style.display='';
	document.getElementById("error_formulario").style.visibility='visible';
}

// cuando se muestra un error en la recomendación y se pulsa sobre Volver
function volverRecomendacion(){
	document.getElementById("formulario").style.display='';
	document.getElementById("formulario").style.visibility='visible';
	document.getElementById("error_formulario").style.display='none';
}

//respuesta AJAX
function processRequest() {
	if (req.readyState == 4) {
  	if (req.status == 200) {			    
			document.getElementById("formulario").style.display='none';
			document.getElementById("feedback").style.display='';
			document.getElementById("feedback").style.visibility='visible';
		}
	}  
}

function initRequest(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
}


// valida una dirección de email
 function validarEmail(valor) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor) ) {
   return (true)
  } else {
   return (false);
  }
 }
 
 
 
//selecciona un producto del combo
//preparamos las variables para que pueda añadirse al carrito
//mostramos el precio
function seleccionaProducto(id_producto, pvp, disponibilidad){
	if (disponibilidad==0){
		document.getElementById("precio_producto").innerHTML = pvp+" €";
		document.getElementById("id_producto").value = id_producto;
	} else {
		document.getElementById("precio_producto").innerHTML = "no disponible";
		document.getElementById("id_producto").value = "";		
	}
}


//añade un producto al carrito de la compra
function addCarro() {
	var id_producto = document.getElementById("id_producto").value;
	var id_producto_padre = document.getElementById("id_producto_padre").value;
	document.location.href="manageItem.php?action=add&id_producto="+id_producto+"&id_producto_padre="+id_producto_padre;
}