﻿function MediumBasketCookie(cookieName){
	this.values = new Array();
	this.expiry = new Date();
	this.cookieName = cookieName;
}
MediumBasketCookie.prototype.setExpiryDays = function(days){
		var baseDate = new Date();
		var expiryDays = 1;
		if (parseInt(days) != 'NaN'){expiryDays = parseInt(days);}
		this.expiry.setTime(baseDate.getTime() + (expiryDays *24 *60 *60 *1000));
		this._writeCookie();
	}
MediumBasketCookie.prototype.addItem = function(newValue){
		this.values[this.values.length] = newValue;
		this._writeCookie();
	}
MediumBasketCookie.prototype.addItems = function(newValues){
		for (var i=0;i<newValues.length;i++){
			this.values[this.values.length] = newValues[i];
		}
		this._writeCookie();
	}
MediumBasketCookie.prototype.addBasketItems = function(newProductLineValues){
		for (var i=0;i<newProductLineValues.length;i++){
			var kvp = new Array();
			kvp[0] = newProductLineValues[i].ProductId;
			kvp[1] = newProductLineValues[i].Quantity;
			this.values[this.values.length] = kvp;
		}
		this._writeCookie();
	}
	MediumBasketCookie.prototype.clear = function() {
	    var _expire = new Date();
	    _expire.setTime(0);
	    document.cookie = this.cookieName + '=; expires=' + _expire.toUTCString() + '; path=/';
	}
	MediumBasketCookie.prototype._writeCookie = function() {
	    var cookie = this.cookieName + '=';

        for (var i = 0; i < this.values.length; i++) {
            if (i > 0) { cookie = cookie + '&'; }
            if (typeof this.values[i] === 'number' || typeof this.values[i] === 'string')
            { cookie = cookie + this.values[i]; }
            else { cookie = cookie + this.values[i][0] + '=' + this.values[i][1]; }
        }
	        
	    cookie = cookie + '; expires=' + this.expiry.toUTCString() + '; path=/';
	    document.cookie = cookie;
	}