'MacroName:formHeadings
'MacroDescription:

global CS As Object   
global AllSubjects$
global AllGenres$
global addedGenre

'------------
function GetAllGenres as string
   dim temp$, x%
   
   x% = 1
   do
      CS.GetField "655", x%, genre$
      if genre$ > "" then
         if instr(genre$, "2 fast") or instr(genre$, "2 rbgenr") then
            ' do nothing
         else
            temp$ = temp$ & genre$ & "|"         
         end if
      else
         exit do
      end if
      x% = x% + 1
   loop

   GetAllGenres = temp$

end function
'------------
function GetAllSubjects(tag$) as string
   dim temp$, x%
   
   x% = 1
   do
      CS.GetField tag$, x%, subject$
      if subject$ > "" then
         if instr(subject$, "2 fast") then
            'do nothing
         else
            temp$ = temp$ & subject$ & "|"         
         end if
      else
         exit do
      end if
      x% = x% + 1
   loop

   GetAllSubjects = temp$

end function
'------------
sub addGenre(subj$, genre$)
   subj$ = "v " & subj$
   if instr(AllSubjects$, subj$) then
      if instr(AllGenres$, genre$) = 0 then 
         CS.Addfield 99, "655 7" & genre$
         addedGenre = TRUE
      end if
   end if
