    $(document).ready(function() {
    
    $('#fsearch').focus(function() {
        if ($(this).val().substring(0,15) == 'szukany tekst') {
            $(this).val('');
        }
        $(this).addClass('active');
    });
    
    $('#fsearch').blur(function() {
        if($(this).val() == 'szukany tekst' || $(this).val() == '') {
            $(this).val('szukany tekst');
        }
        $(this).removeClass('active');        
    });

    $('#search_cat_select').change(function() {

        if ($(this).val()==0)
        {
            $('#search_c').attr('disabled', true);
            $('#search_k').attr('disabled', true);
        }
        else
        {
            $('#search_c').attr('disabled', false);
            $('#search_k').attr('disabled', false);
        }
    });

    $('#search_submit').click(function() {
        $(this).parent().submit();
    });
    
    $('#message-type').change(function() {
        $('#message-list-form').submit();
    });

    $("[id|=sc]").click(function() {
        if ($(this).is(":checked")) {
            $("#clear-select-ads").show();
        }
    });

    $("#clear-select-ads").click(function() {
        $("[id|=sc]").attr("checked", false);
        $(this).hide();
    });




    $('#adv_search').submit(function() {
        var ss = $("input[name='ss']", this);
        if (ss.val()=='') ss.attr('disabled',true);

        var author = $("input[name='author']", this);
        if (author.val()=='') author.attr('disabled',true);

        var days = $("select[name='days']", this);
        if (days.val()==0) days.attr('disabled',true);

        var sort = $("select[name='sort']", this);
        if (sort.val()==0) sort.attr('disabled',true);

        parse_select($("#select_forum_tree"));
    });

});

function parse_select(obj)
{
    if ($(obj).val()==0)
    {
        $('#search_c_adv').attr('disabled', true);
        $('#search_k_adv').attr('disabled', true);
    }
    else
    {
        $('#search_c_adv').attr('disabled', false);
        $('#search_k_adv').attr('disabled', false);
        var ck = $(obj).val().split("_");
        $('#search_c_adv').val(ck[0]);
        $('#search_k_adv').val(ck[1]);
    }
}


function validate_login()
{
  login = document.user_form.login.value;

  if (login.length >32 || login.length<5)
  {
      warn_msg = 'niepoprawny';
      document.getElementById('login-img').src = '/_img/nforum/not_valid.gif';
      document.getElementById('login-img').style.display = 'inline';
      document.getElementById('login-warn').innerHTML =  warn_msg;
      form_ready = 0;
      return 0;  
  }

  if (login.toLowerCase().indexOf('admin')!=-1)
  {
      warn_msg = 'niedozwolona fraza admin';
      document.getElementById('login-img').src = '/_img/nforum/not_valid.gif';
      document.getElementById('login-img').style.display = 'inline';
      document.getElementById('login-warn').innerHTML =  warn_msg;
      form_ready = 0;
      return 0;
  }

  advAJAX.get({
    url:"/_ajax/logged_user/uniq.php?name=login&value="+login,
    onSuccess:function(obj) {
       wynik = obj.responseText;
       if (wynik==1)
       {
           document.getElementById('login-img').src = '/_img/nforum/valid.gif';
           document.getElementById('login-img').style.display = 'inline';
           document.getElementById('login-warn').innerHTML='';
       }
       else
       {
           document.getElementById('login-img').src = '/_img/nforum/not_valid.gif';
           document.getElementById('login-img').style.display = 'inline';
           document.getElementById('login-warn').innerHTML='taki login jest już zajęty';
           form_ready = 0;
       }
    },
    onError:function(obj) {alert(obj.status);}
  });
}

function validate_name()
{
  name = document.user_form.name.value;
  if (name.length == 0)
  {
      document.getElementById('name-img').src = '/_img/nforum/not_valid.gif';
      document.getElementById('name-img').style.display = 'inline';
      document.getElementById('name-warn').innerHTML='pole puste';
      form_ready = 0;
  }
  else
  {
      document.getElementById('name-img').src = '/_img/nforum/valid.gif';
      document.getElementById('name-img').style.display = 'inline';
      document.getElementById('name-warn').innerHTML='';
  }
}

function validate_pass()
{
    pass = document.user_form.pass.value;
    if (pass.length>=6)
    {
        document.getElementById('pass-img').src = '/_img/nforum/valid.gif';
        document.getElementById('pass-img').style.display = 'inline';
        document.getElementById('pass-warn').innerHTML='';
    }
    else
    {
        document.getElementById('pass-img').src = '/_img/nforum/not_valid.gif';
        document.getElementById('pass-img').style.display = 'inline';
        document.getElementById('pass-warn').innerHTML='za krótkie hasło';
        form_ready = 0;
    }
}

