// Setridi pole podle velikosti
function numsort (a, b)
{
	return a > b ? 1 : a == b ? 0 : -1;
}

// Prida zpetne lomitko pred uvozovky a apostrofy
function addslashes (input)
{
	var output = "";
	for (var i = 0; i < input.length; i++)
	{
		if (input.charAt(i) == "'" || input.charAt(i) == '"')
		{
			alert (input.charAt(i));
			output += '\\';
		}
		output += input.charAt(i);
	}
	return output;
}

// Odebere mezery z leve strany retezce
function ltrim (str)
{
	while (str.substring(0, 1) == ' ')
	{
		str = str.substring(1, str.length);
	}
	return str;
}

// Odebere mezery z prave strany retezce
function rtrim (str)
{
	while (str.substring(str.length - 1, str.length) == ' ')
	{
		str = str.substring(0, str.length - 1);
	}
	return str;
}

// Odebere mezery z obou koncu retezce
function trim (str)
{
	return rtrim (ltrim (str));
}

// Zjisti zda je promenna polem
function isArray (anObject)
{
	return isObject (anObject) && anObject.constructor == Array;
}

// Zjisti zda je promenna logickou hodnotou
function isBoolean (anObject)
{
	return typeof anObject == 'boolean';
}

// Zjisti zda je promenna definovana
function isDefined (anObject)
{
	return typeof a != 'undefined';
} 

// Zjisti zda je promenna prazdna
function isEmpty (anObject)
{
	var i;
	if (isObject (anObject))
	{
		for (i in anObject)
		{
			if (isUndefined (anObject[i]) && isFunction (anObject[i]))
			{
				return false;
			}
		}
	}
	return true;
}

// Zjisti zda je promenna funkci
function isFunction (anObject)
{
	return typeof anObject == 'function';
}

// Zjisti zda je promenna null
function isNull (anObject)
{
	return typeof anObject == 'object' && !anObject;
}

// Zjisti zda je promenna cislem
function isNumber (anObject)
{
	return typeof anObject == 'number' && isFinite (anObject);
}

// Zjisti zda je promenna objektem
function isObject (anObject)
{
	return (anObject && typeof anObject == 'object') || isFunction (anObject);
}

// Zjisti zda je promenna retezcem
function isString (anObject)
{
	return typeof anObject == 'string';
}

// Vraci hexadecimalni reprezentaci barvy z urciteho intervalu, dle procent a pomeru dilu
function getColorFromScale (scale, percentage, portions)
{
	if (scale.length == 1) // V palete je jen jedna barva
	{
		return str_pad(scale[0].toString(16), 6, 0, -1); // Vracim barvu (6 hexadecimalnich znaku)
	}
	else
	{
		if (getColorFromScale.arguments.length < 3) // Pokud neni zadan pomer dilu, jsou dily stejne dlouhe
		{
			var index = Math.floor(percentage * (scale.length - 1)); // Poradi aktualniho useku barevneho prechodu (shodne s indexem pocatecni barvy)
			var percentPerInterval = 1 / (scale.length - 1); // Procent na aktualni usek barevneho prechodu
		}
		else // Ruzne dlouhe dily
		{
			var portionSum = eval(portions.join("+")); // Celkova suma pomeru useku
			for (var sum = 0, i = 0; i < portions.length; i++)
			{
				sum += portions[i]; // Sectu delky vsech predchozich intervalu k aktualnimu
				if (percentage < sum / portionSum)
				{
					var index = i; // Poradi aktualniho useku barevneho prechodu (shodne s indexem pocatecni barvy)
					break;
				}
			}
			var percentPerInterval = portions[index] / portionSum; // Procent na aktualni usek barevneho prechodu
		}
		var percentPerActual = percentage % percentPerInterval / percentPerInterval; // Procenta soucasneho intervalu
		var actualColor = 0; // Cerna neovlivni vysledek, ve kterem se pouziva bitove OR
		for (var i = 0; i < 3; i++) // Tri pruchody, pro kazdou slozku barvy jeden
		{
			var mask = 0xFF << 8 * (2 - i); // Maska pro slozku barvy
			if ((scale[index] & mask) < (scale[index + 1] & mask)) // Porovnam zda je pocatecni barva nizsi nez koncova (pouze pro slozku urcenou maskou)
			{
				var actualSize = (scale[index + 1] - scale[index]) & mask; // Celkova sirka intervalu vymaskovane barevne slozky * procenta sirky intervalu, zaokrouhlene
				actualColor |= (scale[index] + Math.round(actualSize * percentPerActual)) & mask; // Sirka se pricte k pocatku vymaskovane barevne slozky
			}
			else
			{
				var actualSize = (scale[index] - scale[index + 1]) & mask; // Celkova sirka intervalu vymaskovane barevne slozky * procenta sirky intervalu, zaokrouhlene
				actualColor |= (scale[index] - Math.round(actualSize * percentPerActual)) & mask; // Sirka se odecte od pocatku vymaskovane barevne slozky
			}
		}
		return str_pad(actualColor.toString(16), 6, 0, -1); // Vracim barvu (6 hexadecimalnich znaku)
	}
}


