CompareCards = function () { 
	return {
		addCard: function(cardId) {
			var compareMaxMsg = "You cannot compare more credit cards because you have 4 selected to compare, which is the maximum number. To save more credit cards, you'll have to remove some you're not interested in first.";

			if (document.getElementById("compare-max-card-msg")) compareMaxMsg = document.getElementById("compare-max-card-msg").value;

			recordClientSideClick('compare-cards-adding-'+cardId);
			var count = this.getCount();
			if (count >= 4) {
				alert(compareMaxMsg);
				return false;
			}
			cardId = cardId.toString();
			if (this.cardIsCompared(cardId) > 0) {
				return true;
			}
			var newCards = Array(cardId);
			
			if (this._getCookie() != null) {
			    
				//newCards = newCards.concat(this._getCookie());
				newCards = (this._getCookie()).concat(newCards);
				
			}
			this._setCookie(newCards);
			return true;
		},
		removeCard: function(cardId) {
			recordClientSideClick('compare-cards-removing-'+cardId);
			cardId = cardId.toString();
			var comparedLocation = this.cardIsCompared(cardId);
			if(comparedLocation==0) return true;
			else {
				var cards = this._getCookie();
				var deleted = cards.splice(comparedLocation-1,1);
				this._setCookie(cards);
			}
		},
		clearCards: function() {
			this._setCookie("");
		},
		cardIsCompared: function(cardId) {
			cardId = cardId.toString();
			var cards = this._getCookie();
			if (cards != null) {
				for (var i=0;i<cards.length;i++) {
					if (cardId == cards[i].toString()) {
						return (i + 1);
					}
				}
			} else {
				return 0;
			}
		},
		getCount: function() {
			if (this._getCookie() != null) {
				return this._getCookie().length; 
			} 
			else {
				return 0;
			}
		},
		assignCV8WSS: function() {
			if (cv != null) {
			    var cvC8 = "";
				var compareCookie = this._getCookie();
				if (compareCookie){
					cvC8 = compareCookie.join(":");
			        _hbSet('cv.c8', cvC8);
			        _hbSend();			
			    }
			}
		},
		viewCompared: function() {
			if(this.getCount() == 0) {
				var noComparedCardMsg = "You've tried to view your compared cards, but you don't have any selected for comparison.";
				if (document.getElementById("no-compared-card-msg")) noComparedCardMsg = document.getElementById("no-compared-card-msg").value;
				alert(noComparedCardMsg);
			} else {
				this.assignCV8WSS();
				var hiddenForm = document.getElementById("compare-form");
				document.getElementById("compare-form").FromResultPage.value= "Y";
				hiddenForm.submit();
			}
		},
		isCardComparedById: function(id) {
			var cards = this._getCookie();
			var i = 0;
			if(cards == null){
				return false;
			}
			for(i=0; i < cards.length; i++){
				if(cards[i] == id){
					return true;
				}                       
			}
			return false;
		},
		getComparedCards: function() {
			var cards = this._getCookie();
			if(cards == null){
				return "";
			}
			return cards;
		},
		_setCookie: function(cookieValue) {
			if(cookieValue.length == 0) {
			    document.cookie = "compare_cards=;path=/;expires=Fri, 02-Jan-1970 00:00:00 GMT";
			} else {
				//var expires = new Date(new Date().getTime() + (7*2*24)*3600000);
				document.cookie = "compare_cards=" + cookieValue.join("|") +";path=/;";
			}
		},
		_getCookie: function() {
			var start = document.cookie.indexOf("compare_cards=");
			if (start == null || start == -1) return null;
			var len = start + 14;
			var end = document.cookie.indexOf(";",len);
			if (end == -1) end = document.cookie.length;
			var cookie = document.cookie.substring(len,end).split("|");	
			return cookie;
		}
	};
}();