$(document).ready(function (){
	$("img.rolloverimagen").mouseover(function (){
			var newImage = $(this).attr('src').replace(/^(.*?)(\.(?:gif|jpg|png))$/, "$1_over$2");
			$(this).attr('src', newImage);
	});
	$("img.rolloverimagen").mouseout(function (){
			var newImage = $(this).attr('src').replace('_over', '');
			$(this).attr('src', newImage);
	});
	
	$("#ver_aviso").mouseover(function (){
		$("#aviso").fadeIn("fast");
	});
	$("#ver_aviso").mouseout(function (){
		$("#aviso").fadeOut("fast");
	});
	
	
	$("#b_articulos").mouseover(function (){
		$("#articulos").css({ display: "block" });
	});
	$("#b_articulos").mouseout(function (){
		$("#articulos").css({ display: "none" });
	});
	
	$("#b_promociones").mouseover(function (){
		$("#promociones").css({ display: "block" });
	});
	$("#b_promociones").mouseout(function (){
		$("#promociones").css({ display: "none" });
	});
	
	$("#b_ofertas").mouseover(function (){
		$("#ofertas").css({ display: "block" });
	});
	$("#b_ofertas").mouseout(function (){
		$("#ofertas").css({ display: "none" });
	});
	
	$("#b_beneficio").mouseover(function (){
		$("#beneficioclub").css({ display: "block" });
	});
	$("#b_beneficio").mouseout(function (){
		$("#beneficioclub").css({ display: "none" });
	});
	
	$("#b_cuenta").mouseover(function (){
		$("#cuenta").css({ display: "block" });
	});
	$("#b_cuenta").mouseout(function (){
		$("#cuenta").css({ display: "none" });
	});
});


function checkLogin(){
	var f = document.getElementById('formLogin');
	var _user = f.user.value;
	var _pass = f.pass.value;
  $.ajax({
           url: "checkLogin.php",
           type: "POST",
           dataType: "text",
           data: {user: _user, pass: _pass},	           		
           error: function(req, err, obj) {
           	alert('error 2: ' + err);
           },
           success: function(txt) {
	           	if (txt=="ERROR"){
	           		alert("Usuario o contraseña incorrectos");
	           	}else if(txt=="PROFILE"){
	           		alert("El tipo de usuario debe ser 'Usuario'");
	           	}else if(txt=="FIRST"){
	       			self.location = "modifica_datos_prev.html";
	           	}else{
	           		self.location="index.html"
	           	}
	           	return false;
        	}                  
		});	
	return false;
}


function isLogged(url){

	  $.ajax({
	           url: "isLogged.php",
	           type: "POST",
	           dataType: "text",
	           //data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
	           	
		           	if (txt!="logueado"){
		           		self.location = url;
		           		return false;
		           	}else{
		       			alert("Sección privada: debe usted autenticarse para acceder");
		       			return false;
		           	}
	       
	        	}                  
			});
			
		return false;
}

function desconectar(){
	$.ajax({
           url: "cerrarSesion.php",
           type: "POST",
           dataType: "text",
           //data: {user: _user, pass: _pass},	           		
           error: function(req, err, obj) {
           	alert('error : ' + err);
           },
           success: function(txt){
	           	self.location="index.html";
	           	return false;
        	}                  
		});	
	return false;
}

function cambiaCat() {
// This can be one statement but it's easier to read this way:
var ind = document.getElementById('cat_sel').selectedIndex;
if (ind>=0) { 
	var id=document.getElementById('cat_sel').options[ind].value;
	var url="ofertas.html";
	if (id!="-1")
		url = 'ofertas.html?cat_offer='+id;
	self.location=url;
	}
}

function accede_enlace_oferta(id){
	$.ajax({
           url: "accedeOferta.php",
           type: "POST",
           dataType: "text",
           data: {id_offer: id},	           		
           error: function(req, err, obj) {
           	alert('error : ' + err);
           },
           success: function(txt){
	           	return false;
        	}                  
		});	
	return false;
}

/*
window.onload=function () {
	if (document.getElementById('cat_sel')) {
		document.getElementById('cat_sel').onchange=function() { cambiaCat(); }
	}
};*/
