﻿var LEVEL_1_DESCRIPTION = 0;
var LEVEL_2_DESCRIPTION = 1;
var LEVEL_3_DESCRIPTION = 2;
var ITEM_PRICE = 3;
var ITEM_OFFER_PRICE = 4;
var ITEM_AVAILABLE_INVENTORY = 5;
var STOCK_STATUS_MESSAGE = 6;
var PRODUCT_IMAGE = 7;

function handleStyleDropDownChange(element)
{

    var edpNo = parseInt(element.attr("edpno"), 10);
    var fieldSuffix = element.attr("suffix");
    var showPrice = false;

    if ($(this).attr("showprice") == "True") showPrice = true;

    var level = parseInt(element.attr("level"), 10);
    var levelCount = parseInt(element.attr("levelcount"), 10);
    var thisVal = element.val();

    var level1Choice = "", level2Choice = "", level3Choice = "";

    var level1id = "#l1desc" + fieldSuffix;
    var level2id = "#l2desc" + fieldSuffix;
    var level3id = "#l3desc" + fieldSuffix;

    var pricingId = "";
    var availabilityId = "";
    var imageId = "";

    if ($(this).attr("pricingid") != "") pricingId = "#" + element.attr("pricingid");
    if ($(this).attr("availabilityid") != "") availabilityId = "#" + element.attr("availabilityid");
    if ($(this).attr("imageid") != "") imageId = "#" + element.attr("imageid");

    level1Choice = $(level1id).val();

    if (levelCount >= 2) level2Choice = $(level2id).val();
    if (levelCount == 3) level3Choice = $(level3id).val();

    if (levelCount > 1) {

        if (level == 1) {
            $(level2id).removeOption(/./);
            PopulateDropDown(edpNo, level2id, level1Choice, LEVEL_1_DESCRIPTION, 2, LEVEL_2_DESCRIPTION, level2Choice, showPrice, levelCount);
            if (levelCount == 3) {
                $(level3id).removeOption(/./);
                PopulateDropDown(edpNo, level3id, level1Choice, LEVEL_1_DESCRIPTION, 3, LEVEL_3_DESCRIPTION, level3Choice, showPrice, levelCount);
            }
        }

        if (level == 2) {
            $(level1id).removeOption(/./);
            PopulateDropDown(edpNo, level1id, level2Choice, LEVEL_2_DESCRIPTION, 1, LEVEL_1_DESCRIPTION, level1Choice, showPrice, levelCount);
            if (levelCount == 3) {
                $(level3id).removeOption(/./);
                PopulateDropDown(edpNo, level3id, level2Choice, LEVEL_2_DESCRIPTION, 3, LEVEL_3_DESCRIPTION, level3Choice, showPrice, levelCount);
            }
        }

        if (level == 3) {
            $(level1id).removeOption(/./);
            PopulateDropDown(edpNo, level1id, level3Choice, LEVEL_3_DESCRIPTION, 1, LEVEL_1_DESCRIPTION, level1Choice, showPrice, levelCount);
            $(level2id).removeOption(/./);
            PopulateDropDown(edpNo, level2id, level3Choice, LEVEL_3_DESCRIPTION, 2, LEVEL_2_DESCRIPTION, level2Choice, showPrice, levelCount);
        }
    }

    level1Choice = $(level1id).val();

    if (levelCount >= 2) level2Choice = $(level2id).val();
    if (levelCount == 3) level3Choice = $(level3id).val();

    $(".swatch").each(function() {        
        if ($("#" + this.id).attr("styleselection") == thisVal) {
            if ($("#" + this.id).attr("image") != "") {
                $(imageId).attr("src", $("#" + this.id).attr("image"));
            }
        }
    });

    UpdatePricingAndAvailability(edpNo, level1Choice, level2Choice, level3Choice, pricingId, availabilityId);

    UpdateProductImage(edpNo, level1Choice, level2Choice, level3Choice, imageId);

}


function UpdateProductImage(edpNo, level1Choice, level2Choice, level3Choice, imageId) {
    if (imageId != "#") {
        if ($(imageId).length > 0) {
            for (i = 0; i < styleProducts[edpNo].length; i++) {
                if (styleProducts[edpNo][i][LEVEL_1_DESCRIPTION] == level1Choice && styleProducts[edpNo][i][LEVEL_2_DESCRIPTION] == level2Choice && styleProducts[edpNo][i][LEVEL_3_DESCRIPTION] == level3Choice) {
                    if (styleProducts[edpNo][i][PRODUCT_IMAGE].indexOf("noimage") < 0) {
                        $(imageId).attr("src", styleProducts[edpNo][i][PRODUCT_IMAGE]);
                    }
                }
            }
        }
    }
}


