
ShopCart = Class.create();


ShopCart.prototype = {
	///
	packages : [],
	courses : [],
	///CLASS BODY
	initialize: function() {
    },
	isEmpty : function(){
		var l = this.packages.length + this.courses.length;
		return (l==0);
	},
	clearArray: function(arr){
		arr.splice(0, arr.length);
	},
	checkSession : function(cb){
		var ap = new AjaxPlace({clas: "LSShoppingCart", func:"getShoppingCart"});
		ap.folder = "admin/";
		var callback = function(transport){
				this.showFromSession(eval(transport.responseText));
				if(cb!=null)
					cb();
		}
		ap.call({ onSuccess:callback.bind(this) });
	},
	showFromSession : function(r){
		///
		this.clearArray(this.packages);
		this.clearArray(this.courses);
		///
		var courses = r[0];
		var packs = r[1];
		for(var t=0; t<courses.length; t++){
			this.addCourse(courses[t].id, false);
		}
		for(var t=0; t<packs.length; t++){
			this.addPackage(packs[t].id, false);
		}
		this.countItems();
	},
	empty : function(){
		var ap = new AjaxPlace({clas: "LSShoppingCart", func:"clearShoppingCart"});
		ap.folder = "admin/";
		var callback = function(transport){
			this.clearArray(this.packages);
			this.clearArray(this.courses);
			this.countItems();
		}
		ap.call({ onSuccess:callback.bind(this) });
	},
	isInArray : function(arr, value){
		for(var t=0; t<arr.length; t++){
			if(arr[t] == value){
				return t;
			}
		}
		return -1;
	},
	addPackage : function(id, makeCall){
		makeCall = (makeCall == null)?true:makeCall;
		if( this.isInArray(this.packages, id) == -1 ){
			if(makeCall){
				if(!confirm("Are you sure you want to add this product to your shopping cart?"))
					return;
			}
			var ap = new AjaxPlace({clas: "LSShoppingCart", func:"addPackage", args:[id]});
			ap.folder = "admin/";
			var callback = function(transport){
				///alert(transport.responseText);
				///var r = eval(transport.responseText);
			}
			if(makeCall)
				ap.call({ onSuccess:callback.bind(this) });
			///
			this.packages.push(id);
			this.countItems();
		}else{
			alert("this product is already in your shopping cart");
		}
	},
	addCourse : function(id, makeCall){
		if(this.courses.length > 0){
			//alert("You can only buy one product at a time");
			alert("Only one training can be subscribed to at the same time. \nPlease remove the already selected training from your cart, before adding another one");
			return;
		}
		makeCall = (makeCall == null)?true:makeCall;
		if( this.isInArray(this.courses, id) == -1 ){
			if(makeCall){
				if(!confirm("Click OK to add this product to your shopping cart"))
					return;
			}
			var ap = new AjaxPlace({clas: "LSShoppingCart", func:"addCourse", args:[id]});
			ap.folder = "admin/";
			var callback = function(transport){
			    App.loadSection(6);
				//alert(transport.responseText);
				//var r = eval(transport.responseText);
				
			}
			if(makeCall)
				ap.call({ onSuccess:callback.bind(this) });
			///
			this.courses.push(id);
			this.countItems();
		}else{
			alert("this product is already in your shopping cart");
		}
	},
	removeCourse: function(id){
		var pos = this.isInArray(this.courses, id)
		this.courses.splice(pos, 1);
		this.countItems();
	},
	removePackage: function(id){
		var pos = this.isInArray(this.packages, id)
		this.packages.splice(pos, 1);
		this.countItems();
	},
	countItems : function(){
		var total = this.packages.length + this.courses.length;
		this.dispatchEvent( 'onNewItem', { total: total } );
	}
}

Object.extend( ShopCart.prototype, Event.Publisher );
