// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var hash = "";
var section_id = 1;

var size = 0;

function check_all_with_classname(classname) {
  process_all_with_classname(classname, true);
}

function uncheck_all_with_classname(classname) {
  process_all_with_classname(classname, false);
}

function process_all_with_classname(classname, checked) {
  elements = document.getElementsByClassName(classname);
  for (var i=0; i<elements.length; i++) {
    elements[i].checked = checked;
  }  
}

function show_indicator() {
  Element.show('indicator');
}

function hide_indicator() {
  Element.hide('indicator');
}

	function swap_img(id,ac) {
		var ha = (ac == 0) ? '':'h';
		document.getElementById('link_img_'+id).src='/images/layout/top_link_'+id+ha+'.gif';
	}

  function get_folder_name() {
    return "new_folder=" + window.prompt('Podaj nazwę dla nowego folderu');
  }

  function change_active(id) {
    actives = $('menu').getElementsByClassName('active')
    actives.each(function(item) {
      Element.removeClassName(item, 'active');
    });
    if(id != "") {
      Element.addClassName(id, 'active');
    }
  }

  function remove_flash() {
    if($('mainFlash')) {
      $('mainFlash').hide();
    }
    return true;
  }

  // TODO remove_actives
  
  
function clearOnInitialFocus ( fieldName ) {
  var clearedOnce = false;
  document.getElementById( fieldName ).onfocus = (function () {
    if (clearedOnce == false) {
      this.value = '';
      clearedOnce = true;
    }
  })
}

function submit_on_key_press(fieldName) {
  document.getElementById(fieldName).setAttribute('onsubmit', $('submit_form').getAttribute('onclick'));
}

function check_form() {
  if($('contact_mail').value == '') {
    alert('Należy wypełnić wszystkie pola.');
    return false; 
  }
  return true;
}

function initPage() {
  if($('mail')) clearOnInitialFocus('mail');
}

Event.observe(window, 'load', initPage, false);


//HRT

function addAddress() {
  $j.ajax({
      type: "POST",
      url: "/page/add_address",
      dataType: 'html',
      data: "email=" + $j("#newsletterAddress").val(),
      beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/html");},
      success: function(data) {
          $j("#newsletterInfo").empty()
          $j("#newsletterInfo").append(data);
          }
    });

}

function removeAddress() {
  $j.ajax({
      type: "POST",
      url: "/page/remove_address",
      dataType: 'html',
      data: "email=" + $j("#newsletterAddress").val(),
      beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/html");},
      success: function(data) {
          $j("#newsletterInfo").empty()
          $j("#newsletterInfo").append(data);
          }
    });

}

function sendNewsletter() {
  $j.ajax({
      type: "POST",
      url: "/admin/newsletters/send_newsletter",
      dataType: 'html',
      data: "article=" + $j("#article_form_id").val(),
      beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/html");},
      success: function(data) {
          }
    });
    alert("Newsletter został wysłany");
}

function sendSite() {
  $j("#friendForm").toggle();
}

function resetFriendForm()  {
  $j("#friendAddress").val = ""
}


function chkizba(){
  e = document.getElementsByClassName ('izbachk');
  for (i =0; i < e.length; i++)
    e[i].checked = true;
}

function chknotizba(){
  e = document.getElementsByClassName ('notizbachk');
  for (i =0; i < e.length; i++)
    e[i].checked = true;
}

function unchkall(){
  e = document.getElementsByClassName ('notizbachk');
  for (i =0; i < e.length; i++)
    e[i].checked = false;
  e = document.getElementsByClassName ('izbachk');
  for (i =0; i < e.length; i++)
    e[i].checked = false;
}


function wymagane(pl){
  inp=document.getElementsByTagName("input");
  for (i=0; i < inp.length; i++){
    if (inp[i].className == "needed" && inp[i].value.length == 0){
      alert(pl ? "Wypełnij wszystkie wymagane pola!" : "Fill up all needed fields!");
      return false;
    }
    if (inp[i].className == "needed_check" && !inp[i].checked){
      alert(pl ? "Wypełnij wszystkie wymagane pola!" : "Fill up all needed fields!");
      return false;
    }
  }
  inp=document.getElementsByTagName("textarea");
  for (i=0; i < inp.length; i++){
    if (inp[i].className == "needed" && inp[i].value.length == 0){
      alert(pl ? "Wypełnij wszystkie wymagane pola!" : "Fill up all needed fields!");
      return false;
    }
  }
  inp=document.getElementsByTagName("fieldset");
  for (i=0; i < inp.length; i++){
    rb=inp[i].getElementsByTagName("input");
    val=false;
    rbc=0;
    for (j=0; j< rb.length; j++){
      if (rb[j].className == "needed_rad"){
        if(rb[j].checked)
          val=true;
        rbc++;
      }
    }
    if (!val && rbc>0){
      alert(pl ? "Wypełnij wszystkie wymagane pola!" : "Fill up all needed fields!");
      return false;
    }
  }
  return true;
}

function search_location (urlsite, urlnet){
  form = document.getElementById ('searchform');
  opt2 = document.getElementById ('opt2');
  if (opt2.checked){
    form.action = urlnet;
    form.target = "_blank";
  }  else {
    form.action = urlsite;
    form.target = "_self";
  }
}

function check_newsletter (lang){
  n = document.getElementById ("newsletter_name").value;
  o = document.getElementById ("newsletter_organization").value;
  e = document.getElementById ("newsletter_email").value;
  if (n.indexOf ("wpisz") >= 0 || n.indexOf ("Write") >= 0 || o.indexOf ("wpisz") >= 0 || o.indexOf ("Write") >= 0 || e.indexOf ("wpisz") >= 0 || e.indexOf ("Write") >= 0 || e.indexOf ("@") < 0 || o.length == 0 || n.length == 0 || e.length < 3){
    if (lang == "pl")
      alert ("Wypełnij poprawnie wszystkie pola!");
    else
      alert ("Fill up correctly all fields!");
    return false;
  }
  return true;

}

function printse (){
  if (document.getSelection){
    if (document.getSelection().length == 0){
      alert ("Nic nie zaznaczono!");
      return false;
    }
    w=window.open("about:blank");
    w.document.write(document.getSelection());
    if (window.opera)
      setTimeout ("w.print(); w.close()", 2000);
    else{
      w.print ();
      w.close ();
      }
  }
  if (document.selection){
    if (document.selection.createRange().text.length == 0){
      alert ("Nic nie zaznaczono!");
      return false;
    }
    w = window.open ("about:blank");
    w.document.write(document.selection.createRange().text);
    w.print ();
    w.close ();
  }
}

function fontsize (delta){
  if (size+delta > 2 || size + delta < 0)
    return false;
  art = document.getElementById ("article");
  fonts = art.getElementsByTagName ("FONT");
  for (i = 0; i < fonts.length; i++){
    if (fonts[i].size)
      fonts[i].size = (Number(fonts[i].size)+delta);
  }
  size += delta;
  art.style.fontSize = (12+2*size)+"px";
  return false;
}
