var totprods=0;
var catscol=new Array();
var clasescol=new Array();
var lpedido=new Array();
var cadpedido='';
var interv;
var totalped=0;

var position = 0;
	
	function scrollingDetector(){
	if (document.getElementById('fondo').style.display=='block') {
		if (navigator.appName == "Microsoft Internet Explorer"){
		//alert("You've scrolled to " + document.body.scrollTop + " pixels.");
		document.getElementById('fondo').style.top=document.body.scrollTop;
		} else {
		//else{alert ("You've scrolled to " + window.pageYOffset + " pixels.");}
		document.getElementById('fondo').style.top=window.pageYOffset+'px';
		//alert(window.pageYOffset);
		}
	}
	}
	

function nuevoAjax(){
var xmlhttp=false;
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}

var ajax=nuevoAjax();
var ajax2=nuevoAjax();

function cargadiv(url,capa) {
	contenedor = document.getElementById(capa);
	contenedor.innerHTML='';	
	//alert(url);
	 nocache = Math.random();
	 ajax.open("GET", url+'&nocahe='+nocache.toString(),true);
	 ajax.onreadystatechange=function() {
			
	if (ajax.readyState==4) {
		
		contenedor.innerHTML=ajax.responseText;	
	}
	}
	ajax.send(null);
	}
function cargadiv2(url,capa) {
	contenedor2 = document.getElementById(capa);
	contenedor2.innerHTML='';	
	//alert(url);
	 nocache = Math.random();
	 ajax2.open("GET", url+'&nocahe='+nocache.toString(),true);
	 ajax2.onreadystatechange=function() {
			
	if (ajax2.readyState==4) {
		
		contenedor2.innerHTML=ajax2.responseText;	
	}
	}
	ajax2.send(null);
	}

