SET NOCOUNT ON

DECLARE @GroupName varchar(30)
DECLARE @SortNumber int
DECLARE @GroupCode varchar(3)

DECLARE @CountryName varchar(30)
DECLARE @CountryCode varchar(3)
DECLARE @CurrencyCode varchar(3)
DECLARE @CurrencyName varchar(30)

DECLARE @CountryGroupNumber int

DECLARE @OtherEurope int
DECLARE @OtherAfrica int
DECLARE @OtherAmerica int
DECLARE @OtherAsia int
DECLARE @OtherOceanic int

DECLARE @TheCountryCode varchar(3)

SELECT @TheCountryCode = CountryCode FROM Countries WITH (NOLOCK)
WHERE CountryNumber = (SELECT KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'COUNTRYCODE')

IF @TheCountryCode = 'DK'
BEGIN
  CREATE TABLE #TempGroups (
    CountryName VARCHAR(30) NOT NULL,
    GroupSortNumber INT NOT NULL,
    CountryCode VARCHAR(3)  NOT NULL
  )

  INSERT INTO #TempGroups VALUES ('Danmark',1,'DK')
  INSERT INTO #TempGroups VALUES ('Færøerne',2,'FO')
  INSERT INTO #TempGroups VALUES ('Grønland',3,'GL')
  INSERT INTO #TempGroups VALUES ('Belgien',4,'B')
  INSERT INTO #TempGroups VALUES ('Bosnien_Hercegovina',5,'BA')  
  INSERT INTO #TempGroups VALUES ('Bulgarien',6,'BG')
  INSERT INTO #TempGroups VALUES ('Cypern',7,'CY')
  INSERT INTO #TempGroups VALUES ('Estland',8,'EE')
  INSERT INTO #TempGroups VALUES ('Finland',9,'FIN')
  INSERT INTO #TempGroups VALUES ('Frankrig',10,'F')
  INSERT INTO #TempGroups VALUES ('Grækenland',11,'GR')
  INSERT INTO #TempGroups VALUES ('Nederlandene',12,'NL')
  INSERT INTO #TempGroups VALUES ('Irland',13,'IRL')
  INSERT INTO #TempGroups VALUES ('Island',14,'IS')
  INSERT INTO #TempGroups VALUES ('Italien',15,'I')
  INSERT INTO #TempGroups VALUES ('Kroatien',16,'HR')
  INSERT INTO #TempGroups VALUES ('Letland',17,'LV')
  INSERT INTO #TempGroups VALUES ('Litauen',18,'LT')
  INSERT INTO #TempGroups VALUES ('Luxemborg',19,'L')
  INSERT INTO #TempGroups VALUES ('Makedonien',20,'MKD')
  INSERT INTO #TempGroups VALUES ('Malta',21,'MT')
  INSERT INTO #TempGroups VALUES ('Montenegro',22,'ME')
  INSERT INTO #TempGroups VALUES ('Norge',23,'N')
  INSERT INTO #TempGroups VALUES ('Polen',24,'PL')
  INSERT INTO #TempGroups VALUES ('Portugal',25,'P')
  INSERT INTO #TempGroups VALUES ('Rumænien',26,'P')
  INSERT INTO #TempGroups VALUES ('Rusland',27,'RU')
  INSERT INTO #TempGroups VALUES ('Schweiz',28,'CH')
  INSERT INTO #TempGroups VALUES ('Serbien',29,'RS')
  INSERT INTO #TempGroups VALUES ('Slovakiet',30,'SK')
  INSERT INTO #TempGroups VALUES ('Slovenien',31,'SI')
  INSERT INTO #TempGroups VALUES ('Spanien',32,'E')
  INSERT INTO #TempGroups VALUES ('Storbritanien',33,'GB')
  INSERT INTO #TempGroups VALUES ('Sverige',34,'S')
  INSERT INTO #TempGroups VALUES ('Tjekkiet',35,'CZ')
  INSERT INTO #TempGroups VALUES ('Tyrkiet',36,'TR')
  INSERT INTO #TempGroups VALUES ('Tyskland',37,'D')
  INSERT INTO #TempGroups VALUES ('Ukraine',38,'UA')
  INSERT INTO #TempGroups VALUES ('Ungarn',39,'H')
  INSERT INTO #TempGroups VALUES ('Østrig',40,'A')
  INSERT INTO #TempGroups VALUES ('Andre europæiske lande',41,'OE')
  INSERT INTO #TempGroups VALUES ('Sydafrika',42,'ZA')
  INSERT INTO #TempGroups VALUES ('Andre afrikanske lande',43,'OA1')
  INSERT INTO #TempGroups VALUES ('USA',44,'USA')
  INSERT INTO #TempGroups VALUES ('Canada',45,'CA')
  INSERT INTO #TempGroups VALUES ('Brasilien',46,'BR')
  INSERT INTO #TempGroups VALUES ('Andre amerikanske lande',47,'OA')
  INSERT INTO #TempGroups VALUES ('Folkerepublikken Kina',48,'CN')
  INSERT INTO #TempGroups VALUES ('Japan',49,'JP')
  INSERT INTO #TempGroups VALUES ('Sydkorea',50,'KR')
  INSERT INTO #TempGroups VALUES ('Indien',51,'IND')
  INSERT INTO #TempGroups VALUES ('Thailand',52,'TH')
  INSERT INTO #TempGroups VALUES ('Andre asiatiske lande',53,'OAL')
  INSERT INTO #TempGroups VALUES ('Australien',54,'AU')
  INSERT INTO #TempGroups VALUES ('Andre oceaniske lande',55,'OOL')

  DECLARE db_cur CURSOR FOR
    SELECT * FROM #TempGroups WITH (nolock)
  OPEN db_cur 
	   
  FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    
  WHILE @@fetch_status = 0 
  BEGIN
    -- Der er nogle lande hvor GroupName skal renames
    IF @SortNumber = 12   -- Holland
    BEGIN
      UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName LIKE '%Holland%'
      FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
      CONTINUE
    END
    
    IF NOT EXISTS (SELECT GroupName FROM CountryGroups WITH (NOLOCK) WHERE GroupName = @GroupName) 
    BEGIN
      INSERT INTO CountryGroups (GroupName, GroupSortNumber, GroupCode) VALUES (@GroupName,@SortNumber,@GroupCode)
    END
    ELSE
    BEGIN
      UPDATE CountryGroups SET GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName = @GroupName
    END
	  
    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
  END
  
  CREATE TABLE #TempCountries (
    CountryName VARCHAR(30) NOT NULL,
    CountryCode VARCHAR(3) NOT NULL,
    CurrencyCode VARCHAR(3) NOT NULL,
    CurrencyName VARCHAR(30) NOT NULL
  )
  
  INSERT INTO #TempCountries VALUES ('Denmark','DK','DKK','Danske kroner')
  INSERT INTO #TempCountries VALUES ('Faroe Islands','FO','DKK','Danske kroner')
  INSERT INTO #TempCountries VALUES ('Greenland','GL','DKK','Danske kroner')
  INSERT INTO #TempCountries VALUES ('Belgium','B','BEF','Franc')
  INSERT INTO #TempCountries VALUES ('Bosnia and Herzegovina','BA','BAM','Convertible Marka')  
  INSERT INTO #TempCountries VALUES ('Bulgaria','BG','BGN','Lev')
  INSERT INTO #TempCountries VALUES ('Cyprus','CY','CYP','Pound')
  INSERT INTO #TempCountries VALUES ('Estonia','EE','EEK','Kroon')
  INSERT INTO #TempCountries VALUES ('Finland','FIN','FIM','Markka')
  INSERT INTO #TempCountries VALUES ('France','F','FRF','Franc')
  INSERT INTO #TempCountries VALUES ('Greece','GR','GRD','Drachma')
  INSERT INTO #TempCountries VALUES ('Netherland','NL','NLG','Guilder')
  INSERT INTO #TempCountries VALUES ('Ireland','IRL','IEP','Punt')
  INSERT INTO #TempCountries VALUES ('Iceland','IS','ISK','Krona')
  INSERT INTO #TempCountries VALUES ('Italy','I','ITL','Lira')
  INSERT INTO #TempCountries VALUES ('Croatia','HR','HRK','Kuna')
  INSERT INTO #TempCountries VALUES ('Latvia','LV','LVL','Lat')
  INSERT INTO #TempCountries VALUES ('Lithuania','LT','LTL','Lita')
  INSERT INTO #TempCountries VALUES ('Luxembourg','L','LUF','Franc')
  INSERT INTO #TempCountries VALUES ('Macedonia','MKD','MKD','Denar')
  INSERT INTO #TempCountries VALUES ('Malta','MT','MTL','Lira')
  INSERT INTO #TempCountries VALUES ('Montenegro','ME','EUR','Euro')
  INSERT INTO #TempCountries VALUES ('Norway','N','NOK','Krone')
  INSERT INTO #TempCountries VALUES ('Poland','PL','PLN','Zloty')
  INSERT INTO #TempCountries VALUES ('Portugal','P','PTE','Escudo')
  INSERT INTO #TempCountries VALUES ('Russia','RU','RUR','Ruble')
  INSERT INTO #TempCountries VALUES ('Switzerland','CH','CHF','Franc')
  INSERT INTO #TempCountries VALUES ('Serbia','RS','RSD','Dinar')
  INSERT INTO #TempCountries VALUES ('Slovakia','SK','SKK','Koruna')
  INSERT INTO #TempCountries VALUES ('Slovenia','SI','SIT','Tolar')
  INSERT INTO #TempCountries VALUES ('Spain','E','ESP','Peseta')
  INSERT INTO #TempCountries VALUES ('Great Britain','GB','GBP','Pound Sterling')
  INSERT INTO #TempCountries VALUES ('Sweden','S','SEK','Krona')
  INSERT INTO #TempCountries VALUES ('Czech Republic','CZ','CZK','Koruna')
  INSERT INTO #TempCountries VALUES ('Turkia','TR','TRL','Lira')
  INSERT INTO #TempCountries VALUES ('Germany','D','DEM','Mark')
  INSERT INTO #TempCountries VALUES ('Ukraine','UA','UAK','Hryvnia')
  INSERT INTO #TempCountries VALUES ('Hungary','H','HUF','Forint')
  INSERT INTO #TempCountries VALUES ('Austria','A','ATS','Schilling')
  INSERT INTO #TempCountries VALUES ('Other Europe','OE','OE','')
  INSERT INTO #TempCountries VALUES ('South Africa','ZA','ZAR','Rand')
  INSERT INTO #TempCountries VALUES ('Other Africa','OA1','OA1','')
  INSERT INTO #TempCountries VALUES ('USA','USA','USD','Dollar')
  INSERT INTO #TempCountries VALUES ('Canada','CA','CAD','Dollar')
  INSERT INTO #TempCountries VALUES ('Brazil','BR','BRL','Real')
  INSERT INTO #TempCountries VALUES ('Other America','OA','OA','')
  INSERT INTO #TempCountries VALUES ('China','CN','CNY','Yuan Renminbi')
  INSERT INTO #TempCountries VALUES ('Japan','JP','JPY','Yen')
  INSERT INTO #TempCountries VALUES ('South Korea','KR','KRW','Won')
  INSERT INTO #TempCountries VALUES ('India','IND','INR','Rupee')
  INSERT INTO #TempCountries VALUES ('Thailand','TH','THB','Baht')
  INSERT INTO #TempCountries VALUES ('Other Asien','OAL','OAL','')
  INSERT INTO #TempCountries VALUES ('Australia','AU','AUD','Dollar')
  INSERT INTO #TempCountries VALUES ('Other Transatlantic','OOL','OOL','')
  INSERT INTO #TempCountries VALUES ('Liechtenstein','LI','CHF','Franc')
  INSERT INTO #TempCountries VALUES ('Mexico','Mx','MXN','Peso')

  DECLARE db_cur1 CURSOR FOR
    SELECT * FROM #TempCountries WITH (nolock)
  OPEN db_cur1 
	 
  FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	  
  WHILE @@fetch_status = 0 
  BEGIN
    IF NOT EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName) 
    BEGIN
      -- landet findes ikke - indsæt det
      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
      INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName, @CountryCode, 0, @CountryGroupNumber, @CurrencyCode, @CurrencyName, 999)
    END
    ELSE
    BEGIN 
      -- Landet findes - kontroller at groupnumber er korrekt.
      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
    END
	
    FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
  END

  --Hent Hvilket nr de enkelte grupper har fået.
  SELECT @OtherEurope = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 41
  SELECT @OtherAfrica = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 43
  SELECT @OtherAmerica = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 47
  SELECT @OtherAsia = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 53
  SELECT @OtherOceanic = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 55
	
  --hent liste over alle andre lande og del op i korrekte grupper
  DECLARE db_cur2 CURSOR FOR
    SELECT CountryName, CountryGroupNumber FROM Countries WITH (NOLOCK)
  OPEN db_cur2 
 
  FETCH NEXT FROM db_cur2 INTO @CountryName, @CountryGroupNumber
  
  WHILE @@fetch_status = 0 
  BEGIN
    IF @CountryName = 'Algeria' or @CountryName = 'Angola' or @CountryName = 'Benin' or @CountryName = 'Botswana' or @CountryName = 'Burkina Faso' or 
       @CountryName = 'Burundi' or @CountryName = 'Cameroon' or @CountryName = 'Cape Verde' or @CountryName = 'Central African Republic' or @CountryName = 'Chad' or 
       @CountryName = 'Comoros' or @CountryName = 'Cote dIvoire' or @CountryName = 'Egypt' or @CountryName = 'Equatorial Guinea' or @CountryName = 'Eritrea' or 
       @CountryName = 'Ethiopia' or @CountryName = 'Gabon' or @CountryName = 'Gambia' or @CountryName = 'Ghana' or @CountryName = 'Guinea-Bissau' or 
       @CountryName = 'Guinea' or @CountryName = 'Kenya' or @CountryName = 'Liberia' or @CountryName = 'Libya' or @CountryName = 'Madagascar' or @CountryName = 'Malawi' or 
       @CountryName = 'Mali' or @CountryName = 'Mauritania' or @CountryName = 'Mauritius' or @CountryName = 'Morocco' or @CountryName = 'Mozambique' or 
       @CountryName = 'Namibia' or @CountryName = 'Nigeria' or @CountryName = 'Niger' or @CountryName = 'Republic of the Congo' or @CountryName = 'Rwanda' or 
       @CountryName = 'Sao Tome and Principe' or @CountryName = 'Senegal' or @CountryName = 'Seychelles' or @CountryName = 'Sierra Leone' or @CountryName = 'Somalia' or 
       @CountryName = 'Sudan' or @CountryName = 'Swaziland' or @CountryName = 'Tanzania' or @CountryName = 'Togo' or @CountryName = 'Tunisia' or 
       @CountryName = 'Uganda' or @CountryName = 'Zambia' or @CountryName = 'Zimbabwe' or @CountryName = 'Congo' or @CountryName = 'Tunesia' 
    BEGIN
      -- Africa
      UPDATE Countries SET CountryGroupNumber = @OtherAfrica WHERE CountryName = @CountryName 
   END

   IF @CountryName = 'Armenia' OR @CountryName = 'ORAzerbaijan' OR @CountryName = 'Bangladesh' OR @CountryName = 'Brunei' OR @CountryName = 'Cambodia' OR
      @CountryName = 'Georgia' OR @CountryName = 'Hong Kong' OR @CountryName = 'Indonesia' OR @CountryName = 'Iran' OR
      @CountryName = 'Iraq' OR @CountryName = 'Israel' OR @CountryName = 'Jordan' OR @CountryName = 'Kazakhstan' OR @CountryName = 'Kuwait' OR
      @CountryName = 'Laos' OR @CountryName = 'Lebanon' OR @CountryName = 'Macao' OR @CountryName = 'Malaysia' OR @CountryName = 'Maldives' OR @CountryName = 'Mongolia' OR
      @CountryName = 'Myanmar' OR @CountryName = 'Nepal' OR @CountryName = 'North Korea' OR @CountryName = 'Oman' OR @CountryName = 'Pakistan' OR
      @CountryName = 'Philippines' OR @CountryName = 'Qatar' OR @CountryName = 'Saudi Arabia' OR @CountryName = 'Singapore' OR @CountryName = 'Sri Lanka' OR
      @CountryName = 'Syria' OR @CountryName = 'Taiwan' OR @CountryName = 'Tajikistan' OR @CountryName = 'Oman' OR
      @CountryName = 'United Arab Emirates' OR @CountryName = 'Vietnam' OR @CountryName = 'Yemen' OR @CountryName = 'Sri Lanca'
    BEGIN
      -- Asia
      UPDATE Countries SET CountryGroupNumber = @OtherAsia WHERE CountryName = @CountryName 
    END

    IF @CountryName = 'Federated States of Micronesia' OR @CountryName = 'Fiji' OR @CountryName = 'Kiribati' OR @CountryName = 'Marshall Islands' OR
       @CountryName = 'Nauru' OR @CountryName = 'New Zealand' OR @CountryName = 'Palau' OR @CountryName = 'Papua New Guinea' OR @CountryName = 'Samoa' OR
       @CountryName = 'Solomon Islands' OR @CountryName = 'Tonga' OR @CountryName = 'Tuvalu' OR @CountryName = 'Vanuatu'
    BEGIN
      -- Oceanic
      UPDATE Countries SET CountryGroupNumber = @OtherOceanic WHERE CountryName = @CountryName 
    END

    IF @CountryName = 'Albania' or @CountryName = 'Andorra' or @CountryName = 'Belarus' or
       @CountryName = 'Liechtenstein' or @CountryName = 'Moldova' or
       @CountryName = 'San Marino' or @CountryName = 'Vatican City' or @CountryName = 'Yugoslavia' OR @CountryName = 'Euro' or @CountryName = 'Vatican'
    BEGIN
      -- Europe 
      UPDATE Countries SET CountryGroupNumber = @OtherEurope WHERE CountryName = @CountryName 
    END

    IF @CountryName = 'Belize' OR @CountryName = 'Costa Rica' OR @CountryName = 'Cuba' OR @CountryName = 'Dominican Republic' OR @CountryName = 'El Salvador' OR
       @CountryName = 'Guatemala' OR @CountryName = 'Haiti' OR @CountryName = 'Honduras' OR @CountryName = 'Jamaica' OR @CountryName = 'Mexico' OR
       @CountryName = 'Nicaragua' OR @CountryName = 'Panama' OR @CountryName = 'Argentina' OR @CountryName = 'Bolivia' OR @CountryName = 'Chile' OR
       @CountryName = 'Colombia' OR @CountryName = 'Ecuador' OR @CountryName = 'French Guiana' OR @CountryName = 'Guyana' OR @CountryName = 'Paraguay' OR
       @CountryName = 'Peru' OR @CountryName = 'Suriname' OR @CountryName = 'Uruguay' OR @CountryName = 'Venezuela' OR @CountryName = 'Equador'
    BEGIN
      --America
      UPDATE Countries SET CountryGroupNumber = @OtherAmerica WHERE CountryName = @CountryName 
    END

    FETCH NEXT FROM db_cur2 INTO @CountryName, @CountryGroupNumber
  END

  DEALLOCATE db_cur
  DEALLOCATE db_cur1
  DEALLOCATE db_cur2

  DROP TABLE #TempGroups
  DROP TABLE #TempCountries
END