function searchClear(target, searchText)
{
  if (target.value == searchText)
  {
    target.value = '';
  }
}

function searchRestore(target, searchText)
{
  if (target.value == '')
  {
    target.value = searchText;
  }
}

function submitOnChange() {
  document.getElementById('hledej_dotace').submit();
}

function bar(url, title) {
	wasOpen = false;
	win     = window.open(url, title);    
	return (typeof(win) == 'object') ? true : false;
}

function myPopImage(imageURL,imageTitle,imageWidth,imageHeight) {

  PositionX = 30;
  PositionY = 30;
  
  ratio = imageWidth/imageHeight;
  maxHeight = screen.height - 105;
  maxWidth = maxHeight*ratio;
  if (maxWidth > screen.width-30) {
    maxWidth = screen.width - 80;
    maxHeight = maxWidth/ratio;
  }
    
  var opt='width=320,height=240,left='+PositionX+',top='+PositionY+',status=0,toolbar=0,menubar=0,location=0';
  imgWin=window.open('about:blank','null',opt);
  with (imgWin.document) {
    writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');
    writeln('<sc'+'ript>');
    writeln('function reSizeToImage(){');
    writeln('if (document.images[0].height > '+maxHeight+' || document.images[0].width > '+maxWidth+'){');
    writeln('height = '+maxHeight+';');
    writeln('width = '+maxWidth+';');
    writeln('document.images[0].width = '+maxWidth+';');
    writeln('document.images[0].height = '+maxHeight+';');
    writeln('window.resizeTo(width+40,height+100);}');
    writeln('else {');
    writeln('height = document.images[0].height;');
    writeln('width = document.images[0].width;');
    writeln('window.resizeTo(width+40,height+100);}}');
    writeln('function doTitle(){document.title="'+imageTitle+'";}');
    writeln('</sc'+'ript>');
    writeln('</head><body bgcolor="FFFFFF" onload="reSizeToImage();doTitle();self.focus()">');
    writeln('<img alt="'+imageTitle+'" title="'+imageTitle+'" src="'+imageURL+'" style="display:block; margin:0 auto;" /></body></html>');
    close();
  }
}

function myPopWindow(windowURL, windowName, windowFeatures) {
  var defaultFeatures = {"status": 0, "toolbar": 0, "location": 0, "menubar": 0,
                         "directories": 0, "resizeable": 1, "scrollbars": 1,
                         "width": 640, "height": 480};
  var features = '';

  for (i in windowFeatures) { defaultFeatures[i] = windowFeatures[i]; }
  for (i in defaultFeatures) { features += i + '=' + defaultFeatures[i] + ','; }

  window.open(windowURL, windowName, features);
  return false;
}

function switchBmk(n, css) {
  if (document.getElementById('bmk_header')) {
    var num = document.getElementById('bmk_header').getElementsByTagName('div').length;
    for (var i=1;i<=num;i++) {
      var elt = document.getElementById('bmk_title_' + i);
      var elc = document.getElementById('bmk_content_' + i);
      if (i == n) {
        elt.className = css + '_title active';
        elc.className = css + '_content visible';
      }
      else {
        elt.className = css + '_title';
        elc.className = css + '_content invisible';
      }
    }
  }
}

function showAdvanced(o, b, c) {
  if ($(o).visible()) {
    $(o).hide();
    $(b).className = 'show';
    $(c).value = '0';
  } else {
    $(o).show();
    $(b).className = 'hide';
    $(c).value = '1';
  }
}

// parse search words from current URL (via $loc and $varName) and highlight them in target element (via $elID)
function highlightWords(loc, elId, varName) {
  var params = loc.split('&');
  for (var i=0;i<params.length;i++) {
    if (params[i].match(varName)) {
      var str = params[i].split('=')[1];
    }
  }
  if ($(elId) && (typeof str != 'undefined' || str != '')) {
    var words = decodeURI(str);
    words = words.split('+');
    for (var i=0;i<words.length;i++) {
      words[i] = words[i].escapeHTML();
    }
    var re = new RegExp('('+words.join('|')+')', 'gi');
    highlightWordsElem($(elId), re);
  }
}

function highlightWordsElem(el, re) {
  for (var i=0;i<el.childNodes.length;i++) {
    if (el.childNodes[i].nodeName == '#text' && el.className != 'highlight_words') {
      var text = el.childNodes[i].data || el.childNodes[i].textContent;
      var textContentEscaped = text.escapeHTML();
      if (textContentEscaped.match(re)) {
        var html = textContentEscaped.replace(re, '<span class="highlight_words">$1</span>');
        if (el.childNodes.length == 1) {
          el.innerHTML = html;
        } else {
          var hSpan = new Element('span');
          hSpan.innerHTML = html;
          if (typeof el.childNodes[i].nextSibling != 'undefined') {
            el.insertBefore(hSpan, el.childNodes[i].nextSibling);
            if (typeof el.childNodes[i].data != 'undefined') {
              el.childNodes[i].data = '';
            } else {
              el.childNodes[i].textContent = '';
            }
          } else if (typeof el.childNodes[i].previousSibling != 'undefined') {
            el.appendChild(hSpan);
            if (typeof el.childNodes[i].data != 'undefined') {
              el.childNodes[i].data = '';
            } else {
              el.childNodes[i].textContent = '';
            }
          }
        }
      }
    } else {
      highlightWordsElem(el.childNodes[i], re);
    }
  }
}