
// otevření okna s obrázkem
//------------------------------------------------------------------------------
function openwindow (url, width, height)
{
  var browser = navigator.appName;
  var verze = parseInt(navigator.appVersion);
  var top, left;
  if ((browser == "Microsoft Internet Explorer" & verze >= 4) | (browser == "Netscape" & verze >= 4))
  {
    top = (screen.height/2) - (height/2) - 40;
    left = (screen.width/2) - (width/2) - 40;
  }
  else
  {
    top = 0;
    left = 0;
  }
  imgwindow = window.open (url,'','width='+width+', height='+height+', top='+top+', left='+left+', toolbar=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=1');
  //imgwindow.focus();
}

// vkládání do formulářů
//------------------------------------------------------------------------------
/*
function insert_tag (id, tag)
{
 var obj = document.getElementById(id);
 obj.focus();
 if (document.selection)
 {
   var textrange = document.selection.createRange();
   textrange.text = tag;
 }
 else if  (obj.selectionStart || obj.selectionStart == '0')
   obj.value = obj.value.substring(0,obj.selectionStart) + tag + obj.value.substring(obj.selectionStart, obj.value.length);
 else
  obj.value += tag;
}
*/

//------------------------------------------------------------------------------
// TEXT TO HTML - vkládání značek do formulářových polí
//------------------------------------------------------------------------------

function text2html_tag (id, tag, tag2)
{
 var obj = document.getElementById(id);
 obj.focus();
 
 // IE
 if (document.selection)
 {
   var textrange = document.selection.createRange();
   // odstranění poslední mezery z výběru
   if (textrange.text.substr(textrange.text.length-1,1) == ' ')
   {
     textrange.moveStart("character", -1);
     textrange.select();
   }
   // zrušení výběru při požadavku "no_select" -  umístění kurzoru na konec výběru
   if (tag2 == "no_select")
   {
     textrange.moveStart("character", textrange.text.length);
     textrange.select();
     tag2 = "";
   }
   
   // nulový výběr
   if (!textrange.text.length) move_cursor = true;
   
   // přidat tagy
   textrange.text = tag + textrange.text + tag2;
   
   // nulový výběr
   if (move_cursor)
   {
     textrange.moveStart("character", -tag2.length);
     textrange.collapse();
     textrange.select();
   }
 }
 
 // Mozilla
 else if  (obj.selectionStart || obj.selectionStart == '0')
 {
   // uložení pozic výběru
   sel_start = obj.selectionStart;
   sel_end = obj.selectionEnd;
   scroll_position = obj.scrollTop;
   
   // zrušení výběru při tomto požadavku (umístění kurzoru na konec výběru)
   if (tag2 == "no_select")
   {
     sel_start = sel_end;
     tag2 = "";
   }
   
   // odebrat z výběru poslední mezeru v případě vkládání párového tagu
   if (obj.value.substring(sel_end-1,sel_end) == ' ' && tag2 && obj.selectionStart != obj.selectionEnd)
     sel_end--;
   
   // vložení tagů
   obj.value = obj.value.substring(0,sel_start) + tag + obj.value.substring(sel_start, sel_end) + tag2 + obj.value.substring(sel_end, obj.value.length);
   
   // znovunastavení výběru
   obj.selectionEnd = sel_end + tag.length + tag2.length;
   obj.selectionStart = obj.selectionEnd;
   
   // nulový výběr > posun doprostřed
   if (sel_start == sel_end)
     obj.selectionStart = obj.selectionEnd = obj.selectionStart - tag2.length;

   
   // scrollování v textarea
   obj.scrollTop = scroll_position;
  
 }
 
 // jiné
 else
  obj.value += (tag + tag2);
}

// označení všech formulářových prvků daného jména
//------------------------------------------------------------------------------
function set_checkboxes (id, field, value)
{
  for (i = 0; i < id.length; i++)
    if (field == id[i].name) id[i].checked = value;
}

// kontrola formuláře (chat-i)
//------------------------------------------------------------------------------
function chat_check_form (f)
{
  var err = false;
  var str = '';
  if (f.name.value == '') {str = str+'Napiš své jméno\n'; err = true;}
  if (f.text.value == '') {str = str+'Napiš nějaký text\n'; err = true;}
  if (err) {alert(str); return false;}
  else return true;
}

