
$(document).ready(function() {
    GetFacilityTypes();
    GetProducts();

    $(".calcField").format({ precision: 3, allow_negative: false, autofix: true });
    $(selectors.numberOfGrams).format({ precision: 1, allow_negative: false, autofix: true, restrictDecimalNumbers: [0, 5] });

    $(selectors.facilityTypes.dropdown).change(function() {
        var results = jLinq.from(facilityTypes).match("ID", $(this).val()).select();
        var rate = 0;
        if (results.length > 0) {
            rate = results[0].Rate;
        }

        $(selectors.facilityTypes.rate).val(rate);
    });

    $(selectors.products.dropdown).change(function() {
        
        var results = jLinq.from(products).match("ID", $(this).val()).select();
        var price = 0;
        var override = false;
        var physician = 0;
        var outpatient = 0;
        var overrideText = "";
        var phyOverrideText = "";
        if (results.length > 0) {
            price = results[0].Price;
            //alert(price);
            override = results[0].Override;
            physician = results[0].Physician;
            outpatient = results[0].Outpatient;
            overrideText = results[0].OverrideText;
            phyOverrideText = results[0].PhysicianOverrideText;
        }
        $(selectors.products.price).val(price);
        $(selectors.products.override).val(override);
        $(selectors.products.physician).val(physician);
        $(selectors.products.outpatient).val(outpatient);
        $(selectors.products.overrideText).val(overrideText);
        $(selectors.products.physicianOverrideText).val(phyOverrideText);
        
    });

    $(selectors.calculateButton).click(function() {
        Calculate();
        return false;
    });
});

function Calculate() {
    var grams = $(selectors.numberOfGrams).val();
    var costGram = $(selectors.costPerGram).val();
    var facilityRate = $(selectors.facilityTypes.rate).val();
    var productPrice = $(selectors.products.price).val();
    var productOverride = $(selectors.products.override).val();
    var productPhysicianValue = $(selectors.products.physician).val();
    var productOutpatientValue = $(selectors.products.outpatient).val();
    var productOverrideText = $(selectors.products.overrideText).val();
    var physicianOverrideText = $(selectors.products.physicianOverrideText).val();

    //Clear totals
    $(selectors.totalReimbursement).val('');
    $(selectors.totalProfitLoss).val('');
    $(selectors.overrideMessage).hide();
    $(selectors.overrideMessage).text('');
    
    if (facilityRate.length == 0 || facilityRate == "0") {
        alert("You must select a Facility");
        return false;
    }

    if (productPrice.length == 0 || productPrice == "0") {
        alert("You must select a Product");
        return false;
    }

    var totalCostProduct, totalCostReimb, totalProfitLoss, reimbRate;
    
    if (grams.length > 0 && costGram.length > 0) {
        totalCostProduct = parseFloat(grams) * parseFloat(costGram);
        $(selectors.totalCostProduct).val(totalCostProduct);
        $(selectors.totalCostProduct).format2({ format: "#,###.000" });

        if (productOverride == "true") {
            var isOutpatient = false;
            var type = $(selectors.facilityTypes.dropdown + " option:selected").text();
            if (type.indexOf("Outpatient") >= 0)
            {
                isOutpatient = true;
                reimbRate = productOutpatientValue;
            }
            else
            {
                reimbRate = productPhysicianValue;
            }
            totalCostReimb = parseFloat(grams) * parseFloat(reimbRate);
            totalProfitLoss = totalCostReimb - totalCostProduct;
            $(selectors.totalReimbursement).val(totalCostReimb);
            $(selectors.totalProfitLoss).val(totalProfitLoss);

            $(selectors.totalReimbursement).format2({ format: "#,###.000" });
            $(selectors.totalProfitLoss).format2({ format: "#,###.000" });

            if (isOutpatient && productOverrideText.length > 0) {
                $(selectors.overrideMessage).show();
                $(selectors.overrideMessage).text(productOverrideText);
            }

            if (!isOutpatient && physicianOverrideText.length > 0) {
                $(selectors.overrideMessage).show();
                $(selectors.overrideMessage).text(physicianOverrideText);
            }
        }
        else if (facilityRate.length > 0 && productPrice.length > 0) {
            reimbRate = parseFloat(facilityRate) * parseFloat(productPrice);
            totalCostReimb = parseFloat(grams) * parseFloat(reimbRate);
            totalProfitLoss = totalCostReimb - totalCostProduct;
            $(selectors.totalReimbursement).val(totalCostReimb);
            $(selectors.totalProfitLoss).val(totalProfitLoss);

            $(selectors.totalReimbursement).format2({ format: "#,###.000" });
            $(selectors.totalProfitLoss).format2({ format: "#,###.000" });
        }
    }

}

function GetFacilityTypes()
{
    $.ajax({
    type: "POST",
    url: "IVIGCalculator.aspx/GetFacilityTypesJSON",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        facilityTypes = (new Function("return " + msg.d))();
    }
    });
}

function GetProducts() {
    $.ajax({
        type: "POST",
        url: "IVIGCalculator.aspx/GetProductsJSON",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            products = (new Function("return " + msg.d))();
        }
    });
}


