﻿// Name: Utilities.js
// Version: 1.0.0.0
// FileVersion: 1.0.0.0
// -----------------------------------------------------------------------
//  Copyright (C) Peter Bredenberg System. All rights reserved.
// -----------------------------------------------------------------------

// Save cookie
function setCookie(name, value, expiredays) {
    var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

// Disable all child controls
function disableCtrls(nodes) {
    var i = 0;
    for (i = 0; i < nodes.length; i++) {
        var node = nodes[i];
        if (node.nodeType == 1) {
            if (node.childNodes.length > 0)
                disableCtrls(node.childNodes);
            node.disabled = true;
        }
    }
}

// Enable all child controls
function enableCtrls(nodes) {
    var i = 0;
    for (i = 0; i < nodes.length; i++) {
        var node = nodes[i];
        if (node.nodeType == 1) {
            if (node.childNodes.length > 0)
                enableCtrls(node.childNodes);
            node.disabled = false;
        }
    }
}

// Find a control by id
function isDlgCtrl(node, refObj) {
    refObj.value = false;
    if (node == null || node.id == null)
        return;
    else {
        if (node.id.indexOf('Dialog') != -1) {
            refObj.value = true;
            return;
        }
        else
            return isDlgCtrl(node.parentNode, refObj);
    }
}

// Get the width of the browser window
function getWindowWidth() {
    var clientWidth = 0, bodyWidth = 0;
    if (navigator.appName.indexOf("Microsoft") != -1)
        clientWidth = document.documentElement.clientWidth;
    bodyWidth = document.body.offsetWidth;
    return clientWidth > bodyWidth ? clientWidth : bodyWidth;
}

// Show unexpected error message
function showUnexpectedErrorMsg(result) {
    alert('Ett oväntat fel har inträffat. Vänligen försök igen senare.\n\nTeknisk information:\n' + result.get_message());
}

// Stop event bubbling
function stopEventBubbling(e) {
    if (window.event) {
        window.event.returnValue = false;
        window.event.cancel = true;
        window.event.cancelBubble = true;
        return false;
    }
    else {
        e.preventDefault();
        return false;
    }
}

// Default button click
function defaultButtonHandler(e, button) {
    // Process only the Enter key
    var kC = (window.event != null) ? event.keyCode : e.charCode;
    if (kC == 13) {
        stopEventBubbling(e);
        button.click();
        return false;
    }
}

// Close current dialog
function closeDialog(closeMe, e) {
    // Process only the Escape key
    var kC = (window.event != null) ? event.keyCode : e.keyCode;
    if (kC == 27) closeMe();
}

// Open popup
function openPopup(name, file, width, height) {
    var popup = window.open(file, name, "location='no', status='no', scrollbars='no', resizable='no' width=" + width + ", height=" + height);
    popup.focus();
}

// Notify ScriptManager that this is the end of the script
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


