function upload(EnhetsId, NamnKontrollID, TranKontrollID) {
    if (fileID != null) {
        var beskr = document.getElementById(NamnKontrollID).value;
        var transk = "n/a";
        if (document.getElementById(TranKontrollID) != null) {
            transk = document.getElementById(TranKontrollID).value;
        }
        if (beskr == "") {
            beskr = "Inget namn";
        }
        if (transk == "") {
            transk = "n/a";
        }
        uploader.upload(fileID, "/Upload.aspx",
		                "POST",
		                {
		                    namn: beskr,
		                    enhet: EnhetsId,
		                    tran: transk
		                });
        fileID = null; // Fanns i original simple så fick vara kvar
    }
    else {
        alert("Ingen fil vald");
    }
}

function putv(kontroll, till) {
    $("#" + kontroll).val(till);
    $("#cal2" + kontroll).fadeOut();
    if ($("#" + kontroll).hasClass("autopb")) {
        document.forms[0].submit();
    }
}

function putv2(kontroll, till, kontroll2, till2) {
    $("#" + kontroll).val(till);
    $("#" + kontroll2).val(till2);
    $("#cal2" + kontroll).fadeOut();
}

$(document).ready(function() {
    // CRM
    var Uppe = false;
    $('input:checkbox').each(function() {
        if (this.checked) {
            Uppe = true;
        }
    });
    if (Uppe) {
        // Göm fakturaadress
        $(".faktadr").hide();
    }
    $("input:checkbox").click(function(e) {
        var Uppe = false;
        $('input:checkbox').each(function() {
            if (this.checked) {
                Uppe = true;
            }
        });
        if (Uppe) {
            $(".faktadr").slideUp(500);
        }
        else {
            $(".faktadr").slideDown(500);
        }
    });
    $("input").focus(function() {
        if ($(this).val() == "Ny etikett" || $(this).val() == "Sök") {
            $(this).val('');
        }
    });
    $(".ghost").blur(function() {
        if ($(this).val() == '') {
            $(this).val("Ny etikett");
        }
    });

    // Nyhetsticker
    $('marquee').marquee('pointer').mouseover(function() {
        $(this).trigger('stop');
    }).mouseout(function() {
        $(this).trigger('start');
    }).mousemove(function(event) {
        if ($(this).data('drag') == true) {
            this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
        }
    }).mousedown(function(event) {
        $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
    }).mouseup(function() {
        $(this).data('drag', false);
    });

    $(".viewmenow").click(function(e) {
        var BFId = $(this).attr("restparam1");
        $(".viewer").animate(
                {
                    "height": "400px",
                    "opacity": 1.0
                }, 400);
        // Rulla ut befintlig bild
        $(".infade").animate(
                {
                    "left": "500px"
                }, 400);
        if ($(this).attr("blackkey") != null) {
            $(".viewer").css("background-color", "black");
        }
        else {
            $(".viewer").css("background-color", "white");
        }
        // Ladda med ajax efter 100 ms
        $(this).oneTime(100, function() {
            $(".viewer").load("/rest/mediefil.aspx?b=" + BFId);
        });
        $.scrollTo($(".viewer"), 600, { offset: -200 });
        // Fördröj 400 ms
        $(this).oneTime(400, function() {
            $(".infade").fadeIn(5).animate(
                {
                    "left": (296 - $(".infade").width() / 2) + "px"
                }, 400);
        });
    });

    $(".dragtd1").draggable({ revert: "true", helper: 'clone' });
    $(".dragtd2").draggable({ revert: "true", helper: 'clone' });
    $(".droptd1").droppable({ hoverClass: 'hover', accept: ".dragtd1", drop: function(e, ui) {
        var sourceJury = $(ui.draggable);
        var targetJury = $(this);
        var myForm = sourceJury.attr("axis");
        var conftext = targetJury.attr("conftxt");
        var sourceId = sourceJury.attr("callander");
        var targetId = targetJury.attr("callander");
        if (confirm(conftext)) {
            window.document.location.href = myForm + "?from=" + sourceId + "&to=" + targetId;
        }
    }
    });
    $(".droptd2").droppable({ hoverClass: 'hover', accept: ".dragtd2", drop: function(e, ui) {
        var sourceJury = $(ui.draggable);
        var targetJury = $(this);
        var myForm = sourceJury.attr("axis");
        var sourceId = sourceJury.attr("callander");
        var conftext = targetJury.attr("conftxt");
        var targetId = targetJury.attr("callander");
        if (confirm(conftext)) {
            window.document.location.href = myForm + "?from=" + sourceId + "&to=" + targetId;
        }
    }
    });
    $(".harfokus").focus();
    $(".markerad").select();

    $(".infobar").slideDown(600);
    $(".chartzoom").animate(
                {
                    "width": "100px",
                    //                "height": "70px",
                    "opacity": 0.4
                }, 1);
    $(".chartzoom").click(function() {
        if ($(this).width() > 100) {
            $(this).animate({
                "width": "100px",
                "marginLeft": "0px",
                //                "height": "70px",
                "opacity": 0.4
            }, 800);
        }
        else {
            $(this).animate({
                "width": "679px",
                "marginLeft": "-30px",
                "marginRight": "-30px",
                //                "height": "550px",
                "opacity": 1.0
            }, 800);
        }
    });

    $(".toggler1").click(function() {
        $(".toggle1").fadeIn(500);
        $(this).fadeOut(500);
    });
    $(".toggler2").click(function() {
        $(".toggle2").fadeIn(500);
        $(this).hide();
    });
    $(".klicktd").mouseover(function(e) {
        $(this).addClass("hover");
    }).mouseout(function(e) {
        $(this).removeClass("hover");
    }).click(function(e) {
        $(this).find("a").each(function(e) {
            if ($(this).attr("href") != null) {
                document.location = $(this).attr("href");
            }
        });
    });

    $(".klick").mouseenter(function(e) {
        var AltElement = $(this).attr("c4alt");
        if (AltElement != null) {
            $(AltElement).fadeIn(120);
            //return false;
        }
        //e.stopPropagation();
    }).mouseleave(function(e) {
        var AltElement = $(this).attr("c4alt");
        if (AltElement != null) {
            $(AltElement).fadeOut(120);
            //return false;
        }
        //e.stopPropagation();
    });

    $(".soktd").mouseover(function(e) {
        $(this).addClass("sokhover");
    }).mouseout(function(e) {
        $(this).removeClass("sokhover");
    }).click(function(e) {
        $(this).find("a").each(function(e) {
            if ($(this).attr("href") != null) {
                document.location = $(this).attr("href");
                return false;
            }
        });
    });

    $(".fiffig").keypress(function(e) {
        var c = String.fromCharCode(e.which);
        var str = $(this).val() + c;
    });

    $(".fiffig").keyup(function(e) {
        var str = $(this).val();
        var HelperId = "#cal2" + this.id;
        var ParamId = "#par2" + this.id;
        var Kategori = $(ParamId).children("span:first").text();
        if (str.length > 0) {
            $(HelperId).fadeIn();
            $(HelperId).load("/Suggest.aspx?t=" + Kategori + "&k=" + this.id + "&v=" + encodeURI(str));
        }
        $(HelperId).css("top", $(this).offset().top);
        $(HelperId).css("left", $(this).offset().left + 156);
    });

    $(".rostlista input").click(
            function() {
                $.scrollTo(this, 900, { offset: -200 });
            }
        );

    $(".uniksiffra").blur(
        function() {
            var mittVarde = $(this).val();
            var v_ok = true;
            var antalSamma = 0;
            var MaxVarde = 0;
            if (mittVarde != "") {
                $(".uniksiffra").each(
                function(i, n) {
                    MaxVarde++;
                    if (mittVarde == $(this).val())
                        antalSamma++;
                    if (antalSamma > 1)
                        v_ok = false;
                }
            );
                if (!v_ok) {
                    $(this).val("");
                    $(this).select();
                    alert("Värdet " + mittVarde + " har tilldelats två gånger! Ange ett annat värde.");
                }
                if (mittVarde > MaxVarde) {
                    $(this).val("");
                    $(this).select();
                    alert("Värdet var för stort!");
                }
            }
        }
        );

    $(".nav").click(function(fa) {
        var mytarget = $(this).attr("href");
        $(".meny1").animate({ top: "80px" }, 200, function(fb) {
            // Hitta länken och gå dit
            document.location = mytarget;
            return false;
        });
        return false;
    });

    $(".egg").click(function(fa) {
        $(".meny2").fadeOut(200);
        var mytarget = $(this).attr("href");
        $(".meny1").animate({ top: "110px" }, 220, function() {
            // Hitta länken och gå dit
            document.location = mytarget;
            return false;
        });
        return false;
    });

    // Generell mekanism för att förekomsten av ett objekt styr synligheten av andra objekt
    $(".c4show").each(function(i) {
        var attVisa = $(this).attr("c4target");
        $(attVisa).fadeIn();
    });
    // Generell mekanism för att förekomsten av ett objekt adderar klass till andra objekt
    $(".c4addclass").each(function(i) {
        var attVisa = $(this).attr("c4target");
        var nyKlass = $(this).attr("c4class");
        $(attVisa).addClass(nyKlass);
    });
    // Generell mekanism för att förekomsten av ett objekt adderar klass till andra objekt
    $(".c4removeclass").each(function(i) {
        var attVisa = $(this).attr("c4target");
        var nyKlass = $(this).attr("c4class");
        $(attVisa).removeClass(nyKlass);
    });
});
