// function add_to_cart(item_number)
//
//     item_number         A string containsing the number that uniquely identifies the item.
//     redirect_page       What web page to goto next
//     qty                 The quantity of the product to add
//
// This function adds an item to the user's shopping cart.
function add_to_cart(item_number, redirect_page, qty) {
 

    //alert("Add to cart: qty = " + qty);
    // Loop through the database_records array
    for (var counter = 0; counter < database_records.length; counter++) {
  
        // Is this the item number that's being ordered?
        if (database_records[counter].number == item_number) {

            // If yes, get the item's current order quantity
            var current_quantity = database_records[counter].quantity;
            
            // is the value a number?
            if(its_a_number(qty) == false)
            {
                alert("Please enter a valid number.");                
                return;
            }
            
            // Is the value empty or negative?
            if (its_empty(qty) || parseInt(qty) < 0) {
                
                // If so, warn the user, set the focus(), and return
                alert("Please enter a quantity greater than or equal to 0.");                
                return;
            }
            
            // Is the value a floating-point number?
            if (its_floating_point(qty)) {
                
                // If so, warn the user and return
                alert("Please enter an integer quantity.");
                return;
            }
            
            
            //alert("Add to cart: current_quantity = " + current_quantity);
            var new_total = parseInt(current_quantity);
            new_total += parseInt(qty);
            if (qty != 0) {
                
                // If not, set the quantity to qty to add the item to the cart
                update_record_quantity(counter, new_total);
            }

            // Exit the loop
            break
        }
    }

    // Save the orders to the cookie
    save_orders();

    // Take the user to the redirect page unless it is null cart page
    if(redirect_page != "null")
          location = redirect_page;
}

// function update_item_quantity(record_index, record_quantity)
//
//     record_index       The record's index within the database_records array.
//     record_quantity    The quantity ordered.
//
// This function updates a record's order quantity to record_quantity.

function update_record_quantity(record_index, record_quantity) {

    // Update the quantity
    database_records[record_index].quantity = record_quantity;

    // Is the new quantity 0?
    if (record_quantity == 0) {
                
        // If so, decrement the total number of items ordered
        // Note: This removes the item from the shopping cart
        if(total_ordered > 0)
            total_ordered--;
    }
}

// function recalculate_order()
//
// This function recalculates the shopping cart totals.

function recalculate_order(showAlert) {

     if(showAlert == 1)
     {
          alert("Recalculating order with new quantities.");
     }
     
    // Loop through the fields in the shopping cart form
    for (var counter = 0; counter < document.shopping_cart.length; counter++) {
        
        // Only work with the "Quantity" text fields
        if (document.shopping_cart[counter].type == "text") {
            
            
            current_quantity = document.shopping_cart[counter].value;
            
            // is the value a number?
            if(its_a_number(current_quantity) == false)
            {
                alert("Please enter a valid number.");                
                return;
            }
            
            // Is the value empty or negative?
            if (its_empty(current_quantity) || parseInt(current_quantity) < 0) {
                
                // If so, warn the user, set the focus(), and return
                alert("Please enter a quantity greater than or equal to 0.");
                document.shopping_cart[counter].focus();
                return
            }
            
            // Is the value a floating-point number?
            if (its_floating_point(current_quantity)) {
                
                // If so, warn the user and return
                alert("Please enter an integer quantity.");
                document.shopping_cart[counter].focus();
                return
            }
            
                       
            
            // Otherwise, the quantity is valid, so update it
            var current_index = document.shopping_cart[counter].record_index;
            update_record_quantity(current_index, current_quantity);
        }
    }

    // Save the updated orders to the cookie
    save_orders();
            
    // Reload the page
    location.reload();
}

// function remove_from_cart(record_index)
//
//     record_index    The record's index within the database_records array.
//
// This function removes the item specified by record_index.

