function $() {
   var res = [];
   for (var i = 0; i < arguments.length; i++) {
      var e = arguments[i];
      if (typeof e == 'string') {
         e = document.getElementById(e);
      }
      if (arguments.length == 1) {
         return e;
      }
      res.push(e);
  }
  return res;
}

/**
 * Topmenu onmouseover/out image swap functions.
 */
function topHin(e) {
   e.setAttribute('_mem', e.src);
   e.src = e.src.replace(/_[ne]_/, "_h_");
}
function topHout(e) {
   e.src = e.getAttribute('_mem');
}

/* gfTable - onMouseOver
 */
function hIn(e, recurseCells) {
   var e, recurseCells, i;
   e.setAttribute('memBgColor', e.style.backgroundColor);
   e.setAttribute('memColor', e.style.color);

   e.style.backgroundColor = '#c4ccd2';
   e.style.color = 'black';

   if (recurseCells && e.tagName == 'TR') {
      for (i = 0; i < e.cells.length; i++) {
         hIn(e.cells[i],0);
      }
   }
}

/* gfTable - onMouseOut
 */
function hOut(e, recurseCells) {
   if (gfHasAttribute(e, 'memBgColor')) {
      e.style.backgroundColor = e.getAttribute('memBgColor');
      e.style.color = e.getAttribute('memColor');
   }
   if (recurseCells && e.tagName == 'TR') {
      for (i = 0; i < e.cells.length; i++) {
         hOut(e.cells[i],0);
      }
   }
}

/* Check if element has attribute present
 */
function gfHasAttribute(element, attribute) {
   var element, attribute;
   try {
      return element.attributes[attribute].specified;
   }
   catch(err) {
      try {
         return element.hasAttributes(attribute);
      }
      catch (err) {
      }
   }
   return false;
}


/**
 * gfTable() tr-onclick navigate to product
 */
function pid(e) {
   var pid = e.getAttribute('p');
   if (pid === null) {
      // for compatibility reasons
      pid = e.getAttribute('__pid');
   }
   top.location.href = 'products.php?pid=' + pid;
}


function g(url) {
   top.location.href = url;
}


function gfSearch() {
   g('search.php?search=' + gfEscape(document.getElementById('topsearch').value));
}

function gfEscape(str) {
   return escape(str).replace(/\+/g, '%2B').replace(/\//g, '%2F');
}

function gfSecretToStr(secret) {
   var offset = 333;
   var res = '';
   for (var i = 0, j = 0; i < secret.length; i=(i+3), j++) {
      var nbr = new Number(secret.substring(i, i+3)) - (offset + j);
      res += String.fromCharCode(nbr);
   }
   return res;
}

/**
 * Convert secret to <a href="mailto:<address>"><address></a>
 */
function e(secret) {
   var res = gfSecretToStr(secret);
   document.write('<a href="mailto: ' + res + '">' + res + '</a>');
}


function gfPrintPage() {
   var url = top.location.href.replace(/#.*$/, '');
   if (url.indexOf('?') == -1) {
      url += '?';
   }
   else {
      url += '&';
   }
   url += 'printable=1';
   var w = window.open(url, 'wnd_print', 'width=650,height=768,top=20,left=140,resizable=1,scrollbars=1,status=1,toolbar=1,menubar=1');
   w.focus();
   w.print();
}


function popup(url) {
   var w = window.open(url, 'wnd_print', 'width=550,height=768,top=20,left=140,resizable=1,scrollbars=1,status=1,toolbar=1,menubar=1');
}

// Helper for admin pid
function popupWiringDiagram(srcName) {
   var i = document.frmDicon[srcName].selectedIndex;
   var img = document.frmDicon[srcName][i].value;
   popup('media/wiring_diagrams/' + img);
}

// Helper for admin pid
function popupCadLink(srcName) {
   var href = document.frmDicon[srcName].value;
   popup(href);
}

// Admin ajax accesories popup
function gfShowAccessories(element) {
   if (! $('acdiv')) {
      var e = document.createElement('div');
      e.id = 'acdiv';
      element.parentNode.appendChild(e);
   }
   gfDivNav('acdiv', 'ajax_accessory_table', {accessories:element.value}); 
}

// Subscribe newsletter
function showSubscribe() {
   $('subscribe1').style.display = 'none';
   $('subscribe2').style.display = 'block';
   $('snamn').focus();
}
function doSubscribe() {
   var snamn = escape($('snamn').value);
   var semail = escape($('semail').value);
   top.location.href = 'http://www.epostservice.com/subscribe.asp?code=6744108&url=http%3A%2F%2Fwww.ebmpapst.se%2Fsv%2Fsubscribeok.php&' +
                       'unsuburl=http%3A%2F%2Fwww.ebmpapst.se&verifyurl=http%3A%2F%2Fwww.ebmpapst.se&listid=13615&extrafields=&' +
                       'namn=' + snamn + '&email=' + semail;
}
