//otwórz stronę
function loaddoc(link) {
var link;
window.location.href=link;
}

//otwarcie zdjęcia w nowym oknie
function new_img(link, width, height) {
var link;
var width;
var height;
window.open('window.php?bimage='+link+'', 'foto' ,'left=20,top=20, width='+width+' , height='+height+', menubar=0, resizable=1');
}

//otwarcie danych w nowym oknie
function new_win(link, width, height) {
var link;
var width;
var height;
window.open(link, 'win' ,'left=20,top=10, width='+width+' , height='+height+', menubar=0, resizable=1, scrollbars=1');
}

//oblicza wartosc calego zamowienia w zaleznosci od wybranej formy zaplaty (przelew/pobranie)
function payment() 
{
total = 0;

	dostawca = document.formularz.dostawca.value;
	pobranie = pobr_tab[dostawca];
	cena = cena_tab[dostawca];
	termin = term_tab[dostawca];

	if (document.formularz.gratis.value!=1) //jeżeli nie jest przesyłka gratis
	{
		//z pobraniem 
		if (document.formularz.zaplata.value == 2)
		{
		total = razem + pobranie + cena;
		pack = pobranie + cena;
		}
		else //bez pobrania
		{
		total = razem + cena;
		pack = cena;
		}
	document.formularz.total.value = total.toFixed(2);
	document.formularz.pack.value = pack.toFixed(2);
	}
	document.formularz.term_dost.value = termin;
}


//wstawia dzisiejszą datę wysłania zamówienia
function wstaw_date()
{
	for (i=1; i<=y; i++)
	{
		if (document.forms[i].elements['status'].options[1].selected==true)
		{
		var now = new Date();
		if (document.forms[i].elements['day_w'].value == "") document.forms[i].elements['day_w'].value = now.getDate()
		if (document.forms[i].elements['month'].value == "") document.forms[i].elements['month'].value = now.getMonth()+1
		if (document.forms[i].elements['year'].value == "") document.forms[i].elements['year'].value = now.getYear()
		}

		if (document.forms[i].elements['status'].options[0].selected==true || document.forms[i].elements['status'].options[3].selected==true)
		{
		document.forms[i].elements['day_w'].value = "";
		document.forms[i].elements['month'].value = "";
		document.forms[i].elements['year'].value = "";
		}
	}
}

// by Jan Bobrowski
function draglist(id)
{
	this.root = document.getElementById(id)
	if(!this.root) return
	this.root.draglist = this
	this.scan()
}

draglist.prototype.scan = function() {
	var o = this.root.firstChild
	this.items = []
	for(var i=0; o; o=o.nextSibling) {
		if(!o.tagName) continue
		o.onmousedown = this.down
		o.dl_idx = i
		this.items[i++] = o
		o.style.cursor = "default"
	}
}

draglist.prototype.yof = function(e) {
	var y = e.offsetTop
	while(e = e.offsetParent)
		y += e.offsetTop
	return y
}

draglist.prototype.down = function(e) {
	if(!e) e = window.event
	var o=this, c,l,y

	c = o.parentNode
	l = c.draglist
	if(l.items.length<=1) return true
	document.dl_dragged = o
	l.rooty = l.yof(c)
	y = l.yof(o)
	l.prevy = e.clientY
	l.handy = e.clientY - y + c.scrollTop
	l.origy = y - l.rooty

	document.onmousemove = l.move
	document.onmouseup = l.up
	return false
}

draglist.prototype.posfor = function(y) {
	var o,a=0,b=this.items.length,i=0
	y += this.rooty
	for(;;) {
		o = this.items[i]
		if(y < this.yof(o) + o.offsetHeight/2) b = i
		else a = i
		i = a+b>>1
		if(i == a) break
	}
	return b
}

draglist.prototype.move = function(e) {
	if(!e) e = window.event
	var o,c,l,y,m,d,i

	o = this.dl_dragged
	c = o.parentNode
	l = c.draglist
	y = e.clientY - l.rooty - l.handy

	m = c.offsetHeight-o.offsetHeight
	d = e.clientY - l.prevy
	l.prevy += d
	if(d < 0) {
		if(y < 0) c.scrollTop += y
	} else {
		if(y > m) c.scrollTop += y - m
	}
	y += c.scrollTop

	i = l.posfor(y + (d<0 ? 0 : o.offsetHeight))
	if(i<o.dl_idx || i>o.dl_idx+1) {
		c.removeChild(o)
		if(i < l.items.length)
			c.insertBefore(o, l.items[i])
		else
			c.appendChild(o)
		l.origy = l.yof(o) - l.rooty - (parseInt(o.style.top)||0)
		l.scan()
	}

	if(y<0) y=0; else {
		m = c.scrollHeight-o.offsetHeight
		if(y > m) y = m
	}
	o.style.top = (y - l.origy)+"px"

	return false
}

draglist.prototype.up = function(e) {
	if(!e) e = window.event
	var o,c,l

	o = this.dl_dragged
	c = o.parentNode
	l = c.draglist
	if(l.origy+o.offsetHeight > c.scrollTop+c.offsetHeight)
		c.scrollTop = l.origy+o.offsetHeight-c.offsetHeight
	if(l.origy < c.scrollTop)
		c.scrollTop = l.origy

	this.dl_dragged = null
	this.onmousemove = null
	this.onmouseup = null

	o.style.top = "0px"
	return false
}

//pokazuje formularz z danymi do faktury
function show_form()
{
	if (document.formularz.elements['change'].checked == true)
	{
	document.getElementById('d1').style.display="block";
	}
	else
	{
	document.getElementById('d1').style.display="none";
	}
}

//w zależnosci od wybranej kategorii produktu pokazuje odpowiedni select z podgrupami
function zmien_kat()
{
	temp = 0;
	//podkategorie
	for (i=0; i<kategorie.length; i++)
	{
	kk = kategorie[i];
	z = 'nr_'+kk;
	p = document.getElementById(z)
	
		if (document.formularz.elements['kat'].value == kk)
		p.style.display="block";
		else
		{
		p.style.display="none";
		document.formularz.elements['podgr_'+kk].value = '';
		}

		//podkategorie2
		for (j=0; j<podkategorie.length; j++)
		{
			pk = podkategorie[j];
			y = 'nr2_'+pk;
			
			r = document.getElementById(y)
			if (document.formularz.elements['podgr_'+kk].value == pk)
			{
				r.style.display="block";
				temp = pk;
			}
			else //wykasuj wszystkie divy drugiego poziomu za wyjatkiem tego ktory zostal znaleziony
			{
				if (temp!=pk)
				{
				r.style.display="none";
				document.formularz.elements['podgr2_'+pk].value = ''
				}
			}
			
		}

	}
}
