﻿// Include QjavaScriptFunctins.js

function OpenInfoDialog(info, seconds, height, width)
{
    if (height == null)
        height = '300';

    if (width == null)
        width = '400';

    if (seconds == null)
        seconds = '5';

    info = encodeURI(info);

    var url = BaseUrl() + "/DialogWindows/Info/Info.aspx?Info=" + info + "&Seconds=" + seconds;

    OpenModalDialogWindow(url, 'Info', height, width);
}

function OpenPopupCopyTask(taskLink)
{
    var url = BaseUrl() + '/Popup/CopyTask.aspx?TaskLink=' + taskLink;

    return OpenModalDialogWindow(url, 'PopupCopyTask', 600, 520, true);
}

function OpenPopupSendTaskInfo(taskLink, taskCommentLink)
{
    var url = BaseUrl() + '/Popup/SendTaskInfo.aspx?TaskLink=' + taskLink;

    if (taskCommentLink != null)
    {
        url = url + '&TaskCommentLink=' + taskCommentLink;
    }

    return OpenModalDialogWindow(url, 'PopupSendTaskInfo', 700, 800, true);
}

function ShowExcelFile(type, fileType)
{
    var url = BaseUrl();

    if (fileType != null)
    {
        url = url + '/PDF/ExcelGenerator.aspx?Type=' + type + '&FileType=' + fileType;
    }
    else
    {
        url = url + '/PDF/ExcelGenerator.aspx?Type=' + type;
    }

    return OpenDialogWindow(url, 'ExcelGenerator', null, 820, 1020, true);
}

function OpenVoucherAccount(companyLink)
{
    var url = BaseUrl() + '/Vouchers/VoucherAccount.aspx?CompanyLink=' + companyLink;

    return OpenDialogWindow(url, 'SimpleTaskList', null, 820, 1020, true);
}

function OpenSimpleTaskList(companyLink, moduleCode)
{
    var url = BaseUrl() + '/Popup/SimpleTaskList.aspx?CompanyLink=' + companyLink + '&ModuleCode=' + moduleCode;

    return OpenDialogWindow(url, 'SimpleTaskList', null, 700, 900, true);
}

function ShowTaskDialog(id)
{
    var url = BaseUrl() + '/Tasks/ShowTask.aspx?TaskLink=' + id;

    return OpenDialogWindow(url, 'ShowTask', null, 700, 960, true);
}

function ShowModalTaskDialog(id)
{
    var url = BaseUrl() + '/Tasks/ShowTask.aspx?TaskLink=' + id;

    return OpenModalDialogWindow(url, 'ShowTask', 700, 960, true);
}

function ShowNewTaskDialog2(taskLink)
{
    var url = BaseUrl() + '/DialogWindows/Task/TaskDialog.aspx?TaskLink=' + taskLink;

    return OpenDialogWindow(url, 'TaskDialogWindow', null, 700, 900, true);
}

function ShowModalNewTaskDialog(taskLink)
{
    var url = BaseUrl() + '/DialogWindows/Task/TaskDialog.aspx?TaskLink=' + taskLink;

    return OpenModalDialogWindow(url, 'TaskDialogWindow', 700, 900, true);
}

function OpenAddContainerDialog()
{
    var url = BaseUrl() + '/WasteManagement/ShowContainer.aspx?AddContainer=True';

    return OpenDialogWindow(url, 'ShowContainer', null, 700, 900, true);
}

function OpenQmanagerContainerDialog(id)
{
    var url = BaseUrl() + '/WasteManagement/ShowContainer.aspx?ContainerLink=' + id;

    return OpenDialogWindow(url, 'ShowContainer', null, 700, 900, true);
}

function OpenAddTaskOnLocation(locationID)
{
    var parameters = 'LocationLink=' + locationID;

    return OpenAddTaskDialog(parameters)
}

function OpenAddTaskOnAddress(addressID, serviceID)
{
    var parameters = 'AddressLink=' + addressID + '&ServiceLink=' + serviceID;

    return OpenAddTaskDialog(parameters)
}

function OpenAddTaskOnContainer(containerID, serviceID)
{
    var parameters = 'ContainerLink=' + containerID + '&ServiceLink=' + serviceID;

    return OpenAddTaskDialog(parameters)
}

function OpenAddTaskOnCase(caseNumber)
{
    var parameters = 'CaseNumber=' + caseNumber;

    return OpenAddTaskDialog(parameters)
}

