var XMLHTTP

function GetXmlHttpObject()
{
  var XMLHTTP=null;
  try
  {
  // Firefox, Opera 8.0+, Safari
	XMLHTTP=new XMLHttpRequest();
  }
  catch (e)
  {
  // Internet Explorer
  try
	{
	  XMLHTTP=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
	  XMLHTTP=new ActiveXObject("Microsoft.XMLHTTP");
	}
  }
return XMLHTTP;
}
function ApriToolOver(elem)
{
  var elemento = "rieprova_"+elem;
	$("#"+elemento).slideDown("slow");
}
function ChiudiToolOver(elem)
{
  var elemento = "rieprova_"+elem;
	$("#"+elemento).slideUp(function(){
		// and setting that timeout to do the work for us
		window.willhide = setTimeout(function() {
			$("#"+elemento).hide("fast");
		},3550); // with a 750 ms delay
	});
}
function AggiornaCarrello()
{
  //var form = document.forms['JoinForm'];
  //alert(form['NEmail'].value);
  //alert("AJAX ID = "+id+"  N = "+n+"  Quantità = "+quantita);
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
	alert ("Your browser does not support AJAX!");
	return;
  } 
  var url="ajax_carrello.asp";
  //alert(email+'  '+pass);
  url=url+"?tipo=4";
  url=url+"&sid="+Math.random();
  //alert(url);
  //alert(url);
  XMLHTTP.open("GET",url,false);
  XMLHTTP.send(null);
  if(XMLHTTP.status == 200) 
  {
	 return(XMLHTTP.responseText);
  } 
  else 
  {
	alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
  }
}

function NowSession()
{
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
	alert ("Your browser does not support AJAX!");
	return;
  } 
  var url="ajax_carrello.asp";
  //alert(email+'  '+pass);
  url=url+"?tipo=47";
  url=url+"&sid="+Math.random();
  //alert(url);
  //alert(url);
  XMLHTTP.open("GET",url,false);
  XMLHTTP.send(null);
  if(XMLHTTP.status == 200) 
  {
	 return(XMLHTTP.responseText);
  } 
  else 
  {
	alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
  }

}

function ImpostaSessione(padre,idart)
{
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
	alert ("Your browser does not support AJAX!");
	return;
  } 
  var url="ajax_carrello.asp";
  //alert(email+'  '+pass);
  url=url+"?tipo=48&padre="+padre+"&idart="+idart;
  url=url+"&sid="+Math.random();
  //alert(url);
  //alert(url);
  XMLHTTP.open("GET",url,false);
  XMLHTTP.send(null);
  if(XMLHTTP.status == 200) 
  {
	 return(XMLHTTP.responseText);
  } 
  else 
  {
	alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
  }

}
function AggiornaSemafori(idarticolo)
{
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
	alert ("Your browser does not support AJAX!");
	return;
  } 
  var tempiconsegna;
  var numero;
  var semaforo,dimm,inputquantita;
  var url="ajax_carrello.asp";
  
  //alert(email+'  '+pass);
  url=url+"?tipo=43&idart="+idarticolo;
  url=url+"&sid="+Math.random();
  //alert(url);
  XMLHTTP.open("GET",url,false);
  XMLHTTP.send(null);
  if(XMLHTTP.status == 200) 
  {
	numero = XMLHTTP.responseText;
	//alert(numero);
	if(numero>0)
	{
	  semaforo = "divsemaforo_"+numero;
	  tcons = TempiConsegna(idarticolo);
	  dimm = DispImmediata(idarticolo);
	  if(dimm>0)
	  {
		tcons = 1;
	  }
	  //alert(tcons);
	  if(tcons>0)
	  {
		inputquantita = "Quantita_"+numero;
		//alert(inputquantita);		
		document.getElementById(inputquantita).disabled=false;
		//alert(document.getElementById(inputquantita).disabled);	
	  }
	 // alert("N="+numero+"semaforo = "+semaforo+"Tempi="+tcons);
	  document.getElementById(semaforo).innerHTML=CambiaSemaforo(tcons,numero,0);
	}
  } 
  else 
  {
	alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
  }
}
function EliminaCarrelloOverlay(id)
{
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
	alert ("Your browser does not support AJAX!");
	return;
  } 
  var url="ajax_carrello.asp";
  //alert(email+'  '+pass);
  url=url+"?tipo=5&idart="+id;
  url=url+"&sid="+Math.random();
  //alert("ELIMINA RIEPILOGO");
  //alert("QUI");
  XMLHTTP.open("GET",url,false);
  XMLHTTP.send(null);
  if(XMLHTTP.status == 200) 
  {
	if(id!="SVUOTA")
	{
	  AggiornaSemafori(id);
	  AggiornaRiepilogo();
      numero = NumeroArticoliCarrello();
	  //alert(numero);
	  if(numero==0)
	  {
		$(".riepilogocarrello").colorbox.close();  
	  }
	}
	else
	{
	  $(".riepilogocarrello").colorbox.close();
	  document.getElementById("numeroarticoli").innerHTML = AggiornaCarrello();
	}
	
  } 
  else 
  {
	alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
  }
}

