var couponType = null;
var couponAmount = null;

function applyCouponCode(name, type, amount) {
	couponType = type;
	couponAmount = amount;
   	var couponCodeProps = {"title": "Distributor Code", "price": 0};
   	var discountStr = "";
   	if(type == "%") {
   	   	discountStr = amount + "%";
   	} else if(type == "$") {
   	   	discountStr = "$" + amount;
   	} else {
   	   	//alert user of error
   	   	setCouponCodeStatus("Failed to retrieve Distributor Code: \""+name+"\"");
   	}
   	discountStr += " Off";

   	setCouponCodeStatus("Applied Distributor Code: \""+name+"\"");
	var couponCodeCustomProps = {"code":name, "discount":discountStr};
	var codeItem = googlecart.makeItem(couponCodeProps, couponCodeCustomProps, 1);
	codeItem.setPrivateData('[{"type":"'+type+'","amount":'+amount+'}]');
	googlecart.addItem(codeItem);
	//googlecart.saveCartAndRefreshWidget();
	//updateCouponCode(codeItem);
}

function checkForCouponCode() {
	var items = null;
	try {
		items = googlecart.getItems();
	} catch(err) {
	}
	if(items && items.length > 0) {
		var codeItem = googlecart.getItem(0);
		if(codeItem && codeItem.getTitle() == "Distributor Code") {
			return codeItem;
		}
	}
	return null;
}

function removeExistingCouponCode(index) {
	var codeItem = null;
	try {
		codeItem = googlecart.getItem(index);
	} catch(err) {
		return null;
	}
	if(codeItem.getTitle() != "Distributor Code") {
		return null;
	}
	codeItem.markForRemoval();
	googlecart.expungeMarkedForRemoval();
	codeItem.setPricePerUnit(0);
	codeItem.unmarkForRemoval();
	return codeItem;
}

function updateCouponCode(codeItem) {
	codeItem.setPricePerUnit(0);
	var subtotal = googlecart.getSubtotal();
	var discount = 0;
	var privateData = null;
	var type = null;
	var amount = null;
	try {
		var pds = codeItem.getPrivateData();
		privateData = eval(pds);
		privateData = privateData[0];
	} catch(err) {
	}

	if(privateData != null) {
		type = privateData.type;
		amount = privateData.amount;
	}

	/*
	if(couponType == "%") {
		discount = subtotal * -1 * couponAmount / 100;
	} else if(couponType == "$") {
		discount = couponAmount;
		if(discount > subtotal) {
			discount = subtotal;
		}
		discount = discount * -1;
	}*/

	if(type == "%") {
		discount = subtotal * -1 * amount / 100;
	} else if(type == "$") {
		discount = amount;
		if(discount > subtotal) {
			discount = subtotal;
		}
		discount = discount * -1;
	}
	
	codeItem.setPricePerUnit(discount);
	codeItem.setMaxQuantity(1);
	codeItem.setWeight(0.01);
	//codeItem.setShippingFirst(1);
	//codeItem.setDigital(true);
	//codeItem.setDigitalOptimistic(false);
	//codeItem.setDigitalDescription('');
	googlecart.saveCartAndRefreshWidget();
}

function googlecartAfterAdd(item, index) {
	if(item.getTitle() == "Distributor Code") {
	   	disableAddCouponCode();
		var existingCodeItem = removeExistingCouponCode(1);
		updateCouponCode(item);
		return;
	}
	
	if(index == 0) {
		/*need to move coupon code, if exists, to front of the list*/
		/*it should be 2nd element*/
		var codeItem = removeExistingCouponCode(1);
		if(codeItem != null) {
			googlecart.addItem(codeItem);
			googlecart.saveCartAndRefreshWidget();
		}
	} else {
		var codeItem = checkForCouponCode();
		if(codeItem != null) {
			updateCouponCode(codeItem);
		}
	}
}

function googlecartAfterRemove(item, index) {
	var codeItem = checkForCouponCode();
	if(codeItem != null) {
		updateCouponCode(codeItem);
		if(item.getTitle() == "Distributor Code")
			enableAddCouponCode();
	}
}

