CREATE PROCEDURE [dbo].[OnlineUpdateAddress]
  @AReferenceNumber INT, @Addresstype SMALLINT, @Firmname VARCHAR(50), @AFirstname VARCHAR(50), @ALastname VARCHAR(50),
  @AAddress1 VARCHAR(50), @AAdderss2 VARCHAR(50), @AAdderss3 VARCHAR(50)='', @AZipcode VARCHAR(10), @ACity VARCHAR(50),
  @ACountryCode VARCHAR(3), @APhone VARCHAR(50), @AEmail VARCHAR(96), @ACell VARCHAR(50),
  @FAddress1 VARCHAR(50), @FAddress2 VARCHAR(50), @FZipcode VARCHAR(10), @FCity VARCHAR(50), @FCountryCode VARCHAR(3),
  @FPhone VARCHAR(30), @FMail VARCHAR(96), @ConFirstname VARCHAR(25), @ConLastname VARCHAR(25), @ConPhone VARCHAR(30),
  @ConMail VARCHAR(96), @ANoteText TEXT, @HasMoreRooms BIT, @ACustomerNumber INT = 0, @eanNumber VARCHAR(50) = '',
  @accountNumber VARCHAR(50) = '', @orderNumber VARCHAR(50) = '', @AGuestCustomerNumber INT = 0, @AFirmCustomerNumber INT = 0,
  @AContactCustomerNumber INT = 0, @AUseCustomerdiscount BIT = 0, @AUpdateReservationNote BIT = 0, @ANotePage INT = 1,
  @ANoNews BIT = 0, @GuestNumberNamelist int = 0, @CampaignCodeId INT = 0, @HotelNumber INT = 0,
  @AUserName VARCHAR(50)='', @APassWord VARCHAR(50)='', @ARoomDiscountPercent INT = 0
AS SET NOCOUNT ON
/*
    Created by Troels Buch Juni 2004

    Changes:
	AHE20210208: "LINK = @AGuestCustomerNumber," added for @Addresstype = 1 at line 163
	AHE20201030: OnlineAddCustomerMarketing and call to OnlineCreateCustomer removed
	AHE20201027: OnlineAddCustomerMarketing changed to OnlineAddCustomerMarketing2
	AHE20200311: FK_HotelCustomerNumber changed to "-1" in Reservationnotes, and new line only inserted if @ANoteText <> ''
    vd 20190927: Provision values now taken from CustomerFieldsPerHotel
    SG 2019-06-21: Store CountryNumber as well as CountryCode in ReservationAddress
    SG 2019-01-21: INSERT statement conflicted with the FOREIGN KEY constraint "FK_RESERVATIONNOTES_RESERVATIONS". @AUpdateReservationNote not used anymore
    TBU20180326: Added OnlineCreateCustomer and changed OnlineCreateCustomer
    TBU20180205: Removed OnlineCreateCustomer
    TBU20170707: Added @ARoomDiscountPercent
    KJ 20161230: Using UPPER results in table scan. removed to use index
    KJ 20161230: SQL optimize: No join predicate has been detected. Req 3279
	TBU20161031: Added IsNull - WHEN IsNull(ProvisionBill, 0) >= IsNull(ProvisionAccount, 0) THEN ProvisionBill   
	KJ 20160629: Bug: Fix for "Key constraint error" 
    SG 20151104: @ANotePage used in INSERT (was 1)
    TBU20150826: Added Agancy to Update splitbill
    TBU20150526: Added 0 to OnlineCreateCustomer
    TBU20150519: Added Discount for Guest
    TBU20150504: Added @AUserName VARCHAR(50)='', @APassWord VARCHAR(50)=''
    TB20131114: Changed campaigncode sql
    KJ 20131024: Added discount per hotel.
    TB20131016: Added @CampaignCodeId
    TB20130916: Added @ProvisionRate INT, @ProvisionType INT, @ProvisionOnGuest BIT
    TB20121203: Changed @Addresstype > 2 AND @Addresstype < 8 
	LJE20120713: If discount already found in ReservationAddress - prevent from overwritting.
    TB20120712: Added @AAdderss3 VARCHAR(50)=''
    TB20101118: Added @GuestNumberNamelist
	TB20090806: Added @ANoNews - Kun i 4.2
	TB20090225: Added @AUpdateReservationNote and @ANotePage
	TB20081209: Save Guestname when more rooms is booked
    AV20080612 Added CustomerDiscount
	TB20080611: Added parameter @AUseCustomerdiscount
	TB20071108: Added Default parameters
	NLB20061212: Added @eanNumber, @accountNumber and @orderNumber
	20051207: Save new customer in Marketing
	20050526 JP: changed to use GetNextNumber
	20050309:	Added AddressTypeNumber to ReservationAddressXTab
	July JP: cleaned up and called followup
    TB20041006: Moved  INSERT INTO Reservationnotes...  down  after END
	TB20041027: Added more parimiter with firminfo and use them  if Addresstype = 2
	TB20041206:  Change updating. If only 1 room is booked for Firm, then guestaddress  is also saved else  Firmname is save as guest Lastname/Arrangement

    NOTES: 	- 
*/

