// JavaScript Document
function round(number, X) 
	{
		// rounds number to X decimal places, defaults to 2
		X = (!X ? 2 : X);
		return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
	}
	
	function formatCurrency(num) 
	{
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
			num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
			cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	}

	
	function onCalcuate() 
	{ 
		var nUserCount = document.form1.UserCount.value;
		var nCallsPerMonth = document.form1.CallsPerMonth.value;
		var nCallDuration = document.form1.CallDuration.value;
		var nHelpDeskHourlyRate = document.form1.HelpDeskHourlyRate.value;
		var nStaffHourlyRate = document.form1.StaffHourlyRate.value;
		var nResetCallsPercent = document.form1.ResetCallsPercent.value / 100;
		var nUtilizationPercent = document.form1.UtilizationPercent.value / 100;
		
		var nMinutesTotal = nUserCount * nCallDuration * (nCallsPerMonth * nResetCallsPercent * nUtilizationPercent);

		var nStaffCost = (nMinutesTotal / 60) * nStaffHourlyRate;
		var nHelpDeskCost = (nMinutesTotal / 60) * nHelpDeskHourlyRate;
		var nTotalCost = nStaffCost + nHelpDeskCost;
		
		var nPurchaseCost;
		var nPrice = 10;
		
		
		// 0001 - 2999 = 10
		// 3000 - 3999 = 9.5
		// 4000 - 4999 = 9
		// 5000 - 5999 = 8.5
		// 6000 - 6999 = 8 
		// 7000 - 7999 = 7.5
		// 8000 - 8999 = 7
		// 9000 - 9999 = 6.5
		// 10000       = 6
		
		if (nUserCount > 10000)
			nPrice = 7;
		else if (nUserCount > 8999)
			nPrice = 8;
		else if (nUserCount > 7999)
			nPrice = 9;
		else if (nUserCount > 6999)
			nPrice = 10;
		else if (nUserCount > 5999)
			nPrice = 11;
		else if (nUserCount > 4999)
			nPrice = 12;
		else if (nUserCount > 3999)
			nPrice = 13;
		else if (nUserCount > 2999)
			nPrice = 14;
		else
			nPrice = 15;
			
		nPurchaseCost = nUserCount * nPrice;
		var nMaintenance = .3;
		
		var ROI = (nPurchaseCost + (nPurchaseCost * (1 * nMaintenance))) / nTotalCost;
		if (ROI > 12)
			ROI = (nPurchaseCost + (nPurchaseCost * (2 * nMaintenance))) / nTotalCost;
		if (ROI > 24)
			ROI = (nPurchaseCost + (nPurchaseCost * (3 * nMaintenance))) / nTotalCost;


		document.form1.roi.value = round(ROI, 1);
		document.form1.SavingsYear1.value = formatCurrency((12 * nTotalCost) - (nPurchaseCost + (nPurchaseCost * nMaintenance)));
		document.form1.SavingsYear3.value = formatCurrency((36 * nTotalCost) - (nPurchaseCost + (nPurchaseCost * (3 * nMaintenance))));

	} 