//--------------------------------------------------------------------
// v1.0
function zaokraglij( liczba )
{
	return Math.round( liczba * 100 ) / 100 ;
}
//-----------------------------------------------------------------------
// v1.0
function odwrotnyVAT( vat )
{
	return zaokraglij1( vat / ( 1 + vat) , 4) ;
}
//--------------------------------------------------------------------
// v1.0
function zaokraglij1( liczba , miejsce )
{   // miejsce - do ktorego miejsca po przecinku zaokraglic
	// zak�adam, �e miejsce jest liczb� ca�kowit�

	var mnoznik = 1 ;

	for( var i = 0 ; i < miejsce ; i++)
	{                // 10 ^ miejsce
		mnoznik *= 10 ;
	}

	return Math.round( liczba * mnoznik ) / mnoznik ;
}
//-----------------------------------------------------------
// v1.0
function kwota( liczba )
{

	var tekst_kwoty  = Math.floor( liczba ) + '.'  ;  // obetnij cz�� po przecinku

	liczba      = zaokraglij(liczba * 100)          ;
	var reszta   = liczba % 100 ;     // reszta z dzielenia

	if( reszta == 0)
		return tekst_kwoty + '00'  ;

	if( reszta >= 10)
		return tekst_kwoty + reszta ;

	if( reszta < 10 )
		return tekst_kwoty + '0' + reszta ;

	return '' ;

}

//-------------------------------------------------------------------
// v1.0
function formatuj(tekst , szerokosc , wyrownanie)
{
	tekst = tekst + '' ;
	if( tekst.length >= szerokosc)
	{
		return tekst.substring(0 , szerokosc ) ;
	}

	if( wyrownanie == 'do_lewej' )
		return tekst + spacje( szerokosc - tekst.length ) ;

	if( wyrownanie == 'do_prawej' )
		return spacje( szerokosc - tekst.length) + tekst ;

	if( wyrownanie == 'wysrodkuj' )
	{
		if( ((szerokosc - tekst.length) % 2) == 0 )
			return spacje( (szerokosc - tekst.length) / 2 )
			+ tekst
			+ spacje( (szerokosc - tekst.length) / 2 )  ;
		else
			return spacje( (szerokosc - tekst.length) / 2 )
			+ tekst
			+ spacje( ((szerokosc - tekst.length) / 2) + 1 )   ;

	}

	return tekst ;    // nieprawidlowe wyr�wnanie
}
//----------------------------------------------------------------
// v1.0
function spacje( ile )
{
	var odstep = '' ;

	for( var i = 1 ; i <= ile ; i++ )
		odstep += ' ' ;

	return odstep ;
}

function kontekstStart()
{
	if( (self != top) || (document.location.href.substring(0,22) != "http://www.zabawki.pl/"))
		//	if(self != top)
		return ;
	else
		document.location.href = '/' ;
		
}

//-------------------------------------------------------------------
function kontekst( startowa , document , ramka )
{
	// v. 1.2

	if( (self != top) || (document.location.href.substring(0,22) != "http://www.zabawki.pl/"))
		return ;



	var poz ;
	var search = document.location.search ;
	var pytanie ;

	if((poz = search.indexOf('&all')) != -1) 
		pytanie = search.substring(0 , poz) + search.substring(poz + 4 , search.length) ;
	else
		pytanie = search ;
	
	document.location.href = startowa + '?url=' 
	+ escape(document.location.pathname + pytanie)
	+ '&ramka=' + ramka ;

}

/*
    Funkcje zwi�zane z cookie
    Piotr Ptak 
    28.05.2006
 */

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 cookieString = name + "=" +escape(value) +
	( (expires) ? ";expires=" + expires.toGMTString() : "") +
	( (path) ? ";path=" + path : "") +
	( (domain) ? ";domain=" + domain : "") +
	( (secure) ? ";secure" : "");
	document.cookie = cookieString;
}

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


