/*
Jeśli chcemy coś wykonać po załadowaniu strony to nalezy dodać funkc
__onload.push( { 'fun' : funkcja } );
*/
var __onload = new Array();

window.onload = function(){
    var i = 0;

    for( i = 0; i < __onload.length; i++ ){
        if( typeof(__onload[i].fun) == 'function'){
            __onload[i].fun();
        }
    }
}

function onload_add( f_fun ){
    __onload.push( { 'fun' : f_fun });
}


var tab  = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
var okno = false;

function print_r( zmienna, nazwa, wciecie ){

    if( !okno ){
        okno = window.open("","okienko");
        okno.document.open();
        okno.focus();
    }

    switch(typeof(zmienna)){

    case "number":
        if( nazwa ){
            okno.document.writeln( wciecie + nazwa + " = " + zmienna + ";<br>");
        }else{
            okno.document.writeln( wciecie + zmienna );
        }
        
    break;

    case "string":
        if( nazwa ){
            okno.document.writeln( wciecie + nazwa + " = " + zmienna + ";<br>");
        }else{
            okno.document.writeln( wciecie + zmienna );
        }

    break;

    case "boolean":
        if( nazwa ){
            okno.document.writeln( wciecie + nazwa + " = " + zmienna+ ";<br>");
        }else{
            okno.document.writeln( wciecie + zmienna );
        }
    break;

    case "object":
    //object lub array
        if(typeof(zmienna.length) != "undefined" ){
            typ = "Array( <br>";
        }else{
            typ = "Object( <br>";
        }
        if( nazwa ){
            okno.document.writeln( wciecie + nazwa + " " + typ);
        }else{
            okno.document.writeln( wciecie + typ );
        }

        for( var i in zmienna){
            print_r( zmienna[i], i, wciecie+tab );
        }
        okno.document.writeln( wciecie + "); <br>" );
        
    break;

    case "function":
        if( nazwa != "" ){
            okno.document.writeln( wciecie + "function " +nazwa + "(){<br>}<br>");
        }else{
            okno.document.writeln( wciecie + "function(){}" );
        }

    break;

    case "undefined":
        if( nazwa != "" ){
            okno.document.writeln( wciecie + nazwa + " = " + "undefined<br>");
        }else{
            okno.document.writeln( wciecie + "undefined<br>" );
        }
    break;

    }//end switch
}



function zwin_rozwin(obrazek, obiekt_id){
    if( typeof(obiekt_id) == 'string' ){
        var el = document.getElementById(obiekt_id);
    }else if( typeof(obiekt_id) == 'object' ){
        var el = obiekt_id;
    }

    if( el.clientWidth > 0  ){
        el.style.display = 'none';
        //zamieniam minus na plus
        //obrazek.src = obrazek.src.replace('minus.png','plus.png');

        obrazek.src = obrazek.src.substring(0, obrazek.src.lastIndexOf('/')+1) + 'plus.png';
    }else{
        el.style.display = 'block';
        //zamieniam plus na minus
        //obrazek.src = obrazek.src.replace('plus.png','minus.png');
        obrazek.src = obrazek.src.substring(0, obrazek.src.lastIndexOf('/')+1) + 'minus.png';

    }
}

function zwin_rozwin2(obrazek, obiekt_id){

    if( typeof(obiekt_id) == 'string' ){
        var el = document.getElementById(obiekt_id);
    }else if( typeof(obiekt_id) == 'object' ){
        var el = obiekt_id;
    }

    if( el.clientWidth > 0  ){
        el.style.display = 'none';
        //zamieniam minus na plus
        //obrazek.src = obrazek.src.replace('minus.png','plus.png');
        obrazek.src = obrazek.src.substring(0, obrazek.src.lastIndexOf('/')+1) + 'down_arr.gif';

    }else{
        //domyslne
        el.style.display = 'block';
        //zamieniam plus na minus
        //obrazek.src = obrazek.src.replace('plus.png','minus.png');
        obrazek.src = obrazek.src.substring(0, obrazek.src.lastIndexOf('/')+1) + 'up_arr.gif';
    }

}


