// JavaScript Document
var secArray = new Array('coffee', 'tea', 'cleaning', 'consumable');


function sum(i)
{	
	var totalitems = document.getElementById('totalCount').value;
	sum1= document.getElementById("quantity"+i).value
	sum1= parseInt(sum1);
	sum2= document.getElementById("cost"+i).value
	total=sum2*sum1;
	document.getElementById("sub"+i).value = roundNumber(total);
	var total = 0;
	var tempOrd = '';
	for(var i=0; i < totalitems; i++){
			subtotal = parseFloat(document.getElementById("sub"+i).value);
			tempQ = document.getElementById("quantity"+i).value;
			if(tempQ > 0){
				tempOrd += '<li>'+document.getElementById("type"+i).value+" - "+tempQ+" unit(s) ($"+subtotal+") - <a href='javascript:;' onclick='removeItem("+i+");'>remove</a></li>";
			}
			total += subtotal
	}
	if(total > 0){
		if(total < 100){
			tempOrd += '<li>Freight of $9.50</li>';
			total += 9.5;
		} else {
			tempOrd += '<li>Free Freight</li>';
		}
	}
	document.getElementById("currentOrd").innerHTML = '<strong>Your current order is:</strong><ul>'+tempOrd+'</ul>';
	document.getElementById("total").value = roundNumber(total);
}
function roundNumber(number) {
	var rlength = 2; // The number of decimal places to round to
	var newnumber = Math.round(number*Math.pow(10,rlength))/Math.pow(10,rlength);
	return pad_with_zeros(newnumber, 2)

}
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
}

function checkOrder() {
	if(document.getElementById("total").value == 0.00) {
		alert('You have not selected any items to order. Please select at least 1 item before submitting the form');
		return false;	
	} else {
		return true;
	}
}

function init() {
	sum(0);
}	

function removeItem(id){
	document.getElementById("quantity"+id).selectedIndex = 0;
	sum(id);
}

function showSec(name) {
	document.getElementById(name+'_tag').style.display = 'block';
	var arLen=secArray.length;
	for(i=0; i < arLen; i++){
		if(secArray[i] != name){
			document.getElementById(secArray[i]+'_tag').style.display = 'none';
		}		
	}
}
