/**
 * @author Juan
 */

var mostrar_pedido = false;
var mostrar_inicio = false;
var mostrar_carrito = false;
var mostrar_carrito2 = false;
var mostrar_seccion = 0;
var mostrar_categoria = 0;
var empresa = false;
var sesion = "";
var txt_carrito = "Actualizando pedido . . .";
var txt_categorias = "Espere por favor";
var dominio_ssl = '192.168.1.4';
var txt_contenido = "Cargando . . .";
var imagen_actual = '';
var ssl = 0;

function retrieveURL(url) {
		
	if (window.XMLHttpRequest) { // Non-IE browsers
	  req = new XMLHttpRequest();
	  req.onreadystatechange = processStateChange;
	  try {
	    req.open("GET", url, true);
	  } catch (e) {
	    alert(e);
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
	    req.onreadystatechange = processStateChange;
	    req.open("GET", url, true);
	    req.send();
	  }
	}
} 

function processStateChange() {
	document.getElementById("contenido").innerHTML = "<div style='width: 400px;' align=\"center\"><h2 style=\"font-family: 'Trebuchet MS'; font-size: 18px; color: #aabfca;\">" + txt_contenido +"</h2><img src=\"imgs/carga_contenido.gif\" /></div>";
   //document.getElementById("contenido").innerHTML = "<div style='width: 426px;'><h2 style=\"color:black;\">Cargando ...</h2></div>";
   if (req.readyState == 4) { // Complete
     if (req.status == 200) { // OK response
       document.getElementById("contenido").innerHTML = req.responseText;
	   if (mostrar_carrito)
		   	mostrarCarrito();
		
	} else {
        alert("Problema: " + req.status);
     }
   }
}

function sesionURL(url) {		
	if (window.XMLHttpRequest) { // Non-IE browsers
	  req = new XMLHttpRequest();
	  req.onreadystatechange = callbackSesion;
	  try {
	    req.open("GET", url, true);
	  } catch (e) {
	    alert(e);
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
	    req.onreadystatechange = callbackSesion;
	    req.open("GET", url, true);
	    req.send();
	  }
	}
} 

function callbackSesion() {
   document.getElementById("registro").innerHTML = "<div style='width: 200px;' align=\"center\"><h2 style=\"color:black; font-size: 12px; font-family: 'Trebuchet MS'; color: #aabfca;\">" + sesion +"</h2><img src=\"imgs/carga_login.gif\" /></div>";
   if (req.readyState == 4) { // Complete
     if (req.status == 200) { // OK response
       document.getElementById("registro").innerHTML = req.responseText;
       if (ssl == 1)
	   	document.location.href='http://www.altamirastock.com/index.php';
	   else
	   	document.location.href='http://www.altamirastock.com/index.php';
	   ssl = 0;
	} else {
        alert("Problema: " + req.status);
     }
   }
}

function datosSocioURL(url) {
		
	if (window.XMLHttpRequest) { // Non-IE browsers
	  req = new XMLHttpRequest();
	  req.onreadystatechange = callbackDatosSocio;
	  try {
	    req.open("GET", url, true);
	  } catch (e) {
	    alert(e);
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
	    req.onreadystatechange = callbackDatosSocio;
	    req.open("GET", url, true);
	    req.send();
	  }
	}
} 

function callbackDatosSocio() {
   //document.getElementById("contenido").innerHTML = "<div style='width: 426px;'><h2 style=\"color:black;\">Cargando ...</h2></div>";
   document.getElementById("datos_usuario_ctdo").innerHTML = "<div style='width: 400px;' align=\"center\"><h2 style=\"font-family: 'Trebuchet MS'; font-size: 18px; color: #aabfca;\">" + txt_contenido +"</h2><img src=\"imgs/carga_contenido.gif\" /></div>";
   if (req.readyState == 4) { // Complete
     if (req.status == 200) { // OK response
       document.getElementById("datos_usuario_ctdo").innerHTML = req.responseText;
	} else {
        alert("Problema: " + req.status);
     }
   }
}

function carritoURL(url) {
		
	if (window.XMLHttpRequest) { // Non-IE browsers
	  req = new XMLHttpRequest();
	  req.onreadystatechange = callbackCarrito;
	  try {
	    req.open("GET", url, true);
	  } catch (e) {
	    alert(e);
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
	    req.onreadystatechange = callbackCarrito;
	    req.open("GET", url, true);
	    req.send();
	  }
	}
} 

function callbackCarrito() {
	document.getElementById("id_carrito").innerHTML = "<div style='width: 200px;' align=\"center\"><h2 style=\"color:black; font-size: 12px; color: #aabfca;font-family: 'Trebuchet MS';\">" + txt_carrito +"</h2><img src=\"imgs/carga_carrito.gif\" /></div>";
   if (req.readyState == 4) { // Complete
     if (req.status == 200) { // OK response
       document.getElementById("id_carrito").innerHTML = req.responseText;	 
	   if (mostrar_pedido == true){
	   		cargarPedido();
			mostrar_pedido = false;
			
		}  
		carritoDinURL('php/articulos/carrito_dinamico.inc.php');
	} else {
        alert("Problema: " + req.status);
     }
   }
}

function carritoDinURL(url) {
	if (window.XMLHttpRequest) { // Non-IE browsers
	  req = new XMLHttpRequest();
	  req.onreadystatechange = callbackCarritoDin;
	  try {
	    req.open("GET", url, true);
	  } catch (e) {
	    alert(e);
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
	    req.onreadystatechange = callbackCarritoDin;
	    req.open("GET", url, true);
	    req.send();
	  }
	}
} 

