var WishCartName = "GSWishListCart";
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(Id,s_id)
{
	var CartList = GetCookie(WishCartName).split('|');
	
	for (var cntr = 0; cntr < CartList.length; cntr++)
	{
		var item = CartList[cntr].split(':')[4];
		var size = CartList[cntr].split(':')[1];
		if (item == Id && size == s_id ) {
			Counter = cntr;
			return true;
		}
	}
	
	return false;
}

/* add wish list to the cart list */
function AddWishList(Name,Size,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 + ':' + Size +':' + Qty + ':' + Price + ":" + Id;
		SetCookie(WishCartName, CartList);
		
	} else {
		/* re compute qty if new wish list is already used */
		if (WishListExisting(Id,Size) == true) {
			var SplitedCartList = CartList.split('|');
			var ExistingWishList = SplitedCartList[Counter].split(':');
			var OldWishList = SplitedCartList[Counter];
			var oldQty = Math.abs(ExistingWishList[2]) + Math.abs(Qty);
			
			var NewWishList = ExistingWishList[0] + ':' + ExistingWishList[1] + ':' + oldQty +  ':' + ExistingWishList[3]+ ':' + ExistingWishList[4];
			
			CartList = Replace(CartList, OldWishList, NewWishList);
			
		} else {
			CartList = CartList + '|' + Name + ':' + Size +':' + 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,Size)
{
	/* 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(':')[4];
			var size = CartList[cntr].split(':')[1];
	
			if (item == Id && size == Size) {
				
			}
			else{
				NewCartList = NewCartList + iif(NewCartList.length > 0, '|', '') + CartList[cntr];	
			}
		}
		
		DelCookie(WishCartName); //delete old cart cookies
		SetCookie(WishCartName, NewCartList); //create and add wish list
		
		location.href = 'view_cart.html';
	}
}

function removelist(Id,Size){
	if(confirm("Are you sure to clear this item?")){
		RemoveWishList(Id,Size)
	} 
}


/* 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,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[4] == Id && item[1] == Size) {			
				NewItem = item[0] + ':' + item[1] + ':' + me.value + ':' + 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 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[2] * 1) * (item[3] * 1);
		total=total + (item[2] * 1) * (item[3] * 1);
	}
		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 = "view_cart.html?action=clear";
	} else {
		alert("No product list to clear");
	}
}

function clearallitems(){
	if(confirm("Are you sure to clear all items?")){
		ClearWishList()
	}
}

function LoadCart()
{
	var url = document.URL.split('/');
	
	url = url[url.length -1];
	url = document.URL.replace(url, 'view_cart.html');
	
	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 PlaceOrder()
{
	var form_credit = document.frmcredit;
	var ans = 0;

	
	if(form_credit.txtCVV2.value == ""){
		form_credit.txtCVV2.focus();
		ans = 1;
	} 
	
	if(form_credit.txtCreditCardNumber.value == ""){
		form_credit.txtCreditCardNumber.focus();
		ans = 1;
	}
	
	if(ans == 0){
		form_credit.action = "checkout_proccess.php"
		form_credit.submit();
	}
	else {
		alert("Please check the required fields before you place your order.");
	}
}

function vCart(){
	var frm_vcart = document.cart;
	
	frm_vcart.action = "checkout_form.html";
	frm_vcart.submit();
}

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[2] * 1);
			}
			return totqty;
		}
	}
	else{
		return totqty;
	}
}

function ClearWishList2()
{	if (GetCookie(WishCartName) != null) {
		DelCookie(WishCartName);
		document.getElementById('myitembag').innerHTML='0';
	}
}



