
dojo.require("dojo.cubeware.contentpane");
dojo.require("dojo.cubeware.dnd");
dojo.require("dojo.cubeware.menu");

var GlobalLoaderFlag = false;
var isCtrl = false;
var isShift = false;

//-----------------------------------------------------------------------------
// Loader
//-----------------------------------------------------------------------------
function initLoader(portlet)
{
    var bild = 'images/ajax-loader.gif';
    if(portlet)
    {
        bild = portlet+bild;
    }
    if(!document.getElementById('progressbar'))
    {
        document.write('<div id="progressbar"'+ 
          ' style="position:absolute;visibility:visible;right:42px;top:10px;width:1px;height:1px;z-index:256;display:none;">'+
          '<img src="'+bild+'" alt="Loading"/></div>');
    }
}

function addLoader()
{
    if(GlobalLoaderFlag && document.getElementById && document.getElementById('progressbar'))
    {
        var loader = document.getElementById('progressbar');
        loader.style.display = "";
    }
    GlobalLoaderFlag = false;
}

function removeLoader()
{
    if(!GlobalLoaderFlag && document.getElementById && document.getElementById('progressbar'))
    {
        var loader = document.getElementById('progressbar');
        loader.style.display = "none";
    }
    GlobalLoaderFlag = false;
}

//--------------------------------------
// Reload ContentPane 
//--------------------------------------
function rcp(strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2)
{
    GlobalLoaderFlag = true
    setTimeout(addLoader, 1100);

    // realize autoscroll
    setAutoscrollMarker("no", "rcp()");
    setTimeout("autoScroll()", 2000);
    
    return rcp2(strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2);
//  return false; //prevent default behavior
}

function rcp2(strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2) {

    return dojo.cubeware.contentpane.reloadPane(strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2);
}

function rcp3(strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2, astrExtra) {

    return dojo.cubeware.contentpane.reloadPane2(strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2, astrExtra);
}

//--------------------------------------
// Keyboardevent
//--------------------------------------
function krcp(event, strPane, btnok, btnok_value, btncancel, btncancel_value, btnhelp, btnhelp_value)
{
    var keyCode = event.keyCode+""; //as string
    switch (keyCode)
    {
        // Enter
        case "13":
            rcp(strPane, "btn_id", btnok, "btn_value", btnok_value);
            return false;
        // Escape
        case "27":
            rcp(strPane, "btn_id", btncancel, "btn_value", btncancel_value);
            return false;
        // F1
        case "112":
            rcp(strPane, "btn_id", btnhelp, "btn_value", btnhelp_value);
            return false;
        // F5 (Browser page reload)
        case "116":
            //rcp(strPane, "btn_id", btnhelp, "btn_value", btnhelp_value);
            return false;
    }

    return true;
}

//--------------------------------------
// DEPRECATED - use cad() function 
// "Change focus on enter": for <input> fields: jump to next field
//--------------------------------------
function cfe(event, nextElemId)
{
    var keyCode = event.keyCode + ""; //as string
    switch (keyCode) {
        case "13":
            {
                var nextElem;
                // specific element given
                if (nextElemId) {
                    nextElem = document.getElementById(nextElemId);
                }
                // no element given -> choose default existing element (e.g. w_1 - main pane) to change focus
                else {
                    nextElem = document.getElementById('w_1');
                }
                // jump to element
                if (nextElem) // paranoia
                {
                    nextElem.focus();
                    nextElem.select();
                    break;
                }
            }
            
        // Strg is released
        case "17":
        {
            isCtrl = false;
            break;
        }
    }
    
    return true;
}

//--------------------------------------
// ECI "Edit current input" Realize Ctrl-A: for <input> fields
//--------------------------------------
function eci(event, strHtmlId) {
    var keyCode = event.keyCode + ""; //as string
    //alert(keyCode);
    switch (keyCode) {
        case "17": // CTRL
            {
                isCtrl = true;
                break;
            }
        case "65": // A
            {
                if (isCtrl) {
                    markText(document.getElementById(strHtmlId));
                }
                break;
            }
    }
    return true;
}

