$(document).ready(function(){
  // Dictionary.
  var dictionaryEn = {
    txtErrorPageGoals : "Please select at least one learning goal.",
    txtErrorPriorities : "Please select at least one learning priority.",
    txtErrorSpecify : "Please let us know how you heard about MSK English"
  };
  var dictionaryEs = {
    txtErrorPageGoals : "Por favor elige al menos un objetivo para tus clases.",
    txtErrorPriorities : "Por favor elegi al menos una prioridad para tus clases.",
    txtErrorSpecify : "Por favor dinos como nos has conocido."
  };
  var dictionaryFr = {
    txtErrorPageGoals : "S'il vous pla\u00EEt s\u00E9lectionner au moins un obectif pour votre apprentissage.",
    txtErrorPriorities : "S'il vous pla\u00EEt s\u00E9lectionner au moins un apprentissage prioritaire.",
    txtErrorSpecify : "Comment avez-vous connu MSK English."
  };
  var dictionaryDe = {
    txtErrorPageGoals : "Bitte w\u00E4hlen Sie mindestens ein Lernziel aus.",
    txtErrorPriorities : "Bitte w\u00E4hlen Sie mindestens eine lern Priorit\u00E4t aus.",
    txtErrorSpecify : "Bitte lassen Sie uns wissen wie Sie von MSK English geh\u00F6rt haben."
  };
  var dictionaryPl = {
    txtErrorPageGoals : "Prosz\u0119 wybra\u0107 przynajmniej jeden cel nauki.",
    txtErrorPriorities : "Prosz\u0119 wybra\u0107 przynajmniej jeden z priorytet\u00F3w nauki.",
    txtErrorSpecify : "Prosz\u0119 zakre\u015Bli\u0107 przynajmniej jeden ze sposob\u00F3w jak sie o nas dowiedzia\u0142e\u015B/-a\u015B."
  };


  /* Find out page language */
  var pageLang = $("html").attr("lang");
  /* Set dictionary based on page language */
  if (pageLang == "en") {
    dictionaryLang = dictionaryEn;
  } else if (pageLang == "de") {
    dictionaryLang = dictionaryDe;
  } else if (pageLang == "fr") {
    dictionaryLang = dictionaryFr;
  } else if (pageLang == "pl") {
    dictionaryLang = dictionaryPl;
  } else if (pageLang == "es") {
    dictionaryLang = dictionaryEs;
  } else {
    // Unsupported dictionary lang. Show English.
    dictionaryLang = dictionaryEn;
  }

  /* Hide title form field for languages that don't require it */
  if (pageLang == "es") {
    $('tr.title-field').hide();
  }

  // Check whether 'Specify' field contains anything upon submission.
  var isEmpty = $('#specify').attr("value");
  if (isEmpty == "") {
    // Do nothing.
  }

  else {
    $('tr.specifyhowyouheard').show();
  }

  // Check/Uncheck all checkboxes buttons.
  $("#goal_all").click(function() {
      if ($(this).attr("checked")) {
        // Check all boxes.
         $("input.goals").attr("checked", "checked");
      }

      else {
        // Uncheck all boxes.
        $("input.goals").removeAttr("checked");
      }
  });

  // Display specify box only upon certain "how did you hear" selections.
  $("#howdidyouhear").change(function() {
    if ($(this).children("[@selected]")) {
      var valueOfThis = $(this).attr("value");
      if (valueOfThis == "Media" || valueOfThis == "Reseller" || valueOfThis == "Other") {
        $("tr.specifyhowyouheard").fadeIn();
      }

      else {
        valueOfThis = "";
        $("tr.specifyhowyouheard").fadeOut();
      }
    }
  });

  // Validate checkboxes on form prior to submitting.
  $("form").submit(function() {
    // Create counter variable.
    var e = 0;

    var goalsChecked = $("input[@class=goals]:checked").length;
    if (goalsChecked == 0) {
      errorGoal = dictionaryLang.txtErrorPageGoals;
      e ++;
      $("h4.goal_error").css({"color" : "rgb(255,0,0)",
                              "font-weight" : "bold"});
      alert( errorGoal );
      return false;
    }

    else {
      $("h4.goal_error").removeAttr("style");
    }

    var prioritiesChecked = $("input[@class=priorities]:checked").length;
    if (prioritiesChecked == 0) {
      e ++;
      errorPriorities = dictionaryLang.txtErrorPriorities;

      $("h4.priority_error").css({"color" : "rgb(255,0,0)",
                                  "font-weight" : "bold"});
      alert( errorPriorities );
      return false;
    }

    else {
      $("h4.priority_error").removeAttr("style");
    }

    var isVisible = $('tr.specifyhowyouheard').is(':visible');
    var isHidden = $('tr.specifyhowyouheard').is(':hidden');

    if (isVisible) {
      var specifyValue = $("#specify").val();
      if (specifyValue == "") {
        errorSpecify = dictionaryLang.txtErrorSpecify;

        $("h4.specify_error").css({"color" : "rgb(255,0,0)",
                                    "font-weight" : "bold"});

        alert(errorSpecify);
        return false;
      }

      else {
        $("h4.specify_error").removeAttr("style");
      }
    }

    if (e == 0) {
      return true
    }

    else {
      return false;
    }
  });
});