/***************************************************************************/
/* Condor Creative - Perth Web Design - http://www.condorcreative.com.au/ */
/**************************************************************************/

// Set global variables
var total_volume;
var time_per_metre = 6;

function calculate() {
    
    // Fix jQuery conflicts
    jQuery.noConflict();
    
    // Set vriable to starting number
    total_volume = 0;
    
    // Loop through all elements with the className "calculate"
    
    jQuery('.calculate').each(function() {
        
        // Set var to current element within the loop
        var currentElement = jQuery(this);
    
        // Get id of current element
        var currentId = currentElement.attr('id');
        
        // Format id of element that contains the volume for this field
        var volumeId = currentId + '_volume';
        
        // Get volume for this item
        var volume = jQuery('#' + volumeId).val();
        
        // Get amount of these items they have
        var items = currentElement.val();
        
        // Parse both these values as an int
        volume = parseFloat(volume);
        items = parseInt(items, 10);
        
        if (!isNaN(items)) {
        
            // Get amount of total volume for this item
            total_item_volume = items * volume;
        
            // Now add the total volume for this field to the total volume for all the fields
            total_volume += total_item_volume;
            
        }
        
    });
    
    // Trim final amount
    total_volume = trim_float(total_volume, 2);
    
    // Now set the value of the estimated volume field to this value
    jQuery('#estimated_volume').val(total_volume);
    
    // Now work out the estimated time needed for this job
    var time_needed = total_volume * time_per_metre;
    
    // Trim final amount
    time_needed = trim_float(time_needed, 2);    
    
    // Format time to hours if needed
    time_needed = convert_minutes(time_needed);
    
    // Now enter time needed into estimated time field
    jQuery('#estimated_time').val(time_needed);
    
}

/* Trims numbers to a specifed amount of decimal places */

function trim_float(num,dec){
    return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
}

/* Turns the passed in amount of minutes into hours if the value is 60 or more */

function convert_minutes(amount) {
    
    amount = parseFloat(amount);
    
    if (amount >= 60) {
        
        // Work out how many hours
        amount /= 60;
        
        // Trim final amount
        amount = trim_float(amount, 2);
        
        if (amount !== 1) {
            // Append hours text to values
            amount = amount + ' Hours';
        } else {
            // Append hours text to values
            amount = amount + ' Hour';
        }
        
    } else {
        
        if (amount !== 1) {
            // Append hours text to values
            amount = amount + ' Minutes';
        } else {
            // Append hours text to values
            amount = amount + ' Minute';
        }
        
    } 
    
    // Return result
    return amount;
    
}
