/* cache backgroundimages in IE */
try
{
    document.execCommand("BackgroundImageCache", false, true);
}
catch (err) { }

function imgswap(from, to)
{
    if (document.images)
    {
        document.images[from].src = pics[to].src;
    }
}

/* ----------------- WIDGET DRAG/DROP ----------------- */
function WidgetDropped(container, item, position)
{
    var instanceId = item.getAttribute("InstanceId");
    var columnNo = parseInt(container.getAttribute("columnNo"));
    var row = position;
    if (item.getAttribute("IsTemplate") != null && item.getAttribute("IsTemplate") == "true")
    {
        var pageId = container.getAttribute("PageId");
        //alert("widget dropped callback -> col:"+ columnNo +", page:" + pageId+", inst:"+instanceId);
        _4screen.CSB.WebServices.WidgetService.AddWidgetInstance(instanceId, pageId, columnNo, row);
        var ctrlName = ''
        try
        {
            ctrlName = container.childNodes[0].id;
            if (!ctrlName)
            {
                ctrlName = container.childNodes[1].id;
            }
            if (!ctrlName)
            {
                ctrlName = container.childNodes[2].id;
            }
            __doPostBack(ctrlName.replace(/_/g, '$'), '');
        }
        catch (Err)
        {
        }
    }
    else
    {
        _4screen.CSB.WebServices.WidgetService.MoveWidgetInstance(instanceId, columnNo, row);
    }
}

function onDrop(sender, e)
{
    var container = e.get_container();
    var item = e.get_droppedItem();
    var position = e.get_position();

    //alert( String.format( "Container: {0}, Item: {1}, Position: {2}", container.id, item.id, position ) );

    var instanceId = item.getAttribute("InstanceId");
    var columnNo = parseInt(container.getAttribute("columnNo"));
    var row = position;
    if (item.getAttribute("IsTemplate") != null && item.getAttribute("IsTemplate") == "true")
    {
        var pageId = container.getAttribute("PageId");
        //alert("widget dropped callback -> col:"+ columnNo +", page:" + pageId+", inst:"+instanceId);
        _4screen.CSB.WebServices.WidgetService.AddWidgetInstance(instanceId, pageId, columnNo, row);
        var ctrlName = ''
        try
        {
            ctrlName = container.childNodes[0].id;
            if (!ctrlName)
            {
                ctrlName = container.childNodes[1].id;
            }
            if (!ctrlName)
            {
                ctrlName = container.childNodes[2].id;
            }
            __doPostBack(ctrlName.replace(/_/g, '$'), '');
        }
        catch (Err)
        {
        }
    }
    else
    {
        _4screen.CSB.WebServices.WidgetService.MoveWidgetInstance(instanceId, columnNo, row);
    }
}

function $hide(id)
{
    document.getElementById(id).style.display = "none";
}