//---------------------------
// change active datacell with arrow keys and enter - mit onKeyUp verwendet.
//---------------------------
function cad(event, enter, top, right, bottom, left ) 
{
    var keyCode = event.keyCode + ""; //as string
    //alert(keyCode);
    nextElem = "";
    switch (keyCode) {
        case "13": // enter; go down same functionality as arrow down, except it works without ctrl or shift key
            {
                if (enter != '(null)' && !isCtrl && !isShift) {
                    nextElem = document.getElementById(enter);
                }
                // blur current cell to fake a change cell to make IE invoke the onchange event
                else if (enter == '(null)' && event.srcElement.id) {
                    document.getElementById(event.srcElement.id).blur();
                }
                break;
            }
            // setze globale Variable isCtrl zurück 
        case "17": // CTRL
            {
                isCtrl = false;
                break;
            }
            // setze globale Variable isShift zurück 
        case "16": // SHIFT
            {
                isShift = false;
                break;
            }
        case "27": // ESC
            {
                // dummy needed to to make cursor visible again after Esc-Undo function in Internet Explorer; see: function ctrlZ()
                break;
            }
        case "37": // LEFT arrow
            {
                if (left != '(null)' && isCtrl) {
                    nextElem = document.getElementById(left);
                }
                break;
            }
        case "38": // UP arrow
            {
                if (top != '(null)') {
                    nextElem = document.getElementById(top);
                }
                break;
            }
        case "39": // RIGHT arrow
            {
                if (right != '(null)' && isCtrl) {
                    nextElem = document.getElementById(right);
                }
                break;
            }   
        case "40": // DOWN arrow
            {
                if (bottom != '(null)') {
                    nextElem = document.getElementById(bottom);
                }
                break;
            }
    }
    // jump if target is valid
    if (nextElem != "" && nextElem != null) // paranoia
    {
        nextElem.focus();
        nextElem.select();
    }
}

//--------------------------------------
// gf "get focus" realizes text selection when get focus for IE
//--------------------------------------
function gf(event, strHtmlId) {
    //markText(document.getElementById(strHtmlId));
    elem = document.getElementById(strHtmlId);
    if (elem != null) {
        var r = elem.createControlRange();
        if (r != null) { // do
        } else { /*alert("no text range found.");*/ }
    } else { /*alert("elem: " + elem)*/ }
}

//--------------------------------------
// Text markieren mit range
//--------------------------------------
function markText(elem) {
    if (document.selection && document.selection.createRange) {
        //alert("IE: " + elem);
        var textRange = document.selection.createRange();
        textRange.moveToElementText(elem);
        textRange.select();
    } else if (document.createRange && window.getSelection) {
        //alert("Mozilla: " + elem);
        var range = document.createRange();
        range.selectNode(elem);
        var selection = window.getSelection();
        selection.removeAllRanges();
        selection.addRange(range);
    }
}


//--------------------------------------
// Stop event bubbling
//--------------------------------------
function seb(e)
{
    if (!e) var e = window.event;
    
    // suitable for IE
    if (!e.stopPropagation)
    {
        e.cancelBubble = true;
    }
    // not IE (Firefox, Opera...)
    if (e.stopPropagation)
    {
        e.stopPropagation();
    }
    return true;
}

//--------------------------------------
// New CWDropTarget
//--------------------------------------
function ndndt(nodeId, type, cpID)
{
    return new dojo.cubeware.dnd.CWDropTarget(dojo.byId(nodeId), type, cpID);
}

//--------------------------------------
// New CWDragSource
//--------------------------------------
function ndnds(nodeId, types, cpID)
{
    return new dojo.cubeware.dnd.CWDragSource(dojo.byId(nodeId), types, cpID);
}

//--------------------------------------
// OnMouseUp für Context Menu
//--------------------------------------
function omu(aEvt, strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2)
{
    return dojo.cubeware.menu.onMouseUp(aEvt, strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2);
}

//--------------------------------------
// OnMouseDown für Context Menu - nur für opera nötig
//--------------------------------------
function omd(aEvt, strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2)
{
    return dojo.cubeware.menu.onMouseDown(aEvt, strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2);
}

//--------------------------------------
// OnTouchStart für Context Menu
//--------------------------------------
function ots(aEvt, strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2) {
    return dojo.cubeware.menu.onTouchStart(aEvt);
}

//--------------------------------------
// Key down for edit - currently not used
//--------------------------------------
function ekd(aEvt, strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2)
{
    aEvt = dojo.event.browser.fixEvent(aEvt, null);
    dojo.event.browser.currentEvent = aEvt;

    // Enter?
    if (13 == aEvt.keyCode)
    {
        rcp(strPane, astrActionId, astrActionValue, astrActionId2, astrActionValue2);
    }
}

