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

// Removes leading and ending whitespaces
function trim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

function trim_alternativeoptimized(str) {
  return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function swapImg(el,imgsrc) {
	el.src=imgsrc;
}

function swapImgId(id,imgsrc) {
	document.getElementById(id).src=imgsrc;
}

function set_opacity(el,opacity) {
		el.style['background'] = "white";
		el.style['filter'] = "alpha(opacity="+(opacity*100)+")";		
		el.style['-moz-opacity'] = opacity;
		el.style['opacity'] = opacity;
}


function toggle_div_display(divId) {
	var div = document.getElementById(divId);
    if(div.style.display == 'none') {
			div.style.display = 'block';
		} else {
			div.style.display = 'none';
		}
}

function hide_div(divId) {
	var div = document.getElementById(divId);
	div.style.display = 'none';
}

function show_div(divId) {
	var div = document.getElementById(divId);
  div.style.display = 'block';
}

function submitLogBookForm() {
  var submit=1;
	if (trim(document.getElementById("inputLogBookName").value) == 0){
    alert("Nevyplneno jmeno!");
    submit=0;
  }
	if (document.getElementById("textareaLogBook").value.length == 0){
    alert("Nevyplnen vzkaz!");
    submit=0;
  }
	if (document.getElementById("inputLogBookCode").value.length != 4){
    alert("Nevyplnen ctyrmistny kod!");
    submit=0;
  }
	if (submit == 1) document.getElementById("formLogBook").submit();
}






function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
   if (title!="") {
      writeCookie(title);
   }
}

function getInactiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.disabled) return a.getAttribute("title");
  }
  return null;
}

function readCookie() {
    var theme = document.cookie;
    var theme = unescape(theme);

    return theme;
}

function writeCookie(theme) {
   //FIXME - set expires
   var original_cookie = "theme=" + escape(theme);
   document.cookie = original_cookie;
}

function checkForTheme() {
   var theme = readCookie();
   //alert(theme);
   if (theme=="undefined") {
      var theme = "none";
   }
}

// what a kludge. Luckily I found a clean way
function alignForGorilla() {
var image_preview = document.getElementById('preview');
image_preview.style.marginLeft = "-" + (image_preview.width/2 + 16) + "px";
}

// to hide and show the comment block
// inspired by www.wikipedia.org
function toggle_comment() {
    var comment_form = document.getElementById('comment_form');
    var showlink=document.getElementById('showlink');
    var hidelink=document.getElementById('hidelink');
    if(comment_form.style.display == 'none') {
	comment_was = comment_form.style.display; 
	comment_form.style.display = '';
	hidelink.style.display='';
	showlink.style.display='none';
    } else {
	comment_form.style.display = comment_was;
	hidelink.style.display='none';
	showlink.style.display='';
    }
}

function toggle_div(sidebar,main) {
	var divSidebar = document.getElementById(sidebar);
	var divMain = document.getElementById(main);
    if(divSidebar.style.display == 'none') {
			divSidebar.style.display = 'block';
			divMain.style.marginLeft = '190px';	
		} else {
			divSidebar.style.display = 'none';
			divMain.style.marginLeft = '10px';	
		}
}


