// Calculator jQuery/JavaScript Document

function detectBrowser()
{
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	if ((browser!="Microsoft Internet Explorer"))
	  {
	  //  Not IE
	  $("#calcborder").css({'width':'500px','height':'300px'});
	  $("#summary .item ").css({'width':'214px'});
	  }
}

	function filllists() {
	
		// Server Packages data
		var servpack = new Array("1 CPU, 0.5 GB Memory, 50 GB Disk Size, $0.05 per hour","1 CPU, 1 GB Memory, 100 GB Disk Size, $0.08 per hour", "2 CPU's, 2 GB Memory, 200 GB Disk Size, $0.16 per hour","4 CPU's, 4 GB Memory, 400 GB Disk Size, $0.32 per hour","8 CPU's, 8 GB Memory, 800 GB Disk Size, $0.64 per hour"); 
						
		var servpack_values = new Array(0.05, 0.08, 0.16, 0.32, 0.64); 

		// Software Template data
		var softtemp = new Array("CentOS 5.3 32-bit",
		"CentOS 5.3 64-bit",
		"CentOS 5.3 64-bit LAMP",
		"CentOS 5.3 64-bit LAMP + GUI",
		"CentOS + enStratus",
		"Debian 5.0 32-bit",
		"Debian 5.0 64-bit",
		"Debian 5.0 64-bit LAMP",
		"Debian 5.0 64-bit LAMP + GUI",
		"Debian 5.0 + enStratus",
		"FreeBSD 8 32-bit",
		"FreeBSD 8 64-bit",
		"Ubuntu 8.04 LTS 32-bit",
		"Ubuntu 8.04 LTS 64-bit",
		"Ubuntu 8.04 LTS 64-bit LAMP", 
		"Ubuntu 8.04 LTS 64-bit LAMP + GUI",
		"Ubuntu 8.04 LTS + enStratus",
		"Ubuntu 9.10 32-bit",
		"Ubuntu 9.10 64-bit",
		"Ubuntu 9.10 64-bit LAMP",
		"Ubuntu 9.10 64-bit LAMP + GUI",
		"Ubuntu 10.04 LTS",
		"Ubuntu 10.04 LTS LAMP",
		"Ubuntu 10.04 LTS LAMP GNOME",
		"Windows Server 2003 32-bit",
		"Windows Server 2003 32-bit with IIS",
		"Windows Server 2003 64-bit IIS and .NET 3.5",
		"Windows Server 2003 64-bit SQL Server 2005",
		"Windows Server 2003 64-bit with IIS",
		"Windows Server 2008 32-bit",
		"Windows Server 2008 64-bit with IIS",
		"Windows Server 2008 64-bit with Plesk 9.2",
		"Windows Server 2008 64-bit SQL 2008 Express",
		"Windows Server 2008 MSSQL Express 2008 DotNet4",
		"Windows Server 2008 R2");
								
		var softtemp_values = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.05,0.05,0.05,0.65,0.05,0.05,0.05,0.05,0.05,0.05,0.05); 
		
		// Fill Server Packages list
		for (i=0;i<=servpack.length-1;i++)
		{
			var optn = document.createElement("OPTION");
			optn.text = servpack[i];
			optn.value = servpack_values[i];
			document.calc_frm.serv_pack.options.add(optn);	
		}

		// Fill Cloud Server list
		for (var i=1;i<=100;i++)
		{
			var optn = document.createElement("OPTION");
			optn.text = i;
			optn.value = i;
					
			document.calc_frm.serv_count.options.add(optn);	
		}

		// Fill Software Template list
		for (i=0;i<=softtemp_values.length-1;i++)
		{
			var optn = document.createElement("OPTION");
			optn.text = softtemp[i];
			optn.value = softtemp_values[i];
			document.calc_frm.stemplate.options.add(optn);	
		}				

		// Fill IP list		
		for (i=2;i<=100;i++)
		{
			var optn = document.createElement("OPTION");
			optn.text = i;
			optn.value = i - 1;
	
			document.calc_frm.ip_list.options.add(optn);	
		}
		
		detectBrowser();
	}

