function jeszczeJednaKategoria()
{
    var limit=document.getElementById('liczbaKategorii');
    limit.value=parseInt(parseInt(limit.value)+1);
    var liczbaKategorii=limit.value;

    var div = document.createElement('div');
    div.style.marginBottom="15px";
    div.innerHTML=document.getElementById('wzorKategorie').innerHTML.replace("unikatowaNazwaKategorie","kat"+limit.value);

    limit.parentNode.insertBefore(div, limit);
}

function jeszczeJedenArtykul()
{
    var limit=document.getElementById('liczbaArtykulow');
    limit.value=parseInt(parseInt(limit.value)+1);
    var liczbaKategorii=limit.value;

    var div = document.createElement('div');
    div.style.marginBottom="15px";
    div.innerHTML=document.getElementById('wzorArtykuly').innerHTML.replace("unikatowaNazwaArtykuly","art"+limit.value);

    limit.parentNode.insertBefore(div, limit);
}

function gebn(name)
{
    elementy=document.getElementsByName(name);
    return elementy[0];
}

function przepisz(from,to)
{
    if(gebn(to).value=='') gebn(to).value=gebn(from).value;
}

function jeszczeJedenLink()
{
    //var liczbazdjec=parseInt(document.getElementById('liczbazdjec').value)+1;

    //document.getElementById('liczbazdjec').value=liczbazdjec;
    var limit=document.getElementById('liczbaLinkow');
    limit.value=parseInt(parseInt(limit.value)+1);
    var liczbazdjec=limit.value;
    
    var hr = document.createElement('hr');
    hr.className='linkiHr';
    var select = document.createElement('select');
    
    select.name='link'+liczbazdjec;
    var wzorzec=document.getElementById('wzorzecLov');

    for(i=0;i<wzorzec.length;i++)
    {
        var opcja = document.createElement('option');
        opcja.text = wzorzec.options[i].text;
        opcja.value = wzorzec.options[i].value;

        try
        {
            select.add(opcja, null); // standards compliant; doesn't work in IE
        }
        catch(ex)
        {
            select.add(opcja); // IE only
        }
    }

    limit=document.getElementById('liczbaLinkow');
    
    limit.parentNode.insertBefore(hr, limit);
    limit.parentNode.insertBefore(select, limit);
    
}

function rekursywneOdznaczenie(id)
{
    var localrodzic=rodzic[id];
    var liczba=0;



    if(localrodzic>0)
    {
        //alert(localrodzic);

        for(i=0;i<dzieci[localrodzic].length;i++)
        {
            if(document.getElementById(dzieci[localrodzic][i]).checked==true)
            {
                //alert('klikniety!:' + dzieci[localrodzic][i]);
                liczba++;
            }
        //alert(dzieci[localrodzic][i]);


        }

        //alert(liczba);
        if(liczba==0)
        {
            document.getElementById(localrodzic).checked=false;
            rekursywneOdznaczenie(localrodzic);
        }
    }
}

function selectParental(checkbox)
{
    var rodzicId=checkbox.id;

    if(checkbox.checked==false)
    {
        rekursywneOdznaczenie(rodzicId);
    }
    else
    {

        while(rodzicId>0)
        {
            //alert(rodzicId);
            document.getElementById(rodzicId).checked=true;
            rodzicId=rodzic[rodzicId];
        }

    }
}

var mapa,cos,marker,zindex=666;

function zwin(id)
{
    if(document.getElementById('zabiegiDiv'+id))
    {
        if(document.getElementById('zabiegiDiv'+id).style.display=="none")
        {
            document.getElementById('zabiegiDiv'+id).style.display="block";
            document.getElementById('zwin'+id).innerHTML="zwiń";
        }
        else
        {
            document.getElementById('zabiegiDiv'+id).style.display="none";
            document.getElementById('zwin'+id).innerHTML="rozwiń";
        }
    }
}

function email(uzytkownik, serwer)
{
    location.href = "mailto:" + uzytkownik + "@" + serwer;
}

function zobaczWiecejOpinii(liczba)
{
    for(i=1;i<liczba;i++) document.getElementById('opinia'+i).style.display='block';
}

function zwinRozwin(anchora)
{
    //alert()
    if(anchora.innerHTML=='(rozwiń opis)')
    {
        document.getElementById('produktOpis').style.overflow='auto';
        document.getElementById('produktOpis').style.height=''
        document.getElementById('pasek').style.display='none';
        anchora.innerHTML='(zwiń opis)';
    }
    else
    {
        document.getElementById('produktOpis').style.overflow='hidden';
        document.getElementById('produktOpis').style.height='145px';
        document.getElementById('pasek').style.display='block';
        anchora.innerHTML='(rozwiń opis)';
    }
}

