function updateTransportCost( id )
{
    var poleSpan = document.getElementById( 'kosztTransportu' );
    var opcjaTransportu = id;
    var sumaKoszyka = document.getElementById( 'sumaKoszyka' );
    var sumaZawartosci = document.getElementById( 'sumaFormularza' ).value;
    var sposobTransportu = document.getElementById( 'sposobTransportu' );

    if( opcjaTransportu == '0' )
    {
        poleSpan.style.display = 'none';
        sumaKoszyka.innerHTML = sumaZawartosci;
        sposobTransportu.value = 0;
    }
    else
    {
        sposobTransportu.value = opcjaTransportu;
        poleSpan.style.display = 'inline';
        poleSpan.innerHTML = 'Cena: ' + kosztTransportu[opcjaTransportu] + ' pln';
        Koszt = Number(sumaZawartosci) + Number(kosztTransportu[opcjaTransportu]);
        KosztTotal = new Number( Koszt );
        sumaKoszyka.innerHTML = KosztTotal.toFixed( 2 );
    }
}

function checkPaymentType( wartosc, koszt )
{
    var poleSelect = document.getElementById( 'pay_type' );
    var poleZagla = document.getElementById( 'infoZagiel' );
    var suma = document.getElementById( 'sumaFormularza' ).value;
    var total = document.getElementById( 'sumaKoszyka' );
    var kosztPrzesylki = document.getElementById( 'koszt_platnosci' );

    if( koszt > 0 )
    {
        kosztPrzesylki.innerHTML = koszt.toFixed( 2 ) + " pln<br /><br />";
    }
    else
    {
        kosztPrzesylki.innerHTML = "";
    }

    var Koszt = new Number( Number(suma) + Number(koszt) );
    total.innerHTML = Koszt.toFixed( 2 );

    if( wartosc == "pla" )
    {
        poleSelect.disabled = "";
    }
    else
    {
        poleSelect.disabled = "disabled";
    }

    if( wartosc == "rz" )
    {
        poleZagla.style.display = 'block';
    }
    else
    {
        if( poleZagla )
        {
            poleZagla.style.display = 'none';
        }
    }
}

