 Type.registerNamespace("Common");

 postback = 0;
 Common.WindowManager = function()
 {
    ///<summary>This class is responsable for the Modal Windows Management Functionality</summary>        
    this.btnRemoveMarked = null;
    this.OnConfirmationDialogClose = null;
 }

 Common.WindowManager.prototype = {

     GetRadWindow: function() {
         ///– gets the current window
         var oWindow = null;

         if (window.radWindow) {
             oWindow = window.radWindow;
         }
         else if (window.frameElement != null && window.frameElement.radWindow) {
             oWindow = window.frameElement.radWindow;
         }

         return oWindow;
     },

     GetActiveWindow: function() {
         return this.GetWindowManager().GetActiveWindow();
     },

     CreateWindowFromWindowManager: function(manager, width, heigth, arguments) {
         active = this.GetRadWindow();

         //One way to open a window from parent page
         var oBrowserWnd = manager.CreateWindowObject();

         oBrowserWnd.Show();
         oBrowserWnd.SetModal(true);
         // doesn't work with methode centre in this case 
         oBrowserWnd.Center();

         if (active != null) {
             oBrowserWnd.Top = parseInt(active.GetTopPosition());
             oBrowserWnd.Left = parseInt(active.GetLeftPosition()) + (active.GetWidth() - width) / 2;
             oBrowserWnd.MoveTo(oBrowserWnd.Left, oBrowserWnd.Top);
         }

         SetRadWindowVisible(oBrowserWnd, true);

         oBrowserWnd.SetWidth(width);
         oBrowserWnd.SetHeight(heigth);

         //An argument can be anything - string, number, object.
         //If it is an object, various properties can be
         //set for carrying multiple information

         if (this.GetRadWindow() != null) {
             if (arguments == null) {
                 arguments = new Object();
             }

             arguments.ParentWindow = this.GetRadWindow().Name;
         }

         oBrowserWnd.Argument = arguments;

         return oBrowserWnd;
     },

     CreateWindow: function(width, heigth, arguments) {
         var manager = this.GetWindowManager();

         return this.CreateWindowFromWindowManager(manager, width, heigth, arguments);
     },

     GetWindowManager: function() {
         /// gets the current RdWindow manager.
         var oManager;

         if (this.GetRadWindow() == null) {
             oManager = GetRadWindowManager();
         }
         else {
             oManager = this.GetRadWindow().GetWindowManager();
         }

         return oManager;
     },

     SuppressRefreshOfParentWindow: function() {
         var manager = this.GetWindowManager();
         var avtiveWindow = manager.GetActiveWindow();

         if (avtiveWindow.Argument == null) {
             avtiveWindow.Argument = new Object();
         }

         avtiveWindow.Argument.isRefreshSuppressed = true;
     },

     EnableRefreshOfParentWindow: function() {
         var manager = this.GetWindowManager();

         var avtiveWindow = manager.GetActiveWindow();

         if (avtiveWindow.Argument == null) {
             avtiveWindow.Argument = new Object();
         }
         avtiveWindow.Argument.ReloadParentOnClose == true;
     },
     //Close Parent Window and display a message on one of windows below
     CloseParentWithMessage: function(messageType, messageString, parentWndName, paramNames, paramValues) {
         var parentWnd = this.GetRadWindow();
         if ((parentWnd) && (parentWnd.Argument) && (parentWnd.Argument.ParentWindow != null) && (parentWnd.Argument.ParentWindow != '')) {
             var manager = this.GetWindowManager();
             var windowToClose = manager.GetWindowByName(parentWnd.Argument.ParentWindow);
             windowToClose.Close();
             if ((parentWndName != null) && (parentWndName != '')) {
                 var parentWindow = manager.GetWindowByName(parentWndName);

                 var refreshLink = this.GetWindowDocument(parentWindow);

                 var refreshLinkTemp;
                 var paramSymbol = '?';
                 if (refreshLink.location.href.indexOf("?opp") > 0) {
                     refreshLinkTemp = refreshLink.location.href.substr(0, refreshLink.location.href.indexOf("opp") - 1) + '?opp=' + messageString;
                 }
                 else {
                     if (refreshLink.location.href.indexOf("&opp") > 0) {
                         refreshLinkTemp = refreshLink.location.href.substr(0, refreshLink.location.href.indexOf("opp") - 1) + '&opp=' + messageString;
                     }
                     else {
                         if (refreshLink.location.href.indexOf("?") > 0) paramSymbol = '&';
                         else paramSymbol = '?';
                         refreshLinkTemp = refreshLink.location.href + paramSymbol + 'opp=' + messageString;

                         if (paramNames != null) {
                             for (i = 0; i < paramNames.length; i++) {
                                 refreshLinkTemp = ReplaceFieldValueInQueryString(paramNames[i], paramValues[i], refreshLinkTemp);
                             }
                         }
                     }

                 }
                 refreshLink.location.href = refreshLinkTemp;
             }
         }
         this.CloseActiveWithMessageOnParent(messageType, messageString, parentWndName);
     },

     GetMessageControl: function(window, isBrowserWindow) {
         var windowMessageControl = null;
         if (NavigatorIsInternetExplorer() == true) {
             if (isBrowserWindow) {
                 windowMessageControl = window.document.Script.msgControl;
             }
             else {
                 windowMessageControl = window.IframeDocument.Script.msgControl;
             }
         }
         else {
             if (isBrowserWindow) {
                 windowMessageControl = window.document.defaultView.msgControl;
             }
             else {
                 windowMessageControl = window.IframeDocument.defaultView.msgControl;
             }
         }

         return windowMessageControl;
     },

     CloseActiveWithMessageOnParent: function(messageType, messageString, parentWndName) {
         var manager = this.GetWindowManager();
         var activeWindow = manager.GetActiveWindow();
         if (activeWindow != null) {
             activeWindow.Close();
         }
         // get window to display the message in it
         var parentWindow = null;
         var isBrowserWindow = false;
         if ((parentWndName != null) && (parentWndName != '')) {
             parentWindow = manager.GetWindowByName(parentWndName);
         }
         else {
             parentWindow = activeWindow.BrowserWindow;
             isBrowserWindow = true;
         }

         if (window.parent != null && window.parent.opener && parentWindow == null) {
             parentWindow = activeWindow.BrowserWindow;
             isBrowserWindow = true;
         }

         if (messageType != null && messageString != null) {
             // messageType & messageString were provided both
             var msgCtrl = windowManager.GetMessageControl(parentWindow, isBrowserWindow);

             switch (messageType) {
                 case MessageType.Message:
                     msgCtrl.SetMessage(messageString);
                     break;

                 case MessageType.Warning:
                     msgCtrl.SetWarning(messageString);
                     break;

                 case MessageType.Error:
                     msgCtrl.SetErrorMessage(messageString);
                     break;

                 default:
                     // do nothing
                     break;
             }
         }
     },

     CloseByName: function(windowName) {

         var oWindow = null;
         if (window.radWindow) oWindow = window.radWindow;
         else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
         oWindow.Close();
     },

     CloseActive: function() {
         ///closes the active window.
         var manager = this.GetWindowManager();
         var activeWindow = manager.GetActiveWindow();

         this.CloseWindow(activeWindow);
     },

     CloseWindow: function(wnd) {
         if (wnd != null) {

             this.HideUnhideFlashCharts(wnd);

             if (wnd.Argument != null
                    && typeof (wnd.Argument.ReloadParentOnClose) != 'undefined'
                    && wnd.Argument.ReloadParentOnClose == true
                    && typeof (wnd.Argument.isRefreshSuppressed) == 'undefined')//suppress refresh in case it is not required for a window
             {
                 this.RefreshParent();
             }
             else {
                 wnd.Close();
             }

             var manager = this.GetWindowManager();
             var avtiveWindow = manager.GetActiveWindow();
             if (avtiveWindow != null && this.GetWindowDocument(avtiveWindow) != null && window.navigator.appName.toLowerCase().indexOf("netscape") == -1) {
                 this.GetWindowDocument(avtiveWindow).documentElement.document.body.focus();
             }
         }
     },

     RefreshActive: function() {
         var manager = this.GetWindowManager();
         var avtiveWindow = manager.GetActiveWindow();

         if (avtiveWindow != null) {
             this.GetWindowDocument(avtiveWindow).location.href = this.GetWindowDocument(oWnd).location.href;
         }
     },

     SetRefreshActiveOnClose: function() {
         var manager = this.GetWindowManager();
         var activeWindow = manager.GetActiveWindow();

         if (activeWindow != null) {
             activeWindow.OnClientClose = function() {
                activeWindow.BrowserWindow.location.href = activeWindow.BrowserWindow.location.href;
             };
         }
     },

     OpenRadWindow: function(url, windowName, arguments) {
         var noCache = '&noCache=';
         if (url.indexOf('?') == -1) {
             noCache = '?noCache=';
         }
         url = url + noCache + Math.random();
         this.OpenRadWindowBase(url, windowName, arguments);
     },


     UnCashRadWindow: function(oWnd) // FIXES : https://cp.compudava.com/show_bug.cgi?id=6850
     {
         if ((oWnd.GetContentFrame() != null) &&
            (oWnd.GetContentFrame().contentWindow != null) &&
            (oWnd.GetContentFrame().contentWindow.document != null)) {
             oWnd.GetContentFrame().contentWindow.document.write('<html><body style="background:transparent;"></body></html>');
             oWnd.GetContentFrame().contentWindow.document.close();
         }
     },

     OpenRadWindowSimple: function(url, windowName) {
         // Avoid adding any logic here, 
         // the purpose of the method is just to open a rad window
         // without any arguments or parent refreshing or anything else.

         var manager = GetRadWindowManager();
         if (NavigatorIsInternetExplorer() == true) {
             manager = this.GetWindowManager();
         }

         var oWnd = manager.GetWindowByName(windowName);

         this.UnCashRadWindow(oWnd);

         oWnd.SetModal(true);
         oWnd.SetUrl(url);
         oWnd.Show();

         return oWnd;
     },

     OpenRadWindowBase: function(url, windowName, arguments) {

         var divChartHolder = $get('divChartHolder');

         if (divChartHolder != null) {

             divChartHolder.style.display = "none";

         }
         ///opens a radWindow with the specified Url, and WindowName, arguments can be optionaly.
         msgControl.ClearMessage();
         var oManager = this.GetWindowManager();
         var oWnd = oManager.GetWindowByName(windowName);

         if (oWnd != null) {

             this.UnCashRadWindow(oWnd);

             oWnd.SetUrl(url);

             if (this.GetRadWindow() != null) {
                 if (arguments == null) {
                     arguments = new Object();
                 }

                 arguments.ParentWindow = this.GetRadWindow().Name;
             }

             oWnd.Argument = arguments;
             //Success. Opening window
             oWnd.Show();

             // Do not remove : this is work arround for Safari Browser...
             SetRadWindowVisible(oWnd, true);
         }
         else {
             if (this.GetRadWindow() != null) {
                 if (arguments == null) {
                     arguments = new Object();
                 }

                 arguments.ParentWindow = this.GetRadWindow().Name;
             }

             var oWnd = radopen(url, windowName);

             oWnd.Argument = arguments;
         }

         return oWnd;
     },

     RefreshParent: function() {
         var radWindow = this.GetRadWindow();

         this.HideUnhideFlashCharts(radWindow);

         if (radWindow != null) {
             if (radWindow.Argument != null && radWindow.Argument.ParentWindow != null) {
                 ///opens a radWindow with the specified Url, and WindowName, arguments can be optionaly.
                 var oManager = this.GetWindowManager();
                 var oWnd = oManager.GetWindowByName(radWindow.Argument.ParentWindow);

                 if (oWnd.Argument == null) {
                     oWnd.Argument = new Object();
                 }

                 oWnd.Argument.ReloadParentOnClose = true;

                 this.GetWindowDocument(oWnd).location.href = this.GetWindowDocument(oWnd).location.href;
             }
             else {
                 radWindow.BrowserWindow.location.href = radWindow.BrowserWindow.location.href;
             }

             radWindow.Close();
         }
     },
     
     RefreshParentByUrl: function(url) {
         var radWindow = this.GetRadWindow();

         if (radWindow != null) {
             radWindow.BrowserWindow.location.href = url;
             radWindow.Close();
         }
     },

     RefreshParentAndAddMsg: function(statutOpp) {
         var refreshLinkTemp;
         var refreshLink;
         var radWindow = windowManager.GetRadWindow();
         if (radWindow != null) {
             if (radWindow.Argument != null && radWindow.Argument.ParentWindow != null) {
                 var oManager = this.GetWindowManager();
                 var oWnd = oManager.GetWindowByName(radWindow.Argument.ParentWindow);
                 refreshLink = oWnd.GetContentFrame().src;
             }
             else {
                 refreshLink = radWindow.BrowserWindow.location.href;
             }
             var paramSymbol = '?';
             if (refreshLink.indexOf("?opp") > 0) {
                 refreshLinkTemp = refreshLink.substr(0, refreshLink.indexOf("opp") - 1) + '?opp=' + statutOpp;
             }
             else {
                 if (refreshLink.indexOf("&opp") > 0) {
                     refreshLinkTemp = refreshLink.substr(0, refreshLink.indexOf("opp") - 1) + '&opp=' + statutOpp;
                 }
                 else {
                     if (refreshLink.indexOf("?") > 0) paramSymbol = '&';
                     else paramSymbol = '?';
                     refreshLinkTemp = refreshLink + paramSymbol + 'opp=' + statutOpp;
                 }

             }
             if (radWindow.Argument != null && radWindow.Argument.ParentWindow != null) {
                 oWnd.GetContentFrame().src = refreshLinkTemp;
             }
             else {
                 radWindow.BrowserWindow.location.href = refreshLinkTemp;
             }
             radWindow.Close();
         }
     },

     RefreshWindowWithMessageInURL: function(parentWindow, paramName, meesageText) {
         if (parentWindow != null) {
             var refreshLink = this.GetWindowDocument(parentWindow);

             var refreshLinkTemp;
             var paramSymbol = '?';
             if (refreshLink.location.href.indexOf("?" + paramName) > 0) {
                 refreshLinkTemp = refreshLink.location.href.substr(0, refreshLink.location.href.indexOf(paramName) - 1) + '?' + paramName + '=' + meesageText;
             }
             else {
                 if (refreshLink.location.href.indexOf("&" + paramName) > 0) {
                     refreshLinkTemp = refreshLink.location.href.substr(0, refreshLink.location.href.indexOf(paramName) - 1) + '&' + paramName + '=' + meesageText;
                 }
                 else {
                     if (refreshLink.location.href.indexOf("?") > 0) paramSymbol = '&';
                     else paramSymbol = '?';
                     refreshLinkTemp = refreshLink.location.href + paramSymbol + paramName + '=' + meesageText;
                 }

             }
             refreshLink.location.href = refreshLinkTemp;
         }
     },

     ConfirmItemDeletion: function(url, btnRemoveMarked, messageKey) {
         var argument = new Object();
         argument.btnRemoveMarked = btnRemoveMarked;

         // In some cases client-side messageKey contains already translated message.
         // In some cases TR_CLIENT_MESSAGES is undefined.
         if ((TR_CLIENT_MESSAGES != null) && (TR_CLIENT_MESSAGES[messageKey]) != null) {
             var wnd = windowManager.OpenRadWindow(url + TR_CLIENT_MESSAGES[messageKey], 'ConfirmationDialog', argument);

         }
         else {
             var wnd = windowManager.OpenRadWindow(url + messageKey, 'ConfirmationDialog', argument);
         }

         return false;
     },

     AfterConfirmDialogClosed: function(radWindow) {
         //debugger;
         if (radWindow != null && radWindow.Argument) {
             if (typeof (radWindow.Argument.dlgResult) != 'undefined' && radWindow.Argument.dlgResult == true) {

                 if (radWindow.YesButtonClicked != null) {
                     if (radWindow.YesButtonClicked == true) {
                         var button = $get(radWindow.Argument.btnRemoveMarked);

                         if (button == null) {
                             // button is null - try to get the button from parent window
                             var parentWnd = windowManager.GetWindowByName(radWindow.Argument.ParentWindow);
                             var parentWndDoc = parentWnd.IframeDocument;
                             button = parentWndDoc.getElementById(radWindow.Argument.btnRemoveMarked)
                         }

                         if (button && button.click) {
                             button.click();
                         }
                     }
                     else {
                         // do nothing : do not remove this else block,
                         // the popup is just being closed
                     }
                 }
                 else if (radWindow.Argument.btnRemoveMarked != null) {
                     var button = $get(radWindow.Argument.btnRemoveMarked);

                     if (button == null) {
                         // button is null - try to get the button from parent window
                         var parentWnd = windowManager.GetWindowByName(radWindow.Argument.ParentWindow);
                         var parentWndDoc = parentWnd.IframeDocument;
                         button = parentWndDoc.getElementById(radWindow.Argument.btnRemoveMarked)
                     }

                     if (button && button.click) {
                         button.click();
                     }
                 }

                 if (windowManager.OnConfirmationDialogClose != null)
                     this.OnConfirmationDialogClose(true);
             }
             else {
                 if (typeof (OnNoPressed) != 'undefined') {
                     OnNoPressed();
                 }

                 if (windowManager.OnConfirmationDialogClose != null)
                     windowManager.OnConfirmationDialogClose(false);
             }
         }
     },

     GetWindowByName: function(radWndName) {
         return this.GetWindowManager().GetWindowByName(radWndName);
     },

     GetElementByIdFromParentWindow: function(radWindow, elementId) {
         return this.GetWindowDocument(radWindow.GetWindowManager().GetWindowByName(radWindow.Argument.ParentWindow)).getElementById(elementId);
     },

     GetWindowDocument: function(oWnd) {
         if (oWnd == null || oWnd.IframeDocument == undefined) {
             return null;
         }

         try {
             if (oWnd.IframeDocument.documentElement.ownerDocument) {
                 return oWnd.IframeDocument.documentElement.ownerDocument;
             }
             else {
                 return oWnd.IframeDocument.documentElement.document;
             }
         }
         catch (err) {
             return null;
         }
     },

     HideUnhideFlashCharts: function(wnd) {

         var divChartHolder = wnd.BrowserWindow.document.getElementById('divChartHolder');

         if (divChartHolder != null) {

             divChartHolder.style.display = "";
         }
     },

     RefreshLists: function() {
         var manager = this.GetWindowManager();
         var radWindow = manager.GetActiveWindow();
         if (radWindow != null && radWindow.Argument) {
             if (typeof (radWindow.Argument.RefreshList) != 'undefined') {
                 var btnRefreshList = radWindow.BrowserWindow.document.getElementById(radWindow.Argument.RefreshList);
                 if (null == btnRefreshList) {
                     btnRefreshList = this.GetElementByIdFromParentWindow(radWindow, radWindow.Argument.RefreshList)
                 }
                 if (btnRefreshList != null) {
                     btnRefreshList.click();
                 }
             }
         }
     }
 }
 
 
Common.MessageType = function() {}
Common.MessageType.prototype = 
{
    Error:   1,
    Warning: 2,
    Message: 3
}


Common.MessageType.registerClass('Common.MessageType', null, Sys.IDisposable);
Common.WindowManager.registerClass('Common.WindowManager', null, Sys.IDisposable);

var MessageType = new Common.MessageType();

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

function OnClientShow(windowManager)
    {    
        if (!window.opener)
        {
            var radwindow = windowManager.GetRadWindow();
            if(radwindow != null )
            {
                var iframe = windowManager.GetRadWindow().GetContentFrame();
                if (iframe != null)
                {
                    iframe.allowTransparency = true;
                    iframe.style.background = "transparent";
                }
            }
        }
    }