function callbackCarritoDin() {
	//document.getElementById("id_carrito").innerHTML = "<div style='width: 200px;' align=\"center\"><h2 style=\"color:black; font-size: 12px; color: #aabfca;font-family: 'Trebuchet MS';\">" + txt_carrito +"</h2><img src=\"imgs/carga_carrito.gif\" /></div>";
   if (req.readyState == 4) { // Complete
     if (req.status == 200) { // OK response
       document.getElementById("cont_carrito_din").innerHTML = req.responseText;	 
	    
	} else {
        alert("Problema: " + req.status);
     }
   }
}

function formularioURL(url) {
		
	if (window.XMLHttpRequest) { // Non-IE browsers
	  req = new XMLHttpRequest();
	  req.onreadystatechange = mostrar_formulario;
	  try {
	    req.open("GET", url, true);
	  } catch (e) {
	    alert(e);
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
	    req.onreadystatechange = mostrar_formulario;
	    req.open("GET", url, true);
	    req.send();
	  }
	}
} 

function mostrar_formulario() {
	document.getElementById("contenido").innerHTML = "<div style='width: 400px;' align=\"center\"><h2 style=\"font-family: 'Trebuchet MS'; font-size: 18px; color: #aabfca;\">" + txt_contenido +"</h2><img src=\"imgs/carga_contenido.gif\" /></div>";
   //document.getElementById("contenido").innerHTML = "<div style='width: 426px;'><h2 style=\"color:black;\">Cargando ...</h2></div>";
   if (req.readyState == 4) { // Complete
     if (req.status == 200) { // OK response
       document.getElementById("contenido").innerHTML = req.responseText;
	   
	} else {
        alert("Problema: " + req.status);
     }
   }
}

function detalleURL(url) {
		
	if (window.XMLHttpRequest) { // Non-IE browsers
	  req = new XMLHttpRequest();
	  req.onreadystatechange = callbackDetalle;
	  try {
	    req.open("GET", url, true);
	  } catch (e) {
	    alert(e);
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
	    req.onreadystatechange = callbackDetalle;
	    req.open("GET", url, true);
	    req.send();
	  }
	}
} 

function callbackDetalle() {
	
   //document.getElementById("detalles").innerHTML = "<div style='width: 426px;'><h2 style=\"color:black;\">Cargando ...</h2></div>";
   if (req.readyState == 4) { // Complete
     if (req.status == 200) { // OK response
       document.getElementById("detalles").innerHTML = req.responseText;
	   
	} else {
        alert("Problema: " + req.status);
     }
   }
}

function menuCategoriasURL(url) {
		
	if (window.XMLHttpRequest) { // Non-IE browsers
	  req = new XMLHttpRequest();
	  req.onreadystatechange = callbackMenu;
	  try {
	    req.open("GET", url, true);
	  } catch (e) {
	    alert(e);
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
	    req.onreadystatechange = callbackMenu;
	    req.open("GET", url, true);
	    req.send();
	  }
	}
} 

function callbackMenu() {
	document.getElementById("menu_cat").innerHTML = "<div id='cargando'><p>" + txt_categorias +"</p><center><img src=\"imgs/carga_categorias.gif\" /></center></div>";
   //document.getElementById("menu_cat").innerHTML = "<h2 style=\"color:black;\">Cargando ...</h2>";
   if (req.readyState == 4) { // Complete
     if (req.status == 200) { // OK response
       document.getElementById("menu_cat").innerHTML = req.responseText;
	   if (mostrar_inicio){
	   		// Cargamos el menu central
			var url = "php/secciones/inicio.inc.php";
			retrieveURL(url);
			mostrar_inicio = false;
	   }
	   else if (mostrar_seccion > 0){
	   		// Cargamos la pagina con la seccion
			var url = 'php/secciones/seccion.inc.php?seccion=' + mostrar_seccion;
			retrieveURL(url);
			
	   }
	   if (mostrar_categoria > 0){
	   		// Cargamos la pagina con la seccion
			var url = 'php/articulos/articulos.inc.php?seccion=' + mostrar_seccion + '&categoria=' + mostrar_categoria;
			retrieveURL(url);
			
	   }
	   mostrar_seccion = 0;
	   mostrar_categoria = 0;
	} else {
        alert("Problema: " + req.status);
     }
   }
}

function provinciasURL(url) {
		
	if (window.XMLHttpRequest) { // Non-IE browsers
	  req = new XMLHttpRequest();
	  req.onreadystatechange = callbackProvincias;
	  try {
	    req.open("GET", url, true);
	  } catch (e) {
	    alert(e);
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
	    req.onreadystatechange = callbackProvincias;
	    req.open("GET", url, true);
	    req.send();
	  }
	}
} 

function callbackProvincias() {
   if (req.readyState == 4) { // Complete
     if (req.status == 200) { // OK response
       if (empresa == false)
	       document.getElementById("provincias").innerHTML = req.responseText;
	   else if (empresa == true)
	   	   document.getElementById("provincias_e").innerHTML = req.responseText;
	} else {
        alert("Problema: " + req.status);
     }
   }
}

function modSocio(url) {
	if (window.XMLHttpRequest) { // Non-IE browsers
	  req = new XMLHttpRequest();
	  req.onreadystatechange = modSocioCallback;
	  try {
	    req.open("GET", url, true);
	  } catch (e) {
	    alert(e);
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
	    req.onreadystatechange = modSocioCallback;
	    req.open("GET", url, true);
	    req.send();
	  }
	}
} 