var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var pedidocookie = GetCookie('tajmahal_pedido');
if (pedidocookie!=null) {
//alert(pedidocookie);
lped=pedidocookie.split('/');
for (i=0; i<(lped.length-1); i++) {
	lped2=lped[i].split(',');
	lpedido.push(new Array(lped2[0],lped2[1],lped2[2]));
}
cadpedido='';
	for (i=0; i<lpedido.length; i++) {
	cadpedido+=lpedido[i].toString()+'/';
	}
	
} else {
lpedido=new Array();
}

 function reservar() {
	 if(confirm("¿Deseas que te avisemos cuando este artículo este disponible?")) {
	 document.formres.action='reservar.php';
	 document.formres.submit();
	 }
	 
 }
	function showdiv(id) {	
		document.getElementById('fondo').style.display='block';
		document.getElementById(id).style.display='block';
	}
	function hidediv(id) {
		document.getElementById('fondo').style.display='none';
		document.getElementById(id).style.display='none';
		
	}
	function imprimirdiv(id)
	{
	
	var bName = navigator.appName;
	var bVer = parseFloat(navigator.appVersion);
	
	var contenido = document.getElementById(id).innerHTML;
	ventana=window.open("/print.php","ventana","width=560");
	ventana.document.open();
	ventana.document.write('<html><head><title>Titulo< \/title><link rel="stylesheet" type="text/css" href="css/css.css"><\/head><body style=\"background-color: #FFFFFF\">');
	
	ventana.document.write(contenido);
	ventana.document.close();
	ventana.print();
	ventana.focus();
	}
	
	function isEmailAddress(theElement)
	{
	var s = theElement.value;
	var filter=/^[A-Za-z][A-Za-z0-9_.-]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	if (filter.test(s))
	return true;
	else
	return false;
	}
	function cancelarped(ind) {
	if(confirm("¿Seguro que quieres cancelar este pedido?")) {
		lpedido=new Array(); cadpedido=''; hidediv('pedido');
	}
	}
	
	function recalc(gastos,op) {
	if (op==1)  { document.getElementById('botonpago').value='PAGAR AHORA'; } else { document.getElementById('botonpago').value='FINALIZAR PEDIDO'; }
	
	subtot=0;
	for (i=0; i<lpedido.length; i++) {
	subtot+=(lpedido[i][1]*(lpedido[i][2]));
	}
	subtot=Math.round(subtot*100)/100;
	iva=Math.round((subtot*descuento)*100)/100;
	tota=subtot+gastos-iva;
	//alert(subtot+' '+iva);
	total=Math.round((tota)*100)/100;
	document.getElementById('gastos2').innerHTML=(gastos)+'€';
	document.getElementById('total2').innerHTML=(total)+'€';
		
	totalped=total;
	}
	
	
	
	function comprobar_disponibilidad(id,ind,cant) {
		cargadiv2('disponible.php?ind='+ind+'&cant='+cant+'&art='+id,'ajax');
	}
	
	function disponible(ok,ind,cant) {
		//alert(ok+' '+ind+' '+cant);
		if (ok>=cant) { cambiarped(ind,cant,1); } else {
		alert('No hay suficiente unidades de este artículo');
		cambiarped(ind,ok,1);
		//mostrarpedido();
		}
		
	}
	
	function cambiarped(ind,cant,force) {
	//alert(ind+' '+cant);
	if ((cant=='') || (cant==0)) { cant=1;	//document.getElementById('cant'+ind).value=cant;
	}
	cant=parseInt(cant);
	lpedido[ind][1]=cant;
	// forzamos el cambio de cantidad en el campo
	if (force==1) { document.getElementById('cant'+ind).value=cant; }
	pvpart=lpedido[ind][1]*lpedido[ind][2];
	document.getElementById('totlin'+ind).innerHTML=Math.round(pvpart*100)/100+'€';
	
	subtot=0;
	for (i=0; i<lpedido.length; i++) {
	subtot+=(lpedido[i][1]*(lpedido[i][2]));
	}
	subtot=Math.round(subtot*100)/100;
	iva=Math.round((subtot*descuento)*100)/100;
	gastos=0;
	tota=subtot+gastos-iva;
	//alert(subtot+' '+iva);
	total=Math.round((tota)*100)/100;
	document.getElementById('subtot').innerHTML=(subtot)+'€';
	document.getElementById('iva').innerHTML=-(iva)+'€';
	document.getElementById('total').innerHTML=(total)+'€';
	cadpedido='';
		for (i=0; i<lpedido.length; i++) {
		cadpedido+=lpedido[i].toString()+'/';
		}
	SetCookie('tajmahal_pedido',cadpedido);
	
	}
	
	function borrped(ind) {
	if(confirm("¿Seguro que quieres eliminar este artículo del pedido?")) {
		lpedido.splice(ind,1);
		cadpedido='';
		for (i=0; i<lpedido.length; i++) {
		cadpedido+=lpedido[i].toString()+'/';
		}
		SetCookie('tajmahal_pedido',cadpedido);
		mostrarpedido();
	}
	}
	function addtocar(id,nom,pvp) {
	scroll(0,0);
	enc=0;
	encind=0;
	//comprobamos si existe
	for (i=0; i<lpedido.length; i++) {
	//alert(lpedido[i][0]);
	if (id==lpedido[i][0]) { enc=1; encind=i; lpedido[i][1]=parseInt(lpedido[i][1])+1; }
	}
	if (enc==0) {
	lpedido.push(new Array(id,1,pvp));
	} else 
	cadpedido='';
	for (i=0; i<lpedido.length; i++) {
	cadpedido+=lpedido[i].toString()+'/';
	}
	SetCookie('tajmahal_pedido',cadpedido);
	//alert(cadpedido);	
	mostrarpedido();
	if (enc==1) { comprobar_disponibilidad(lpedido[encind][0],encind,lpedido[encind][1]); }
	}
	
	function mostrarpedido() {
	showdiv('pedido');
	cadpedido='';
	for (i=0; i<lpedido.length; i++) {
	cadpedido+=lpedido[i].toString()+'/';
	}
	
	if (lpedido.length>0) {
	cargadiv('includes/lpedido.php?ped='+cadpedido,'lpedido');
	document.getElementById('botfinped').style.display='block';
	document.getElementById('botcancped').style.display='block';
	
	} else {
	document.getElementById('botfinped').style.display='none';
	document.getElementById('botcancped').style.display='none';
	document.getElementById('lpedido').innerHTML='No ha incluido ningún artículo en su pedido. <br/>Para añadir artículos pulse el botón AÑADIR AL CARRO en la ficha del artículo';	
	}
	}
	
	function mostrarpedido2() {
	hidediv('pedido');
	showdiv('pedido2');
	cadpedido='';
	for (i=0; i<lpedido.length; i++) {
	cadpedido+=lpedido[i].toString()+'/';
	}
	if (lpedido.length>0) {
	cargadiv('includes/pago.php?ped='+cadpedido,'pedido2');
	} else {
	//document.getElementById('lpedido').innerHTML='No ha incluido ningún artículo en su pedido. <br/>Para añadir artículos pulse el botón AÑADIR AL CARRO en la ficha del artículo';	
	}
	}
	
	function calc() {
		alert('Una vez realizado el pago, NO OLVIDE pulsar CONTINUAR para volver a la web y tramitar su pedido correctamente');
		//document.getElementById('Ds_Merchant_Order').value=id;
		
		document.pago.submit();
	}
	
	function guardarpedido() {
	//alert(url);
	 nocache = Math.random();
	 fp=1;
	 if (document.getElementById('fp1').checked) fp=1;
	 if (document.getElementById('fp2').checked) fp=2;
	 if (document.getElementById('fp3').checked) fp=3;
									   
	 clicad='tot='+totalped+'&fp='+fp+'&dir='+document.getElementById('pedido_direccion').value+'&cp='+document.getElementById('pedido_cp').value+'&pob='+document.getElementById('pedido_poblacion').value+'&prov='+document.getElementById('pedido_provincia').value+'&tel1='+document.getElementById('pedido_telf').value+'&tel2='+document.getElementById('pedido_telf2').value+'&save='+document.getElementById('savedir').checked;
	 //alert(clicad);
	 ajax.open("GET", 'guardarpedido.php?'+clicad+'&ped='+cadpedido+'&nocahe='+nocache.toString(),true);
	 ajax.onreadystatechange=function() {
			
	if (ajax.readyState==4) {
		contenedor = document.getElementById('saveped');
		contenedor.innerHTML=ajax.responseText;	
	}
	}
	
								  
	ajax.send(null);
	}
	
	 function validarregistro() {
	   
	   //alert(document.form1.acept.checked);
	   if (document.formreg.clientes_nombre.value == "") { alert('Falta la siguiente información: Nombre'); document.formreg.clientes_nombre.focus(); return; } 
	   if (document.formreg.clientes_apellidos.value == "") { alert('Falta la siguiente información: Apellidos'); document.formreg.clientes_apellidos.focus(); return; } 
	   if (document.formreg.clientes_email.value == "") { alert('Falta la siguiente información: Email'); document.formreg.clientes_email.focus(); return; } 
	   
	   if (document.formreg.clientes_direccion.value == "") { alert('Falta la siguiente información: Dirección'); document.formreg.clientes_direccion.focus(); return; } 
	   if (document.formreg.clientes_poblacion.value == "") { alert('Falta la siguiente información: Población'); document.formreg.clientes_poblacion.focus(); return; } 
	   if (document.formreg.clientes_cp.value == "") { alert('Falta la siguiente información: Codigo Postal'); document.formreg.clientes_cp.focus(); return; } 
	   if (document.formreg.clientes_provincia.value == "") { alert('Falta la siguiente información: Provincia'); document.formreg.clientes_provincia.focus(); return; } 
	   if (document.formreg.clientes_telf.value == "") { alert('Falta la siguiente información: Teléfono'); document.formreg.clientes_telf.focus(); return; } 
	   
	   if (isEmailAddress(document.formreg.clientes_email)==false) { alert('La dirección de email es incorrecta.'); document.formreg.clientes_email.focus(); return; } 
	   
	   if (document.formreg.clientes_pass.value == "") { alert('Falta la siguiente información: Contraseña'); document.formreg.clientes_pass.focus(); return; } 
	   
	   if (document.formreg.clientes_pass.value!=document.formreg.clientes_pass2.value) { alert('Las contraseñas no coinciden, por favor indique la misma contraseña en los dos campos.'); return; } 
	   
	   if (document.formreg.acept.checked==false) { alert('Debe aceptar las condiciones de uso y privacidad de datos'); return; } 
	   
	   document.formreg.email.value='none@tajmahalcomics.com';
	   document.formreg.action='registrar.php';
	   document.formreg.submit();
   }
   
	function ancho() {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;   
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;    
	  }
	  return myWidth-2;
	}
	
	function alto() {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	  }
	  return myHeight-2;
	}
	

	function resiz(){
			//clearInterval(RESIZtimer);
			//ocultarcapa("fichahtml");
			
			pagetam=ancho()-22;
			contam=pagetam-nottam;
			
			caben=Math.floor(contam/coltam);
			
			document.getElementById("contenido").style.width=(caben*coltam)+'px';
			document.getElementById("pagina").style.width=((caben*coltam)+nottam)+'px';
			
			
			// y actualizamos separadores
			col=0;
			for (i=0; i<totprods; i++) {
				obj=document.getElementById("sep"+i);
				col++;
				if (col==(caben)) { col = 0; obj.style.clear='both'; obj.style.background='#000000'; } else { obj.style.clear='none'; }
				
			
			}
			
			interv=setInterval("scrollingDetector()",1);
 	};
	
	window.onresize = function(){ resiz(); }
