/*  ==================================================
    Standar dialogs of ScriptServer
    The syntax is described at help.scriptserver.com
    
    You can open dialogs modal or modeless by calling
    w3ss_openDialog or w3ss_openPopUp respectively. 
    ==================================================  */
    function w3ss_openPopUp(dialogName, onCompleteEventName, userParam)
    {
        var url = w3ss_composeDialogURL(dialogName, arguments);
        
        // skip width and height for now, perhaps we want to add it later on 
        w3ss_createDialog(url, onCompleteEventName, userParam, true);
    }
    
    function w3ss_openDialog(dialogName, onCompleteEventName, userParam)
    {
        var url = w3ss_composeDialogURL(dialogName, arguments);
        
        // skip width and height for now, perhaps we want to add it later on 
        w3ss_createDialog(url, onCompleteEventName, userParam, false);
    }


    function w3ss_composeDialogURL(dialogName, args)
    {
        var url;
        
        function composeParam(argNo, paramName)
        {
            if(argNo >= args.length)
                return '';
            
            
            return '&'+paramName+'='+args[argNo];
        }

        switch(dialogName.toLowerCase())
        {
            case('properties'):
                url = '/sys/common/dialogs/properties?key='+args[3];
                break;
            
            case('security'):
                url = '/sys/common/dialogs/security?keyOrPath='+args[3];
                break;
            
            case('move'):
                url = '/sys/Common/Dialogs/Move?key='+args[3];
                break;
			
            case('copy'):
                url = '/sys/Common/Dialogs/Copy?key='+args[3];
                break;

            case('mediamanager'):
                url = '/sys/MediaManager?nil=nil';
                url += composeParam(3, 'restrictToBranch');
                url += composeParam(4, 'browseToFolder');
                url += composeParam(5, 'selectionLimit');
                break;
            
            case('createfilefolder'):
                url = '/sys/MediaManager/createFileFolder?path='+args[3];
                break;
            
            case('renamefileandfolder'):
                url = '/sys/MediaManager/renamefileandfolder?path='+args[3];
                break;
         
            case('upload'):
                url = '/sys/common/Dialogs/upload?nil=nil';
                url += composeParam(3, 'restrictToFolder');
                url += composeParam(4, 'restrictToBranch');
                url += composeParam(5, 'suggestedFolder');
                url += composeParam(6, 'restrictToFileName');
                url += composeParam(7, 'maxUploads');
                url += composeParam(8, 'allowOverwrite');
                break;

            case('pickaccounts'):
                url = '/sys/common/dialogs/pickAccounts?nil=nil';
                url += composeParam(3, 'selectionLimit');
                url += composeParam(4, 'restrictToType');
                url += composeParam(5, 'restrictToProvider');
                url += composeParam(5, 'displayAutoAccounts');
                break;
            
            case('picklcid'):
                url = '/sys/common/dialogs/pickLCID?limit='+arguments[3];
                break;
            
            case('pickcolor'):
                url = '/sys/common/dialogs/pickColor';
                break;

            case('pickpage'):
                url = '/sys/common/dialogs/pickPage?nil=nil';
                url += composeParam(3, 'selectionLimit');
                url += composeParam(4, 'expandToKeyOrPath');
                url += composeParam(5, 'docType');
                break;

            case('pickfilefolder'):
                url = '/sys/common/dialogs/pickFileFolder?nil=nil';
                url += composeParam(3, 'selectedFolderPath');
                url += composeParam(4, 'restrictToBranch');
                break;

            case('createlink'):
                url = '/Sys/Common/Dialogs/CreateLink';
                break;

            case('insertrichedittable'):
                url = '/Sys/Common/Dialogs/RichEditTable';
                break;

            case('richedittableproperties'):
                url = '/Sys/Common/Dialogs/RichEditTable/TableProperties';
                break;

            case('testadoconnection'):
                url = '/sys/common/dialogs/testadoconnection?nil=nil';
                url += composeParam(3, 'conString');
                break;

            case('testsmtp'):
                url = '/sys/common/dialogs/testSMTP?nil=nil';
                url += composeParam(3, 'SMTPAddress');
                break;

            default:
                url = dialogName;
                if(url.indexOf('?') == -1)
                    url += '?nil=nil';
                break;
        }
        
        return url;
    }

    
    var w3ss_DialogParamValues = new Array();
    
    function w3ss_createDialog(url, onCompleteEventName, userParam, forceModeless, width, height)
    {
        if(!height)
            height = '500px';
            
        if(!width)
            width = '500px';
		
        var ua = navigator.userAgent.toLowerCase(); 
    	if(!forceModeless && (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1) && parseInt(navigator.appVersion) >= 4)
        {
            var retVal = window.showModalDialog(url, window, 'dialogHeight:'+height+'; dialogWidth:'+width+'; edge:raised; unadorned:yes; status:no; scroll:yes;');
            
            if(onCompleteEventName)
                eval( onCompleteEventName+'(retVal, userParam)' );
        }
        else
        {   
            // Center the dialog
            var x = screen.width;
       	    var y = screen.height;
            
            var top = parseInt(y/2-height/2-16);
            var left = parseInt(x/2-width/2-5);
            
            // save the userParam for later return
            var idx = w3ss_DialogParamValues.length;
            w3ss_DialogParamValues [idx] = userParam;
            
            // create a unique dialog name to avoid window reuse
            var dlgName = "dlg_"+ Date.parse(new Date());
            
            if(url.indexOf('?') == -1)
                url += '?nil=nil';
            
            w3ss_tempWinObj = window.open(url+'&onCompleteEventName='+onCompleteEventName+'&userParamIdx='+idx, dlgName, "width="+width+",height="+height+",top="+top+",left="+left+",scrollbars=yes,resizable=yes");
            window.setTimeout('w3ss_checkForPopupBlocker()', 3000);
        }
    }
    
    var w3ss_tempWinObj;
    
    function w3ss_checkForPopupBlocker()
    {
        if(!w3ss_tempWinObj)
            alert('Your web browser is currently blocking pop up windows. For this web site to function, you will need to allow pop up windows.')
    }
    
    function w3ss_returnFromDialog(retVal, idx, onCompleteEventName)
    {
        if(onCompleteEventName)
            eval( onCompleteEventName+'(retVal, w3ss_DialogParamValues [Number(idx)])' );
    }

