var printBasketTimeoutId = 0;
var MAX_ITEM_SIZE = 10;

function addPrintBasket()
{
	var pb = new PrintBasket(g_country);
	var title = document.title;
	var pageNameElement = document.getElementById("page_name");
	if (pageNameElement != null) {
		title = pageNameElement.innerHTML;	
	}
	var url = document.URL;
	var printMsg = document.getElementById("addPrintBasketMessage");

	if (isCookieEnabled()) {
		if (pb.getListLength() < MAX_ITEM_SIZE) {
			if (pb.addPrintItem(title, url)) {
				printMsg.innerHTML = "<span style=\"color:#F00;\">This page was added to print basket.</span>";
			}	else {
				printMsg.innerHTML = "<span style=\"color:#2543C3;\">This page has been added to print basket before.</span>";
			}
		} else {
			printMsg.innerHTML = "<span style=\"color:#2543C3;\">Your print basket is full. Please go to manage print basket.</span>";
		}
	} else {
		printMsg.innerHTML = "<span style=\"color:#2543C3;\">Please enable cookies in your browser setting first.</span>";
	}
	printBasketTimeoutId = setTimeout("clearPrintBasketMessage()", 5000);
}

function clearPrintBasketMessage()
{
	var printMsg = document.getElementById("addPrintBasketMessage");
	printMsg.innerHTML = "";
	clearTimeout(printBasketTimeoutId);
}

// print basket obj
function PrintBasket(lang) {
	//this._cookieNamePrefix += lang + "_";
	var thisPath = window.location.pathname;
	var thisPathList = null;
	if (thisPath != null) {
		thisPathList = thisPath.split("/");
	}
	if (thisPathList.length > 1) {
		this.lang = thisPathList[1];
	}
	this.printItems = this.getPrintItemFromCookie();
}

PrintBasket.prototype.printItems = new Array();
PrintBasket.prototype._duration = 30; //days
PrintBasket.prototype._cookieNamePrefix = "hktb_print_basket_";
PrintBasket.prototype.lang = "";

PrintBasket.prototype.addPrintItem = function(title, url) {
	var count = 0;
	var len = this.printItems.length;
	for (var i = 0; i < len; i++) {
		if (this.printItems[i].url != url) {
			count++;
		}
		else {
			break;
		}
	}
	
	if (count == len) {
		this.printItems.push(new PrintItem(title, url));
		this.addPrintItemToCookie(this.printItems.length - 1, title, url);
		return true;
	}
	else {
		return false;
	}
}

PrintBasket.prototype.getListLength = function() {
	var len = this.printItems.length;
	return len;
}

PrintBasket.prototype.deletePrintItem = function(idx) {
	this.printItems.splice(idx, 1);
	this.resetPrintItemToCookie();
}

PrintBasket.prototype.clearAllPrintItems = function(idx) {
	this.printItems = new Array();
	this.eraseAllPrintItemsFromCookie();
}

PrintBasket.prototype.getPrintItemFromCookie = function() {
	var i = 0;
	var items = new Array();
	while (1) {
		var title = readCookie(this._cookieNamePrefix + "title" + i);
		var url = readCookie(this._cookieNamePrefix + "url" + i);
		if ((title != null) && (url != null)) {
			var thisPath = url.substring(7);
			if (thisPath.indexOf("/")>0) {
				thisPath = thisPath.substring(thisPath.indexOf("/"));
				var thisPathList = thisPath.split("/");
				if (thisPathList.length > 1) {
					if (this.lang != thisPathList[1]) {
						//alert("remove " + url + " from cookies.");
						this.deletePrintItem(i);
					} else {
						items.push(new PrintItem(title, url));
					}
				}
			}
			i++;
		}	else {
			break;
		}
	}
	return items;
}

PrintBasket.prototype.addPrintItemToCookie = function(idx, title, url) {
	createCookie(this._cookieNamePrefix + "title" + idx, title, this._duration);
	createCookie(this._cookieNamePrefix + "url" + idx, url, this._duration);
}

PrintBasket.prototype.resetPrintItemToCookie = function() {
	this.eraseAllPrintItemsFromCookie();
	var len = this.printItems.length;
	for (var i = 0; i < len; i++) {
		createCookie(this._cookieNamePrefix + "title" + i, this.printItems[i].title, this._duration);
		createCookie(this._cookieNamePrefix + "url" + i, this.printItems[i].url, this._duration);
	}
}

PrintBasket.prototype.eraseAllPrintItemsFromCookie = function() {
	eraseAllCookies(this._cookieNamePrefix);
}

// print item obj
function PrintItem(title, url) {
	this.title = title;
	this.url = url;
}

PrintItem.prototype.title = "";
PrintItem.prototype.url = "";

// cookie util
function createCookie(name,value,days) {
	value = encodeURIComponent(value);
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return decodeURIComponent(c.substring(nameEQ.length,c.length));
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function eraseAllCookies(prefix) {
	
	var pattern = new RegExp(prefix + "[^=]*=", "g");
	var names = document.cookie.match(pattern);
	
	if (names != null) {
		var len = names.length;
		for (var i = 0; i < len; i++) {
			eraseCookie(names[i].substr(0, names[i].length-1));
		}
	}
	
}

function isCookieEnabled() {
	var isEnabled = false;
	var tmpCookieName = "tmpcookie";
	var tmpCookieValue = "tmpCookieValue";
	createCookie(tmpCookieName,tmpCookieValue,1000);
	var cookieValue = readCookie(tmpCookieName);
	if (cookieValue != null && cookieValue == tmpCookieValue) {
		isEnabled = true;
		eraseCookie(tmpCookieName);
	}
	return isEnabled;
}