function policzRate( koszyk )
{
    window.open('http://www.zagiel.com.pl/kalkulator/index_smart.php?action=getklientdet_si_rata&shopNo=01157536&goodsValue='+koszyk,
                'Policz_rate',
                'width=630,height=500,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
}

function zagielRegulamin()
{
    window.open('http://www.zagiel.com.pl/kalkulator/jak_kupic.html',
                'nowe_okno',
                'width=600,height=500,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
}

function okno(img)
{
pozX = Math.floor(((screen.width)/2)-25);
pozY = Math.floor(((screen.height)/2)-25);
fotowindow = window.open('/thumb/bigFoto.php?name=' + img, 'shop', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,fullscreen=no,channelmode=no,width=100,height=100, left=' + pozX + ', top=' + pozY);
fotowindow.focus();
}

function dodajOpinie()
{
    var formdiv = document.getElementById( 'formAddComment' );

    formdiv.style.display = 'block';
}

function showAlert( text, error )
{
    var spanKomunikat = document.getElementById( 'trescKomunikatu' );
    var divKomunikat = document.getElementById( 'komunikat' );
    var Wleft = Math.floor( ( ( screen.width ) / 2 ) - 250 );
    var Wtop = Math.floor( ( ( screen.height ) / 2 ) - 150 );

    if (document.documentElement && document.documentElement.scrollTop)
        theTop = document.documentElement.scrollTop;
    else if (document.body)
        theTop = document.body.scrollTop

    Wtop = Wtop + theTop;
    divKomunikat.style.left = Wleft + "px";
    divKomunikat.style.top = Wtop + "px";
    divKomunikat.style.display = "block";
    if( error == 1 ) spanKomunikat.style.color = "red";
    spanKomunikat.innerHTML = text;
}

function hideAlert()
{
    var divKomunikat = document.getElementById( 'komunikat' );
    divKomunikat.style.display = "none";
}

function changeLargerPhoto( foto )
{
    var komorka = document.getElementById( 'largerPhoto' );
    var html = '';

    html += '<a href="javascript:void(0)" onclick="okno( \'' + foto + '\' )">';
    html += '<img src="/thumb/miniatura.php?rx=200&amp;name=' + foto + '" alt="" />';
    html += '</a>';

    komorka.innerHTML = html;
}

function fillAddressForm()
{
    var adresID = document.getElementById( 'istniejacyAdres' ).value;
    var imieF 	= document.getElementById( 'imie' );
    var nazwiskoF 	= document.getElementById( 'nazwisko' );
    var ulicaF 	= document.getElementById( 'ulica' );
    var kodF 	= document.getElementById( 'kod' );
    var miastoF 	= document.getElementById( 'miasto' );
    if( adresID > 0 )
    {
        imieF.value  = imie[adresID];
        nazwiskoF.value = nazwisko[adresID];
        ulicaF.value = ulica[adresID];
        kodF.value	 = kod[adresID];
        miastoF.value= miasto[adresID];
    }
    else
    {
        ulicaF.value = '';
        imieF.value = '';
        nazwiskoF.value = '';
        kodF.value	 = '';
        miastoF.value= '';
    }
}

function showSendAddressForm()
{
    var formularz 	= document.getElementById( 'adresWysylkiForm' );
    var etykieta = document.getElementById( 'addressFormLabel' );

    if( formularz.style.display == 'none' )
    {
        if( navigator.appName == 'Microsoft Internet Explorer' )
        {
            formularz.style.display = 'block';
        }
        else
        {
            formularz.style.display = 'table-cell';
        }
        etykieta.innerHTML = 'nie zmieniaj adresu wysyłki';
    }
    else
    {
        formularz.style.display = 'none';
        etykieta.innerHTML = 'wprowadź adres wysyłki, jeżeli jest inny niż adres powyżej';
    }
}

function generujRaport()
{
	var d_od = document.getElementById( 'f' ).value;
	var d_do = document.getElementById( 't' ).value;
	
	if( d_od == '' || d_do == '' ) 
	{
		alert( 'Nie wprowadzono zakresu dat dla raportu.' );
		return;
	}
	
	data = d_od.split( "-" );
	d_od = Date.UTC( data[0], data[1]-1, data[2], 0, 0, 0 );
	
	data = d_do.split( "-" );
	d_do = Date.UTC( data[0], data[1]-1, data[2], 0, 0, 0 );
		
	if( d_od > d_do ) 
	{
		alert( 'Data początku raportu jest później niż data zakończenia raportu' );
		return;
	}
	
	document.getElementById( 'f' ).value = d_od / 1000;
	document.getElementById( 't' ).value = d_do / 1000;
	
	document.raportSprzedazy.submit();
}

function producers()
{
	var lista = document.getElementById("producersList");
	
	if( lista.style.display == 'none' )
	{
		lista.style.display = '';
	}
	else
	{
		lista.style.display = 'none';
	}
}

function updateNetto( field )
{
	field.value = field.value.replace(",", ".");
	if( !isNaN( field.value ) )
	{
		var poleNetto = document.getElementById( 'prodCena' );
		var vat = document.getElementById( 'prodVat' ).value;
		
		poleNetto.value = (field.value / ( 1 + vat / 100 )).toFixed(2);
	}	
}

function toggleFilter( attr )
{
	var more = document.getElementById( 'pp' + attr );
	var opts = document.getElementById( 'dd' + attr );
	
	if( opts.style.display == 'none' )
	{
		opts.style.display = 'block';
		more.style.display = 'none';
	}
	else
	{
		opts.style.display = 'none';
		more.style.display = 'block';
	}
}

function expand( idPozycji, currentPos )
{
	var nazwa = "pole_" + idPozycji;
	var nazwaHtml = $("#" + nazwa).get();
     	$.post("/menu/menu.php", {id: idPozycji, current: currentPos}, function(strHTML) {
	var content = strHTML;
	if( $("#" + nazwa).attr("class") != "open" ) {
		if( content.length > 0 ) {
	       	var row = $("#" + nazwa).append( content ).children("ul");
	       	row.hide();
	       	row.slideDown( "slow" );
			$("#" + nazwa).addClass("open");
			row.removeClass("open");
		}
	}
	else {
		$("#" + nazwa).children("ul").slideUp("slow");
		$("#" + nazwa).children("ul").hide();
		$("#" + nazwa).removeClass("open");
		setTimeout( function() { $("#" + nazwa).children("ul").empty() }, 1000 );
	}
     });	
}

function expandProducers( )
{
	var idPozycji = 'producent';
	var nazwa = "pole_producent";
	var nazwaHtml = $("#" + nazwa).get();
     	$.post("/menu/producers.php", {id: idPozycji}, function(strHTML) {
	var content = strHTML;
	if( $("#" + nazwa).attr("class") != "open" ) {
		if( content.length > 0 ) {
	       	var row = $("#" + nazwa).append( content ).children("ul");
	       	row.hide();
	       	row.slideDown( "slow" );
			$("#" + nazwa).addClass("open");
			row.removeClass("open");
		}
	}
	else {
		$("#" + nazwa).children("ul").slideUp("slow");
		$("#" + nazwa).children("ul").hide();
		$("#" + nazwa).removeClass("open");
		setTimeout( function() { $("#" + nazwa).children("ul").empty() }, 1000 );
	}
     });	
}