﻿
function submitBenefitChange(controller) {

	actionUrl = "/" + controller + "/PersistBenefitChange";

	// persist change  
	$.post(actionUrl, $("form").serialize(), function (data) {
		// update current totals
	    $("#RunningTotals").html(data.RunningTotals);

        // update running totals                
        $("#BenefitRates").html(data.BenefitRates);
    });
}     

function UpdateSelectList($list, data) {
	// Get Current Value
	var currentValue = $list.val();

	// IE HACK, if you modify the options IE looses the
	// width.
	var width = $list.width();

	// Clear out the current dropdown value
	$list.empty();

	// Rebuild the dropdown with the new values
	var previousExists;
	$.each(data, function (index, optionData) {
	    $list.append(
				$("<option>")
				.attr("value", optionData.Value)
				.text(optionData.Text));
	    previousExists = previousExists || optionData.Value == currentValue;
	});

	// set the value to what it was before the rebuild
	if (previousExists) {
	    $list.val(currentValue);
	}
	$list.width(width);
}

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 ToggleInputBenefitOptionControls(inputName) {
    $("input[name='" + inputName + "']:checked").each(function () {
        $(this).parents(".benefit-option:first").find("select").attr("disabled", "");
    });
    $("input[name='" + inputName + "']:not(:checked)").each(function () {
        $(this).parents(".benefit-option:first").find("select").attr("disabled", "disabled");
    });
}

function ToggleDialogButtonEnable(dialogSelector, buttonName, setEnabled) {
    var buttons = $(dialogSelector + ' .ui-dialog-buttonpane button');
    for (var i = 0; i < buttons.length; ++i) {
        var jButton = $(buttons[i]);
        if (jButton.text() == buttonName) {
            if (setEnabled) {
                jButton.attr('disabled', '').removeClass('ui-state-disabled');
            }
            else {
                jButton.attr('disabled', 'disabled').addClass('ui-state-disabled');
            }
        }
    }
}