function modSocioCallback() {
	document.getElementById("contenido").innerHTML = "<div style='width: 400px;' align=\"center\"><h2 style=\"font-family: 'Trebuchet MS'; font-size: 18px; color: #aabfca;\">" + txt_contenido +"</h2><img src=\"imgs/carga_contenido.gif\" /></div>";
   //document.getElementById("contenido").innerHTML = "<div style='width: 426px;'><h2 style=\"color:black;\">Cargando ...</h2></div>";
   if (req.readyState == 4) { // Complete
     if (req.status == 200) { // OK response
       var retorno = req.responseText;
	   alert(retorno);
	   if (retorno == "0"){
			socioDatos();
	   }
	   else{
			alert("Error actualizando los datos del socio");
			socioDatos();
	   }
	} else {
        alert("Problema: " + req.status);
     }
   }
}

// Funcion para incrementar el contador de las lineas
function incrCantidad(id){
	var cantidad = parseInt(document.getElementById(id).value);
	if (cantidad < 99)
		cantidad++;
	document.getElementById(id).value = cantidad;
}

// Funcion para decrementar el contador de las lineas
function decrCantidad(id){
	var cantidad = parseInt(document.getElementById(id).value);
	if (cantidad > 1)
		cantidad--;
	document.getElementById(id).value = cantidad;
}

function cargarDetalle(id){
	txt_contenido = "Cargando detalle . . .";
	var url = 'php/articulos/detalle.inc.php?id=' + id;
	retrieveURL(url);
}

function cargarDetalleBuscador(id, cadena){
	txt_contenido = "Cargando detalle . . .";
	var url = 'php/buscador/detalle_buscador.inc.php?id=' + id + '&cadena=' + cadena;
	retrieveURL(url);
}

function cargarArticulos(seccion, categoria){
	txt_contenido = "Cargando articulos . . .";
	var url = 'php/articulos/articulos.inc.php?seccion=' + seccion + '&categoria=' + categoria;
	retrieveURL(url);
}

function mostrarDetalle(id){
	txt_contenido = "Cargando detalle . . .";
	imagen_actual = id + ".jpg";
	var url = 'php/articulos/datos_articulo.inc.php?id=' + id;
	detalleURL(url);
}

function cambiarMenuCategorias(id_seccion){
	txt_contenido = "Cambiando seccion . . .";
	var url = 'php/menus/menu_categorias.inc.php?id=' + id_seccion;
	mostrar_seccion = id_seccion;
	menuCategoriasURL(url);
	
}

function cambiarMenuCategoriasN(id_seccion, id_categoria){
	txt_contenido = "Cambiando seccion . . .";
	var url = 'php/menus/menu_categorias.inc.php?id=' + id_seccion;
	mostrar_seccion = id_seccion;
	mostrar_categoria = id_categoria;
	menuCategoriasURL(url);
	
}

function cargarFormulario(){
	txt_contenido = "Cargando formulario . . .";
	var url = 'php/registro/formulario.inc.php';
	formularioURL(url);
	//var url = 'https://' + dominio_ssl + '/index.php?form=1';
	//window.location.href = url;
}

function cambiarFormAdscripcion(){
	var tipo = document.getElementById('tipo_cliente').value;
	if (tipo == 1){
		document.getElementById('form_empresa').style.display = 'none';
		document.getElementById('form_particular').style.display = 'block';
		

	}
	else{
		document.getElementById('form_empresa').style.display = 'block';
		document.getElementById('form_particular').style.display = 'none';
	}
}

function cambiarFormPago(){
	var tipo = document.getElementById('formas_pago').value;
	if (tipo == 1){
		//document.getElementById('form_tarjeta').style.display = 'block';
		document.getElementById('form_transferencia').style.display = 'none';
		document.getElementById('form_reembolso').style.display = 'none';
		document.getElementById('form_paypal').style.display = 'none';
		document.getElementById('form_google').style.display = 'none';
	}
	else if (tipo == 2){
		//document.getElementById('form_tarjeta').style.display = 'none';
		document.getElementById('form_transferencia').style.display = 'block';
		document.getElementById('form_reembolso').style.display = 'none';
		document.getElementById('form_paypal').style.display = 'none';
		document.getElementById('form_google').style.display = 'none';
	}
	else if (tipo == 3){
		//document.getElementById('form_tarjeta').style.display = 'none';
		document.getElementById('form_transferencia').style.display = 'none';
		document.getElementById('form_reembolso').style.display = 'block';
		document.getElementById('form_paypal').style.display = 'none';
		document.getElementById('form_google').style.display = 'none';
	}
	else if (tipo == 4){
		//document.getElementById('form_tarjeta').style.display = 'none';
		document.getElementById('form_transferencia').style.display = 'none';
		document.getElementById('form_reembolso').style.display = 'none';
		document.getElementById('form_paypal').style.display = 'block';
		document.getElementById('form_google').style.display = 'none';
	}
	else if (tipo == 5){
		//document.getElementById('form_tarjeta').style.display = 'none';
		document.getElementById('form_transferencia').style.display = 'none';
		document.getElementById('form_reembolso').style.display = 'none';
		document.getElementById('form_paypal').style.display = 'none';
		document.getElementById('form_google').style.display = 'block';
	}
}
function cargarRegistro(){
	txt_contenido = "Cargando registro . . .";
	var url="php/registro.inc.php";
	retrieveURL(url);
}

function cargarPedido(){
	txt_contenido = "Cargando pedido . . .";
	var url= "php/compra/pedido.inc.php";
	retrieveURL(url);
}

