//var ShopCart = Class.create();

// implemented singlieton 
var ShopCart = {
	session_id: null,
	cart_items: 0,
	cart_pricetotal: 0,
	
	loadSession: function () {
		if ( CookieJar.get('session_id') != null && CookieJar.get('session_id').length == 32 )
		{
			this.session_id = CookieJar.get('session_id');
			this.cart_items = CookieJar.get('cart_items');
			this.cart_totalprice = CookieJar.get('cart_pricetotal');
		} else {
			var tmpSessID = ShopCart.generateSessionID();
			CookieJar.set('session_id', tmpSessID, 7);
			CookieJar.set('cart_items', 0, 7);
			CookieJar.set('cart_pricetotal', 0, 7);
			this.session_id = tmpSessID; 
			this.cart_items = 0; 
			this.cart_pricetotal = 0; 
		}
	},
	
	generateSessionID: function() {
		var strChars = '0 1 2 3 4 5 6 7 8 9 a b c d e f';
		var arrChars = []; arrChars = strChars.split(' ');
		var str_compiled = ''; var max_length = 31;
		
		// escape first zero
		str_compiled = str_compiled + arrChars [ Math.floor( (arrChars.length-1) * Math.random()) + 1 ];
		while (max_length) {
			str_compiled = str_compiled + arrChars [ Math.floor(arrChars.length * Math.random()) ];
			max_length--;
		}
		
		//bugme('Final\n\nString: ' + str_compiled + '\nLength: ' + str_compiled.length );
		return str_compiled;
	},
	
	add: function ( product_id ) {		
		var sURL = ShopCart.getRequestUrl('itemAdd', product_id);
		remoteJson(
				{"uri": sURL,
            	"callback": function(){window.location='cart.html';},
            	"condition": "typeof(remote_data) != 'undefined'"}
        );	
		
	},
	
	remove: function ( product_id ) {
		var sURL = ShopCart.getRequestUrl('itemRemove', product_id);		
		remoteJson(
				{"uri": sURL,
            	"callback": function(){window.location='cart.html';},
            	"condition": "typeof(remote_data) != 'undefined'"}
        );
	},
	
	getItems: function ( returnFunc ) {		
		var sURL = ShopCart.getRequestUrl('itemList', 'all');
		remoteJson(
				{"uri": sURL,
            	"callback": function(){ if (remote_data.status='true') { returnFunc(remote_data.data, remote_data.status);} },
            	"condition": "typeof(remote_data) != 'undefined'"}
        );	
	},
	
	getOrderDetails: function ( returnFunc, hash_key ) {
		var sURL = ShopCart.getRequestUrl('orderDetails', hash_key);
		remoteJson(
				{"uri": sURL,
            	"callback": function(){ if (remote_data.status='true') { returnFunc(remote_data.data, remote_data.status);} },
            	"condition": "typeof(remote_data) != 'undefined'"}
        );		
	}, 
		
	getRequestUrl: 	function(op, val) {
		var dt_tm = new Date();
		var tm_now = dt_tm.getTime();
		var query = {
			'url' : JSE_Config.RxsecuredShopCart,
			'op' : escape(op),
			'val' : escape (val),			
			'sess' : CookieJar.get('session_id'),
			'affid' : JSE_Config.AffiliateID,
			'siteid' : JSE_Config.SiteID,
			'tm'	: tm_now
			};
		var objStrConnTpl = new Template ('#{url}?sess=#{sess}&site_id=#{siteid}&aff_id=#{affid}&op=#{op}&val=#{val}&tm=#{tm}'); 
		var str_connection = objStrConnTpl.evaluate ( query );		
		
		return str_connection;	
	}
		
};