
function selectNotesDatabase ()
{
   conn = form.loadConnector();
   conn.initialize (null);
   var sel = form.chooseFromList ("Select Database", conn.queryDatabases());
   if (sel != null)
   {
      form.setConfig (button.getName(), sel);
      form.updateControl (button.getName());
   }
   conn.terminate();
}

function selectNotesView()
{
   conn = form.loadConnector();
   conn.initialize (null);
   var sel = form.chooseFromList ("Select View", conn.queryViews());
   if (sel != null)
   {
      form.setConfig (button.getName(), sel);
      form.updateControl (button.getName());
   }
   conn.terminate();
}

function selectJDBCTable ()
{
   conn = form.loadConnector();
   conn.initialize (null);
   var sel;
   try 
   {
      sel = form.chooseFromList ("Select Table", conn.queryTables());
   }
   catch (e)
   {
      conn.terminate();
      throw e;
   }
   if (sel != null)
   {
      if ( sel.indexOf(" ") != -1 )
         sel = "'" + sel + "'";
      form.setConfig (button.getName(), sel);
      form.updateControl (button.getName());
   }
   conn.terminate();
}

function selectSysStoreTable ()
{
   conn = form.loadConnector();
   conn.initialize (null);
   var sel;
   try 
   {
      sel = form.chooseFromList ("Select Table", conn.queryTables());
   }
   catch (e)
   {
      conn.terminate();
      throw e;
   }
   if (sel != null)
   {
      if ( sel.indexOf(" ") != -1 )
         sel = "'" + sel + "'";
      form.setConfig (button.getName(), sel);
      form.updateControl (button.getName());
   }
   conn.terminate();
}



function deleteDelta ()
{
   var path = form.getConfigValue ("deltaDB");
   if (path == null || path.length() == 0)
      return;

   if ( form.getConfigValue("driver").equals ("CloudScape") )
   {
      if ( form.prompt ("Delete table " + path ) )
      {
		var table = path;
		if (! table.startsWith("IDI_DS_"))
		   table = "IDI_DS_" + path;
         var str = Packages.com.ibm.di.store.StoreFactory.dropTable ( table );
         if ( str != null )
            form.alert ( str );
         else
            form.alert ("Table " + path + " deleted" );
		Packages.com.ibm.di.store.StoreFactory.shutdown();
      }
   } else
   {
      if (form.prompt("Delete " + path))
      {
         if (!form.deleteFile (path))
            form.alert ("Delta DB non existing or access denied");
         else
            form.alert ("Delta DB Deleted");
      }
   }
}

function selectFile ()
{
   selectFileWithFilter ( null )
}

function selectXMLFile ()
{
   selectFileWithFilter ( "xml" )
}

function verifyXMLFile ()
{
   form.loadNamespace()
}

function selectConfigFile ()
{
   selectFileWithFilter ( "config" )
}

function selectFileWithFilter ( filter )
{
   var name = button.getName();
   var path = form.selectFile ( form.getConfig ( name ), filter );
   if ( path != null )
   {
      form.setConfig ( name, path );
      form.updateControl ( name );
   }
}

function selectMultipleFiles ()
{
   var name = button.getName();
   var paths = form.addFiles ( form.getConfig ( name ) );
   if ( paths != null )
   {
        form.setConfig ( name, paths );
        form.updateControl ( name );
   }
}

function chooseColor ()
{
   form.chooseColor ( button.getName() );
}

function selectGlobalProlog ()
{
   var value = form.selectGlobalScripts (form.getConfig(button.getName()));
   if ( value != null )
   {
      form.setConfig (button.getName(), value);
      form.updateControl ( button.getName() );
   }
}

function selectLDAPSearchBase ()
{
   conn = form.loadConnector();
   conn.initialize (null);
   var sel = form.chooseFromList ("Select Table", conn.queryNamingContexts());
   if (sel != null)
   {
      form.setConfig (button.getName(), sel);
      form.updateControl (button.getName());
   }
   conn.terminate();
}

function checkLDAPPageSize () {
   conn = form.loadConnector();
   conn.initialize (null);
   if ( conn.supportsPagedResults() )
   {
      form.alert ("OK - Server says it supports the Paged Search control");
   } else
   {
      var e = conn.getServerInfo();
      if (e.getString("error") != null )
         form.alert ("Warning: Server does not respond to this query. Paged search may work.");
      else
         form.alert ("This LDAP server does not support paged search.");
   }

}

