// Copyright (C) Acquiro Systems Inc. 2008 - All Rights Reserved

function MessagingMessage(instance,subjectFieldId)
{this.CoreListItem(instance);this.m_subjectFieldId=subjectFieldId;}
copyPrototype(MessagingMessage,CoreListItem);MessagingMessage.prototype.getSubject=function()
{return this.m_instance.getText_Value(this.m_subjectFieldId);}
function SendMessageDialog(message)
{this.ModalDialog();this.m_showCancel=true;this.m_showOk=false;this.m_message=message;}
copyPrototype(SendMessageDialog,ModalDialog);SendMessageDialog.prototype.showDialog=function(callback)
{var dialogLayout=new TableLayout();dialogLayout.setBackgroundColor("#F8F8F8");var lbl=new Label(getResource1(MESSAGING_SEND_DIALOG_INSTRUCTIONS,this.m_message.getSubject()));lbl.setPadding(3,0,3,0);lbl.setBorder(0,0,0,1,"solid","#BDBDBD");dialogLayout.addRow("40");dialogLayout.addCell(0,lbl,TABLE_LAYOUT_REMAINDER);dialogLayout.addRow(TABLE_LAYOUT_REMAINDER);dialogLayout.addCell(1,new Label("Loading list of users..."),TABLE_LAYOUT_REMAINDER);this.m_sendBtn=new Button(getResource(MESSAGING_SEND_DIALOG_BTN),getResource1(MESSAGING_SEND_DIALOG_BTN_DESC,this.m_message.getSubject()),null);this.m_sendBtn.addListener(this);this.m_sendBtn.disable();this.m_sendBtn.setMargin(0,5,0,0);this.m_toolbar.addCell(0,this.m_sendBtn,"60");var args=new Array();args[0]=getResource(MESSAGING_SEND_DIALOG_TITLE);args[1]=dialogLayout;args[2]=500;args[3]=400;args[4]=callback;ModalDialog.prototype.showDialog.apply(this,args);var request=new AjaxRequest(APP_URL+"/"+MESSAGING_ACTION);request.addParam("action","listUsers");request.postRequest(this.onListUsers_Response.bind(this));}
SendMessageDialog.prototype.onListUsers_Response=function(response)
{eval(response.getJavaScript());var curUserInstance;var ckOptions=new Array();for(var i=0;i<users.length;i++)
{curUserInstance=users[i].getInstance();ckOptions.push(new CheckboxListOption(curUserInstance.getId(),curUserInstance.getInstanceDisplayValue(" ")));}
this.m_ckList=new CheckboxList(ckOptions,new Array());this.m_ckList.addListener(this);this.m_content.replaceCell(1,0,this.m_ckList);}
SendMessageDialog.prototype.onButtonClick=function(objBtn)
{var args=new Array();args.push(objBtn);ModalDialog.prototype.onButtonClick.apply(this,args);if(objBtn==this.m_sendBtn)
{var userInstancesId=this.m_ckList.getSelected()
userInstancesId=stringArrayToString(userInstancesId,",");var request=new AjaxRequest(APP_URL+"/"+MESSAGING_ACTION);request.addParam("action","sendMessage");request.addParam("messageInstancesId",this.m_message.getInstanceId());request.addParam("userInstancesId",userInstancesId);request.postRequest(this.onSendMessage_Response.bind(this));}}
SendMessageDialog.prototype.onSendMessage_Response=function(response)
{this.hideDialog();msgDialog(MESSAGING_SEND_DIALOG_SUCCESS,MESSAGING_SEND_DIALOG_SUCCESS,null);}
SendMessageDialog.prototype.onCheckboxListSelectionChanged=function(response)
{if(this.m_ckList.getSelected().length>0)
{this.m_sendBtn.enable();}
else
{this.m_sendBtn.disable();}}
var MESSAGING_ACTION=null;function MessagingMessagesEditor(windowManager)
{var settings=new CoreListEditorSettings();settings.m_newIcon=APP_URL+"/images/acquiro/editors/list_icons/item_new.png";settings.m_editIcon=APP_URL+"/images/acquiro/editors/list_icons/item_edit.png";settings.m_deleteIcon=APP_URL+"/images/acquiro/editors/list_icons/item_delete.png";settings.m_searchIcon=null;settings.m_refreshIcon=APP_URL+"/images/acquiro/editors/list_icons/item_refresh.png";settings.m_action=MESSAGING_ACTION;settings.m_sorted=true;settings.m_listItemName=getResource(MESSAGING_LIST_SINGLE_ITEM);settings.m_listItemsName=getResource(MESSAGING_LIST_MULITIPLE_ITEMS);this.CoreListEditor(settings,windowManager);this.m_debugName="MessagingMessagesEditor";}
copyPrototype(MessagingMessagesEditor,CoreListEditor);MessagingMessagesEditor.prototype.createToolbar=function()
{var args=new Array();CoreListEditor.prototype.createToolbar.apply(this,args);this.m_sendBtn=this.m_toolbar.addIconButton(APP_URL+"/images/acquiro/messaging/item_send.png",getResource(MESSAGING_LIST_SEND_MESSAGE),30);this.m_toolbar.disableButton(this.m_sendBtn);}
MessagingMessagesEditor.prototype.onListSelectionChange=function(objList)
{var args=new Array();args.push(objList);CoreListEditor.prototype.onListSelectionChange.apply(this,args);if(this.m_list.getSelectedItems().length>0)
{this.m_toolbar.enableButton(this.m_sendBtn);}
else
{this.m_toolbar.disableButton(this.m_sendBtn);}}
MessagingMessagesEditor.prototype.onToolbarButtonClick=function(objBtn)
{var args=new Array();args.push(objBtn);CoreListEditor.prototype.onToolbarButtonClick.apply(this,args);if(objBtn==this.m_sendBtn)
{this.onSendMessage();}}
MessagingMessagesEditor.prototype.onSendMessage=function(objBtn)
{var dialog=new SendMessageDialog(this.m_list.getSelectedItems()[0]);dialog.showDialog(this.onSendMessageDialog_Closed.bind(this));}
MessagingMessagesEditor.prototype.onSendMessageDialog_Closed=function(cancelled)
{if(cancelled)
{return;}}
function MessagingTab()
{this.MainTab();}
copyPrototype(MessagingTab,MainTab);MessagingTab.prototype.initialize_toolbar=function()
{this.m_toolbar=new MainTabToolbar(this);this.m_windowManager=new CoreObjectWindowManager();}
MessagingTab.prototype.initialize_rightPane=function()
{var args=new Array();MainTab.prototype.initialize_rightPane.apply(this,args);this.m_windowManager.setBackgroundColor("#BECAC8");this.m_windowManager.setToolbar(this.m_toolbar);this.m_windowManager.addListener(this);this.m_rightPane=this.m_windowManager;}
MessagingTab.prototype.initialize_list=function()
{var args=new Array();MainTab.prototype.initialize_list.apply(this,args);this.m_messagesList=new MessagingMessagesEditor(this.m_windowManager);this.m_messagesList.setWidth(175);this.m_listPanel=new TabControl();this.m_listPanel.setUseSmallTabs(true);this.m_listPanelTab=new Tab(getResource(MESSAGING_LIST_TITLE),getResource(MESSAGING_LIST_DESC),null);this.m_listPanelTab.setWidth(120);this.m_listPanel.addTab(this.m_listPanelTab,this.m_messagesList);}
MessagingTab.prototype.onWindowActivate=function()
{this.checkUIControls();}
MessagingTab.prototype.onWindowChanged=function(objWindow)
{this.checkUIControls();}
MessagingTab.prototype.onSave=function()
{this.m_windowManager.saveWindow();}
MessagingTab.prototype.onRefresh=function()
{this.m_windowManager.refreshWindow();}
MessagingTab.prototype.onWindowSaved=function()
{this.checkUIControls();}
MessagingTab.prototype.onWindowLoaded=function()
{this.checkUIControls();}
MessagingTab.prototype.onWindowLoaded=function()
{this.checkUIControls();}
MessagingTab.prototype.checkUIControls=function()
{if(!this.isCreated())
{return;}
this.m_toolbar.disableAll();var objWindow=this.m_windowManager.getActiveWindow();if(objWindow!=null)
{this.m_toolbar.enablePrint();if(objWindow.hasChanged())
{this.m_toolbar.enableSave();}
if(objWindow.canRefresh())
{this.m_toolbar.enableRefresh();}}}