function SetupEvents() {

	$('#serv_pack').change(function () { doCalculate(); });
	$('#serv_pack').keypress(function () { doCalculate(); });
	$('#serv_pack').keyup(function () { doCalculate(); });

	$("input[name='tf']").click(function () { doCalculate(); }); 

	$('#serv_count').change(function () { doCalculate(); });
	$('#serv_count').keypress(function () { doCalculate(); });
	$('#serv_count').keyup(function () { doCalculate(); });

	$('#stemplate').change(function () { doCalculate(); });
	$('#stemplate').keypress(function () { doCalculate(); });
	$('#stemplate').keyup(function () { doCalculate(); });

	$('#txt_bw').change(function () { doCalculate(); });
	$('#txt_bw').keypress(function () { doCalculate(); });
	$('#txt_bw').keyup(function () { doCalculate(); });
	
	$('#ip_list').change(function () { doCalculate(); });
	$('#ip_list').keypress(function () { doCalculate(); });
	$('#ip_list').keyup(function () { doCalculate(); });
}

function doCalculate()
{
	var timeframe = 730;
	var servercount = 0;
	var servercost = 0;
	var templatecost = 0;
	var bandwidthOut = 0;
	var ipcost = 0;
	
	timeframe = parseInt(getTimeFrame());
	servercount = $("#serv_count").val();
	servercost = (parseFloat($("#serv_pack").val()) * timeframe) * servercount;
	templatecost =  (parseFloat($("#stemplate").val()) * timeframe) * servercount;
	ipcost = $("#ip_list").val();
	bandwidthOut = getBandwidthCost( $('#txt_bw').val());
	
	var total_cost = parseFloat(servercost) + parseFloat(templatecost) + parseFloat(ipcost) + 					                     parseFloat(bandwidthOut);
	// Totals
	$('#sv_cost').text(formatMoney(servercost));
	$('#tp_cost').text(formatMoney(templatecost));
	$('#bw_cost').text(formatMoney(bandwidthOut));
	$('#ip_cost').text(formatMoney(ipcost));
	$('#ttl_cost').text(formatMoney(total_cost));
}

function getTimeFrame()
{
	var tmfm = ($("input[name='tf']:checked").val());
	var hrs;
	
	if (tmfm == "hour")
	{
		changeTotalHeaders("1 Hour ");
		hrs = 1;
	}
	else if (tmfm == "day")
	{
		changeTotalHeaders("1 Day ");		
		hrs = 24;
	}
	else if (tmfm == "month")
	{
		changeTotalHeaders("Monthly ");		
		hrs = 730;
	}
		return hrs;
}

function changeTotalHeaders(tf)
{
	$('#txt_servcost').text(tf + "Server Costs:");
	$('#txt_tempcost').text(tf + "Template Costs:");
	$('#txt_bandcost').text(tf + "Bandwidth Costs:");
	$('#txt_ipcost').text(tf + "IP Costs:");
}

function getBandwidthCost(qty)
{
	qty = stripChar(qty);
	$("#bw_cost").text(formatMoney(qty * .12));
	return (qty * .12);
} 

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);
	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}
function formatMoney(number)
{
	if (isNaN(number))
		return "$0.00";
	else
	{
		// adapted from http://www.sonofsofaman.com/hobbies/code/js/formatcurrency.asp
		number = Math.floor(number*100+0.50000000001);
		intCents = number%100;
		strCents = intCents.toString();
		dblValue = Math.floor(number/100).toString();
		if(intCents<10)
			strCents = "0" + strCents
		for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
			dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
			dblValue.substring(dblValue.length-(4*i+3));
		return '$' + dblValue + '.' + strCents;
	}
}
function formatNumber(str)
{
    str += '';
    x = str.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1))
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    return x1 + x2;
}
function stripChar(str)
{
   return str.replace(/[^\d]/g,"");
}

