﻿//registration

$(document).ready(function() {
    $().ajaxStart($.blockUI).ajaxStop($.unblockUI);
    reactivate();
});

function reactivate() {
    //$('a.helpLink').click(toggleHelp);
    //$('p.moreInfo').hide();
    //$('span.expand').removeClass('hidden');
    //$('div.excessDescription').hide();
    //$('span.expand').live("click", toggle);
    //$('span.expandExcess').live("click", toggleExcess);
    //$('.tooltip').tipsy({ fade: true, gravity: 'w' });
    //$('.datePicker').datePicker({ startDate: '1996-01-01' });

    // init defaultvalues
    $('.defaultvalue').each(function() {
        var default_value = $(this).attr('title');

        if (this.value == '') {
            this.value = default_value;
        }
        else {
            if (this.value != default_value) {
                $(this).removeClass("defaultvalue");
            }
        }

        $(this).focus(function() {
            if (this.value == default_value) {
                this.value = '';
                $(this).removeClass("defaultvalue");
            }
        });

        $(this).blur(function() {
            if ($.trim(this.value) == '') {
                this.value = default_value;
                $(this).addClass("defaultvalue");
            }
            else {
                if (this.value != default_value) {
                    $(this).removeClass("defaultvalue");
                }
            }
        });

        $('.premiumButton').click(function() {
            $('.defaultvalue').each(function() {
                this.value = '';
            });
        });
    });

    // init extension-panels
    initExtensionPanels();
}

var extensionPanels = { groups: [] };

function initExtensionPanels() {
    $('input[rel]:radio').each(function() {
        var triggerId = $(this)[0].id;
        var meta = $(this).attr('rel').split(/;/);
        var groupId = meta[0];

        if (extensionPanels.groups[groupId] == undefined) {
            extensionPanels.groups[groupId] = { panels: [] };
        }

        var panelIds = [];

        if (meta.length == 2) {
            panelIds = meta[1].split(/,/);
        }

        // load panels
        for (var index in panelIds) {
            var panelId = panelIds[index];

            if ((panelId != '') && (panelId != undefined)) {
                if (extensionPanels.groups[groupId].panels[panelId] == undefined) {
                    extensionPanels.groups[groupId].panels[panelId] = { triggers: [], siblings: [] };
                }

                extensionPanels.groups[groupId].panels[panelId].triggers[triggerId] = {};

                // find all siblings
                $('#' + panelId + ' input:radio').each(function() {
                    var siblingId = $(this).attr('id');

                    if ((siblingId != '') && (siblingId != undefined)) {
                        extensionPanels.groups[groupId].panels[panelId].siblings[siblingId] = false;
                    }
                });
            }
        }

        // add event
        $(this).click(function() {
            var meta = $(this).attr('rel').split(/;/);
            var groupId = meta[0];

            // foreach panel in this group
            for (var panelId in extensionPanels.groups[groupId].panels) {
                // check if any trigger is selected
                var isSelected = false;

                for (var triggerId in extensionPanels.groups[groupId].panels[panelId].triggers) {
                    if ($('#' + triggerId).attr('checked')) {
                        isSelected = true;
                    }
                }

                if (isSelected) {
                    $('#' + panelId).slideDown(500);
                }
                else {
                    $('#' + panelId).slideUp(500);

                    // reset siblings
                    for (var siblingId in extensionPanels.groups[groupId].panels[panelId].siblings) {
                        $('#' + siblingId).attr('checked', false);
                    }
                }
            }
        });
    });

    // hide panels
    var panelIds = [];

    for (var groupId in extensionPanels.groups) {
        for (var panelId in extensionPanels.groups[groupId].panels) {
            if (panelIds[panelId] != true) {
                panelIds[panelId] = false;

                for (var triggerId in extensionPanels.groups[groupId].panels[panelId].triggers) {
                    if ($('#' + triggerId).attr('checked')) {
                        panelIds[panelId] = true;
                    }
                }
            }
        }
    }

    for (var panelId in panelIds) {
        if (panelIds[panelId] == true) {
            $('#' + panelId).show();
        }
        else {
            $('#' + panelId).hide();
        }
    }
}

function toggleHelp(e) {
    e.preventDefault();
    $(this).siblings('.helpText').modal();
}

function toggle() {
    $(this).siblings('p.moreInfo').slideToggle(500);
    var link = $(this).find('a');
    if (link.hasClass('minus')) {
        link.removeClass('minus');
        link.addClass('plus');
    } else {
        link.removeClass('plus');
        link.addClass('minus');
    }
}

function toggleExcess() {
    $(this).siblings('div.excessDescription').slideToggle(500);
    var link = $(this).find('a');
    if (link.hasClass('minus')) {
        link.removeClass('minus');
        link.addClass('plus');
    } else {
        link.removeClass('plus');
        link.addClass('minus');
    }
}

function Validate_Date(source, arguments) {
    try {
        var date = new GetDate(arguments.Value);
        arguments.IsValid = date.valid;

        // update with correct format
        if ((date.valid) && (source) && (source.controltovalidate)) {
            $('#' + source.controltovalidate).val(date.value);
        }
    }
    catch (e) {
        arguments.IsValid = false;
    }
}

function GetDate(value) {
    this.valid = false;

    // yyyy-mm-dd
    if (value.match(/^(\d{2,4})[- ]*(0[1-9]|1[0-2])[- ]*([12]\d|0[1-9]|3[01])$/)) {
        this.year = ((RegExp.$1.length == 2) ? '20' : '') + RegExp.$1;
        this.month = RegExp.$2;
        this.day = RegExp.$3;
        this.value = this.year + '-' + this.month + '-' + this.day;
        this.valid = true;
    }
    else {
        // dd/mm/yyyy
        if (value.match(/^([12]\d|0[1-9]|3[01])[\/ ]*(0[1-9]|1[0-2])[\/ ]*(\d{2,4})$/)) {
            this.year = ((RegExp.$3.length == 2) ? '20' : '') + RegExp.$3;
            this.month = RegExp.$2;
            this.day = RegExp.$1;
            this.value = this.year + '-' + this.month + '-' + this.day;
            this.valid = true;
        }
    }
}

function Validate_Personnummer(source, arguments) {
    try {
        var pn = new Personnummer(arguments.Value);
        arguments.IsValid = pn.valid;

        // update with correct format
        if ((pn.valid) && (source) && (source.controltovalidate)) {
            $('#' + source.controltovalidate).val(pn.value);
        }
    }
    catch (e) {
        arguments.IsValid = false;
    }
}

function Personnummer(value) {
    if (!value.match(/^(\d{2})?(\d{2})(\d{2})(\d{2})[\-]?(\d{4})$/)) {
        this.valid = false;
        return;
    }

    this.fullYear = ((RegExp.$1 == '') ? '19' : RegExp.$1) + RegExp.$2;
    this.year = RegExp.$2;
    this.month = RegExp.$3;
    this.day = RegExp.$4;
    this.controldigits = RegExp.$5;
    this.sex = parseInt(this.controldigits.substring(2, 3)) % 2;

    var nr = this.year + this.month + this.day + this.controldigits;
    var nn = '';

    for (var n = 0; n < nr.length; n++) {
        nn += ((((n + 1) % 2) + 1) * nr.substring(n, n + 1));
    }

    this.checksum = 0;

    for (var n = 0; n < nn.length; n++) {
        this.checksum += nn.substring(n, n + 1) * 1;
    }

    this.valid = (this.checksum % 10 == 0) ? true : false;
    this.value = this.fullYear + this.month + this.day + '-' + this.controldigits;
}