var n_konq  = navigator.userAgent.indexOf('Konqueror') > 0;
var n_opera = navigator.userAgent.indexOf('Opera') > 0; 
var n_msie  = navigator.userAgent.indexOf('MSIE') > 0 && !n_opera;  
var n_gecko = navigator.userAgent.indexOf('Gecko') && !n_msie && !n_opera;

var ostatnia_pomoc = '';

function pokazPomoc(nazwa, el, popup_listing)
{   
    if (!popup_listing) {
        if (ostatnia_pomoc == nazwa) {ukryjPomoc(ostatnia_pomoc); return;}
        if (ostatnia_pomoc != nazwa) {ukryjPomoc(ostatnia_pomoc);}
        if (ostatnia_pomoc != '') ukryjPomoc(ostatnia_pomoc);
    }

    var p = document.getElementById(nazwa);
    if (popup_listing) {
        przesuniecie = 0;
    } else {
        przesuniecie = 20;
    }
    if (p) {
        if (typeof $ == 'function') {
            node = $$('#'+nazwa+' [src2]');
            for (var i=0; i < node.length; i++) {
                node[i].src = node[i].getAttribute('src2');
                if ($(node[i].getAttribute('pmi')) != null) {
                  mini_x = $(node[i].getAttribute('pmi')).width;
                  if (mini_x == 80) {
                    mini_y = $(node[i].getAttribute('pmi')).height;
                    node[i].parentNode.style.height = 328 * mini_y / 80 + 10 + 'px';
                  }
                }
            }
        }
        p.style.display = 'block';
        var windowWidth = findWindowWidth();
        var left = findPosX(el);
        
        if (typeof el.getWidth == 'function') {
            left = left + el.getWidth();
        }
        if (left + findWidth(p) > windowWidth) {
            left = left - findWidth(p) - 20;
        }
        p.style.left = (left + 20)+'px';
        
        var top = findPosY(el);
        var bottom;
        var height = findHeight(p);
        var windowHeight = findWindowHeight();
        var scrollY = findScrollY();

        if (top + height + przesuniecie < scrollY + windowHeight) {
            p.style.top = (top + przesuniecie)+'px';
        } else {
            p.style.top = (scrollY+windowHeight-przesuniecie-height-10)+'px';
        }
        if (typeof $ == 'function') {
            node = $$('#'+nazwa+' .strzalka');
            for(var i=0; i < node.length; i++){
                a = (top - parseInt(p.style.top));
                a = a + el.getHeight() / 2 - 10;
                if (a < 0) {
                    a = 0;
                }
                  
                if (a + node[i].getHeight() > p.getHeight()) {
                    a = p.getHeight() - node[i].getHeight()-1;
                }
                node[i].style.top = a + 'px';
            }
        }
        ostatnia_pomoc = nazwa;
    }
}

function ukryjPomoc(nazwa)
{
    var p = document.getElementById(nazwa);
    if (p) {
        p.style.display = 'none';
        ostatnia_pomoc = '';
    }
}

function wyczyscFormularz() {
    f = document.frm_zamowienie;
    f.zglaszajacy.value = '';
    f.telefon.value = '';
    f.email.value = '';
    f.uwagi.value = '';
    
    document.cookie = 'dane_zglaszajacy=; path=/';
    document.cookie = 'dane_telefon=; path=/';
    document.cookie = 'dane_email=; path=/';
}

