// JavaScript Document

function surroundText(text1, text2, textarea)
{
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

		if (temp_length == 0)
		{
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		}
		else
			textarea.focus(caretPos);
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else
	{
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}
function zprava()
{
alert("pepoty kanče :-)");
}

function replaceText(text, textarea)
{
	// Attempt to create a text range (IE).
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		caretPos.select();
	}
	// Mozilla text range replace.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text + end;

		if (textarea.setSelectionRange)
		{
			textarea.focus();
			textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put it on the end.
	else
	{
		textarea.value += text;
		textarea.focus(textarea.value.length - 1);
	}
}


function initRequest()
		{
        var httpRequest;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
		return httpRequest;
		}



function makeRequest(actionid,sndtype,sndcontent,sndparams) {
    var httpRequest;
		var url;
		var content;
		var type;
		var param;
		
		httpRequest = initRequest();
		type = sndtype;
		content = sndcontent;
		param = sndparams;
		url = actionid;
					
		if ((type=='GET')&&(param!='')) 
			{
			url=url+'?'+param;
			param=null;
			}
						
	if (param=='') { param = null; } else { param=encodeURI(param); };
	
        httpRequest.onreadystatechange = function() { alertContents(httpRequest,content); };
        httpRequest.open(type, url, true)
		    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8'); 
		    httpRequest.setRequestHeader('Content-length', param.length);
        httpRequest.send(param);
        alert(httpRequest);

    }

  function alertContents(httpRequest,sndcontent) {
        var obsah;
		if (httpRequest.readyState == 4) 
			{
            if (httpRequest.status == 200) 
				{
                obsah = document.getElementById(sndcontent);
				obsah.innerHTML = httpRequest.responseText;
				alert(sndcontent);
				} else 
				{
                alert(httpRequest.status+' Problem se zpracovanim pozadavku');
            	}
        	}

    	}

function show_hide(el) {
    if (/(^| )hidden-js( |$)/.test(el.className)) {
        el.className = el.className.replace(/(^| )hidden-js( |$)/, '$2');
    } else {
        el.className += ' hidden-js';
    }
    
    return true;
}

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


function activate(id){
 $('.m-kat').removeClass('hover');
 $('#kat-'+id).addClass('hover');
}

function odkryt(){
  if(document.getElementById("skryte").style.display == "block"){
  document.getElementById("skryte").style.display = "none";
  document.getElementById("skryte_text").innerHTML = "BBCode, HTML...";
  }
  else{
  document.getElementById("skryte").style.display = "block";
  document.getElementById("skryte_text").innerHTML = "Skrýt...";
  }
}
