// easy function for formatting a number with commas
$.fn.digits = function(){ 
    return this.each(function(){ 
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
}

// estimate the monthly balance when the page is first loaded
// (for reloads of the page where values are specified)
$(document).ready(function() {
	
	/*
	 * add function calls to the calculator form fields for instance
	 * re-calculation.
	 */

	$('[name=c1_comp_hours]').keyup(function() {
		estimate_monthly_balance();
	});
	
	$('[name=c2_comp_hours]').keyup(function() {
		estimate_monthly_balance();
	});
	
	$('[name=m1_comp_hours]').keyup(function() {
		estimate_monthly_balance();
	});
	
	$('[name=s1_comp_hours]').keyup(function() {
		estimate_monthly_balance();
	});
	
	$('[name=c1_realtime_hours]').keyup(function() {
		estimate_monthly_balance();
	});
	
	$('[name=c2_realtime_hours]').keyup(function() {
		estimate_monthly_balance();
	});
	
	$('[name=m1_realtime_hours]').keyup(function() {
		estimate_monthly_balance();
	});
	
	$('[name=s1_realtime_hours]').keyup(function() {
		estimate_monthly_balance();
	});
	
	$('[name=storage_amt]').keyup(function() {
		estimate_monthly_balance();
	});

	$('[name=data_in]').keyup(function() {
		estimate_monthly_balance();
	});

	$('[name=data_out]').keyup(function() {
		estimate_monthly_balance();
	});

	$('[name=support_plan]').change(function() {
		estimate_monthly_balance();
	});
	
	$('#example_1_calc_link').click(function() {
		example1_calculator();
	});
	
	$('#example_2_calc_link').click(function() {
		example2_calculator();
	});
	
	$('#example_3_calc_link').click(function() {
		example3_calculator();
	});
	
	estimate_monthly_balance();
});



/*
 * Current Rates
 */


var core_rate = {
		'c1': 0.05,
		'c2': 0.13,
		'm1': 0.35,
		's1': 0.04,
}

var core_realtime_rate = {
		'c1': 0.015,
		'c2': 0.038,
		'm1': 0.105,
		's1': 0.01,
}

var storage_tiers = {
		5000000: 0.065,
		1000000: 0.90,
		500000: 0.115,
		100000: 0.140,
		50000: 0.150,
		0: 0.160,
}

var data_in_rate = 0.0;
var data_out_tiers = {
		150000: 0.09,
		50000: 0.10,
		10000: 0.12,
		0: 0.16,
}

var support_plan_tiers = {
		'Basic': 0.0,
		'Silver': 50.0,
		'Gold': 150.0,
		'Platinum': 400.0,
}

/*
 * Calculates the cost of using a resource that has prices
 * by tiers.
 */
function calculate_tier_cost(tiers, amount) {
	
	var total = 0.0;
	
	for(key in tiers) {
	
		if(amount > key) {
			total += (amount - key) * tiers[key];
			amount = key;
		}
	}
	
	return total;
}

function reset_calculator() {
	$('[name=c1_comp_hours]').val('0');
	$('[name=c2_comp_hours]').val('0');
	$('[name=m1_comp_hours]').val('0');
	$('[name=s1_comp_hours]').val('0');
	$('[name=c1_realtime_hours]').val('0');
	$('[name=c2_realtime_hours]').val('0');
	$('[name=m1_realtime_hours]').val('0');
	$('[name=s1_realtime_hours]').val('0');
	$('[name=storage_amt]').val('0');
	$('[name=data_in]').val('0');
	$('[name=data_out]').val('0');
	$('[name=support_plan]').val('Basic');
	estimate_monthly_balance();
}

function example1_calculator() {
	reset_calculator();
	$('[name=c1_comp_hours]').val('2.5');
	estimate_monthly_balance();
}

function example2_calculator() {
	reset_calculator();
	$('[name=c1_comp_hours]').val('2.5');
	$('[name=c1_realtime_hours]').val('20');
	estimate_monthly_balance();
}

function estimate_monthly_balance() {
	/*
	 * Function to estimate the user's monthly balance with the html form fields
	 * they have specified. Automatically updates the #monthly_calculator_estimate
	 * element.
	 */
	
	var total = 0.0;
	
	var c1_comp_hours = parseFloat($('[name=c1_comp_hours]').val());
	var c2_comp_hours = parseFloat($('[name=c2_comp_hours]').val());
	var m1_comp_hours = parseFloat($('[name=m1_comp_hours]').val());
	var s1_comp_hours = parseFloat($('[name=s1_comp_hours]').val());
	var c1_realtime_hours = Math.ceil(parseFloat($('[name=c1_realtime_hours]').val()));
	var c2_realtime_hours = Math.ceil(parseFloat($('[name=c2_realtime_hours]').val()));
	var m1_realtime_hours = Math.ceil(parseFloat($('[name=m1_realtime_hours]').val()));
	var s1_realtime_hours = Math.ceil(parseFloat($('[name=s1_realtime_hours]').val()));
	var storage_amt = parseFloat($('[name=storage_amt]').val());
	var data_in = parseFloat($('[name=data_in]').val());
	var data_out = parseFloat($('[name=data_out]').val());
	var support_plan = $('[name=support_plan]').val();
	
	var reserved_hours = parseFloat($('[name=reserved_hours]').val());
	
	var standard_hours = 0.0;
	var used_reserved_hours = 0.0;
	
	
	if(c1_comp_hours) {
		total += c1_comp_hours * core_rate['c1'];
	}
	
	if(c2_comp_hours) {
		total += c2_comp_hours * core_rate['c2'];
	}
	
	if(m1_comp_hours) {
		total += m1_comp_hours * core_rate['m1'];
	}
	
	if(s1_comp_hours) {
		total += s1_comp_hours * core_rate['s1'];
	}

	if(c1_realtime_hours) {
		total += c1_realtime_hours * core_realtime_rate['c1'];
	}
	
	if(c2_realtime_hours) {
		total += c2_realtime_hours * core_realtime_rate['c2'];
	}
	
	if(m1_realtime_hours) {
		total += m1_realtime_hours * core_realtime_rate['m1'];
	}
	
	if(s1_realtime_hours) {
		total += s1_realtime_hours * core_realtime_rate['s1'];
	}
	
	if(storage_amt) {
		total += calculate_tier_cost(storage_tiers, storage_amt);
	}
	
	if(data_in) {
		total += data_in * data_in_rate;
	}
	
	if(data_out) {
		total += calculate_tier_cost(data_out_tiers, data_out);
	}
	
	if(support_plan) {
		total += support_plan_tiers[support_plan];
	}
	
	// catch if they are paying over single digit millions per month, and replace estimate with "Too much"
	// that way the dollars don't overflow the div. plus it's just silly to get that high w/o massive
	// inflation...
	if(('$'+total.toFixed(2)).length > 11) {
		$('#monthly_calculator_estimate').text('Too much');
	} else {
		$('#monthly_calculator_estimate').text('$'+total.toFixed(2));
		$('#monthly_calculator_estimate').digits();
	}
	
}