function remove_from_cart(item_number) {

    //alert("Remove from cart: item_number = " + item_number);
    
    // Loop through the database_records array
    for (var counter = 0; counter < database_records.length; counter++) {
  
        // Is this the item number that's being deleted?
        if (database_records[counter].number == item_number) {

            // If yes, get the item's current order quantity
            var current_quantity = database_records[counter].quantity;
            
            //alert("Remove from cart: current_quantity = " + current_quantity);
            
            if(current_quantity > 0)
            {
            
                //var new_total = parseInt(current_quantity);
                //new_total--;
                
                // If not, set the quantity to qty to add the item to the cart
                update_record_quantity(counter, 0);
                
            }

            // Exit the loop
            break
        }
    }

    // Save the orders to the cookie
    save_orders();

    // Reload the page
    location.reload();
}

// function empty_cart()
//
// This function deletes everything in the shopping
// cart by deleting the shopping_cart cookie.

function empty_cart() {
    
    // Loop through the database_records array
    for (var counter = 0; counter < database_records.length; counter++) {
  
        // If so, set the item's quantity to 0
        update_record_quantity(counter, 0);
    }

    // Save the updated database to the cookie
    save_orders();

    // Reload the page
    location.reload();
}

function empty_cart_no_reload()
{
     // Loop through the database_records array
    for (var counter = 0; counter < database_records.length; counter++) {
  
        // If so, set the item's quantity to 0
        update_record_quantity(counter, 0);
    }

    // Save the updated database to the cookie
    save_orders();    
}



// function round_decimals(original_number, decimals)
//
//     original_number    A floating-point number.
//     decimals           The number of decimal places.
//
// Returns: the original_number rounded to decimals decimal places.

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return pad_with_zeros(result3, decimals);
}

// function pad_with_zeros(rounded_value, decimal_places)
//
//     rounded_value    A rounded floating-point number.
//     decimals         The number of decimal places.
//
// Returns: the rounded_number padded with extra zeros 
// to get the specified number of decimal places.

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString();
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".");

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0;
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : "";
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1;
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0";
        }
    return value_string;
}

// this function will figure out the unit price based on the item and quantity
// being purchased
// db_index is the index into our array of item structures (begins with 0)
// item_quantity is the number being purchased
function get_item_price(db_index, item_quantity){

    // Get the price and calculate the item's total price (quantity * price)
    item_number = db_index+1;

    if (item_number == 37 || item_number == 40 || item_number == 45) {
        if (item_quantity < 10) {
            item_price = database_records[db_index].price
        }
        else if (item_quantity < 25) {
            item_price = database_records[db_index].price * 0.95
        }
        else if (item_quantity < 50) {
            item_price = database_records[db_index].price * 0.90
        }
        else{
            item_price = database_records[db_index].price * 0.85
        }
    }
    else {

        if (item_quantity < 10 || (item_number >= 22 && item_number <= 26) || (item_number == 30) || (item_number >= 33 && item_number <= 36) || (item_number >= 41 && item_number <= 43) || (item_number == 48)) {
            item_price = database_records[db_index].price
        }
        else if (item_quantity < 25) {
            item_price = database_records[db_index].price * 0.95
        }
        else if (item_quantity < 50) {
            item_price = database_records[db_index].price * 0.90
        }
        else if (item_quantity < 100) {
            item_price = database_records[db_index].price * 0.85
        }
        else {
            item_price = database_records[db_index].price * 0.80
        }
    }
    
    item_price = round_decimals(item_price, 3);    
    return round_decimals(item_price, 2);    
} 

function get_total_item_count()
{
    var total = 0;
    
    //alert ("database_record.len = " + database_records.length);
    // Loop through the database_records array
    for (var counter = 0; counter < database_records.length; counter++) {
        
        if(parseInt(database_records[counter].quantity) > 0)
        	total++;
        
    }
    
    return total;
}

function get_total_price()
{
    var total = 0;
    var qty=0;
    var sub_total = 0;
    
    // Loop through the database_records array
    for (var counter = 0; counter < database_records.length; counter++) {
        
        qty = database_records[counter].quantity;
        sub_total = 0;
        
        if(qty > 0)
        {
            sub_total = parseFloat(get_item_price(counter, qty))*parseInt(qty);
             
            total += parseFloat(sub_total);   
        }
        
    }
    
    return total;
}