function OpenAddTaskDialog(parameters)
{
    var url = BaseUrl() + '/Tasks/CreateTask.aspx?' + parameters;

    return OpenDialogWindow(url, 'AddTask', null, 850, 1024, true);
}

function OpenSearchCriteriaDialog(searchCriteriaID)
{
    var url = BaseUrl();

    if (searchCriteriaID == undefined || searchCriteriaID == 0)
        url += '/DialogWindows/Task/SearchCriteriaDialog.aspx';
    else
        url += '/DialogWindows/Task/SearchCriteriaDialog.aspx?SearchCriteriaLink=' + searchCriteriaID;

    return OpenModalDialogWindow(url, 'ShowSearchCriteria', 500, 860);
}

function OpenUserContactInfo(parameters)
{
    var url = BaseUrl() + '/DialogWindows/Core/UserContactInfoDialog.aspx?' + parameters;

    OpenModalDialogWindow(url, 'UserContactInfo', 200, 300);
}

function OpenUserInfo()
{
    var url = BaseUrl() + '/DialogWindows/Core/UserInfoDialog.aspx';

    OpenModalDialogWindow(url, 'UserContactInfo', 378, 600);
}

function ShowLocationDialog(locationID, buildingID, addressID, companyID, mode)
{
    if (mode == null || mode == undefined)
        mode = "Edit";

    var parameters = "?";
    var addANDSign = false;

    if (locationID != null && locationID != undefined)
    {
        parameters += "LocationID=" + locationID;
        addANDSign = true;
    }

    if (buildingID != null && buildingID != undefined)
    {
        parameters += ((addANDSign) ? "&" : "") + "BuildingID=" + buildingID;
        addANDSign = true;
    }

    if (addressID != null && addressID != undefined)
    {
        parameters += ((addANDSign) ? "&" : "") + "AddressID=" + addressID;
        addANDSign = true;
    }

    if (companyID != null && companyID != undefined)
    {
        parameters += ((addANDSign) ? "&" : "") + "CurrentCompany=" + companyID;
        addANDSign = true;
    }

    if (mode != null && mode != undefined)
    {
        parameters += ((addANDSign) ? "&" : "") + "Mode=" + mode;
    }

    var url = BaseUrl() + "/DialogWindows/Core/LocationDialog.aspx" + parameters;

    return OpenModalDialogWindow(url, 'ShowLocation', 600, 880);

    //    return OpenDialogWindow(url, 'ShowLocation', null, 600, 880, true);
}

function ShowLocationDialogFromTaskInfo(locationID)
{
    var mode = "Edit";

    var parameters = "?";
    var addANDSign = false;

    if (locationID != null && locationID != undefined)
    {
        parameters += "LocationID=" + locationID;
        addANDSign = true;
    }

    if (mode != null && mode != undefined)
    {
        parameters += ((addANDSign) ? "&" : "") + "Mode=" + mode;
    }

    var url = BaseUrl() + "/DialogWindows/Core/LocationDialog.aspx" + parameters;

    return OpenDialogWindow(url, 'ShowLocation', null, 600, 880);
}

function ShowFile(fileID, showFile)
{
    if (showFile != null && showFile )
        fileID = fileID + "&Show=true";

    var url = BaseUrl() + "/DocumentHandler/ShowFile.aspx?NodeID=" + fileID;

    return OpenDialogWindow(url, 'ShowFile', null, 800, 1000, true);
}

function FileCount(fileCount)
{
    if (typeof NodeWindowClosed != 'undefined')
        NodeWindowClosed(fileCount);

    if (imageClientID == null)
        return;

    var objectNodeImage = $get(imageClientID);

    if (objectNodeImage != null)
    {
        if (nodeObjectName == null || nodeObjectName == undefined)
            nodeObjectName = "";

        if (fileCount == '0')
        {
            objectNodeImage.title = 'Ingen filer tilknyttet ' + nodeObjectName;
            objectNodeImage.src = objectNodeImage.src.replace('ObjectNodeFilesAttached', 'ObjectNodeNoFilesAttached');
        }
        else
        {
            if (fileCount == '1')
                objectNodeImage.title = fileCount + ' fil tilknyttet ' + nodeObjectName;
            else
                objectNodeImage.title = fileCount + ' filer tilknyttet ' + nodeObjectName;

            objectNodeImage.src = objectNodeImage.src.replace('ObjectNodeNoFilesAttached', 'ObjectNodeFilesAttached');
        }
    }
}

var imageClientID = "";
var nodeObjectName = "";

