
function Basket() {
 	var nextyear = new Date();
 	nextyear.setFullYear(nextyear.getFullYear()+ 1);
 	this.path = '/';
 	this.domain = document.domain;
 	this.info = "path="+this.path+";domain="+this.domain;
	this.cart = "cart";
	//this.javacart = "javacart";
	this.lastitem = "";
	this.islastchange = true;
	this.form ="";
	this.currency = "";
};

	/**
	 *	a item is one element separated by 'cart=id|skew|price|qty#'  skew,skew,...
	 *	@param string item + skew (skew list of the options separated by _)
	 *	@param double price
	 *	@param integer quantity
	 *	@param	object	form
	 */
	Basket.prototype.addItem = function(id,qnty,skew) {

	 	if(!this.checkQuantity(qnty)) return true;

	 	//if(!confirm("Vous allez ajouter cet article à votre panier. Vous pourrez à tout moment le supprimer en vous rendant sur Mon panier. Souhaitez-vous continuer ?")) {
	 	//	this.islastchange = false;
	 	//	return false;
	 	//}

	 	//this.lastitem = Itemqty + " * ";

	 	// *** test to know if the product exists ever
	 	// *** Reading cookie
	 	if(this.exists(id,skew)) {
	 		//items = this.UpdateItem(Item,Itemprice,Itemqty);
        } else {
            items = getCookie(this.cart);
			if(items==null) items ='';
			if(items!='')  items+='#';
            items += id+ "|" + skew + "|" + qnty;

		}
        setCookie(this.cart,items,null,'/',document.domain);
 	};

	Basket.prototype.exists = function(id,skew) {
        return false;
    }

	/**
	 *	Check if the item is ever in the basket
	 */
	Basket.prototype.Check = function(article) {
		var elements = getCookie(this.cart);
		if(elements==null) return false;
		items = elements.split("#");
		for(var i=0;i<items.length;i++){
			if(article == items[i].substring(0,items[i].lastIndexOf("|"))) {
			    return true;
            }
		}
		return false;
	};

  	/**
  	 *	Change the quantity
  	 *	@param string Item
  	 *	@param double cost
  	 *	@param	integer quantity
  	 */
  	Basket.prototype.UpdateItem = function(Item, Price, Qty) {
  		var buffer="";
  		cart = getCookie(this.cart);
  		items = cart.split("#");
  		for(var i=0;i < items.length;i++) {
    			pos = items[i].indexOf("|");
    			pos = items[i].indexOf("|",pos+1);
    			if(items[i].substring(0,pos)==Item) {	// we check the item and the skew
    	  			posq = items[i].substring(items[i].lastIndexOf("|")+1);
    	  			qty = parseInt(posq)+parseInt(Qty);  // New quantity
      				items[i]= items[i].substring(0,pos) + "|" + Price + "|" + qty;
      				//alert("nouvrqu res"+items[i]);
      			}
      			buffer += items[i] + "#";
  		}
  		buffer = buffer.substring(0,buffer.length -1);
  		return buffer;
  	};

 	/**
 	 *	Check if the quantity if qty>0
 	 *	Customize with the name of the frames
 	 */
 	Basket.prototype.checkQuantity = function(qnty) {
 		if(qnty>0 && isNaN(qnty)==false) {
 			this.islastchange = true;
 			return true;
 		}
 		this.islastchange = false;
 		alert("D‚sol‚, vous devez rentrer une quantit‚ positive");
 		return false;
 	};


	/**
	 *	a item is one element separated by 'cart=item|price|qty#'  skew,skew,...
	 *	Contrary to the cookie cart in the skew we put the name of the product and a formated skew
	 *	We don't use the id of the product - So it's possible to have some errors in some cases
	 *	@param string item + skew (skew list of the options separated by _)
	 *	@param double price
	 *	@param integer quantity
	 *	@param	object	form
	 */
	Basket.prototype.SetDisplayJavaCart = function (Form) {
		var item = Form.itemshrt.value;
		if(Form.unitdesc!=null) {
			if(Form.unitdesc.type=='select-one')
				item += " (" + Form.unitdesc.options[Form.unitdesc.selectedIndex].text+")";
			else
				item += " " + Form.unitdesc.value;
		}
		var qnty = 1
		//Form.qty[Form.qty.selectedIndex].value;
		var cost = Form.unitcost.value;
		if(this.CheckJavaCart(item +"|"+ cost))  { // item is ever in the basket
	 		items = this.UpdateItemJavaCart(item,cost,qnty);
		}else {					// new item
			items = this.getCookieVal(this.javacart);
			if(items!="") items+="#";
			if(items==null) items="";
			items += item + "|" + cost + "|" + qnty;
		}
		document.cookie= this.javacart + "=" + escape(items)+";" +this.info;
		var values = this.GetArray(items);
		//this.PrintBasket(values['qnty'],values['desc'],values['cost']);
 	};

	Basket.prototype.ShowBasket = function() {
		var items = getCookie(this.javacart);
		//alert(items);
		if(items==null || items=="") {
			text = 'Aucun article';
			window.layerWrite('basketjava',null,text);
		}else {
			var item = items.split('+');
			items = item.join(' ');
			document.cookie= this.javacart + "=" + escape(items)+";" +this.info;
			//alert(items);
			var values = this.GetArray(items);
			this.PrintBasket(values['qnty'],values['desc'],values['cost']);
		}
	};

	Basket.prototype.GetArray = function (strcart) {
		items = strcart.split("#");
		var values = new Array();
		values['qnty'] = new Array();
		values['desc'] = new Array();
		values['cost'] = new Array();

		for(var i=0;i<items.length;i++) {
			var elements=items[i].split("|");
			values['qnty'][i] = elements[2];
			values['cost'][i] = elements[1];
			values['desc'][i] = elements[0];
		}
		return values;
	};

	Basket.prototype.PrintBasket = function(qnty,desc,cost) {

		var textStart = "<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"0\">\n";
		textStart += "<tr><td class=\"text\" width=\"8%\"><b>qt&eacute;.</b></td><td class=\"textl\" width=\"92%\"><b>article</b></td></tr>\n";

		var textItems  = "";

		for(var i=0; i < qnty.length;i++) {
			textItems+= "<tr><td class=\"text\" valign=\"top\">"+qnty[i]+"</td><td class=\"textl\" valign=\"top\">"+desc[i]+"</td></tr><tr><td class=\"text\" valign=\"top\" colspan=\"3\"><center><img src=\"{DIR_CHARTE}dotsh120.gif\"/ border=\"0\"></center></td></tr>";
		}

		var text2 = "<tr><td colspan=\"2\" class=\"text\"><br><b>Sous-total :&nbsp;&nbsp;</b>";
		var textAmount = this.GetAmountBasket(qnty,cost);
		if(this.GetCurrencySymbol()=="right") {
			textAmount+= this.currency;
		}else {
			textAmount = this.currency+textAmount;
		}
		var textEnd = " </td></tr>\n";
		textEnd+= "<tr><td colspan=\"2\"><img src=\"{DIR_CHARTE}pix.gif\"/ width=\"1\" height=\"8\" border=\"0\"></td></tr><tr><td colspan=\"2\" class=\"text\"><center><a class=\"link\" href=\"{DIR_PROCESS}basket.php\">Je commande</a></center></td></tr>";
		var text = textStart + textItems + text2 + textAmount + textEnd;
		if(ns4) {
			text = textAmount;
			//alert('houra');
		}
		window.layerWrite('basketjava',null,text);
	};



	/**
	 *	@param	array quantity array[index]=quantity
	 *	@param	array cost array[index]=cost
	 */
	Basket.prototype.GetAmountBasket = function(quantity,cost) {
		var amount = 0;
		for(var i=0;i < quantity.length;i++) {
			//alert(parseInt(quantity[i])+'cour'+cost[i])
			amount += parseInt(quantity[i]) * cost[i];
		}
 		if(amount=="") amount = 0;
 		amount = this.CERound(amount,2);
		return amount;
	};

	Basket.prototype.GetCurrencySymbol = function() {
		var symbol = getCookie("curr");
		if(symbol==null) {
			this.currency = "{currency}";
			return "right";

		} else {
			var values = symbol.split("|");
			this.currency = values[0];
			return values[1];
		}
	}

	Basket.prototype.CERound = function(numbr,dec) {
 		// numbr = Math.ceil(numbr);
 		 var s = "" + numbr;
 		 pos = s.indexOf('.');
 		if(pos==-1) return numbr;
 		int1 = s.substr(0,pos);
 		dec1 = s.substr(pos+1,dec);
 		if(dec1.length<dec) dec1 = dec1 + '0';
 		result = int1 + "." + dec1;
 		return result;
 	};

	Basket.prototype.Reset = function () {
 	// this function will be assured by the server
 		return;
	};

	Basket.prototype.GetCurrency = function (nb) {
		var s = "" + nb;
		pos = s.indexOf('.');
		if(pos==-1) pos=s.length;
		int1 = s.substr(0,pos);
		dec1 = s.substr(pos+1,2);
		if(dec1=="") dec1="00";
		result = int1 + "." + dec1;
 		return this.currencySymbol + result;
 	};

	Basket.prototype.Destroy = function () {
 		return;
	};


 	/**
 	 *	This function is called just when we want to amend the basket. the customer comes from 'checkout'
 	 */
 	Basket.prototype.Mode = function (pointer) {
 		document.cookie = "from=amend;pointer=";
 		document.cookie = "pointer="+pointer;
 		return true;
	};


	Basket.prototype.CheckJavaCart = function(article) {
		var elements = getCookie(this.javacart)
		if(elements==null) return false;
		items = elements.split("#");
		for(var i=0;i<items.length;i++) {
			if(article==items[i].substring(0,items[i].lastIndexOf("|"))){
				 return true;
			}
		}
		return false;
	};

  	/**
  	 *	Update the basket for the display in dhtml
  	 *	@param  string name and skew of the product
  	 *	@param  double cost
  	 *	@param	integer qnty
  	 */
  	Basket.prototype.UpdateItemJavaCart = function(item, cost, qnty) {
  		var buffer="";
  		cart = getCookie(this.javacart);
  		items = cart.split("#");
  		for(var i=0;i<items.length;i++) {
			pos = items[i].indexOf("|");
			pos = items[i].indexOf("|",pos+1);;
			if(items[i].substring(0,pos)==(item+'|'+cost)) {
	  			posq = items[i].substring(items[i].lastIndexOf("|")+1);
	  			qty = parseInt(posq)+parseInt(qnty);  // New quantity
  				items[i]= items[i].substring(0,pos) + "|" + qty;
  			}
  			buffer += items[i] + "#";
  		}
  		buffer = buffer.substring(0,buffer.length -1);
		return buffer;
  	};

	Basket.prototype.Setup = function() {
		this.deleteCookie(this.cart,this.path);
		this.deleteCookie(this.javacart,this.path);
		this.deleteCookie(this.amountBasket,this.path);
	};

	// @param offset: name of the variable in the cookie
	// @result : the value of the variable in the cookie
	// @date 12/01/00
	// @author Olivier Ricard
	Basket.prototype.getCookieVal = function (offset)
	{
		var posstr=document.cookie.indexOf (offset);
		if (posstr==-1) 	return "";
		var endstr = document.cookie.indexOf(";",posstr);
		if(endstr==-1) endstr=document.cookie.length;
			return unescape(document.cookie.substring(posstr+offset.length+1, endstr));
	};

	/**
	 *	Used in the checkout template for the amend  and the removal
	 *	To Modify
	 *	@param	object form
	 *	@param	string name of the object
	 */
	Basket.prototype.LastChangeCheckout = function (Form,Obj) {
		pos=Obj.indexOf("_");
		var k;
		if( pos!= -1){
			buton   = Obj.substring(0,pos);
			pointer = Obj.substring(pos+1);
			for(var i=0;i>Form.length;i++) {
				if(qty = "qnty_"+ pointer      == Form.elements[i].name) k=i;
				if(qty = "itemname_"+ pointer == Form.elements[i].name) j=i;
			}
			if(Form.elements[k].value>0 && isNaN(Form.elements[k].value)==false){
				itemname = Form.elements[k].value;
				itemname += "" + Form.elements[j].value;	//desc
				if(pointer!="" && buton=="remove") 	this.LastChange(itemname,1);
				if(pointer!="" && buton=="update") 	this.LastChange(itemname,2);
			} else {
				if(Form.elements[k].value==0) {		// *** We remove the item
					itemname = Form.elements[k].value;
					if(pointer!="" && buton=="update") 	this.LastChange(itemname,1);
				} else {
					Form.elements[k].value = Form.elements[k].defaultValue;
					return false;
				}
			}
		} else {
			buton = Obj;
			if(buton =="reset")   			this.LastChange("",3);
		}
		return true;
		//alert(Form.elements[i].name);
		//alert(Form.elements[i].value);
	};


	// Give the last product
	// Not used still
	// To customize with the name of the html file
	// Id : 0 <=> Added
	// Id : 1 <=> Deleted
	// Id : 2 <=> Amended
	Basket.prototype.LastChange = function(Itemdesc,Id) {
		if(this.islastchange==false) return false;
		parent.lastItemChanged = this.lastitem + Itemdesc;
		if(typeof Id == "undefined") Id=0;
		switch(Id){
			case 0: parent.lastItemChanged += " added";		break;
			case 1: parent.lastItemChanged += " removed";		break;
			case 2: parent.lastItemChanged += " updated";		break;
			case 3: parent.lastItemChanged  = " Empty Basket";	break;
			default:parent.lastItemChanged  = "";			break;
		}
		//window.open('shopping/stat.htm','baskstat');
		//top.frames[2].location.href = 'shopping/stat.htm';
		//parent.frames['baskstat'].location.href = 'shopping/stat.htm';
		//alert("yres");
		//alert(parent.lastItemChanged);
		//alert(top.frames[2].levelbasket.value);
		//top.frames[2].levelbasket.value = 'test';
		//top.frames[2].setval("testsz");
		//top.frames[2].form1.levelbasket.value="yeseee";
		//top.frames[1].form1.levelbasket.value = parent.lastItemChanged;
		return true;
	};

	/* Added by hauser to only check if or not empty */
	Basket.prototype.notempty = function() {
		items = getCookie(this.cart);
        if(items==null) return false;
		return true;
	}
