$(document).ready(function () {
	$("#toolTable :input").each(function () { $(this).val("0") });
	$(".toolTip").each(function () {
		$(this).mouseenter(function () {
			var alt = $(this).attr("alt");
			$("body").prepend('<div class="tooltip-popup" style="display:none">'+alt+'</div>');
			$(this).mousemove(function(e){
				$(".tooltip-popup").css({left: e.pageX-10, top:e.pageY+20}).show();
			}); 
			$(this).attr("alt", "");
		});
		$(this).mouseout(function () {
			var alt = $(".tooltip-popup").html();
			$(this).attr("alt", alt);
			$(".tooltip-popup").remove();
			$(this).unbind("mousemove");
		});
	});
	
	
});
	
function fieldFocus(field) {
	if ($(field).val() == "0") $(field).val(""); 
}

function newCustomers() {
	var go = true;
	
	var targets = doTheInt("#qTargets");
	var conversion = doTheInt("#cRate", false, true);
	
	if ((targets > 0) && (conversion > 0)) {
		var newCustomers = parseFloat(targets/100);
		newCustomers = newCustomers*parseFloat(conversion);
		newCustomers = Math.round(newCustomers);
		$("#nCustomers").html(newCustomers);
	} else {
		$("#nCustomers").html("0");
	}
	calculateBase();
	
}
	
function keptCustomers() {
	var go = true;
	var eCustomers = doTheInt("#eCustomers");
	var rRate = doTheInt("#rRate", false, true);	
	if ((rRate > 0) && (eCustomers > 0)) {
		var kCustomers = parseFloat(eCustomers)/100;
		kCustomers = kCustomers*parseFloat(rRate);
		kCustomers = Math.round(kCustomers);
		
		$("#kCustomers").html(kCustomers);
	} else {
		$("#kCustomers").html("0");
	}
	calculateBase();
}
	
function calculateBase() {
	var nCustomers = doTheInt("#nCustomers", true);
	var kCustomers = doTheInt("#kCustomers", true);
	var cBase = nCustomers+kCustomers;
	$("#customerBase").html(cBase);
	transactionValue();
	customerLeverage();
}
	
function transactionValue() {
	var cBase = doTheInt("#customerBase", true);
	var price = doTheInt("#price");
	var bundling = doTheInt("#bundling");
	var frequency = doTheInt("#frequency");
	var priceBundle = price+bundling;
	if ((priceBundle > 0) && (frequency > 0)) {
		var transaction = cBase*priceBundle*frequency;
		transaction = addCommas(transaction);
		$("#transactionValue").html(transaction);
	} else {
		$("#transactionValue").html("0");
	}
}
	
function customerLeverage() {
	var cBase = doTheInt("#customerBase", true);
	var crossSell = doTheInt("#crossSell");
	var referrals = doTheInt("#referrals");
	var assets = doTheInt("#assets");
	var working = crossSell+referrals+assets;
	if (working > 0) {
		var leverage = working*cBase;
		leverage = addCommas(leverage);
		$("#customerLeverage").html(leverage);
	} else {
		$("#customerLeverage").html("0");
	}
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.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 doTheInt(field, html, floater) {
	if (html == undefined) html = false;
	if (floater == undefined) floater = false;
	var val;
	if (html == true) {
		val = $(field).html();
	} else {
		val = $(field).val();
	}
	if (floater == false) {
		val = parseInt(val);
	} else {
		val = parseFloat(val);
	}
	if (isNaN(val)) {
		val = 0;
	} 
	if (html == true) {
		$(field).html(val);
	} else {
		$(field).val(val);
	}
	return val;
}
