
function setCookie(name, value) {
  var curCookie = name + "=" + escape(value)
  document.cookie = curCookie;
}

function getSubCookieValue(product){
	var curcookieVal =  getCookie("afx_wishlist");
	var outString = 0;
	if(curcookieVal){
		var arrVal = curcookieVal.split(",");
		for(i=0;i<arrVal.length;i++){
			if(arrVal[i].indexOf(product)!=-1){
				outString = arrVal[i].substring(arrVal[i].indexOf('%')+1,arrVal[i].length);
			}
		}
	}
	
	return outString;
}

function setSubCookieValue(product, qty){
	var curcookieVal =  getCookie("afx_wishlist");
	newcookieVal = '';
	if(curcookieVal){
		var valExists=false;
		var arrVal = curcookieVal.split(",");
		for(i=0;i<arrVal.length;i++){
			if(arrVal[i].indexOf(product)!=-1){
				if(qty=='0'){
					//arrVal.splice(i,1); //remove 1 value from array at index i
					delete arrVal[i];
				}else{
					arrVal[i] = product + "%" + qty;
				}
				valExists = true;
			}
		}
		newcookieVal = arrVal.toString();
	}
	
	if(!valExists){
		newcookieVal +="," + product + "%" + qty;
	}
	setCookie("afx_wishlist", newcookieVal);
	
}

function appendToCookie(product, qty){
	
	if(qty=='-1' && parseInt(getSubCookieValue(product))<=1){
		setSubCookieValue(product, '0');
		passWishProds();
	}else{
	setSubCookieValue(product,parseInt(getSubCookieValue(product)) + parseInt(qty));
	
	parseDocForWishList();
	//passWishProds();
	}
}

function deleteSubCookie(product){
	
}


function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
  passWishProds();
}
//deleteCookie("afx_wishlist");


function parseDocForWishList(){
	var divvies = document.getElementsByTagName('INPUT');
	for(var i=0;i<divvies.length;i++){
		
		if(divvies[i].className == 'wishlist'){
			
			divvies[i].value = getSubCookieValue(divvies[i].id);
		}
	}
	divvies = document.getElementsByTagName('DIV');
	for(var i=0;i<divvies.length;i++){
		
		if(divvies[i].className == 'wishlist'){
			if(getSubCookieValue(divvies[i].id)!=0)
				divvies[i].innerHTML = '<img src="/images/afx/on_my_wishlist.gif" />';
		}
	}
}

/*function parseDocForWishList(){
	var divvies = document.getElementsByTagName('DIV');
	var o=0;
	for(var i=0;i<divvies.length;i++){
		
		if(divvies[i].className == 'wishlist'){
			if(getSubCookieValue(divvies[i].id)!=0)
				divvies[i].innerHTML = '<img src="/images/afx/on_my_wishlist.gif" />';
		}
	}
}*/

function passWishProds(){
	var targurl = '/afx/browse.asp?containerID=afx_wishlist&contents=';
	var wishlist = getCookie('afx_wishlist');
	if(wishlist){
		var productstring = ''
		var prodarray = wishlist.split(',');
		for(var i=0; i<prodarray.length; i++){
			productstring += prodarray[i].substring(0,prodarray[i].indexOf("%")) + ',';
		}
	}
	window.location.href=targurl + productstring;
}
