function add(ref,cat,check){
	muestra('blocked');
	muestra('producto');
		var nocache  = parseInt(Math.random()*999999999);
    url='/producto.php?ref='+ref+'&cat='+cat+'&nocache='+nocache;
   	if(check=='si') {url+="&checkout=yes";}
		trae(url,'#producto');
}

function borra(ref,recargar){
	var nocache  = parseInt(Math.random()*999999999);
	var url      = "/borra.php?ref="+ref+"&nocache="+nocache;
	if(recargar!='si')
	 trae(url,'#ajaxcontent');
	else
	
		trae(url+'&reload=yes','#storage');
	
}

function guardaproducto(){
	oculta('producto');
	oculta('blocked');
	document.getElementById('producto').innerHTML='';
}


function trae(url,destino){
 	$.ajax({
   type: "GET",
   url: url,
   dataType: "HTML",
    
   error: function (objeto) {
  		$(destino).prepend("<br /><span class='error'>Error interno. Si el problema continua por favor av&iacute;senos. Pruebe con otro producto<br /><br /><a style='color:blue; ' href='javascript:location.reload()'>Recargar P&aacute;gina</a></span>");
	},
	
	success: function(html){
    	 $(destino).html(html);

   }
 });	
 
 }   
 
 
function checkout(){
ajaxenvio('storage',true,'yes');
}

function ajaxenvio(div,actualiza,reload){
	
	var first=0;
	var unidades= 0;
	var detalles="";
	var numcolores = document.form1.numcolores.value;
	var id_cat   = document.form1.id_cat.value;
	var ref      = document.form1.ref.value;
	
	
	for (i=0;i<numcolores;i++)
		{
			
			color=eval("document.form1.c"+i+".value"); //nombre color
			color=jQuery.trim(color);
			uds=eval("document.form1.c"+i+ref+".value"); //unidades de ese color
			uds=jQuery.trim(uds);
			if (uds=='')
			 { alert("Por favor no deje casillas sin nada escrito"); return;}
			 
			if ( (uds!=0  || actualiza==true) && uds!=''){
				
				tallas=eval("document.form1.t"+i+".value");
				tallas=jQuery.trim(tallas);
				if (!isUnsignedInteger(uds) && uds!='' )
			 	{ 
			 		alert("El nœmero de unidades de cada color debe ser un numero. Gracias!"); 
			 		eval("document.form1.c"+i+ref+".focus()");
			 		return;
			  	}
			
				if(tallas=='')
				{
					alert("ÀEn que talla para el color: "+color+"?"); 
			 		eval("document.form1.t"+i+".focus()");
			 		return;
				}
				
				tallas=tallas.replace(/-/g, "@@");
				tallas=tallas.replace(/_/g, "%")
				if(first!=0)
				  detalles+="_";
				 else
				 	first=1;	
				 detalles +=color+"-"+uds+"-"+tallas;
				unidades = unidades+parseInt(uds); 		
				
			}
		}


   if (unidades==0)  {alert("Para eliminar por completo una referencia pulse en la papelera que aparece junto a cada producto en su carrito. Ha a–adido 0 unidades."); guardaproducto(); return;}
	
	var nocache  = parseInt(Math.random()*999999999);
	var url      = "/actualiza_carrito.php?ref="+ref+"&id_cat="+id_cat+"&unidades="+unidades+"&detalles="+detalles+"&nocache="+nocache;
	//alert(url);
	if (reload=='yes')
	 url += "&reload=yes";
	destino="#"+div; 
	trae(url,destino);
	//por ultimo guardamos el producto en su caja
	guardaproducto();
	//subimos scroll
	$('#carrito').scrollTo('0');
}

function sendsugerencia(formulario){
var email= formulario.email.value;
var consulta= formulario.consulta.value;
var nocache  = parseInt(Math.random()*999999999);

var url = "send.php?email="+email+"&consulta="+consulta+"&nocache="+nocache;

trae(url,'#respuestadistri');

}

function valida(item,que){
errorformu=false;
switch(que){
 case "email":
 		var s = item;
 		
		var filter=/^[A-Za-z0-9\.][A-Za-z0-9_\-\.]*@[A-Za-z0-9_\-]+\.[A-Za-z0-9_.]+[A-Za-z]$/;
		if (s.length == 0) 
			errorformu=true;
		if (!filter.test(s))
			errorformu=true;
			
		break;

 case "telefono":
 		if (item.length <9)
 			errorformu=true;
 		break;
		
		
 case "direccion":
 		if (item.length <7)
 			errorformu=true;
 		break;
 case "cif":
 		if (item.length <9)
 			errorformu=true;
 		break;
default:
 		if (item.length <4)
 			errorformu=true;
 		break;
 		
}

if (errorformu){

 document.getElementById("tr"+que).style.backgroundColor='#9d0d15';
 return false;
}
else
{
document.getElementById("tr"+que).style.backgroundColor='#c4860e';
return true;
}
//oculta(que);
} 

