
var WishCartName = "NWUCart";
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;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function proceed(){
	document.cart.action="https://www.sandbox.paypal.com/us/cgi-bin/webscr";
	document.cart.submit();
}
/* add wish list to the cart list */
function AddWishList(prodID, Qty, color){
	
	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;
		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 + ':' + ExistingWishList[2];
			
			CartList = Replace(CartList, OldWishList, NewWishList);
			
		} else {
			CartList = CartList + '|' + prodID + ':' + Qty + ':' + color;	
		}
	}
//	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)
{	
	/* 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) {
				NewCartList = NewCartList + iif(NewCartList.length > 0, '|', '') + CartList[cntr];
			}
		}
		
		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)
{	
	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 + ':' + item[2];
			else {
				NewItem = item[0] + ':' +  me.value + ':' + item[2];
			}
		}
		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;

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

function ClearWishList()
{

	if (GetCookie(WishCartName) != null) {
		DelCookie(WishCartName);
		window.location = "cart.php?action=clear";
	} else {
		alert("No Wish list to clear");
	}
}
function clearallitems(){
	if(confirm("Are you sure to clear all items?")){
		ClearWishList()
	}
}
function ClearWishList1(){

	if (GetCookie(WishCartName) != null) {
		DelCookie(WishCartName);
	} 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 getKeyCode(e)
{
	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;
}

function keyRestrict(e, validchars) { 
	var key='', keychar='';
	key = getKeyCode(e);
	if (key == null) return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	validchars = validchars.toLowerCase();
	if (validchars.indexOf(keychar) != -1)
		return true;
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;
	return false;
}
