var clock_stamp;

function clock_output(){

 var now = new Date(clock_stamp * 1000);

 h = now.getHours();
 m = now.getMinutes();
 s = now.getSeconds();

 if(h < 10) h = "0" + h;
 if(m < 10) m = "0" + m;
 if(s < 10) s = "0" + s;

 var ___hodiny = document.getElementById('clock');
 ___hodiny.innerHTML = h + ":" + m + ":" + s;

}


function clock_update(){

 clock_stamp += 1;
 var _hodiny = document.getElementById('clock');

 if (!_hodiny) return;

 clock_output();

}
	
function clock_boot(stamp){

 clock_stamp = stamp;
 clock_output();
 setInterval("clock_update();", 1000);
    
}


function PridejPredmet(kdo,ktery){
 document.formsmile.predmet.value="RE : "+kdo+" (id: "+ktery+")";
 document.formsmile.text.focus()
}


function otevrit(id){
 el=document.getElementById(id).style;
 el.display=(el.display == 'block')?'none':'block';
}


function zkontroluj(formular){
 if(formular.infosraz.value==""){
  if(confirm("Nebyl zadán žádný text, to znamená že se žádný sraz nekoná a vymažou se všechny přihlášky, pokračovat?")){return true;}
  else{return false;}
 }
}


function insertTag(tag, smile){
	
	var textarea = document.forms["formsmile"].elements["text"];
	var startTag = '[' + tag + ']';
	var endTag = '[/' + tag + ']';
  var tagText = getTextareaSelection();	
	
	if (typeof smile == 'undefined') var smile = false; else var smile = true;
	if (smile){
   endTag = '';
   startTag = '**'+ tag +'*';
  }
		
	var text2insert = startTag + tagText + endTag;
	
	//vložení:
	textarea.focus();
	
	if (typeof textarea.selectionStart != 'undefined') {
		var start = textarea.selectionStart;
		var end = textarea.selectionEnd;
		
		textarea.value = textarea.value.substr(0, start) + text2insert + textarea.value.substr(end);
		
		//nastaveni kurzoru na spravné misto / vybrani pozadovaneho textu
		if (typeof selectionStart != 'undefined' && typeof selectionEnd != 'undefined') {
			textarea.selectionStart = start + selectionStart;
			textarea.selectionEnd = start + selectionEnd;
		} else {
			if (tagText == '') {
				//skoceni mezi tagy
				textarea.selectionStart = start + startTag.length;
				textarea.selectionEnd = start + startTag.length;
			} else {
				//skoceni za posledni tag
				textarea.selectionStart = start + text2insert.length;
				textarea.selectionEnd = start + text2insert.length;
			}
		}
	}
	else if (typeof document.selection != 'undefined') {
		var range = document.selection.createRange();
		range.text = text2insert;
		
		//nastaveni kurzoru na spravné misto / vybrani pozadovaneho textu
		if (typeof selectionStart != 'undefined' && typeof selectionEnd != 'undefined') {
			range.moveStart('character', -text2insert.length + selectionStart);
			range.moveEnd('character', -text2insert.length + selectionEnd);
		} else {
			if (tagText == '') {
				//skoceni mezi tagy
				range.move('character', -(endTag.length));
			}
		}
		range.select();
	}

	//unset promenych
	delete selectionStart;
	delete selectionEnd;
}


function getTextareaSelection(){

	var textarea = document.forms["formsmile"].elements["text"];
	
	if (typeof textarea.selectionStart != 'undefined') {
		var selection = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
	} else if (typeof document.selection != 'undefined') {
		var selection = document.selection.createRange().text;
	}
	return selection;
	
}