function validate_pass2()
{
    pass = document.user_form.pass.value;
    pass2 = document.user_form.pass2.value;
    if (pass==pass2 && pass2.length>=6)
    {
        document.getElementById('pass2-img').src = '/_img/nforum/valid.gif';
        document.getElementById('pass2-img').style.display = 'inline';
        document.getElementById('pass2-warn').innerHTML='';
    }
    else
    {
        document.getElementById('pass2-img').src = '/_img/nforum/not_valid.gif';
        document.getElementById('pass2-img').style.display = 'inline';
        document.getElementById('pass2-warn').innerHTML='hasła nie s± identyczne';
        form_ready = 0;
    }
}

function validate_email()
{
    email = document.user_form.email.value;
    apos=email.indexOf("@");
    dotpos=email.lastIndexOf(".");
    if (apos<1||dotpos-apos<2) 
    {
        document.getElementById('email-img').src = '/_img/nforum/not_valid.gif';
        document.getElementById('email-img').style.display = 'inline';
        document.getElementById('email-warn').innerHTML='niepoprawny';
        form_ready = 0;
    }
    else
    {
          advAJAX.get({
            url:"/_ajax/logged_user/uniq.php?name=email&value="+email,
            onSuccess:function(obj) {
               wynik = obj.responseText;
               if (wynik==1)
               {
                    document.getElementById('email-img').src = '/_img/nforum/valid.gif';
                    document.getElementById('email-img').style.display = 'inline';
                    document.getElementById('email-warn').innerHTML='';
               }
               else
               {
                   document.getElementById('email-img').src = '/_img/nforum/not_valid.gif';
                   document.getElementById('email-img').style.display = 'inline';
                   document.getElementById('email-warn').innerHTML='taki email jest już używany';
                   form_ready = 0;
               }
            },
            onError:function(obj) {alert(obj.status);}
          });
    }
}

function validate_rules()
{
    rules = document.user_form.rules;
    if (rules.checked)
    {
        document.getElementById('rules-img').src = '/_img/nforum/valid.gif';
        document.getElementById('rules-img').style.display = 'inline';
        document.getElementById('rules-warn').innerHTML='';
    }
    else
    {
        form_ready = 0;
        document.getElementById('rules-img').src = '/_img/nforum/not_valid.gif';
        document.getElementById('rules-img').style.display = 'inline';
        document.getElementById('rules-warn').innerHTML='musisz zaakceptować regulamin';
    }
}

function validate_all()
{
    form_ready = 1;

    validate_login();
    if (document.getElementById('login-warn').innerHTML)
    {
        form_ready = 0; 
    }
    validate_pass();
    validate_pass2();
    validate_email();
    validate_rules();
    if (document.getElementById('email-warn').innerHTML)
    {
        form_ready = 0; 
    }


    return form_ready ? true : false;
}

function validate_ed()
{
    pass = document.user_form.pass.value;
    pass2 = document.user_form.pass.value;

    form_ready = 1;
    if (pass.length>0 && pass!="" && pass!=null)
    {
        validate_pass();
        validate_pass2();
    }

    return form_ready ? true : false;
}

function validate_post_title()
{
    title = document.new_post.title.value;

    allowed = 'qwertyuiopasdfghjklzxcvbnm';
    allowed += 'QWERTYUIOPASDFGHJKLZXCVBNM';
    allowed += '±ęćóżĽł¶';
    allowed += 'AĘĆÓŻ¬Ł¦';

    //czy 1 znak ok?
    var first_char_ok = 0;

    for (var i=0; i<allowed.length; i++)
    {
        if (title.charAt(0) == allowed.charAt(i))
        {
            first_char_ok = 1;
            break;
        }
    }

    if (!first_char_ok)
    {
        form_ready = 0;
        document.getElementById('title-img').src = '/_img/nforum/not_valid.gif';
        document.getElementById('title-img').style.display = 'block';
        document.getElementById('title-warn').innerHTML='niepoprawny tytuł';
    }
    else if (title.length < 5)
    {
        form_ready = 0;
        document.getElementById('title-img').src = '/_img/nforum/not_valid.gif';
        document.getElementById('title-img').style.display = 'block';
        document.getElementById('title-warn').innerHTML='za krótki tytuł';
    }
    else
    {
        document.getElementById('title-img').src = '/_img/nforum/valid.gif';
        document.getElementById('title-img').style.display = 'block';
        document.getElementById('title-warn').innerHTML='';
    }

}

function validate_post_content()
{
    content = document.new_post.content.value;
    if (content.length < 5)
    {
        form_ready = 0;
        document.getElementById('content-img').src = '/_img/nforum/not_valid.gif';
        document.getElementById('content-img').style.display = 'block';
        document.getElementById('content-warn').innerHTML='za krótka tre¶ć (min 5 znaków)';
    }
    else
    {
        document.getElementById('content-img').src = '/_img/nforum/valid.gif';
        document.getElementById('content-img').style.display = 'block';
        document.getElementById('content-warn').innerHTML='';
    }
}

function validate_post_author()
{
    author = document.new_post.author.value;
    if (author.length < 2 || author=="")
    {
        form_ready = 0;
        document.getElementById('author-img').src = '/_img/nforum/not_valid.gif';
        document.getElementById('author-img').style.display = 'block';
        document.getElementById('author-warn').innerHTML='pole autor wymagane (min 2 znaki)';
    }
    else
    {
        document.getElementById('author-img').src = '/_img/nforum/valid.gif';
        document.getElementById('author-img').style.display = 'block';
        document.getElementById('author-warn').innerHTML='';
    }
}