end sub
'------------
Sub Main

   Set CS = CreateObject("Connex.Client")
   
   AllGenres$ = GetAllGenres()
   AllSubjects$ = GetAllSubjects("600")
   AllSubjects$ = AllSubjects & GetAllSubjects("610")
   AllSubjects$ = AllSubjects & GetAllSubjects("611")
   AllSubjects$ = AllSubjects & GetAllSubjects("630")
   AllSubjects$ = AllSubjects & GetAllSubjects("650")
   AllSubjects$ = AllSubjects & GetAllSubjects("651")
   
   'atlases
   CS.GetFixedField "Audn", Audn$
   if instr(AllSubjects$, "tlases") then
      if Audn$ like "[a-d, j]" or instr(AllSubjects$, "Children's atlases") then
            CS.AddField 99, "655 7Children's atlases"
      else
            CS.AddField 99, "655 7Atlases"
      end if         
   end if   
   
   'biography/literary text
   CS.GetFixedField "type", MARCtype$
   if MarcType$ = "i" then
      CS.GetFixedField "LTxt", biog$
      select case biog$
         case "a"
            form$ = "Autobiographies"
         case "b"
            form$ = "Biographies"
         case "c"
         case "d"
         case "e"
            form$ = "Essays"
         case "f"
         case "g"
         case "h"
         case "i"
            form$ = "Instructional and educational works"
         case "j"
         case "k"
         case "l"
         case "m"
            form$ = "Autobiographies"
         case "n"
         case "o"
         case "p"
         case "r"
         case "s"
         case "t"
            form$ = "Interviews (Sound recordings)"
      end select
   else      
      CS.GetFixedField "biog", biog$
      select case biog$
         case "a"
            form$ = "Autobiographies"
         case "b", "c", "d"
            form$ = "Biographies"
      end select
   end if
   
   if instr(AllGenres$, form$) or form$ = "" then
      'do nothing
   else
      CS.AddField 99, "655 7" & form$
   end if
   
   'form
   CS.GetFixedField "form", form$
   select case form$
      'case "d"
         'temp$ = "Large type books. " & chr(223) & "2 local"
      case "f"
         temp$ = "Braille books"
      case "r"
         temp$ = "Facsimiles"
      case else
         temp$ = ""         
   end select
   if temp$ > "" and instr(AllGenres$, temp$) = 0 then CS.AddField 99, "655 7" & temp$
   
   'magazines
   CS.GetFixedField "SrTp", srtp$
   select case srtp$
      case "p"
         form$ = "Periodicals"
      case "n"
         form$ = "Newspapers"
      case else
         form$ = ""
   end select
   
   if instr(AllGenres$, form$) or form$ = "" then
      'do nothing
   else
      CS.AddField 99, "655 7" & form$
   end if

   'LitF
   CS.GetFixedField "LitF", LitF$
   select case LitF$
      case "d"
         temp$ = "Drama"
      case "e"
         temp$ = "Essays"
      case "f"
         if instr(AllGenres$, "iction") = 0 and instr(AllGenres$, "stories") = 0  then temp$ = "Fiction"
      case "h"
         temp$ = "Humor"
      case "i"
         temp$ = "Personal correspondence"
      case "j"
         temp$ = "Short stories"
      case "p"
         temp$ = "Poetry"
      case "s"
         temp$ = "Speeches"
      case else
         temp$ = ""         
   end select
   if temp$ > "" then 
      if instr(AllGenres$, temp$) = 0 then CS.Addfield 99, "655 7" & temp$
   end if

   'uniform titles
   CS.GetField "240", 1, UT$
   if UT$ > "" then
      if instr(UT$, "Laws") then CS.AddField 999, "655 7Laws"
   end if
      
   'subject form headings
   call addGenre("Adaptations", "Adaptations")
   call addGenre("Almanacs", "Almanacs")
   call addGenre("Amateurs' manuals", "Handbooks and manuals")
   call addGenre("Anecdotes", "Anecdotes")

   'Annals and chronicles
   if instr(AllSubjects$, "History " & chr(223) & "v Chronology") then
      if instr(AllGenres$, "Annals and chronicles") = 0 then CS.Addfield 99, "655 7Annals and chronicles"
   end if

   'Arthurian romances
   if instr(AllSubjects$, "Arthurian romances") then
      if instr(AllGenres$, "Arthurian romances") = 0 then CS.Addfield 99, "655 7Arthurian romances"
   end if

   call addGenre("Bibliography", "Bibliographies")
   call addGenre("Bio-bibliography", "Bibliographies")
   call addGenre("Bio-bibliography", "Biographies")
   call addGenre("Blogs", "Blogs")
   call addGenre("Catalogs", "Catalogs")
   call addGenre("Catalogues raison", "Catalogues raisonnes")
   call addGenre("Charters", "Charters and articles of incorporation")
   call addGenre("Chronology", "Chronologies")
   call addGenre("Comic books, strips, etc.", "Graphic novels")
   call addGenre("Concordances", "Concordances")
   call addGenre("Conversation and phrase books", "Phrase books")
   call addGenre("Correspondence", "Personal correspondence")
   call addGenre("Databases", "Databases")
   call addGenre("Diaries", "Diaries")
   call addGenre("Dictionaries", "Dictionaries")
   if instr(AllSubjects, "Picture dictionaries") then CS.AddField 999, "655 7Picture dictionaries"  
   call addGenre("Directories", "Directories")
   call addGenre("Discography", "Discographies")
   call addGenre("Encyclopedias", "Encyclopedias")
   call addGenre("Exhibitions", "Exhibition catalogs")
   call addGenre("Examinations", "Examinations")
   call addGenre("Gazetteers", "Gazetteers")
   call addGenre("Guidebooks", "Guidebooks")
   call addGenre("Handbooks, manuals", "Handbooks and manuals")
   call addGenre("Humor", "Humor")
   call addGenre("Indexes", "Indexes")
   call addGenre("Interviews", "Interviews")
   call addGenre("Life skills guides", "Handbooks and manuals")
   call addGenre("Librettos", "Librettos")
   call addGenre("Messages", "Speeches")
   call addGenre("Methods", "Methods (Music)")
   call addGenre("Miscellanea", "Trivia and miscellanea")
   call addGenre("Motion picture plays", "Screenplays")
   call addGenre("Outlines, syllabi, etc.", "Outlines and syllabi")   
   call addGenre("Personal narratives", "Personal narratives")   
   call addGenre("Pictorial works", "Illustrated works")
   call addGenre("Problems, exercises", "Problems and exercises")
   call addGenre("Programmed instruction", "Programmed instructional materials")   
   call addGenre("Quotations", "Quotations")   
   call addGenre("Readers", "Readers (Publications)")   
   call addGenre("Recipes", "Cookbooks")   
   call addGenre("Reverse indexes", "Reverse dictionaries")   
   call addGenre("Self-instruction", "Programmed instructional materials")
   call addGenre("Slave narratives", "Slave narratives")   
   call addGenre("Statistics", "Statistics")   
   call addGenre("Study guides", "Study guides")   
   call addGenre("Stories, plots, etc.", "Plot summaries")   
   call addGenre("Style manuals", "Style manuals")   
   call addGenre("Tables", "Tables (Data)")   
   call addGenre("Telephone directories", "Telephone directories")   
   call addGenre("Terminology", "Dictionaries")   
   call addGenre("Textbooks", "Textbooks")   
   call addGenre("Toy and movable books", "Toy and movable books") 

   'travel writing
   if instr(AllSubjects$, "x Travel") then CS.AddField 99, "655 7Travel writing"

   'true crime stories
   CS.GetField "082", 1, dewey$
   if dewey$ = "" then CS.GetField "092", 1, dewey$
   if mid$(dewey$, 6, 5) = "364.1" then CS.Addfield 99, "655 7True crime stories"
   
   'yearbooks
   if instr(AllSubjects$, "Students " & chr(223) & "v Yearbooks") then
      if instr(AllGenres$, "yearbooks") = 0 then CS.Addfield 99, "655 7School yearbooks"
   end if

   call addGenre("Yellow pages", "Yellow pages")   

   '------------------
   'Juvenile forms
   if instr(AllSubjects, "Adventure and adventurers") then CS.AddField 999, "655 7Action and adventure fiction"
   
   if instr(AllSubjects$, "Pop-up books " & chr(223) & "v Specimens") then
      if instr(AllGenres$, "Pop-up books") = 0 then CS.Addfield 99, "655 7Pop-up books"
   end if
   if instr(AllSubjects, "Stories in rhyme") then CS.AddField 999, "655 7Stories in rhyme"
   
   if instr(AllSubjects$, "Toy and movable books " & chr(223) & "v Specimens") then
      if instr(AllGenres$, "Toy and movable books") = 0 then CS.Addfield 99, "655 7Toy and movable books"
   end if
   '------------------
   'religion
   call addGenre("Apologetic works", "Apologetic writings")   
   if instr(AllSubjects, "Bible stories") then CS.AddField 999, "655 7Bible stories"
   call addGenre("Catechisms", "Catechisms")   
   call addGenre("Creeds", "Creeds")   
   if instr(AllSubjects, "Devotional calendars") then CS.AddField 999, "655 7Devotional literature"
   call addGenre("Devotional literature", "Devotional literature")   
   call addGenre("Harmonies", "Harmonies")   
   call addGenre("Meditations", "Meditations")   
   call addGenre("Papal documents", "Papal documents")   
   call addGenre("Parallel version", "Sacred books")   
   call addGenre("Paraphrases", "Sacred books")   
   call addGenre("Pastoral letters and charges", "Pastoral letters and charges")   
   call addGenre("Picture Bibles", "Sacred works")         
   call addGenre("Picture Bibles", "Illustrated works")         
   call addGenre("Prayers", "Prayers")         
   call addGenre("Sermons", "Sermons") 
   
   'sacred works     
   CS.GetField "130", 1, tag130$
   tag130$ = mid(tag130$, 6)
   if left(tag130$, 6) = "Bible." or left(tag130$, 7) = "Qurå®¶n." then  
      if instr(AllGenres$, "Sacred works") = 0 then CS.AddField 99, "655 7 Sacred works"
   end if
   call addGenre("Sacred books", "Sacred works") 
   '------------------
   
   r = CS.CursorRow
   CS.RunMacro "DPK_RDA!385"
   CS.RunMacro "DPK_RDA!386"
   CS.RunMacro "DPK_RDA!fictionGenres"
   CS.CursorRow = r
End Sub