// JavaScript Document
var langs = new Object(); //napisy w jezykach

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 });
}

function obj2str( o_obj ){
    var str = '';
    for( var field in o_obj ){
        str += field + ' = ' + o_obj[field] + "\n";
    }
    return str;
}

/* funkcja, która wybiera obszar w obiekcie tekstowym, przekazywanym jako jej parametr */
function selectRange(oText, start, length){
    // sprawdz rodzaj przeglądarki - IE czy FF
    if (oText.createTextRange){
      //IE
      var oRange = oText.createTextRange();
      oRange.moveStart("character", start);
      oRange.moveEnd("character", length - oText.value.length);
      oRange.select();
    }else
      // FF
      if (oText.setSelectionRange){
        oText.setSelectionRange(start, length);
      }
    oText.focus();
}



/* funkcja koduja url*/
function encode(uri){
    if ( encodeURIComponent ){
        return encodeURIComponent(uri);
    }else if (escape){
        return escape(uri);
    }else{
        return uri;
    }
}

function handle_select_opk(sel){
  if(sel.selectedIndex > 0){
    sel.form.elements['OPK_SZT'].disabled = false;
    przelicz_opk2szt(sel.form);
  }else{
    sel.form.elements['OPK_SZT'].disabled = true;
  }
}
/* Opakowania zbiorcze towaru */
var towar_opk = [];

/* */
function przelicz_opk2szt(f){
  if(f.elements['OPK_ID'].selectedIndex > 0){
    var opk = f.elements['OPK_ID'].selectedIndex - 1;
    var pojemnosc = towar_opk[opk];

    var szt = parseInt(f.elements['OPK_SZT'].value);
    if( isNaN(szt) ){
      szt = 1;
      f.elements['OPK_SZT'].value = 1;
    }
    f.elements['SZT'].value = pojemnosc * szt;
  }
}

function przelicz_szt2opk(f){
  if( f.elements['OPK_ID'] && f.elements['OPK_ID'].selectedIndex > 0){
    var opk = f.elements['OPK_ID'].selectedIndex - 1;
    var pojemnosc = towar_opk[opk];

    var szt = parseInt(f.elements['SZT'].value);
    if( isNaN(szt) ){
      szt = 1;
      f.elements['SZT'].value = 1;
    }
    f.elements['OPK_SZT'].value = Math.round(szt/pojemnosc);
  }
}


/* el oznacza jaki element nalezy zwiekszyc */
function opk_zwieksz(el){
    /* zwieksz tylko wtedy gdy zostało wybrane opakowanie zbiorcze */
    if( el.form.elements['OPK_ID'].selectedIndex > 0 ){
        val = parseFloat(el.value.replace(',','.'));
        if( isNaN(val) ){
            val = 1;
        }else{
            val++;
        }
        el.value = val;
        przelicz_opk2szt(el.form);
    }
}

/* el oznacza jaki element nalezy zmniejszyc */
function opk_zmniejsz(el){
    /* zmniejsz tylko wtedy gdy zostało wybrane opakowanie zbiorcze */
    if(el.form.elements['OPK_ID'].selectedIndex > 0){
        val = parseFloat(el.value.replace(',','.'));
        if(isNaN(val)){
            val = 1;
        }else if(val > 0){
            val--;
        }
        el.value = val;
        przelicz_opk2szt(el.form);
    }
}

function zwieksz(el){
    if(el){
        val = el.value - 0;
        if(isNaN(val)){
            val = 1;
        }else{
            val++;
        }
        el.value = val;
        przelicz_szt2opk(el.form);
    }
}

function zmniejsz(el){
    if(el){
        val = el.value - 0;
        if(isNaN(val)){
            val = 1;
        }else if(val > 0){
            val--;
        }
        el.value = val;
        przelicz_szt2opk(el.form);
    }
}

/* usuwa spacje z początku i końca łańcucha*/
function trim(s){
    return s.replace(/(^\s+)|(\s+$)/g, "");
}


function basename( path ){
    return path.substring(path.lastIndexOf('/') + 1, path.length);
}

function dirname( path ){
    return path.substring(0, (path.lastIndexOf('/') + 1) );
}

function fileext(filename){
  var znak = ext = '';
  var i = 0;

  for( i = filename.length - 1; i > 0; i-- ){
      znak = filename.charAt(i);
      if( znak == '.' ){
          ext = filename.substring( i + 1 );
          break;
      }else if( znak == '/' || znak == '\\' ){
          break;
      }
  }

  return ext;
}

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;
    }

    //alert( el.clientHeight );

    if( el.scrollHeight > 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;
    }

    //alert( el.scrollHeight );

    if( el.scrollHeight > 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.scrollHeight && el.scrollHeight > 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.scrollHeight && el.scrollHeight > 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';
}