function pokaz_ukryj_obiekt(obiekt_id){
    var el = null;

    if( typeof(obiekt_id) == 'string' ){
        el = document.getElementById(obiekt_id);
    }else if( typeof(obiekt_id) == 'object' ){
        el = obiekt_id;
    }


    if( el.clientHeight && el.clientHeight > 0 ){
        el.style.display = 'none';
    }else{
        //domyslne
        el.style.display = 'block';
    }
}

function show_hide(obiekt_id){
    var el = null;

    if( typeof(obiekt_id) == 'string' ){
        el = document.getElementById(obiekt_id);
    }else if( typeof(obiekt_id) == 'object' ){
        el = obiekt_id;
    }

    if( el.clientHeight && el.clientHeight > 0 ){
        el.style.display = 'none';
    }else{
        //domyslne
        el.style.display = 'block';
    }
}

function show(id_obiektu){
    var el = document.getElementById(id_obiektu);
    el.style.display = 'block';
    el.style.visibility = 'visible';
}

function show_obj( obj ){
    obj.style.display = 'block';
    obj.style.visibility = 'visible';
}

function show_obj_xy( obj, x, y ){
    obj.style.top = y;
    obj.style.left = x;
    obj.style.display = 'block';
    el.style.visibility = 'visible';
}

function hide(id_obiektu){
    var el = document.getElementById(id_obiektu);
    el.style.display = 'none';
}

function hide_obj(obj){
    obj.style.display = 'none';
    obj.style.visibility = 'hidden';
}

// JavaScript Document
// JavaScript Document
var o_tlo_div = null;

function init_tlo(){
    
    o_tlo_div = document.getElementById('tlo_div');

    if( !o_tlo_div ){
        o_tlo_div = document.createElement('div');

        o_tlo_div.style.position = "absolute";
        o_tlo_div.style.visibility = "hidden";
        o_tlo_div.id = 'tlo_div';
        
        document.body.appendChild(o_tlo_div);
    }
}

//onload_add(init_tlo);

function showTlo(){

  if( !o_tlo_div )
      o_tlo_div = document.getElementById('tlo_div');

  o_tlo_div.style.zIndex = 110;
  o_tlo_div.style.top = 0;
  o_tlo_div.style.left= 0;
  o_tlo_div.style.opacity= 0.6;
  o_tlo_div.style.filter = 'alpha(Opacity=60)';
  o_tlo_div.style.background = '#efefef';
  o_tlo_div.style.width  = document.body.scrollWidth;
  o_tlo_div.style.height = document.body.scrollHeight;
  o_tlo_div.style.visibility = "visible";
  o_tlo_div.style.display = "block";
}


function hideTlo(){
  if( !o_tlo_div )
      o_tlo_div = document.getElementById('tlo_div');

  o_tlo_div.style.visibility = "hidden";
}

function center_both( s_id ){
    var o_el = null;

    //sprawdzam czy juz ta funkcja była wywoływana
    //i byc moze juz został utworzony odpowiedni obiekt
    if( typeof(window['o_' + s_id]) == 'object' ){
        o_el = window['o_' + s_id];
    }else{
        o_el = document.getElementById(s_id);
        window['o_' + s_id] = o_el;
    }

    center_both_obj( o_el );

    return o_el;
}

function center_both_obj( obj ){

    obj.style.display = 'block';

    var width  = obj.clientWidth;
    var height = obj.clientHeight;

    var winW = screen.width;
    var winH = screen.height;

    if ( parseInt(navigator.appVersion) > 3 ) {
        if (navigator.appName=="Netscape") {
          winW = window.innerWidth;
          winH = window.innerHeight;
        }
        if (navigator.appName.indexOf("Microsoft")!=-1) {
          winW = document.body.offsetWidth;
          winH = document.body.offsetHeight;
        }
    }

    obj.style.left = parseInt( (winW - width) / 2);
    obj.style.position = 'absolute';
    obj.style.top  = document.body.scrollTop + parseInt( (winH - height) / 2);
    obj.style.zIndex = 111;
    obj.style.visibility = 'visible';
}

function get_dimensions( element, a_dim ){
    a_dim['x'] = find_pos_x(element); //element.offsetLeft;
    a_dim['y'] = find_pos_y(element); //element.offsetTop;
    a_dim['width'] = element.offsetWidth;
    a_dim['height'] = element.offsetHeight;
}