function validateNIP(inputVal) {
    var odp = false;
    var ar = inputVal.replace(/-/g,'');
    var arg = inputVal.replace(/-/g,'');
    var org = inputVal.replace(/-/g,'');
    
    arg = arg.substring(0,10);
    ar = ar.substring(0,10);
    
    // dozwolone nieprawidlowe nipy
    if (window.nipExceptions) {
        if (nipExceptions(org) == true) {
            return true;
        }
    }
    
    var coefficients = '657234567';
    var sum=0;
    var index=8;
    for (index=8; index>=0 ;index--) {
        sum += (parseInt(coefficients.charAt(index)) * parseInt(arg.charAt(index)));
    }

    if ((sum % 11) == 10 ? false : ((sum % 11) == parseInt(arg.charAt(9)))) {
        odp = true;
    }
    //sprawdzenie czy sa takie same liczby w stringu
    if(arg.match(/^[0]+$/) || arg.match(/^[1]+$/) || arg.match(/^[2]+$/) || arg.match(/^[3]+$/) || arg.match(/^[4]+$/) 
       || arg.match(/^[5]+$/) || arg.match(/^[6]+$/) || arg.match(/^[7]+$/) || arg.match(/^[8]+$/) ||
       arg.match(/^[9]+$/)
    ){
        odp = false;
    }

    //sprawdzenie dlugosci nipu
    if(org.length > 10){
       odp = false;
    }
    return odp;
}

// funkcja sprawdzajaca poprawnosc adresu email
function validateEmail(inputVal) {
    ereg = /^[0-9a-zA-Z.\-_\+]+\@[0-9a-zA-Z.\-_\+]+\.[0-9a-zA-Z.\-_\+]+$/;
    if (ereg.test(inputVal) != true || inputVal == 0) {
        return false;
    }
    return true;
}

function zliczBD(miejsce) {
    my_ajax = new MelogAjax();
    my_ajax.wysylaj('get','bdzliczanie.php?m='+miejsce);
}

/**
 * funkcja dodajaca zdarzenie do danego obiektu, dziala takze na onload oraz onunload
 */
function addEvent(obj, type, fn) {
  if (obj.attachEvent) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn](window.event);}
    obj.attachEvent('on'+type, obj[type+fn]);
  } else
    obj.addEventListener(type, fn, false);
}

/*zmienne globalne obslugi tabelki*/

var hl_old_class = '';
var hl_actual = null;

function hl_show(row,klasa){
    if (hl_actual  != null) {
        hl_actual.className = hl_old_class; hl_actual = null;
    }
    hl_actual = row;
    hl_old_class = row.className;
    row.className = klasa;
}
        
function hl_hidde(){
    if (hl_actual  != null) {
        hl_actual.className = hl_old_class; hl_actual = null;
    }
}
                
function jsTrim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
	strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
	strText = strText.substring(0, strText.length-1);

    return strText;
}

function getOffsets (evt) {
  var target = evt.target;
  if (typeof target.offsetLeft == 'undefined') {
    target = target.parentNode;
  }
  var pageCoords = getPageCoords(target);
  var eventCoords = { 
    x: window.pageXOffset + evt.clientX,
    y: window.pageYOffset + evt.clientY
  };
  var offsets = {
    offsetX: eventCoords.x - pageCoords.x,
    offsetY: eventCoords.y - pageCoords.y
  }
  return offsets;
}

function getPageCoords (element) {
  var coords = {x : 0, y : 0};
  while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}

function decode64(input) {
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie = c_name+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString());
}
  function PokazUkryj(id, styl) {
      if (styl == null) {
          styl="";
      }
      pole = document.getElementById(id);
      if (pole.style.display == "none") {
           pole.style.display = styl;
      } else {
           pole.style.display = "none";
      }
  }
var
 loadedobjects = "";
function loadobjs(){
if (!document.getElementById)
return
    
    for (i=0; i<arguments.length; i++) {
        var file=arguments[i]
        var fileref=""
        
        if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
            if (file.indexOf(".js")!=-1){ //If object is a js file
                fileref=document.createElement('script')
                fileref.setAttribute("type","text/javascript");
                fileref.setAttribute("src", file);
            } else if (file.indexOf(".css")!=-1){ //If object is a css file
                fileref=document.createElement("link")
                fileref.setAttribute("rel", "stylesheet");
                fileref.setAttribute("type", "text/css");
                fileref.setAttribute("href", file);
            }
        } 
        if (fileref!="") {
            document.getElementsByTagName("head").item(0).appendChild(fileref)
            loadedobjects+=file+" " //Remember this object as being already added to page
        }
    }
}