function InitStyleDropDowns(id, edpNo) {
    $('option', $(id)).each(function() {
        var found = false;

        for (i = 0; i < styleProducts[edpNo].length; i++) {
            if ($(this).attr("value") == styleProducts[edpNo][i][LEVEL_1_DESCRIPTION]) {
                found = true;
                break;
            }
        }

        if (!found) {
            $(id).removeOption($(this).attr("value"));
        }

    });
}





//Update pricing and availability on the fly. This can vary from client to client and it is left to the developer to make it fit the clients needs visually.
function UpdatePricingAndAvailability(edpNo, level1Choice, level2Choice, level3Choice, pricingId, availabilityId) {
    if (pricingId != "#" && availabilityId != "#") {
        for (i = 0; i < styleProducts[edpNo].length; i++) {
            if (styleProducts[edpNo][i][LEVEL_1_DESCRIPTION] == level1Choice && styleProducts[edpNo][i][LEVEL_2_DESCRIPTION] == level2Choice && styleProducts[edpNo][i][LEVEL_3_DESCRIPTION] == level3Choice) {
                if ($(pricingId).length > 0) {
                    if (styleProducts[edpNo][i][ITEM_OFFER_PRICE] > 0 && (parseFloat(styleProducts[edpNo][i][ITEM_OFFER_PRICE]) < parseFloat(styleProducts[edpNo][i][ITEM_PRICE]))) {
                        $("[id*='label_sale']").show();
                        $("[id*='label_regular']").show();
                        $("[id*='price_sale']").show().html(formatCurrency(styleProducts[edpNo][i][ITEM_OFFER_PRICE]));
                        $("[id*='price_regular']").show().html(formatCurrency(styleProducts[edpNo][i][ITEM_PRICE]));
                    } else {
                        $("[id*='label_sale']").hide();
                        $("[id*='label_regular']").hide();
                        $("[id*='price_sale']").hide().html("");
                        $("[id*='price_regular']").html(formatCurrency(styleProducts[edpNo][i][ITEM_OFFER_PRICE]));
                    }
                }

                if ($(availabilityId).length > 0) $(availabilityId).html(styleProducts[edpNo][i][STOCK_STATUS_MESSAGE]);
            }
        }
    }        
}

//We loop through the style array and find any entries which match our currentLevelChoice we then update the new drop down with the values we find for that level.
function PopulateDropDown(edpNo, elementId, currentLevelChoice, currentStyleArrayIndex, newlevel, newLevelStyleArrayIndex, currentNewDropDownChoice, showPrice, levelCount) {
    var fallBackChoice = "";

    for (i = 0; i < styleProducts[edpNo].length; i++) {
        if (styleProducts[edpNo][i][currentStyleArrayIndex] == currentLevelChoice) {
            if (!$(elementId).containsOption(styleProducts[edpNo][i][newLevelStyleArrayIndex])) {

                if (fallBackChoice == "") fallBackChoice = styleProducts[edpNo][i][newLevelStyleArrayIndex];

                var selected = styleProducts[edpNo][i][newLevelStyleArrayIndex] == currentNewDropDownChoice ? true : false;

                if (showPrice && levelCount == newlevel) {
                    $(elementId).addOption(styleProducts[edpNo][i][newLevelStyleArrayIndex], styleProducts[edpNo][i][newLevelStyleArrayIndex] + ' - ' + formatCurrency(styleProducts[edpNo][i][ITEM_OFFER_PRICE]));
                } else {
                    $(elementId).addOption(styleProducts[edpNo][i][newLevelStyleArrayIndex], styleProducts[edpNo][i][newLevelStyleArrayIndex]);
                }
            }
        }
    }

    if ($(elementId).containsOption(currentNewDropDownChoice)) {
        $(elementId).val(currentNewDropDownChoice);
    } else {
        $(elementId).val(fallBackChoice);
    }

}

function formatCurrency(strValue) {
    strValue = strValue.toString().replace(/\$|\,/g, '');
    dblValue = parseFloat(strValue);

    blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
    dblValue = Math.floor(dblValue * 100 + 0.50000000001);
    intCents = dblValue % 100;
    strCents = intCents.toString();
    dblValue = Math.floor(dblValue / 100).toString();
    if (intCents < 10)
        strCents = "0" + strCents;
    for (var i = 0; i < Math.floor((dblValue.length - (1 + i)) / 3); i++)
        dblValue = dblValue.substring(0, dblValue.length - (4 * i + 3)) + ',' +
		dblValue.substring(dblValue.length - (4 * i + 3));
    return (((blnSign) ? '' : '-') + '$' + dblValue + '.' + strCents);
}