$(document).ready(function(){    
  checkCart();  
});

function ricerca(){
  str = document.getElementById("txtSearch").value;
  str = str.replace(/[^A-Za-z0-9]+/gi," ");
  document.location.href = "/it/6072bb4c4f3721f1d8c1e39cca4ba665/_s"+ str +"/ricerca_nel_sito.html";
}  

function ordina(){
  str = document.getElementById("sl_ordine").value;
  url = (window.location + '').split('/');
  url[5] = url[5].replace(/_p+[1-9]+/gi,"_p1");
  console.log(url);
  if(!url[6] || url[6]=="undefined"){
    newurl = "/"+url[3]+"/"+url[4]+"/"+url[5];      
  }
  else{
    newurl = "/"+url[3]+"/"+url[4]+"/"+url[5]+"/"+url[6];  
  }
  setCookie('lstorder',str);
 window.location.href=newurl;
}

function addToCart(){
  strCart = "";
  for(x = 0;x < document.getElementsByTagName("select").length;x++){
    obj = document.getElementsByTagName("select")[x];
    if(obj.id.substr(0,3)=="add"){
      if(obj.value!="0") strCart += obj.id.replace("add_","") +"#"+ obj.value +";";
    }
  } 
  if(strCart!=""){    
    if(confirm("Aggiungere i prodotti selezionati al carrello?")){
      $.get(
        "script/AggiungiCarrello.php",
        {'add':strCart},
        function(){
          alert("I prodotti sono stati aggiunti al carrello.");
          checkCart();
          for(x = 0;x < document.getElementsByTagName("select").length;x++){
            obj = document.getElementsByTagName("select")[x];
            obj.value = 0;
          }           
        }
      );
    }
  }
  else{
    alert("Seleziona il quantitivo da ordinare");
  }
}

function checkCart(){
  $.ajax({
    url: "/script/new_controllaCarrello.php",
    success: function(data){
      document.getElementById("txtCarrello").innerHTML = data;
    },
    dataType: "html"
  });  
}

function carrello(){
  setCookie("cart","");
  document.location.href="/it/8f3341ebe8117552290e6693fe8ca5ec/il_tuo_carrello_wood_and_house.html";  
}

function changeQta(paIDArticolo,paObj){
  $.get(
    "script/ModificaQta.php",
    {'change':paIDArticolo+"#"+paObj.value},
    function(){      
      cartPar = $("#frmCarrello").serialize();
      setCookie("cart",cartPar);      
      document.location.reload();
    }
  );
}

function deleteCart(paIDArticolo){
  if(confirm("Eliminare il prodotto selezionato dal carrello?")){
    $.get(
      "script/EliminaCarrelloArticolo.php",
      {'delete':paIDArticolo},
      function(){
        cartPar = $("#frmCarrello").serialize();
        setCookie("cart",cartPar);      
        document.location.reload();
      }
    );
  }
}

function doLogin(){
  if(document.frmLogin.txtUser.value == "" || document.frmLogin.txtPass.value == ""){
    alert('Dati inseriti non corretti');
    return false;
  }
  $.ajax({
    type: 'POST',
    url: '/script/doLogin.php',
    data: $('#frmLogin').serialize(),
    success: function(data){
      if(data=="ok"){
        window.location.reload();
      }
      else{
        alert(data);
        document.frmLogin.reset();
      }
    },
    dataType: 'html'
  });
}  

function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
  num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
  cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+''+
  num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + num + ',' + cents);
}

// Cookies

function setCookie(c_name,value,expiredays){
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
  ";path=/";
}

function removeCookie(c_name){
  var exdate=new Date();
  exdate.setDate(exdate.getDate()-60);
  document.cookie = c_name +"='';expires="+ exdate.toGMTString();
}

function readCookie(cookieName){
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function controllaCookie(){
  setCookie("cookieEnabled",1);
  if (readCookie("cookieEnabled")!=1){
    alert("Il tuo browser non ha i cookie abilitati.");
  }
}