function ShowObjectNodesDialog(id, type, clientID, companyID, objectName)
{
    if (id == null || type == null)
        alert("ObjectNodesDialog: objectID or objectType can not be null!");

    imageClientID = clientID;
    nodeObjectName = objectName;

    var parameters = "?ID=" + id + "&NodeObjectType=" + type;

    if (companyID != null && companyID != undefined)
    {
        parameters += "&CurrentCompany=" + companyID;
    }

    var url = BaseUrl() + "/DialogWindows/Core/ObjectNodesDialog.aspx" + parameters;

    return OpenNodeDialogWindow(url, 'ShowNodes', 'FileCount', 310, 580, false);
}

function OpenNodeDialogWindow(url, name, callback, height, width, scrollBars)
{
    if (height == null)
        height = '550';

    if (width == null)
        width = '750';

    if (scrollBars == null || !scrollBars)
        scrollBars = 'no';
    else
        scrollBars = 'yes';

    var cw = document.body.clientWidth;
    var ch = document.body.clientHeight;

    var top = ((ch / 2) - (height / 2)) + window.screenTop;
    var left = ((cw / 2) - (width / 2)) + window.screenLeft;

    var features = 'height=' + height + ', width=' + width + ', toolbar=no, directories=no, status=no, menubar=no, location=no, scrollbars=' + scrollBars + ', resizable=yes, top=' + top + ', left=' + left + '';

    if (callback != null)
    {
        if (url.indexOf('?') > 0)
            url = url + '&ClientCallback=' + callback;
        else
            url = url + '?ClientCallback=' + callback;
    }

    xWin = window.open(url, name, features);

    setTimeout("TestPopupBlocker()", 1000);

    CurrentPopupWindow = xWin;
    setTimeout("FucusPopupWindow()", 500);

    return xWin;
}

var CurrentPopupWindow = null;
function FucusPopupWindow()
{
    if (CurrentPopupWindow != null)
    {
        CurrentPopupWindow.focus();
    }
}

var xWin = null;
function OpenDialogWindow(url, name, callback, height, width, scrollBars)
{
    if (height == null)
        height = '550';

    if (width == null)
        width = '750';

    if (scrollBars == null || !scrollBars)
        scrollBars = 'no';
    else
        scrollBars = 'yes';

    var features = 'height=' + height + ', width=' + width + ', toolbar=no, directories=no, status=no, location=no, menubar=no, scrollbars=' + scrollBars + ', resizable=yes, top=0, left=0';

    if (callback != null)
    {
        if (url.indexOf('?') > 0)
            url = url + '&ClientCallback=' + callback;
        else
            url = url + '?ClientCallback=' + callback;
    }

    xWin = window.open(url, name, features);

    setTimeout("TestPopupBlocker()", 400);

    return xWin;
}


function TestPopupBlocker()
{
    // This function checks if the window is blocked by using these 4 scenarios:
    // 1. xWin is undefined (only a popup killer can do this). 
    // 2. xWin is defined, but it isn't a window object (only a popup killer can do this). 
    // 3. xWin is a window object and it is closed earlier than Human_Reaction_Time (only a popup killer can do this). 
    // 4. xWin is a window object and it is closed later than Human_Reaction_Time (it is very likely that a user sees this window and there is no popup killer in his system). 


    if ((xWin == null) || (typeof (xWin) == "undefined"))
        alert("Et pop-up vindue blev blokeret!\n\nFor at kunne bruge Qmanager skal din browser tillade pop-up vinduer og evt. pop-up blokeringsv\346rkt\370jer skal sl\345s fra.");
}

function OpenModalDialogWindow(url, name, height, width, scrollBars)
{
    if (height == null)
        height = '550';

    if (width == null)
        width = '750';

    if (scrollBars == null || !scrollBars)
        scrollBars = 'No';
    else
        scrollBars = 'Yes';

    var features = 'dialogHeight: ' + height + 'px; dialogWidth: ' + width + 'px; center: Yes; resizable: Yes; status: No; location: No; scroll: ' + scrollBars + ';';

    if (window.showModalDialog)
    {
        if (url.indexOf('?') > 0)
            url = url + '&modal=true';
        else
            url = url + '?modal=true';

        return window.showModalDialog(url, name, features);
    }
}

function hasReturnValue(rValue)
{
    return rValue != null && typeof (rValue) != "undefined" && rValue != "";
}

function ScriptServiceError(error)
{
    OpenInfoDialog(error.get_message());
}