function TempiConsegna(idart)
{
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
	alert ("Your browser does not support AJAX!");
	return;
  } 
  var numero,semaforo,url="ajax_carrello.asp";
  
  //alert(email+'  '+pass);
  url=url+"?tipo=44&idart="+idart;
  url=url+"&sid="+Math.random();
  //alert(url);
  XMLHTTP.open("GET",url,false);
  XMLHTTP.send(null);
  if(XMLHTTP.status == 200) 
  {
	return(XMLHTTP.responseText);
  } 
  else 
  {
	alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
  }
}


function AggiornaRiepilogo()
{
  var subtotale;
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
	alert ("Your browser does not support AJAX!");
	return;
  } 
  var url="ajax_carrello.asp";
  //alert(email+'  '+pass);
  url=url+"?tipo=34";
  url=url+"&sid="+Math.random();
  //alert(url);
  XMLHTTP.open("GET",url,false);
  XMLHTTP.send(null);
  if(XMLHTTP.status == 200) 
  {
	if(XMLHTTP.responseText!='0')
	{
	  
	  document.getElementById("contenutocarrello").innerHTML=XMLHTTP.responseText;
	  document.getElementById("numeroarticoli").innerHTML = AggiornaCarrello();
	  subtotale = CalcolaSubtotaleOver();
//	  alert(subtotale);
	  if((isNaN(subtotale)==true)||(subtotale==''))
	  {
		subtotale = 0;	    	   
	  }
	  document.getElementById("divsubtotale").innerHTML="&euro;"+nimporto(subtotale);

	}
	else
	{
	  window.location = "http://www.kikaustore.it/index.asp"
	}
  } 
  else 
  {
	alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
  }
}
function CambiaSemaforo(quale,elem,tipo)
{ 
  //alert("Ciao a tutti "); 
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="ajax.asp";
  //alert(document.getElementById("indspedi").value);
  url=url+"?id=19&quale="+quale+"&n="+elem+"&tipo="+tipo;
  url=url+"&sid="+Math.random();
  //alert(url);
  XMLHTTP.open("GET",url,false);
  XMLHTTP.send(null);
  if(XMLHTTP.status == 200) 
  {
	// alert(XMLHTTP.responseText);
	return(XMLHTTP.responseText);
  } 
  else 
  {
    alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
  }
}

function AggiornaCarrelloOverlay(elem,quantita,tempoconsegna,numvarianti,idarticolo,bloccato,tipo)
{
  var valore,elemento,check,key,semaforo;
  var blocco,tiptquantita;
  elemento = "rieQuantita_"+elem;
  tiptquantita = "rietestoesaurito_"+elem;
  valore = document.getElementById(elemento).value;
 
  //alert(NowSession());
  if(bloccato==1)
  {
    tempoconsegna = 0;
  }
  
  disp = quantita - valore;
  semaforo = "riedivsemaforo_"+elem;
  //alert("Bloccato = "+bloccato+" Disp = "+disp);

  //alert(bloccato+"  "+tempoconsegna+"  "+disp);
  if(tempoconsegna==0)
  {
	tempoconsegna = 30;  
  }
  if(parseInt(disp)>=0)
  {
    //alert("CONSEGNA 1-2 GIORNI LAVORATIVI");
	document.getElementById(semaforo).innerHTML=CambiaSemaforo(1,elem,tipo);
  }
  else
  {
    //alert("CONSEGNA "+tempoconsegna+" GIORNI LAVORATIVI")
	document.getElementById(semaforo).innerHTML=CambiaSemaforo(tempoconsegna,elem,tipo);
  }
  $(function () {
		$('.bubbleInfo').each(function () {
			var distance = 0;
			var time = 50;
			var hideDelay = 200;
	
			var hideDelayTimer = null;
	
			var beingShown = false;
			var shown = false;
			var trigger = $('.trigger', this);
			var info = $('.tooltip', this).css('opacity', 0);
	
	
			$([trigger.get(0), info.get(0)]).mouseover(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				if (beingShown || shown) {
					// don't trigger the animation again
					return;
				} else {
					// reset position of info box
					beingShown = true;
	
					info.css({
						top: 0,
						left: 0,
						display: 'block'
					}).animate({
						top: '-=' + distance + 'px',
						opacity: 1
					}, time, 'swing', function() {
						beingShown = false;
						shown = true;
					});
				}
	
				return false;
			}).mouseout(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					info.animate({
						top: '-=' + distance + 'px',
						opacity: 0
					}, time, 'swing', function () {
						shown = false;
						info.css('display', 'none');
					});
	
				}, hideDelay);
	
				return false;
			});
		});
	});
  //alert(disp+"  "+tempoconsegna);
  }
  
