
var WishCartName = "CrescendoWishListCart";
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,color,size){
	var CartList = GetCookie(WishCartName).split('|');
	
	for (var cntr = 0; cntr < CartList.length; cntr++)
	{
		var item = CartList[cntr].split(':')[0];
		var chkcolor = CartList[cntr].split(':')[2];
		var chksize = CartList[cntr].split(':')[3];

		if (item == prodID && chkcolor == color && chksize == size ) {
			Counter = cntr;
			return true;
		}
	}
	
	return false;
}

/* add wish list to the cart list */
function AddWishList(prodID, Qty, color,size,sp){
	
	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 + ':' + color + ':' + size + ':' + sp;
		SetCookie(WishCartName, CartList);
		
	} else {
		/* re compute qty if new wish list is already used */
		if (WishListExisting(prodID,color,size) == 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 + ':' + ExistingWishList[2] + ':' + ExistingWishList[3] + ':' + ExistingWishList[4];
			
			CartList = Replace(CartList, OldWishList, NewWishList);
			
		} else {
			CartList = CartList + '|' + prodID + ':' + Qty + ':' + color + ':' + size + ':' + sp;	
		}
	}
//	alert(CartList);
	
	DelCookie(WishCartName); //delete old cart cookies
	SetCookie(WishCartName, CartList); //create and add wish list
	LoadCart();
}

/* remove the wish list to the cart list */
function RemoveWishList(prodID,color,size,sp)
{	
	/* 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];
			var chkcolor = CartList[cntr].split(':')[2];
			var chksize = CartList[cntr].split(':')[3];
			if( item == prodID && chkcolor == color && chksize == size){//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
		if(sp==1){
		}else{
		location.href = 'cart.php';
		}
	}
}
function removelist(prodID,color,size){
	if(confirm("Are you sure to clear this item?")){
		RemoveWishList(prodID,color,size)
	}
}
function removelistddl(prodID,color,size){
	if(confirm("Are you sure to clear this item?")){
		RemoveWishList(prodID,color,size,1);
		acolor = color.replace(" ","");
		asize = size.replace(" ","");
		$("#ddlitem"+prodID+"_"+acolor+"_"+asize).css("background","#363535");
		$("#ddlitem"+prodID+"_"+acolor+"_"+asize).fadeOut(1000,function(){
			$("#ddlitem"+prodID+"_"+acolor+"_"+asize).remove();
			$("#cartQty").html(viewitembag());
		})

	}
}
/* 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, color, size, me)
{	
	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 && item[2] == color && item[3] == size) {			
			if (col == 1)
				NewItem = item[0] + ':' +  me.value + ':' + item[2] + ':' + item[3] + ':' + item[4];
			else {
				NewItem = item[0] + ':' +  me.value + ':' + item[2] + ':' + item[3] + ':' + item[4];
			}
		}
		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 = "$ " + currencyFormat(centFormat(RoundOff(amt,2)));
	}
	//document.getElementById('DIVtotal').innerText = currencyFormat(centFormat(RoundOff(total,2)));
	document.getElementById("DIVtotal").innerHTML = "$ " + 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 = url;
}

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;
	}
}
