//------------ wwc_practiceMenuForm.js
//
//  This form is used to support the practice menu form on practice.jsp
//

PracticeMenuForm = Class.create();

PracticeMenuForm.prototype = {

   initialize: function( formId, contentId, url ) {
      this.id          = formId;
      this.myForm      = $(this.id);
      this.contentId   = contentId;
      this.industryCompany   = new Object();
      this.statutory         = new Object();
      this.practiceType      = new Object();
      this.provinceCity      = new Object();
      this.industryCompanyOn = new Array();
      this.statutoryOn       = new Array();
      this.practiceTypeOn    = new Array();
      this.provinceCityOn    = new Array();
          
      this.initAjax(url);
      this.initializeArrays();
//      this.injectFormBehavior();
      
      this.callRicoAjaxEngine();
   },

   initAjax: function(url) {
      ajaxEngine.registerRequest( this.id + '_request', url );
      ajaxEngine.registerAjaxObject( this.id + '_updater', this );
   },
   
   injectFormBehavior: function() {

   },
   

   initializeArrays: function() {
       var myName, myType, myChecked, myElement, myId, myFormElement;
       
       for ( var i=0; i < this.myForm.elements.length; i++ ) {
       
          myElement = this.myForm.elements[i];
          myName = myElement.name;
          myType = myElement.type;
          myId = myElement.id;     
          myChecked = myElement.checked;
          
         if (( myType == 'checkbox' ) || ( myType = 'radio' )) {
             if ( myName == 'industryCompany') {
                 this.industryCompany[myId] = myChecked;
             }
             else if ( myName == 'statutory') {
                 this.statutory[myId] = myChecked;
             }
             else if ( myName == 'practiceType') {
                 this.practiceType[myId] = myChecked;
             }
             else if ( myName == 'provinceCity') {
                 this.provinceCity[myId] = myChecked;
             }
          }
       }
//       alert("here");
   },
   
   myClicked: function( obj ) {
      var myName, myValue, myType, myChecked, myId;
      myName = obj.name;
      myValue = obj.value;
      myType = obj.type;
      myChecked = obj.checked;
      myId = obj.id;
      
      if (myName == 'industryCompany') {
          this.industryCompany[myId] = myChecked;
      }
      else if (myName == 'practiceType') {
          this.practiceType[myId] = myChecked;
      }
      else if (myName == 'provinceCity') {
          this.provinceCity[myId] = myChecked;
      }
      else if (myName == 'statutory') {
          for ( var i in this.statutory ) {
              this.statutory[i] = false;
          }
          this.statutory[myId] = myChecked;
      }
      
      this.callRicoAjaxEngine();
   },
   
   convertToQueryString: function( myArray ) {
       var myStr = '';
       
       for ( var i=0; i < myArray.length; i++ ) {
       
           if ( myArray[i] == 'extend' )
               continue;
               
           if ( i == 0 )
               myStr += myArray[0];
           else
               myStr += "," + myArray[i];
       }
       
       return myStr;
   },
     
   callRicoAjaxEngine: function() {
      var callParams = [];
      var myName, myValue, myType, myChecked, myQueryStr, myElement, myId, myFormElement;
      
      callParams.push( this.id + '_request');
      
      this.industryCompanyOn.clear();
      this.statutoryOn.clear();
      this.practiceTypeOn.clear();
      this.provinceCityOn.clear();
      
      // Create arrays of the elements that are checked
      
      for ( var id in this.industryCompany ) {
          if ( this.industryCompany[id] )
               this.industryCompanyOn.push( id );
      }
      
      for ( var id in this.statutory ) {
          if ( this.statutory[id] )
              this.statutoryOn.push( id );
      }
      
      for ( var id in this.practiceType ) {
          if ( this.practiceType[id] )
              this.practiceTypeOn.push( id );
      }
      
      for ( var id in this.provinceCity ) {
          if ( this.provinceCity[id] )
              this.provinceCityOn.push( id );
      }   
      // Now add them as call parameters
      
      if ( this.industryCompanyOn.length > 0 ) {
          myQueryStr = this.convertToQueryString( this.industryCompanyOn );
          callParams.push( 'industryCompany=' + myQueryStr );          
      }
      if ( this.statutoryOn.length > 0 ) {
          myQueryStr = this.convertToQueryString( this.statutoryOn );
          callParams.push( 'statutory=' + myQueryStr );          
      }          
      if ( this.practiceTypeOn.length > 0 ) {
          myQueryStr = this.convertToQueryString( this.practiceTypeOn );
          callParams.push( 'practiceType=' + myQueryStr );          
      }
      if ( this.provinceCityOn.length > 0 ) {
          myQueryStr = this.convertToQueryString( this.provinceCityOn );
          callParams.push( 'provinceCity=' + myQueryStr );          
      }           
      
//      alert("callParams = " + callParams);
      ajaxEngine.sendRequest.apply( ajaxEngine, callParams );
   },
   
   ajaxUpdate: function( ajaxResponse ) {
      var myText;
      var contentRegion;
      
      myText = RicoUtil.getContentAsString( ajaxResponse );
      contentRegion = $(this.contentId);
      contentRegion.innerHTML = myText;

   },
   
   submitHandler: function() {

       this.callRicoAjaxEngine();
       return false;
   }
   
};
 