﻿Type.registerNamespace("GIRO.Hastinfo.Web.Fix");

GIRO.Hastinfo.Web.Fix.ComboBoxManager = function(element) 
   {
   GIRO.Hastinfo.Web.Fix.ComboBoxManager.initializeBase(this, [element]);

   this._clientStateField = null;
   
   this._managedComboBoxId = null;
   
   this._childIds = null;
   this._parentIds = null;
   
   this._onSelectedIndexChanged$delegate = null;   
   this._onClear$delegate = null;   
   this._onClearAndReQuery$delegate = null;   
   this._onItemsRequesting$delegate = null; 
   this._onItemsRequested$delegate = null; 
   this._onReQuery$delegate = null; 
   this._onReQueryOnLoad$delegate = null; 
   }    

GIRO.Hastinfo.Web.Fix.ComboBoxManager.prototype = 
   {
   initialize : function() 
      {
      GIRO.Hastinfo.Web.Fix.ComboBoxManager.callBaseMethod(this, "initialize");
      
      this.loadClientState();
      this.registerEventHandlers();
      },    

   dispose : function() 
      {
      GIRO.Hastinfo.Web.Fix.ComboBoxManager.callBaseMethod(this, "dispose");
      },
      
   doReQuery : function(isReQueryOnLoad)
      {
      var isAParentRequesting = false;
      for (var i = 0; i < this._parentIds.length; i++)
         if (eval(this._parentIds[i]) != null)
            if (isReQueryOnLoad ? eval(this._parentIds[i])._isWaitingForReQueryOnLoad : eval(this._parentIds[i])._isWaitingRequestItems)
               {
               isAParentRequesting = true;
               break;
               }
               
      var managedComboBox = eval(this._managedComboBoxId);
      if (managedComboBox != null)
         {
         if (!isAParentRequesting)
            {
            managedComboBox._isWaitingRequestItems = true;
            managedComboBox.requestItems(managedComboBox._reQueryText, false /*appendItems*/); 
            } 
         else if (!isReQueryOnLoad)
            {
            managedComboBox._isWaitingForAParentToRequest = true;
            }        
         }                
      },   
      
   loadClientState : function() 
      {
                             // TODO: Clear when SS SaveClientState will be empty
      
      if (this._clientStateField == null)
         return;
         
      var value = this._clientStateField.value;
      var payload = Sys.Serialization.JavaScriptSerializer.deserialize(value);
      
      this._childIds = new Array();
      this._parentIds = new Array();      
      
      if (payload != null)
         {
         for (var i = 0; i < payload.ChildIdsCount; i++) 
            this._childIds[i] = eval("payload.ChildIds" + i);
            
         for (var i = 0; i < payload.ParentIdsCount; i++) 
            this._parentIds[i] = eval("payload.ParentIds" + i);
            
         this._managedComboBoxId = payload.ManagedComboBoxID;            
         }
      },
            
   onSelectedIndexChanged : function(sender, args)
      {
      for (i = 0; i < this._childIds.length; i++)
         eval(this._childIds[i]).clearAndReQuery(true /*queryEmptyString*/);
      },
      
   onClear : function(sender, args)
      {
      for (i = 0; i < this._childIds.length; i++)
         {
         var child = eval(this._childIds[i]);
         child.clear(child._queryAll ? false : true /*clearItems*/);
         }
      },  
      
   onClearAndReQuery : function(sender, args)
      {
      for (i = 0; i < this._childIds.length; i++)
         eval(this._childIds[i]).clearAndReQuery(false /*queryEmptyString*/);
      },  
      
   onItemsRequesting : function(sender, args)
      {
      var state = new String();
      
      for (var i = 0; i < this._parentIds.length; i++)
         {
         var selectedIndex = (eval(this._parentIds[i]) != null) ? eval(this._parentIds[i])._selectedIndex : '';
         var value = (eval(this._parentIds[i]) != null) ? eval(this._parentIds[i]).GetValue() : ''; 
         state += selectedIndex + "~~" + value;
         
         if ((i + 1) < this._parentIds.length)  
            state += "~**~";
         }
         
      args.SetClientData(state);
      }, 
      
   onItemsRequested : function(sender, args)
      {
      for (i = 0; i < this._childIds.length; i++)
         {
         var childComboBox = eval(this._childIds[i]);
         if (childComboBox != null)
            if (childComboBox._isWaitingRequestItems == false)
               if (childComboBox._isWaitingForAParentToRequest == true)
                  childComboBox.ReQuery();
               else if (childComboBox._isWaitingForReQueryOnLoad == true)
                  childComboBox.reQueryOnLoad();
         } 
      },       
      
   onReQuery : function(sender, args)
      {
      this.doReQuery(false /*isReQueryOnLoad*/);
      },
      
   onReQueryOnLoad : function(sender, args)
      {
      this.doReQuery(true /*isReQueryOnLoad*/);      
      },                
     
   registerEventHandlers : function() 
      {
      this._onSelectedIndexChanged$delegate = Function.createDelegate(this, this.onSelectedIndexChanged);
      this._onClear$delegate = Function.createDelegate(this, this.onClear);
      this._onClearAndReQuery$delegate = Function.createDelegate(this, this.onClearAndReQuery);
      this._onItemsRequesting$delegate = Function.createDelegate(this, this.onItemsRequesting);
      this._onItemsRequested$delegate = Function.createDelegate(this, this.onItemsRequested);
      this._onReQuery$delegate = Function.createDelegate(this, this.onReQuery);
      this._onReQueryOnLoad$delegate = Function.createDelegate(this, this.onReQueryOnLoad);
      
      var managedComboBox = eval(this._managedComboBoxId);
      managedComboBox.get_events().addHandler(GIRO.Hastinfo.Web.Fix.ComboBoxEvents.SelectedIndexChanged.toString(), this._onSelectedIndexChanged$delegate);
      managedComboBox.get_events().addHandler(GIRO.Hastinfo.Web.Fix.ComboBoxEvents.Clear.toString(), this._onClear$delegate);
      managedComboBox.get_events().addHandler(GIRO.Hastinfo.Web.Fix.ComboBoxEvents.ClearAndReQuery.toString(), this._onClearAndReQuery$delegate);
      managedComboBox.get_events().addHandler(GIRO.Hastinfo.Web.Fix.ComboBoxEvents.ItemsRequesting.toString(), this._onItemsRequesting$delegate);
      managedComboBox.get_events().addHandler(GIRO.Hastinfo.Web.Fix.ComboBoxEvents.ItemsRequested.toString(), this._onItemsRequested$delegate);
      managedComboBox.get_events().addHandler(GIRO.Hastinfo.Web.Fix.ComboBoxEvents.ReQuery.toString(), this._onReQuery$delegate);
      managedComboBox.get_events().addHandler(GIRO.Hastinfo.Web.Fix.ComboBoxEvents.ReQueryOnLoad.toString(), this._onReQueryOnLoad$delegate);
      },       
      
   saveClientState : function() 
      {
      if (this._clientStateField == null)
          return;
          
      this._clientStateField.value = "";
      },
      
   get_clientStateField : function() { return this._clientStateField; },
   set_clientStateField : function(value) { this._clientStateField = value; }
   
   }
GIRO.Hastinfo.Web.Fix.ComboBoxManager.registerClass("GIRO.Hastinfo.Web.Fix.ComboBoxManager", Sys.UI.Control);

if (typeof(Sys) !== 'undefined') 
   Sys.Application.notifyScriptLoaded(); 
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();