﻿jQuery(function () {

    var m = Offerta.WORKTYPES;

    var updateWhat1 = function (id0, id1) {
        jQuery(".WhatList1 option:not(:first)").remove();
        if (id0 == "0") {
            Offerta.SetInputDisabled(jQuery(".WhatList1").get(0), true);
            jQuery(".WhatField").val("0");
            jQuery(".WhatHint").text("").hide();
        }
        else {
            Offerta.SetInputDisabled(jQuery(".WhatList1").addOptions(m[id0], id1).get(0), false);
            if (id1) {
                jQuery(".WhatList1").selectOption(id1);
                jQuery(".WhatField").val(id1);
                updateWhatHint(id0, id1, false);
            }
            else {
                jQuery(".WhatField").val("0");
                jQuery(".WhatHint").text("").hide();
            }
        }
    };

    var updateWhatHint = function (id0, id1, hl) {
        var jH = jQuery(".WhatHint");
        if (jH.length == 0) return;
        if (id1 == "0") { jH.text("").hide(); return; }
        var hint = "";
        for (var i = 0, n = m[id0].length; n > i; ++i) { if (m[id0][i][0] == id1) { hint = m[id0][i][4] == null ? "" : "(" + m[id0][i][4] + ")"; break; } }
        if (jH.text() == hint) return;
        jH.text(hint);
        if (hint != "") {
            if (hl) {
                jH.effect("highlight", { color: jQuery(".WhatHintFade").css("background-color") }, 1500);
            }
            else {
                jH.show();
            }
        }
    };

    jQuery(".WhatList0").change(function (event) { updateWhat1(event.target.value); });

    jQuery(".WhatList1").change(function (event) {
        jQuery(".WhatField").val(event.target.value);
        updateWhatHint(jQuery(".WhatList0 option:selected").val(), event.target.value, true);
    });

    updateWhat1(jQuery(".WhatList0 option:selected").val(), jQuery(".WhatField").val());

    if ($(".WhatList1").parents(".input-container").find(".input-error-tooltip").length > 0) {
        if ($(".WhatList1").parents(".rc-input").is(".rc-input-default")) {
            $(".WhatList1").parents(".rc-input").removeClass("rc-input-default").addClass("rc-input-error");
        }
    }

});
