if (!String.prototype.trim) {
	String.prototype.trim = function() {
		var str = this.replace(/^\s*/, '');
		return str.replace(/\s*$/, '');
	};
}

function popupAlert(title, content, className) {
  if (alertWindow) {
    className = (className ? className : "info");
    var alert_title_div = $('alert_title_div');
    if (alert_title_div) {
      alert_title_div.className = "message message-" + className;
    }
    var alert_title = $('alert_title');
    if (alert_title) {
      alert_title.innerHTML = title;
      alert_title.className = "title " + className;
    }
    var alert_content = $('alert_content');
    if (alert_content) {
      alert_content.innerHTML = content;
    }
    var alert_button = $('alert_button');
    if (alert_button) {
      alert_button.className = className + '_button';
    }
    alertWindow.show();
  } else {
    alert(content);
  }
}

function showPopupArticle(link) {
  $('show_popup_article_title').innerHTML = link.title;
  $('show_popup_article_content_div').src = link.href.replace('/article/', '/popup/');
  onPopupArticleLoad = function () {
    showArticlePopupWindow.show();
    onPopupArticleLoad = {};
  };
}

function onPopupArticleLoad() {}


function printSelection(id) {
  var content = $(id).innerHTML;
  var pwin=window.open('','print_content','width=100,height=100');
  pwin.document.open();
  pwin.document.write('<html><body onload="window.print()">' + content + '</body></html>');
  pwin.document.close();
  setTimeout(function(){pwin.close();},1000);
}

function getTagValue(xml, tag) {
  var elements = xml.getElementsByTagName(tag);
  if (elements) {
    var element = elements[0];
    if (element && element.hasChildNodes) {
      return element.childNodes[0].nodeValue;
    }
  }
  return "";
}

function getCDataValue(xml, tag) {
  var elements = xml.getElementsByTagName(tag);
  if (elements) {
    var element = elements[0];
    if (element && element.hasChildNodes) {
      return (Prototype.Browser.IE) ?
        element.childNodes[0].nodeValue : element.childNodes[0].textContent;
    }
  }
  return "";
}

function addBookmark(url, title)
{
  if (!url) url = location.href;
  if (!title) title = document.title;

  //Gecko
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, '');
  //IE4+
  else if (typeof window.external == "object") window.external.AddFavorite(url, title);
  //Opera7+
  else if (window.opera && document.createElement)
  {
    var a = document.createElement('A');
    if (!a) return false; //IF Opera 6
    a.setAttribute('rel','sidebar');
    a.setAttribute('href',url);
    a.setAttribute('title',title);
    a.click();
  }
  else return false;

  return true;
}

function increaseClicks(type, name) {
  ajaxRequestXml('/increase_statistics.htm?type=' + type + '&key=' + name, null, null);
}

function loadCaptcha(){
  var src = "/captcha.htm?rand=" + Math.random();
  var captcha = $('captcha_container');
  var img = new Image();
  img.src = src;
  img.onload = function() {
    captcha.innerHTML = '<img class="captcha" src="' + src + '" onclick="loadCaptcha();"/>';
  };
}

function onShowHideClick(id) {
  var element = $(id);
  if (element) {
    element.style.display = ('none' == element.style.display) ? '' : 'none';
  }
}