function cambiapais(pais,retri){
window.location.href= "checkout.php?pais="+pais+"&retri="+retri;

}

function ordena(pagina,orderby){
window.location.href=pagina+"?orderby="+orderby;
}
function ir_a(pagina,numero){
window.location.href=pagina+"?pagina="+numero;
}

function tramita(formulario){

	 if (valida(formulario.email.value,'email')  && valida(formulario.direccion.value,'direccion') && 	valida(formulario.ciudad.value,'ciudad')&& valida(formulario.nombre.value,'nombre') && valida(formulario.cif.value,'cif') && valida(formulario.cp.value,'localidad') && valida(formulario.localidad.value,'localidad') && valida(formulario.telefono.value,'telefono') && valida(formulario.passwd.value,'passwd'))
  		formulario.submit();
 else
 	{
 		alert("Por favor Rellena o corrige el formulario de datos. Gracias!"); 
 		return false
 	}
}

function check(formulario){

	 if (valida(formulario.email.value,'email')  && valida(formulario.direccion.value,'direccion') && 	valida(formulario.ciudad.value,'ciudad')&& valida(formulario.nombre.value,'nombre') && valida(formulario.cif.value,'cif') && valida(formulario.cp.value,'localidad') && valida(formulario.localidad.value,'localidad') && valida(formulario.telefono.value,'telefono'))
  		formulario.submit();
 else
 	{
 		alert("Por favor Rellena o corrige el formulario de datos. Gracias!"); 
 		return false
 	}
}


 
function formu(elemento){

	switch(elemento){
		case "tpv":
				oculta('paypal');
				oculta('transferencia');
				muestra('pagoexpress');
				muestra('tarjeta');
				break;
		case "transferencia":
				oculta('paypal');
				oculta('tarjeta');
				muestra('pagoexpress');
				muestra('transferencia');
				break;
		case "paypal": 
				oculta('pagoexpress');
				oculta('tarjeta');
				oculta('transferencia');
				muestra('paypal');
				break;
		default:
			alert("Se ha producido error interno respecto al formulario prueba a recargar la p‡gina. Gracias.");
			break;
	}
document.location="#datospago";
}

 
 
function oculta(item){
document.getElementById(item).style.display='none';
}

function muestra(item){
document.getElementById(item).style.display='block';
}    
 
function limpiarespuesta(div){
	
	document.getElementById(div).innerHTML='';
	oculta(div);

}
 
 function iniciasesion(){
	var email=document.sesionform.email.value;
	var password= document.sesionform.passwd.value;
	if(email.length<4 || password.length<4)
		{ 
			alert("Por favor rellene bien los campos de email y contrase–a. Gracias!.");
			return false;
		}
		
	var nochache = parseInt(Math.random()*99999999);
 	var url="/login.php?email="+email+"&passwd="+password+"&nocache="+nochache;
 	
 	$('#flechasesion').hide('slow');
 	muestra('zonarespuestas');
 	trae(url,'#zonarespuestas');
 	return false;	
}

function recordar(){
oculta('flechasesion');
muestra('recordatorio');
muestra('zonarecuerda');
que=document.getElementById('emailrecordatorio');
que.focus();
}

function recordarinicio(){
muestra('recordatorio');
que=document.getElementById('emailrecordatorio');
que.focus();

}

function iniciasesionINI(){

	var email=document.sesionform.email.value;
	var password= document.sesionform.passwd.value;
	if(email.length<4 || password.length<4)
		{ 
			alert("Por favor rellene bien los campos de email y contrase–a. Gracias!.");
			return false;
		}
		
	var nochache = parseInt(Math.random()*99999999);
 	var url="/login.php?email="+email+"&passwd="+password+"&nocache="+nochache;
 	
 	muestra('respuestasesion');
 	trae(url,'#respuestasesion');
 	return false;	
}


function destruyesesion(){
	
	var nochache = parseInt(Math.random()*99999999);
 	var url="/logout.php?nocache="+nochache;
 	
 	trae(url,'#respuestasesion');
 	
} 

function isUnsignedInteger(s) {
return (s.toString().search(/^[0-9]+$/) == 0);
}


// when the DOM is ready...
$(document).ready(function () {

var displayverano=1;
imagePreview();
/* Listeners */
$("div.clickable").click(
function()
{
    window.location = $(this).attr("url");
});


$('#flecha').click(function(){
 	$.scrollTo('#accesos');
});


$("#loading").ajaxStart(function(){
   $(this).show();
});
$("#loading").ajaxStop(function(){
   $(this).hide();
}); 

});

/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 * modificado para que muestre la imagen segun indica name y no href.
 */
 
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
	
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='/Imagenes/Productos/Preview/"+ this.name +"' alt='Image preview' />"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


// starting the script on page load

