var AblFrame = {
	Name: "AblFrame",
	ModifyRowID: -1,
  ModifyAction: 'none',

  InitFrame: function() {
 	  AblFrame.CreateAblList();	
		AblFrame.HideEditForm();
    jQuery("#AblListPanel").contextMenu({menu: 'AblListMenu'}, AblFrame.HandleAblListMenu);

		jQuery("#AblSearchForm").submit(AblFrame.OnSubmitAblSearchForm);
		jQuery("#AblEditForm").submit(AblFrame.OnSubmitAblEditForm);
		jQuery("#AblCancelButton").click(AblFrame.clickAblCancelButton);
  },

  ShowFrame: function() {
		jQuery('#AblFrame').show();
		AblFrame.OnFrameResize();
    AblFrame.LoadAblList('');
		SetWindowKeyPressHandler(AblFrame.OnKeyPress);
  },

  HideFrame: function() {
    jQuery('#AblFrame').hide();
  },

	OnKeyPress: function(event) {
		switch (event.keyCode) {
			case 116:
				AblFrame.OnSubmitAblSearchForm();
				return false;
				break;
		}
	},
	
	OnLogoff: function() {
		jQuery("#AblList").clearGridData();
	},
	
  OnFrameResize: function() {
		var listWidth = jQuery("#AblInputPanel").width();
		var listHeight = Layout.state.container.innerHeight - Layout.state.north.size - Layout.state.south.size - jQuery("#AblInputPanel").height() - 120;
		
    jQuery("#AblList").setGridWidth(listWidth, false); 
    jQuery("#AblList").setGridHeight(listHeight, false); 
	},

  CreateAblList: function() {
    jQuery("#AblList").jqGrid({
			datatype: "local",
			height: 250,
			colNames:[_('ID'), '', _('Address'), _('Valid until'), _('Comment')],
			colModel:[
				{name:'id',index:'id', width:100, hidden:true},
				{name:'image',index:'image', width:20, formatter:AblFrame.ImageFormatter},
				{name:'address',index:'address', width:300},
				{name:'validTo',index:'validTo', width:100, hidden:true},
				{name:'comment',index:'comment', width:500}
				],
			imgpath: gridimgpath,
			autoencode: true,
			multiselect: false,
			ondblClickRow: AblFrame.OnDblClickAblList
    }); 
  },

	ImageFormatter: function (cellvalue, options, rowObject) {
      return '<img src="/gui/images/icons/pin_red.png">';
	},

  HandleAblListMenu: function(action, el, pos) {
		switch(action) {
			case 'Add':
			  AblFrame.AddEntry();
			  break;
			case 'Edit':
  			AblFrame.EditEntry();
			  break;
			case 'Delete':
			  AblFrame.DeleteEntry();
			  break;
		}
	},

	OnSubmitAblSearchForm: function() {
	  AblFrame.LoadAblList(document.getElementById("AblSearchFormEmailAddress").value);
	},

  //----------------------------------------------------------------------------------------------------------------

	ShowEditForm: function() {
	  jQuery("#AblSearchFormContainer").hide();
	  jQuery("#AblEditFormContainer").show();
	},

	HideEditForm: function() {
	  jQuery("#AblEditFormContainer").hide();
	  jQuery("#AblSearchFormContainer").show();
	},

  //----------------------------------------------------------------------------------------------------------------

	LoadAblList: function(QueryString) {
	  jQuery("#lui_AblList, #load_AblList").show();
	
    var service = new Spamfinder(serviceURL, Session.SessionID);

    service.GetAddressBlackList(QueryString,
      {
        success: AblFrame.GetAddressBlackList,
        failure: ErrorHandler
      }
    );
  },

  GetAddressBlackList: function(result) {
	  jQuery("#AblList").clearGridData();
	  for (i = 0; i < result.EntryList.length; i++) {
	    Row = {id:result.EntryList[i].ID,
			       image:'',
			       address:result.EntryList[i].SenderAddress,
			       validTo:result.EntryList[i].ValidTo,
						 comment:result.EntryList[i].Description
						};

      jQuery("#AblList").addRowData(i+1, Row); 
	  }
	  jQuery("#lui_AblList, #load_AblList").hide();
  },

//----------------------------------------------------------------------------------------------------------------

  AddEntry: function() {
		document.getElementById("AblEditFormEmailAddress").value = '';
		document.getElementById("AblEditFormDescription").value = '';
		
    AblFrame.ModifyAction = 'AddEntry';
 		AblFrame.ShowEditForm();
		AblFrame.OnFrameResize();

    jQuery("#AblListPanel").disableContextMenu();

    document.getElementById("AblEditFormEmailAddress").focus();
	},
	
//----------------------------------------------------------------------------------------------------------------

  EditEntry: function() {
		AblFrame.ModifyRowID = jQuery("#AblList").getGridParam('selrow'); 
		if( AblFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

    var RowData = jQuery("#AblList").getRowData(AblFrame.ModifyRowID); 

		document.getElementById("AblEditFormEmailAddress").value = RowData.address;
		document.getElementById("AblEditFormDescription").value = RowData.comment;

    AblFrame.ModifyAction = 'EditEntry';
 		AblFrame.ShowEditForm();
		AblFrame.OnFrameResize();

    jQuery("#AblListPanel").disableContextMenu();

    document.getElementById("AblEditFormEmailAddress").focus();
  },

//----------------------------------------------------------------------------------------------------------------

  DeleteEntry: function() {
		AblFrame.ModifyRowID = jQuery("#AblList").getGridParam('selrow'); 
		if( AblFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

		jConfirm(_('Do you want to delete the selected items ?'), _('Confirmation'),
		function(r) {
		  if (r) {
				jQuery("#AblListPanel").disableContextMenu();

				var id = jQuery("#AblList").getCell(AblFrame.ModifyRowID, 'id');
				
				var service = new Spamfinder(serviceURL, Session.SessionID);
				service.DeleteAddressListEntry(id,
					{
						success: AblFrame.DeleteAddressListEntry,
						failure: ErrorHandler
					}
				);
			}
		});
	},

  DeleteAddressListEntry: function() {
    jQuery("#AblList").delRowData(AblFrame.ModifyRowID);
		AblFrame.ModifyRowID = -1;
    jQuery("#AblListPanel").enableContextMenu();
	},
	
//----------------------------------------------------------------------------------------------------------------

  OnDblClickAblList: function(rowid, iRow, iCol) {
    AblFrame.HandleAblListMenu('Edit', null, null);
  },

//----------------------------------------------------------------------------------------------------------------

  clickAblCancelButton: function() {
    AblFrame.ModifyAction = 'none';
    AblFrame.HideEditForm();
		AblFrame.OnFrameResize();
		
    jQuery("#AblListPanel").enableContextMenu();
	},	

//----------------------------------------------------------------------------------------------------------------

  OnSubmitAblEditForm: function() {
		switch(AblFrame.ModifyAction) {
			case 'AddEntry':
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:-1,
              		 		 SenderAddress:document.getElementById("AblEditFormEmailAddress").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:30,
											 UserID:Session.UserID,
											 Description:document.getElementById("AblEditFormDescription").value
					         	  }; 
					service.SaveAddressListEntry(Entry, false,
						{
							success: AblFrame.SaveAddressListEntry,
							failure: ErrorHandler
						}
					);
			  break;
			case 'EditEntry':
					var id = jQuery("#AblList").getCell(AblFrame.ModifyRowID, 'id');
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:id,
              		 		 SenderAddress:document.getElementById("AblEditFormEmailAddress").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:30,
											 UserID:Session.UserID,
											 Description:document.getElementById("AblEditFormDescription").value
					         	  }; 
					service.SaveAddressListEntry(Entry, false,
						{
							success: AblFrame.SaveAddressListEntry,
							failure: ErrorHandler
						}
					);
			  break;
		}
		
		return false;
	},
	
  SaveAddressListEntry: function() {
    AblFrame.ModifyAction = 'none';
    AblFrame.HideEditForm();
    AblFrame.OnFrameResize();
	 
    jQuery("#AblListPanel").enableContextMenu();

    AblFrame.LoadAblList(document.getElementById("AblSearchFormEmailAddress").value);
	}
}
