var DwlFrame = {
	Name: "DwlFrame",
	ModifyRowID: -1,
  ModifyAction: 'none',

  InitFrame: function() {
 	  DwlFrame.CreateDwlList();	
		DwlFrame.HideEditForm();
    jQuery("#DwlListPanel").contextMenu({menu: 'DwlListMenu'}, DwlFrame.HandleDwlListMenu);

		jQuery("#DwlSearchForm").submit(DwlFrame.OnSubmitDwlSearchForm);
		jQuery("#DwlEditForm").submit(DwlFrame.OnSubmitDwlEditForm);
		jQuery("#DwlCancelButton").click(DwlFrame.clickDwlCancelButton);
  },

  ShowFrame: function() {
		jQuery('#DwlFrame').show();
		DwlFrame.OnFrameResize();
    DwlFrame.LoadDwlList('');
		SetWindowKeyPressHandler(DwlFrame.OnKeyPress);
  },

  HideFrame: function() {
    jQuery('#DwlFrame').hide();
  },

	OnKeyPress: function(event) {
		switch (event.keyCode) {
			case 116:
				DwlFrame.OnSubmitDwlSearchForm();
				return false;
				break;
		}
	},
	
	OnLogoff: function() {
		jQuery("#DwlList").clearGridData();
	},
	
  OnFrameResize: function() {
		var listWidth = jQuery("#DwlInputPanel").width();
		var listHeight = Layout.state.container.innerHeight - Layout.state.north.size - Layout.state.south.size - jQuery("#DwlInputPanel").height() - 120;
		
    jQuery("#DwlList").setGridWidth(listWidth, false); 
    jQuery("#DwlList").setGridHeight(listHeight, false); 
	},

  CreateDwlList: function() {
    jQuery("#DwlList").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:DwlFrame.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: DwlFrame.OnDblClickDwlList
    }); 
  },

	ImageFormatter: function (cellvalue, options, rowObject) {
      return '<img src="/gui/images/icons/pin_green.png">';
	},

  HandleDwlListMenu: function(action, el, pos) {
		switch(action) {
			case 'Add':
			  DwlFrame.AddEntry();
			  break;
			case 'Edit':
  			DwlFrame.EditEntry();
			  break;
			case 'Delete':
			  DwlFrame.DeleteEntry();
			  break;
		}
	},

	OnSubmitDwlSearchForm: function() {
	  DwlFrame.LoadDwlList(document.getElementById("DwlSearchFormDomain").value);
	},
		
  //----------------------------------------------------------------------------------------------------------------
	
	ShowEditForm: function() {
	  jQuery("#DwlSearchFormContainer").hide();
	  jQuery("#DwlEditFormContainer").show();
	},

	HideEditForm: function() {
	  jQuery("#DwlEditFormContainer").hide();
	  jQuery("#DwlSearchFormContainer").show();
	},
	
  //----------------------------------------------------------------------------------------------------------------

	LoadDwlList: function(QueryString) {
	  jQuery("#lui_DwlList, #load_DwlList").show();
	
    var service = new Spamfinder(serviceURL, Session.SessionID);

    service.GetDomainWhiteList(QueryString,
      {
        success: DwlFrame.GetDomainWhiteList,
        failure: ErrorHandler
      }
    );
  },

  GetDomainWhiteList: function(result) {
	  jQuery("#DwlList").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("#DwlList").addRowData(i+1, Row); 
	  }
	  jQuery("#lui_DwlList, #load_DwlList").hide();
  },

//----------------------------------------------------------------------------------------------------------------

  AddEntry: function() {
		document.getElementById("DwlEditFormDomain").value = '';
		document.getElementById("DwlEditFormDescription").value = '';
		
    DwlFrame.ModifyAction = 'AddEntry';
 		DwlFrame.ShowEditForm();
		DwlFrame.OnFrameResize();

    jQuery("#DwlListPanel").disableContextMenu();

    document.getElementById("DwlEditFormDomain").focus();
	},
	
//----------------------------------------------------------------------------------------------------------------

  EditEntry: function() {
		DwlFrame.ModifyRowID = jQuery("#DwlList").getGridParam('selrow'); 
		if( DwlFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

    var RowData = jQuery("#DwlList").getRowData(DwlFrame.ModifyRowID); 

		document.getElementById("DwlEditFormDomain").value = RowData.domain;
		document.getElementById("DwlEditFormDescription").value = RowData.comment;

    DwlFrame.ModifyAction = 'EditEntry';
 		DwlFrame.ShowEditForm();
		DwlFrame.OnFrameResize();

    jQuery("#DwlListPanel").disableContextMenu();

    document.getElementById("DwlEditFormDomain").focus();
  },

//----------------------------------------------------------------------------------------------------------------

  DeleteEntry: function() {
		DwlFrame.ModifyRowID = jQuery("#DwlList").getGridParam('selrow'); 
		if( DwlFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

		jConfirm(_('Do you want to delete the selected items ?'), _('Confirmation'),
		function(r) {
		  if (r) {
				jQuery("#DwlListPanel").disableContextMenu();

				var id = jQuery("#DwlList").getCell(DwlFrame.ModifyRowID, 'id');
				
				var service = new Spamfinder(serviceURL, Session.SessionID);
				service.DeleteDomainListEntry(id,
					{
						success: DwlFrame.DeleteDomainListEntry,
						failure: ErrorHandler
					}
				);
			}
		});
	},

  DeleteDomainListEntry: function() {
    jQuery("#DwlList").delRowData(DwlFrame.ModifyRowID);
		DwlFrame.ModifyRowID = -1;
    jQuery("#DwlListPanel").enableContextMenu();
	},
	
//----------------------------------------------------------------------------------------------------------------

  OnDblClickDwlList: function(rowid, iRow, iCol) {
    DwlFrame.HandleDwlListMenu('Edit', null, null);
  },

//----------------------------------------------------------------------------------------------------------------

  clickDwlCancelButton: function() {
    DwlFrame.ModifyAction = 'none';
    DwlFrame.HideEditForm();
		DwlFrame.OnFrameResize();
		
    jQuery("#DwlListPanel").enableContextMenu();
	},	

//----------------------------------------------------------------------------------------------------------------

  OnSubmitDwlEditForm: function() {
		switch(DwlFrame.ModifyAction) {
			case 'AddEntry':
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:-1,
              		 		 SenderDomain:document.getElementById("DwlEditFormDomain").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:0,
											 UserID:Session.UserID,
											 Description:document.getElementById("DwlEditFormDescription").value
					         	  }; 
					service.SaveDomainListEntry(Entry,
						{
							success: DwlFrame.SaveDomainListEntry,
							failure: ErrorHandler
						}
					);
			  break;
			case 'EditEntry':
			    var id = jQuery("#DwlList").getCell(DwlFrame.ModifyRowID, 'id');
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:id,
              		 		 SenderDomain:document.getElementById("DwlEditFormDomain").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:0,
											 UserID:Session.UserID,
											 Description:document.getElementById("DwlEditFormDescription").value
					         	  }; 
					service.SaveDomainListEntry(Entry,
						{
							success: DwlFrame.SaveDomainListEntry,
							failure: ErrorHandler
						}
					);
			  break;
		}
		
		return false;
	},
	
  SaveDomainListEntry: function() {
    DwlFrame.ModifyAction = 'none';
    DwlFrame.HideEditForm();
    DwlFrame.OnFrameResize();
	 
    jQuery("#DwlListPanel").enableContextMenu();

    DwlFrame.LoadDwlList(document.getElementById("DwlSearchFormDomain").value);
	}
}