// vytvoření emailu
//------------------------------------------------------------------------------
function make_email (email)
{
  var arr = email.split (' [z] ');
  if (arr.length == 1) document.write ("nemá");
  else document.write ("<a href='mailto:" + arr[0] + "@" + arr[1] + "'>" + arr[0] + "@" + arr[1] + "</a>");
}


//------------------------------------------------------------------------------
// zobrazení / skrytí elementu
//------------------------------------------------------------------------------
function switchElementVisibility(id, displayType)
{
  if (document.getElementById(id).style.display == 'none')
    document.getElementById(id).style.display = displayType;
  else
    document.getElementById(id).style.display = 'none';
}

/**
 * TYP PROHLÍŽEČE
 * @return string - "ie" pro IE | "moz" pro mozillu/operu/safari | "unknown" neznámé
 */
function browserType()
{
    if (window.ActiveXObject)
        return "ie";
    // mozilla/opera/safari
    else if (window.XMLHttpRequest)
        return "moz";
    else
        return "unknown";
}

//------------------------------------------------------------------------------
// ajax
//------------------------------------------------------------------------------

function getHttpRequest()
{
    var httpRequest;
    var borwser = browserType();

    // mozilla/opera/safari
    if (borwser == "moz")
    {
        httpRequest = new XMLHttpRequest();
    }
    // IE
    if (borwser == "ie")
    {
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch(e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		    } catch(e) {
                httpRequest = false;
                alert("Browser is not compatible with AJAX")
		    }
	    }
    }
    return httpRequest;
}


function sendRequest(method, url, content)
{
    var httpRequest = getHttpRequest();
    if (!httpRequest) return false;
    if (method == "post")
    {
        httpRequest.open("POST", url);
        httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        httpRequest.send(content);
    }
    else if (method == "get")
    {
        httpRequest.open("GET", url);
        httpRequest.send(content);
    }
    return httpRequest;
}



//------------------------------------------------------------------------------


function dialogClose(id)
{
  div = document.getElementById(id);
  div.style.display = "none";
}

//------------------------------------------------------------------------------

function programGameSearchDialogOpen()
{
  div = document.getElementById('program_game_add');
  div.style.display = "block";
}

function programGameSearch()
{
    var div = document.getElementById('game_search_results');
    var str = encodeURIComponent(document.getElementById('game_search').value);
    var httpRequest = sendRequest("get", "program-ax.php?search_games=1&str="+str, null);
    httpRequest.onreadystatechange = function()
    {
        if (httpRequest.readyState == 4)
        {
            if (httpRequest.status == 200)
            {
                div.innerHTML = httpRequest.responseText;
            }
        }
    };
}

function programGetGameDetail(id)
{
    var div = document.getElementById('game_search_detail');
    var httpRequest = sendRequest("get", "program-ax.php?get_game_detail=1&id="+id, null);
    httpRequest.onreadystatechange = function()
    {
        if (httpRequest.readyState == 4)
        {
            if (httpRequest.status == 200)
            {
                div.innerHTML = httpRequest.responseText;
            }
        }
    };

}

function programGameSelect(id, name)
{
    var mladsi = document.getElementById('hrali_mladsi').checked ? 1 : 0;
    var starsi = document.getElementById('hrali_starsi').checked ? 1 : 0;
    var div = document.getElementById('program_selected_games');
    var div = document.getElementById('program_selected_games');
    var httpRequest = sendRequest("get", "program-ax.php?game_select=1&id="+id+"&name="+name+"&mladsi="+mladsi+"&starsi="+starsi, null);
    httpRequest.onreadystatechange = function()
    {
        if (httpRequest.readyState == 4)
        {
            if (httpRequest.status == 200)
            {
                div.innerHTML = httpRequest.responseText;
            }
        }
    };

}

function programDelSelectedGame(id)
{
    var div = document.getElementById('program_selected_games');
    var httpRequest = sendRequest("get", "program-ax.php?del_selected_game=1&id="+id, null);
    httpRequest.onreadystatechange = function()
    {
        if (httpRequest.readyState == 4)
        {
            if (httpRequest.status == 200)
            {
                div.innerHTML = httpRequest.responseText;
            }
        }
    };

}