function pokazUkryjOpinie(anchora,string)
{
    var plus='';
    var minus='';

    
    if(string=='sklepie') {

        if(anchora.value=='Napisz opinię o '+string+plus)
        {
            //alert('ANapisz opinię o '+string + plus);
            document.getElementById('dodajOpinie').style.display='block';
            anchora.value='Ukryj opinię o '+string + minus;
        }
        else
        {
            //alert('BNapisz opinię o '+string + plus);
            document.getElementById('dodajOpinie').style.display='none';
            anchora.value='Napisz opinię o '+string + plus;
        }
    }
    else
    {
        if(anchora.innerHTML=='Napisz opinię o '+string+plus)
        {
            //alert('ANapisz opinię o '+string + plus);
            document.getElementById('dodajOpinie').style.display='block';
            anchora.innerHTML='Ukryj opinię o '+string + minus;
        }
        else
        {
            //alert('BNapisz opinię o '+string + plus);
            document.getElementById('dodajOpinie').style.display='none';
            anchora.innerHTML='Napisz opinię o '+string + plus;
        }
    }
}

function czyPotrzebnyJestPrzyciskRozwinOpis()
{
    //rozwijamy

    document.getElementById('produktOpis').style.overflow='auto';
    document.getElementById('produktOpis').style.height='';

    var koncowa=parseInt(document.getElementById('produktOpis').offsetHeight);
    
    if((koncowa+1)<=176)
    {
        document.getElementById('rozwinOpis').style.display='none';
        document.getElementById('pasek').style.display='none';
    }
      
    //zwijamy

    document.getElementById('produktOpis').style.overflow='hidden';
    document.getElementById('produktOpis').style.height='145px';
}