DECLARE
@ErrorLevel INT,
@Attentionname VARCHAR(100),
@AddressNumber INT,
@DiscountRooms MONEY,
@CurrentDiscount MONEY,
@DiscountAll MONEY,
@DiscountHalls MONEY,
@DiscountMeeting MONEY,
@TmpCustomerNumber INT,
@ProvisionRate INT,
@ProvisionType INT,
@ProvisionOnGuest BIT,
@SplitBill INT,
@ACountryNumber int, --Guest
@FCountryNumber int --Company

DECLARE @AddressTypeTmp INT 

IF @AGuestCustomerNumber = 0
  SET @AGuestCustomerNumber = null
IF @AFirmCustomerNumber = 0
  SET @AFirmCustomerNumber = null
IF @AContactCustomerNumber = 0
  SET @AContactCustomerNumber = null

SET @ErrorLevel = 00

set @Attentionname = @ConFirstname + ' ' +@ConLastname
/*
  Error Levels:
  00 : No errors
  01 : Guest Address not found
*/

--DECLARE @OnlineAddCustomerMarketing2 BIT
--SELECT @OnlineAddCustomerMarketing2=KeyValue FROM HotelSetup (NOLOCK) WHERE SearchKey='OnlineAddCustomerMarketing2'

DECLARE @HotelCustomerNumber INT

IF @AUseCustomerdiscount = 0
BEGIN
  SET @DiscountRooms = NULL
  SET @DiscountAll = NULL
  SET @DiscountMeeting = NULL
  SET @DiscountHalls = NULL
  SET @ProvisionRate = 0
  SET @ProvisionType = 0
  SET @ProvisionOnGuest = 0
  SET @SplitBill = 0;
  
  --If discount already found in ReservationAddress - prevent from overwritting.
  select @DiscountRooms = ISNULL(ra.RoomDiscountRate,0)
    ,@DiscountAll = ISNULL(ra.DISCOUNTRATE,0)
    ,@DiscountMeeting = ISNULL(ra.MeetingDiscountRate,0)
    ,@DiscountHalls = ISNULL(ra.HallDiscountRate,0)
  from reservations r WITH (NOLOCK)
  INNER JOIN reservationaddressxtab rx WITH (NOLOCK) ON r.ReferenceNumber = rx.REFERENCENUMBER
  INNER JOIN RESERVATIONADDRESS ra with (nolock) ON rx.ADDRESSNUMBER = ra.addressnumber
  where r.ReferenceNumber = @AReferenceNumber
