﻿/*
** kdLibrary.js
** LastUpdate: 14.02.2012 22:28
** Rolf Terje Kvam
** Kvam data AS
** http://www.kvamdata.no
** -----------------------------------------------------------------
** .printWindow()
** .Help(file)
** .Window(url, title, l, t, w)
** .Dialog(url, content, title, l, t, w)
** .showWindow (url, title, l, t, w, h)
** .closeWindow ()
** .showDialog (url, id, title, l, t, w, h)
** .createElement(e, id)
** .modul()
** .appfile()
** .getElementValue(item)
** .setElementValue(item, v)
** .Height()
** .Width()
** .updateDiv(divName, response, clickEvent)
** .showLoading()
** .hideLoading()
**
** errAjax(XMLHttpRequest, textStatus, errorThrown)
** doAjax(method, async, p1, p2, okResponse, errorResponse)
** doAjaxModul(m, async, service, p1, p2, okResponse, errorResponse)
**
** kdUtility_RowDblClick (o)
** kdUtility_RowClick (o)
** kdUtility_quickSearch(v)
** kdUtility_OnChange()
** -----------------------------------------------------------------
*/

var kdLib = {

    version: '2.0 - 15.02.2012 11:37',
    copyright: 'Kvam data AS',

    printWindow: function () { window.print() },

    Help: function (file) {
        kdLib.Dialog('/html/no/' + file, 'content', "Hjelp", 20, 20, 700);
    },

    Window: function (url, title, l, t, w) {
        var h = kdLib.Height() - t - 70;
        var m = kdLib.showWindow(url, title, l, t, w, h);
        return m;
    },

    Dialog: function (url, content, title, l, t, w) {
        var h = kdLib.Height() - t - 30;
        kdLib.showDialog(url, content, title, l, t, w, h);
    },

    showWindow: function (url, title, l, t, w, h)
    {
        var strTitle = (title != null) ? title : 'HRA System II (w)';

        var iLeft   = (l) ? l : 50;
        var iTop    = (t) ? t : 50;
        var iWidth  = (w) ? w : 900;
        var iHeight = (h) ? h : kdLib.Height() - iTop - 70;

        var horizontalPadding = 20;
        var verticalPadding   = 0;

        if ($('#externalSite').length == 0)
        {
            var myWindow = '<iframe';

            myWindow += ' id="externalSite"';
            myWindow += ' class="externalsite"';
            myWindow += ' frameborder = "0"';
            myWindow += ' />';

            $('body').append(myWindow);
        }

        $('#externalSite').attr('src', url);

        var m = $('#externalSite').dialog(
        {
            title: strTitle,
            autoOpen: true,
            position: [iLeft, iTop],
            width: iWidth,
            height: iHeight,
            modal: true,
            resizable: true,
            autoResize: true,
            close: function () { if (typeof closeWindow == 'function') closeWindow() },
            overlay:
            {
                opacity: 0.5,
                background: "black"
            }
        }).width(iWidth - horizontalPadding).height(iHeight - verticalPadding);

        return m;
    },

    closeWindow: function ()
    {
        $('#externalSite').dialog("close");
    },

    showDialog: function (url, id, title, l, t, w, h) {

        var myUrl;

        if (id != null)
            myUrl = url + ' #' + id;
        else
            myUrl = url;

        var iLeft   = (l) ? l : 50;
        var iTop    = (t) ? t : 50;
        var iWidth  = (w) ? w : 800;
        var iHeight = (h) ? h : kdLib.Height() - t - 30;

        var strTitle = (title != null) ? title : 'Kvam data AS';

        $('<div>').dialog({
            title: strTitle,
            close: function () { $(this).remove(); },
            open: function () { $(this).load(myUrl); },
            position: [iLeft, iTop],
            height: iHeight,
            width: iWidth,
            maximize: true,
            resizable: true,
            modal: true
        });

    },

    createElement: function (e, id, width) {
        var div = document.getElementById(id);

        if (div == null) {
            div = document.createElement(e);
            div.id = id;
            div.className = id;
            document.body.appendChild(div);
        }

        if (width) {
            div.style.width = width;
        }

        return id;
    },


    modul: function () {
        var v = document.getElementById('ctl00_kdLib_modul');
        if (v == null)
            v = document.getElementById('kdLib_modul');
        return v.value;
    },

    appfile: function () {
        var v = document.getElementById('ctl00_kdLib_xmlfile');
        if (v == null)
            v = document.getElementById('kdLib_xmlfile');
        return v.value;
    },

    getElementValue: function (item) {
        var e = document.getElementById('ctl00_BodyContent_' + item);
        if (e == null)
            e = document.getElementById(item);
        return e.value;
    },

    setElementValue: function (item, v) {
        var e = document.getElementById('ctl00_BodyContent_' + item);
        if (e == null)
            e = document.getElementById(item);
        if (e == null)
            alert('DEBUG: Finner ikke elementet: ' + item);
        e.value = v;
    },


    Height: function () {
        var iHeight = 0;

        if (typeof (window.innerWidth) == 'number') {
            //Non-IE
            iHeight = window.innerHeight;
        } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            iHeight = document.documentElement.clientHeight;
        } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            //IE 4 compatible
            iHeight = document.body.clientHeight;
        }
        return (iHeight);
    },

    Width: function () {
        var iWidth = 0;

        if (typeof (window.innerWidth) == 'number') {
            //Non-IE
            iWidth = window.innerWidth;
        } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            iWidth = document.documentElement.clientWidth;
        } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            //IE 4 compatible
            iWidth = document.body.clientWidth;
        }
        return (iWidth);
    },

    updateDiv: function (divName, response, clickEvent) {
        var myDiv;
        var myDivName;

        myDivName = divName;
        myDiv = document.getElementById(myDivName);

        if (myDiv == null) {
            myDivName = 'MainContent_' + divName;
            myDiv = document.getElementById(myDivName);
        }

        if (myDiv) {
            myDiv.innerHTML = response.d;

            if (clickEvent != null) {
                $('#' + myDivName + ' a').bind('click', clickEvent);
                $('#' + myDivName + ' input').bind('click', clickEvent);
            }
        }
    },

    showLoading: function () {
        $('#ventLitt').show();
    },

    hideLoading: function () {
        $('#ventLitt').hide();
    }


};