//----------------------------------------------------------------
// checks for autoscroll marker and selected elements to scroll to
//----------------------------------------------------------------
function autoScroll() {
    if (document.getElementsByName('autoscroll')) {
            // single select
        if (document.getElementById('selectedRadioBox') && document.getElementById("single")) 
        {
            if (document.getElementById("single").value == "yes") 
            {
                hidden = document.getElementById('selectedRadioBox');
                scrollToId = hidden.value;
                splitValues = scrollToId.split("r");
                newValue = splitValues[0];
                result = scrollToElement(document.getElementById(scrollToId), document.getElementById(newValue));
            }
         }
            //multiselect
        else if (document.getElementsByName('selectedCheckBoxes').length > 0)
        {
            numberOfSelected = document.getElementsByName('selectedCheckBoxes').length;
            hiddenValues = document.getElementsByName('selectedCheckBoxes');
            hidden = hiddenValues[numberOfSelected - 1];
            scrollToId = hidden.value;
            autoScrollIndicator = "as_" + scrollToId;
            if (document.getElementById(autoScrollIndicator) && document.getElementById(autoScrollIndicator).value == "yes")
            {
                splitValues = scrollToId.split("m");
                newValue = splitValues[0];
                result = scrollToElement(document.getElementById(scrollToId), document.getElementById(newValue));
            }
        }
    } //else{alert("no flag: " + flag);}
}

//-----------------------------------------------------------
// Scroll to selected element (used in "Elementauswahl"-Tree)
//-----------------------------------------------------------
function scrollToElement(theElement, pane) {
    //alert("scroll: " + theElement.id + " what: " + pane.id);
    var selectedPosX = 0;
    var selectedPosY = 0;
    pane.scrollTop = 0;

   while (theElement != pane && theElement != null) {
       selectedPosX += theElement.offsetLeft;
       selectedPosY += theElement.offsetTop;
       theElement = theElement.offsetParent;
    }
    // scroll 5px before selected element to avoid that the selected element is truncated on the upper border of the frame
    pane.scrollTop = selectedPosY - 5;
}

//------------------------------------------------------------------
// set or reset autoscroll marker; used before updating content pane
//------------------------------------------------------------------
function setAutoscrollMarker(value, where)
{
    var elements = null;
    var values = "";
    if (document.getElementsByName('autoscroll')) {
        elements = document.getElementsByName('autoscroll');
        for (i = 0; i < elements.length; i++) {
            values = values + elements[i].id + " ";
            elements[i].value = value;
        }
        /*if (values != "")
        {
            alert("resetted values: " + values + " " + where);
        }*/
    }
    return true;
}

//------------------------------------------------------------------
// send imAlive to events.aspx
//------------------------------------------------------------------
function sendAlive() {
    dojo.cubeware.events.notifySessionTimer("IAmAlive");
}


//------------------------------------------------------------------
// alert mit timer slow down
//------------------------------------------------------------------
function cubewareAlert(text) {
    dojo.cubeware.events.notifySessionTimer("GoSlow");
    alert(text);
    dojo.cubeware.events.notifySessionTimer("GoNormal");
}

//------------------------------------------------------------------
// CTRL-Z (undo) feature on Escape key; used in splashing mode - mit onKeyDown verwendet.
// realisiert gleichzeitig das setzen der CTRL und Shift Variable für Tastenkombinationen
//------------------------------------------------------------------
function mapCtrlZ(elem, event) {

    var keyCode = event.keyCode + ""; //as string
    switch (keyCode) {

        // setze globale Variable isCtrl  
        case "16": // Shift
            {
                isShift = true;
                break;
            }
        // setze globale Variable isCtrl
        case "17": // CTRL
            {
                isCtrl = true;
                break;
            }
        // Esc
        case "27":
            // Firefox does not support this feature
            if (dojo.render.html.currentBrowser == "Firefox") {
                //alert(unescape('Escape wird nur im Internet Explorer unterstützt.%0ABitte benutzen Sie Strg-Z, um Änderungen zurückzunehmen.'));
            }
            else {
                document.execCommand("undo", false, 0);
            }
    }
}

//------------------------------------------------------------------
// used to set the variable document.domain, which is needed when 
// Cockpit is used within a iFrame e.g. SAP NetWeaver
//------------------------------------------------------------------
function setDomain()
{
    if( parent.frames.length == 0 )  // no iframe embedding?
        return;  // as before; don't modify document.domain

    // setting domain name
    var hostname = window.location.hostname;
    var domainParts = hostname.split(".");
    var domain = "";
	//alert("document.domain: " + document.domain + " ;Parts: " + domainParts.length);
    if (domainParts.length >= 2)
    {
        beginSecondPart = hostname.indexOf(".");
        domain = hostname.substring(beginSecondPart + 1);
		//alert("domain: " + domain);
    }
    else {
        domain = hostname;
		//alert("domain = hostname" + hostname);
    }

    document.domain = domain;
}