function validate_accept_opinion_rules()
{
    v = document.new_post.accept_opinion_rules;
    if (typeof(v)=='object')
    {
        if (!v.checked)
        {
            form_ready = 0;
            document.getElementById('accept_opinion_rules-img').src = '/_img/nforum/not_valid.gif';
            document.getElementById('accept_opinion_rules-img').style.display = 'inline';
            document.getElementById('accept_opinion_rules-warn').innerHTML='musisz zaakceptować regulamin opinii';
        }
        else
        {
            document.getElementById('accept_opinion_rules-img').src = '/_img/nforum/valid.gif';
            document.getElementById('accept_opinion_rules-img').style.display = 'inline';
            document.getElementById('accept_opinion_rules-warn').innerHTML='';
        }
    }
}

function validate_post()
{
    form_ready = 1;

    validate_post_title();
    validate_post_content();
    if (!document.new_post.author2)
    {
        validate_post_author();
    }

    validate_accept_opinion_rules();

    if (form_ready && !block_submit)
    {
        return true;
    }
    else
    {
        return false;
    }
}


function validate_title_and_content(title, content)
{
    if (content.length == 0 && title.length==0) 
    {
        form_ready = 0;
        info_text = 'musisz podać tytuł lub tre¶ć'; 
        return false;
    }
    else 
    {   
        info_text = '';
        return true;
    }
}


function validate_author_email(email)
{
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(email) == false && email.length!=0) 
    {
        form_ready = 0;
        info_text = 'niepoprawny adres email'; 
        return false;
    }
    return true;
}

function validate_post_tree(form)
{
    form_ready = 1;

    validate_title_and_content(form.title.value, form.content.value);
    validate_author_email(form.author_email.value);

    if (form_ready)
    {
        form.validation_message.style.visibility='hidden';
        return true;
    }
    else
    {
        form.validation_message.style.visibility='visible';
        form.validation_message.value = info_text;
        return false;
    }
}


function unhide_topic_content(tid)
{
    document.getElementById('c'+tid).setAttribute('class','content-on');
    document.getElementById('h'+tid).setAttribute('class','content-off');
    //IE
    document.getElementById('c'+tid).setAttribute('className','content-on');
    document.getElementById('h'+tid).setAttribute('className','content-off');
}

function hide_topic_content(tid)
{
    document.getElementById('c'+tid).setAttribute('class','content-off');
    document.getElementById('h'+tid).setAttribute('class','content-on');
    //IE
    document.getElementById('c'+tid).setAttribute('className','content-off');
    document.getElementById('h'+tid).setAttribute('className','content-on');
}

function validate_search_string(form)
{
    var string = form.ss.value;
    if (string.length < 2)
    {
        alert("Za krótki tekst, minimum 2 znaki.")
        form.ss.focus();
        return false;
    }
    else
    {
        return true;
    }
}

function quick_mod(mode)
{
   if (mode)
   {
        document.getElementById('quick-mod-topic').style.display='block';
        document.getElementById('quick-mod-topic').style.visibility='visible';
        document.getElementById('quick-mod-off').style.visibility='hidden';
        document.getElementById('quick-mod-off').style.display='none';
        divs_display = 'block';
        divs_visibility = 'visible';
	document.cookie = "quick_mod=1;expires=Sat, 01 Jan 2020 00:00:00 GMT";
        
        
   }
   else
   {
        document.getElementById('quick-mod-topic').style.display='none';
        document.getElementById('quick-mod-topic').style.visibility='hidden';
        document.getElementById('quick-mod-off').style.visibility='visible';
        document.getElementById('quick-mod-off').style.display='block';
        divs_display = 'none';
        divs_visibility = 'hidden';
	document.cookie = "quick_mod=0;expires=Sat, 01 Jan 2020 00:00:00 GMT";
   }

   divs = document.getElementsByTagName('div');
   n = divs.length;
   for (var i=0; i < n; i++)
   {
       if (divs[i].className == 'quick-mod-post')
       {
           divs[i].style.display = divs_display;
           divs[i].style.visibility = divs_visibility;
       }
   }
}

function search_places(text)
{
    if (text=='')
    {
        document.getElementById('all-places').style.visibility='visible';
        document.getElementById('places-match-search').style.visibility='hidden';
        document.getElementById('places-match-search').style.display='none';
    }
    if (text.length>2)
    {
        advAJAX.get({
            url:"search_places.php?text="+text,
            onSuccess:function(obj) {
                wynik = obj.responseText;
                if (wynik.length)
                {
                    document.getElementById('places-match-search').style.display='';
                    document.getElementById('places-match-search').style.visibility='visible';
                    document.getElementById('places-match-search').innerHTML=wynik;
                    document.getElementById('all-places').style.visibility='hidden';
                }
            },
            onError:function(obj) {alert(obj.status);}
        });
    }
}