function getLastNSChangeNumber ()
{
   conn = form.loadConnector();
   conn.initialize (null)
   var ctx = conn.getLdapContext();
   var attrs = ctx.getAttributes("");
   if ( attrs == null )
   {
      form.alert ("Sorry, this LDAP server does not provide server information");
      conn.terminate();
      return;
   }
   var fcn = attrs.get("firstchangenumber");
   var lcn = attrs.get("lastchangenumber");
   if ( fcn == null )
   {
      form.alert ("The \"firstchangenumber\" Context attribute of this LDAP server does not exist. Check the LDAP Server configuration.");
   } else
   {
      var str = "First Changenumber = " + fcn.get() + "\nLast Changenumber = " + lcn.get();
      form.alert ( str );
      form.setConfig (button.getName(), fcn.get());
   }
   
   form.updateControl (button.getName());
   conn.terminate();
   
}

function deleteIteratorStateKey ()
{
   var ss = new Packages.com.ibm.di.store.PropertyStore (Packages.com.ibm.di.store.StoreFactory.DEFAULT_PROPERTYSTORE);
   ss.removeProperty ( form.getConfig ( button.getName() ) );
   ss.closeStore();
   Packages.com.ibm.di.store.StoreFactory.shutdown();
}

function getChangelogBase ()
{
   conn = form.loadConnector();
   conn.initialize (null)
   var ctx = conn.getLdapContext();
   var attrs = ctx.getAttributes("");
   if ( attrs == null )
   {
      form.alert ("Sorry, this LDAP server does not provide server information");
      conn.terminate();
      return;
   }
   var fcn = attrs.get("changelog");
   if ( fcn == null )
   {
      form.alert ("This LDAP server does not report a 'changelog' attribute in it's server info entry");
   } else
   {
      form.alert ("changelog value = '" + fcn.get() + "'");
      form.setConfig (button.getName(), fcn.get());
   }
   form.updateControl (button.getName());
   conn.terminate();
}

function getNamingContext () {
   var configObj = form.getConfigObject();
   var namingContexts = configObj.getParameter("namingContextList");
   var namingContextArray = new java.util.ArrayList();
   
   if (namingContexts != null)
   {
      namingContexts.trim();   
      if (namingContexts.length() == 0)
      {
         namingContexts = null;
      }
   } 

   if (namingContexts != null)
   {
      var tokenizer = new java.util.StringTokenizer(namingContexts, "&");
      while (tokenizer.hasMoreTokens())
      {
         namingContextArray.add(tokenizer.nextToken());
      }
   }
   var newNamingContextRet = form.addDeleteEntry( namingContextArray.toArray() );
   
   if (newNamingContextRet != null)
   {
      var command = newNamingContextRet.get(0);
      var newNamingContext = newNamingContextRet.get(1);
      
      var p = form.getControl("namingContext");
      var combo = p.getComponent(0);
      var button = p.getComponent(1);

      var alAdd = form.getControl("addAssemblyLine");
      var alSearch = form.getControl("searchAssemblyLine");
      var alModify = form.getControl("modifyAssemblyLine");
      var alDelete = form.getControl("deleteAssemblyLine");
      var alCompare = form.getControl("compareAssemblyLine");
      var alModifyDn = form.getControl("modifyDnAssemblyLine");
      
      
      if (command.equalsIgnoreCase("Add"))
      {
         if (newNamingContext != null)
         {
            
            if (!namingContextArray.contains(newNamingContext))
            {
               if (namingContexts == null)
               { 
                  namingContexts = newNamingContext;
               }
               else
               {
                  namingContexts = namingContexts + "&" + newNamingContext;
               }
               configObj.setParameter("namingContextList", namingContexts);
               combo.addItem(newNamingContext);
               combo.setSelectedItem(newNamingContext);
               form.setConfig ("namingContext", newNamingContext);
               form.updateControl ("namingContext");
               configObj.setParameter("selectedNamingContext", newNamingContext);
               alAdd.setSelectedIndex(0);
               alSearch.setSelectedIndex(0);
               alModify.setSelectedIndex(0);
               alDelete.setSelectedIndex(0);
               alCompare.setSelectedIndex(0);
               alModifyDn.setSelectedIndex(0);
            } else
            {
               combo.setSelectedItem(newNamingContext);
               form.setConfig ("namingContext", newNamingContext);
               form.updateControl ("namingContext");
               configObj.setParameter("selectedNamingContext", newNamingContext);
               var addSelected = configObj.getParameter(newNamingContext + "." + "addAssemblyLine");
               var searchSelected = configObj.getParameter(newNamingContext + "." + "searchAssemblyLine");
               var modifySelected = configObj.getParameter(newNamingContext + "." + "modifyAssemblyLine");
               var deleteSelected = configObj.getParameter(newNamingContext + "." + "deleteAssemblyLine");
               var compareSelected = configObj.getParameter(newNamingContext + "." + "compareAssemblyLine");
               var modifyDnSelected = configObj.getParameter(newNamingContext + "." + "modifyDnAssemblyLine");
               
               alAdd.setSelectedItem(addSelected);
               alSearch.setSelectedItem(searchSelected);
               alModify.setSelectedItem(modifySelected);
               alDelete.setSelectedItem(deleteSelected);
               alCompare.setSelectedItem(compareSelected);
               alModifyDn.setSelectedItem(modifyDnSelected);
            }
         }
      } else
      {
         if (namingContextArray.contains(newNamingContext))
         {
            namingContextArray.remove(newNamingContext);
            combo.removeItem(newNamingContext);
            
            if (namingContextArray.size() > 0)
            {
               namingContexts = namingContextArray.get(0);
               for (var i = 1; i < namingContextArray.size(); i++)
               {
                  namingContexts =  namingContexts + "&" + namingContextArray.get(i); 
               }
            }
            else
            {
               namingContexts = null;
            } 
            
            configObj.setParameter("namingContextList", namingContexts);
            
            configObj.removeParameter(newNamingContext + "." + "addAssemblyLine");
            configObj.removeParameter(newNamingContext + "." + "searchAssemblyLine");
            configObj.removeParameter(newNamingContext + "." + "modifyAssemblyLine");
            configObj.removeParameter(newNamingContext + "." + "deleteAssemblyLine");
            configObj.removeParameter(newNamingContext + "." + "compareAssemblyLine");
            configObj.removeParameter(newNamingContext + "." + "modifyDnAssemblyLine");
         }

      }        

   }

}