END
ELSE
BEGIN
  SET @TmpCustomerNumber = @ACustomerNumber
  IF @AddressType > 1
    SET @TmpCustomerNumber = @AFirmCustomerNumber  
    
  IF @AddressType = 1 AND @AGuestCustomerNumber > 0
    SET @TmpCustomerNumber = @AGuestCustomerNumber 

  SELECT
    @ProvisionRate =  
    CASE
	  WHEN ISNULL(CFPH.ProvisionBill, 0) >= ISNULL(CFPH.ProvisionAccount, 0) THEN ISNULL(CFPH.ProvisionBill, 0)
	  ELSE ISNULL(CFPH.ProvisionAccount, 0)
	END,
    @ProvisionType =
	CASE
	  WHEN ISNULL(CFPH.ProvisionBill, 0) > 0 AND ISNULL(CFPH.ProvisionBill, 0) >= ISNULL(CFPH.ProvisionAccount, 0) THEN 2
	  WHEN ISNULL(CFPH.ProvisionAccount, 0) > 0 THEN 3
	  ELSE CFPH.ProvisionNetto  -- 0 or 1
	END,   
    @DiscountRooms = ISNULL(CFPH.DiscountRooms, 0),
	@DiscountAll = ISNULL(CFPH.DiscountAll, 0),
	@ProvisionOnGuest = ISNULL(CFPH.ProvisionPostOnGuest, 0),
	@DiscountHalls = ISNULL(CFPH.DiscountHalls, 0),
	@DiscountMeeting = ISNULL(CFPH.DiscountMetting, 0),
	@SplitBill = ISNULL(Customer.SplitBillNumber,0) 
  FROM Customer WITH (NOLOCK) LEFT JOIN CustomerFieldsPerHotel CFPH WITH (NOLOCK) ON 
    CFPH.CustomerNumber = Customer.CustomerNumber
  WHERE Customer.CustomerNumber = @TmpCustomerNumber AND CFPH.HotelNumber = @HotelNumber
END

--Find CountryNumbers:
SELECT TOP 1 @ACountryNumber = CountryNumber FROM Countries with (nolock) WHERE CountryCode = @ACountryCode
SELECT TOP 1 @FCountryNumber = CountryNumber FROM Countries with (nolock) WHERE CountryCode = @FCountryCode

