﻿AV.Committee.Committee = new function(){  
 
  var win;
  var current_committee_id = '';
  var current_committee_type = '';
  
  function confirmDelete(btn)
  {          
    if(btn == 'yes')
    {        
      AV.Committee.CommitteeForm.getCommitteeForm().getForm().submit({                         
        waitTitle:'Please Wait', 
        waitMsg:'Sending delete request...',
        url: 'Committee2.asp?action=delete&committee_id=' + current_committee_id, 
        clientValidation: false,
          	                        
        success:function(form, action){ 
        
          //Used for debugging
          //var obj = Ext.util.JSON.decode(action.response.responseText);                            

          //win.close();	      
          win.hide();
          window.location.href=window.location.href;
        },

        failure:function(form, action){                         
                                      
          if(action.failureType == 'server'){ 
              var obj = Ext.util.JSON.decode(action.response.responseText);                               
              Ext.Msg.alert('Delete Failed!', unescape(obj.errors.reason)); 
          }
          else{ Ext.Msg.alert('Warning!', 'server may be unreachable : ' + action.response.responseText); } 
        } 
      });  
          
    }
  }
   
  function getWin(title, instructions, committee_id, committee_type) 
  {               
    //set current committee (for edit and delete)
    current_committee_id = committee_id;    
    current_committee_type = committee_type;
    
    if(!win)
    {
      win = new Ext.Window({
          closable:true,
          resizable: false,
          width:355,        
          plain:true,
          title: title,
          autoDestroy: false,
          closeAction: 'hide',
          modal:true,       
          buttons: [{
                    id: "btn_save_committee",
                    text: "Save",
                    handler: function(){
                    
                                        
                      if(Ext.getCmp('txtCommitteeName').isValid() )
                      {                                                    
                        if(current_committee_type == 'board' && Ext.getCmp('txtCommitteeName').getRawValue().toLowerCase().indexOf('board') != 0)
                        {
                          Ext.MessageBox.alert('Invalid Name','The Committee Name for the Board of Directors must begin with the word "Board".');
                          return;
                        }
                        
                        if(current_committee_type != 'board' && Ext.getCmp('txtCommitteeName').getRawValue().toLowerCase().indexOf('board') == 0)
                        {
                          Ext.MessageBox.alert('Invalid Name','The Committee Name can not begin with the word "Board".');
                          return;
                        }
                                     
                        AV.Committee.CommitteeForm.getCommitteeForm().getForm().submit({                         
                          waitTitle:'Please Wait', 
                          waitMsg:'Sending data...',
                          url: function() {                                                         
                            return 'Committee2.asp?action=add&committee_id=' + current_committee_id;
                          },
                            	                        
                          success:function(form, action){ 
                          
                            //Used for debugging
                            //var obj = Ext.util.JSON.decode(action.response.responseText);    
                            
                            //alert(action.response.responseText);

	                          win.hide();	      
	                          window.location.href=window.location.href;
                          },

                          failure:function(form, action){                         
                                                        
                            if(action.failureType == 'server'){ 
                                var obj = Ext.util.JSON.decode(action.response.responseText);   
                                //EXAMPLE { success: false, errors: { reason: 'The save action was not completed.  There was an error.' }}                             
                                Ext.Msg.alert('Save Failed!', unescape(obj.errors.reason)); 
                            }
                            else{ Ext.Msg.alert('Warning!', 'server may be unreachable : ' + action.response.responseText); } 
                          } 
                        });                                                 
                      }    
                                                                          
                    }                   
                  },
                  {
                    id: "btn_cancel_committee",
                    text: "Cancel",
                    handler: function(){
                      
                      if (document.getElementById('headerHide')) document.getElementById('headerHide').style.visibility = "visible";                    
                      win.hide();                  
                    }
                  },
                  {
                    id: "btn_delete_committee",
                    text: "Delete",
                    //hidden: true,
                    handler: function(){
                      
                      Ext.MessageBox.confirm('Confirm Delete?', 'Click "Yes" to delete this committee', confirmDelete);                                                                      
                      if (document.getElementById('headerHide')) document.getElementById('headerHide').style.visibility = "visible";            
                                       
                    }
                  }
                 ],
          items: [AV.Committee.CommitteeForm.getCommitteeFormInstruction('', instructions), AV.Committee.CommitteeForm.getCommitteeForm()]  
      });        
      
      //This allows the ext dialog to be displayed "above" a flash header
      if (document.getElementById('headerHide')) document.getElementById('headerHide').style.visibility = "hidden";
      //this is here because function "HM_f_PageClick" in HMScriptDOM.js gets called on every page on mouse down by default and pulls flash headers in front of the dialog
      var bodyEl = Ext.get(window.document);
      bodyEl.addListener('mousedown', function (){ if (document.getElementById('headerHide')) { document.getElementById('headerHide').style.visibility = "hidden"; } });                
    }           
    
    return win;
  } 

  return {
  
    addCommittee: function(anim_link) {        

      var instructions = 'Enter the name of the new committee.';  
      
      var winAddCommittee = getWin('Add Committee', instructions, '0');
      winAddCommittee.show(anim_link);   
      
      //Clear form each time it is displayed          
      AV.Committee.CommitteeForm.clearFormValues();    
      //hide delete button
      Ext.getCmp('btn_delete_committee').hide();
    },
    
    editCommittee: function(anim_link, committee_id, committee_name, committee_type) {              
      
      var instructions = 'Edit the committee name or delete the committee.';  
      
      var winEditCommittee = getWin('Edit Committee', instructions, committee_id, committee_type);
      winEditCommittee.show(anim_link); 
      
      //show delete button for committee (not board)
      if(committee_type != 'board') {Ext.getCmp('btn_delete_committee').show(); }
      else { Ext.getCmp('btn_delete_committee').hide(); }
      
      //set committee name
      if(committee_name != '') {Ext.getCmp('txtCommitteeName').setRawValue(committee_name);}   
    }   
    
  }//End Return
   
} //End object AV.Committee.Committee



