
var WishCartName = "CitiWellnessWishListCart";
var Counter = 0; //for existing wish list counter


/* check if new wish list is existing if yes return the counter
   and populate the item to get the qty and add to the existing */
function WishListExisting(prodID){
	var CartList = GetCookie(WishCartName).split('|');
	
	for (var cntr = 0; cntr < CartList.length; cntr++)
	{
		var item = CartList[cntr].split(':')[0];

		if (item == prodID) {
			Counter = cntr;
			return true;
		}
	}
	return false;
}

/* add wish list to the cart list */
function AddWishList(prodID, Qty){
	if(Qty == "" || Qty == "0"){	
	alert("Please enter the quantity.");
	return false;
	} else {
	
	var CartList = GetCookie(WishCartName);
	
	/* check if cart cookies is already defined, if no create then add wish list */
	if (CartList == null || CartList == '' ) {
		CartList = prodID + ':' + Qty;
		SetCookie(WishCartName, CartList);
	} else {
		/* re compute qty if new wish list is already used */
		if (WishListExisting(prodID) == true) {
			var SplitedCartList = CartList.split('|');
			var ExistingWishList = SplitedCartList[Counter].split(':');
			var OldWishList = SplitedCartList[Counter];
			var oldQty = Math.abs(ExistingWishList[1]) + Math.abs(Qty);
			
			var NewWishList = ExistingWishList[0] + ':' + oldQty;
			
			CartList = Replace(CartList, OldWishList, NewWishList);
			
		} else {
			CartList = CartList + '|' + prodID + ':' + Qty;	
		}
	}
	}
//	alert(CartList);
	
	DelCookie(WishCartName); //delete old cart cookies
	SetCookie(WishCartName, CartList); //create and add wish list
	location.href = 'cart.php';
}

/* remove the wish list to the cart list */
function RemoveWishList(prodID) 
{	
	/* check if cart cookies is already defined, if yes remove wish list */
	if (GetCookie(WishCartName) != null)
	{
		var CartList = GetCookie(WishCartName).split('|');
		var NewCartList = '';
		
		for (var cntr = 0; cntr < CartList.length; cntr++)
		{
			var item = CartList[cntr].split(':')[0];
			if( item == prodID){//buburahin
				
			}
			else{
				NewCartList = NewCartList + iif(NewCartList.length > 0, '|', '') + CartList[cntr];
			}
		}
		
		
		//alert(NewCartList)
		DelCookie(WishCartName); //delete old cart cookies
		SetCookie(WishCartName, NewCartList); //create and add wish list
		location.href = 'cart.php';
	}
}

function removelist(prodID){
	if(confirm("Are you sure to clear this item?")){
		RemoveWishList(prodID)
	}
}
/* On populated list during changing of qty and Price
   re modify the cart list but this time with edited qty and Price */
function ModifyWishList(row, col, prodID, me)
{	
	if (me.value=="") {
	me.value=0;
	}
	
	var CartList = GetCookie(WishCartName).split('|');
	var NewCartList = '';
	
	for (var cntr = 0; cntr < CartList.length; cntr++)
	{
		var item = CartList[cntr].split(':');
		var NewItem = CartList[cntr];
		
		if (item[0] == prodID) {			
			if (col == 1)
				NewItem = item[0] + ':' +  me.value;
			else {
				NewItem = item[0] + ':' +  me.value;
			}
		}
		NewCartList = NewCartList + iif(NewCartList.length > 0, '|', '') + NewItem;
	}
	DelCookie(WishCartName); //delete old cart cookies
	SetCookie(WishCartName, NewCartList); //create and add wish list
}

function recalculate()//recalculate function
{	
	var CartList = GetCookie(WishCartName).split('|');
	var total= 0;
	var amt = 0;
	var totalqty = 0;
	for (var cntr = 0; cntr < CartList.length; cntr++)
	{	
		var price = document.getElementById('price'+cntr).value;
		var item = CartList[cntr].split(':');
		amt = (item[1] * 1) * price;
		total = total + amt;
		totalqty = totalqty + (item[1] * 1);
		document.getElementById("amt"+cntr).innerHTML = "PhP " + currencyFormat(centFormat(RoundOff(amt,2)));
	}
	//document.getElementById('DIVtotal').innerText = currencyFormat(centFormat(RoundOff(total,2)));
	document.getElementById("DIVtotal").innerHTML = "PhP " + currencyFormat(centFormat(RoundOff(total,2)));
	document.getElementById('cartQty').innerHTML=totalqty;
}
 
function ClearWishList()
{

	if (GetCookie(WishCartName) != null) {
		DelCookie(WishCartName);
		window.location = "cart.php?action=clear";
	} else {
		alert("No Wish list to clear");
	}
}
function ClearAll(){

	if (GetCookie(WishCartName) != null) {
		DelCookie(WishCartName);
	}
}
function clearallitems(){
	if(confirm("Are you sure to clear all items?")){
		ClearWishList()
	}
}
function ClearWishList1(){

	if (GetCookie(WishCartName) != null) {
		DelCookie(WishCartName);
		document.getElementById('cartQty').innerHTML='0';
	} else {
		alert("No Cart List to clear");
	}
}

function LoadCart(){
//	var url = document.URL.split('/');
//	
//	url = url[url.length -1];
//	url = document.URL.replace(url, 'cart.php');
//	
	window.location = cart.php;
}

function PrintWishList()
{
    var wins = window.open('printwishlist.html', '', '');
}


function viewitembag()//recalculate function
{	var totqty = 0;
	if(GetCookie(WishCartName) != null){
		var CartList = GetCookie(WishCartName).split('|');
		
		if(CartList == "" || CartList == null){
			return totqty;
		}
		else{
			for (var cntr = 0; cntr < CartList.length; cntr++)
			{	
			var item = CartList[cntr].split(':');
			totqty = totqty + (item[1] * 1);
			}
			return totqty;
		}
	}
	else{
		return totqty;
	}
}

function ClearWishList2(){

	if (GetCookie(WishCartName) != null) {
		DelCookie(WishCartName);
		document.getElementById('cartQty').innerHTML='0';
	}
}