-- TAddressType = (atNone, // 0  The order of these must NOT be changed!   atGuest, // 1     atCompany, // 2      atAgency, // 3      atAgent, // 4

IF @Addresstype = 1
BEGIN
   UPDATE ReservationAddress SET AddressType = 1, Firstname = @AFirstname, Lastname = @ALastname, LINK = @AGuestCustomerNumber,  
          Address1 = @AAddress1, Address2 = @AAdderss2, ADDRESS3 = @AAdderss3, Zipcode = @AZipcode, City = @ACity, 
		  CountryCode = @ACountryCode, CountryNumber = @ACountryNumber,
		  Phone = @APhone, Email = @AEmail, Cell = @ACell,
          RoomDiscountRate = @DiscountRooms, MeetingDiscountRate = @DiscountMeeting , HallDiscountRate = @DiscountHalls, DISCOUNTRATE = @DiscountAll 
   WHERE AddressType=1 AND AddressNumber IN (SELECT AddressNumber FROM ReservationAddressXTab (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=@GuestNumberNamelist)

   DECLARE @NumOf INT
   SELECT @NumOf= COUNT(*) FROM InetCustomer WHERE CustomerNumber=@AGuestCustomerNumber
   
   IF @AUserName <> '' AND @AGuestCustomerNumber > 0
   BEGIN
      IF @NumOf > 0
      BEGIN
         UPDATE InetCustomer SET Customerlogin=@AUserName, CustomerPassword=@APassWord WHERE CustomerNumber=@AGuestCustomerNumber
      END
      ELSE
      BEGIN
         INSERT INTO InetCustomer VALUES (@AGuestCustomerNumber,@AUserName,@APassWord)
      END	     
   END
END

IF  @Addresstype = 2 
BEGIN
  IF @HasMoreRooms = 1 -- If number of rooms total is more than 1, no guest information is saved
  BEGIN
      IF @AFirstname <> '' AND @ALastname <> ''
      BEGIN
         UPDATE ReservationAddress SET Firstname = @AFirstname, Lastname = @ALastname, AddressType =1, LINK = @AGuestCustomerNumber, RoomDiscountRate =@DiscountRooms, MeetingDiscountRate = @DiscountMeeting , HallDiscountRate = @DiscountHalls, DISCOUNTRATE = @DiscountAll 
         WHERE AddressType=1 AND AddressNumber IN (SELECT AddressNumber FROM ReservationAddressXTab (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=@GuestNumberNamelist)
      END
      ELSE
      BEGIN
         UPDATE ReservationAddress SET AddressType =1, Lastname = @Firmname, LINK = @AGuestCustomerNumber, RoomDiscountRate =@DiscountRooms, MeetingDiscountRate = @DiscountMeeting , HallDiscountRate = @DiscountHalls, DISCOUNTRATE = @DiscountAll 
         WHERE AddressType=1 AND AddressNumber IN (SELECT AddressNumber FROM ReservationAddressXTab (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=@GuestNumberNamelist)
      END
  END
  ELSE
  BEGIN
      UPDATE ReservationAddress SET AddressType = 1, Firstname = @AFirstname, Lastname = @ALastname,  
	         Address1 = @AAddress1, Address2 = @AAdderss2, ADDRESS3 = @AAdderss3, Zipcode = @AZipcode, City = @ACity, 
			 CountryCode = @ACountryCode, CountryNumber = @ACountryNumber,
			 Phone = @APhone, Email = @AEmail, Cell = @ACell, Link = @AGuestCustomerNumber,
             RoomDiscountRate = @DiscountRooms, MeetingDiscountRate = @DiscountMeeting , HallDiscountRate = @DiscountHalls, DISCOUNTRATE = @DiscountAll, 
			 PROVISIONRATE = IsNull(@ProvisionRate, 0), PROVISIONTYPE = IsNull(@ProvisionType, 0), ProvisionPostOnGuest = IsNull(@ProvisionOnGuest,0)
      WHERE AddressType=1 AND AddressNumber IN (SELECT AddressNumber FROM ReservationAddressXTab (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=@GuestNumberNamelist)     
  END

  EXEC GetNextNumber 'ReservationAddressIdentity', 1, @AddressNumber OUTPUT

  INSERT INTO ReservationAddress (AddressNumber, AddressType, Lastname, Address1, Address2, Zipcode, City, 
          CountryCode, CountryNumber,
		  Phone, Email, attentionname, companyForm, buyersAccount, buyersOrderID, 
		  Link, ContactpersonCustomerNumber, 
		  RoomDiscountRate, MeetingDiscountRate, HallDiscountRate, DISCOUNTRATE, PROVISIONRATE, ProvisionPostOnGuest, PROVISIONTYPE) 
  VALUES (@AddressNumber, 2, @Firmname, @FAddress1, @FAddress2, @FZipcode, @FCity, 
          @FCountryCode, @FCountryNumber,
		  @FPhone, @FMail, @Attentionname, @eanNumber, @accountNumber, @orderNumber, 
          @AFirmCustomerNumber, @AContactCustomerNumber, 
		  @DiscountRooms,  @DiscountMeeting , @DiscountHalls, @DiscountAll, @ProvisionRate, IsNull(@ProvisionOnGuest,0), @ProvisionType )
  INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@AReferenceNumber, @GuestNumberNamelist, @AddressNumber, @Addresstype)  --Added AddressTypeNumber

END

IF  @Addresstype > 2 AND @Addresstype < 8 
BEGIN  
  IF @HasMoreRooms = 1 -- If number of rooms total is more than 1, no guest information is saved
  BEGIN
      IF @AFirstname <> '' AND @ALastname <> ''
      BEGIN
         UPDATE ReservationAddress SET Firstname = @AFirstname, Lastname = @ALastname, AddressType =1, LINK = @AGuestCustomerNumber, RoomDiscountRate =@DiscountRooms, MeetingDiscountRate = @DiscountMeeting , HallDiscountRate = @DiscountHalls, DISCOUNTRATE = @DiscountAll 
         WHERE AddressType=1 AND AddressNumber IN (SELECT AddressNumber FROM ReservationAddressXTab (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=@GuestNumberNamelist)
      END
      ELSE
      BEGIN
         UPDATE ReservationAddress SET AddressType =1, Lastname = @Firmname, LINK = @AGuestCustomerNumber, RoomDiscountRate =@DiscountRooms, MeetingDiscountRate = @DiscountMeeting , HallDiscountRate = @DiscountHalls, DISCOUNTRATE = @DiscountAll 
         WHERE AddressType=1 AND AddressNumber IN (SELECT AddressNumber FROM ReservationAddressXTab (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=@GuestNumberNamelist)
      END
  END
  ELSE
  BEGIN
      UPDATE ReservationAddress SET AddressType = 1, Firstname = @AFirstname, Lastname = @ALastname,  
	         Address1 = @AAddress1, Address2 = @AAdderss2, ADDRESS3 = @AAdderss3, Zipcode = @AZipcode, City = @ACity, 
			 CountryCode = @ACountryCode, CountryNumber = @ACountryNumber,
			 Phone = @APhone, Email = @AEmail, Cell = @ACell, Link = @AGuestCustomerNumber,
             RoomDiscountRate = @DiscountRooms, MeetingDiscountRate = @DiscountMeeting , HallDiscountRate = @DiscountHalls, DISCOUNTRATE = @DiscountAll, 
			 PROVISIONRATE= IsNull(@ProvisionRate, 0), PROVISIONTYPE = IsNull(@ProvisionType, 0), ProvisionPostOnGuest = IsNull(@ProvisionOnGuest,0) 
      WHERE AddressType=1 AND AddressNumber IN (SELECT AddressNumber FROM ReservationAddressXTab (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=@GuestNumberNamelist)     
  END

  EXEC GetNextNumber 'ReservationAddressIdentity', 1, @AddressNumber OUTPUT
  
  SET @AddressTypeTmp = @Addresstype
  IF @Addresstype =6  	
     SET @AddresstypeTmp = 4 

  INSERT INTO ReservationAddress (AddressNumber, AddressType, Lastname, Address1, Address2, Zipcode, City, 
         CountryCode, CountryNumber,
		 Phone, Email, attentionname, companyForm, buyersAccount, buyersOrderID, 
		 Link, ContactpersonCustomerNumber, 
		 RoomDiscountRate, MeetingDiscountRate, HallDiscountRate, DISCOUNTRATE, PROVISIONRATE, ProvisionPostOnGuest, PROVISIONTYPE) 
  VALUES (@AddressNumber, @AddressTypeTmp, @Firmname, @FAddress1, @FAddress2, @FZipcode, @FCity, 
         @FCountryCode, @FCountryNumber,
		 @FPhone, @FMail, @Attentionname, @eanNumber, @accountNumber, @orderNumber, 
		 @AFirmCustomerNumber, @AContactCustomerNumber, 
		 @DiscountRooms,  @DiscountMeeting , @DiscountHalls, @DiscountAll, @ProvisionRate, IsNull(@ProvisionOnGuest,0), @ProvisionType)
  INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@AReferenceNumber, @GuestNumberNamelist, @AddressNumber, @AddresstypeTmp)  --Added AddressTypeNumber

END

SET @AddressTypeTmp = @Addresstype
IF @Addresstype =6  	
  SET @AddresstypeTmp = 4 
  
IF @AUseCustomerdiscount = 1 AND (@DiscountRooms > 0 OR @DiscountAll > 0 OR @DiscountHalls > 0 OR @DiscountMeeting > 0 ) AND @AddresstypeTmp IN (1, 2, 3, 4)
  UPDATE Reservations SET DiscountCustomerType = @AddresstypeTmp WHERE Reservations.ReferenceNumber = @AReferenceNumber   
   
IF @AUseCustomerdiscount = 1 AND IsNull(@SplitBill,0) > 0 AND @AddresstypeTmp IN (1, 2, 4)
  UPDATE Reservations SET SPLITBILL= IsNull(@SplitBill,0) WHERE Reservations.ReferenceNumber = @AReferenceNumber  

SELECT @HotelCustomerNumber = ReservationMain.FkCustomerHotel
FROM ReservationMain WITH (NOLOCK)
WHERE ReservationMain.ReferenceNumber = @AReferenceNumber

UPDATE Reservationnotes SET Text = char(13)+char(13)+ CONVERT(VARCHAR(7000),Text) + CONVERT(VARCHAR(7000), @ANoteText) 
WHERE referencenumber = @AReferenceNumber AND Guestnumber = @GuestNumberNamelist AND NotePage = @ANotePage
IF @@ROWCOUNT = 0
BEGIN
  IF CONVERT(VARCHAR(30),@ANoteText) <> ''
    INSERT INTO Reservationnotes (ReferenceNumber, Guestnumber, notepage, [text], FK_HotelCustomerNumber) 
    VALUES(@AReferenceNumber, @GuestNumberNamelist, @ANotePage,  @ANoteText, -1)  --QQAH @HotelCustomerNumber => -1 AND  @ANoteText <> ''
END

-- Check CampaignCode
DECLARE @DiscountCustomerType INT
DECLARE @CampaignId INT
DECLARE @DiscountType INT
DECLARE @DiscountPercent INT
IF @CampaignCodeId > 0
BEGIN

	IF @Addresstype = 1 OR @Addresstype = 2
	BEGIN		
  
		SELECT TOP 1 @DiscountType=IsNull(ccd.DiscountType,0), @DiscountPercent=IsNull(ccd.Value,0), @DiscountCustomerType=IsNull(cch.DiscountCustomerType,0) FROM CampaignCodes cc WITH (NOLOCK) 
		INNER JOIN CampaignCodesDiscount ccd WITH (NOLOCK) ON cc.PK_CampaignCode = ccd.PK_CampaignCode 
		INNER JOIN CampaignCodesHotel cch WITH (NOLOCK) ON cc.PK_CampaignCode = cch.PK_CampaignCode 
		WHERE cc.PK_CampaignCode =@CampaignCodeId
		AND DiscountType IN (0,1) 
		AND cch.FK_HotelCustomerNumber = (SELECT CustomerNumber FROM Customer WITH (NOLOCK) 
		WHERE HotelNumber = @HotelNumber)
	    
	    UPDATE Reservations SET DiscountCustomerType = @DiscountCustomerType, FK_CampaignCode=@CampaignCodeId WHERE Reservations.ReferenceNumber = @AReferenceNumber AND GuestNumber=0
	    
	    IF @DiscountType = 0
			SET @DiscountAll = @DiscountPercent
		IF @DiscountType = 1
			SET @DiscountRooms = @DiscountPercent			
	    
		UPDATE ReservationAddress SET RoomDiscountRate =@DiscountRooms, MeetingDiscountRate = @DiscountMeeting, DISCOUNTRATE = @DiscountAll 
		WHERE AddressType=1 AND AddressNumber IN (SELECT AddressNumber FROM ReservationAddressXTab WITH (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=@GuestNumberNamelist)   
	END		
END


-- Room discount
IF @ARoomDiscountPercent > 0
BEGIN
   UPDATE ReservationAddress SET RoomDiscountRate =@ARoomDiscountPercent
   WHERE AddressType=1 AND AddressNumber IN (SELECT AddressNumber FROM ReservationAddressXTab WITH (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=@GuestNumberNamelist)  
END

/*IF @OnlineAddCustomerMarketing2 = 1
BEGIN
   IF @Addresstype = 1
   BEGIN   
      EXEC OnlineCreateCustomer @AReferenceNumber, @AFirstname,  @ALastname, @AAddress1, @AAdderss2, @AZipcode, @ACity, @ACountryCode, @APhone, @ACell, @AEmail,  '', 1, @eanNumber, @ANoNews, 0, @AUserName, @APassWord 
   END
   IF @Addresstype = 2
   BEGIN       
      EXEC OnlineCreateCustomer @AReferenceNumber,  '',  '', @FAddress1, @FAddress2, @FZipcode, @FCity, @FCountryCode, @FPhone, '', @FMail, @Firmname, 2, @eanNumber, @ANoNews, 0, @AUserName, @APassWord 
   END
END */

IF @@rowcount = 0
BEGIN
  SET @ErrorLevel = 01
  RETURN
END

-- FOLLOWUP ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

EXEC OnlineUpdateFollowUp @AReferenceNumber

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