function wysrodkuj( el ){

    var width  = el.clientWidth;
    var height = el.clientHeight;

    winW = screen.width;
    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;
        }
    }

    el.style.left = parseInt( (winW - width) / 2);
    el.style.top  = document.body.scrollTop + parseInt( (winH - height) / 2);
}

function doubleClick(el, adres, width, height){
    var teraz = new Date();
    if( (teraz.getTime() - el.lastClick) < 300){
        otworz_oknoWH(adres, width, height);
    }else{
        el.lastClick = teraz.getTime();
        if( el.style.backgroundColor == ""){
            el.style.backgroundColor = "#efefef";
        }else{
            el.style.backgroundColor = "";
        }
    }
}


function zamkniecie(){
    if( odliczanie > 0 ){
        el = document.getElementById('close_info');
        if( typeof(el) == 'object'){
            el.innerHTML = odliczanie;
        }
        odliczanie--;

        setTimeout('zamkniecie()', 1000);
    }else{
        window.close();
    }
}

function przejdz_do(link){
    if( odliczanie > 0 ){
        el = document.getElementById('close_info');
        if( typeof(el) == 'object'){
            el.innerHTML = odliczanie;
        }
        odliczanie--;

        setTimeout('przejdz_do(\'' + link + '\')', 1000);
    }else{
        window.location = link;
    }
}


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();
}

function otworz_oknoWH(adres, width, height, nazwa ){
     cx = (screen.availWidth-width) / 2;
     cy = (screen.availHeight-height) / 2;

     if( !nazwa || nazwa.length == 0 ){
          nazwa = 'okno';
     }
     
     var okno = window.open(adres, nazwa, "height="+height+",width="+width+",top="+cy+",left="+cx+",status=0,scrollbars=1,resizable=1;");
     okno.focus();
}


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(langs['pole_niepuste']);
                              }
         }

         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;
    }
  }
}
var check_input  = checkInput;

function check_email( email ){
    var reg_expr = /^[\w\-\.]+@[^\.]+(\.\w+)+$/;
    return reg_expr.test(email);
}

function check_telefon( numer ){
    var reg_expr = /^[+]{0,1}[0-9\-\(\) ]+$/;
    return reg_expr.test(numer);
}

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{10})|(\d{3}\-\d{3}-\d{2}-\d{2})|(\d{3}\-\d{2}-\d{2}-\d{3})$/,'NIP powinien składać się z 10 cyfr i być wpisany w następującym formacie: 999-999-99-99 lub 999-99-99-999. Dopuszczalna jest też forma bez myślników.');

    if(error){
        return false;
    }

    wartosc = nip_input.value;
    wartosc = wartosc.replace(/^(\s)+/,"");
    wartosc = wartosc.replace(/(\s)+$/,"");

    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 sprawdzCenyPrzedzial(f){
  var cena_od = parseFloat(f.cena_od.value.replace(',','.'));
  if( isNaN(cena_od)){
    cena_od = 0;
    f.cena_od.value = '';
  }
  
  
  var cena_do = parseFloat(f.cena_do.value.replace(',','.'));
  if( isNaN(cena_do)){
    cena_do = 0;
    f.cena_do.value = '';
  }
  
  
  //sprawdzaj tylko gdy cena do jest wieksza od zera bo ktos moze sobie zyczyć
  //wyświetlac towary tylko od pewnej ceny nie ograniczone z góry przez zadna wartosc
  if ( cena_do > 0 ){
    if( (cena_do - cena_od) < 0 ){
        zmien_kolor_obramowania(f.cena_od, true, 'Nieprawidłowy przedział cenowy! Cena od powinna być mniejsza od: ' + cena_do);
        zmien_kolor_obramowania(f.cena_do, true, 'Nieprawidłowy przedział cenowy! Cena do powinna być większa od: ' + cena_od);
        return false;
    }else{
        zmien_kolor_obramowania(f.cena_od, false, '');
        zmien_kolor_obramowania(f.cena_do, false, '');
        return true;
    }
  }else{
    return true;
  }
}


// JavaScript Document
// JavaScript Document
var o_tlo_div = null;

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 tloRegFunction( action, funkcja ){
  if( !o_tlo_div )
      o_tlo_div = document.getElementById('tlo_div');

  o_tlo_div[action] = funkcja;
}


