var DblFrame = {
	Name: "DblFrame",
	ModifyRowID: -1,
  ModifyAction: 'none',

  InitFrame: function() {
 	  DblFrame.CreateDblList();	
		DblFrame.HideEditForm();
    jQuery("#DblListPanel").contextMenu({menu: 'DblListMenu'}, DblFrame.HandleDblListMenu);

		jQuery("#DblSearchForm").submit(DblFrame.OnSubmitDblSearchForm);
		jQuery("#DblEditForm").submit(DblFrame.OnSubmitDblEditForm);
		jQuery("#DblCancelButton").click(DblFrame.clickDblCancelButton);
  },

  ShowFrame: function() {
		jQuery('#DblFrame').show();
		DblFrame.OnFrameResize();
    DblFrame.LoadDblList('');
		SetWindowKeyPressHandler(DblFrame.OnKeyPress);
  },

  HideFrame: function() {
    jQuery('#DblFrame').hide();
  },

	OnKeyPress: function(event) {
		switch (event.keyCode) {
			case 116:
				DblFrame.OnSubmitDblSearchForm();
				return false;
				break;
		}
	},
	
	OnLogoff: function() {
		jQuery("#DblList").clearGridData();
	},
	
  OnFrameResize: function() {
		var listWidth = jQuery("#DblInputPanel").width();
		var listHeight = Layout.state.container.innerHeight - Layout.state.north.size - Layout.state.south.size - jQuery("#DblInputPanel").height() - 120;
		
    jQuery("#DblList").setGridWidth(listWidth, false); 
    jQuery("#DblList").setGridHeight(listHeight, false); 
	},

  CreateDblList: function() {
    jQuery("#DblList").jqGrid({
			datatype: "local",
			height: 250,
			colNames:[_('ID'), '', _('Domain'), _('Valid until'), _('Comment')],
			colModel:[
				{name:'id',index:'id', width:100, hidden:true},
				{name:'image',index:'image', width:20, formatter:DblFrame.ImageFormatter},
				{name:'domain',index:'domain', width:300},
				{name:'validTo',index:'validTo', width:100, hidden:true},
				{name:'comment',index:'comment', width:500}
				],
			imgpath: gridimgpath,
			autoencode: true,
			multiselect: false,
			ondblClickRow: DblFrame.OnDblClickDblList
    }); 
  },

	ImageFormatter: function (cellvalue, options, rowObject) {
      return '<img src="/gui/images/icons/pin_red.png">';
	},

  HandleDblListMenu: function(action, el, pos) {
		switch(action) {
			case 'Add':
			  DblFrame.AddEntry();
			  break;
			case 'Edit':
  			DblFrame.EditEntry();
			  break;
			case 'Delete':
			  DblFrame.DeleteEntry();
			  break;
		}
	},

	OnSubmitDblSearchForm: function() {
	  DblFrame.LoadDblList(document.getElementById("DblSearchFormDomain").value);
	},

  //----------------------------------------------------------------------------------------------------------------

	ShowEditForm: function() {
	  jQuery("#DblSearchFormContainer").hide();
	  jQuery("#DblEditFormContainer").show();
	},

	HideEditForm: function() {
	  jQuery("#DblEditFormContainer").hide();
	  jQuery("#DblSearchFormContainer").show();
	},

  //----------------------------------------------------------------------------------------------------------------

	LoadDblList: function(QueryString) {
	  jQuery("#lui_DblList, #load_DblList").show();
	
    var service = new Spamfinder(serviceURL, Session.SessionID);

    service.GetDomainBlackList(QueryString,
      {
        success: DblFrame.GetSubjectBlackList,
        failure: ErrorHandler
      }
    );
  },

  GetSubjectBlackList: function(result) {
	  jQuery("#DblList").clearGridData();
	  for (i = 0; i < result.EntryList.length; i++) {
	    Row = {id:result.EntryList[i].ID,
			       image:'',
			       domain:result.EntryList[i].SenderDomain,
			       validTo:result.EntryList[i].ValidTo,
						 comment:result.EntryList[i].Description
						};

      jQuery("#DblList").addRowData(i+1, Row); 
	  }
	  jQuery("#lui_DblList, #load_DblList").hide();
  },

//----------------------------------------------------------------------------------------------------------------

  AddEntry: function() {
		document.getElementById("DblEditFormDomain").value = '';
		document.getElementById("DblEditFormDescription").value = '';
		
    DblFrame.ModifyAction = 'AddEntry';
 		DblFrame.ShowEditForm();
		DblFrame.OnFrameResize();

    jQuery("#DblListPanel").disableContextMenu();

    document.getElementById("DblEditFormDomain").focus();
	},
	
//----------------------------------------------------------------------------------------------------------------

  EditEntry: function() {
		DblFrame.ModifyRowID = jQuery("#DblList").getGridParam('selrow'); 
		if( DblFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

    var RowData = jQuery("#DblList").getRowData(DblFrame.ModifyRowID); 

		document.getElementById("DblEditFormDomain").value = RowData.domain;
		document.getElementById("DblEditFormDescription").value = RowData.comment;

    DblFrame.ModifyAction = 'EditEntry';
 		DblFrame.ShowEditForm();
		DblFrame.OnFrameResize();

    jQuery("#DblListPanel").disableContextMenu();

    document.getElementById("DblEditFormDomain").focus();
  },

//----------------------------------------------------------------------------------------------------------------

  DeleteEntry: function() {
		DblFrame.ModifyRowID = jQuery("#DblList").getGridParam('selrow'); 
		if( DblFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

		jConfirm(_('Do you want to delete the selected items ?'), _('Confirmation'),
		function(r) {
		  if (r) {
				jQuery("#DblListPanel").disableContextMenu();

				var id = jQuery("#DblList").getCell(DblFrame.ModifyRowID, 'id');
				
				var service = new Spamfinder(serviceURL, Session.SessionID);
				service.DeleteDomainListEntry(id,
					{
						success: DblFrame.DeleteDomainListEntry,
						failure: ErrorHandler
					}
				);
			}
		});
	},

  DeleteDomainListEntry: function() {
    jQuery("#DblList").delRowData(DblFrame.ModifyRowID);
		DblFrame.ModifyRowID = -1;
    jQuery("#DblListPanel").enableContextMenu();
	},
	
//----------------------------------------------------------------------------------------------------------------

  OnDblClickDblList: function(rowid, iRow, iCol) {
    DblFrame.HandleDblListMenu('Edit', null, null);
  },

//----------------------------------------------------------------------------------------------------------------

  clickDblCancelButton: function() {
    DblFrame.ModifyAction = 'none';
    DblFrame.HideEditForm();
		DblFrame.OnFrameResize();
		
    jQuery("#DblListPanel").enableContextMenu();
	},	

//----------------------------------------------------------------------------------------------------------------

  OnSubmitDblEditForm: function() {
		switch(DblFrame.ModifyAction) {
			case 'AddEntry':
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:-1,
              		 		 SenderDomain:document.getElementById("DblEditFormDomain").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:30,
											 UserID:Session.UserID,
											 Description:document.getElementById("DblEditFormDescription").value
					         	  }; 
					service.SaveDomainListEntry(Entry,
						{
							success: DblFrame.SaveDomainListEntry,
							failure: ErrorHandler
						}
					);
			  break;
			case 'EditEntry':
					var id = jQuery("#DblList").getCell(DblFrame.ModifyRowID, 'id');
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:id,
              		 		 SenderDomain:document.getElementById("DblEditFormDomain").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:30,
											 UserID:Session.UserID,
											 Description:document.getElementById("DblEditFormDescription").value
					         	  }; 
					service.SaveDomainListEntry(Entry,
						{
							success: DblFrame.SaveDomainListEntry,
							failure: ErrorHandler
						}
					);
			  break;
		}
		
		return false;
	},
	
  SaveDomainListEntry: function() {
    DblFrame.ModifyAction = 'none';
    DblFrame.HideEditForm();
    DblFrame.OnFrameResize();
	 
    jQuery("#DblListPanel").enableContextMenu();

    DblFrame.LoadDblList(document.getElementById("DblSearchFormDomain").value);
	}
}

