konverzny_kurz = 30.126;
euro_sign = "&#8364;";

function open_window ( url, width, height )
{
	var twidth = width;
	var theight = height;
	var scrollbars = 0;

	if(twidth > screen.width - 80)
	{
		twidth = screen.width - 80;
		scrollbars = 1;
	}
	if(theight > screen.height - 80)
	{
		theight = screen.height - 80;
		scrollbars = 1;
	}

	var pos_x = (screen.width - twidth - 10)/2;
	var pos_y = (screen.height - theight - 29)/2;

	if(typeof(source_window) == "undefined" || source_window.closed)
		source_window = window.open( url,'source_window','menubar=0,toolbar=0,location=0,scrollbars=' + scrollbars + ',screenX='+pos_x+',screenY='+pos_y+',left='+pos_x+',top='+pos_y+',width='+twidth+',height='+theight+',resizable=0');
	else source_window.location.href = url;

	source_window.focus();
}

function op ( url, width, height )
{
	open_window ( url, width, height );
}

function oo ( url, h )
{
	open_window ( url, 450, h, false, false );
}

function FormatujCislo ( cislo )
{
	cislo += '';
	x = cislo.split ( '.' );
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;

	while ( rgx.test ( x1 ) )
	{
		x1 = x1.replace ( rgx, '$1' + ' ' + '$2' );
	}

	cislo = x1 + x2;
	cislo = cislo.replace ( ".", "," );

	var dpos = cislo.indexOf ( "," );
	if ( dpos == -1 )
	{
		cislo += ",00";
	}else if ( dpos == cislo.length - 2 )
	{
		cislo += "0";
	}	

	return cislo;
}

function FormatujCenu ( cena, nosign, sk_no, dph )
{
	if (typeof dph == "undefined")
		var dph = 0;

	cena = Math.round ( cena * 100 ) / 100;

	cenas = FormatujCislo ( cena );

	if (!nosign)
		cenas += " " + euro_sign;

	if (dph > 0) {
		cena_bez_dph = cena / dph;
		cena_bez_dph = Math.round(cena_bez_dph * 100) / 100;

		dphs = FormatujCislo(cena_bez_dph);
		cenas += " (" + dphs;

		if (!nosign)
			cenas += " " + euro_sign;

		cenas += " bez DPH)";
	}

	cenas = "<strong>" + cenas + "</strong>";

	if ( !sk_no )
	{
		cenas += "&nbsp;(";
		cenas += FormatujCenuSk ( cena, nosign );
		cenas += ")";
	}

	return cenas;
}

function FormatujCenuSk ( cena, nosign )
{
	cena = Math.round ( cena * konverzny_kurz * 100 ) / 100;

	cenas = FormatujCislo ( cena );

	if ( !nosign )
	{
		cenas += " Sk";
	}

	return cenas;
}

function VyratajCenu(form, dph)
{
	var terminy = form.elements['item2[]'];

	if ( terminy == null ) return;

	var cena_spolu = 0;
	var terminyl = terminy.length;

	if ( terminyl > 0 )
	{
		for ( i=0; i<terminyl; i++ )
		{
			var termin = terminy[i];

			if ( !termin.checked )
			{
				continue;
			}

			var cena = cena_terminu[i];
			cena_spolu += cena;
		}
	}else
	{
		if ( terminy.checked )
		{
			cena_spolu = cena_terminu[0];
		}
	}

	var dest = document.getElementById ( "cena_spolu" );

	if ( dest == null ) return;

	dest.innerHTML = FormatujCenu(cena_spolu, false, true, dph);
}

function VyratajCenuKnih (form, dph)
{
	var key;
	var text = "";
	var cena_spolu = 0;
	var elementsl = form.elements.length;

	for ( var i=0; i<elementsl; i++ )
	{
		var elem = form.elements[i];

		if ( elem.name.match(/^kniha/) )
		{
			if ( elem.value == "" || isNaN ( elem.value ) )
			{
				elem.value = "";

				continue;
			}

			elem.value = Math.floor ( elem.value );

			eval ( "cena_spolu += " + elem.value + " * " + elem.name );
		}
	}

	var dest = document.getElementById ( "cena_spolu" );

	if ( dest == null ) return;

	dest.innerHTML = FormatujCenu ( cena_spolu, false, true, dph);
}

function PripravLinky ()
{
	var anchors = document.getElementsByTagName ( "a" );

	for ( var i = 0; i < anchors.length; i++ )
	{
		var anchor = anchors[i];
		var relAttribute = String(anchor.getAttribute("rel"));
		if (anchor.getAttribute("href"))
		{
			if ( relAttribute.match ( /^oo/ ) )
			{
				eval ( "anchor.onclick = function () { " + relAttribute + "; return false; }" );

				anchor.href = "javascript:void(0)";
			}
		}
	}

	var objednavka = document.getElementById ( "objednavka" );
	if ( objednavka == null ) return;
	VyratajCenu ( objednavka );
}

if (window.addEventListener) {
	window.addEventListener("load",PripravLinky,false);
} else if (window.attachEvent) {
	window.attachEvent("onload",PripravLinky);
} else {
	window.onload = function() {PripravLinky();}
}