// JavaScript Document
var o_loading_div = null;
function showLoading(x, y){
    if( !o_loading_div) o_loading_div = document.getElementById('loading_div');

    o_loading_div.style.visibility = 'hidden';
    o_loading_div.style.display = 'block';
    
    if( arguments.length == 0){
        var width  = o_loading_div.clientWidth;
        var height = o_loading_div.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;
            }
        }

        x = parseInt( (winW - width) / 2);
        y = document.body.scrollTop + parseInt( (winH - height) / 2);
    }

    o_loading_div.style.left = x;
    o_loading_div.style.top  =  y
    o_loading_div.style.zIndex = 111;
    //o_loading_div.style.position = 'fixed';
    o_loading_div.style.visibility = 'visible';
}


function hideLoading(){
  if( !o_loading_div) o_loading_div = document.getElementById('loading_div');
  
  o_loading_div.style.visibility = "hidden";
  o_loading_div.style.display = "none";
  o_loading_div.style.top = 0;
  o_loading_div.style.left = 0;
}



function check_adres_form( f ){
   var error = false;

   //miasto
   if(!checkInput(f.PLACE, false, '', '')){
      message='Wpisz nazwę miejscowości!';
      error = true;
   }
    //kod
   if(!checkInput(f.ZIPCODE, false, /^[0-9][0-9]-[0-9][0-9][0-9]$/,'Nieprawidłowy format kodu pocztowego!','')){
      message='Nieprawidłowy format kodu pocztowego!';
      error = true;
   }
   //dom
   if(!checkInput(f.BUILDING_NUMBER, false, '', '')){
      message='Wpisz numer domu!';
      error = true;
   }
    //ulica

   if( !checkInput(f.STREET, false, '', '') ){
      message = 'Wpisz nazwę ulicy!';
      error = true;
   }

   return error;
}



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';

    
    var top = document.body.scrollTop + parseInt( (winH - height) / 2)
    if( top  < 0 ){
        top = 0;
    }
    obj.style.top  = top + 'px';
    
    
    obj.style.zIndex = 111;
    obj.style.visibility = 'visible';
}


function add_event(type, obj, fn, cpt){
  	if( null == obj ){
        return false;
    }

    obj["__" + type] = fn;
    obj[type+ "_cpt"] = cpt;
    
    if (obj.addEventListener) {  // DOM Level 2 Event Model
        // Register capturing event handlers
        obj.addEventListener(type, obj["__" + type], 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] );
    } 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( obj.detachEvent ){
  		  obj.detachEvent('on'+type, obj["__" + type]);
  	}else if( obj.removeEventListener ) {
  		  obj.removeEventListener(type, obj["__" + type], obj[type+ "_cpt"]);
  	}else{
        obj['on' + type] = function(){};
    }
    obj["__" + type] = null;
}

function htmlspecialchars( str ){
    var old_str = new String(str);
    var new_str = new String('');
    znak = '';
    
    for( i = 0; i < old_str.length; i++ ){
        znak = old_str.charAt(i)

        if( znak  == '"' ){
            new_str += '&quot;'
        }else if( znak  == "'" ) {
            new_str += '&#039;';
        }else if( znak  == "&" ){
            new_str += '&amp;';
        }else if( znak  == "<" ){
            new_str += '&lt;';
        }else if( znak  == ">" ){
            new_str += '&gt;';
        }else{
            new_str += znak;
        }
    }

    return new_str;
}

function number_format( value_str ){

    if( typeof(value_str) != 'string' ){
        value_str = new String(value_str);
    }
    value_str = value_str.replace(',', '.' );
    value_str = value_str.replace(' ', '' );

    val = parseFloat( value_str );
    if( isNaN(val) ){
        val = 0;
    }

    return val;
}

function round( liczba, po_przecinku ){
    var tmp_var = Math.pow(10, po_przecinku);
    return Math.round(liczba * tmp_var) / tmp_var;
}

