// walidacja dodawanego artykulu
var article = {
  validateTitle: function() {
    if ($F("f_title").length < 5 || $F("f_title").length > 90) {
      helper.note("Tytuł artykułu musi składać się z od 5 do 90 znaków. Twój tytuł posiada " + $F("f_title").length + " znaków.");
    }
  },
  validateDesc: function() {
    if ($F("f_desc").length < 50) {
      helper.note("Opis artykułu musi składać się z przynajmniej 50 znaków. Twój opis posiada " + $F("f_desc").length + " znaków.");
    }
  },
  validateKeys: function() {
    if ($F("f_keywords") == "") {
      helper.note("Podaj kilka słów kluczowych oddzielonych spacją, które opisują artykuł");
    }
  },
  subcategory: function() {
    new Ajax.Updater('f_subcategory', URL + '/admin/article/subcategory/', {
      method: "post",
      postBody: "id=" + $F("f_category")
    });
  },
  submit: function() {
    var status  = true;
    
    if ($F("f_title").length < 5 || $F("f_title").length > 90) {
      helper.bad("f_title");
      status = false;
    }
    if ($F("f_desc").length < 50) {
      helper.bad("f_desc");
      status = false;
    }
    if ($F("f_keywords") == "") {
      helper.bad("f_keywords");
      status = false;
    }
    
    if (status == true) {
      var content = $F("f_content");
      
      while (content.indexOf('&nbsp;') != -1) {
        content = content.replace('&nbsp;', '');
      }
      while (content.indexOf('  ') != -1) {
        content = content.replace('  ', ' ');
      }
      
      if ($F("f_co") == 'a') {
        new Ajax.Request(URL + '/aktualnosci/news/zapisz/', {
          method: "post",
          postBody: "title=" + $F("f_title") + "&desc=" + $F("f_desc")
          + "&category=" + $F("f_category") + "&subcategory="
          + $F("f_subcategory") + "&keywords=" + $F("f_keywords") 
          + "&content=" + content,
          onInteractive: helper.create,
          onComplete: article.response,
          onFailure: helper.failure
        });
      }
      if ($F("f_co") == 'w') {
        new Ajax.Request(URL + '/wydarzenia/news/zapisz', {
          method: "post",
          postBody: "title=" + $F("f_title") + "&desc=" + $F("f_desc")
          + "&category=" + $F("f_category") + "&subcategory="
          + $F("f_subcategory") + "&keywords=" + $F("f_keywords") + "&start="
          + $F("f_start_publishing") + "&finish=" + $F("f_finish_publishing")
          + "&id=" + $F("f_id") + "&content=" + content,
          onInteractive: helper.create,
          onComplete: article.response,
          onFailure: helper.failure
        });
      }  
    }
  },
  response: function(request) {
    helper.complete;
    if ($F("f_co") == 'a') {
      window.location = URL+"/aktualnosci/news/kom";
    }
    if ($F("f_co") == 'w') {
      window.location = URL+"/wydarzenia/news/kom";
    }
  }
}
// pomocnik dla walidatorow
var helper = {
  border: function(element, color) {
    $(element).style.borderColor = color;
  },
  note: function(message) {
    Element.show('note');
    $('message').innerHTML = message;
    setTimeout('Element.hide("note")', 7000);
  },
  bad: function(element) {
    helper.border(element, "#f00");
    helper.note("Musisz poprawić pola zaznaczone na czerwono.")
    setTimeout('helper.border("' + element + '", "#dadada")', 7000);
  },
  create: function() {
    Element.show('status');
    $('status').style.color       = "#f60";
    $('status').style.borderColor = "#f60";
    $('status').innerHTML         = "Proszę czekać. Trwa aktualizacja danych.";
  },
  complete: function() {
    Element.show('status');
    $('status').style.color       = "#690";
    $('status').style.borderColor = "#690";
    $('status').innerHTML         = "Gotowe. Dane zostały zaktualizowane.";
    setTimeout('Element.hide("status")', 2000);
  },
  failure: function() {
    Element.show('status');
    $('status').style.color       = "#f00";
    $('status').style.borderColor = "#f00";
    $('status').innerHTML         = "Wystąpił problem z aktualizacją danych.";
    setTimeout('Element.hide("status")', 3000);
  }
}
