
function getCartCookie() {
	var value = '';
	var allcookie = document.cookie;
	var pos = allcookie.indexOf('cart_items=');
	if (pos != -1) {
		var start = pos + 11;
		var end = allcookie.indexOf(';', start);
		if (end == -1) {
			end = allcookie.length;
		}
		value = allcookie.substring(start, end);
	}
	return unescape(value);
}

function setCartCookie(cookieString) {
	var today = new Date();
	var nextyear = new Date();
	nextyear.setFullYear(today.getFullYear() + 1);
	document.cookie = 'cart_items='+escape(cookieString)+'; path=/; domain=hilton.com; expires='+nextyear.toGMTString();
}

function getTodayDate() {
	var now = new Date();
	var theDay = now.getDate();
	var theMonth = now.getMonth() + 1;
	var theYear = now.getFullYear();
	if (theDay < 10) {
		theDay = '0'+theDay;
	}
	if (theMonth < 10) {
		theMonth = '0'+theMonth;
	}
	return theMonth+'/'+theDay+'/'+theYear;
}

function removeItem(cart, ctyhocn) {
	var returnValue = '';
	var items = cart.split('|');
	for(i = 0; i < items.length; i++) {
		if (items[i].indexOf(ctyhocn) == -1) {
			returnValue = returnValue + '|' + items[i];
		}
	}
	return returnValue.substring(1);
}

function removeAllItem() {
	setCartCookie('');
}

function disableAddToCart(ctyhocn) {
	var textLink = document.getElementById('add_to_cart_text_link_'+ctyhocn);
	var textTxt = document.getElementById('add_to_cart_text_txt_'+ctyhocn);
	if (textLink != null && textTxt != null ) {
		textLink.removeAttribute('href');
		textTxt.innerHTML = '<span class="selected">Added to Favorite Hotels</span>';
	}
}

function disableAddToCart2(ctyhocn) {
	var iconImg = document.getElementById('add_to_cart_icon_img_'+ctyhocn);
	var textTxt = document.getElementById('add_to_cart_text_txt_'+ctyhocn);
	var iconLink = document.getElementById('add_to_cart_link_'+ctyhocn);
	if ( iconImg != null && iconLink != null && textTxt != null) {
		iconImg.src = '/common/media/images/icons/icon_cart_off.gif';
		iconLink.removeAttribute('href');
		textTxt.innerHTML = ' Added to Favorite Hotels';
	}
}

function writeAddToCart(ctyhocn) {
	var cart = getCartCookie();

	if (cart.indexOf(ctyhocn) == -1) {
		document.write('<tr><td width="12%"><img src="../../../common/media/images/misc/spacer.gif" width="20" height="15"></td><td width="124" align="left" title="Add to My Favorite Hotels"><a id="add_to_cart_text_link_'+ctyhocn+'" href="javascript:addToCart(\''+ctyhocn+'\',false)" class="navigation"><span id="add_to_cart_text_txt_'+ctyhocn+'">Add to Favorite Hotels</span></a></td></tr>');
	} else {
		document.write('<tr><td width="12%"><img src="../../../common/media/images/misc/spacer.gif" width="20" height="15"></td><td width="124" align="left" title="Added to My Favorite Hotels" class="selected" >Added to Favorite Hotels</td></tr>');	
	}
}

function writeAddToCart2(ctyhocn) {
	var cart = getCartCookie();
	if (cart.indexOf(ctyhocn) == -1) {
		document.write( '<a id="add_to_cart_link_'+ctyhocn+'" href="javascript:addToCart(\''+ctyhocn+'\',false)"><img id="add_to_cart_icon_img_'+ctyhocn+'" src="/common/media/images/icons/icon_cart_on.gif" border=0><span id="add_to_cart_text_txt_'+ctyhocn+'"> Add to Favorite Hotels</span></a>' );
	} else {
		document.write( '<img src="/common/media/images/icons/icon_cart_off.gif" border=0> Added to Favorite Hotels' );
	}
	disableAddToCart = disableAddToCart2;
}

function checkCookies(ctyhocn, isPop) {
	var cart = getCartCookie();
	if (cart.indexOf(ctyhocn) == -1) {
		alert('Please enable your cookies to use this feature.');
	} else {
		if (isPop == true) {
			alert('Hotel has been added to your list of Favorites. You can click on "My Favorite Hotels" at any time to view your selections.');
		} else {
			disableAddToCart(ctyhocn);
		}
	}
}

function addToCookie(ctyhocn) {
	var oldCart = getCartCookie();
	var newCart = removeItem(oldCart, ctyhocn);
	if ( newCart.length > 0 ) {
		setCartCookie(newCart+'|'+ctyhocn+':'+getTodayDate());
	} else {
		setCartCookie(ctyhocn+':'+getTodayDate());
	}
}

function removeFromCookie(ctyhocn) {
	var oldCart = getCartCookie();
	var newCart = removeItem(oldCart, ctyhocn);

	setCartCookie(newCart);
}

//add to cart and display js popup message or replace the add to cart button
function addToCart(ctyhocn, ispop) {
	addToCookie(ctyhocn);
	checkCookies(ctyhocn, ispop);
}

