function SapphireLookup(){
    this.api_desc = 'Sapphire API for managing lookup pages and lookup dialogs.';
    this.blankUrl = 'WEB-CORE/blank.html';

    this.open = function(sUrl, sName, sFeatures, lReplace, lSapphire){
        function getFeature(sString, sFeature){
            var sT = '';
            var iH = sString.lastIndexOf(sFeature + '=');
            if (iH > -1){
                sT = sString.substr(iH + sFeature.length + 1);
                iH = sT.indexOf(',');
                if (iH > -1){
                    sT = sT.substr(0, iH);
                }
            }
            return sT;
        }
        var iHeight = 800;
        var iWidth = 600;
        var lMax = false;
        if (typeof(sFeatures) != 'undefined'){
            sFeatures = sFeatures.toLowerCase();
            iHeight = parseInt(getFeature(sFeatures,'height'));
            if (isNaN(iHeight)){
                iHeight = 800;
            }
			iWidth = parseInt(getFeature(sFeatures, 'width'));
			if ( isNaN(iWidth) ) {
				iWidth = 600;
			}
			var sT = getFeature(sFeatures,'fullscreen');
            lMax = (sT == 'yes' || sT == '1');
        }
        if (typeof(sName) == 'undefined'){
            sName = 'unnamed';
        }
        this.util.openWindow(sName, 'SAPPHIRE', sUrl, iWidth, iHeight, lSapphire, null, lMax);
        if (lSapphire){
            return this.util.lookupDialog;
        }
        else{
            return this.util.lookupWindow;
        }
    }

    this.link = {
        api_desc: 'Sapphire API for showing link lookup.',
        open: function(sFieldName){
            var oField = document.getElementById(sFieldName);
            if (oField != null){
                sapphire.lookup.util.openWindow( 'LookupLink', 'Lookup Link', 'rc?command=file&file=WEB-CORE/modules/webadmin/editorlookup_link.jsp&btns=N&url=' + escape(oField.value),
                        600, 200, true );
                var oDialog = sapphire.lookup.util.lookupDialog;
                sapphire.ui.dialog.addButtons(oDialog,{'OK':'this.dialog.frame.ok()','Cancel':'this.dialog.frame.cancel()'},true);
                oDialog.fieldName = sFieldName;
                oDialog.propertyid = 'newurl';
                oDialog.dialogArguments = [];
                oDialog.dialogCallback = 'sapphire.lookup.link.open_callback';
            }
        },
        open_callback: function(oDialog){
            var oArgs = oDialog.dialogArguments;
            if ( oArgs != null ) {
                var oField = oDialog.opener.document.getElementById(oDialog.fieldName);
                var sNew = oArgs[ oDialog.propertyid ];
                if ( sNew != undefined && sNew.length > 0 && oField != null ){
                    oField.value = sNew;
                    oField.fireEvent('onchange');
                }
            }
        }
    }

    this.date = {
        api_desc: 'Sapphire API for showing date lookup.',
        newlookup: null,
        open: function( sFieldId, sNotifyMethod, sDSName, sIndex, sColumnId, sFormat, lSapphire ){
            if ( typeof( sNotifyMethod ) == 'undefined' ){
                sNotifyMethod = '';
            }
            if ( typeof( sDSName ) == 'undefined' ){
                sDSName = '';
            }
            if ( typeof( sIndex ) == 'undefined' ){
                sIndex = '';
            }
            if ( typeof( sColumnId ) == 'undefined' ){
                sColumnId = '';
            }
            if ( typeof( sFormat ) == 'undefined' ){
                sFormat = '';
            }

            var oPL = sapphire.util.propertyList.create();
            oPL.set( 'fieldid', sFieldId );
            oPL.set( 'notifymethod', sNotifyMethod );
            oPL.set( 'dsname', sDSName );
            oPL.set( 'index', sIndex );
            oPL.set( 'columnid', sColumnId );
            oPL.set( 'format', sFormat );
            if ( typeof( lSapphire ) == 'undefined' ){
                lSapphire = true;
            }
            sapphire.lookup.util.openWindow( 'datelookup', 'Lookup Date', 'rc?command=file&file=WEB-CORE/lookup/calendar.jsp', 290, 250, lSapphire, oPL );

        }
    }

    this.sdi = {
        api_desc: 'Sapphire API for looking up sdis.',        
        openSimple: function( sFieldId, sSDCId ){
            this.open( sFieldId, sSDCId );    
        },
        openPage: function( sFieldId, sSDCId, sPageId, sMapColumnId, lUseSapphireDialog, oPageDirectives ){
            this.open( sFieldId, sSDCId, '', 'N', '', '','','','','','', sPageId, sMapColumnId, lUseSapphireDialog, oPageDirectives );
        },
        open: function( sFieldId, sSDCId, sColumns, sMultiselect, sNotifyMethod, sKeyId1, sKeyId2, sKeyId3, sDSName, sIndex, sColumnId, sPageId, sMapColumnId, lUseSapphireDialog, oPageDirectives ){
            var sUrl = 'rc?command=file&file=WEB-CORE/lookup/lookup.jsp';
			if ( typeof(sPageId) != 'undefined' && sPageId.length > 0 ) {
                if ( sPageId.indexOf( 'rc?' ) > -1 ){
                    sUrl = sPageId;
                }
                else{
                    sUrl = 'rc?command=page&page=' + sPageId;
                }
            }
            if ( sUrl.indexOf('[') > -1 ){
                sUrl = sapphire.util.evaluateExpression(sUrl);
            }
            if ( typeof( sKeyId1 ) != 'undefined' && sKeyId1.length > 0 ){
                sUrl += '&keyid1=' + sKeyId1;
            }
            if ( typeof( sKeyId2 ) != 'undefined' && sKeyId2.length > 0 ){
                sUrl += '&keyid2=' + sKeyId2;
            }
            if ( typeof( sKeyId3 ) != 'undefined' && sKeyId3.length > 0 ){
                sUrl += '&keyid3=' + sKeyId3;
            }
            sUrl += '&sdcid=' + sSDCId;
            var oPL = sapphire.util.propertyList.create();
            oPL.set('fieldid', sFieldId);
            if ( typeof( sMapColumnId ) == 'undefined' ){
                sMapColumnId = '';
            }            
            oPL.set('mapcolumnid', sMapColumnId);
            if ( typeof( sColumns ) != 'undefined' && sColumns.length > 0 ){
                oPL.set('columns', sColumns);
            }
            if ( typeof( sMultiselect ) == 'undefined' || sMultiselect.length == 0 ){
                sMultiselect = 'N';
            }
            oPL.set('multiselect', sMultiselect);
            if ( typeof( sNotifyMethod ) != 'undefined' && sNotifyMethod.length > 0 ){
                oPL.set('notify', sNotifyMethod);
            }
            if ( typeof( sDSName ) != 'undefined' && sDSName.length > 0 ){
                oPL.set('dsname', sDSName);
            }
            if ( typeof( sIndex ) != 'undefined' && sIndex.length > 0 ){
                oPL.set('index', sIndex);
            }
            if ( typeof( sColumnId ) != 'undefined' && sColumnId.length > 0 ){
                oPL.set('columnid', sColumnId);
            }
            if ( typeof( oPageDirectives ) != 'undefined' ) {
                if ( typeof( oPageDirectives ) == 'string' ) {
                    oPL.set('__pagedirectives', oPageDirectives );
                }
                else {
                    oPL.set('__pagedirectives', objectToJSONString(oPageDirectives) );
                }
            }
            sapphire.lookup.util.openWindow( 'sdilookup', 'Sapphire Lookup', sUrl, 600, 400, lUseSapphireDialog, oPL );
        },
        openDefault: function( sFieldId, sSDCId, sMultiselect, sNotifyMethod, lUseSapphireDialog ){
            this.open( sFieldId, sSDCId, '', sMultiselect, sNotifyMethod, '','','','','','', '', '', lUseSapphireDialog );
        },
        clear: function( sFieldId ){
            if ( typeof(sFieldId) != 'undefined' && sFieldId.length > 0 ){
                var oaFields = sFieldId.split(';');
                var iL = oaFields.length;
                var oDoc = document;
                for ( var iIndex = 0; iIndex < iL; iIndex++ ){
                    var oEl = oDoc.getElementById(oaFields[iIndex]);
                    if ( oEl != null ){
                        oEl.value = '';
                        oEl.fireEvent( 'onchange' );
                    }
                }
            } 
        }
    }

    this.fileSystem = {
        api_desc: 'Sapphire API for showing server file dialog.',
        fileLookup: null,
        browseFile: function ( vElement, vFileType, sBaseDir, sFileLocationType ){
            this.create( vElement, vFileType, sBaseDir, false, false, sFileLocationType );
        },
        browseWeb: function( vElement, vFileType, sBaseDir, sFileLocationType ){
            if ( sBaseDir != null && (( sBaseDir.indexOf( 'EAServer' ) >= 0 || sBaseDir.indexOf( 'Sybase' ) >= 0 ) )&& sBaseDir.indexOf('/Repository/WebApplication') == -1 ){
                sBaseDir += '/Repository/WebApplication';
            }
            this.create( vElement, vFileType, sBaseDir, true, true, sFileLocationType, true );
        },
        browseZip: function( vElement, vFileType, sBaseDir, sFileLocationType ){
            this.create( vElement, vFileType, sBaseDir, false, false, sFileLocationType, false, false, false, true );
        },
        browseFolder: function( vElement, sBaseDir, sFileLocationType ){
            this.create( vElement, '', sBaseDir, false, false, sFileLocationType, false, true, false );
        },
        createURL: function( vElement, vFileType, sBaseDir, lRelative, lForceSub, sFileLocationType, lWebFile,
                          lFoldersOnly, lDirLock, lUseZip,
                          vShortCuts, sActiveExtension, lValidateFiles,
                          sFunctionText, sTitleText, sButtonText ){
               var sElementId = '';
            var sURLArgs = '';

            if ( typeof( vElement ) == 'string' ){
                sElementId = vElement;
            }
            else{
                if ( typeof( vElement.id ) != 'undefined' ){
                    sElementId = vElement.id;
                }
            }

            if ( typeof( lRelative ) == 'undefined' || lRelative == null ){
                if ( typeof( sBaseDir ) != 'undefined' && sBaseDir != null && baseDir.length > 0 ) {
                    sURLArgs += '&relative=Y';
                 }
                 else {
                    sURLArgs += '&relative=N';
                }
            }
            else{
                sURLArgs += lRelative ? '&relative=Y' : '&relative=N';
            }

            if ( typeof( lForceSub ) == 'undefined' ){
                if ( lRelative ){
                    sURLArgs += '&forcesub=Y';
                }
                else{
                    sURLArgs += '&forcesub=N';
                }
            }
            else{
                sURLArgs += lForceSub ? '&forcesub=Y' : '&forcesub=N';
            }

            if ( typeof( sBaseDir ) != 'undefined' && sBaseDir != null && sBaseDir.length > 0 ){
                sURLArgs += '&basedir=' + sBaseDir;
            }

            if ( typeof( sFunctionText ) != 'undefined' && sFunctionText != null && sFunctionText.length > 0 ){
                sURLArgs += '&function=' + sFunctionText;
            }
            if ( typeof( sTitleText ) != 'undefined' && sTitleText != null && sTitleText.length > 0 ){
                sURLArgs += '&title=' + sTitleText;
            }
            if ( typeof( sButtonText ) != 'undefined' && sButtonText != null && sButtonText.length > 0 ){
                sURLArgs += '&button=' + sButtonText;
            }

            if ( typeof( vFileType ) != 'undefined' &&  vFileType != null ){
                if ( typeof( vFileType ) == 'string' ){
                    sURLArgs += '&filetype=' + vFileType;
                }
                else{
                    var sExtensions = '';
                    for ( var oKey1 in vFileType ){
                        if ( sExtensions.length > 0 ){
                            sExtensions += ';' + oKey1 + '|' + vFileType[oKey1];
                        }
                        else{
                            sExtensions = oKey1 + '|' + vFileType[oKey1];
                        }
                    }
                    sURLArgs += '&extensions=' + sExtensions;
                }
            }

            if ( typeof( vShortCuts ) != 'undefined' &&  vShortCuts != null ){
                if ( typeof( vShortCuts ) == 'string' ){
                    sURLArgs += '&shortcuts=' + vShortCuts;
                }
                else{
                    var sShorts = '';
                    for ( var oKey in vShortCuts ){
                        if ( sShorts.length > 0 ){
                            sShorts += ';' + oKey + '|' + vShortCuts[oKey];
                        }
                        else{
                            sShorts = oKey + '|' + vShortCuts[oKey];
                        }
                    }
                    sURLArgs += '&shortcuts=' + sShorts;
                }
            }

            if ( typeof( sFileLocationType )!= 'undefined' && sFileLocationType != null && sFileLocationType.length > 0 ){
                sURLArgs += '&filelocationtype=' + sFileLocationType
            }
            if ( typeof( sActiveExtension )!= 'undefined' && sActiveExtension != null && sActiveExtension.length > 0 ){
                sURLArgs += '&activeextension=' + sActiveExtension
            }
            if ( typeof( lWebFile )!= 'undefined' && lWebFile != null ){
                sURLArgs += lWebFile ? '&web=Y' : '&web=N';
            }
            if ( typeof( lValidateFiles )!= 'undefined' && lValidateFiles != null ){
                sURLArgs += lValidateFiles ? '&validate=Y' : '&web=N';
            }
            if ( typeof( lFoldersOnly )!= 'undefined' && lFoldersOnly != null ){
                sURLArgs += lFoldersOnly ? '&folders=Y' : '&folders=N';
            }
            if ( typeof( lDirLock )!= 'undefined' && lDirLock != null ){
                sURLArgs += lDirLock ? '&dirlock=Y' : '&dirlock=N';
            }
            if ( typeof( lDirLock )!= 'undefined' && lDirLock != null ){
                sURLArgs += lDirLock ? '&dirlock=Y' : '&dirlock=N';
            }
            if ( typeof( lUseZip )!= 'undefined' && lUseZip != null ){
                sURLArgs += lUseZip ? '&usezip=Y' : '&usezip=N';
            }
            return 'rc?command=file&file=WEB-CORE/utils/lookup/filesystem.jsp&fieldid=' +
                    sElementId +
                    '&' + sURLArgs;
        },
        create: function( vElement, vFileType, sBaseDir, lRelative, lForceSub, sFileLocationType, lWebFile,
                          lFoldersOnly, lDirLock, lUseZip,
                          vShortCuts, sActiveExtension, lValidateFiles,
                          sFunctionText, sTitleText, sButtonText ){
           this.createForTarget(null, vElement, vFileType, sBaseDir, lRelative, lForceSub, sFileLocationType, lWebFile,
                          lFoldersOnly, lDirLock, lUseZip,
                          vShortCuts, sActiveExtension, lValidateFiles,
                          sFunctionText, sTitleText, sButtonText);
        },
        createForTarget: function( sTarget, vElement, vFileType, sBaseDir, lRelative, lForceSub, sFileLocationType, lWebFile,
                          lFoldersOnly, lDirLock, lUseZip,
                          vShortCuts, sActiveExtension, lValidateFiles,
                          sFunctionText, sTitleText, sButtonText ){
            var sURL = this.createURL(vElement, vFileType, sBaseDir, lRelative, lForceSub, sFileLocationType, lWebFile,
                              lFoldersOnly, lDirLock, lUseZip,
                              vShortCuts, sActiveExtension, lValidateFiles,
                              sFunctionText, sTitleText, sButtonText);
            if (typeof(sTarget) == 'undefined' || sTarget == null || sTarget.length == 0 || sTarget.toLowerCase() == '_blank'){
                sapphire.ui.dialog.open( 'Open',
                        sURL,
                        true, 600, 400, null, null, true, true );
            }
            else{
                sapphire.page.navigate(sURL, 'N', sTarget, null);
            }
        }
    }

    this.util = {
        debug: false,
        location: false,
        lookupWindow: null,
        lookupDialog: null,
        api_desc: 'Sapphire API for lookup utility methods.',
        getCoord: function( iWidth, iHeight ){
            var iLeft = ( window.screen.availWidth / 2 ) - ( iWidth / 2 );
            var iTop = ( window.screen.availHeight / 2 ) - ( iHeight / 2 );
            return [iLeft,iTop];
        },
        closeWindow: function(){
            if ( this.lookupWindow != null && typeof(this.lookupWindow.window) != 'unknown' ){
                try{
                    this.lookupWindow.close();
                }
                catch(e){}
                this.lookupWindow = null;                
            }
            if ( this.lookupDialog != null ){
                sapphire.ui.dialog.close( this.lookupDialog.dialogNumber );
                this.lookupDialog = null;                
            }
        },
        openWindow: function( sName, sTitle, sUrl, iWidth, iHeight, lSapphire, oPropertyList, lMax, lFocus ){
            if ( typeof( lFocus ) == 'undefined' ){
                lFocus = false;
            }
            if ( typeof( sName ) == 'undefined' || sName == null || sName.length == 0 ){
                sName = 'sapphirelookup';
            }
            if ( window.name == sName || top.name == sName ){
                sName = '_' + sName;
            }
            if ( sUrl.indexOf( 'rc?' ) == -1 && sUrl.indexOf('http://') == -1 && sUrl.indexOf('/') == -1 ) {
                sUrl = 'rc?command=page&page=' + sUrl;
            }            
            if ( typeof( sTitle ) == 'undefined' || sTitle == null || sTitle.length == 0 ){
                sTitle = 'Sapphire';
            }
            if ( typeof( oPropertyList ) == 'undefined' || oPropertyList == null ){
                oPropertyList = sapphire.util.propertyList.create();
            }
            else if ( typeof(oPropertyList.set) == 'undefined' ||  typeof(oPropertyList.toForm) == 'undefined' ){
                oPropertyList = sapphire.util.propertyList.create(oPropertyList);
            }
           
            oPropertyList.set('connectionid',sapphire.connection.connectionId);            
            var oForm = oPropertyList.toForm();
            if ( typeof(lSapphire) == 'undefined' || !lSapphire ){
                var iaCoord = this.getCoord( iWidth, iHeight );
                if ( this.lookupWindow != null && typeof(this.lookupWindow.window) != 'unknown' ){
                    if (!lFocus ){
                        this.lookupWindow.close();
                    }
                    else{
                        this.lookupWindow.focus();
                        return false;
                    }
                }

                if ( !this.debug ){
                    var sProps = 'resizable=yes,height=' + iHeight + ',width=' + iWidth + ',left=' + iaCoord[0] + ',top=' + iaCoord[1];
                    if (this.location){
                        sProps += ',location=yes';
                    }
                    this.lookupWindow = window.open( sapphire.lookup.blankUrl, sName, sProps );
                }
                try{
                    //oForm.action = sUrl;
                    sapphire.util.url.addToForm(sUrl,oForm,sapphire.util.url.defaultExcludes);
                    if ( !this.debug ){
                        oForm.target = sName;
                    }
                    oForm.method = 'POST';
                    document.body.appendChild( oForm );
                    oForm.submit();
                    if ( typeof(lMax) != 'undefined' && lMax && !this.debug ){
                        this.lookupWindow.moveTo( 0,0 );
                        this.lookupWindow.resizeTo( screen.availWidth, screen.availHeight );
                    }
                }
                finally{
                    sapphire.garbage.add( oForm );
                }
            }
            else{
                try{
                    if ( this.lookupDialog != null ){
                        if ( !lFocus ){
                            sapphire.ui.dialog.close( this.lookupDialog.dialogNumber );
                        }
                        else{
                            sapphire.ui.dialog.focus(this.lookupDialog.dialogNumber);
                            return false;
                        }
                    }
                    this.lookupDialog = sapphire.ui.dialog.open( sTitle, sUrl, true, iWidth, iHeight, null, oForm, true, true );
                    if ( typeof(lMax) != 'undefined' && lMax ){
                        sapphire.ui.dialog.maximize( this.lookupDialog.dialogNumber );
                    }
                }
                finally{
                    sapphire.garbage.add( oForm );
                }
            }
        },
        pageDirectives: {
            getDefault: function( sSDCId, sTitle ){
                var oPd = sapphire.util.propertyList.create();
                var oLayout = sapphire.util.propertyList.create();
                oLayout.set( 'objectname', 'WEB-OPAL/layouts/popup/popuplayout.jsp');
                oLayout.set( 'stylesheet', 'WEB-OPAL/stylesheets/opal.css');
                oLayout.set( 'applicationtitle', sapphire.translate('SAPPHIRE'));
                oLayout.set( 'hidetitle', 'Y');
                oPd.set( 'layout', oLayout );
                oPd.set( 'sdcid', sSDCId );
                oPd.set( 'rowsperpage', '50' );
                oPd.set( 'selectortype', 'none' );
                oPd.set('title', sTitle);
                return oPd;
            },
            addButton: function( oPageDirectives, sText, sStandardAction, sTip, sImage, sPageUrl, sTarget, lStoreSelection, sId ){
                var oButtons;
                if( typeof(sPageUrl) == 'undefined' ){
                    sPageUrl = '';
                }
                if( typeof(sTarget) == 'undefined' ){
                    sTarget = '_top';
                }
                else if(sPageUrl.indexOf('http:') == -1 && sPageUrl.indexOf('rc?') == -1){
                    sPageUrl = 'rc?command=page&page=' + sPageUrl;    
                }
                if (typeof(oPageDirectives['buttons']) != 'undefined'){
                    oButtons = oPageDirectives.getCollection('buttons');
                }
                else{
                    oButtons = sapphire.util.propertyListCollection.create();
                    oPageDirectives.set('buttons', oButtons);
                }
                var oBut = sapphire.util.propertyList.create();
                var oCP = sapphire.util.propertyList.create();
                oCP.set('text',sapphire.translate(sText));
                if ( typeof(sTip) != 'undefined' ){
                    oCP.set('tip',sapphire.translate(sTip));
                }
                if ( typeof(sImage) != 'undefined' ){
                    oCP.set('image',sImage);
                }
                oBut.set('commonprops', oCP);
                if (typeof(sId) != 'undefined'){
                    oBut.set('id',sId);
                }
                else{
                    oBut.set('id','button' + oButtons.length);
                }
                if ( typeof(sStandardAction) != 'undefined' && sStandardAction.length > 0 ){
                    var oSBP = sapphire.util.propertyList.create();
                    oSBP.set('action', sStandardAction );
                    oSBP.set('page', sPageUrl );
                    sStandardAction = sStandardAction.toLowerCase();
                    if ( lStoreSelection && ( sStandardAction == 'add' || sStandardAction == 'edit')){
                        oSBP.set('storeselection', 'Y' );
                    }
                    oBut.set('standardbuttonprops',oSBP);
                }
                oButtons.add(oBut);
                return oBut;
            },
            addColumn: function(oPageDirectives, sTitle, sColumnId, lDisplay, lReturnValue, sLinkHref, sLinkTip, sId ){
                var oColumns;
                if (typeof(oPageDirectives['columns']) != 'undefined'){
                    oColumns = oPageDirectives.getCollection('columns');
                }
                else{
                    oColumns = sapphire.util.propertyListCollection.create();
                    oPageDirectives.set('columns', oColumns);
                }
                var oCol = sapphire.util.propertyList.create();
                oCol.set('title', sapphire.translate(sTitle));
                oCol.set('columnid', sColumnId);
                if (lDisplay){
                    oCol.set('mode', 'Display Text');
                }
                else{
                    oCol.set('mode', 'Hidden Value');
                }
                if (typeof(sLinkHref) != 'undefined' && sLinkHref.length > 0){
                    var oLink = sapphire.util.propertyList.create();
                    oLink.set('href', sLinkHref);
                    oLink.set('tip', sapphire.translate(sLinkTip));
                    oCol.set('link', oLink);
                }
                if (lReturnValue){
                    oCol.set('returnvalue', 'Y');
                }
                else{
                    oCol.set('returnvalue', 'N');
                }
                if (typeof(sId) != 'undefined'){
                    oCol.set('id',sId);
                }
                else{
                    oCol.set('id','column' + oColumns.length);
                }

                oColumns.add(oCol);
                return oCol;
            }
        }


    }
    this.fileSystem.util = this.util;

}


