﻿var product;
var productInputPrefix;
var tierCount;
var addonCount;
var appPath = "";
var defaultSelectOption = '<option value="0">Please Select an Option</option>';
var tierPath = "";
var addons = "";

$(document).ready(function() {

    if (document.getElementById("Product") != null) {
        product = $("#Product").val();
        tierCount = $("#TierCount").val();
        addonCount = $("#AddonCount").val();
        
        productInputPrefix = product.replace(/ /g, '');

        if (product != null) {
            // Disable all tiers except the first
            ResetTiers(2);
        }
    }
});

function SelectListChanged(tier, selectId) {

    if (_gaq != null) {
        _gaq.push(['_trackEvent', 'PrintCalculator', product, 'tier ' + tier]);
    }
    
    var selectedValue = $("#" + selectId).val();
    
    ResetTiers(parseInt(tier) + 1);
    BuildTierSelectionPath(parseInt(tier));
    BuildAddons();
    
    var requestString = appPath + "/printing/productdata?Tier=" + tier + "&SelectedValue=" + selectedValue + "&Product=" + product + "&tierPath=" + tierPath + "&addons=" + addons;

    $.post(requestString, "", function(data) {

        switch (data.Command) {
            case "UpdateTier":
                UpdateTier(data.Data, parseInt(tier) + 1);
                break;
            case "UpdatePrice":
                UpdatePrice(data.Data);
                break;
        }
    }, "json");
}

function GetUpdatedPrice() {
    var topTierVal  = $("#" + productInputPrefix + tierCount).val();

    if (topTierVal != 0) {
        BuildTierSelectionPath(parseInt(tierCount));
        BuildAddons();

        var requestString = appPath + "/printing/productdata?Tier=" + tierCount + "&Product=" + product + "&tierPath=" + tierPath + "&addons=" + addons;

        $.post(requestString, "", function(data) {

            switch (data.Command) {
                case "UpdatePrice":
                    UpdatePrice(data.Data);
                    break;
            }
        }, "json");
    }
    
}

function UpdateTier(data, tier) {
    var selectId = productInputPrefix + tier;

    // Enable this tier
    $("#" + selectId).attr('disabled', '');

    // Update the select options
    var options = defaultSelectOption;
    for (o in data) {
        options += '<option value="' + data[o].key + '">' + data[o].value + '</option>';
    }

    $("#" + selectId).html(options);
}

function UpdatePrice(data) {

    $("#printDesignPrice").html(data.combinedPrice);
    $("#printPrice").html(data.printPrice);
    $("#productSummary").show();
}


// Resets tiers starting at the supplied tier
function ResetTiers(tier) {
    $("#productPrice").html("");
    $("#productSummary").hide();
    for (i = tier; i <= tierCount; i++) {
        $("#" + productInputPrefix + i).html(defaultSelectOption);
        $("#" + productInputPrefix + i).attr('disabled', 'disabled');
    }
}

// Builds the TierSelectionPath up to the supplied tier
function BuildTierSelectionPath(tier) {
    tierPath = "";
    for (i = 1; i <= parseInt(tier); i++) {
        var selectedVal = $("#" + productInputPrefix + i).val();
        tierPath = tierPath + i + "." + selectedVal + "|";
    }
}

function BuildAddons() {
    addons = "";
    for (i = 1; i <= parseInt(addonCount); i++) {
        var val = $("#" + productInputPrefix + "_addon" + i).is(':checked');
        if (val == true) {
            val = 1;
        } else {
            val = 0;
        }
        addons = addons + i + "." + val + "|";
    }

}