var AwlFrame = {
	Name: "AwlFrame",
	ModifyRowID: -1,
  ModifyAction: 'none',

  InitFrame: function() {
 	  AwlFrame.CreateAwlList();	
		AwlFrame.HideEditForm();
    jQuery("#AwlListPanel").contextMenu({menu: 'AwlListMenu'}, AwlFrame.HandleAwlListMenu);

		jQuery("#AwlSearchForm").submit(AwlFrame.OnSubmitAwlSearchForm);
		jQuery("#AwlEditForm").submit(AwlFrame.OnSubmitAwlEditForm);
		jQuery("#AwlCancelButton").click(AwlFrame.clickAwlCancelButton);
  },

  ShowFrame: function() {
		jQuery('#AwlFrame').show();
		AwlFrame.OnFrameResize();
    AwlFrame.LoadAwlList('');
		SetWindowKeyPressHandler(AwlFrame.OnKeyPress);
  },

  HideFrame: function() {
    jQuery('#AwlFrame').hide();
  },

	OnKeyPress: function(event) {
		switch (event.keyCode) {
			case 116:
				AwlFrame.OnSubmitAwlSearchForm();
				return false;
				break;
		}
	},
	
	OnLogoff: function() {
		jQuery("#AWlList").clearGridData();
	},
	
  OnFrameResize: function() {
		var listWidth = jQuery("#AwlInputPanel").width();
		var listHeight = Layout.state.container.innerHeight - Layout.state.north.size - Layout.state.south.size - jQuery("#AwlInputPanel").height() - 120;
		
    jQuery("#AwlList").setGridWidth(listWidth, false); 
    jQuery("#AwlList").setGridHeight(listHeight, false); 
	},

  CreateAwlList: function() {
    jQuery("#AwlList").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:AwlFrame.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: AwlFrame.OnDblClickAwlList
    }); 
  },

	ImageFormatter: function (cellvalue, options, rowObject) {
      return '<img src="/gui/images/icons/pin_green.png">';
	},

  HandleAwlListMenu: function(action, el, pos) {
		switch(action) {
			case 'Add':
			  AwlFrame.AddEntry();
			  break;
			case 'Edit':
  			AwlFrame.EditEntry();
			  break;
			case 'Delete':
			  AwlFrame.DeleteEntry();
			  break;
		}
	},

	OnSubmitAwlSearchForm: function() {
	  AwlFrame.LoadAwlList(document.getElementById("AwlSearchFormEmailAddress").value);
	},
		
  //----------------------------------------------------------------------------------------------------------------
	
	ShowEditForm: function() {
	  jQuery("#AwlSearchFormContainer").hide();
	  jQuery("#AwlEditFormContainer").show();
	},

	HideEditForm: function() {
	  jQuery("#AwlEditFormContainer").hide();
	  jQuery("#AwlSearchFormContainer").show();
	},
	
  //----------------------------------------------------------------------------------------------------------------

	LoadAwlList: function(QueryString) {
	  jQuery("#lui_AwlList, #load_AwlList").show();
	
    var service = new Spamfinder(serviceURL, Session.SessionID);

    service.GetAddressWhiteList(QueryString,
      {
        success: AwlFrame.GetAddressWhiteList,
        failure: ErrorHandler
      }
    );
  },

  GetAddressWhiteList: function(result) {
	  jQuery("#AwlList").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("#AwlList").addRowData(i+1, Row); 
	  }
	  jQuery("#lui_AwlList, #load_AwlList").hide();
  },

//----------------------------------------------------------------------------------------------------------------

  AddEntry: function() {
		document.getElementById("AwlEditFormEmailAddress").value = '';
		document.getElementById("AwlEditFormDescription").value = '';
		
    AwlFrame.ModifyAction = 'AddEntry';
 		AwlFrame.ShowEditForm();
		AwlFrame.OnFrameResize();

    jQuery("#AwlListPanel").disableContextMenu();

    document.getElementById("AwlEditFormEmailAddress").focus();
	},
	
