﻿$(document).ready(function() {

    $("#MyPagesLoginLink a").click(
        function() {
            $('#MyPagesLogin').toggle();
            return false;
        }
    );
    $("#MyPagesLoginCloseButton").click(
        function() {
            $('#MyPagesLogin').toggle();
            return false;
        }
    );


    $("#CountAndSignBtn").click(
        function() {
            $('#CountAndSign').toggle();
            return false;
        }
    );

    $(".GoCountAndSign").click(
        function() {
            $("#CountAndSign").show();
        }
    );

    $(".QuickCalcGo").click(
        function(e) {
            if (productCalculatorForm) {
                var ok = true;
                for (var i = 0; i < quickCalcForm.Fields.length; i++) {
                    ok = quickCalcForm.Fields[i].Validate() && ok;
                }
                if (ok) {
                    for (var i = 0; i < quickCalcForm.Fields.length; i++) {
                        quickCalcForm.Fields[i].TransferValue(productCalculatorForm);
                    }
                    $("#CountAndSign").show();
                    $("#ProductMainSection").hide();
                    if (productCalculatorForm.CalcButtonClientId !== "") {
                        $("#" + productCalculatorForm.CalcButtonClientId).click();
                    }
                }
                else {
                    e.preventDefault();
                }
            }
        }
    );


    $(".PrintPage a").click(
        function() {
            if (window.print) {
                window.print();
            }
            return false;
        }
    );

    $(".IconChooser li").click(
        function(e) {
            $(".IconChooser li").removeClass("Selected");
            $(this).addClass("Selected");
            $(".IconChooser input").attr('checked', false);
            $(this).find("input").attr('checked', true);
            e.preventDefault();
        }
    );

    $(".BI_InsuranceListRadio li").click(
        function(e) {
            $(".BI_InsuranceList li").removeClass("Checked");
            $(this).addClass("Checked");
            $(this).find("input").attr('checked', true);
            removeSelection()
            e.preventDefault();

        }
    );

    $(".BI_InsuranceListRadio li a").focus(
        function() {
            $(this).parent().parent().parent().parent().parent().addClass("Hover");
        }
    );

    $(".BI_InsuranceListRadio li a").blur(
        function() {
            $(this).parent().parent().parent().parent().parent().removeClass("Hover");
        }
    );

    $(".CheckboxWrapper").click(
        function(e) {
            if ($(this).find("label").is(".Checked")) {
                $(this).find("label").removeClass("Checked");
                $(this).find("input").attr('checked', false);
            } else {
                $(this).find("label").addClass("Checked");
                $(this).find("input").attr('checked', true);
            }
            removeSelection()
            e.preventDefault();

        }
    );

    $(".BuildingListButtonDel").click(
    function(e) {
        var li = $(this).parent();
        li.find("input").val("");
        li.find("select").val(0);
        li.find("select").change();
        if ($(this).attr("id") != "BuildingListButtonDel0")
            li.hide();
        //        $(".BuildingListButtonAdd").show();
        var ui = $(".BuildingList");
        var lis = ui.find(".BuildingListItem:visible");
        lis.last().find(".BuildingListButtonAdd").show();
        removeSelection()
        e.preventDefault();
    }
    );

    $(".BuildingListButtonAdd").click(
    function(e) {
        var ui = $(".BuildingList");
        var lis = ui.find(".BuildingListItem:hidden");
        //        if (lis.length == 1)
        //            $(".BuildingListButtonAdd").hide();
        lis.first().show();
        lis.find(".BuildingListButtonAdd").show();
        $(this).hide();
        removeSelection()
        e.preventDefault();
    }
    );

    $(".TextFieldBox .TextField").focus(
        function() {
            $(this).parent().addClass("TextFieldFocus");
        }
    );

    $(".TextFieldBox .TextField").blur(
        function() {
            $(this).parent().removeClass("TextFieldFocus");
        }
    );

    tooltip();
    tooltip2();
    tooltip3();
    tooltip4();


});

function removeSelection() {
    if (window.getSelection) {  // all browsers, except IE before version 9
        var selection = window.getSelection();
        selection.removeAllRanges();
    }
    else {
        if (document.selection.createRange) {        // Internet Explorer
            var range = document.selection.createRange();
            document.selection.empty();
        }
    }
}


function ProductCalculatorTextField(key, textBoxClientId) {
    this.Key = key;
    this.TextBoxClientId = textBoxClientId;
    this.Type = "Text";
}

ProductCalculatorTextField.prototype.SetValue = function(text) {
    $("#" + this.TextBoxClientId).val(text);
}


function ProductCalculatorRadioField(listClientId) {
    this.Options = [];
    this.Type = "Radio";
    this.ListClientId = listClientId;
}




function QuickCalcTextField(key, textBoxClientId, type, isOptional) {
    this.Key = key;
    this.TextBoxClientId = textBoxClientId;
    this.Type = type;
    this.IsOptional = isOptional;
}

QuickCalcTextField.prototype.GetTextBox = function() {
    return $("#" + this.TextBoxClientId);
}

QuickCalcTextField.prototype.GetValue = function() {
    return this.GetTextBox().val().replace(/^\s+|\s+$/g, '');
}

