﻿/*
 * tools.js Version 1.02 (Jan Sugarek)
 * - zbierka zakladnych pouzivanych javascript funkcii
 *
 * v1.02 (19.7.2008)
 * - doplnena funkcia htmlspecialchars s rovnakou funckou ako rovnomenna php funkcia
 *
 * v1.01 (16.7.2008)
 * - doplnena funkcia pre serializaciu XML
 */

// zo zadaného eventu vytiahne objekt ktory event zavolal
function event_get_target(e)
{
	var target = null;
	if(e.target) target = e.target;
	else if(e.srcElement) target = e.srcElement;
	if(target && target.nodeType == 3) target = target.parentNode;

	return target;
}

/*
 * Funckia zakaze oznacovanie na zadanom objekte
 */
function disable_select(elem)
{
	elem.onselectstart = function() { return false; };
	elem.unselectable = "on";
	elem.style.MozUserSelect = "none";
	elem.style.cursor = "default";
}

/*
 * Funkcia povoli oznacovanie na zadanom objekte
 */
function enable_select(elem)
{
	elem.onselectstart = function() { return true; };
	elem.unselectable = "off";
	elem.style.MozUserSelect = null;
	elem.style.cursor = "auto";
}

/*
 * Funckia prevedie retazec v tvare
 * string1-string2-string3...
 * na retazec string1String2String3...
 */
function jsformat_stylename(style_name)
{
	var name_array = style_name.split("-");
	style_name = "";

	for(var i = 0; i < name_array.length; i++){
		if(i != 0 && name_array[i][0] != ""){
			name_array[i] = name_array[i].substr(0,1).toUpperCase() + name_array[i].substr(1);
		}
		style_name += name_array[i];
	}

	return style_name;
}


/*
 * Vrati aktualnu hodnotu stylu nastaveneho pomocou css
 * a takisto aj pomocou attributu style
 *
 * elem - referencia na objekt
 * style_name - nazov stylu v pomlckovom tvare!!!
 *
 * Poznamka: Nefunguje pre vsetky styly
 */
function get_style(elem,style_name)
{
	if(elem.currentStyle)
		style_value = elem.currentStyle[jsformat_stylename(style_name)];
	else if(window.getComputedStyle)
		style_value = document.defaultView.getComputedStyle(elem,null).getPropertyValue(style_name);

	return style_value;
}

/*
 * Funckia odstrani medzery na zaciatku a na konci retazca
 */
function trim(s){
	return s.replace(/(^\s+)|(\s+$)/g, "")
}

/*
 * Funkcia vrati aktualny cas "v uplnom" zapise to znamena 1 = 01
 */
function get_fulltime(){
	var time = new Date();
	var time_string = "";

	if(time.getHours() < 10) time_string += "0";
	time_string += time.getHours() + ":";

	if(time.getMinutes() < 10) time_string += "0";
	time_string += time.getMinutes() + ":";

	if(time.getSeconds() < 10) time_string += "0";
	time_string += time.getSeconds();

	return time_string;
}

/*
 * Funkcia hlada prveho potomka typu element
 */
function firstChild_elem(elem){
	var child = elem.firstChild;
	while(child && child.nodeType != 1) child = child.nextSibling;

	return child;
}

/*
 * Funkcia hlada dalsieho surodenca typu element
 */
function nextSibling_elem(elem){
	var next = elem.nextSibling;
	while(next && next.nodeType != 1) next = next.nextSibling;

	return next;
}

/*
 * Funkcia hlada predosleho surodenca typu element
 */
function previousSibling_elem(elem){
	var previous = elem.previousSibling;
	while(previous && previous.nodeType != 1) previous = previous.previousSibling;

	return previous;
}

/*
 * Funkcia hlada posledneho surodenca typu element
 */
function lastSibling_elem(elem){
	var last = elem.parentNode.lastChild;

	while(last && last.nodeType != 1) last = last.previousSibling;

	return last;
}

/*
 * Funkcia serializuje zadany node a jeho potomkov do textu
 */
function xml_to_string(elem)
{
    try
    {
        return (new XMLSerializer()).serializeToString(elem);
    }
    catch(e)
    {
	    return elem.xml;
	}
}

/*
 *	funkcia funguje ako znama php funkcia htmlspecialchars
 *  rozdiel je v tom ze nedovoluje vyber charsetu a to co ma robit s quote " a '
 *  - najdene na nete - toto je original
 */
function htmlspecialchars(text) {
	text = text.replace(/&/g,"&amp;")
	text = text.replace(/\"/g,"&quot;")
	text = text.replace(/\'/g,"&#039;")
	text = text.replace(/</g,"&lt;")
	text = text.replace(/>/g,"&gt;")

	return text
}

function set_style(e, style)
{
	var e = e ? e : window.event;
	var target = event_get_target(e);

	if(target)
	{
		target.className = style;
	}
}

function showCaution(message){
	return confirm(message);
}