/*
** -----------------------------------
** Ajax
** -----------------------------------
*/

function doAjaxModul(m, a, service, p1, p2, okResponse, errorResponse) {
    var method = '/modules/' + m + '/' + m + '.aspx' + '/' + service;
    doAjax(method, a, p1, p2, okResponse, errorResponse);
}

function doAjax(method, a, p1, p2, okResponse, errorResponse) {
    var parameter = '{"p1":"' + p1 + '", "p2":"' + p2 + '"}';
    var eFunction = (errorResponse) ? errorResponse : errAjax;

    $.ajax (
        {
            type: "POST",
            url: method,
            data: parameter,
            cache: false,
            async: a,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: okResponse,
            error: eFunction,
            failure: errAjax
        });
};

function errAjax(XMLHttpRequest, textStatus, errorThrown) {

    var e = eval("(" + XMLHttpRequest.responseText + ")");

    var myError = document.getElementById('e_msg');

    if (myError) {
        myError.style.color = 'red';
        myError.style.background = 'yellow';

        $('#e_msg').empty();
        $('#e_msg').append(e.Message);
    }
    else {
        alert(e.Message);
    }
}

/*
** -----------------------------------------------------------------
** kdUtility: default functions
** -----------------------------------------------------------------
*/

function kdUtility_RowDblClick(o) {
    if (window.local_RowDblClick) {
        local_RowDblClick(o);
    }
}

function kdUtility_RowClick(o) {

    var x = true;

    if (window.local_RowClick)
    {
        x = !local_RowClick(o);
    }

    if (x == true)
    {
        kdUtility__RowClick(o);
    }
}

function kdUtility__RowClick(o)
{
    var pid   = null;
    var pNode = o.parentNode;

    if (pNode) pNode = pNode.parentNode;
    if (pNode) pid   = pNode.id;

    if (pid == null) return;

    /* var pid = o.parentNode.parentNode.id; */

    var rc = $('#' + pid).data('_rowColor');
    var rn = $('#' + pid).data('_rowName');

    if (rn)
    {
        $('#' + rn).css('background-color', rc);
    }

    rn = o.id;
    rc = $('#' + rn).css('background-color');

    $('#' + rn).css('background-color', '#c0c0c0');

    $('#' + pid).data('_rowColor', rc);
    $('#' + pid).data('_rowName', rn);
}

function kdUtility_quickSearch(v)
{
    if (window.local_quickSearch)
        local_quickSearch(v);
}

function kdUtility_OnChange()
{
    if (window.local_OnChange) {
        local_OnChange();
        return;
    }

    var btn = document.getElementById('ID_WINDOW_UPD');

    if (btn != null)
    {
        btn.style.visibility = 'visible';
        btn.disabled = false;
    }
}

