// nežinau kol kas browserio, kuris palaikytų array.pop ;)
if (typeof(Array.prototype.pop) == "undefined") {
  Array.prototype.pop = Array_pop
}

function Array_pop() {
  var response = this[this.length - 1];
  this.length--;
  return response;
}

// --------------------------------------------------------------------------------------- //

function FormCheck(form) {

  var _t = this;

  this.checkFields = checkFields;

  this.form   = form;
  this.errors = [];

  this.form.submit(
    function() { return _t.checkFields(); }
  );

  $("p.submit", this.form).before('<div class="error_block"></div>');
  $(".error_block", this.form).hide();


  function checkFields() {

    if (this.errors.length) {
      this.errors = [];
    }

    $(".error_block", this.form).hide().html("");

    // kiekvienas TR elementas, turintis klasę "req"
    $(".req", this.form).each(

      function() {

        if ($(this).parent().parent().parent().is(":visible")) {


          // panaikinam TR klasę "error", kad nesikartotų
          $(this).removeClass("error");

          // pagal paskutinę TR klasę žiūrim koks tikrinimo tipas
          switch ($(this)[0].className.split(" ").pop()) {

            // laukelis turi būti užpildytas (taikoma tekstiniams laukams)
            case "notempty":
              // paprastas tekstinis INPUT
              if ($("input", $(this)).size() > 0) {
                if ($("input", $(this))[0].value == "") {
                  $(this).addClass("error");
                  _t.errors[_t.errors.length] = "<strong>" + Translations.error_1 + ":</strong> " + $("th strong", $(this)).text();
                }
              }
              // TEXTAREA
              else if ($("textarea", $(this)).size() > 0) {
                if ($("textarea", $(this)).val() == "") {
                  $(this).addClass("error");
                  _t.errors[_t.errors.length] = "<strong>" + Translations.error_2 + ":</strong> " + $("th strong", $(this)).text();
                }
              }
              break;

            case "file":
              if ($("input", $(this))[0].value == "") {
                $(this).addClass("error");
                _t.errors[_t.errors.length] = "<strong>" + Translations.error_5 + ":</strong> " + $("th strong", $(this)).text();
              }
              break;

            // laukelis turi atitikti email adreso patterna (taikoma tekstiniams laukams)
            case "email":
              if ($("input", $(this))[0].value == "") {
                $(this).addClass("error");
                _t.errors[_t.errors.length] = "<strong>" + Translations.error_1 + ":</strong> " + $("th strong", $(this)).text();
              }
              else if (!/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/.test($("input", $(this))[0].value)) {
                $(this).addClass("error");
                _t.errors[_t.errors.length] = "<strong>" + Translations.error_3 + ":</strong> " + $("th strong", $(this)).text();
              }
              break;

            // turi būti pasirinktas nors vienas variantas iš keleto radio arba checkbox
            case "selected":
              if ($("input:checked", $(this)).size() < 1) {
                $(this).addClass("error");
                _t.errors[_t.errors.length] = "<strong>" + Translations.error_4 + "</strong> " + $("th strong", $(this)).text();
              }
              break;

            case "selectbox":
              break;

          }

        }

      }

    );

    if (this.errors.length) {

      // ...imam kiekvieną ir metam į klaidų bloką
      for (var i = 0; i < this.errors.length; i++) {
        $(".error_block", this.form).append('<p>' + this.errors[i] + '</p>').focus();
      }

      $(".error_block", this.form).show();

      // anketa nepraėjo
      return false;
    }

    if ($("#agree").size() > 0 && $("#agree")[0].checked === false) {
      alert("Prašome perskaityti sąlygas");
      return false;
    }

    $(this.form).find("div.member:hidden").remove();

  }

}