﻿// pomocne js funkcie a konstanty

var TEXT_TO_REPLACE = "<textToReplace/>";
var DELIMITER = "<delimiter/>";

function displayElement(elementId, display)
{
/// <summary>Zmena zobrazenia zadaneho elementu pomocou display. Default je jeho zobrazenie.</summary>
/// <param name="elementId" type="String" mayBeNull="true">ID elementu.</param>
/// <param name="display" type="String" mayBeNull="true">Pozadovane zobrazenie (pre CSS display). Ak je null, bude "inline".</param>
	
	var element = document.getElementById(elementId);
	if ( null == element )
		return;
	
	if ( null == display )
		display = "inline";
	element.style.display = display;
}

function hideElement(elementId)
{
/// <summary>Skrytie zadaneho elementu pomocou visibility.</summary>
/// <param name="elementId" type="String" mayBeNull="true">ID elementu.</param>
	
	showElement(elementId, "hidden");
}

function showElement(elementId, visibility)
{
/// <summary>Zmena zobrazenia zadaneho elementu pomocou visibility. Default je jeho zobrazenie.</summary>
/// <param name="elementId" type="String" mayBeNull="true">ID elementu.</param>
/// <param name="visibility" type="String" mayBeNull="true">Pozadovane zobrazenie (pre CSS visibility). Ak je null, bude "inherit".</param>
	
	var element = document.getElementById(elementId);
	if ( null == element )
		return;
	
	if ( null == visibility )
		visibility = "inherit";
	element.style.visibility = visibility;
}

function selectOptionMove(srcListId, destListId)
{
/// <summary>Presun polozky z jednoho zoznamu (html select-u) do druheho.</summary>
/// <param name="srcListId" type="String" mayBeNull="false">ID zdrojoveho zoznamu.</param>
/// <param name="destListId" type="String" mayBeNull="false">ID cieloveho zoznamu.</param>

	var src = document.getElementById(srcListId),
		dest = document.getElementById(destListId);
	if ( null == src || null == dest )
		return;
	
	if ( null == src.selectedIndex || src.selectedIndex < 0 )
	{
		if ( src.length < 1 )
			return;
			
		src.selectedIndex = 0;
	}
	
	var newOption = document.createElement("OPTION"),
		oldOption = src[src.selectedIndex];
	
	newOption.text = oldOption.text;
	newOption.value = oldOption.value;
	newOption.stlpec = oldOption.stlpec;
	newOption.className = oldOption.className;
	
	dest.options.add(newOption);
	
	var index = src.selectedIndex;
	if ( index == src.length - 1 )
		--index;
	
	src.removeChild(oldOption);
	src.selectedIndex = index;
}

function selectOptionUp(listId)
{
/// <summary>Posun aktivnej polozky hore.</summary>
/// <param name="listId" type="String" mayBeNull="false">ID zoznamu.</param>

	var list = document.getElementById(listId);
	if ( null == list )
		return;
	
	var index = list.selectedIndex;
	if ( index >= 1 )
	{
		list.selectedIndex = -1;
		
		var option = list[index];
		list.removeChild(option);
		list.insertBefore(option, list[index - 1]);
		
		list.selectedIndex = index - 1;
	}
}

function selectOptionDown(listId)
{
/// <summary>Posun aktivnej polozky hore.</summary>
/// <param name="listId" type="String" mayBeNull="false">ID zoznamu.</param>

	var list = document.getElementById(listId);
	if ( null == list )
		return;
	
	var index = list.selectedIndex;
	if ( index < list.length - 1 )
	{
		list.selectedIndex = -1;
		
		var option = list[index + 1];
		list.removeChild(option);
		list.insertBefore(option, list[index]);
		
		list.selectedIndex = index + 1;
	}
}

function getEventTarget(event)
{
/// <summary>Ziskanie targetu eventu.</summary>
/// <param name="event" type="jsEvent" mayBeNull="false">Zdrojovy js event.</param>

	return event.target ? event.target : event.srcElement;
}

function textLengthValidator_Validate(source, args) {
	/// <summary>Validácia dĺžky textu.</summary>
	/// <param name="source" type="object" mayBeNull="false">Validator.</param>
	/// <param name="args" type="object" mayBeNull="false">Argumenty validacie.</param>

	var minTextLength = source.getAttribute("MinTextLength");
	if (null != minTextLength && (null == args.Value || args.Value.length < minTextLength)) {
		args.IsValid = false;
		return;
	}

	var maxTextLength = source.getAttribute("MaxTextLength");
	if (null != maxTextLength && null != args.Value && args.Value.length > maxTextLength) {
		args.IsValid = false;
		return;
	}
	
	args.IsValid = true;
}

function jsEncode(vstup)
{
	/// <summary>Zakodovanie retazca pre pouzitie v javascript-ovej hodnote. Kodovane znaky su \ " \r \n \t.</summary>
	/// <param name="vstup">Vstupny retazec.</param>
	/// <returns>Zakodovany retazec.</returns>
	
	if ( null == vstup )
		return null;
			
	return vstup.replace(/\\/g, "\\\\").replace(/\"/g, "\\\"").replace(/\r/g, "\\r").replace(/\n/g, "\\n").replace(/\t/g, "\\t");
}

if(typeof(Sys)!=="undefined")Sys.Application.notifyScriptLoaded();