// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.3/standard_patch_03/wwwroot/WEB-INF/config/modules/standard/multiselect/js/multiselect.js $ 
// $Date: 8-02-08 8:28 $
// $Revision: 18 $ 
// $Author: Nsm $
// =============================================================== 

   var multiselect_globalCbHttp  = null;

            
   function Multiselect () 
   {
      this.dialogPage               = "multiselect-show-selectbox";
      this.getSavedLocateResultPage = "multiselect-read-datasource";
   
      this.startextent = null;

      this.multiselect = null;      
      this.selectors = new Array();
      this.nselectors = 0;

      this.divBoxTop = '10 px';
      this.divBoxLeft = '10 px';

      this.divBoxWidth = '245px';
      this.divBoxHeight = '250px';

      this.baseQueryUrl = null;
      this.queryUrl = null;
      
      this.actionParams = new Array();
      
      this.actionHandler= null;
      this.auxActionHandler= null;      
      this.autoActionHandler= null;    
      
      this.useQuery = false;
      this.useAutoAction = true;
      
      this.seekAllThemes = "true";
      
      this.searchtext = '';
      
      this.isAutoActionDone = false;
   }
   
   // *************************************************************
   //  Globals
   // *************************************************************

   var multiselect = new Multiselect();
   
   // *************************************************************
   //  Reset
   // *************************************************************
   
   Multiselect.prototype.reset = function () 
   {
      this.startextent = null;

      this.multiselect = null;      
      this.selectors = new Array();
      this.nselectors = 0;

      this.divBoxTop = (cbKort.mapX+10)+'px';
      this.divBoxLeft = (cbKort.mapY+10)+'px';

      this.divBoxWidth = '245px';
      this.divBoxHeight = '270px';

      this.queryUrl = this.baseQueryUrl;

      this.actionParams = new Array();

      this.actionHandler= null;
      this.auxActionHandler= null;
      this.autoActionHandler= null;      

      this.closeOnExit = true;

      this.seekAllThemes = "true";   
      
      this.isAutoActionDone = false;         
   }
   
   // *************************************************************
   //  Initialize a new multiselect
   // *************************************************************
   
   Multiselect.prototype.initDo = function 
   (  multiselect_, 
      title_, 
      width_, 
      height_,
      actionHandler_,
      auxActionHandler_,
      autoActionHandler_,
      useQuery_,
      useAutoAction_,
      closeOnExit_
   )
   {  this.useQuery = useQuery_;
      this.useAutoAction = useAutoAction_;
      this.init (multiselect_, title_, width_, height_, actionHandler_, auxActionHandler_, autoActionHandler_);
      this.closeOnExit = closeOnExit_;
   }

   Multiselect.prototype.initFind = function 
   (  multiselect_, 
      title_, 
      width_, 
      height_ 
   )
   {  this.useQuery = false;
      this.init (multiselect_, title_, width_, height_, this.locate, null, this.locate);
      this.closeOnExit = true;
   }

   Multiselect.prototype.initFindWithQuery = function 
   (  multiselect_, 
      title_, 
      width_, 
      height_ 
   )
   {  this.useQuery = true;
      this.init (multiselect_, title_, width_, height_, this.locate, this.query, this.locate);
      this.closeOnExit = true;
   }


   Multiselect.prototype.initQuery = function 
   (  multiselect_, 
      title_, 
      width_, 
      height_ 
   )
   {  this.useQuery = true;
      this.init (multiselect_, title_, width_, height_, this.query, null, this.locate);
      this.closeOnExit = true;
   }
           
   Multiselect.prototype.init = function 
   (  multiselect_, 
      title_, 
      width_, 
      height_, 
      actionHandler_,
      auxActionHandler_,
      autoActionHandler_
   )
   {
      this.reset();
      
      this.startextent = cbKort.getExtentAsString();

      if (width_!=null)
      {  this.divBoxWidth = width_;
      }
      
      if (height_!=null)
      {  this.divBoxHeight = height_;
      }
      
      this.multiselect       = multiselect_;      
      this.baseQueryUrl      = cbKort.getFormParamAsUrl();
      this.queryUrl          = this.baseQueryUrl;

      this.actionHandler     = actionHandler_;
      this.auxActionHandler  = auxActionHandler_;
      this.autoActionHandler = autoActionHandler_;

      var useAutoAction = "true";
      if (!this.useAutoAction) 
      {  useAutoAction = "false";
      }
      
      var useAuxAction  = (this.auxActionHandler != null);

      var useQuery      = "false";
      if (this.useQuery) 
      {  useQuery = "true";
      }

      var profile = cbKort.getProfile();
      var sessionid = getSessionId();
        
      var src = '/cbkort?page='+this.dialogPage+'&profile='+profile+'&multiselectname='+this.multiselect+'&sessionid='+sessionid+'&autoaction='+useAutoAction+'&auxaction='+useAuxAction+'&query='+useQuery;
      
      var divBoxTop = (cbKort.mapX+10)+'px';
      var divBoxLeft = (cbKort.mapY+10)+'px';
      
      showDivBox(title_, src, true, divBoxLeft, divBoxTop, this.divBoxWidth, this.divBoxHeight);
   }  

   // *************************************************************
   //  Add new selector
   // *************************************************************
   Multiselect.prototype.addSelector = function 
   (   name,
       text, 
       postVar, 
       displayVar, 
       processText, 
       nullDisplayText, 
       exceptionText, 
       queryParams
   )
   {
	   this.nselectors++;
       this.selectors[this.selectors.length] = 
       {  name:name, 
          text:text, 
          postvar:postVar, 
          displayvar:displayVar, 
          processtext:processText, 
          nulldisplaytext:nullDisplayText, 
          exceptiontext:exceptionText, 
          queryparams:queryParams
       };
   }
   
   // *************************************************************
   //  Simple setters
   // *************************************************************

   Multiselect.prototype.setActionParameters = function(params)
   {
      this.actionParams = params;
   }

   Multiselect.prototype.setSeekAllThemes = function(allThemes)
   {
      this.seekAllThemes = allThemes;
   }

   Multiselect.prototype.setSearchText = function(searchtext)
   {
      this.searchtext = searchtext;
   }

   Multiselect.prototype.setAutoActionDone = function(isDone)
   {
      this.isAutoActionDone = isDone;
   }


   // *************************************************************
   //  Simple getters
   // *************************************************************       
       
   Multiselect.prototype.getSelectorCount = function()
   {  return this.nselectors;
   }

   Multiselect.prototype.getSelector = function(index)
   {
       return this.selectors[index];
   }

   Multiselect.prototype.getProcessText = function (index)
   {
       if (index<this.nselectors)
          return this.getSelector(index).processtext;
       else
          return "";   
   }    

   Multiselect.prototype.getNullDisplayText = function (index)
   {
       if (index<this.nselectors)
          return this.getSelector(index).nulldisplaytext;
       else
          return "";   
   }    

   Multiselect.prototype.getExceptionText = function (index)
   {
       if (index<this.nselectors)
          return this.getSelector(index).exceptiontext;
       else
          return "";   
   }    
   
   Multiselect.prototype.isCloseOnExit = function ()
   {
       return this.closeOnExit;
   }    
   
   Multiselect.prototype.getSearchText = function()
   {
      return this.searchtext;
   }
   
   // *************************************************************
   //  Actions
   // *************************************************************

   Multiselect.prototype.doAction = function (postValue)
   {  this.actionHandler(postValue);
   }

   Multiselect.prototype.doAuxAction = function (postValue)
   {  this.auxActionHandler(postValue);
   }

   Multiselect.prototype.doAutoAction = function (postValue)
   {  this.autoActionHandler(postValue);
      this.isAutoActionDone = true;  
   }
   
   Multiselect.prototype.locate = function (postValue)
   {
      var url = this.getActionUrl (postValue);

      url = replaceUrlParam('mapext', 'lastcreatedlayer', url);
      showWaitingBox();
      setHelperframeSrc(url);  
   }

   Multiselect.prototype.query = function (postValue)
   {
      var actionurl = this.getActionUrl (postValue); 
      actionurl = replaceUrlParam('seek_all_themes',this.seekAllThemes, actionurl);
      actionurl = replaceUrlParam('distance','0', actionurl);

      var profilequery = getUrlParamAdv('profilequery',actionurl);
      var page = getUrlParamAdv('page',actionurl);
      
      if ( ! this.isAutoActionDone && page!=null && page!="")
      {
         var cbHttp  = new CBhttp();
         var pComp = cbHttp.executeUrl(actionurl, false);          
      }
      
      if(this.isCloseOnExit())
      {  closeDivBox();
      }
      
      setTimeout('spatialquery_doQuery ("'+profilequery+'", "'+actionurl+'", "'+this.searchtext+'");',100);
   }

   // *************************************************************
   //  Get selection list
   // *************************************************************

   Multiselect.prototype.getSelectionList = function (index, postValue)
   { 
      this.selectionListHandler = null;
      
      var sel = this.getSelector(index);

      var url = this.getSelectionListQueryUrl (index, postValue);
 
      var cbHttp  = new CBhttp();
       
      var pComp = cbHttp.executeUrl(url, false);
      
      var selArray = null;

      if (pComp!=null && pComp.isPComposite() )
      {  var rowList = pComp.get(0);
         selArray = this.createSelectionList (sel, pComp);
      }   
    
      return selArray;              
   }

   Multiselect.prototype.getSelectionListAsync = function (index, postValue, selectionListHandler)
   { 
      var sel = this.getSelector(index);

      var url = this.getSelectionListQueryUrl (index, postValue);
 
      multiselect_globalCbHttp  = new CBhttp();
       
      multiselect_globalCbHttp.executeUrlAsync (url, false, selectionListHandler);
   }

   Multiselect.prototype.createSelectionList = function (selector, pComp)
   { 
      var rowList = pComp.get(0);

      var selArray = null;

      if (rowList != null && rowList.isRowList() )
      {  
         selArray = new Array();
         for (var rowIx=0; rowIx < rowList.size(); rowIx++)
         {
            var row = rowList.row(rowIx);
            if (row!=null)
            {  var rowArray = new Array();
               var column1 = row.column(selector.postvar);
               var column2 = row.column(selector.displayvar);
            
               if (column1==null)
               {  throw new Error ("Postvariable '" + selector.postvar + "' missing in input");
               }

               if (column2==null)
               {  throw new Error ("Displayvariable '" + selector.displayvar + "' missing in input");
               }

               var displayValue = column1.getValue();
               if (displayValue==null)
               {  displayValue = "";
               }
               
               var id = column2.getValue();
               if (id==null)
               {  id = "";
               }
           
               rowArray = {id:id,disp:displayValue}
               selArray[rowIx] = rowArray;
            }   
         }
      }
      
      // *** Skal først testes
      // selArray.sort (Multiselect_sortAscendingIgnoreCase);
      return selArray;              
   }

       
   // *************************************************************
   //  Private methods
   // *************************************************************

   Multiselect.prototype.getSelectionListQueryUrl = function (index, postValue)
   {  
      // Add queryparams to url.

      var strUrl = this.queryUrl;
      var pArray = this.selectors[index].queryparams;

      if (pArray!=null)
      {
         var pName=null;
         var pValue=null;
         for(var i=0;i<pArray.length;i++)
         {
            pName=pArray[i].name;
            pValue=pArray[i].value;     
            strUrl = replaceUrlParam(pName,pValue, strUrl);
         }
      }   

      // Replace / add postvar and postvalue

      var sel = this.getSelector(index);

      if (index>0 && postValue!=null)
      {
         var postVar = this.getSelector(index-1).postvar;
         var lastBox = 'kode'+(index-1);
         strUrl = replaceUrlParam (postVar, postValue, strUrl);
      }
    
      strUrl = replaceUrlParam("jdaf.error.xslt", "", strUrl);
      strUrl = replaceUrlParam("jdaf.error.contenttype", "text/xml", strUrl);
    
      this.queryUrl = strUrl;
    
      return this.queryUrl;
   }   
   
   Multiselect.prototype.getActionUrl = function (postValue)
   {
      var url = this.queryUrl;
      url = replaceUrlParam('layers', cbKort.getLayers(), url);

      if (this.actionParams!=null)
      {
         var pName=null;
         var pValue=null;
         for (var i=0; i<this.actionParams.length; i++)
         {
            pName=this.actionParams[i].name;
            pValue=this.actionParams[i].value;     
            url = replaceUrlParam (pName, pValue, url);
         }
      }   
    
      var postVar = this.selectors[this.selectors.length-1].postvar;
      url = replaceUrlParam(postVar, postValue, url);

      url = replaceUrlParam("jdaf.error.xslt", "", url);
      url = replaceUrlParam("jdaf.error.contenttype", "text/xml", url);
      
      return url;
   }   
   
     
  //
  // Helper function
  //
  
  function Multiselect_sortAscendingIgnoreCase (a, b)
  {
     var v1 = a.disp.toLowerCase(); 
     var v2 = b.disp.toLowerCase();
     if (v1>v2) return 1;
     if (v2<v1) return -1;
     return 0; 
  }
  
  Multiselect.prototype.resetBox = function (top_,left_,width_,height_) 
   {
      this.startextent = null;

      this.multiselect = null;      
      this.selectors = new Array();
      this.nselectors = 0;

      this.divBoxTop = top_;
      this.divBoxLeft = left_;
      this.divBoxWidth = width_;
      this.divBoxHeight = height_;

      this.queryUrl = this.baseQueryUrl;

      this.actionParams = new Array();

      this.actionHandler= null;
      this.auxActionHandler= null;
      this.autoActionHandler= null;      

      this.closeOnExit = true;

      this.seekAllThemes = "true";      
   }
   
   Multiselect.prototype.initQueryBox = function 
   (  multiselect_, 
      title_,
      top_,
      left_,
      width_, 
      height_ 
   )
   {  this.useQuery = true;
      this.initBox (multiselect_, title_, top_, left_, width_, height_, this.query, null, this.locate);
      this.closeOnExit = true;
   }
   
   Multiselect.prototype.initBox = function 
   (  multiselect_, 
      title_,
      top_,
      left_,
      width_,
      height_, 
      actionHandler_,
      auxActionHandler_,
      autoActionHandler_
   )
   {
      this.resetBox();
      
      this.startextent = cbKort.getExtentAsString();

      if (top_!=null)
      {  this.divBoxTop = top_;
      }
      
      if (left_!=null)
      {  this.divBoxLeft = left_;
      }      

      if (width_!=null)
      {  this.divBoxWidth = width_;
      }
      
      if (height_!=null)
      {  this.divBoxHeight = height_;
      }
      
      this.multiselect       = multiselect_;      
      this.baseQueryUrl      = cbKort.getFormParamAsUrl();
      this.queryUrl          = this.baseQueryUrl;

      this.actionHandler     = actionHandler_;
      this.auxActionHandler  = auxActionHandler_;
      this.autoActionHandler = autoActionHandler_;

      var useAutoAction = "true";
      if (!this.useAutoAction) 
      {  useAutoAction = "false";
      }
      
      var useAuxAction  = (this.auxActionHandler != null);

      var useQuery      = "false";
      if (this.useQuery) 
      {  useQuery = "true";
      }

      var profile = cbKort.getProfile();
      var sessionid = getSessionId();
        
      var src = '/cbkort?page='+this.dialogPage+'&profile='+profile+'&multiselectname='+this.multiselect+'&sessionid='+sessionid+'&autoaction='+useAutoAction+'&auxaction='+useAuxAction+'&query='+useQuery;
      
      showDivBox(title_, src, true, this.divBoxLeft, this.divBoxTop, this.divBoxWidth, this.divBoxHeight);
   }
   
   Multiselect.prototype.initFindBox = function 
   (  multiselect_, 
      title_,
      top_,
      left_,      
      width_, 
      height_ 
   )
   {  this.useQuery = false;
      this.initBox (multiselect_, title_, top_, left_, width_, height_, this.locate, null, this.locate);
      this.closeOnExit = true;
   }   
  