function find_pos_x(obj){
    var posLeft = 0;
    if( obj.offsetParent ){
        while( true ){
            posLeft += obj.offsetLeft;
            if(!obj.offsetParent){
                break;
            }
            obj = obj.offsetParent;
        }
    }else if( obj.x ){
        posLeft += obj.x;
    }

    return posLeft;
}

function find_pos_y(obj){
    var cur = 0;
    if(obj.offsetParent)
    while( true ){
        cur += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
    }
    else if(obj.y)
    cur += obj.y;
    return cur;
}

function clear_form(o_form){
    
    for(i = 0; i < o_form.elements.length; i++){
        if(o_form.elements[i].type == 'text' || o_form.elements[i].type == 'textarea' ){
            o_form.elements[i].value = '';
        }else if( o_form.elements[i].type == 'checkbox'){
            toLowerCase.elements[i].checked = false;
        }else if( o_form.elements[i].tagName.toUpperCase() == 'SELECT' ){
            if( o_form.elements[i].options.length > 0 ){
                o_form.elements[i].selectedIndex = 0;
            }
        }else {
            //alert(formularz.elements[i].type);
        }
    }
    
}


function zmien_kolor_obramowania(el, error, komunikat){
  if(!error){
          el.onmouseover=function anonymous(){}
          el.onmouseout=function anonymous(){}
          el.style.borderColor='#e2e8e2';
          return true;
  }else{
          el.style.borderColor='red';
          el.onmouseover=function anonymous(){ return overlib(komunikat); }
          el.onmouseout=function anonymous(){nd();}
          el.onclick=function anonymous(){ nd(); this.onmouseover=function anonymous(){} }
          return false;
  }
}

function checkInput(el, zezwalajNaPuste, wyrazenie, komunikat){
  if(el == 'undefined') return false;

  var wartosc = el.value;

  //usuwamy spacje z poczatku i końca ciagu
  wartosc=wartosc.replace(/^(\s)+/,"");
  wartosc=wartosc.replace(/(\s)+$/,"");
  if( wartosc == ""){
   if(zezwalajNaPuste){
     el.onmouseover=function anonymous(){}
     el.onmouseout=function anonymous(){}
     el.style.borderColor='#e2e8e2';
     return true;
   }else{
     el.style.borderColor='red';
     if(komunikat != ""){
        el.onmouseover=function anonymous(){
                                  return overlib(komunikat);
                          }

     }else{
        el.onmouseover=function anonymous(){
                                  return overlib('To pole nie może być puste.');
                          }
     }

     el.onmouseout=function anonymous(){
                                  nd();
                          }
     el.onclick=function anonymous(){
                                  nd();
                                  this.onmouseover=function anonymous(){}
                          }
     return false;
   }
  }else{
    if(typeof(wyrazenie)!="string") {
       if(wyrazenie.test(wartosc)){
          el.onmouseover=function anonymous(){}
          el.onmouseout=function anonymous(){}
          el.style.borderColor='#e2e8e2';
          return true;
       }else{
          el.style.borderColor='red';
          el.onmouseover=function anonymous(){
                                  return overlib(komunikat);
                          }
          el.onmouseout=function anonymous(){
                                  nd();
                          }
          el.onclick=function anonymous(){
                                  nd();
                                  this.onmouseover=function anonymous(){}
                          }
          return false;
       }
    }else{
          el.onmouseover=function anonymous(){}
          el.onmouseout=function anonymous(){}
          el.style.borderColor='#e2e8e2';
          return true;
    }
  }
}