//----------------------------------------------------------------------------------------------------------------

  EditEntry: function() {
		AwlFrame.ModifyRowID = jQuery("#AwlList").getGridParam('selrow'); 
		if( AwlFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

    var RowData = jQuery("#AwlList").getRowData(AwlFrame.ModifyRowID); 

		document.getElementById("AwlEditFormEmailAddress").value = RowData.address;
		document.getElementById("AwlEditFormDescription").value = RowData.comment;

    AwlFrame.ModifyAction = 'EditEntry';
 		AwlFrame.ShowEditForm();
		AwlFrame.OnFrameResize();

    jQuery("#AwlListPanel").disableContextMenu();

    document.getElementById("AwlEditFormEmailAddress").focus();
  },

//----------------------------------------------------------------------------------------------------------------

  DeleteEntry: function() {
		AwlFrame.ModifyRowID = jQuery("#AwlList").getGridParam('selrow'); 
		if( AwlFrame.ModifyRowID == null ) {
			jAlert(_('No row selected.'), _('Information'));
  		return;
		}

		jConfirm(_('Do you want to delete the selected items ?'), _('Confirmation'),
		function(r) {
		  if (r) {
				jQuery("#AwlListPanel").disableContextMenu();

				var id = jQuery("#AwlList").getCell(AwlFrame.ModifyRowID, 'id');
				
				var service = new Spamfinder(serviceURL, Session.SessionID);
				service.DeleteAddressListEntry(id,
					{
						success: AwlFrame.DeleteAddressListEntry,
						failure: ErrorHandler
					}
				);
			}
		});
	},

  DeleteAddressListEntry: function() {
    jQuery("#AwlList").delRowData(AwlFrame.ModifyRowID);
		AwlFrame.ModifyRowID = -1;
    jQuery("#AwlListPanel").enableContextMenu();
	},
	
//----------------------------------------------------------------------------------------------------------------

  OnDblClickAwlList: function(rowid, iRow, iCol) {
    AwlFrame.HandleAwlListMenu('Edit', null, null);
  },

//----------------------------------------------------------------------------------------------------------------

  clickAwlCancelButton: function() {
    AwlFrame.ModifyAction = 'none';
    AwlFrame.HideEditForm();
		AwlFrame.OnFrameResize();
		
    jQuery("#AwlListPanel").enableContextMenu();
	},	

//----------------------------------------------------------------------------------------------------------------

  OnSubmitAwlEditForm: function() {
		switch(AwlFrame.ModifyAction) {
			case 'AddEntry':
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:-1,
              		 		 SenderAddress:document.getElementById("AwlEditFormEmailAddress").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:0,
											 UserID:Session.UserID,
											 Description:document.getElementById("AwlEditFormDescription").value
					         	  }; 
					service.SaveAddressListEntry(Entry, false,
						{
							success: AwlFrame.SaveAddressListEntry,
							failure: ErrorHandler
						}
					);
			  break;
			case 'EditEntry':
			    var id = jQuery("#AwlList").getCell(AwlFrame.ModifyRowID, 'id');
					var service = new Spamfinder(serviceURL, Session.SessionID);
					var Entry = {ID:id,
              		 		 SenderAddress:document.getElementById("AwlEditFormEmailAddress").value,
            					 ValidTo:Date.parse('2099-12-31'),
											 Action:0,
											 UserID:Session.UserID,
											 Description:document.getElementById("AwlEditFormDescription").value
					         	  }; 
					service.SaveAddressListEntry(Entry, false,
						{
							success: AwlFrame.SaveAddressListEntry,
							failure: ErrorHandler
						}
					);
			  break;
		}
		
		return false;
	},
	
  SaveAddressListEntry: function() {
    AwlFrame.ModifyAction = 'none';
    AwlFrame.HideEditForm();
    AwlFrame.OnFrameResize();
	 
    jQuery("#AwlListPanel").enableContextMenu();

    AwlFrame.LoadAwlList(document.getElementById("AwlSearchFormEmailAddress").value);
	}
}

