//
//Copyright AddEight 2011
//AJS 2011.03.21

$(document).ready(function () {
	// Handler for .ready() called.
	$("#txtHpvNumDowntimes").blur(calculateHourlyProductionValue);
	$("#txtHpvNumDowntimes").keydown(maskInput);
	$("#txtHpvDowntimeLength").blur(calculateHourlyProductionValue);
	$("#txtHpvDowntimeLength").keydown(maskInput);
	$("#txtHpvHourlyProductionValue").blur(calculateHourlyProductionValue);
	$("#txtHpvHourlyProductionValue").keydown(maskInput);

	$("#txtSriExistingScrapRate").blur(calculateScrapRateImprovement);
	$("#txtSriExistingScrapRate").keydown(maskInputWithDecimal);
	$("#txtSriNewScrapRate").blur(calculateScrapRateImprovement);
	$("#txtSriNewScrapRate").keydown(maskInputWithDecimal);
	$("#txtSriNumProductsPerYear").blur(calculateScrapRateImprovement);
	$("#txtSriNumProductsPerYear").keydown(maskInput);
	$("#txtSriMfgCostPerPart").blur(calculateScrapRateImprovement);
	$("#txtSriMfgCostPerPart").keydown(maskInputWithDecimal);
	
	$("#txtLcciExistingInventory").blur(calculateLoweredCostOfCarryingInventory);
	$("#txtLcciExistingInventory").keydown(maskInput);
	$("#txtLcciDecrease").blur(calculateLoweredCostOfCarryingInventory);
	$("#txtLcciDecrease").keydown(maskInput);
});

function calculateHourlyProductionValue() {
	var numDowntimes = parseFloat($("#txtHpvNumDowntimes").val());
	var downtimeLength = parseFloat($("#txtHpvDowntimeLength").val());
	var hourlyProductionValue = parseFloat($("#txtHpvHourlyProductionValue").val());

	var total = numDowntimes * (downtimeLength / 60) * hourlyProductionValue;

	if (!isNaN(total))
		$("#txtHpvTotal").val("$" + total.toFixed(2));
	else
		$("#txtHpvTotal").val("");
}

function calculateScrapRateImprovement() {
	var existingScrapRate = parseFloat($("#txtSriExistingScrapRate").val());
	var newScrapRate = parseFloat($("#txtSriNewScrapRate").val());
	var numProductsPerYear = parseFloat($("#txtSriNumProductsPerYear").val());
	var mfgCostPerPart = parseFloat($("#txtSriMfgCostPerPart").val());

	var total = ((numProductsPerYear * (existingScrapRate / 100)) - (numProductsPerYear * (newScrapRate / 100))) * mfgCostPerPart;

	if (!isNaN(total))
		$("#txtSriTotal").val("$" + total.toFixed(2));
	else
		$("#txtSriTotal").val("");
}

function calculateLoweredCostOfCarryingInventory() {
	var existingInventory = parseFloat($("#txtLcciExistingInventory").val());
	var decrease = parseFloat($("#txtLcciDecrease").val());

	var total = (existingInventory * (decrease / 100) * .25);

	if (!isNaN(total))
		$("#txtLcciTotal").val("$" + total.toFixed(2));
	else
		$("#txtLcciTotal").val("");
}

function maskInput() {
	var key_code = window.event.keyCode;
	var oElement = window.event.srcElement;
	if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
		if ((key_code > 47 && key_code < 58) || (key_code > 95 && key_code < 106)) {
			if (key_code > 95)
				key_code -= (95 - 47);
			oElement.value = oElement.value;
		} else if (key_code == 8) {
			oElement.value = oElement.value;
		} else if (key_code != 9 && key_code != 16 && key_code != 46 && key_code != 37 && key_code != 39) {
			event.returnValue = false;
		}
	}
}

function maskInputWithDecimal() {
//exceptions=[8,46,37,39,13,9];   // backspace, delete, arrowleft & right, enter, tab
	var key_code = window.event.keyCode;
	var oElement = window.event.srcElement;
	if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
		if ((key_code > 47 && key_code < 58) || (key_code > 95 && key_code < 106)) {
			if (key_code > 95)
				key_code -= (95 - 47);
			oElement.value = oElement.value;
		} else if (key_code == 8) {
			oElement.value = oElement.value;
		} else if (key_code != 9 && key_code != 16 && key_code != 46 && key_code != 37 && key_code != 39 && key_code != 110 && key_code != 190) {
			event.returnValue = false;
		}
	}
}
