
var WishCartName = "FBCWishlist";
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(Name)
{
	var CartList = GetCookie(WishCartName).split('|');
	
	for (var cntr = 0; cntr < CartList.length; cntr++)
	{
		var item = CartList[cntr].split(':')[0];

		if (item == Name) {
			Counter = cntr;
			return true;
		}
	}
	
	return false;
}

/* add wish list to the cart list */
function AddWishList(Name, Qty, Price, Id){
	
	
	
	var CartList = GetCookie(WishCartName);

	/* check if cart cookies is already defined, if no create then add wish list */
	if (CartList == null || CartList == '' ) {
		CartList = Name + ':' + Qty + ':' + Price + ":" + Id;
		SetCookie(WishCartName, CartList);
		
	} else {
		/* re compute qty if new wish list is already used */
		if (WishListExisting(Name) == 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];
			
			CartList = Replace(CartList, OldWishList, NewWishList);
			
		} else {
			CartList = CartList + '|' + Name + ':' + Qty + ':' + Price + ':' + Id;	
		}
	}
//	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(Id)
{
	/* 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(':')[3];
	
			if (item != Id) {
				NewCartList = NewCartList + iif(NewCartList.length > 0, '|', '') + CartList[cntr];
			}
		}
		
		DelCookie(WishCartName); //delete old cart cookies
		SetCookie(WishCartName, NewCartList); //create and add wish list
		
		location.href = 'viewcart.php';
	}
}

function removelist(Id){
	if(confirm("Est\u00E1 seguro de borrar este art\u00EDculo?")){
		RemoveWishList(Id)
	} 
}


/* On populated list during changing of qty and comments
   re modify the cart list but this time with edited qty and comments */
function ModifyWishList(Id,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[3] == Id) {			
				NewItem = item[0] + ':' + me.value + ':' + item[2] + ':' + item[3];
			}
		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 discount_total = 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) * (item[2] * 1);
		total=total + (item[1] * 1) * (item[2] * 1);
		//size= item[2];
		document.getElementById("amt"+cntr).innerHTML = "$ " + currencyFormat(centFormat(RoundOff(amt,2)));
		//document.getElementById("DIVtotal").innerHTML = "$ " + currencyFormat(centFormat(RoundOff(total,2)));
		//discount_total=discount_total+((document.getElementById('discount'+cntr).value*1)/100)*total;
	}
		//total = total + amt;
		document.getElementById("DIVtotal").innerHTML = "$ " + currencyFormat(centFormat(RoundOff(total,2)));
		//LoadCart();
}

function currencyFormat(amount)
{
 amount += "";

 var x = amount.split('.');
 var x1 = x[0];
 var x2 = x.length > 1 ? "." + x[1] : "";
 var rgx = /(\d+)(\d{3})/;

 while (rgx.test(x1)) {
  x1 = x1.replace(rgx, "$1" + "," + "$2");
 }

 return x1 + x2;
}


function centFormat(amount) 
{
    amount -= 0;
    
    return (amount == Math.floor(amount)) ? amount + '.00' : ((amount * 10 == Math.floor(amount * 10)) ? amount + '0' : amount);
}

function RoundOff(x, y) 
{
    var scale = Math.pow(10, y);
    
    return Math.round(x * scale) / scale;
}
//create div inner HTML

function ClearWishList()
{
	if (GetCookie(WishCartName) != null) {
		DelCookie(WishCartName);

		window.location = "viewcart.php?action=clear";
	} else {
		alert("No hay lista de productos para limpiar.");
	}
}

function clearallitems(){
	if(confirm("Est\u00E1 seguro de borrar todos los elementos?")){
		ClearWishList()
	}
}

function LoadCart()
{
	var url = document.URL.split('/');
	
	url = url[url.length -1];
	url = document.URL.replace(url, 'viewcart.php');
	
	window.location = url;
}

function Total() 
{
	var form_credit = document.frmcredit;
	var rValue = "";
	
	rValue = form_credit.txtProductTotal.value + ":" + form_credit.txtTaxes.value + ":" + 
			 form_credit.txtShipping.value + ":" + form_credit.txtGrandTotal.value;
			 
	form_credit = null;
	
	return rValue;
}

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