function CalcolaSubtotaleOver()
{
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  //alert("IDCategoria = "+idcategoria+"  Marchio = "+marchio);
  var url="ajax_carrello.asp";
  url=url+"?tipo=7";
  url=url+"&sid="+Math.random();
 // alert(url);
  XMLHTTP.open("GET",url,false);
  XMLHTTP.send(null);
  if(XMLHTTP.status == 200) 
  {
	 //alert(XMLHTTP.responseText);
     return(XMLHTTP.responseText);
  } 
  else 
  {
    alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
  }
}
  
  function CalcolaImportoOverlay(id,n,prezzo,bloccato,quantita,multipli,tipo)
  {
      var importo,quantita,elequa,eleimp,qua,pre,k,subtotale;
      k = 0;
      elequa = "rieQuantita_"+n;
      eleimp = "importo_"+n;
      //alert("id = "+id);
      quantita = document.getElementById(elequa).value;
      
      //alert("quantita = "+quantita+" Prezzo = "+prezzo);
      
      qua = quantita.replace(",",".");
      pre = prezzo.replace(",",".");
      importo = qua * pre;
      document.getElementById(eleimp).innerHTML = "&euro;&nbsp;"+nimporto(importo);
      if(tipo==1)
      { 
        AggiornaQuantita(id,n,pre,bloccato,qua,multipli);
		totsped = CalcolaSubtotaleOver();
		if(parseInt(totsped) > 150)
		{
		  document.getElementById("labelspedizione").innerHTML = "SPEDIZIONE GRATUITA";
		}
		else
		{
		  document.getElementById("labelspedizione").innerHTML = "";
		}
        document.getElementById("divsubtotale").innerHTML = "&euro;&nbsp;"+nimporto(CalcolaSubtotaleOver());
      }
    }