function currency_format( cena ){
    cena = new Number( cena );
    
    //zamieniam na ciag
    var s_cena = cena.toFixed(2);
    //zamieniam kropki na przecinki
    s_cena = s_cena.replace(/\./, ',');
    //sprawdzam miejsce przecinka
    var pos = s_cena.indexOf(',');

    var czesc_ulamkowa = '';
    var czesc_calkowita = '';
    var cena_f = ''
    
    if( pos > 0){
        //wycinam miejscea po przecinku
        czesc_ulamkowa = s_cena.substr(pos + 1);
        czesc_calkowita = s_cena.substr(0, pos);
        if( czesc_ulamkowa.length < 2 ){
            czesc_ulamkowa += '0';
        }
    }else{
        czesc_ulamkowa = '00';
        czesc_calkowita = s_cena;
    }
    var i,j;
    for( i = czesc_calkowita.length-1, j = 1; i > 0; i--, j++ ){
        cena_f = czesc_calkowita.charAt(i) + cena_f;
        if( j % 3 == 0 ){
            cena_f = ' ' + cena_f;
            j = 1;
        }
    }
    cena_f = czesc_calkowita.charAt(0) + cena_f;
    
    
    return (cena_f + ',' + czesc_ulamkowa);
}


function ukryj_zawartosc( el ){
    if( el.old_width ){
        //to pokazujemy zawartosc
        el.style.width = el.old_width;
        el.style.height = el.old_height;

        //el.style.position = '';
        el.style.overflow = 'hidden';
    }
}

function pokaz_zawartosc( el ){
    if( el.scrollWidth > el.clientWidth || el.scrollHeight > el.scrollHeight ){
        //to pokazujemy zawartosc
        el.old_width    = el.clientWidth;
        el.old_height   = el.clientHeight;

        //el.style.position = 'absolute';
        el.style.overflow = 'visible';
    }
}

//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;
}



var old_row = null;
function podswietl_wiersz( row ){
    if( old_row ){
        old_row.className = old_row.className.replace(/[\s]{0,1}highlight/g, '');
    }

    old_row = row;
    
    row.className += ' highlight';
}

function clear_select( o_select, skip ){
    var i = 0;

    skip = parseInt(skip);
    
    if( isNaN(skip ) || skip < 0 || skip > o_select.length){
        skip = 0;
    }

    //usuwam opcje za wyjatkiem pierwszej i która domyslnie to utwórz nowe konto
    for( i = o_select.length - 1; i >= skip; i-- ){
        o_select.remove(i);
    }
}

function  make_select( o_select, a_options, selected ){

    //usuwam opcje
    for( i = o_select.length - 1; i >= 0; i--){
        o_select.remove(i);
    }

    //opcja wybierz
    opt = document.createElement('option');
    opt.value = '';
    opt.text  = 'Wybierz';

    o_select.add( opt, null);

    for(i in a_options){
        opt = document.createElement('option');
        opt.value = i;
        opt.text  = a_options[i];
        if( selected == i ){
            opt.selected = true;
        }

        try{
            o_select.add(opt, null);
        }catch(err){
            o_select.add(opt);
        }
    }
}


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 select_option(oSelect, value, defaultIndex, search_in_names ){

    if( arguments.length < 3 ){
        defaultIndex = 0;
        search_in_names = false;
    }
    
    var selectedIndex = defaultIndex;
    
    //pierwsza opcja
    oSelect['options'][defaultIndex].selected == true;
    
    
    if( value != '' ){
        if( search_in_names ){
            var value_lower = value.toLowerCase();
            
            for(var j = 0; j < oSelect.length; j++ ){
                if( oSelect['options'][j].value == value || oSelect['options'][j].text.toLowerCase() == value_lower ){
                    oSelect['options'][j].selected = true;
                    selectedIndex = j;
                    break;
                }
            }
        }else{
            //sprawdzam czy wartosc jest w
            for(var j = 0; j < oSelect.length; j++ ){
                if( oSelect['options'][j].value == value ){
                    oSelect['options'][j].selected = true;
                    selectedIndex = j;
                    break;
                }
            }
        }
    }

    return selectedIndex;

}



function combo_select( o_select, field ){
    if( o_select.selectedIndex > 0 ){
        o_select.form.elements[field].value = o_select.options[o_select.selectedIndex].text;
    }else{
        o_select.form.elements[field].value = '';
    }
}

function pre(obj){
    str = '';
    for(i in obj){
        str += i + ' = ' + obj[i];

    }
    alert (str);
}

function is_object( obj ){
    return (obj != null && typeof( obj ) == 'object');
}

function a_blank(){}

function empty( mixed_var ) {
    var key;

    if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || mixed_var === undefined ){
        return true;
    }

    if (typeof mixed_var == 'object') {
        for (key in mixed_var) {
            if (typeof mixed_var[key] !== 'function' ) {
	            return false;
            }
        }
        return true;
    }
    
    return false;
}