function change_menu2(akcja, nazwa) {
    if (akcja == 'off') {
        nazwa = menu1_default;
    } else {
        menu2_timer('off');
    }


    var menu1_old = document.getElementById('m1_' + menu1_current);
    if (menu1_old) {
        menu1_old.parentNode.className = menu1_old.parentNode.className.replace('here', '');
    }

    var menu1_new = document.getElementById('m1_' + nazwa);
    if (menu1_new) {
        menu1_new.parentNode.className = menu1_new.parentNode.className + ' here';
    }

    var menu2 =  document.getElementById(menu2_id);

    if (menu2_data[nazwa]) {
        menu2.innerHTML = menu2_data[nazwa];
    } else {
        menu2.innerHTML = '<li></li>';
    }
    
    menu1_current = nazwa;
    return true;
}

function menu2_timer(akcja) {
    if (akcja == 'on') {
        menu1_return = setTimeout("change_menu2('on', '" + menu1_default + "')", menu1_timeout);
    } else if (typeof menu1_return != 'undefined') {
        clearTimeout(menu1_return);
    }
}

function getStyle(el,styleProp)
{
    var x = el;
    if (x.currentStyle)
        var y = x.currentStyle[styleProp];
    else if (window.getComputedStyle)
        var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
    return y;
}


function findPosX(obj) {
    var curleft = 0;
    
    if(obj.offsetParent) {
        while(1) {
            if (getStyle(obj, "position") != 'absolute') {
                curleft += obj.offsetLeft;
            }
            
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if(obj.x) {
        curleft += obj.x;
    }

    obj.style.position = "static";

    return curleft;
}

function findPosY(obj) {
    var curtop = 0;

    if(obj.offsetParent) {
        while(1) {
            if (getStyle(obj, "position") != 'absolute') {
                curtop += obj.offsetTop;
            }
            if(!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if(obj.y) {
    	curtop += obj.y;
    }

    return curtop;
}

function findHeight(obj) {
    if (obj.offsetHeight) {
        return obj.offsetHeight;
    } else if (obj.style.pixelHeight) {
        return obj.style.pixelHeight;
    }
    return 0;
}
function findWidth(obj) {
    if (obj.offsetWidth) {
        return obj.offsetWidth;
    } else if (obj.style.pixelWidth) {
        return obj.style.pixelWidth;
    }
    return 0;
}

function findWindowHeight() {
    var myHeight = 0;
    if (typeof(window.innerHeight) == 'number' ) {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}
function findWindowWidth() {
    var myWidth = 0;
    if (typeof(window.innerWidth) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}


function findScrollX() {
    var scrOfX = 0;
    if (typeof(window.pageXOffset) == 'number') {
        //Netscape compliant
        scrOfX = window.pageXOffset;
    } else if (document.body && document.body.scrollLeft) {
        //DOM compliant
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && document.documentElement.scrollLeft) {
        //IE6 standards compliant mode
        scrOfX = document.documentElement.scrollLeft;
	}
    return scrOfX;
}

function findScrollY() {
    var scrOfY = 0;
    if (typeof(window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
    } else if (document.body && document.body.scrollTop) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}
/**
  * Funkcja umożliwia dynamiczne ładowanie plików javascript
  *
  */
function loadScript(src, callback) {
  var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  var loaded = false;
  script.setAttribute('src', src);
  script.onload = script.onreadystatechange = function() {
    if (!loaded && (!this.readyState || this.readyState == 'complete'
                                     || this.readyState == 'loaded') ) {
      loaded = true;
      callback();
      script.onload = script.onreadystatechange = null;
      head.removeChild(script);
    }
  }
   head.appendChild(script);
  
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };


function podswietlPole(id, tresc) {
    if (typeof id != 'object') {
        pole = $(id);
    } else {
        pole = id;
    }
    pole.className = 'input_aktywny';
    if (pole.value == tresc) {
        pole.value ='';
    }
}
function wygasPole(id, tresc) {
    if (typeof id != 'object') {
        pole = $(id);
    } else {
        pole = id;
    }
    if (pole.value == '') {
        pole.value = tresc;
        pole.className = 'input_wygaszony';
    }
}
