var SwlFrame = {
	Name: "SwlFrame",
	ModifyRowID: -1,
  ModifyAction: 'none',

  InitFrame: function() {
 	  SwlFrame.CreateSwlList();	
		SwlFrame.HideEditForm();
    jQuery("#SwlListPanel").contextMenu({menu: 'SwlListMenu'}, SwlFrame.HandleSwlListMenu);

		jQuery("#SwlSearchForm").submit(SwlFrame.OnSubmitSwlSearchForm);
		jQuery("#SwlEditForm").submit(SwlFrame.OnSubmitSwlEditForm);
		jQuery("#SwlCancelButton").click(SwlFrame.clickSwlCancelButton);
  },

  ShowFrame: function() {
		jQuery('#SwlFrame').show();
		SwlFrame.OnFrameResize();
    SwlFrame.LoadSwlList('');
		SetWindowKeyPressHandler(SwlFrame.OnKeyPress);
  },

  HideFrame: function() {
    jQuery('#SwlFrame').hide();
  },

	OnKeyPress: function(event) {
		switch (event.keyCode) {
			case 116:
				SwlFrame.OnSubmitSwlSearchForm();
				return false;
				break;
		}
	},

	OnLogoff: function() {
		jQuery("#SwlList").clearGridData();
	},
	
  OnFrameResize: function() {
		var listWidth = jQuery("#SwlInputPanel").width();
		var listHeight = Layout.state.container.innerHeight - Layout.state.north.size - Layout.state.south.size - jQuery("#SwlInputPanel").height() - 120;
		
    jQuery("#SwlList").setGridWidth(listWidth, false); 
    jQuery("#SwlList").setGridHeight(listHeight, false); 
	},

  CreateSwlList: function() {
    jQuery("#SwlList").jqGrid({
			datatype: "local",
			height: 250,
			colNames:[_('ID'), '', _('Subject pattern'), _('Valid until'), _('Comment')],
			colModel:[
				{name:'id',index:'id', width:100, hidden:true},
				{name:'image',index:'image', width:20, formatter:SwlFrame.ImageFormatter},
				{name:'subjectPattern',index:'subjectPattern', width:300},
				{name:'validTo',index:'validTo', width:100, hidden:true},
				{name:'comment',index:'comment', width:500}
				],
			imgpath: gridimgpath,
			autoencode: true,
			multiselect: false,
			ondblClickRow: SwlFrame.OnDblClickSwlList
    }); 
  },

	ImageFormatter: function (cellvalue, options, rowObject) {
      return '<img src="/gui/images/icons/pin_green.png">';
	},

  HandleSwlListMenu: function(action, el, pos) {
		switch(action) {
			case 'Add':
			  SwlFrame.AddEntry();
			  break;
			case 'Edit':
  			SwlFrame.EditEntry();
			  break;
			case 'Delete':
			  SwlFrame.DeleteEntry();
			  break;
		}
	},

	OnSubmitSwlSearchForm: function() {
	  SwlFrame.LoadSwlList(document.getElementById("SwlSearchFormSubjectPattern").value);
	},
		
  //----------------------------------------------------------------------------------------------------------------
	
	ShowEditForm: function() {
	  jQuery("#SwlSearchFormContainer").hide();
	  jQuery("#SwlEditFormContainer").show();
	},

	HideEditForm: function() {
	  jQuery("#SwlEditFormContainer").hide();
	  jQuery("#SwlSearchFormContainer").show();
	},
	
  //----------------------------------------------------------------------------------------------------------------

	LoadSwlList: function(QueryString) {
	  jQuery("#lui_SwlList, #load_SwlList").show();
	
    var service = new Spamfinder(serviceURL, Session.SessionID);

    service.GetSubjectWhiteList(QueryString,
      {
        success: SwlFrame.GetSubjectWhiteList,
        failure: ErrorHandler
      }
    );
  },

  GetSubjectWhiteList: function(result) {
	  jQuery("#SwlList").clearGridData();
	  for (i = 0; i < result.EntryList.length; i++) {
	    Row = {id:result.EntryList[i].ID,
			       image:'',
			       subjectPattern:result.EntryList[i].SubjectPattern,
			       validTo:result.EntryList[i].ValidTo,
						 comment:result.EntryList[i].Description
						};

      jQuery("#SwlList").addRowData(i+1, Row); 
	  }
	  jQuery("#lui_SwlList, #load_SwlList").hide();
  },

//----------------------------------------------------------------------------------------------------------------

  AddEntry: function() {
		document.getElementById("SwlEditFormSubjectPattern").value = '';
		document.getElementById("SwlEditFormDescription").value = '';
		
    SwlFrame.ModifyAction = 'AddEntry';
 		SwlFrame.ShowEditForm();
		SwlFrame.OnFrameResize();

    jQuery("#SwlListPanel").disableContextMenu();

    document.getElementById("SwlEditFormSubjectPattern").focus();
	},
	
//----------------------------------------------------------------------------------------------------------------

  EditEntry: function() {
		SwlFrame.ModifyRowID = jQuery("#SwlList").getGridParam('selrow'); 
		if( SwlFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

    var RowData = jQuery("#SwlList").getRowData(SwlFrame.ModifyRowID); 

		document.getElementById("SwlEditFormSubjectPattern").value = RowData.subjectPattern;
		document.getElementById("SwlEditFormDescription").value = RowData.comment;

    SwlFrame.ModifyAction = 'EditEntry';
 		SwlFrame.ShowEditForm();
		SwlFrame.OnFrameResize();

    jQuery("#SwlListPanel").disableContextMenu();

    document.getElementById("SwlEditFormSubjectPattern").focus();
  },

//----------------------------------------------------------------------------------------------------------------

  DeleteEntry: function() {
		SwlFrame.ModifyRowID = jQuery("#SwlList").getGridParam('selrow'); 
		if( SwlFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

		jConfirm(_('Do you want to delete the selected items ?'), _('Confirmation'),
		function(r) {
		  if (r) {
				jQuery("#SwlListPanel").disableContextMenu();

				var id = jQuery("#SwlList").getCell(SwlFrame.ModifyRowID, 'id');
				
				var service = new Spamfinder(serviceURL, Session.SessionID);
				service.DeleteSubjectListEntry(id,
					{
						success: SwlFrame.DeleteSubjectListEntry,
						failure: ErrorHandler
					}
				);
			}
		});
	},

  DeleteSubjectListEntry: function() {
    jQuery("#SwlList").delRowData(SwlFrame.ModifyRowID);
		SwlFrame.ModifyRowID = -1;
    jQuery("#SwlListPanel").enableContextMenu();
	},
	
//----------------------------------------------------------------------------------------------------------------

  OnDblClickSwlList: function(rowid, iRow, iCol) {
    SwlFrame.HandleSwlListMenu('Edit', null, null);
  },

//----------------------------------------------------------------------------------------------------------------

  clickSwlCancelButton: function() {
    SwlFrame.ModifyAction = 'none';
    SwlFrame.HideEditForm();
		SwlFrame.OnFrameResize();
		
    jQuery("#SwlListPanel").enableContextMenu();
	},	

//----------------------------------------------------------------------------------------------------------------

  OnSubmitSwlEditForm: function() {
		switch(SwlFrame.ModifyAction) {
			case 'AddEntry':
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:-1,
              		 		 SubjectPattern:document.getElementById("SwlEditFormSubjectPattern").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:0,
											 UserID:Session.UserID,
											 Description:document.getElementById("SwlEditFormDescription").value
					         	  }; 
					service.SaveSubjectListEntry(Entry,
						{
							success: SwlFrame.SaveSubjectListEntry,
							failure: ErrorHandler
						}
					);
			  break;
			case 'EditEntry':
			    var id = jQuery("#SwlList").getCell(SwlFrame.ModifyRowID, 'id');
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:id,
              		 		 SubjectPattern:document.getElementById("SwlEditFormSubjectPattern").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:0,
											 UserID:Session.UserID,
											 Description:document.getElementById("SwlEditFormDescription").value
					         	  }; 
					service.SaveSubjectListEntry(Entry,
						{
							success: SwlFrame.SaveSubjectListEntry,
							failure: ErrorHandler
						}
					);
			  break;
		}
		
		return false;
	},
	
  SaveSubjectListEntry: function() {
    SwlFrame.ModifyAction = 'none';
    SwlFrame.HideEditForm();
    SwlFrame.OnFrameResize();
	 
    jQuery("#SwlListPanel").enableContextMenu();

    SwlFrame.LoadSwlList(document.getElementById("SwlSearchFormSubjectPattern").value);
	}
}