function AggiornaQuantita(id,n,prezzo,bloccato,valore,multipli)
{
  //alert("AJAX ID = "+id+"  N = "+n+"  Quantità = "+quantita);
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
    alert ("Your browser does not support AJAX!");
	return;
  } 

	var elemento,tiptquantita,semaforo;
	elemento = "rieQuantita_"+n;
	importoelemento = "importo_"+n;
	tiptquantita = "rietestoesaurito_"+n;
	semaforo = "riedivsemaforo_"+n;
    quantita = GuardaCarrello(id);
	//alert("Valore ="+valore+" Quantita="+quantita);
	if(valore==0)
	{
	  EliminaCarrello(id);
	}
	else
	{
	  if(bloccato>-1)
	  {
		 if(valore>quantita)
		 {
		   
		    valoremax = parseInt(quantita) + parseInt(DispImmediata(id));
			//alert("1"+quantita+"  "+DispImmediata(id)+" = "+valoremax);
			valore = valoremax;
			quantita = valore;
			document.getElementById(elemento).value = quantita;
			/*qua = quantita.replace(",",".");
			pre = prezzo.replace(",",".");*/
			importo = quantita * prezzo;
			 // alert(bloccato);
			document.getElementById(importoelemento).innerHTML = "&euro;&nbsp;"+nimporto(importo);
			valore = quantita;
			if(quantita==1)
			{
			  descqua = "1 Pezzo"
			}
			else
			{
			  descqua = quantita + " Pezzi"
			}
			document.getElementById(tiptquantita).innerHTML = '<br>La quantit&agrave; massima <br>acquistabile per questo articolo <br>&egrave; '+descqua;
			//alert("ApriOver");
			ApriToolOver(n);
			document.getElementById(semaforo).innerHTML=CambiaSemaforo(1,n,1);
			
		 }
	  }
	  
	  if(multipli>1)
	  {
		ris = valore / multipli;
		g = Math.floor(ris);
		f = valore % multipli
		if (f>0)
		{
		  //alert("Ris = "+ris+"  G =  "+g);
		  if(g>0)
		  {
			s = g + 1
			k = multipli * (s)
		  }
		  else
		  {
			k = multipli
		  }
		
		  document.getElementById(elemento).value = k;
		  valore = k;

		  importo = k * prezzo;
			 // alert(bloccato);
		  document.getElementById(importoelemento).innerHTML = "&euro;&nbsp;"+nimporto(importo); 
		  document.getElementById(tiptquantita).innerHTML = '<br>Articolo acquistabile in quantit&agrave; multiple di '+multipli+' pezzi.<br>Sono stati aggiunti al carrello <br>'+k+' pezzi';
		  ApriToolOver(n);
		}
	}
    var url="ajax_carrello.asp";
    url=url+"?tipo=35&idart="+id+"&quantita="+valore;
    url=url+"&sid="+Math.random();
   // alert(url);
    XMLHTTP.open("GET",url,false);
    XMLHTTP.send(null);
    if(XMLHTTP.status == 200) 
    {
	  
    } 
    else 
    {
      alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
    }
  }	
}
function GuardaCarrello(idart)
{
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
    alert ("Your browser does not support AJAX!");
	return;
  } 
   var url="ajax_carrello.asp";
	url=url+"?tipo=36&idart="+idart;
	url=url+"&sid="+Math.random();
   // alert(url);
	XMLHTTP.open("GET",url,false);
	XMLHTTP.send(null);
	if(XMLHTTP.status == 200) 
	{
      return(XMLHTTP.responseText);
	} 
	else 
	{
	  alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
	}	
}
    function nimporto(prezzo)
    {
        num=Math.round(parseFloat(prezzo)*100)/100;
        num=""+num;
        if(num.indexOf(".") == -1)
        {
            num=num+".00";
        }
        else
        {
            var splitted=num.split(".");
            if(splitted[1].length < 2)splitted[1]=splitted[1]+"0";
            num=splitted[0]+"."+splitted[1];
        }
        num = num.replace(".",",");
        return num;
        
    }
function ApriRiepilogoCarrello2(padre,idart)
{
  var numero;
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="ajax_carrello.asp";
  url=url+"?tipo=34";
  url=url+"&sid="+Math.random();
  //alert("OKRiepilogo");
  ImpostaSessione(padre,idart);
  //alert("Now "+NowSession());
  numero = NumeroArticoliCarrello();
  //alert(numero);
 if(numero>0)
  {
	
	XMLHTTP.open("GET",url,false);
    XMLHTTP.send(null);
    if(XMLHTTP.status==200)
    {
	  //alert(XMLHTTP.responseText);
	  document.getElementById("riepilogocarrello").innerHTML = XMLHTTP.responseText;
	  WinCarrello();
      //INSERISCI RICHIESTA	  
    } 
    else 
    {
      alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
    }
  }
}

function ApriRiepilogoCarrello()
{
  var numero;
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="ajax_carrello.asp";
  url=url+"?tipo=34";
  url=url+"&sid="+Math.random();
  //alert("OKRiepilogo");
	
  numero = NumeroArticoliCarrello();
  //alert(numero);
 if(numero>0)
  {
	
	XMLHTTP.open("GET",url,false);
    XMLHTTP.send(null);
    if(XMLHTTP.status==200)
    {
	  //alert(XMLHTTP.responseText);
	  document.getElementById("riepilogocarrello").innerHTML = XMLHTTP.responseText;
	  WinCarrello();
      //INSERISCI RICHIESTA	  
    } 
    else 
    {
      alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
    }
  }
}

function NumeroArticoliCarrello()
{
  XMLHTTP=GetXmlHttpObject();
  if (XMLHTTP==null)
  {
    alert ("Your browser does not support AJAX!");
	return;
  } 
  var url="ajax_carrello.asp";
  //alert(email+'  '+pass);
  url=url+"?tipo=4";
  url=url+"&sid="+Math.random();
  //alert(url);
  XMLHTTP.open("GET",url,false);
  XMLHTTP.send(null);
  if(XMLHTTP.status == 200) 
  {
     return(XMLHTTP.responseText);
  } 
  else 
  {
    alert("Si è verificato un errore: \n" + XMLHTTP.status + " - " + XMLHTTP.statusText)
  }
}
