function applyBdayDiscount(name, type, amount) {
	var fbidinput = document.getElementById("fb_id");
	var fbid = fbidinput.value;
	
	couponType = type;
	couponAmount = amount;
   	var couponCodeProps = {"title": "Birthday Gift", "price": 0};
   	
	var couponCodeCustomProps = {"FBID":fbid,"Discount":"$7 Off"};
	var codeItem = googlecart.makeItem(couponCodeProps, couponCodeCustomProps, 1);
	googlecart.addItem(codeItem);
}

function checkForBdayGift() {
	var items = null;
	try {
		items = googlecart.getItems();
	} catch(err) {}

	if(items && items.length > 0) {
		for(i = 0; i < items.length; i++) {
			var giftItem = googlecart.getItem(i);
			if(giftItem && giftItem.getTitle() == "Birthday Gift") {
				//change button
				var bday_button = document.getElementById("bday_button");
				if(bday_button) {
					bday_button.value = "Remove Birthday Gift";
					bday_button.onclick = removeBdayDiscount;
				}
				return giftItem;
			}
		}
	}
	return null;
}

function updateBdayGift(item) {
	item.setPricePerUnit(0);
	var subtotal = googlecart.getSubtotal();
	var discount = -7;

	try {
		var items = null;
		items = googlecart.getItems();

		if(items && items.length > 0) {
			for(i = 0; i < items.length; i++) {
				var codeItem = googlecart.getItem(i);
				if(codeItem && codeItem.getTitle() == "Distributor Code") {
					subtotal = subtotal - codeItem.getPricePerUnit();
				}
			}
		}
	} catch(err) {}
		
	discount = (subtotal >= discount * -1) ? -7 : subtotal * -1;
	
	item.setPricePerUnit(discount);
	item.setMaxQuantity(1);
	item.setWeight(0.01);
	googlecart.saveCartAndRefreshWidget();
}

function removeBdayDiscount() {
	var giftItem = checkForBdayGift();
	if(giftItem.getTitle() != "Birthday Gift") {
		return null;
	}
	giftItem.markForRemoval();
	googlecart.expungeMarkedForRemoval();
	giftItem.setPricePerUnit(0);
	giftItem.unmarkForRemoval();
	
	try {
		var codeItem = googlecart.getItem(0);
		if(codeItem.getTitle() == "Distributor Code") {
			updateCouponCode(codeItem);
		}
	} catch(err) {}
	
	//change button
	var bday_button = document.getElementById("bday_button");
	bday_button.value = "Claim Your Birthday Gift";
	bday_button.onclick = applyBdayDiscount;
	
	return giftItem;
}

var distGooglecartAfterAdd = googlecartAfterAdd;
function bdayGooglecartAfterAdd(item, index) {
	try {
		if(item.getTitle() == "Birthday Gift") {
			//change button
			var bday_button = document.getElementById("bday_button");
			bday_button.value = "Remove Birthday Gift";
			bday_button.onclick = removeBdayDiscount;
			updateBdayGift(item);
		} else {
			var giftItem = checkForBdayGift();
			if(giftItem != null) {
				updateBdayGift(giftItem);
			}
		}
	} catch(err) {}

	distGooglecartAfterAdd(item, index);
}
googlecartAfterAdd = bdayGooglecartAfterAdd;

var distGooglecartAfterRemove = googlecartAfterRemove;
function bdayGooglecartAfterRemove(item, index) {
	if(item.getTitle() != "Birthday Gift") {
		if(item.getTitle() != "Distributor Code") {
			var giftItem = checkForBdayGift();
			if(giftItem)
				updateBdayGift(giftItem);
		}
	} else {
		//change button
		var bday_button = document.getElementById("bday_button");
		bday_button.value = "Claim Your Birthday Gift";
		bday_button.onclick = applyBdayDiscount;
	}
	
	distGooglecartAfterRemove(item, index);
}
googlecartAfterRemove = bdayGooglecartAfterRemove;

