// Manages function on product pages
var ajaxCategory='';



function showCart(){
  //changes document location to cart]
  //document.location.href="http://lanatura.mg/shoppingBag.php?categories="+ajaxCategory;
  document.location.href="http://www.lanatura.com/shoppingBag.php?categories="+ajaxCategory;
  }
  
  
  
function showCategory(category){
  //ajax function to dynamically replace contents of productSummary div
  //create Ajax object
  var ajaxObj=new Ajax();
  //reference object handle
  var myAjax=ajaxObj.handle;
  
  //setup callback function
    myAjax.onreadystatechange=function(){
      if(myAjax.readyState==4 && myAjax.status==200){
        document.getElementById("productSummary").innerHTML=myAjax.responseText;
        updateButtons(category);
        //alert(myAjax.responseText);
      }
    }
  
  //open page and send request
    myAjax.open("POST","ajaxProductSummary.php",true);
    
    //build params string
    var params="category="+encodeURIComponent(category);
    //alert(params);
      myAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      myAjax.setRequestHeader("Content-length", params.length);
      myAjax.setRequestHeader("Connection", "close");
      myAjax.send(params);
            
  }
  
function addToBag(productId){
  //will add product to shopping basket
  //get quantity of product to add
  var quantity=document.getElementById('qty'+productId).value;
  var otherData=(document.getElementById('details'+productId).value).split('|');
  var unitPrice=otherData[0];
  var offerCode=otherData[1];
  
  if(isNaN(parseInt(quantity))){
    alert("Please enter a numerical value for the item quantity");
    return;
    }
    
  
  //alert(productId+":"+ajaxCategory);
  //ajax function to dynamically add an item to the shopping bag
  //create Ajax object
  var ajaxObj=new Ajax();
  //reference object handle
  var myAjax=ajaxObj.handle;
  
  //setup callback function
    myAjax.onreadystatechange=function(){
      if(myAjax.readyState==4 && myAjax.status==200){
        showCart();
        
        
      }
    }
  
  //open page and send request
    myAjax.open("POST","ajaxShoppingBag.php",true);
    
    //build params string
    var params="cmd=addItem&productId="+encodeURIComponent(productId)+"&quantity="+encodeURIComponent(quantity)+"&unitPrice="+encodeURIComponent(unitPrice)+"&offerCode="+encodeURIComponent(offerCode);
    //alert(params);
      myAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      myAjax.setRequestHeader("Content-length", params.length);
      myAjax.setRequestHeader("Connection", "close");
      myAjax.send(params);
            
}

function updateQuantity(productId,newQuantity,unitPrice,invoiceId,offerCode){
  //calls ajax funtion to update quantity of item in cart
  
  var ajaxObj=new Ajax();
  //reference object handle
  var myAjax=ajaxObj.handle;
  
  //setup callback function
    myAjax.onreadystatechange=function(){
      if(myAjax.readyState==4 && myAjax.status==200){
        showCart();
        
        
      }
    }
  
  //open page and send request
    myAjax.open("POST","ajaxShoppingBag.php",true);
    
    //build params string
    var params="cmd=updateQuantity&productId="+encodeURIComponent(productId)+"&newQuantity="+encodeURIComponent(newQuantity)+"&invoiceId="+encodeURIComponent(invoiceId)+"&unitPrice="+encodeURIComponent(unitPrice)+"&offerCode="+encodeURIComponent(offerCode);
      //alert(params);
      myAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      myAjax.setRequestHeader("Content-length", params.length);
      myAjax.setRequestHeader("Connection", "close");
      myAjax.send(params);

}




  
  
  

  
  