function dump(arr, level)
{
    var dumped_text = "";
    if (!level) level = 0;

    //The padding given at the beginning of the line.
    var level_padding = "";
    for (var j = 0; j < level + 1; j++) level_padding += "    ";

    if (typeof (arr) == 'object')
    { //Array/Hashes/Objects 
        for (var item in arr)
        {
            var value = arr[item];

            if (typeof (value) == 'object')
            { //If it is an array,
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += dump(value, level + 1);
            } else
            {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else
    { //Stings/Chars/Numbers etc.
        dumped_text = "===>" + arr + "<===(" + typeof (arr) + ")";
    }
    return dumped_text;
}

/* ----------------- Handle Flash Video Visibility ----------------- */
var VideoDiv;
function DisableVideoDiv()
{
    if (VideoDiv != undefined)
    {
        VideoDiv.style.visibility = 'hidden';
        VideoDiv.style.display = 'none';
    }
}
function EnableVideoDiv()
{
    if (VideoDiv != 'undefined')
    {
        VideoDiv.style.visibility = 'visible';
        VideoDiv.style.display = 'inline';
    }
}

/* ----------------- handle ENTER keys ----------------- */
function DisableEnterKey(e)
{
    var key;
    if (window.event)
        key = window.event.keyCode;
    else
        key = e.which;

    return (key != 13);
}

function DoPostbackOnEnterKey(e, postbackId)
{
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (parseInt(code) == 13)
    {
        __doPostBack(postbackId, '');
        return false;
    }
}

function DoLoginOnEnterKey(e, postbackId)
{
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (parseInt(code) == 13)
    {
        if (document.getElementById('SecXmlToken') != null)
        {
            RemoveNodeByID('SecXmlToken');
        }
        __doPostBack(postbackId, '');
        return false;
    }
}

function HitTagFilter(e)
{
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (parseInt(code) == 13)
    {
        var obj = document.getElementById('txtTag');
        var hrf = document.getElementById('href1');
        window.location.href = hrf.href + '?mtag=' + obj.value;
        return true;
    }
}

function RemoveNodeByID(nodeID)
{
    var node = document.getElementById(nodeID);
    if (node != null && node.parentNode != null)
    {
        node.parentNode.removeChild(node);
    }
}

function DeleteDataObject(url)
{
    if (confirm(unescape("Soll dieser Inhalt wirklich gel%F6scht werden?")))
    {
        radWinOpen(url, unescape("Inhalt l%F6schen"), 300, 150, false, 'SetTab')
    }

    return false;
}

function SelectTextBox(textboxId)
{
    document.getElementById(textboxId).focus();
    document.getElementById(textboxId).select();
}

function SelectAll(checkbox, childName)
{
    if (checkbox.checked == true)
    {
        var i;
        for (i = 0; i < document.forms[0].elements.length; i++)
            if ((document.forms[0].elements[i].type == 'checkbox') && (document.forms[0].elements[i].name.indexOf(childName) > -1))
            document.forms[0].elements[i].checked = true;
    }
    else
    {
        var i;
        for (i = 0; i < document.forms[0].elements.length; i++)
            if ((document.forms[0].elements[i].type == 'checkbox') && (document.forms[0].elements[i].name.indexOf(childName) > -1))
            document.forms[0].elements[i].checked = false;
    }
}

function stopPropagation(e)
{
    e.cancelBubble = true;
    if (e.stopPropagation)
    {
        e.stopPropagation();
    }
}

function setComboboxText(id)
{
    var combobox = document.getElementById(id + "_Input");
    combobox.value = "";
    var checkboxIndex = 0;
    do
    {
        var checkbox = document.getElementById(id + "_i" + checkboxIndex + "_CheckBox");
        checkboxIndex++;
        if (checkbox != null)
        {
            if (checkbox.checked)
            {
                combobox.value += checkbox.nextSibling.firstChild.nodeValue + ",";
            }
        }
    } while (checkbox != null)
    combobox.value = combobox.value.replace(/,$/, "");
}

function LayoutChange(currentDropzones, newDropzones)
{
    if (newDropzones < currentDropzones)
        alert('Das neue Layout hat weniger Spalten. Die Widgets werden automatisch verschoben.');
}

function ReloadPageWithTheme(theme)
{
    var urlParts = window.location.href.split("?");
    if (urlParts.length == 2)
    {
        var queryStringParts = urlParts[1].split("&");
        var url = urlParts[0];
        var queryString = "";
        for (var i = 0; i < queryStringParts.length; i++)
        {
            if (queryStringParts[i].indexOf("Theme") != 0)
            {
                queryString += queryStringParts[i] + "&";
            }
        }
        window.location.href = url + "?" + queryString + "Theme=" + theme.value;
    }
}

function StyleSwitchMode(previewElementID, rbNewID, rbColorID, rbImageID, tbColorID, tbImageID, pnlColorolorID, pnlImagemageID, cbRepeatHID, cbRepeatVID)
{
    var previewElement = document.getElementById(previewElementID);
    var rbColor = document.getElementById(rbColorID);
    var rbImage = document.getElementById(rbImageID);
    var tbColor = document.getElementById(tbColorID);
    var tbImage = document.getElementById(tbImageID);
    var pnlColor = document.getElementById(pnlColorolorID);
    var pnlImage = document.getElementById(pnlImagemageID);

    previewElement.style.backgroundImage = "none";
    previewElement.style.backgroundRepeat = "no-repeat";
    previewElement.style.backgroundColor = "transparent";

    if (rbNewID == rbColorID) // Switch to color mode
    {
        pnlColor.style.display = "inline";
        pnlImage.style.display = "none";

        if (tbColor.value.indexOf('#') != -1)
        {
            try { previewElement.style.backgroundColor = tbColor.value; }
            catch (error) { }
        }
    }
    else // Switch to image mode
    {
        pnlImage.style.display = "inline";
        pnlColor.style.display = "none";

        var imageLarge = tbImage.value.replace(/BG\/S/i, "BG/L");
        previewElement.style.backgroundImage = "url(" + imageLarge + ")";

        StyleSetRepeating(previewElementID, cbRepeatHID, cbRepeatVID);
    }
}

function StyleColorChange(previewElementID, colorBoxID, tbColorID)
{
    var previewElement = document.getElementById(previewElementID);
    var colorBox = document.getElementById(colorBoxID);
    var tbColor = document.getElementById(tbColorID);

    try
    {
        previewElement.style.backgroundColor = tbColor.value;
        colorBox.style.backgroundColor = tbColor.value;
    }
    catch (error)
    {
        previewElement.style.backgroundColor = "transparent";
        colorBox.style.backgroundColor = "transparent";
        tbColor.value = "Keine Farbe ausgewählt";
    }
}

function StyleSetRepeating(previewElementID, cbRepeatHID, cbRepeatVID)
{
    var previewElement = document.getElementById(previewElementID);
    var cbRepeatH = document.getElementById(cbRepeatHID);
    var cbRepeatV = document.getElementById(cbRepeatVID);

    previewElement.style.backgroundRepeat = "no-repeat";
    if (cbRepeatH.checked)
        previewElement.style.backgroundRepeat = "repeat-x";
    if (cbRepeatV.checked)
        previewElement.style.backgroundRepeat = "repeat-y";
    if (cbRepeatH.checked && cbRepeatV.checked)
        previewElement.style.backgroundRepeat = "repeat";
}

function StyleSetFooterHeight(previewElementID, tbFooterHeightID)
{
    var previewElement = document.getElementById(previewElementID);
    var tbFooterHeight = document.getElementById(tbFooterHeightID);
    var validChars = "0123456789";
    var isNumber = true;
    var character;

    for (var i = 0; i < tbFooterHeight.value.length && isNumber == true; i++)
    {
        character = tbFooterHeight.value.charAt(i);
        if (validChars.indexOf(character) == -1)
        {
            isNumber = false;
        }
    }
    if (isNumber)
    {
        if (tbFooterHeight.value > 500)
            tbFooterHeight.value = 500;
        previewElement.style.height = tbFooterHeight.value + "px";
    }
}

function ChangeRatingImg(clientId, i)
{
    for (var x = 1; x <= i; x++)
    {
        var img = document.getElementById(clientId + '_RAT' + x);
        img.src = img.getAttribute('nsrc');
    }
}

function RestoreRatingImg(clientId, i)
{
    for (var x = 1; x <= i; x++)
    {
        var img = document.getElementById(clientId + '_RAT' + x);
        img.src = img.getAttribute('ssrc');
    }
}

function OnTextboxFocus(textbox, defaultValue)
{
    if (textbox.value == (defaultValue))
    {
        textbox.value = "";
    }
}

function AttachEvent(parentObject, eventName, eventHandler)
{
    eventName = GetEventName(eventName);
    if (parentObject.addEventListener)
    {
        parentObject.addEventListener(eventName, eventHandler, false);
    }
    else
    {
        if (parentObject.attachEvent)
        {
            parentObject.attachEvent(eventName, eventHandler);
        }
    }
}

function DetachEvent(parentObject, eventName, eventHandler)
{
    eventName = GetEventName(eventName);
    if (parentObject.addEventListener)
    {
        parentObject.removeEventListener(eventName, eventHandler, false);
    }
    else
    {
        if (parentObject.detachEvent)
        {
            parentObject.detachEvent(eventName, eventHandler);
        }
    }
}

function GetEventName(eventName)
{
    eventName = eventName.toLowerCase();
    if (document.addEventListener)
    {
        if (0 == eventName.indexOf('on'))
        {
            return eventName.substr(2);
        }
        else
        {
            return eventName;
        }
    }
    else
    {
        if (document.attachEvent && !(0 == eventName.indexOf('on')))
        {
            return 'on' + eventName;
        }
        else
        {
            return eventName;
        }
    }
}

function GetModalOverlayImage()
{
    var imageElement = document.createElement('IMG');
    imageElement.src = VRoot + '/Library/Images/transp.gif';
    imageElement.style.position = 'absolute';
    imageElement.style.zIndex = '5000';
    imageElement.className = 'RadWModalImage';
    return imageElement;
}

function SetModalBackgroundSize(imageElement)
{
    var _3 = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    var _4 = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    var _5;
    if (window.innerWidth)
    {
        _5 = Math.min(window.innerWidth, document.documentElement.clientWidth);
    }
    else
    {
        _5 = Math.max(document.body.clientWidth, document.documentElement.clientWidth);
    }
    var _6;
    if (window.innerHeight)
    {
        _6 = Math.max(window.innerHeight, document.documentElement.clientHeight);
    }
    else
    {
        if (document.documentElement.clientHeight > 0)
        {
            _6 = document.documentElement.clientHeight;
        }
        else
        {
            _6 = document.body.clientHeight;
        }
    }
    imageElement.style.left = _3 + 'px';
    imageElement.style.top = _4 + 'px';
    imageElement.style.width = _5 + 'px';
    imageElement.style.height = _6 + 'px';
    imageElement.style.display = '';
}

var modalBackgroundImage = null;

function SetModalBackground()
{
    if (modalBackgroundImage == null)
    {
        modalBackgroundImage = GetModalOverlayImage();
        AttachEvent(window, 'resize', SetModalBackground);
        AttachEvent(window, 'scroll', SetModalBackground);
    }

    SetModalBackgroundSize(modalBackgroundImage);

    if (null != document.readyState)
    {
        if ('complete' == document.readyState)
            document.body.appendChild(modalBackgroundImage);
        else
            window.setTimeout(SetModalBackground, 100);
    }
    else
    {
        document.body.appendChild(modalBackgroundImage);
    }
}

function HideModalBackground()
{
    if (modalBackgroundImage != null)
    {
        modalBackgroundImage.style.display = 'none';
        DetachEvent(window, 'resize', SetModalBackground);
        DetachEvent(window, 'scroll', SetModalBackground);
    }
}

var popupWindowDiv = null;
var popupElementId = null;
var popupWidth = null;
var popupLeft = null;
var popupTop = null;
var popupTitle = null;
var popupContent = null;
var popupIsModal = null;

function SetPopupWindow(elementId, width, left, top, title, content, isModal)
{
    popupElementId = elementId;
    popupWidth = width;
    popupLeft = left;
    popupTop = top;
    popupTitle = title;
    popupContent = content;
    popupIsModal = isModal;
    SetPopupWindowReady();
}

function SetPopupWindowReady()
{
    popupContent = popupContent.replace(/&lt;/g, "<");
    popupContent = popupContent.replace(/&gt;/g, ">");

    if (popupWindowDiv == null)
    {
        popupWindowDiv = document.createElement('DIV');
        popupWindowDiv.style.position = 'absolute';
        popupWindowDiv.style.zIndex = '10000';
        popupWindowDiv.style.top = popupTop + 'px';
        var innerWidth;
        if (window.innerWidth)
            innerWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
        else
            innerWidth = Math.max(document.body.clientWidth, document.documentElement.clientWidth);
        if (popupLeft == null || popupLeft == 0)
            popupWindowDiv.style.left = (innerWidth / 2 - popupWidth / 2) + 'px';
        else
            popupWindowDiv.style.left = popupLeft + 'px';
    }

    popupWindowDiv.style.display = '';
    popupWindowDiv.innerHTML = "<div style='position:absolute; z-index:10000;' unselectable='on' class='radtooltip_Web20' id='" + popupElementId + "'><table class='ToolTipWrapper' width='" + popupWidth + "'><tbody><tr><td class='ToolTipTopLeft'>&nbsp;</td><td class='ToolTipTopCenter'><div class='ToolTipTitlebar VisibleTitlebar'>" + popupTitle + "</div><a title='Schliessen' class='CloseButton' onClick=\"HidePopupWindow('" + popupElementId + "')\" href='javascript:void(0);'><span>Close</span></a></td></td><td class='ToolTipTopRight'>&nbsp;</td></tr><tr><td class='ToolTipLeftMiddle'>&nbsp;</td><td class='ToolTipContent' valign='top'><div>" + popupContent + "</div></td><td class='ToolTipRightMiddle'>&nbsp;</td></tr><tr><td class='ToolTipBottomLeft'>&nbsp;</td><td class='ToolTipBottomCenter'>&nbsp;</td><td class='ToolTipBottomRight'>&nbsp;</td></tr></tbody></table></div>";

    if (document.readyState != null && navigator.userAgent != null && navigator.userAgent.toString().indexOf("MSIE") != -1)
    {
        if ('complete' == document.readyState)
        {
            document.body.appendChild(popupWindowDiv);
        }
        else
        {
            window.setTimeout(SetPopupWindowReady, 100);
        }
    }
    else
    {
        document.body.appendChild(popupWindowDiv);
    }

    if (popupIsModal)
        SetModalBackground();
}

function HidePopupWindow(elementId)
{
    document.getElementById(elementId).style.display = 'none';
    HideModalBackground();
}

function GetRadWindow()
{
    var oWindow = null;
    if (window.radWindow)
        oWindow = window.radWindow;
    else if (window.frameElement != null && window.frameElement.radWindow)
        oWindow = window.frameElement.radWindow;
    return oWindow;
}

function CloseWindow(CWParams)
{
    if (CWParams == 'undefined' || CWParams == null)
    {
        GetRadWindow().Close();
    }
    else
    {
        GetRadWindow().Close(CWParams);
    }
}

function GeoInfoCB(sender, geoInfo)
{
    var manager = GetRadWindowManager();
    var wizardWin = manager.GetWindowByName("wizardWin");
    var currentDocument = null;
    if (wizardWin != null && wizardWin.get_contentFrame() != null && wizardWin.get_contentFrame().contentWindow != null)
    {
        currentDocument = wizardWin.get_contentFrame().contentWindow.document;
    }
    else
    {
        currentDocument = window.document;
    }
    var geoInfoArray = geoInfo.toString().split(",");
    if (geoInfoArray.length == 7)
    {
        var controlId = geoInfoArray[0];
        var txtLong = currentDocument.getElementById(controlId + '_TxtGeoLong');
        var txtLat = currentDocument.getElementById(controlId + '_TxtGeoLat');
        var txtZip = currentDocument.getElementById(controlId + '_HFZip');
        var txtCity = currentDocument.getElementById(controlId + '_HFCity');
        var txtRegion = currentDocument.getElementById(controlId + '_HFRegion');
        var txtCountry = currentDocument.getElementById(controlId + '_HFCountry');
        txtLat.value = geoInfoArray[1];
        txtLong.value = geoInfoArray[2];
        txtZip.value = geoInfoArray[3];
        txtCity.value = geoInfoArray[4];
        txtRegion.value = geoInfoArray[5];
        txtCountry.value = geoInfoArray[6];
    }
}

function RefreshRadWindow(radWindowId)
{
    var radWin = null;
    var currentRadWin = GetRadWindow();
    if (currentRadWin != null)
    {
        var manager = currentRadWin.get_windowManager();
        if (manager != null)
            var radWin = manager.GetWindowByName(radWindowId);
    }
    if (radWin != null)
    {
        radWin.reload();
    }
    else
    {
        RefreshParentPage();
    }
}

function RefreshParentPage(ignoreCache)
{
    if (ignoreCache != null)
        GetRadWindow().BrowserWindow.location.reload(ignoreCache);
    else
        GetRadWindow().BrowserWindow.location.reload();
}

function RedirectParentPage(newUrl)
{
    GetRadWindow().BrowserWindow.document.location.href = newUrl;
}

function CallFunctionOnParentPage(fnName)
{
    var oWindow = GetRadWindow();
    if (oWindow.BrowserWindow[fnName] && typeof (oWindow.BrowserWindow[fnName]) == "function")
    {
        oWindow.BrowserWindow[fnName](oWindow);
    }
}

function radWinOpen(url, title, width, height, isCentered, CallBackFunc, windowId)
{
    var oWindow;
    var winW = 600;
    var winH = 400;
    height = height + 50;
    if (parseInt(navigator.appVersion) > 3)
    {
        if (navigator.appName == "Netscape")
        {
            winW = window.innerWidth - 16;
            winH = window.innerHeight - 16;
        }
        if (navigator.appName.indexOf("Microsoft") != -1)
        {
            winW = document.body.offsetWidth - 20;
            winH = document.body.offsetHeight + 20;
        }
    }

    var scrollTop = document.body.scrollTop;
    if (scrollTop == 0)
    {
        if (window.pageYOffset)
            scrollTop = window.pageYOffset;
        else
            scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }

    if (GetRadWindow() == null)
    {
        oWindow = window.radopen(url, windowId);
    }
    else
    {
        var oBrowserWnd = GetRadWindow().BrowserWindow;
        oWindow = oBrowserWnd.radopen(url, windowId);
    }
    oWindow.SetSize(width, height);
    oWindow.SetTitle(title); // Doesn't work in safari
    //oWindow.get_ContentFrame().contentWindow.document.title = title;
    oWindow.Center();
    //oWindow.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Resize + Telerik.Web.UI.WindowBehaviors.Close);

    if (CallBackFunc != 'undefined' && CallBackFunc != null)
    {
        oWindow.set_clientCallBackFunction(CallBackFunc);
    }
}

function GetXmlHttp()
{
    var x = null;
    try
    {
        x = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            x = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
            x = null;
        }
    }

    if (!x && typeof XMLHttpRequest != "undefined")
    {
        x = new XMLHttpRequest();
    }

    return x;
}