function isIE()
{
    return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function dragMe(e,nazwa)
{
    //var evt=e;
    if(isIE())
    {
        //alert('ie!');
        e=window.event || e
    }
    //else evt=e;
    
    var div=document.getElementById(nazwa);
    zindex=zindex+1;
    div.style.zIndex=zindex;
    // var div2=document.getElementById(nazwa2);
    pX=e.clientX;
    pY=e.clientY;
    pDIVX=parseInt(div.style.left);
    pDIVY=parseInt(div.style.top);
    //alert(document.getElementById(nazwa).style.top);
    function drag(e)
    {
        div.style.top=10;
        if(pDIVY-(pY-e.clientY)>0 && pDIVY-(pY-e.clientY)<screen.height-300) div.style.top=pDIVY-(pY-e.clientY)+"px";
        if(pDIVX-(pX-e.clientX)>0 && pDIVX-(pX-e.clientX)<screen.width-100) div.style.left=pDIVX-(pX-e.clientX)+"px";
        
        document.body.style.cursor = 'move';


    }

    function stop()
    {
        document.onmousemove='';
        document.onmouseup='';
        document.body.style.cursor='default';
    }

    document.onmousemove=drag;
    document.onmouseup=stop;
}

function resizeMe(e,nazwa,nazwa2)
{

    
    var div=document.getElementById(nazwa);
    var div2=document.getElementById(nazwa2);
    zindex=zindex+1;
    div.style.zIndex=zindex;
    pX=e.clientX;
    pY=e.clientY;
    width=parseInt(div.style.width);
    height=parseInt(div.style.height);

    function resize(e)
    {
        //alert(e.clientX);
        div.style.height=height-(pY-e.clientY)+"px";
        div.style.width=width-(pX-e.clientX)+"px";
        div2.style.width=width-(pX-e.clientX)-12+"px";
        document.body.style.cursor = 'move';
    }
    function noresize()
    {

        document.onmousemove='';
        document.onclick='';
        document.body.style.cursor='default';
    }
    document.onmousemove=resize;
    document.onclick=noresize;
//document.getElementById(nazwa).ondblclick=noresize;
}

function mapaStart(pozycjax,pozycjay,zoom,penisable)
{
    if(GBrowserIsCompatible())
    {
      


        /* google.load("maps", "2");
        google.load("elements", "1", {
            packages : ["localsearch"]
        });*/
        
        
       
        mapa = new GMap2(document.getElementById('mapka'),{
            mapTypes: [G_NORMAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP]
            
            
        });
       
        //google.search.NewsSearch("toruń")
        //var lsc = new google.elements.LocalSearch(options);
        //mapa.addControl(lsc);
        //echo 'mapa.setCenter(new GLatLng(51.95780738871554, 19.072265625),6);';
        mapa.setCenter(new GLatLng(pozycjax,pozycjay),zoom);

        //dodajemy zoom myszką
        mapa.enableScrollWheelZoom();
        //dodajemy kontroler
        mapa.addControl(new GLargeMapControl());
        if(penisable==1)
            marker = new GMarker(mapa.getCenter(),
            {
                draggable: true
            
            }
            );
        else
            marker = new GMarker(mapa.getCenter(),
            {
                draggable: false

            }
            );
        mapa.addOverlay(marker);

        GEvent.addListener(marker,'dragend',function()
        {
            
            document.getElementById('pozycjaz').value=(mapa.getZoom());
            cos=marker.getLatLng();
            document.getElementById('pozycjax').value=cos.lat();
            document.getElementById('pozycjay').value=cos.lng();

        });

        GEvent.addListener(mapa,'zoomend',function()
        {
            cos=marker.getLatLng();
            document.getElementById('pozycjaz').value=(mapa.getZoom());
            document.getElementById('pozycjax').value=cos.lat();
            document.getElementById('pozycjay').value=cos.lng();
        });

        
    }
}

function showa(div)
{
    document.getElementById(div).style.display='block';
}

function hidea(div)
{
    document.getElementById(div).style.display='none';
}

function dodajProdukt()
{
    var lista=document.getElementById('selektpodkat');
    var pole=document.getElementById('produkty');
    var liczba=document.getElementById('liczbaProd');
    pole.innerHTML=pole.innerHTML+
    '<div id="div'+document.getElementById('liczbaProd').value+'">'+lista.options[lista.selectedIndex].text+'<input type="hidden" name="produkt'+liczba.value+'" value="'+lista.options[lista.selectedIndex].value+'"> <a href="#" onclick="document.getElementById(\'div'+document.getElementById('liczbaProd').value+'\').innerHTML=\'\';return false;">usuń</a></div>';
    //  liczba.value='ggg';
    //alert(liczba.value+"AAA");
    document.getElementById('liczbaProd').value=parseInt(parseInt(document.getElementById('liczbaProd').value)+1);
//document.getElementById('hiddenProd').innerHTML='<input type="hidden" name="liczbaProd" value="'+liczbazdjec+'"><br>';

}

function dodajNowyZabieg(url)
{
    var liczbaZabiegow=parseInt(document.getElementById('liczbaZabiegow').value)+1;
    
    document.getElementById('liczbaZabiegow').value=liczbaZabiegow;
    var div = document.createElement('div');
    div.id= 'div'+liczbaZabiegow;
    div.className= 'zabiegiLovDiv';
    document.getElementById('liczbaZabiegow').parentNode.insertBefore(div, document.getElementById('liczbaZabiegow'));
    //div.parentNode.insertBefore(input, div);
    new Ajax.Updater('div'+liczbaZabiegow, url+'/numer/'+liczbaZabiegow, {
        asynchronous:true,
        evalScripts:true
    });

    if(liczbaZabiegow>=2)
    {
        document.getElementById('dodajZabiegPrzycisk').style.display='none';
        //alert('Można wybrać maksymalnie trzy zabiegi.');
        return false;
    }
}

function blockingcheckthis(field,observer,limit)
{
    if(field.value.length<=limit) document.getElementById(observer).innerHTML=limit-field.value.length;
    else
    {
        //document.getElementById(observer).innerHTML='<font color="red">'+field.value.length+'</font>';
        alert('limit znaków został przekroczony!');
        field.value=field.value.substring(0,limit);
        document.getElementById(observer).innerHTML=limit-field.value.length;
    }
}

function menuSwitcher(numer)
{
    for(i=1;i<=3;i++)
    {
        if(i==numer) document.getElementById('menu'+i).style.fontWeight='bold';
        else document.getElementById('menu'+i).style.fontWeight='normal';
    }
}

function validacja()
{

    if(document.getElementById('nazwa').value.length==0)
    {
        alert("pole \"nazwa firmy\" jest puste");
        return false;
    }
    if(document.getElementById('kategoria[]').selectedIndex==-1)
    {
        alert("nie Wybrałaś żadnej kategorii");
        return false;
    }
    else if(document.getElementById('kod').value.length==0)
    {
        alert("pole \"kod pocztowy\" jest puste");
        return false;
    }
    else if(document.getElementById('miasto').value.length==0)
    {
        alert("pole \"miejscowość\" jest puste");
        return false;
    }
    else if(document.getElementById('ulica').value.length==0)
    {
        alert("pole \"ulica\" jest puste");
        return false;
    }
    else if(document.getElementById('telefon').value.length==0)
    {
        alert("pole \"telefon\" jest puste");
        return false;
    }
    else if(document.getElementById('fax').value.length==0)
    {
        alert("pole \"fax\" jest puste");
        return false;
    }
    else if(document.getElementById('email').value.length==0)
    {
        alert("pole \"e-mail\" jest puste");
        return false;
    }
    else if(document.getElementById('kraj').value.length==0)
    {
        alert("pole \"kraj\" jest puste");
        return false;
    }

    return true;
}

function zmianaPakietow(select)
{
    for(i=2;i<=3;i++)
    {
        if(select.value>=i) document.getElementById('pakiet'+i).style.display='block';
        else document.getElementById('pakiet'+i).style.display='none';
    }
}
