 # What Costs function
 #
 window.whatCosts = (careCostsForThisWorkY, careCostsForThisWorkN, whatCosts) ->
+  if not checked(careCostsForThisWorkY)
+    S("whatCostsWrap").slideUp 0, -> val(whatCosts,"")
+
   S(careCostsForThisWorkY).on "click", -> S("whatCostsWrap").slideDown 0
 
   S(careCostsForThisWorkN).on "click", -> S("whatCostsWrap").slideUp 0, -> val(whatCosts,"")

    when "monthly" then o.usuallyPaidSameAmountMonthlyTextExpected
    else o.usuallyPaidSameAmountOtherTextExpected

  legend(usuallyPaidSameAmount).html(textToUse)
  checked(o.usuallyPaidSameAmountY, false)
  checked(o.usuallyPaidSameAmountN, false)

onBeenPaidYetY = (ctx) -> #Double -> because it's a function returning a function, which the jQuqery .on function will receive.
  ->                      #The first function exists so the returned function has the "context" variable in scope
    S("beenPaidYetWrap").slideUp 0, -> hideBeenPaidYetWrapOnY(ctx)
    S("usuallyPaidSameAmountWrap").slideUp 0
    S("monthlyPayDayWrap").slideUp 0 if visible ctx.monthlyPayDayWrap
    S("beenPaidYetWrap").slideDown 0
    S("howOften_wrap").slideUp(0, -> val ctx.howOftenFrequencyOther , "") if visible ctx.howOften_wrap

onBeenPaidYetN = (ctx) -> #Double -> because it's a function returning a function, which the jQuqery .on function will receive.
  ->                      #The first function exists so the returned function has the "context" variable in scope
    S("beenPaidYetWrap").slideUp 0, -> hideBeenPaidYetWrapOnN(ctx)
    S("usuallyPaidSameAmountWrap").slideUp 0
    S("monthlyPayDayWrap").slideUp 0 if visible ctx.monthlyPayDayWrap
    S("beenPaidYetWrap").slideDown 0
    S("howOften_wrap").slideUp(0, -> val ctx.howOftenFrequencyOther, "") if visible ctx.howOften_wrap


#
#Been Paid Yet function
#

window.beenPaidYet = (ctx) ->
  S(ctx.beenPaidYetY).on "click", onBeenPaidYetY(ctx)
  S(ctx.beenPaidYetN).on "click", onBeenPaidYetN(ctx)



#
#Pay Day function
#

window.payDay = (o) ->
  S(o.howOftenFrequency).change ->
    if visible("usuallyPaidSameAmountWrap")
      S("usuallyPaidSameAmountWrap").slideUp 0, ->
        checked(o.usuallyPaidSameAmountY,false)
        checked(o.usuallyPaidSameAmountN,false)

    S("monthlyPayDayWrap").slideUp 0, -> val(o.monthlyPayDay,"") if visible("monthlyPayDayWrap")

    if not (val(o.howOftenFrequency) is o.defaultValue or val(o.howOftenFrequency) is "")
      if checked(o.beenPaidYetN)
        textToUse = switch (val(o.howOftenFrequency))
          when "weekly" then o.usuallyPaidSameAmountWeeklyTextExpected
          when "fortnightly" then o.usuallyPaidSameAmountFortnightlyTextExpected
          when "fourweekly" then o.usuallyPaidSameAmountFourWeeklyTextExpected
          when "monthly" then o.usuallyPaidSameAmountMonthlyTextExpected
          else o.usuallyPaidSameAmountOtherTextExpected
        legend(o.usuallyPaidSameAmount).html(textToUse)
          
      if checked(o.beenPaidYetY)
        textToUse = switch (val(o.howOftenFrequency))
          when "weekly" then o.usuallyPaidSameAmountWeeklyText
          when "fortnightly" then o.usuallyPaidSameAmountFortnightlyText
          when "fourweekly" then o.usuallyPaidSameAmountFourWeeklyText
          when "monthly" then o.usuallyPaidSameAmountMonthlyText
          else o.usuallyPaidSameAmountOtherText
        legend(o.usuallyPaidSameAmount).html(textToUse)

      checked(o.usuallyPaidSameAmountY, false)
      checked(o.usuallyPaidSameAmountN, false)
      S("usuallyPaidSameAmountWrap").slideDown(0)


    S("monthlyPayDayWrap").slideDown(0) if val(o.howOftenFrequency) is "monthly"

#
# What For function
#

window.whatFor = (payIntoPensionY, payIntoPensionN, whatFor) ->
  S(payIntoPensionY).on "click", -> S("whatForWrap").slideDown 0

  S(payIntoPensionN).on "click", -> S("whatForWrap").slideUp 0, -> val(whatFor,"")




hideBeenPaidYetWrapOnY = (o,reset) ->
  label(o.howMuchPaid).text(o.howMuchPaidYText)
  val(o.howMuchPaid,"") if reset
  legend(o.whatDatePaid).html(o.whatDatePaidYText)
  val(o.whatDatePaidDay,"") if reset
  val(o.whatDatePaidMonth,"") if reset
  val(o.whatDatePaidYear,"") if reset
  label(o.howOften).text(o.howOftenYText)
  val(o.howOften,"") if reset
  label(o.monthlyPayDay).text(o.monthlyPayDayYText)
  val(o.monthlyPayDay,"") if reset
  textToUse = switch (val(o.howOften))
    when "weekly" then o.usuallyPaidSameAmountWeeklyText
    when "fortnightly" then o.usuallyPaidSameAmountFortnightlyText
