// <!-- 
Jorr_shopping_item = Class.create();
Jorr_shopping_item.prototype = {
	initialize:function(id,ammount,price,title){
		this.id 	= id;
		this.ammount 	= ammount;
		this.title 	= title;
		this.price 	= price;
	},
	get_ammount:function(){
		return this.ammount;
	},
	get_price:function(){
		return this.price;
	},
	get_title:function(){
		return this.title;
	},
	get_price_total:function(){
		return this.price * this.ammount
	},
	get_id:function(){
		return this.id;
	}
}
Jorr_babylon= Class.create();
Jorr_babylon.prototype= {
	/* CONTRUCTOR */
	initialize: function (name,kassaurl,elementid) {
			this.name = name;
			// this.sessionmanager = new Jorr_custom_sessions({shelfLife: 2,connectionurl:connectorurl});
			this.elementid = elementid;
			this.kassaurl = kassaurl;
			// this.sessionmanager.check(); // Create a session
			// this.id = sessionmanager.getSID(); // winkelwagen id
			this.items = [];
			// this.load();
	},
	set_ammount:function(id,ammount){
	        if(ammount==0){
			this.delete_item(id);
		}else{
			index = this.search_item(id);
			this.items[index].ammount = this.checkammount(ammount);
		}
		this.drawinterface();
	},
	set_price:function(id,price){
		index = this.search_item(id);
		this.items[index].price = this.checkprice(price);
	},
	set_title:function(id,title){
		index = this.search_item(id);
		this.items[index].title = this.checktitle(title);
	},
	get_ammount:function(id){
		index = this.search_item(id);
		return this.items[index].ammount;
	},
	get_price:function(id){
		index = this.search_item(id);
		return this.items[index].price;
	},
	get_title:function(id){
		index = this.search_item(id);
		return this.items[index].title;
	},
	get_price_total:function(id){
		index = this.search_item(id);
		return this.items[index].price * this.items[index].ammount;
	},
	get_id:function(id){
		index = this.search_item(id);
		return this.items[index].id;
	},
	/* PUBLIC */
	dosafe:function(){
		jor_sessionmanager.safe("basket",this.items,this.name+".stored");
	},
	/* PRIVATE */
	load: function () {
		// load basket
		jor_sessionmanager.getSID();
		if(jor_sessionmanager.id==null){
			alert("NO SHOPPINGCART AVAILABLE:"+this.id);
			return false;
		}
		jor_sessionmanager.load("basket",this.name+".loaded");
	},
	/* PUBLIC */
	add_item: function (id,ammount,price,title){
		if(
			(this.checkitem_id(id)==false)||
			(this.checkammount(ammount)==false)||
			(this.checktitle(title)==false)||
			(this.checkprice(price)==false)
			){
			alert("No valid item:");
			return false;
		}
		index = this.search_item(id);
		if(index==-1){ //new item
			this.items[this.items.length] = new Jorr_shopping_item(id,ammount,price,title);
		}else{
			// this.items[index].set_ammount(this.items[index].get_ammount()+ammount);
			this.items[index] = new Jorr_shopping_item(id,ammount,price,title);
		}
		
		this.drawinterface();
	
	},
	/* PRIVATE */
	add_item_silent: function (id,ammount,price,title){
		if(
			(this.checkitem_id(id)==false)||
			(this.checkammount(ammount)==false)||
			(this.checktitle(title)==false)||
			(this.checkprice(price)==false)
			){
			alert("No valid item:");
			return false;
		}
		index = this.search_item(id);
		if(index==-1){ //new item
			this.items[this.items.length] = new Jorr_shopping_item(id,ammount,price,title);
		}else{
			// this.items[index].set_ammount(this.items[index].get_ammount()+ammount);
			this.items[index] = new Jorr_shopping_item(id,ammount,price,title);
		}
	},
	/* PUBLIC */
	delete_item: function (id){
		
		index = this.search_item(id);
		if(index>-1){
			this.items[index] = "";
			this.clear_itemlist();
		}
	},
	/* PUBLIC */
	clear: function (id){
			this.items = [];
	},
	clear_itemlist: function (){
		newitems = [];
		for(i=0;i<this.items.length;i++){
			if(typeof this.items[i]=='object'){
				newitems[newitems.length] = this.items[i];
			}
		}
		this.items = newitems;
	},
	total: function (){
		total = 0;
		for(i=0;i<this.items.length;i++){
			if(typeof this.items[i]=='object'){
				total += this.items[i].get_price_total();
			}
		}
		return total;
	},
	totalammount: function (){
		total = 0;
		for(i=0;i<this.items.length;i++){
			if(typeof this.items[i]=='object'){
				total += this.items[i].get_ammount();
			}
		}
		return total;
	},
	totalstring: function (){
		total = 0;
		for(i=0;i<this.items.length;i++){
			if(typeof this.items[i]=='object'){
				total += this.items[i].get_price_total();
			}
		}
		return this.format_currency(total);
	},
	count_items: function (){
		return this.items.length;
	},
	search_item: function (id){
		for(i=0;i<this.items.length;i++){
			if(this.items[i].get_id()==id){
				return i;
			}
		}
		return -1;
	},
	get_item: function (id){
		index = this.search_item(id);
		if(index>-1){
			return this.items[index] ;
		}else{
			return false;
		}
	},
	/* PUBLIC */
	gotoKassa: function (){
		var date = new Date();
		var t = date.getTime();
		var thisloc = encodeURIComponent(document.location);
		document.location = this.kassaurl+"?t="+t+"&sid="+jor_sessionmanager.id+"&callback="+thisloc+"&status=status_1"; 	
	},
	checkprice:function(price){
		price = parseFloat(price);
		if(isNaN(price)){
			alert("Not a number (price):"+price);
			return false;
		}
		return price;
	},
	checkammount:function(ammount){
		ammount = parseInt(ammount);
		if(isNaN(ammount)){
			alert("Not a number (ammount):"+ammount);
			return false;
		}
		return ammount;
	},
	checktitle:function(title){
		title = title.strip();
		if(title==""){
			alert("No title givven:"+title);
			return false;
		}
		return title;
	},
	checkitem_id:function(id){
		if((id=="")||(!id)){
			alert("No itemid :"+id);
			return false;
		}
		return id;
	},
	format_currency: function(price){
		price = Math.round(price*100)/100.0;
		s = ""+price;
		ar = s.split('.');
		if(ar.length==1){
			s = s+",00";
		return s;
		}else if(ar.length==2){
			n2 = ar[1].split('');
		
			if(n2.length==2){
				return ar[0]+","+ar[1];
			}else if(n2.length==1){
				return ar[0]+","+ar[1]+'0';
			}
		}
	},
	loaded:function(str){
		// Returns json object
		// alert('LOADED'+str);
		if(str.isJSON()){
			// alert(str);
			itms = 	str.evalJSON();
			// alert("Loop"+itms.length);
			for(i=0;i<itms.length;i++){
				// id,ammount,price,price
				// alert("ITEM>:"+i+itms[i]);
				// alert(i+"=index:"+itms[i].title);
				this.add_item_silent(itms[i].id,itms[i].ammount,itms[i].price,itms[i].title);
			}
			
			// this.drawinterface();
		}else{
			alert('LOAD FAILURE');
		}
		// alert('DATA LOADED, update interface');
		this.drawinterface();
	},
	stored:function(str){
		// returns id
		// alert("MANDJE STORED");
		if((str=="failure")||(str.length!=36)){
			      alert("ERROR:"+id);
			      return false;
		}
		// if(this.id!=str){
		//	alert('WRONG SESSION');
		// }
		// alert('DATA STORED');
		
	},
	drawinterface:function(){
		basket = $(this.elementid);
		
		if(!basket){
			alert('MISSING HTML ID:'+this.elementid);
			return false;	
		}
		basket.update(); // EMPTY ELEMENT
		// var theText1 = document.createTextNode('This is a sample of some ');
		this.dosafe();
		if(this.items.length<1){
			basket.setStyle({display:'none'})
			return false;
		}else{
			
			basket.setStyle({display:'block'})
		}
		header = document.createElement('DIV');
		Element.extend(header);
		header.update("Uw producten:");
		header.writeAttribute({"id":"jorr_shopping_cart_header"});
		basket.appendChild(header);
		// alert(this.items.length);
		var grandtotal = 0;
		for(i=0;i<this.items.length;i++){
			pro_title = document.createElement('DIV');
			Element.extend(pro_title);
			pro_title.addClassName('jorr_shopping_cart_row');
			total = this.items[i].get_price_total();
			grandtotal += total;
			var s = this.items[i].ammount+" X "+this.items[i].title+ "<br/>EUR "+this.format_currency(total);
			pro_title.update(s);
			
			pro_delete = document.createElement('DIV');
			Element.extend(pro_delete);
			pro_delete.addClassName('j_buttons');
			pro_delete.writeAttribute({"id":"b_delete","pid":this.items[i].id,"o_name":this.name});
			pro_delete.observe('click', function(event){
				var me = Event.element(event);	
				var myid = me.readAttribute("pid");
				var my_o = me.readAttribute("o_name");
				eval("my_basket = "+my_o+";");
				my_basket.set_ammount(myid,0);
			});
			
			pro_plus = document.createElement('DIV');
			Element.extend(pro_plus);
			pro_plus.addClassName('j_buttons');
			pro_plus.writeAttribute({"id":"b_plus","pid":this.items[i].id,"ammount":this.items[i].ammount+1,"o_name":this.name});
			pro_plus.observe('click', function(event){
				var me = Event.element(event);	
				var myid = me.readAttribute("pid");
				var ammount = me.readAttribute("ammount");
				var my_o = me.readAttribute("o_name");
				eval("my_basket = "+my_o+";");
				my_basket.set_ammount(myid,ammount);
			});
			
			pro_min = document.createElement('DIV');
			Element.extend(pro_min);
			pro_min.addClassName('j_buttons');
			pro_min.writeAttribute({"id":"b_min","pid":this.items[i].id,"ammount":this.items[i].ammount-1,"o_name":this.name});
			pro_min.observe('click', function(event){
				var me = Event.element(event);	
				var myid = me.readAttribute("pid");
				var ammount = me.readAttribute("ammount");
				var my_o = me.readAttribute("o_name");
				eval("my_basket = "+my_o+";");
				my_basket.set_ammount(myid,ammount);
			});
			
			pro_title.appendChild(pro_delete);
			pro_title.appendChild(pro_min);
			pro_title.appendChild(pro_plus);
			
			basket.appendChild(pro_title);
		}
		tokassa = document.createElement('DIV');
		Element.extend(tokassa);
		tokassa.writeAttribute({"id":"b_kassa"});
		tokassa.observe('click', function(event){
				mandje.gotoKassa();
			});
		total = document.createElement('DIV');
		Element.extend(total);
		total.writeAttribute({"id":"jorr_shopping_cart_total"});
		total.update("Totaal: EUR "+this.format_currency(grandtotal));
		footer = document.createElement('DIV');
		Element.extend(footer);
		footer.writeAttribute({"id":"jorr_shopping_cart_footer"});
		footer.update(tokassa);
		// footer.appendChild(tokassa);
		basket.appendChild(total);
		basket.appendChild(footer);
	}

}
// --> 
