function calcTotal() {
	var timeslots = document.getElementById("timeslots");
	var rack = document.getElementById("rack");
	var subtotal = document.getElementById("subtotal");
	var discount = document.getElementById("discount");
	var total = document.getElementById("total");
	var grandtotal = document.getElementById("grandtotal");
	var international = document.getElementById("international");
	var item_name_input = document.getElementById("item_name_input");
	var amount_input = document.getElementById("amount_input");
	var quantity_input = document.getElementById("quantity_input");
	var shipping_input = document.getElementById("shipping_input");
	
	
	if (checkValidNumber(timeslots.value)) {
		var intslots = parseInt(document.getElementById('timeslots').value);
		var rackprice;
		var discountperc;
		var item_name;
		switch (document.getElementById('rack').value) {
			case "1": 
				rackprice=8;
				item_name='CCIE4u Rack Rental on Rack 1';
				break;
			case "2": 
				rackprice=8;
				item_name='CCIE4u Rack Rental on Rack 2';
				break;
			case "3": 
				rackprice=8;
				item_name='CCIE4u Rack Rental on Rack 3';
				break;
		}
		
		if (intslots>=5 && intslots<=10) {
			discountperc=10;
			item_name+=' (5-10 timeslots - 10% Discount)';
		} else if (intslots>=11 && intslots<20) {
			discountperc=12;
			item_name+=' (11-19 timeslots - 12% Discount)';
		} else if (intslots>=20 && intslots<30) {
			discountperc=15;
			item_name+=' (20-29 timeslots - 15% Discount)';
		} else if (intslots>=30) {
			discountperc=20;
			item_name+=' (30+ timeslots - 20% Discount)';
		} else {
			discountperc=0;
		}
		
		var subtotalval = intslots * rackprice;
		var perslot = roundNumber(rackprice * (1 - (discountperc/100)))
		var totalval, grandtotalval;
		var internationalfee;
		
		if (international.checked) {
			totalval = subtotalval * (1 - (discountperc/100))
			grandtotalval = (subtotalval * (1 - (discountperc/100))) * 1.03;
			internationalfee = (subtotalval * (1 - (discountperc/100))) * .03
			shipping_input.value = roundNumber(internationalfee);
			item_name+=' 3% International Fee included as Shipping';
		} else {
			grandtotalval = subtotalval * (1 - (discountperc/100));
			shipping_input.value = 0;
		}
		
		discount.innerHTML = 'Discount: '+discountperc+'%';
		subtotal.innerHTML = 'SubTotal: $'+CurrencyFormatted(subtotalval);
		total.innerHTML = 'Total: $'+CurrencyFormatted(totalval);
		internationalDiv.innerHTML = 'International Fee (3%): $'+CurrencyFormatted(internationalfee);
		grandtotal.innerHTML = '<b>Grand Total: $'+CurrencyFormatted(grandtotalval) + '</b>';
		item_name_input.value = item_name;
		amount_input.value = perslot;
		quantity_input.value = intslots;
		
		document.getElementById("subtotalline").style.display="";
		document.getElementById("discline").style.display="";
		document.getElementById("grandtotalline").style.display="";
		document.getElementById("paypalline").style.display="";
		
		if (international.checked) {
			document.getElementById("totalline").style.display="";
			document.getElementById("internationalline").style.display="";	
		} else {
			document.getElementById("totalline").style.display="none";
			document.getElementById("internationalline").style.display="none";	
		}
	} else {
		calcClear();
	}
}


function calcClear() {
	document.getElementById("subtotalline").style.display="none";
	document.getElementById("discline").style.display="none";
	document.getElementById("totalline").style.display="none";	
	document.getElementById("internationalline").style.display="none";	
	document.getElementById("grandtotalline").style.display="none";	
	document.getElementById("paypalline").style.display="none";
	
	document.getElementById("timeslots").value = '';
	document.getElementById("rack").value = '1';
	document.getElementById("subtotal").InnerHTML = '';
	document.getElementById("discount").InnerHTML = '';
	document.getElementById("total").InnerHTML = '';
	document.getElementById("internationalDiv").InnerHTML = '';	
	document.getElementById("grandtotal").InnerHTML = '';
	document.getElementById("item_name_input").value = '';
	document.getElementById("amount_input").value = '';
	document.getElementById("quantity_input").value = '';
	document.getElementById("shipping_input").value = '';
}


function roundNumber(rnum) {
	var rlength = 2; // The number of decimal places to round to
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	return newnumber;
}

function checkValidNumber(val){
var result = null;

// For only positive whole numbers, including zero, uncomment the following:  
result = val.match(/^\d+$/);

// For only positive decimal numbers, including zero, uncomment the following:  
// result = val.match(/^\d+(\.\d+)?$/);

// For positive and negative decimal numbers, including zero, uncomment the following:  
// result = val.match(/^-?\d+(\.\d+)?$/);

// For positive and negative numbers, including zero, uncomment the following:  
// result = val.match(/^-?\d+$/);

return (result != null);
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
// end of function CurrencyFormatted()

