// an array containing the box count by job index
var boxCountList = [0];

/**
 * Update the boxCount array, ordered by job index, that contains the number of box needed for an ups shippement.
 * @param $jobLine the 'tr' line of the job
 * @param productRef the reference of the selected product
 * @param formatRef the reference of the selected format
 */
function updateBoxCount($jobLine)
{
    var productRef = $('select.product_reference', $jobLine).val();
    var formatRef = $('select.format_reference', $jobLine).val();
    var boxCount = 0;
    var $jobDiv = $jobLine.closest("div.job_concerns");
    var index = parseInt($jobDiv.attr('index'));
    if (productRef != 'null')
    {
        var qty = parseInt($('input.quantity_hidden', $jobDiv).val());
        if (qty != 0)
        {
            if (formatRef != 'null')
            {
                var format;
                for (var i in productMap[productRef].formats)
                {
                    if (productMap[productRef].formats[i].reference == formatRef)
                    {
                        format = productMap[productRef].formats[i];
                        break;
                    }
                }
                boxCount = boxCount + Math.ceil(qty / format.quantityPerBox);
            }
        }
    }
    boxCountList[index] = boxCount;
    $('input[type=hidden].box_count', $jobDiv).val(boxCount);
}

function increaseQty(event)
{
    var $jobLine = event.data.$jobLine;
    var increase = event.data.increase;
    var productRef = $('select.product_reference', $jobLine).val();
    if (productRef != 'null')
    {
        var product = productMap[productRef];
        var increaseQty = product.extensionQty * increase;
        var baseQties = product.baseQuantities;
        var currentQty = parseInt($('input.quantity_hidden', $jobLine).val());
        var currentQtyIndex = $.inArray(currentQty, baseQties);
        if (currentQtyIndex == -1 || (currentQtyIndex + increase) == baseQties.length)
        {
            currentQty = currentQty + increaseQty;
        }
        else if (currentQtyIndex + increase != -1)
        {
            currentQty = baseQties[currentQtyIndex + increase];
        }
        $('input.quantity', $jobLine).val(currentQty);
        $('input.quantity_hidden', $jobLine).val(currentQty);
        updateBoxCount($jobLine);
        updateShippingPrice();
        updateJobPrice($jobLine);
    }
    return false;
}