//=============================================================================

AV.Committee.Officer = new function(){  
  
  var animLink;
  var win;
  
  var objEditOfficer = new Object({
  
    contact_id: '0',
    committee_id: '0',
    officer_id: '0'
  
  });
  
  function getWin(title, instructions, dataObj, hideDeleteButton) 
  {       
    if(win) { win.destroy(); }  //hack to allow the cancel button to hide the panel instead of close (close() was causing an error with validation..        
    
    //var data = eval("(" + dataObj + ")");     //The extra parenthesis are required, otherwise you get an Invalid Label error....
    
    var data = Ext.decode(dataObj);
    
    function confirmDelete(btn)
    {          
      if(btn == 'yes')
      {        
        AV.Committee.OfficerForm.getForm().getForm().submit({                         
          waitTitle:'Please Wait', 
          waitMsg:'Sending delete request...',
          url: get_submit_url('delete'),
          clientValidation: false,
            	                        
          success:function(form, action){ 
          
            //Used for debugging
            //var obj = Ext.util.JSON.decode(action.response.responseText);                            

            win.close();	      
            window.location.href=window.location.href;
          },

          failure:function(form, action){                         
                                        
            if(action.failureType == 'server'){ 
                var obj = Ext.util.JSON.decode(action.response.responseText);                               
                Ext.Msg.alert('Delete Failed!', unescape(obj.errors.reason)); 
            }
            else{ Ext.Msg.alert('Warning!', 'server may be unreachable : ' + action.response.responseText); } 
          } 
        });  
            
      }
    }
    
    //Because contacts can belong to more than one committee, I have to reload the window each time
    //if(!win)
    //{
      win = new Ext.Window({
          closable:true,
          resizable: false,
          width: 375,
          height: 285,   
          autoScroll: true,   //Ticket 70901 added autoScroll and height, modified width to accommodate scrollbars
          plain:true,
          title: title,
          autoDestroy: false,
          modal: true,                 
          buttons: [{
                    id: "btn_save",
                    text: "Save",
                    handler: function(){
                    
                    
                      if(Ext.getCmp('comboSelectName').isValid() && Ext.getCmp('txtPosition').isValid() && Ext.getCmp('comboSecurityRole').isValid() && Ext.getCmp('comboDisplayBefore').isValid() && Ext.getCmp('comboSelectCommittee').isValid() )
                      {                                          
                        Ext.getCmp('comboSelectName').setDisabled(false);  //make sure that this enabled before submitting
                        Ext.getCmp('comboSelectCommittee').setDisabled(false);  //make sure that this enabled before submitting
                        
                        AV.Committee.OfficerForm.getForm().getForm().submit({                         
                          waitTitle:'Please Wait', 
                          waitMsg:'Sending data...',
                          url: get_submit_url('save'),
                            	                        
                          success:function(form, action){ 
                          
                            //Used for debugging
                            //var obj = Ext.util.JSON.decode(action.response.responseText);                            

	                          win.close();	      
	                          window.location.href=window.location.href;
                          },

                          failure:function(form, action){                         
                                                        
                            if(action.failureType == 'server'){ 
                                var obj = Ext.util.JSON.decode(action.response.responseText);   
                                //EXAMPLE { success: false, errors: { reason: 'The save action was not completed.  There was an error.' }}                             
                                Ext.Msg.alert('Save Failed!', unescape(obj.errors.reason)); 
                            }
                            else{ Ext.Msg.alert('Warning!', 'server may be unreachable : ' + action.response.responseText); } 
                          } 
                        });                                                 
                      }                                                       
                    }                    
                  },
                  {
                    id: "btn_cancel",
                    text: "Cancel",
                    handler: function(){                      
                      
                      if (document.getElementById('headerHide')) document.getElementById('headerHide').style.visibility = "visible";                    
                      
                      //the window will be destroyed (win.close();) before it is opened again.  closing it here was causing an error with ext validation
                      //win.close();
                      win.hide();                                  
                    }
                  },
                  {
                    id: "btn_delete",
                    text: "Delete",
                    hidden: true,
                    handler: function(){
                      
                        Ext.MessageBox.confirm('Confirm Delete?', 'Click "Yes" to delete this member', confirmDelete);                                                
                      
                      if (document.getElementById('headerHide')) document.getElementById('headerHide').style.visibility = "visible";            
                    }
                  }
                 ],
          items: [AV.Committee.OfficerForm.getOfficerFormInstruction('', instructions), AV.Committee.OfficerForm.getNewOfficerForm(data)]  
      });        
      
      //This allows the ext dialog to be displayed "above" a flash header
      if (document.getElementById('headerHide')) document.getElementById('headerHide').style.visibility = "hidden";
      //this is here because function "HM_f_PageClick" in HMScriptDOM.js gets called on every page on mouse down by default and pulls flash headers in front of the dialog
      var bodyEl = Ext.get(window.document);
      bodyEl.addListener('mousedown', function (){ if (document.getElementById('headerHide')) { document.getElementById('headerHide').style.visibility = "hidden"; } });      
    
    //}
    
    //Toggle delete button on window (hide for ADD and show for EDIT)
    if(hideDeleteButton) { Ext.getCmp('btn_delete').hide(); }
    else { Ext.getCmp('btn_delete').show(); }      
    
    return win;
  }
  
  function getAddDataSuccess(response, options)
  {
    var instructions = 'Select a resident to be a Member.  You can also change the resident\'s security role if necessary.';  
    
    var winManageBoard = getWin('Add Member', instructions, response.responseText, true);
    winManageBoard.show(animLink);   
    
    //Clear form each time it is displayed          
    //AV.Committee.OfficerForm.clearFormValues();
    
    //pre-set data --> in the case of adding a new officer, this only populates the committee combo if their is only one committee (i.e. board of directors)
    AV.Committee.OfficerForm.initFormValues(objEditOfficer.contact_id, 'add');
  }    
  
  function getEditDataSuccess(response, options)
  {         
    var winManageBoard = getWin('Edit Member', 'Edit a Member\'s information.', response.responseText, false);
    winManageBoard.show(animLink);    
    
    //Clear form each time it is displayed          
    //AV.Committee.OfficerForm.clearFormValues();
    
    //pre-set Data
    AV.Committee.OfficerForm.initFormValues(objEditOfficer.contact_id, 'edit');
  }
  
  function getAddDataFail(response, options) { Ext.Msg.alert('Error', response.responseText); }
  function getEditDataFail(response, options) { Ext.Msg.alert('Error', response.responseText); }
  
  function get_submit_url(action)
  {       
    var submit_url = 'Committee_Member_Info.asp?action=' + action;     
    
    if(objEditOfficer.committee_id != '' && objEditOfficer.committee_id != '0')
      { submit_url += '&committee_id=' + objEditOfficer.committee_id; }
   
    if(objEditOfficer.officer_id != '' && objEditOfficer.officer_id != '0')
      { submit_url += '&officer_id=' + objEditOfficer.officer_id; }    
      
    return submit_url;
  }
  
  return {
  
    addCommitteeMember: function(assn_id, anim_link, committee_type) {        
      
      //Set the element from which the window "launches"
      animLink = anim_link;     
      
      //clear the contact_id, committee_id, and officer_id
      objEditOfficer.contact_id = '0';
      objEditOfficer.committee_id = '0';
      objEditOfficer.officer_id = '0';
      
      //Get data for new BM 
      Ext.Ajax.request({
         url: 'Committee_Member_Info.asp?action=add&committee_type=' + committee_type,
         success: getAddDataSuccess,
         failure: getAddDataFail
      });           
    },
    
    editCommitteeMember: function(contact_id, assn_id, anim_link, committee_id, officer_id, committee_type) {  
    
      //Set the element from which the window "launches"
      animLink = anim_link;                 
              
      //store the contact_id, committee_id, and officer_id for the officer being editted  
      objEditOfficer.contact_id = contact_id;
      objEditOfficer.committee_id = committee_id;
      objEditOfficer.officer_id = officer_id;
              
      //Get data for existing BM 
      Ext.Ajax.request({
         url: 'Committee_Member_Info.asp?action=edit&committee_type=' + committee_type + '&committee_id=' + committee_id,
         success: getEditDataSuccess,
         failure: getEditDataFail
      }); 
  
    }
    
  }//End Return
   
} //End object AV.Committee.Officer