function formaPago(){
	// Comprobamos si ha puesto una nueva direccion
	var direccion = document.getElementById('direccion').value;
	var cod_postal = document.getElementById('cod_postal').value;
	var localidad = document.getElementById('localidad').value;
	var provincia = document.getElementById('provincia').value;
	var pais = document.getElementById('pais').value;
	
	if (direccion !== "" || cod_postal !== "" || provincia !== "" || pais !== "" || document.getElementById('sin_direccion').value == 'true'){
		var error = true;
		
		// Validamos la direccion
		
		if (!validar_direccion(document.getElementById('direccion')) || document.getElementById('direccion').value === "")
		{
			document.getElementById('txt_direccion').style.color = 'red';
			error = false;
		}
		else{
			document.getElementById('txt_direccion').style.color = 'black';
		}
		
		// Localidad
		if (!validar_nombre(document.getElementById('localidad')) || document.getElementById('localidad').value === "")
		{
			document.getElementById('txt_localidad').style.color = 'red';
			error = false;
		}
		else{
			document.getElementById('txt_localidad').style.color = 'black';
		}
			
		// Codigo postal / ZIP
		if (document.getElementById('cod_postal').value === "")
		{
			document.getElementById('txt_cod_postal').style.color = 'red';
			error = false;
		}
		else{
			document.getElementById('txt_cod_postal').style.color = 'black';
		}
			
		// Provincia
		if (!validar_nombre(document.getElementById('provincia')) || document.getElementById('provincia').value === "")
		{
			document.getElementById('txt_provincia').style.color = 'red';
			error = false;
		}
		else{
			document.getElementById('txt_provincia').style.color = 'black';
		}
			
		if (document.getElementById('pais').value === "")
		{
			document.getElementById('txt_pais').style.color = 'red';
			error = false;
		}
		else{
			document.getElementById('txt_pais').style.color = 'black';
		}
		
		if (error == false){
			document.getElementById('msg_error').innerHTML = '<div style="height: 20px; border: 1px solid red; color: red; font-weight: bold;">Datos incorrectos. Verifique los campos marcados en rojo</div>';
		}
		else{
			document.getElementById('msg_error').innerHTML = '';
			// Almacenamos la nueva direccion
			var esEnvio = document.getElementById('dir_envio').checked;
			var url = "php/compra/forma_pago.inc.php?tipo=insercion&direccion=" + direccion + "&localidad=" + localidad + "&cod_postal=" + cod_postal + "&provincia=" + provincia + "&pais=" + pais;
			if (esEnvio)
				url = url + "&esEnvio=true";
			retrieveURL(url);
		}
	}
	else{
		var url = "php/compra/forma_pago.inc.php";
		retrieveURL(url);
	}
}

function direccionEnvio(){
	txt_contenido = "Cargando direccion de envio . . .";
	var url = "php/compra/direcc_envio.inc.php";
	retrieveURL(url);
}

function comoComprar(){
	txt_contenido = "Cargando como comprar . . .";
	var url = "php/textos/como_comprar.inc.php";
	retrieveURL(url);
}

function formaPagar(){
	txt_contenido = "Cargando forma de pago . . .";
	var url = "php/textos/como_pagar.inc.php";
	retrieveURL(url);
}

function datosEmpresa(){
	txt_contenido = "Cargando informacion de la empresa . . .";
	var url = "php/textos/datos_empresa.inc.php";
	retrieveURL(url);
}

function validarFormulario(){
	var tipoForm = document.getElementById('tipo_cliente').value;
	
	if (document.getElementById('valido_priv').checked == true) {
		if (tipoForm == 1) {
			// Validamos los datos para un particular
			if (validarParticular()) 
				// Damos de alta el socio
				altaSocio(1);
			else {
				document.getElementById('msg_error').innerHTML = '<div style="height: 20px; border: 1px solid red; color: red; font-weight: bold;">Datos incorrectos. Verifique los campos marcados en rojo</div>';
			}
		}
		else 
			if (tipoForm == 2) {
				// Validamos los datos para una empresa
				if (validarEmpresa()) 
					// Damos de alta el socio
					altaSocio(2);
				else {
					document.getElementById('msg_error_e').innerHTML = '<div style="height: 20xp; border: 1px solid red; color: red; font-weight: bold;">Datos incorrectos. Verifique los campos marcados en rojo</div>';
				}
			}
	}
	else{
		if (tipoForm == 1)
			document.getElementById('msg_error').innerHTML = '<div style="height: 20px; border: 1px solid red; color: red; font-weight: bold;">Debe aceptar los terminos de privacidad para poder continuar</div>';
		else if (tipoForm == 2)
			document.getElementById('msg_error_e').innerHTML = '<div style="height: 20px; border: 1px solid red; color: red; font-weight: bold;">Debe aceptar los terminos de privacidad para poder continuar</div>';
	}
}