function checkNIP( nip_input ){
    var  suma = 0;
    var error = false;

    var  nip  = new String(nip_input.value.replace(/-/g,""));
    nip = nip.replace(/^(\s)+/,"");
    nip = nip.replace(/(\s)+$/,"");
    var  kontrol = parseInt(nip.charAt(nip.length-1));
    error = !checkInput(nip_input, false, /^(\d{3}\-\d{3}-\d{2}-\d{2})|(\d{3}\-\d{2}-\d{2}-\d{3})$/,'NIP powinien mieć następujący format: 999-999-99-99 lub 999-99-99-999');

    if(error){
        return false;
    }

    wartosc = nip_input.value;
    wartosc = wartosc.replace(/^(\s)+/,"");
    wartosc = wartosc.replace(/(\s)+$/,"");
    /*
    if( wartosc == ""){
     nip_input.onmouseover=function anonymous(){}
     nip_input.onmouseout=function anonymous(){}
     nip_input.style.borderColor='#e2e8e2';
     return true;
    }
    */
    suma  = parseInt(nip.charAt(0)) * 6;
    suma += parseInt(nip.charAt(1)) * 5;
    suma += parseInt(nip.charAt(2)) * 7;

    suma += parseInt(nip.charAt(3)) * 2;
    suma += parseInt(nip.charAt(4)) * 3;
    suma += parseInt(nip.charAt(5)) * 4;

    suma += parseInt(nip.charAt(6)) * 5;
    suma += parseInt(nip.charAt(7)) * 6;

    suma += parseInt(nip.charAt(8)) * 7;

    suma-=(Math.floor(suma / 11) * 11);
    if(suma == 10){
        suma = 0;
    }

    if(suma == kontrol){
        zmien_kolor_obramowania(nip_input, false, "");
        return true;
    }else{
        zmien_kolor_obramowania(nip_input, true, "NIP jest niepoprawny!");
        return false;
    }
}


function sprawdz_formularz_kontaktowy(f){
     var error = false;
     var message = "Popraw formularz, wystapiły nastepujące błędy:\n\n";

     //imie i nazwisko
     if( !checkInput( f.imie_nazwisko, false, '', 'Musisz podać swoje imie i nazwisko!' )){
         message += 'Musisz podać swoje imie i nazwisko!\n';
         error = "true";
     }

     //email
     if(!checkInput(f.email, false, /^[\w\-\.]+@[^\.]+(\.\w+)+$/,'Nieprawidłowy format adresu email!')){
        message += 'Niepoprawny format adresu email!\n';
        error = "true";
     }
/*
     //telefon
     if(!checkInput( f.telefon, false, /^[0-9\-\(\)\s]+$/, 'Numer telefonu musi składać się z samych cyfr i ewentualnie znaków spacji!')){
        message += 'Numer telefonu powinien składać się z samych cyfr i ewentualnie znaków spacji!\n';
        error = "true";
     }
  */

     //tresc
     if(!checkInput( f.tresc, false, '', 'Musisz wpisać treść wiadomości!' )){
        message += 'Musisz wpisać treść wiadomości!\n';
        error = "true";
     }
     
      //  zgoda
     if(!f.zgoda.checked){
         message += 'Musisz wyrazić zgodę na przetwarzanie swoich danych osobowych!\n';
         zmien_kolor_obramowania(f.zgoda, true, "Musisz wyrazić zgodę na przetwarzanie swoich danych!");
         error = true;
     }else{
         zmien_kolor_obramowania(f.zgoda, false, "");
     }

     //wyswietlenie komunikatu o bledzie
     if(error){
        alert(message);
        return false;
     }else{
        f.submit();
        //otworzLoadingOkno();
        return true;
     }
}


function select_all( chbox, chx_name){
    var formularz = chbox.form;
    var i = 0;

    if( chbox.checked ){
        while(typeof(formularz.elements[chx_name + '['+ i +']']) == 'object'){
            formularz.elements[chx_name+'['+ i +']'].checked = true;
            i++;
        }
    }else{
        while(typeof(formularz.elements[chx_name+'['+ i +']']) == 'object'){
            formularz.elements[chx_name+'['+ i +']'].checked = false;
            i++;
        }
    }
}


function otworz_okno(adres, nazwa, w, h, menubar_on ){
    var width, height, menubar = '0';

    if( arguments.length > 2 ){
        //znaczy ze zostały podane szerokosc i wysokosc
        width  = w; height = h;

        if( menubar_on ){
            menubar = '1';
        }
    }else{
        width = 800;
        height = 600;
    }

    if( !nazwa || nazwa.length == 0 ){
        nazwa = 'okno';
    }
    cx = (screen.availWidth-width)/2;
    cy = (screen.availHeight-height)/2;
    okno = window.open(adres, "okno", "height="+height+",width="+width+",top="+cy+",left="+cx+",toolbar=0,location=0,menubar="+menubar+",status=1,scrollbars=1,resizable=1;");
    okno.focus();
}

//wyłancza inputa
function input_disable( el ){
    el.className += ' disabled';
    el.readonly = true;
    el.disabled = true;
    return true;
}

