function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
};

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
// end of function CurrencyFormatted()



function calcProdSubTotal() {
    
    var prodSubTotal = 0;

    $(".row-total-input").each(function(){
    
        var valString = $(this).val() || 0;
        
        var valNumber = (+valString);
        
        prodSubTotal += valNumber;    
                    
    });
    
    	var cleanSubTotal = CurrencyFormatted(prodSubTotal);
        
	    $("#product-subtotal").val(cleanSubTotal);


};

function calcTotalBottles() {

    var totalBottles = 0;

    $(".num-bottles-input").each(function() {
    
        var thisValue = $(this).val();
    
        if ( (IsNumeric(thisValue)) &&  (thisValue != '') ) {
        
            totalBottles += parseInt(thisValue);
        
        };
    
    });
    
    $("#total-bottles-input").val(totalBottles);

};

function calcShippingTotal() {

    var totalBottles = $("#total-bottles-input").val() || 0;
    var shippingRate = $("#shipping-rate").text() || 0;
    var shippingTotal = totalBottles * shippingRate;
    
    $("#shipping-subtotal").val(shippingTotal);

};

function calcDiscountTotal() {

    var discountTotal = 0;
    
    var ameridenTotal = 0;
    var otherTotal = 0;
    var quantity = 0;
    
    var readingAmeriden = true;
    $("#order-table tr").each(function(index) {
        var q = $("> td.num-bottles > input", this);
		var $this = $(this);
        if ((!$this.hasClass("even") && !$this.hasClass("odd")) || q.length !== 0) {
            quantity += parseInt(q.val()) || 0;
            if (readingAmeriden) {
                ameridenTotal += parseFloat($("td.row-total > input", this).val()) || 0;
            } else {
                otherTotal += parseFloat($("td.row-total > input", this).val()) || 0;
            }
        } else {
            if ($("> td", this).text() === "Creation's Garden Products") {
                readingAmeriden = false;
            }
        }
    });

    var share = quantity === 1 ? 0.05 :
                quantity >= 2 && quantity <= 5 ? 0.1 :
                quantity >= 6 && quantity <= 11 ? 0.15 :
                quantity >= 12 ? 0.2 :
                1;

    var ameridenDiscount = ameridenTotal * share;
    var otherDiscount = otherTotal * (share > 0.1 ? 0.1 : share);

    var discountTotal = ameridenDiscount + otherDiscount;
    
    var cleanDiscountTotal = CurrencyFormatted(discountTotal);
    
    $("#program-savings").val(cleanDiscountTotal);

};

function calcOrderTotal() {

    var orderTotal = 0;

    var productSubtotal = $("#product-subtotal").val() || 0;
    var discountSubtotal = $("#program-savings").val() || 0;

    var orderTotal = productSubtotal - discountSubtotal;
    var cleanOrderTotal = CurrencyFormatted(orderTotal);
    var orderTotalNice = "$" + cleanOrderTotal;
    
    $("#order-total").val(orderTotalNice);
        
};


$(function(){

    $('.num-bottles-input').blur(function(){
    
        var $this = $(this);
    
        var numBottles = $this.val();
        var multiplier = $this
                            .parent().parent()
                            .find("td.price-per-bottle span")
                            .text();
        
        if ( (IsNumeric(numBottles)) && (numBottles != '') ) {
            
            var rowTotal = numBottles * multiplier;
            var cleanRowTotal = rowTotal.toFixed(2);
            
            $this
                .css("background-color", "white")
                .parent().parent()
                .find("td.row-total input")
                .val(cleanRowTotal); 
//                .val(rowTotal);                    
            
        } else {
        
            $this.css("background-color", "#fffff"); 
                        
        };
        
        calcProdSubTotal();
        calcDiscountTotal()
        calcTotalBottles();
        calcShippingTotal();
        calcOrderTotal();
    
    });

});
