/* BASIC LIB */

var popUpWin = null;

// used params  hyperlink, width of popup, height, enable-scrollbars, top left corner X and top left corner Y
function popUp(href, w , h, sc, x, y) {
    if (!href) return;
    var sw = screen.width;
    var sh = screen.height;
    
    sc = (sc) ? 'yes' : 'no';  
    if (!w) w = 480;
    if (!h) h = 400;
    if (!x) x = Math.round((sw/2) - (w/2));
    if (!y) y = Math.round((sh/2) - (h/2));
    if (popUpWin && !popUpWin.closed) popUpWin.close();
    winFeatures="top="+y+",left="+x+",width="+w+",height="+h+",toolbar=no,location=no,"+
             "directories=yes,status=yes,menubar=no,resizable=yes,scrollbars="+sc;
    popUpWin = window.open(href, "InfoWindow", winFeatures);
    if (!popUpWin.opener) popUpWin.opener = self;
}

// used for quick surveys managing
function quickLink(item,check) {
    
    // non-submit branch
    if (!check) { 
        var destination = item.options[item.selectedIndex].value;
        
        if (destination) location.href = destination;
    
    } // submit branch
    else { 
    
        var destination = item.quickLinkUrl.options[item.quickLinkUrl.selectedIndex].value;
        
        if (destination) return true;
        else return false;    
    }    
}

/**
 * Function allows to show/hide concrete elements within text.
 * Obviously used with hyperlinks.
 *    
 * @var int id  ID of the node within DOM   
 */
function showAndHide(id){
    
    box = document.getElementById(id);
    box.style.display = (box.style.display!='block') ? 'block': 'none';
}

/**
 * This function hides banner bounding element if the banner is visually empty
 *
 * @var string id Id of the bounding element
 * @return Returns true if <img> was found (= visually non-empty)
 */
function hide_if_no_banner(element_id) {
  var el = document.getElementById(element_id);
  var context_el = arguments.length > 1 ? arguments[1] : el;
  var found = false;
  var child;

  if (!context_el)
    return false;
  for (var i = 0; i < context_el.childNodes.length; i++) {
    child = context_el.childNodes.item(i);
    if (child.nodeName.toLowerCase() == 'img') {
      found = true;
      break;
    }
    if (child.childNodes.length)
      if (hide_if_no_banner(element_id, child)) {
        found = true;
        break;
      }
  }
  if (!found) {
    if (arguments.length == 1) {
      el.style.display = 'none';
    }
    return false;
  }
  else
    return true;
}

/**
 * Adds hidden input field has_js to form or extends the <a> href attribute
 *
 * @param string id Id of form or the <a> tag
 */
function saveJsStatus(id)
{
  var el = document.getElementById(id);
  var input = document.createElement('input');
  if (el.tagName == 'A') {
    var href = el.getAttribute('href');
    var sep = href.indexOf('?') == -1 ? '?' : '&';
    el.setAttribute('href', href + sep + 'has_js=yes');
  }
  else {
    input.setAttribute('type', 'hidden');
    input.setAttribute('name', 'has_js');
    input.setAttribute('value', 'yes');
    el.appendChild(input);
  }
}