//włancza inputa
function input_enable( el ){
    el.className = el.className.replace(/[\s]{0,1}disabled/g, '');
    el.readonly = false;
    el.disabled = false;
    return true;
}

function select_option(oSelect, value, defaultIndex ){

    if( arguments.length < 3 ){
        defaultIndex = 0;
    }

    var selectedIndex = defaultIndex

    if( value != '' ){
        //sprawdzam czy login jestw  mailach
        for(var j = 0; j < oSelect.length; j++ ){
            if( oSelect['options'][j].value == value ){
                oSelect['options'][j].selected = true;
                selectedIndex = j;
                break;
            }
        }
    }else if( oSelect.length > 0 && defaultIndex > 0 && defaultIndex < oSelect.length ){
        //domyślna opcja
        oSelect['options'][defaultIndex].selected == true;
    }

    return selectedIndex;

}

function combo_select(o_select, input_name ){

    o_select.form.elements[input_name].value = o_select.options[o_select.selectedIndex].text ;

}

function addEvent(type, obj, fn, cpt){
  	if( null == obj ){
        return false;
    }

    obj[type+fn] = fn;
    if (obj.addEventListener) {  // DOM Level 2 Event Model
        // Register capturing event handlers
        obj.addEventListener(type, obj[type+fn], cpt);
    } else if (document.attachEvent) {  // IE 5+ Event Model
        // In the IE Event model, we can't capture events, so these handlers
        // are triggered when only if the event bubbles up to them.
        // This assumes that there aren't any intervening elements that
        // handle the events and stop them from bubbling.
        obj.attachEvent("on" + type, obj[type+fn] );
    } else {// IE 4 Event Model
        // In IE 4 we can't use attachEvent(), so assign the event handlers
        // directly after storing any previously assigned handlers so they
        // can be restored.  Note that this also relies on event bubbling.
        obj["on" + type] =  function(){obj[type+fn](window.event)};
    }
}

function removeEvent(type, obj){
  	if( obj.detachEvent ){
  		  obj.detachEvent('on'+type, obj[type+fn]);
  	}else if( obj.removeEventListener ) {
  		  obj.removeEventListener(type, obj[type+fn], cpt);
  	}else{
        obj['on' + type] = function(){};
    }
    obj[type+fn] = null;
}

function add_event(type, obj, fn, cpt){
  	if( obj == null ){
        return false;
    }

    if( type.substring(0,2).toLowerCase() == "on" ){
        type = type.substring(2);
    }

    obj[type + "_fun" ]  = fn;
    obj[type + "_cpt" ] = cpt;


    if (obj.addEventListener) {  // DOM Level 2 Event Model
        // Register capturing event handlers
        obj.addEventListener(type, obj[type + "_fun"], cpt);
    } else if (document.attachEvent) {  // IE 5+ Event Model
        // In the IE Event model, we can't capture events, so these handlers
        // are triggered when only if the event bubbles up to them.
        // This assumes that there aren't any intervening elements that
        // handle the events and stop them from bubbling.
        obj.attachEvent("on" + type, obj[type + "_fun"] );
    } else {// IE 4 Event Model
        // In IE 4 we can't use attachEvent(), so assign the event handlers
        // directly after storing any previously assigned handlers so they
        // can be restored.  Note that this also relies on event bubbling.
        obj["on" + type] =  function(){obj[type+fn](window.event)};
    }
}

function remove_event(type, obj){

    if( type.substring(0,2).toLowerCase() == "on" ){
        type = type.substring(2);
    }

    if( obj.detachEvent ){
  		  obj.detachEvent('on'+type, obj[type + "_fun"]);
  	}else if( obj.removeEventListener ) {
  		  obj.removeEventListener(type, obj[type + "_fun"], obj[type+ "_cpt"]);
  	}else{
        obj['on' + type] = function(){};
    }

    obj[type + "_fun"] = null;
}

var old_row = null;
function podswietl_wiersz( row, multi ){
    if( arguments.length == 1 ){ multi = false; }

    if( !multi && old_row ){
        old_row.className = old_row.className.replace(/[\s]{0,1}highlight/g, '');
    }

    old_row = row;

    if( row ){
        row.className += ' highlight';
    }
}
