//Basket content class

function Basket(root) {

this.basketContent = new Array();
this.basketDisplay = new Array();
this.basketQuantity = new Array();
this.catNumbers = new Array();
this.productSrc = new Array();

this.root = root;
var realBasketQuantity = 0;
this.storing = false;

this.restoreBasket = function() {


 if(document.cookie) {
   
    var storedInformation = document.cookie;

    var basketCnt = get_cookie('basketContent');
    var basketQty = get_cookie('basketQuantity');
    var catNr = get_cookie('catNumbers');
    if(basketQty==""||basketCnt==""||catNr=="") return;
    else {
    this.basketContent  = basketCnt.split("§,");
    this.basketQuantity  = basketQty.split("\,");
    this.catNumbers = catNr.split("\,");

    var productSrc = get_cookie('productSrc');
    
    this.productSrc = productSrc.split("\,");
    
    var basketDspl = get_cookie('basketDisplay');
    this.basketDisplay = basketDspl.split("§,");
    //this.basketDisplay.sort();
    if(this.basketDisplay.length > 0) {
      this.displayRestoredBasket();
    }
    }
 }
} 

this.displayRestoredBasket = function() {

 var i;
 realBasketQuantity = 1;

 this.addProductSignification();

 var tmpArray = this.basketDisplay;
 //tmpArray.reverse();
 var tmpArray2 = this.productSrc;  

 for(i=0;i<this.basketDisplay.length;i++) {
   this.insertToBasketDisplay(tmpArray[i],tmpArray2[i]);
   realBasketQuantity++;
 }  


}

this.addProduct = function(name,product,catId) {
   

   var a = new Date();
   var date = new Date( new Date().getTime()+86400000*30 ).toGMTString();
   var expires = "expires="+date;
   var path = "path=/";
   var index = this.verifyProduct(name,this.basketContent);
   if(index < 0) {
   this.basketContent.push(name+'§');
   this.basketDisplay.unshift(name+'§');
   this.basketQuantity.push(1);
   this.catNumbers.push(catId);
   this.productSrc.unshift(product);
   
   realBasketQuantity ++;
   //this.basketDisplay.sort();

   this.insertToBasketDisplay(name+'§',product);

   document.cookie="basketContent="+escape((this.basketContent).toString())+"#;"+path+";";
   document.cookie="basketDisplay="+escape((this.basketDisplay).toString())+"#;"+path+";";
   //document.cookie="basketContent="+escape((this.basketContent).toString())+"#;"+path+";";
   document.cookie="basketQuantity="+(this.basketQuantity).toString()+"#;"+path+";";
   document.cookie="catNumbers="+(this.catNumbers).toString()+"#;"+path+";";
   document.cookie="productSrc="+(this.productSrc).toString()+"#;"+path+";";
   }

   else {
    var val =  parseInt(this.basketQuantity[index]);
    this.basketQuantity[index] = val+1;
    var qty = val + 1;
    this.increaseQty(name,qty);
    document.cookie="basketQuantity="+(this.basketQuantity).toString()+"#;"+path+";";
   }
   
}


this.increaseQty = function(productName,qty) {

  var index = this.verifyProduct(productName,this.basketDisplay);
  if(document.getElementById("basket").hasChildNodes) {
    document.getElementById("basket").getElementsByTagName('tr')[index].childNodes.item(2).firstChild.nodeValue = qty;
  }
}


this.increaseRemoveQty = function(element,irFlag) {

   
   var a = new Date();
   var date = new Date( new Date().getTime()+86400000*30 ).toGMTString();
   var expires = "expires="+date;
   var path = "path=/"
  var product = element.parentNode.childNodes.item(1).firstChild.nodeValue;
  var index = this.verifyProduct(product,this.basketContent);
  var qty = parseInt(this.basketQuantity[index]);
  if(irFlag) {
    this.basketQuantity[index] = qty+1;
    element.parentNode.childNodes.item(2).firstChild.nodeValue = qty+1;
    document.cookie="basketQuantity="+(this.basketQuantity).toString()+"#;"+path+";";
  }
  else {
    if(qty == 1) {
     document.getElementById('basket').removeChild(element.parentNode); 
     realBasketQuantity--;

     this.adjustArrays(product);
     document.cookie="basketContent="+escape((this.basketContent).toString())+"#;"+path+";";
     document.cookie="basketDisplay="+escape((this.basketDisplay).toString())+"#;"+path+";";
     document.cookie="basketQuantity="+(this.basketQuantity).toString()+"#;"+path+";";
     document.cookie="catNumbers="+(this.catNumbers).toString()+"#;"+path+";";
    }
     else {
      this.basketQuantity[index] = qty-1;
      element.parentNode.childNodes.item(2).firstChild.nodeValue = qty-1;
      document.cookie="basketQuantity="+(this.basketQuantity).toString()+"#;"+expires+";"+path;
     }

  }
  
}


this.adjustArrays = function (product) {

 var index1 = this.verifyProduct(product,this.basketContent);
 var index2 = this.verifyProduct(product,this.basketDisplay);

 var length = this.basketDisplay.length;
 var tmpBegin = this.basketDisplay.slice(0,index2);
 var tmpEnd = this.basketDisplay.slice(index2+1,length);
 
 this.basketDisplay = tmpBegin.concat(tmpEnd); 

 length = this.basketContent.length;
 tmpBegin = this.basketContent.slice(0,index1);
 tmpEnd = this.basketContent.slice(index1+1,length);

 this.basketContent = tmpBegin.concat(tmpEnd);

 tmpBegin = this.basketQuantity.slice(0,index1);
 tmpEnd = this.basketQuantity.slice(index1+1,length);

 this.basketQuantity = tmpBegin.concat(tmpEnd);

 tmpBegin = this.catNumbers.slice(0,index1);
 tmpEnd = this.catNumbers.slice(index1+1,length);

 this.catNumbers = tmpBegin.concat(tmpEnd);

 tmpBegin = this.productSrc.slice(0,index1);
 tmpEnd = this.productSrc.slice(index1+1,length);

 this.productSrc = tmpBegin.concat(tmpEnd);

}

this.verifyProduct = function(productName,array) {

var basketLength = array.length;

var foundIndex = -1;

var i;

	for(i=0;i<basketLength;i++) {
		if(array[i]==productName+'§') {
              foundIndex = i;
              break;
              }

	}
 
	return foundIndex; 

}

this.insertToBasketDisplay = function(productNameIn,product) {

 //var tbody = document.getElementById('basket').childNodes.item(0);

 var productName = productNameIn.substring(0,productNameIn.length-1);

 var basketTr=document.createElement('tr');

 var basketProductImage = document.createElement('img');
     basketProductImage.style.width = "50px";
     basketProductImage.style.height = "40px";
     basketProductImage.src = this.root+"/Products/ProductImages/"+product.substring(0,40)+"_t.jpg";
     
 var basketProductImageLink = document.createElement('a');
     basketProductImageLink.onclick = function() {scale(this.childNodes.item(0));return false;};         

     basketProductImageLink.appendChild(basketProductImage); 

 var basketProductImageTd = document.createElement('td');
     basketProductImageTd.className = "basketImg";
     basketProductImageTd.appendChild(basketProductImageLink);
    
     basketTr.appendChild(basketProductImageTd);

 var basketProductName = document.createElement('td');
     basketProductName.className="basketProduct";
     basketProductName.appendChild(document.createTextNode(productName));
 
     basketTr.appendChild(basketProductName); 

     var qty = this.searchQty(productName);

 var basketProductQty = document.createElement('td');
     basketProductQty.className="basketProductQty";
     basketProductQty.appendChild(document.createTextNode(qty)); 
     basketTr.appendChild(basketProductQty); 

 var basketPlus = document.createElement('td');
     basketPlus.className = "basketAddButton";

 var basketPlusLink = document.createElement('a');
     basketPlusLink.onclick = function() {basket.increaseRemoveQty(this.parentNode,true);};
 

 var basketPlusImg = document.createElement('img');
     basketPlusImg.src = this.root+"/Images/plus.jpg";
     //basketPlusImg.oncklick = function () {changeImages(this.id,this.root+'/Images/addBasket.jpg');};
     
     basketPlusLink.appendChild(basketPlusImg);
     basketPlus.appendChild(basketPlusLink);
     basketTr.appendChild(basketPlus); 

 var basketMinusLink = document.createElement('a');
     basketMinusLink.onclick = function() {basket.increaseRemoveQty(this.parentNode,false);};

 var basketMinus = document.createElement('td');
     basketMinus.className = "basketAddButton";
 
 var basketMinusImg = document.createElement('img');
     basketMinusImg.src = this.root+"/Images/minus.jpg";
     basketMinusLink.appendChild(basketMinusImg);

     basketMinus.appendChild(basketMinusLink);
     basketTr.appendChild(basketMinus);
     
     var index = this.verifyProduct(productName,this.basketDisplay);
     if(!(document.getElementById('basket').hasChildNodes())) {
      document.getElementById('basket').appendChild(basketTr);
     }

     else {
      document.getElementById('basket').insertBefore(basketTr,document.getElementById('basket').firstChild);
     }
     


}

this.searchQty = function (product) {
   
   var index = this.verifyProduct(product,this.basketContent);
   return this.basketQuantity[index];

}

this.addProductSignification = function() {

 var length = this.basketContent.length - 1;
 var i;

 var contentName;
 var displayName; 


 for(i=0;i<length;i++) {
   
  contentName = this.basketContent[i]+'§';
  displayName = this.basketDisplay[i]+'§';
  
  this.basketContent[i] = contentName;
  this.basketDisplay[i] = displayName;   

 }

}

};