QuickCalcTextField.prototype.Validate = function() {
    var ok;
    var val = this.GetValue();
    if (val.length === 0 && this.IsOptional) {
        ok = true;
    }
    else {
        switch (this.Type) {
            case "None":
                ok = true;
                break;
            case "Personnummer":
                ok = /^\d{6}\d{4}$/.test(val);
                break;
            case "Postnummer":
                ok = /^\d{3} ?\d{2}$/.test(val);
                break;
            case "Registreringsnummer":
                ok = /^[a-zA-ZåäöÅÄÖ]{3} ?\d{3}$/.test(val);
                break;
            case "Inkomst":
                ok = /^\d{1,6}$/.test(val);
                break;
            default:
                ok = true;
                break;
        }
    }
    if (ok)
        this.GetTextBox().removeClass("TextFieldError2");
    else
        this.GetTextBox().addClass("TextFieldError2");

    return ok;
}

QuickCalcTextField.prototype.TransferValue = function(targetCalculatorForm) {
    var val = this.GetValue();

    for (var i = 0; i < targetCalculatorForm.Fields.length; i++) {
        var field = targetCalculatorForm.Fields[i];
        if (field.Type === "Text" && field.Key === this.Key) {
            field.SetValue(val);
            return;
        }
    }
}


function QuickCalcRadioField() {
    this.Options = [];
}

QuickCalcRadioField.prototype.TransferValue = function(targetCalculatorForm) {
    var selectedKey = 'None';

    for (var o = 0; o < this.Options.length; o++) {
        var opt = this.Options[o];
        if ($("#" + opt.Id).is(":checked")) {
            selectedKey = opt.Key;
            break;
        }
    }

    if (selectedKey === "None") {
        return;
    }

    for (var i = 0; i < targetCalculatorForm.Fields.length; i++) {
        var field = targetCalculatorForm.Fields[i];

        if (field.Type === "Radio") {
            var buttons = $("#" + field.ListClientId + " input");
            for (var to = 0; to < field.Options.length; to++) {
                var tOpt = field.Options[to];
                if (tOpt.Key === selectedKey) {
                    buttons.eq(tOpt.Index).attr("checked", "checked");
                    break;
                }
            }
        }
    }
}

QuickCalcRadioField.prototype.Validate = function() {
    return true;
}



/* ToolTip */
this.tooltip = function() {

    var xOffset = -10;
    var yOffset = -130;


    $(".ProductCatProducts a").hover(function(e) {

        this.t = this.title;
        this.h = $(this).find("img").attr("alt")

        if (this.t != '') this.title = '';

        var c = '<p class="TooltipHeadline">' + this.h + '</p><p class="TooltipText">' + this.t + '</p>';
        $("body").append("<div id='Tooltip'>" + c + "</div>");

        var posX, posY;

        var posX = $(this).offset().left;
        var posY = $(this).offset().top;

        $("#Tooltip")
        .css("top", ((posY + yOffset) + "px"))
        .css("left", ((posX + xOffset) + "px"))
        .show();

    },
	function() {
	    $("#Tooltip").remove();
	    this.title = this.t;
	});
};

/* ToolTip2 */
this.tooltip2 = function() {

    var xOffset = -208;
    var yOffset = -105;

    $(".ExtraInfo").hover(function(e) {
        this.t = this.title;

        if (this.t != '') this.title = '';

        $("body").append("<div id='Tooltip2'>" + this.t + "</div>");

        var posX, posY;

        var posX = $(this).offset().left;
        var posY = $(this).offset().top;

        $("#Tooltip2")
        .css("top", ((posY + yOffset) + "px"))
        .css("left", ((posX + xOffset) + "px"))
        .show();
    },
	function() {
	    $("#Tooltip2").remove();
	    this.title = this.t;

	});
};

/* ToolTip3 */
this.tooltip3 = function() {

    var xOffset = -65;
    var yOffset = -120;

    $(".ShoppingCartInfo").hover(function(e) {


        this.t = this.title;

        if (this.t != '') this.title = '';

        $("body").append("<div id='Tooltip3'>" + this.t + "</div>");

        var posX, posY;

        var posX = $(this).offset().left;
        var posY = $(this).offset().top;

        $("#Tooltip3")
        .css("top", ((posY + yOffset) + "px"))
        .css("left", ((posX + xOffset) + "px"))
        .show();
    },
	function() {
	    $("#Tooltip3").remove();
	    this.title = this.t;

	});
};


/* ToolTip4 */
this.tooltip4 = function() {

    var xOffset = -10;
    var yOffset = -130;


    $(".BI_ChooseBusiness li").hover(function(e) {

        this.t = this.title;
        this.h = $(this).find("a").html();

        if (this.t != '') this.title = '';

        var c = '<p class="TooltipHeadline">' + this.h + '</p><p class="TooltipText">' + this.t + '</p>';
        $("body").append("<div id='Tooltip4'>" + c + "</div>");

        var posX, posY;

        var posX = $(this).offset().left;
        var posY = $(this).offset().top;

        $("#Tooltip4")
        .css("top", ((posY + yOffset) + "px"))
        .css("left", ((posX + xOffset) + "px"))
        .show();

    },
	function() {
	    $("#Tooltip4").remove();
	    this.title = this.t;
	});
};

