$j(document).ready(function($) { 
    
    $qtyValidate = $('#qty-validate');
    correctQty = getCorrectCount($qtyValidate);
    countBox = '<div class="how-many-left">You can add<div id="qtyCount">'+ correctQty +'</div> more items</div>';

    $($qtyValidate).next('br').after(countBox);

    $('.foxycart :input').change(function() {
        updateCount();
    });    
});

function getCorrectCount()
{
    $ = $j;
    correctQty = parseInt($($qtyValidate).text());
    
    if(isNaN(correctQty)) {
        correctQty = 'not available';
    }
    
    return correctQty;
}

function updateCount()
{
    howManyLeft = getHowManyLeft();
    $('#qtyCount').text(howManyLeft);
}

function getHowManyLeft()
{
    $ = $j;
    allInputs = $('.foxycart :input');
    
    var count;
    count = 0;
    $.each(allInputs, function(){
        countThese = ['cherries', 'berries', 'toffee', 'cocoa', 'mocha'];
        name = $(this).attr('name');
        if($.inArray(name, countThese) != '-1') {
            thisCount = parseInt($(this).val());
            if(isNaN(thisCount)) {
                thisCount = 0;
                $(this).val(thisCount);
            }
            count += thisCount;
        }
    });
    
    howManyLeft = correctQty - count;
    return howManyLeft;

}

function checkQuantity()
{
    var qtyCount;
    qtyCount = 0;        
    $('input').filter(function() {
        return $(this).attr('name').match('([0-99]:)?quantity');
    }).each(function() {
        thisValue = parseInt($(this).val());
        if (isNaN(thisValue)) {
            thisValue = 0;
        }
        qtyCount += thisValue;
    });
    
    if ( (qtyCount == '0') || (qtyCount == '') || (qtyCount == 'undefined') ) {
        return false;
    } else {
        return true;
    }
}

function fc_PreProcess() {
    errors = ' ';

    //Check for quantity above 0    
	if (checkQuantity() == false) {
		errors = 'Please select a quantity.\n';
    }
    
    //Check that quantity equals correntCount
    if( (getHowManyLeft() != 0) && (getCorrectCount() != 'not available') )
    {
        errors += 'You must add '+ getCorrectCount() +' items. \n';
    }

    if( errors != ' ') {
        alert(errors);
		return false;
	} else {
		return true;
	}
}

function fc_BuildFoxyCart() {
    clearForm();
    updateCount();
}

function clearForm() {
    $j('.foxycart :input').each(function() {
        var type = this.type;
        var tag = this.tagName.toLowerCase();
        if (type == 'text' || type == 'password' || tag == 'textarea') 
        {
            this.value = "0";
        }
        else if (type == 'checkbox' || type == 'radio') 
        {
            this.checked = false;
        }
        else if (tag == 'select') 
        {
            this.selectedIndex = -1;
        }
    });
};

// *************************
// Foxee functions. Needed for Foxee compatibility. Will need to be removed when Foxee 1.1 arrives
// *************************

function formatFloat(amount, type) {
	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;
	if( type == 'price' )
		s = foxee_currency_symbol_before+s+foxee_currency_symbol_after;
	else if( type == 'percent' )
		s = s+'%';
	return s;
}

function formatPercent(val)
{
	return formatFloat(val, 'percent');
}

function formatPrice(val)
{
	return formatFloat(val, 'price');
}