function validarEmpresa(){
	var error = true;
	
	// Validamos los datos de los particulares
	
	// CIF
	if (document.getElementById('emp_cif').value === "") {
		document.getElementById('txt_emp_cif').style.color = 'red';
		error = false;
	}
	else {
		document.getElementById('txt_emp_cif').style.color = 'black';
	}
	
	// Nombre
	if (!validar_nombre(document.getElementById('nombre_e')) || document.getElementById('nombre_e').value === "") {
		document.getElementById('txt_nombre_e').style.color = 'red';
		error = false;
	}
	else {
		document.getElementById('txt_nombre_e').style.color = 'black';
	}
	
	// Direccion
	if (!validar_direccion(document.getElementById('direccion_e')) || document.getElementById('direccion_e').value === "") {
		document.getElementById('txt_direccion_e').style.color = 'red';
		error = false;
	}
	else {
		document.getElementById('txt_direccion_e').style.color = 'black';
	}
	
	// Localidad
	if (!validar_direccion(document.getElementById('localidad_e')) || document.getElementById('localidad_e').value === "") {
		document.getElementById('txt_localidad_e').style.color = 'red';
		error = false;
	}
	else {
		document.getElementById('txt_localidad_e').style.color = 'black';
	}
	
	// Codigo postal / ZIP
	if (document.getElementById('cod_postal_e').value === "") {
		document.getElementById('txt_cod_postal_e').style.color = 'red';
		error = false;
	}
	else {
		document.getElementById('txt_cod_postal_e').style.color = 'black';
	}
	
	// Pais
	if (document.getElementById('pais_e').value === "") {
		document.getElementById('txt_pais_e').style.color = 'red';
		error = false;
	}
	else {
		document.getElementById('txt_pais_e').style.color = 'black';
	}
	
	// Provincia
	// Comprobamos si es Espanya u otro pais
	if (document.getElementById('pais_e').value == "73") {
		if (document.getElementById('provincia_esp').value === ""){
			document.getElementById('txt_provincia_e').style.color = 'red';
			error = false;
		}
		else{
			document.getElementById('txt_provincia_e').style.color = 'black';
		}
	}
	else {
		if (!validar_nombre(document.getElementById('provincia_e')) || document.getElementById('provincia_e').value === "") {
			document.getElementById('txt_provincia_e').style.color = 'red';
			error = false;
		}
		else {
			document.getElementById('txt_provincia_e').style.color = 'black';
		}
	}
	
	// Tel. Contacto
	if (!validar_telefono(document.getElementById('tfno_contacto_e')) || document.getElementById('tfno_contacto_e').value === "") {
		document.getElementById('txt_tfno_contacto_e').style.color = 'red';
		error = false;
	}
	else {
		document.getElementById('txt_tfno_contacto_e').style.color = 'black';
	}
	
	// Fax
	if (document.getElementById('fax_e').value != "") {
		if (!validar_telefono(document.getElementById('fax_e')) || document.getElementById('fax_e').value === "") {
			document.getElementById('txt_fax_e').style.color = 'red';
			error = false;
		}
		else {
			document.getElementById('txt_fax_e').style.color = 'black';
		}
	}
		
	// Email
	if (!validar_email(document.getElementById('email_e').value) || document.getElementById('email_e').value === "")
	{
		document.getElementById('txt_email_e').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_email_e').style.color = 'black';
	}
		
	// Passwords
	if ((document.getElementById('pass_e').value !== document.getElementById('pass2_e').value))
	{
		document.getElementById('txt_pass_e').style.color = 'red';
		document.getElementById('txt_pass2_e').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_pass_e').style.color = 'black';
		document.getElementById('txt_pass2_e').style.color = 'black';
	}
	
	if (document.getElementById('pass_e').value === "" || document.getElementById('pass2_e').value === "")
	{
		document.getElementById('txt_pass_e').style.color = 'red';
		document.getElementById('txt_pass2_e').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_pass_e').style.color = 'black';
		document.getElementById('txt_pass2_e').style.color = 'black';
	}
	
	return error;
}

function validarParticular(){
	var error = true;
	
	// Validamos los datos de los particulares
	
	// NIF
	if (document.getElementById('nif').value === "")
	{
		document.getElementById('txt_nif').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_nif').style.color = 'black';
	}
	
	// Nombre
	if (!validar_nombre(document.getElementById('nombre')) || document.getElementById('nombre').value === "")
	{
		document.getElementById('txt_nombre').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_nombre').style.color = 'black';
	}
		
	// Apellidos
	if (!validar_nombre(document.getElementById('apellidos')) || document.getElementById('apellidos').value === "")
	{
		document.getElementById('txt_apellidos').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_apellidos').style.color = 'black';
	}
	
	// Direccion
	if (!validar_direccion(document.getElementById('direccion')) || document.getElementById('direccion').value === "")
	{
		document.getElementById('txt_direccion').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_direccion').style.color = 'black';
	}
	
	// Localidad
	if (!validar_direccion(document.getElementById('localidad')) || document.getElementById('localidad').value === "")
	{
		document.getElementById('txt_localidad').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_localidad').style.color = 'black';
	}
		
	// Codigo postal / ZIP
	if (document.getElementById('cod_postal').value === "")
	{
		document.getElementById('txt_zip').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_zip').style.color = 'black';
	}
		
	// Provincia
	// Comprobamos si es Espanya u otro pais
	if (document.getElementById('pais').value == "73") {
		if (document.getElementById('provincia_esp').value === ""){
			document.getElementById('txt_provincia').style.color = 'red';
			error = false;
		}
		else{
			document.getElementById('txt_provincia').style.color = 'black';
		}
	}
	else {
		if (!validar_nombre(document.getElementById('provincia')) || document.getElementById('provincia').value === "") {
			document.getElementById('txt_provincia').style.color = 'red';
			error = false;
		}
		else {
			document.getElementById('txt_provincia').style.color = 'black';
		}
	}
		
	if (document.getElementById('pais').value === "")
	{
		document.getElementById('txt_pais').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_pais').style.color = 'black';
	}
	
	// Tel. Contacto
	if(!validar_telefono(document.getElementById('tfno_contacto')) || document.getElementById('tfno_contacto').value === "")
	{
		document.getElementById('txt_telefono').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_telefono').style.color = 'black';
	}
		
	// Fax
	if (document.getElementById('fax').value != "") {
		if (!validar_telefono(document.getElementById('fax')) || document.getElementById('fax').value === "") {
			document.getElementById('txt_fax').style.color = 'red';
			error = false;
		}
		else {
			document.getElementById('txt_fax').style.color = 'black';
		}
	}	
	// Email
	if (!validar_email(document.getElementById('email').value) || document.getElementById('email').value === "")
	{
		document.getElementById('txt_email').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_email').style.color = 'black';
	}
		
	// Passwords
	if ((document.getElementById('passw').value !== document.getElementById('pass2').value))
	{
		document.getElementById('txt_pass').style.color = 'red';
		document.getElementById('txt_pass2').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_pass').style.color = 'black';
		document.getElementById('txt_pass2').style.color = 'black';
	}
	
	if (document.getElementById('passw').value === "" || document.getElementById('pass2').value === "")
	{
		document.getElementById('txt_pass').style.color = 'red';
		document.getElementById('txt_pass2').style.color = 'red';
		error = false;
	}
	else{
		document.getElementById('txt_pass').style.color = 'black';
		document.getElementById('txt_pass2').style.color = 'black';
	}
	
	return error;
}