function deleteTable()
{
   conn = form.loadConnector();
   conn.initialize (null);
   var path = form.getConfigValue ("dbTableName");
   if (path == null || path.length() == 0)
      return;

	if ( form.prompt ("Delete table " + path ) )
      {
	 var str = conn.dropPesTable ( path );
         if ( str != null )
            form.alert ( str );
         else{
            form.alert ("Table " + path + " deleted" );
            form.updateControl(button.getName());
         }
      }
}

function helpMe ()
{
   form.helpMe();
}

function useDefaultColors()
{
   form.useDefaultColors();
}

function showNullBehavior()
{
	var popup = new Packages.com.ibm.di.admin.action.NullBehaviorAction( config );
	popup.showNullValueBehaviorDialog();
}

function showALPoolSettings()
{
	var alc = config.getParent();
	Packages.com.ibm.di.admin.ui.Util.inputForm(alc.getThreadOptions(), "ALPoolSettings");
}

function listConfiguration()
{
	var ip = form.getConfigValue("IP");
	var port = form.getConfigValue("Port");
	var p = (new Packages.java.lang.Integer(port)).intValue();	
	var username = form.getConfigValue("Username");
	var password = form.getConfigValue("Password");

	var list = (new Packages.com.ibm.di.admin.remote.RemoteConfigProxy(ip, p, "",username,password)).getRemoteInstances();

	if(list.size() > 0) {
		var flist = form.getControl("FileList");
		if(flist != null)
		{
			flist.getViewport().getView().setListData(list);
			flist.getViewport().getView().setSelectedIndex(0);
			form.updateControl("FileList");
		}
	}
}

function showParamSubstEditor()
{
	form.showParamSubstEditor(button.getName(), true);
}

function selectFromSchema() {
	if(config.getConnectionConfig().getInheritsFrom() == null) {
		form.alert("Connector not yet configured");
		return;
	}
	
	conn = form.loadConnector();
	if(conn == null)
		return;
		
	conn.initialize (null);
	s = conn.querySchema(null);
	if(s == null || s.size() == 0) {
		try {
			conn.selectEntries();
			s = conn.querySchema(null);
		} catch (ignore) {
		}
	}
	conn.terminate();
	v = new java.util.Vector();
	if (s != null) {
		for(i = 0; i < s.size(); i++) {
			e = s.get(i);
			v.add(e.getString("name"));
		}
	}
	if(v.size() == 0) {
		c = config.getConnectionConfig().getInheritsFrom().getParent();
		s = c.getSchema(true).getItemNames();
		if(s != null) {
			for(i = 0; i < s.size(); i++)
				v.add(s.get(i));
		}
	}
		
	sel = form.chooseFromList ("Select Attribute", v);

	if (sel != null) {
		form.setConfig (button.getName(), sel);
		form.updateControl (button.getName());
	}
}