// Doplni retezec str na delku len za pomoci znaku pad
function str_pad (str, len, pad, side)
{
	var side2 = 0; // Pouzije se pri side == 0
	if (str_pad.arguments.length < 3)
	{
		pad = ' '; // Predvoleny znak pro doplnovani je mezera
	}
	if (str_pad.arguments.length < 4)
	{
		side = 1; // Bude se doplnovat zprava (-1 je zleva a 0 je z obou stran)
	}
	
	if (isNaN (len))
	{
		alert ('Ve funkci str_pad musí být argument len číselný!');
		return false;
	}
	str = str.toString();
	
	while (str.length < len)
	{
		side < 0 || side2 < 0 ? str = pad + str: side > 0 || side2 > 0 ? str += pad : side2 = -1; // Doplni znak zleva, zprava, nebo stridave z obou stran
		side2 = - side2;
	}
	return str;
}

// Funkce na otevreni okna
openWin.win = window.myWin; // Ukazatel na okno
function openWin (link, target)
{
	if (openWin.win != null && !openWin.win.closed) // Okno existuje a nebylo zavreno
	{
		openWin.win.focus(); // Presunu zamereni
		openWin.win.location.href = link.href; // Nastavim url stranky
	}
	else
	{
		openWin.win = window.open(link.href, target); // Otevru okno
		if (openWin.win == null || typeof(openWin.win) == "undefined") // Okno se nepodarilo otevrit
		{
			return false;
		}
		else
		{
			link.target = target;
			openWin.win.focus();
		}
	}
	return openWin.win;
}


// Moje alert funkce
// string text
// - zobrazovany text
// boolean showIcon
// - zobrazit ikonu
function _alert (text, showIcon)
{
	var tmp = window.showModalDialog("../scripts/dialog.html", "alert;" + text + ";;" + (showIcon), "dialogWidth:450px;dialogHeight:250px;scroll:1;resizable:1");
	if (tmp != null)
	{
		return tmp;
	}
	else
	{
		return alert (text);
	}
}

// Moje confirm funkce
// string text
// - zobrazovany text
// string defaultText
// - predvoleny text
function _confirm (text)
{
	var tmp = window.showModalDialog("../scripts/dialog.html", "confirm;" + text + ";;true", "dialogWidth:450px;dialogHeight:250px;scroll:1;resizable:1");
	if (tmp != null)
	{
		return tmp;
	}
	else
	{
		return confirm (text);
	}
}

// Moje prompt funkce
// string text
// - zobrazovany text
// string defaultText
// - predvoleny text
function _prompt (text, defaultText)
{
	if (_prompt.arguments.length < 2)
	{
		defaultText = "";
	}
	
	var tmp = window.showModalDialog("../scripts/dialog.html", "prompt;" + text + ";" + defaultText + ";true", "dialogWidth:450px;dialogHeight:250px;scroll:1;resizable:1");
	if (tmp != null)
	{
		return tmp;
	}
	else
	{
		return prompt (text, defaultText);
	}
}

// Moje password funkce
// string text
// - zobrazovany text
function _password (text, alertText)
{
	if (_password.arguments.length < 2)
	{
		alertText = "";
	}
	
	var tmp = window.showModalDialog("../scripts/dialog.html", "password;" + text + ";" + alertText + ";true", "dialogWidth:450px;dialogHeight:250px;scroll:1;resizable:1");
	if (tmp != null)
	{
		return tmp;
	}
	else
	{
		return prompt (text, defaultText);
	}
}
