/**
 * klihovna JavaScriptových fcí
 *
 * @author mirek@multimedia.cz
 */


/**
 * vypíná 'probublávání' uživatelské události na dceřiínné objekty
 */
function stop_spreading(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;

	if (e.stopPropagation) e.stopPropagation();
}


/**
 * zoomovací okno s obrázkem
 */
function zoom(url, w, h, title, alt)
{
	var ww = w;
	var wh = h;
	var scrl = 'no';
	if (ww > 1000){ww = 1000; scrl = 'yes';};
	if (wh > 700){wh = 700; scrl = 'yes';};
	var l = screen.width/2-ww/2;
	var t = screen.height/2-wh/2-50;
	if (l<0) l = 0;
	if (t<0) t = 0;

	var o = window.open('','zoomwin','status=no,width='+ww+',height='+wh+',resizable=no,menubar=no,location=no,scrollbars='+scrl+',toolbar=no,left='+l+',top='+t+'');

	d = o.document;
	d.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	d.writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">');
	d.writeln('<head><meta http-equiv="Content-Type" content="text/html; charset=windows-1250"/>');
	d.writeln('<title>'+title+'</title>');
	d.writeln('<style type="text/css" media="screen">');
	d.writeln('BODY{margin: 0px; padding: 0px;}');
	d.writeln('IMG{margin: 0px; padding: 0px; border: 0px solid black; display:block;}');
	d.writeln('</style>');
	d.write('<body >');
	d.write  ('<a href="javascript:close()">');
	d.write  ('<img src="'+url+'" width="'+w+'" height="'+h+'" border="0" hspace="0" vspace="0" alt="" title="'+alt+'">');
	d.write('</a>');
	d.write('</body>')
	d.writeln('</html>');
	d.close();
	d.title = title;
	o.focus();
}


/**
 * obecná fce na otevírání popupů
 */
function popup_open(url, w, h)
{
	var ww = w;
	var wh = h;
	var l = screen.width/2-ww/2;
	var t = screen.height/2-wh/2-50;
	if (l<0) l = 0;
	if (t<0) t = 0;

	var o = window.open(url, 'popupwin', 'status=no,width='+ww+',height='+wh+',resizable=no,menubar=no,location=no,scrollbars=yes,toolbar=no,left='+l+',top='+t+'');
	o.focus();
}


/**
 * testuje, jestli je číslo float a má max povolený
 * počet des míst(0 = libovolné)
 */
function isPosFloat(s, decimals)
{
  s += '';
  if (decimals)
	var reg = new RegExp('(^[0-9]*$)|(^[0-9]*[,.]{1}[0-9]{1,'+decimals+'}$)');
  else
	var reg = new RegExp('(^[0-9]*$)|(^[0-9]*[,.]{1}[0-9]{1,}$)');
  if (!reg.test(s))
    return false;
  return true;
}



/**
 * testuje číslo na integer
 */
function isInt(s)
{
  s += "";
  if (s.indexOf('0') == 0)
    return false;
  var reg = new RegExp("^[0-9]*$");
  if (!reg.test(s))
    return false;
  return true;
}



/**
 * testuje validní email
 */
function isEMail(s)
{
	s += "";
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)");
	var r2 = new RegExp("^[a-zA-Z0-9\\-\\.]+\\@[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,4}$");
	if (r1.test(s) || !r2.test(s))
    	return false;
	return true;
}



/**
 * testuje platný telefon
 */
function isTel(s)
{
  var reg = new RegExp("^[+]{1}[0-9]{12}$|^[123456789]{1}[0-9]{8}$");
  if (!reg.test(s))
    return false;
  return true;
}



/**
 * platné řetězcové id
 */
function isCharId(s)
{
	s += "";
	//var r = new RegExp("^[a-zA-Z]{1}[a-zA-Z0-9_]{0,}$");
	var r = new RegExp("^[a-zA-Z0-9_-]{0,}$");
	if (!r.test(s))
    	return false;
	return true;
}


/**
 * obdoba number_format z php
 */
