﻿// count then number of checkboxes that are checked.
//  args are the ids of the checkboxes to check
function countChecked( /* .... */ ) {
    var numChecked = 0;
    for( var i=0; i <arguments.length; ++i) {
        if ((arguments[i] != 0) && (document.getElementById(arguments[i]).checked))
            ++numChecked;
    } // end for

    return numChecked;
} // end canUncheck

// handle the logic for a set of checkboxes where at least one must be checked
function onClickLogic(count, id, idTotal, arrayTotal) {
    if (count == 0) 
        document.getElementById( id ).checked = true;

    calcTotal(idTotal, arrayTotal);
} // end onClickLogic

// calculate the total given a target id (to store the total) and an array of id's for the checkboxes
function calcTotal(id, arrayIDs) {
    var total = 0;
    for (var i = 0; i < arrayIDs.length; ++i) {
        var checkbox = document.getElementById(arrayIDs[i]);
        if (checkbox.checked) {
            total += parseFloat(checkbox.value, name);
        } // end if
    } // end for

    document.getElementById(id).innerHTML = '&pound;' + formatCurrency(total);
} // end calcTotal

// Calculates the total cost of a product based on the cost and quantity values
// and then displays the total in the text area of the control with the specified
// id.
function calcProductTotal(id, costID, quantityID)
{
    // Get cost
    var cost = document.getElementById(costID);
    cost = parseFloat(cost.value);

    // Get quantity textbox
    var quantity = document.getElementById(quantityID);

    // If invalid quantity
    if(isNaN(quantity.value))
    {
        // Set quantity to 1
        quantity.value = 1;
    }
    
    // Get quantity
    quantity = parseInt(quantity.value);

    // Calculate total
    var total = cost * quantity;
    
    // If valid total
    if(!isNaN(total))
    {
        // Format total for currency
        total = formatCurrency(total);
    }
    else
    {
        // Set total to 0
        total = formatCurrency(0);
    }
    
    // Update total
    document.getElementById(id).innerHTML = total;
}

// Calculates the total cost of the basket based on the cost and quantity values
// and then displays the total in the text area of the control with the specified
// id.
function calcBasketTotal(id, arrayCostIDs, arrayQuantityIDs)
{
    // Initialise total to 0
    var total = 0;
    
    // If quantity and cost arrays are of same length
    if(arrayQuantityIDs.length == arrayCostIDs.length)
    {   
        // For all quantity textboxes and cost hidden fields
        for(var i = 0; i < arrayQuantityIDs.length; ++i)
        {
            // Get cost
            var cost = document.getElementById(arrayCostIDs[i]);
            cost = parseFloat(cost.value);
        
            // Get quantity
            var quantity = document.getElementById(arrayQuantityIDs[i]);
            quantity = parseInt(quantity.value);
            
            // If invalid quantity
            if(isNaN(quantity))
            {
                // Set quantity to 0
                quantity = 0;
            }
            
            // Increment total
            total += (quantity * cost);
        }
    }

    // if valid total
    if(!isNaN(total))
    {
        // Format total for currency
        total = formatCurrency(total);
        
        // Update total
        document.getElementById(id).innerHTML = total;
    }
}

// Returns the specified number formatted as currency (i.e. 2 decimal places).
function formatCurrency(number)
{
    // Round number to 2dp
    number = Math.round(number * 100) / 100;
    
    // Get number as string
    number = number.toString();
    
    // Get index of decimal point
    var dotPos = number.indexOf('.', 0);

    // If decimal point is found
    if(dotPos > -1)
    {
        // Get position to end number at
        var endPos = dotPos + 3;
        
        // While end position is longer than number
        while(endPos > number.length)
        {
            // Decrement end position
            endPos--;
        }
    
        // Get digits after decimal point
        var digits = number.substring(dotPos + 1, endPos);
        
        // If just a single digit
        if(digits.length == 1)
        {
            // Append trailing 0
            number += '0';
        }
    }
    // Else no decimal point
    else
    {
        // Append trailing decimal point and places
        number += '.00';
    }
    
    // Return formatted number
    return number;
}

// Increases the current quantity of the specified quantity text box
function increaseQuantity(quantityID)
{
    // Get quantity text box
    var quantityTextBox = document.getElementById(quantityID);
    
    // Get quantity value
    var quantity = parseInt(quantityTextBox.value);
    
    // If quantity is not a number
    if(isNaN(quantity))
    {
        // Set quantity to 0
        quantity = 0;
    }
    
    // Increment quantity
    quantity++;
    
    // Update quantity text box
    quantityTextBox.value = quantity;
    
    // Fire quantity keyup event
    quantityTextBox.onkeyup();
}

// Decreases the current quantity of the specified quantity text box if greater than 0
function decreaseQuantity(quantityID)
{
    // Get quantity text box
    var quantityTextBox = document.getElementById(quantityID);
    
    // Get quantity value
    var quantity = parseInt(quantityTextBox.value);
    
    // If quantity is not a number
    if(isNaN(quantity))
    {
        // Set quantity to 0
        quantity = 0;
    }
    
    // If quantity is greater than 0
    if(quantity > 0)
    {
        // Decrement quantity
        quantity--;
        
        // Update quantity text box
        quantityTextBox.value = quantity;
        
        // Fire quantity keyup event
        quantityTextBox.onkeyup();
    }
}
