﻿//Premium
Date.format = 'yyyy-mm-dd';
var oneYearFromNow;

$(document).ready(function() {
    oneYearFromNow = new Date();
    oneYearFromNow.setDate(oneYearFromNow.getDate() + 355);

    $().ajaxStart($.blockUI).ajaxStop($.unblockUI);
    reactivate();

    //setTimeout(function() { $.scrollTo($('#StageArea')) }, 1000);
});

function reactivate() {
    $('a.helpLink').click(toggleHelp);
    $('a.helpLinkInline').click(toggleHelp);
    $('a.helpLinkInlineSimple').click(toggleHelp);
    $('a.helpLinkNoImage').click(toggleHelp);

    $('.datePicker').datePicker({ endDate: oneYearFromNow.asString() });

    $('p.moreInfo').hide();
    $('span.expand').removeClass('hidden');
    $('div.excessDescription').hide();

    $('div.premiumTextBox input').focus(function() {
        var title = $(this).attr('title');
        if (title == $(this).val()) {
            $(this).val('');
        }
    });

    $('div.premiumTextBox input').blur(function() {
        if ($(this).val() == '') {
            $(this).val($(this).attr('title'));
        }
    });

    $('span.expand').live("click", toggle);

    $('span.expandExcess').live("click", toggleExcess);

    $('.fancyDropdown').selectbox({
        containerClass: 'selectBox',
        inputClass: 'selectInput'
    });

    try {
        if (typeof (customScript) == 'function') {
            customScript();
        }
    }
    catch (e) { }
}

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');
    }
    return false;
}

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 += 1 * nn.substring(n, n + 1);
    }

    this.valid = (this.checksum % 10 == 0) ? true : false;
    this.value = this.fullYear + this.month + this.day + '-' + this.controldigits;
}

function Validate_PostalCode(source, arguments) {
    try {
        var pc = new PostalCode(arguments.Value);
        arguments.IsValid = pc.valid;

        // update with correct format
        if ((pc.valid) && (source) && (source.controltovalidate)) {
            $('#' + source.controltovalidate).val(pc.value);
        }
    }
    catch (e) {
        arguments.IsValid = false;
    }
}

function PostalCode(value) {
    if (!value.match(/^(\d{3})[- ]*(\d{2})$/)) {
        this.valid = false;
        return;
    }

    this.f = RegExp.$1;
    this.s = RegExp.$2;
    this.value = this.f + this.s;
    this.valid = true;
}