function ajaxHttpRequest(page, id)
{

	  var httpRequest;
	  var httpResponse;
    
    if(typeof window.ActiveXObject != 'undefined')
    {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {  
        httpRequest = new XMLHttpRequest();
    }
    httpRequest.open("GET", page, true);
    httpRequest.onreadystatechange= function () 
    {
        processRequest(httpRequest, id);
    };
    httpRequest.send(null);
}


function processRequest(httpRequest, id) 
{
    if (httpRequest.readyState == 4)
    {
        if(httpRequest.status == 200)
        {
            if (typeof id == 'string') 
            {
                field = id.split('~');
				for(i = 0; i < field.length; i++)
				{
					document.getElementById(field[i]).innerHTML = httpRequest.responseText;
					document.getElementById(field[i]).value = httpRequest.responseText;
				}
                //document.getElementById(id).innerHTML = httpRequest.responseText;
            }
            if (typeof id == 'function') 
            {
              id(httpRequest.responseText);
            }
        }
        else
        {
            alert("Chyba pri nacitani stanky " + httpRequest.status +" : "+ httpRequest.statusText);
        }
    }
    else
    {
        if (typeof id == 'string' && id != '') 
        {
            document.getElementById(id).innerHTML = '';
        }
    }
} 
function ajaxScript(modul,file)
{
    var page;
    if(file)
	{
    	page = "script/"+file+".php?modul="+modul;
	}
    else
    {
    	page = "script/soubor.php?modul="+modul;
    }
    ajaxHttpRequest(page,'soubor')
}


function addOption(fieldName,listName){
	var names, dest;
	names = document.getElementById("src"+listName);
	dest = document.getElementById("dest"+listName);
	for(i = 0; i < names.length; i++){
		if(names.options[i].selected){
			opt = new Option(names.options[i].text,names.options[i].value);
			optExist = false;
			for(j = 0; j < dest.length; j++){
				if(opt.value == dest.options[j].value){
					optExist = true;
					break;
				}
			}
			if(!optExist)
				dest.options[dest.length] = opt;
		}
	}
	saveOption(fieldName, dest);
}
function removeOption(fieldName, listName){
	var dest;
	dest = document.getElementById("dest"+listName);
	for(i = dest.length-1; i >=0; i--){
		if(dest.options[i].selected){
			dest.options[i] = null;
		}
	}
	saveOption(fieldName, dest);
}

function removeAllOptions(fieldName){
	var names, dest;
	names = document.getElementById("src"+listName);
	dest = document.getElementById("dest"+listName);
	for(i = dest.length-1; i >=0; i--){
		dest.options[i] = null;
	}
	saveOption(fieldName, dest);
}

function saveOption(fieldName, dest){
	var txt = "";
	for(i = 0; i < dest.length-1; i++){
		txt += dest.options[i].value + ",";
		
	}
	txt += dest.options[i].value;
	document.zadani[fieldName].value = txt;
}

function js_text_format(tag, fieldname) {
	
	var startTag = "<"+tag+">";
	var endTag = "</"+tag+">";
    js_format_process(startTag, endTag, fieldname);
}

function js_insert_link(tag, fieldname)
{
	x = prompt("URL:", "");
	var startTag = "<"+tag+" "+x+",";
	var endTag  = "/>";
	js_format_process(startTag, endTag, fieldname);
}
function js_insert_mail(fieldname)
{
	x = prompt("E-mail:", "");
	re = new RegExp("^[^@]+@[^.]+\..+$");
    if (!re.test(x))
    {
      alert("NenĂ­ sprĂˇvnĂˇ adresa elektronickĂ© poĹˇty!");
	  return;
	}
	
	var startTag = "<m "+x+"/>";
	var endTag  = "";
	js_format_process(startTag, endTag, fieldname);
}
function js_insert_picture(imagePath)
{
	if(document.getElementById('edit'))
	{
		document.getElementById('edit').contentWindow.document.execCommand('insertimage', false, imagePath);
		akce('zobrazit_zaznam');
		validateField('', '', '', '', '', '', '', 'body+Text:');
	}
	else
	{
		var startTag = "<img src="+imagePath+">";
		var endTag = "";
	    var fieldname = "body";
	    js_format_process(startTag, endTag, fieldname);
	}
}

function js_format_process(startTag, endTag, fieldname)
{
	var myField=document.forms['zadani'].elements[fieldname];
	myField.focus();
    //IE support
    if (document.selection) {
        sel = document.selection.createRange();
        sel.text = startTag+sel.text+endTag;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
                      + startTag 
                      + myField.value.substring(startPos, endPos)
                      + endTag
                      + myField.value.substring(endPos, myField.value.length);
        myField.selectionStart = startPos + startTag.length;
        myField.selectionEnd = endPos + startTag.length;
    } else {
        myField.value += myValue;
    }
}