function altaSocio(tipo){

	if (tipo == 1){
		var nif = document.getElementById('nif').value;
		var nombre = document.getElementById('nombre').value;
		var apellidos = document.getElementById('apellidos').value;
		var direccion = document.getElementById('direccion').value;
		var cod_postal = document.getElementById('cod_postal').value;
		var pais = document.getElementById('pais').value;
		var provincia = "";
		
		if (pais == "73")
			provincia = document.getElementById('provincia_esp').value;
		else
			provincia = document.getElementById('provincia').value;		
		var telefono = document.getElementById('tfno_contacto').value;
		var fax = document.getElementById('fax').value;
		var email = document.getElementById('email').value;
		var pass = document.getElementById('passw').value;
		var localidad = document.getElementById('localidad').value;
		var captcha = document.getElementById('captcha').value;
		var url = 'php/registro/registro.inc.php?tipo=particular&nif=' + nif + '&nombre=' + nombre + '&apellidos=' + apellidos + '&direccion=' + direccion + '&cod_postal=' + cod_postal + '&provincia=' + provincia + '&pais=' + pais + '&telefono=' + telefono + '&fax=' + fax + '&email=' + email + '&pass=' + pass + '&localidad=' + localidad + '&captcha=' + captcha;
		retrieveURL(url);
	}
	else if (tipo == 2){
		var cif = document.getElementById('emp_cif').value;
		var nombre = document.getElementById('nombre_e').value;
		var direccion = document.getElementById('direccion_e').value;
		var cod_postal = document.getElementById('cod_postal_e').value;
		var pais = document.getElementById('pais_e').value;
		var provincia = "";
		if (pais == "73")
			provincia = document.getElementById('provincia_esp').value;
		else
			provincia = document.getElementById('provincia_e').value;
		var telefono = document.getElementById('tfno_contacto_e').value;
		var fax = document.getElementById('fax_e').value;
		var email = document.getElementById('email_e').value;
		var pass = document.getElementById('pass_e').value;
		var localidad = document.getElementById('localidad_e').value;
		var captcha = document.getElementById('captcha').value;
		var url = 'php/registro/registro.inc.php?tipo=empresa&cif=' + cif + '&nombre=' + nombre + '&direccion=' + direccion + '&cod_postal=' + cod_postal + '&provincia=' + provincia + '&pais=' + pais + '&telefono=' + telefono + '&fax=' + fax + '&email=' + email + '&pass=' + pass + '&localidad=' + localidad + '&captcha=' + captcha;
		retrieveURL(url);
	}
}

function agregarArticulo(producto, unidades){
	var url = 'php/compra/gestion_pedido.inc.php?tipo=insercion&id=' + producto + '&cantidad=' + unidades;
	carritoURL(url);

}

function borrarArticulo(producto){
	var url = 'php/compra/gestion_pedido.inc.php?tipo=borrado&id=' + producto;
	mostrar_pedido = true;
	carritoURL(url);
	
}

function borrarArticuloDin(producto){
	var url = 'php/compra/gestion_pedido.inc.php?tipo=borrado&id=' + producto;
	mostrar_pedido = false;	
	carritoURL(url);	
}

function cargarHome(){
	// Ponemos el menu lateral
	var url ="php/menus/menu_principal.inc.php";
	menuCategoriasURL(url);
	mostrar_inicio = true;	
}

function mostrarCarrito(){
	var url ="php/compra/gestion_pedido.inc.php";
	carritoURL(url);
	
}

function realizarVenta(){
	var url = "php/compra/realizar_venta.inc.php";
	txt_contenido = "Realizando la compra . . .";
	retrieveURL(url);
	mostrar_carrito = true;
}

function mostrarResumen(){
	var tipo = document.getElementById('formas_pago').value;
	var usuario = '';
	var passwd = '';
	// Comprobamos si es paypal o google checkout
	if (tipo == 4){ // PayPal
		/*var usuario = document.getElementById('usu_paypal').value;
		var passwd = document.getElementById('pass_paypal').value;*/		
	}
	else if (tipo == 5){ // Google checkout
		var usuario = document.getElementById('usu_google').value;
		var passwd = document.getElementById('pass_google').value;
	}
	var url = "php/compra/resumen.inc.php?tipo=" + tipo + "&usuario=" + usuario + "&pass=" + passwd;
	retrieveURL(url);
}

function mostrarResumenPayPalFAIL(){
	
	var url = "php/compra/resumen.inc.php?tipo=4";
	mostrar_carrito = true;
	retrieveURL(url);
}

function mostrarTallaje(){
	var url = "php/tallaje.inc.php";
	retrieveURL(url);
}

