var bookUrl = '';
var txtAdded = 'Bookmarked';
var txtProducts = 'Products';
var txtHaveAdded = 'You have bookmarked';
var txtView = 'View';

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie =	name + "=" + escape(value) + ((expires) ? "; expires=" +
			expires.toGMTString() : "") + ((path) ? "; path=" + path : "")
			+ ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function addBookmark(cid, pid) {
	// get the pid list: format--> pid123=13|4|64|88|7
	pidList = getCookie('pid'+cid);
	
	if (pidList=='') {
		pidList = pid;
	} else {
		found = false;
		tmpArr = pidList.split('|');
		for (i=0; i<tmpArr.length; i++) {
			if (tmpArr[i]==pid) {
				found = true;
				//alert("Already in bookmark!");
				break;
			}
		}
		if (!found)
			pidList += '|'+pid;
	}

	// expire in 30 day
	var now = new Date();
	now.setTime(now.getTime() + 30 * 24 * 60 * 60 * 1000);
	//now.setTime(now.getTime() + 30 * 1000);
	setCookie('pid'+cid, pidList, now, '/');
	showBookmarkDiv(cid);

	obj = document.getElementById("book_"+pid);
	if (obj)
		obj.innerHTML = "<a href='javascript:void(0)' class=bookT>"+txtAdded+"</a>";
}

function showBookmarkDiv(cid) {
	pidList = getCookie('pid'+cid);
	if (pidList=='') {
		return;
	} else {
		obj = document.getElementById("bookmarkBox");
		obj.style.display = '';
		obj = document.getElementById("bookmarkDiv");
		
		tmpArr = pidList.split('|');
		num = tmpArr.length;
		content = txtHaveAdded+' <b>' + num + '</b> '+txtProducts;
		//content += ' (<a href=\"/catalog/bookmark.jsp?cid='+cid+'&pidList='+escape(pidList.replace(/\|/gi, ','))+'\" target=_blank class=boxLinkFont>View</a>)';
		content += ' (<a href=\"'+bookUrl+'\" class=boxLinkFont>'+txtView+'</a>)';
		obj.innerHTML = content;
	}
}

function doDelete(form1, cid) {
	pidList = getCookie('pid'+cid);
	if (pidList=='')
		return;

	if (form1.pid) {
		if (form1.pid[0]) {
			for (i=0;i<form1.pid.length;i++) {
				if (form1.pid[i].checked==true) {
					pidList = removePid(pidList, form1.pid[i].value);
				}
			}
		} else if (form1.pid.checked==true) {
			pidList = removePid(pidList, form1.pid.value);
		}
	}

	// expire in 30 day
	var now = new Date();
	now.setTime(now.getTime() + 30 * 24 * 60 * 60 * 1000);
	setCookie('pid'+cid, pidList, now, '/');
	
	location = bookUrl;
}

function removePid(pidList, pid) {
	if (pidList=='' || pidList==pid)
		return '';
	pidList = '|'+pidList+'|';
	offset = pidList.indexOf('|'+pid+'|');
	end = pidList.indexOf('|', offset+1);
	if (end==-1)
		end = pidList.length;
	if (offset==0)
		pidList = pidList.substring(end);
	else
		pidList = pidList.substring(0, offset) + pidList.substring(end, pidList.length);
	return pidList.substring(1, pidList.length-1);
}

// enquiry now
function doEnquiry (cid, pid) {
	if (pid)
		addBookmark(cid, pid);
	//pidList = getCookie('pid'+cid);
	location = '/catalog/router/htm/cid_'+cid+'/itemType_5';
}