function przewijaj(doc,id,do_przodu)
{
	div = doc.getElementById('div' + id) ;
	img = doc.getElementById('lupa' + id) ;
  
	if(div.max > -1)
	{
		if(do_przodu)
		{
			if(div.aktualny + 1 > div.max)
				div.aktualny = 0 ;
			else
				div.aktualny += 1;
		}
		else
		{
			if(div.aktualny - 1 < 0)
				div.aktualny = div.max ;
			else
				div.aktualny -= 1 ;
		}
    
		div.style.backgroundImage = "url('" + div.tla[div.aktualny] + "')" ;
		img.style.display = div.maxy[div.aktualny] ? "inline" : "none" ;
	}
	else
	{
		img.style.display = "none" ;
	}
  
  
  
    
  
//  alert(div.style.backgroundImage) ;
}

function otworzObr(doc,id)
{
	var div = doc.getElementById('div' + id) ;

	//  alert('' + div.aktualny) ;
  
	window.open('obrazekMax.jsp?kod=' + div.kod + '&nr=' + div.aktualny , '' + id , 'menubar=no,toolbar=no,status=yes,titlebar=no,scrollbars=no');
}


function przewinBox(y, img, id)
{
	var r   = 'rect(0px,130px,' + y + 'px,0px)';
	obj     = document.getElementById('d_' + id);

	if (y == 0)
	{
		obj.style.display='block';
	}

	obj.style.clip = r ;

	if (y < 130)
	{
		var fun = 'przewinBox(' + (y + 5) +',\"' + img + '\",\"' + id + '\")';
		setTimeout(fun,20);
	}
	else
	{
		obj.style.display='none';
		obj     = document.getElementById ('img_' + id );
		obj.src = img ;
	}
}

//function rozjasnianieOk(idbox)
//{
//	var box = document.getElementById(idbox) ;
//	box.rozjasnij = true ;
//}

function ustawOpacity(obj,p)
{

	//	if(obj.style.opacity != null)
	//	{
	//		var stare = parseFloat(obj.style.opacity) ;

	//		if( isNaN(stare))
	//		{
	//			alert('blad parsowania: ' + obj.style.opacity) ;
	//		}
	//		else if (Math.abs(stare - p) > 0.1)
	//		{
	//			alert('stare: ' + stare + ' != nowe: ' + p) ;
	//		}
	//	}

	obj.style.opacity = p ;
	obj.style.filter = 'alpha(opacity=' + Math.round(p * 100) + ')' ;
}

function rozjasnij(idbox,p)
{
	var box = document.getElementById(idbox);
	var img = document.getElementById('imgbox_' + idbox) ;

	if((box == null) || (img == null))
	{ // tego boxa już nie ma
		return ;
	}

	if(! img.complete)
	{ // box jest ale obrazek nie załądowany -nie ma zgody na rozjaśnianie
		setTimeout('rozjasnij("' + idbox + '" , ' + p + ')' , 300) ;
		return ;
	}

	if(p == 0.0)
	{
		box.style.zoom = '1' ;
		box.style.display = 'block' ;
		ustawOpacity(box,p) ;
	}

	p = zaokraglij1(p + 0.05 , 2) ;

	ustawOpacity(box,p) ;
	//	box.style.opacity = p ;
	//	box.style.filter = 'alpha(opacity=' + Math.round(p * 100) + ')' ;

	if(p >= 1)
	{
		return ;
	}
	
	setTimeout('rozjasnij("' + idbox + '" , ' + p + ')' , 100) ;

}

function bladBox()
{
//	alert('bladBox') ;
// na razie nic nie robi
}



function ustawCookie(name,value,days)
{
	var expires ;

	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else
	{
		expires = "";
	}

	document.cookie = name + "=" + value + expires ; //  + "; path=/";
}


function czytajCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ')
		{
			c = c.substring(1,c.length);
		}

		if (c.indexOf(nameEQ) == 0)
		{
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

function usunCookie(name)
{
	ustawCookie(name,"",-1);
}



/*
function ustawCookie(cookieName,cookieValue,nDays)
{
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0)
	{
		nDays=1;
	}
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)
	+ ";expires="+expire.toGMTString();
}
*/

function ustawOpcje(wartosc,option)
{
	for(var i = 0 ; i < option.options.length ; i++)
	{
		if(option.options[i].value == wartosc)
		{
			option.selectedIndex = i ;
			break ;
		}
	}
}


function wezWartoscRadio(radio)
{
	for(var i = 0 ; i < radio.length ; i++)
	{
		if(radio[i].checked)
		{
			return radio[i].value ;
		}
	}

	return null ;
}