function number_format(number, decimals, dec_point, thousands_sep)
{
	var dec, i, arr;
	number *= 1;
	num = number.toFixed(decimals);

	dec = num.substr(-decimals, decimals);
	num = num.substr(0, num.length-decimals-1);

	num = num+'';

	arr = new Array('');

	for (i = 0; i < num.length; i++)
	{
		arr[i] = num.charAt(i);
	}
	arr.reverse();
	for (i = 1; i < arr.length; i++)
	{
		if (i % 3 == 0)
			arr[i] += thousands_sep;
	}

  arr.reverse();
  num = arr.join('');

	return (num + dec_point + dec);

}


// mredkj.com
// created: 2001-07-11
// last updated: 2001-09-07
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue)
{
var newOpt = new Option(theText, theValue);
var selLength = theSel.length;
theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{
var selLength = theSel.length;
if(selLength>0)
{
theSel.options[theIndex] = null;
}
}

function moveOptions(theSelFrom, theSelTo)
{

var selLength = theSelFrom.length;
var selectedText = new Array();
var selectedValues = new Array();
var selectedCount = 0;

var i;

// Find the selected Options in reverse order
// and delete them from the 'from' Select.
for(i=selLength-1; i>=0; i--)
{
if(theSelFrom.options[i].selected)
{
selectedText[selectedCount] = theSelFrom.options[i].text;
selectedValues[selectedCount] = theSelFrom.options[i].value;
deleteOption(theSelFrom, i);
selectedCount++;
}
}

// Add the selected text/values in reverse order.
// This will add the Options to the 'to' Select
// in the same order as they were in the 'from' Select.
for(i=selectedCount-1; i>=0; i--)
{
addOption(theSelTo, selectedText[i], selectedValues[i]);
}

if(NS4) history.go(0);
}


/**
 * odtranění diakritiky
 */

sdiak="áäčďéěíĺľňóôőöŕšťúůűüýřžÁÄČĎÉĚÍĹĽŇÓÔŐÖŔŠŤÚŮŰÜÝŘŽ";
bdiak="aacdeeillnoooorstuuuuyrzAACDEEILLNOOOORSTUUUUYRZ";

function bezdiak(txt)
{
	tx="";
	for(p=0;p<txt.length;p++)
	{ 
		if (sdiak.indexOf(txt.charAt(p))!=-1) 
		tx+=bdiak.charAt(sdiak.indexOf(txt.charAt(p)));
		else tx+=txt.charAt(p);
	}
	return tx;
}


/**
 * převádí normální string na str id
 */
function strId(val)
{
	var separator = '-';
	if (arguments.length > 1)
		separator = arguments[1];
		
	s = '';
	val = val.toLowerCase();
	val = bezdiak(val);
	for (i=0;i < val.length;i++)
	{
		d = val.charCodeAt(i);
		if ((d > 96 && d < 123) || (d > 47 && d < 58) || d == 45 || d == 95)
			c = val.charAt(i);
		else
			c = separator;
		s += c;
	}
	while (s.indexOf('-_') != -1)
		s = s.replace('-_', separator);
	while (s.indexOf('_-') != -1)
		s = s.replace('_-', separator);
	while (s.indexOf(separator+separator) != -1)
		s = s.replace(separator+separator, separator);
	while (s.charAt(0) == separator && s.length >= 1)
		s = s.substring(1, s.length);
	while (s.charAt(s.length - 1) == separator && s.length >= 1)
		s = s.substring(0, s.length - 1);
	
	return s;
}



/**
 * načte proměnnou prostředí pro uživatele
 * paměť na menu, otevřené větve struktury, etc.
 * TODO zatím je to jenom cookies, do budoucna tahat z db, kam to bude sypat ajax
 */
function setUserVar(userId, name, val)
{
	setCookie(name, val, 365);
}


/**
 * smaže proměnnou prostředí pro uživatele
 * TODO zatím je to jenom cookies, do budoucna tahat z db, kam to bude sypat ajax
 */
function delUserVar(userId, name)
{
	deleteCookie(name);
}



/**
 * práce s cookies na straně klienta
 */
function getCookie( name ) {
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
    return null;
  }
  if ( start == -1 ) return null;
  var end = document.cookie.indexOf( ";", len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}


function setCookie(name, value, expires, path, domain, secure) {
  var today = new Date();
  today.setTime(today.getTime());
  if ( expires ) {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );
  document.cookie = name+"="+escape( value ) +
    ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
  if ( getCookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function showCookies() {
    alert(document.cookie);
}


function switchClass(o, c1)
{
	o.className = c1;
}