function abrirSesion(usuario, passwd){
	sesion = "Iniciando sesion . . .";
	ssl = 1;
	var url = "php/registro/verif_usuario.inc.php?abrir=1&usuario=" + usuario + "&pass=" + passwd;
	sesionURL(url);
}

function cerrarSesion(){
	sesion = "Cerrando sesion . . .";
	ssl = 0;
	var url = "php/registro/verif_usuario.inc.php?cerrar=1";
	sesionURL(url);
}

function socioDatos(){
	txt_contenido = "Cargando datos del socio . . .";
	var url = "php/usuario/datos_socio.inc.php";
	retrieveURL(url);
	
}

function socioInformacion(tipo){
	txt_contenido = "Cargando informacion . . .";
	var url = "php/usuario/datosSocio.inc.php?tipo=" + tipo;
	datosSocioURL(url);
}

function limpiarFormularios(){
	// Limpiamos los formularios de adscripcion
	document.getElementById('txt_nif').style.color = 'black';
	document.getElementById('txt_nombre').style.color = 'black';
	document.getElementById('txt_apellidos').style.color = 'black';
	document.getElementById('txt_direccion').style.color = 'black';
	document.getElementById('txt_localidad').style.color = 'black';
	document.getElementById('txt_zip').style.color = 'black';
	document.getElementById('txt_provincia').style.color = 'black';
	document.getElementById('txt_pais').style.color = 'black';
	document.getElementById('txt_telefono').style.color = 'black';
	document.getElementById('txt_fax').style.color = 'black';
	document.getElementById('txt_email').style.color = 'black';
	document.getElementById('txt_pass').style.color = 'black';
	document.getElementById('txt_pass2').style.color = 'black';
	document.getElementById('txt_emp_cif').style.color = 'black';
	document.getElementById('txt_nombre_e').style.color = 'black';
	document.getElementById('txt_direccion_e').style.color = 'black';
	document.getElementById('txt_localidad_e').style.color = 'black';
	document.getElementById('txt_cod_postal_e').style.color = 'black';
	document.getElementById('txt_provincia_e').style.color = 'black';
	document.getElementById('txt_pais_e').style.color = 'black';
	document.getElementById('txt_tfno_contacto_e').style.color = 'black';
	document.getElementById('txt_fax_e').style.color = 'black';
	document.getElementById('txt_email_e').style.color = 'black';
	document.getElementById('txt_pass_e').style.color = 'black';
	document.getElementById('txt_pass2_e').style.color = 'black';
	document.getElementById('msg_error').innerHTML = '';
	document.getElementById('msg_error_e').innerHTML = '';
}

   function getElementsByClassName(clase){
    var elementos = document.getElementsByTagName('*');
    var listado = new Array();

    for(var i=0;i<elementos.length;i++){
    if( (elementos[i].id != '') && (elementos[i].className == clase) ){
    listado.push(elementos[i].id);
    }
    }

    return listado;
   } 
   
   function limpiarMenuSuperior(){
   		$$('td.menuactual').each(cambiar);		
   }
   
   function cambiar(element){
   		
   		element.removeClassName('menuactual');
		element.addClassName('menu_princ');
	}
	
	function limpiarMenuLateral(){
   		$$('td.enlace_lateral_sel').each(cambiar2);		
   }
   
   function cambiar2(element){   		
   		element.removeClassName('enlace_lateral_sel');
		element.addClassName('enlace_lateral');
	}
	
   function cargarCatalogo(){
		txt_contenido = "Cargando catalogo . . .";
   		var url = "php/secciones/catalogo.inc.php";
		retrieveURL(url);
   }

   function realizarBusqueda(){
		txt_contenido = "Cargando busqueda . . .";
   		var cadena = document.getElementById('pal_clave').value;
		var url ='php/buscador/buscador.inc.php?cadena=' + cadena;	
		retrieveURL(url);		
   }
   
   function mostrarNuevos(){
		txt_contenido = "Cargando nuevos articulos . . .";
   		var url = 'php/secciones/nuevos.inc.php';		
		retrieveURL (url);
   }
   
   function mostrarMasVendidos(){
		txt_contenido = "Cargando articulos mas vendidos . . .";
   		var url = 'php/secciones/mas_vendidos.inc.php';
		retrieveURL(url);
   }
   function validarSSL(){
   		var url_actual = window.location.href;
		var prefijo = url_actual.substring(0, 5);
		if (prefijo === 'https'){
			return true;
		}
		else{
			return false;
		}
   }

   function cambiarProvincias(){
   		var pais = document.getElementById('pais').value;
		empresa = false;
		if (pais == "73"){ // Para Espanya mostramos las provincias
			provinciasURL('php/registro/getProvincias.inc.php?pais=73');
		}
		else{
			document.getElementById('provincias').innerHTML = "<input name=\"provincia\" id=\"provincia\" value=\"\" style=\"border: 1px solid #aabfca;font-family: 'Trebuchet MS';\"/>";
		}
   }
   
   function cambiarProvinciasE(){
   		var pais = document.getElementById('pais_e').value;
		empresa = true;
		if (pais == "73"){ // Para Espanya mostramos las provincias
			provinciasURL('php/registro/getProvincias.inc.php?pais=73');
		}
		else{
			document.getElementById('provincias_e').innerHTML = "<input name=\"provincia_e\" id=\"provincia_e\" value=\"\" style=\"border: 1px solid #aabfca;font-family: 'Trebuchet MS';\"/>";
		}
   }

   function mostrarImagen(cadena){
		imagen_actual = cadena;
		document.getElementById('imagen').src = 'imgs/articulos/' + cadena;
   }
   
   function mostrar_imagen(referencia){
		/*$("overlay").style.visibility = ($("overlay").style.visibility == "visible") ? "hidden" : "visible";
		$("overlay").setOpacity(0.8);*/
		if(imagen_actual == ""){
			$(referencia).style.zIndex = '1000';			
			new Effect.Appear($(referencia));			
			//$(referencia).show();
			imagen_actual = referencia;
		}
		else{
			/*$(imagen_actual).style.zIndex = '1000';*/			
			new Effect.Appear($(imagen_actual));
			//$(imagen_actual).show();
		}
   }
   
   function ocultar_imagen(referencia){
		//$("overlay").style.visibility = ($("overlay").style.visibility == "visible") ? "hidden" : "visible";
		if (imagen_actual == ""){
			$(referencia).style.zIndex = '1000';			
			new Effect.Fade($(referencia));
			$(referencia).hide();
			imagen_actual = referencia;
		}
		else{
			//$(imagen_actual).style.zIndex = '1000';
			new Effect.Fade($(imagen_actual));
			$(imagen_actual).hide();
		}
   }
   
   function modificarSocio(){
		var tipo = document.getElementById('tipo').value;
		
		if (tipo == 'particular'){
			
			// Recogemos los datos del particular
			id = document.getElementById('id_socio');
			nif = document.getElementById('nif');
			nombre = document.getElementById('nombre');
			apellidos = document.getElementById('apellidos');
			telefono = document.getElementById('tfno_contacto');
			fax = document.getElementById('fax');
			pass = document.getElementById('pass');
			
			var error = "OK";
			
			// Validamos
			if (nif.value === "")
			{
				error = "NIF Incorrecto";
				document.getElementById('msg_error').innerHTML= "<div style='border: 1px solid red;color:red'>" + error + "</div>";
				return false;
			}		
			
			// Nombre
			if (!validar_nombre(nombre) || nombre.value === "")
			{
				error = "Nombre Incorrecto";
				document.getElementById('msg_error').innerHTML= "<div style='border: 1px solid red;color:red'>" + error + "</div>";
				return false;
			}
				
			// Apellidos
			if (!validar_nombre(apellidos) || apellidos.value === "")
			{
				error = "Apellidos Incorrectos";
				document.getElementById('msg_error').innerHTML= "<div style='border: 1px solid red;color:red'>" + error + "</div>";
				return false;
			}
			
			// Tel. Contacto
			if(!validar_telefono(telefono) || telefono.value === "")
			{
				error = "Numero de Telefono Incorrecto";
				document.getElementById('msg_error').innerHTML= "<div style='border: 1px solid red;color:red'>" + error + "</div>";
				return false;
			}
					
			// Fax
			if (fax.value != "") {
				if (fax.value != "No Disponible"){
					if (!validar_telefono(fax) || fax.value === "") {
						error = "Numero de FAX incorrecto";
						document.getElementById('msg_error').innerHTML= "<div style='border: 1px solid red;color:red'>" + error + "</div>";
						return false;
					}
				}
			}	

			
			// Mandamos a actualizar los datos
			var url = 'php/usuario/modSocio.inc.php?tipo=particular&id_socio=' + id.value + '&nif=' + nif.value + '&nombre=' + nombre.value + '&apellidos=' + apellidos.value + '&tfno_contacto=' + telefono.value + '&fax=' + fax.value + '&pass=' + pass.value;
			modSocio(url);
			return true;
		}
		else if (tipo == 'empresa'){
			// Recogemos los datos de la empresa
			id = document.getElementById('id_socio');
			cif = document.getElementById('cif');
			nombre = document.getElementById('nombre_e');
			telefono = document.getElementById('tfno_contacto_e');
			fax = document.getElementById('fax_e');
			pass = document.getElementById('pass_e');
			
			var error = "OK";
			
			// Validamos
			if (cif.value === "")
			{
				error = "CIF Incorrecto";
				document.getElementById('msg_error').innerHTML= "<div style='border: 1px solid red;color:red'>" + error + "</div>";
				return false;
			}		
			
			// Nombre
			if (!validar_nombre(nombre) || nombre.value === "")
			{
				error = "Nombre de la Empresa Incorrecto";
				document.getElementById('msg_error').innerHTML= "<div style='border: 1px solid red;color:red'>" + error + "</div>";
				return false;
			}
				
			// Tel. Contacto
			if(!validar_telefono(telefono) || telefono.value === "")
			{
				error = "Numero de Telefono Incorrecto";
				document.getElementById('msg_error').innerHTML= "<div style='border: 1px solid red;color:red'>" + error + "</div>";
				return false;
			}
					
			// Fax
			if (fax.value != "") {
				if (fax.value != "No Disponible"){
					if (!validar_telefono(fax) || fax.value === "") {
						error = "Numero de FAX incorrecto";
						document.getElementById('msg_error').innerHTML= "<div style='border: 1px solid red;color:red'>" + error + "</div>";
						return false;
					}
				}
			}	

			
			// Mandamos a actualizar los datos
			var url = 'php/usuario/modSocio.inc.php?tipo=empresa&id_socio=' + id.value + '&nif=' + cif.value + '&nombre=' + nombre.value + '&tfno_contacto=' + telefono.value + '&fax=' + fax.value + '&pass=' + pass.value;
			alert(url);
			modSocio(url);
			return true;
		}
   }
   
function mostrar_carrito_din(){
	new Effect.BlindDown('carrito_imgs');
}

function ocultar_carrito_din(){
	new Effect.BlindUp('carrito_imgs');
}
