var SblFrame = {
	Name: "SblFrame",
	ModifyRowID: -1,
  ModifyAction: 'none',

  InitFrame: function() {
 	  SblFrame.CreateSblList();	
		SblFrame.HideEditForm();
    jQuery("#SblListPanel").contextMenu({menu: 'SblListMenu'}, SblFrame.HandleSblListMenu);

		jQuery("#SblSearchForm").submit(SblFrame.OnSubmitSblSearchForm);
		jQuery("#SblEditForm").submit(SblFrame.OnSubmitSblEditForm);
		jQuery("#SblCancelButton").click(SblFrame.clickSblCancelButton);
  },

  ShowFrame: function() {
		jQuery('#SblFrame').show();
		SblFrame.OnFrameResize();
    SblFrame.LoadSblList('');
		SetWindowKeyPressHandler(SblFrame.OnKeyPress);
  },

  HideFrame: function() {
    jQuery('#SblFrame').hide();
  },

	OnKeyPress: function(event) {
		switch (event.keyCode) {
			case 116:
				SblFrame.OnSubmitSblSearchForm();
				return false;
				break;
		}
	},

	OnLogoff: function() {
		jQuery("#SblList").clearGridData();
	},
	
  OnFrameResize: function() {
		var listWidth = jQuery("#SblInputPanel").width();
		var listHeight = Layout.state.container.innerHeight - Layout.state.north.size - Layout.state.south.size - jQuery("#SblInputPanel").height() - 120;
		
    jQuery("#SblList").setGridWidth(listWidth, false); 
    jQuery("#SblList").setGridHeight(listHeight, false); 
	},

  CreateSblList: function() {
    jQuery("#SblList").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:SblFrame.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: SblFrame.OnDblClickSblList
    }); 
  },

	ImageFormatter: function (cellvalue, options, rowObject) {
      return '<img src="/gui/images/icons/pin_red.png">';
	},

  HandleSblListMenu: function(action, el, pos) {
		switch(action) {
			case 'Add':
			  SblFrame.AddEntry();
			  break;
			case 'Edit':
  			SblFrame.EditEntry();
			  break;
			case 'Delete':
			  SblFrame.DeleteEntry();
			  break;
		}
	},

	OnSubmitSblSearchForm: function() {
	  SblFrame.LoadSblList(document.getElementById("SblSearchFormSubjectPattern").value);
	},

  //----------------------------------------------------------------------------------------------------------------

	ShowEditForm: function() {
	  jQuery("#SblSearchFormContainer").hide();
	  jQuery("#SblEditFormContainer").show();
	},

	HideEditForm: function() {
	  jQuery("#SblEditFormContainer").hide();
	  jQuery("#SblSearchFormContainer").show();
	},

  //----------------------------------------------------------------------------------------------------------------

	LoadSblList: function(QueryString) {
	  jQuery("#lui_SblList, #load_SblList").show();
	
    var service = new Spamfinder(serviceURL, Session.SessionID);

    service.GetSubjectBlackList(QueryString,
      {
        success: SblFrame.GetSubjectBlackList,
        failure: ErrorHandler
      }
    );
  },

  GetSubjectBlackList: function(result) {
	  jQuery("#SblList").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("#SblList").addRowData(i+1, Row); 
	  }
	  jQuery("#lui_SblList, #load_SblList").hide();
  },

//----------------------------------------------------------------------------------------------------------------

  AddEntry: function() {
		document.getElementById("SblEditFormSubjectPattern").value = '';
		document.getElementById("SblEditFormDescription").value = '';
		
    SblFrame.ModifyAction = 'AddEntry';
 		SblFrame.ShowEditForm();
		SblFrame.OnFrameResize();

    jQuery("#SblListPanel").disableContextMenu();

    document.getElementById("SblEditFormSubjectPattern").focus();
	},
	
//----------------------------------------------------------------------------------------------------------------

  EditEntry: function() {
		SblFrame.ModifyRowID = jQuery("#SblList").getGridParam('selrow'); 
		if( SblFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

    var RowData = jQuery("#SblList").getRowData(SblFrame.ModifyRowID); 

		document.getElementById("SblEditFormSubjectPattern").value = RowData.subjectPattern;
		document.getElementById("SblEditFormDescription").value = RowData.comment;

    SblFrame.ModifyAction = 'EditEntry';
 		SblFrame.ShowEditForm();
		SblFrame.OnFrameResize();

    jQuery("#SblListPanel").disableContextMenu();

    document.getElementById("SblEditFormSubjectPattern").focus();
  },

//----------------------------------------------------------------------------------------------------------------

  DeleteEntry: function() {
		SblFrame.ModifyRowID = jQuery("#SblList").getGridParam('selrow'); 
		if( SblFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

		jConfirm(_('Do you want to delete the selected items ?'), _('Confirmation'),
		function(r) {
		  if (r) {
				jQuery("#SblListPanel").disableContextMenu();

				var id = jQuery("#SblList").getCell(SblFrame.ModifyRowID, 'id');
				
				var service = new Spamfinder(serviceURL, Session.SessionID);
				service.DeleteSubjectListEntry(id,
					{
						success: SblFrame.DeleteSubjectListEntry,
						failure: ErrorHandler
					}
				);
			}
		});
	},

  DeleteSubjectListEntry: function() {
    jQuery("#SblList").delRowData(SblFrame.ModifyRowID);
		SblFrame.ModifyRowID = -1;
    jQuery("#SblListPanel").enableContextMenu();
	},
	
//----------------------------------------------------------------------------------------------------------------

  OnDblClickSblList: function(rowid, iRow, iCol) {
    SblFrame.HandleSblListMenu('Edit', null, null);
  },

//----------------------------------------------------------------------------------------------------------------

  clickSblCancelButton: function() {
    SblFrame.ModifyAction = 'none';
    SblFrame.HideEditForm();
		SblFrame.OnFrameResize();
		
    jQuery("#SblListPanel").enableContextMenu();
	},	

//----------------------------------------------------------------------------------------------------------------

  OnSubmitSblEditForm: function() {
		switch(SblFrame.ModifyAction) {
			case 'AddEntry':
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:-1,
              		 		 SubjectPattern:document.getElementById("SblEditFormSubjectPattern").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:30,
											 UserID:Session.UserID,
											 Description:document.getElementById("SblEditFormDescription").value
					         	  }; 
					service.SaveSubjectListEntry(Entry,
						{
							success: SblFrame.SaveSubjectListEntry,
							failure: ErrorHandler
						}
					);
			  break;
			case 'EditEntry':
					var id = jQuery("#SblList").getCell(SblFrame.ModifyRowID, 'id');
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:id,
              		 		 SubjectPattern:document.getElementById("SblEditFormSubjectPattern").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:30,
											 UserID:Session.UserID,
											 Description:document.getElementById("SblEditFormDescription").value
					         	  }; 
					service.SaveSubjectListEntry(Entry,
						{
							success: SblFrame.SaveSubjectListEntry,
							failure: ErrorHandler
						}
					);
			  break;
		}
		
		return false;
	},
	
  SaveSubjectListEntry: function() {
    SblFrame.ModifyAction = 'none';
    SblFrame.HideEditForm();
    SblFrame.OnFrameResize();
	 
    jQuery("#SblListPanel").enableContextMenu();

    SblFrame.LoadSblList(document.getElementById("SblSearchFormSubjectPattern").value);
	}
}
