var gdiv='d1';

function add_sign(n)
{
	var sp=document.createElement('span');
	sp.className="sign";
	//sp.setAttribute('className','sign');
	
	sp.innerHTML=" *";
	
	return sp;
	
}
function dodaj_powiat(n)
{
		
		if(!document.getElementById('hurtoponpowiatid'+n))
		{
			
		var rootnode,nodeb;
		if(n<6)
		
			rootnode=document.getElementById('fieldset'+n);
			
		
		else
			rootnode=document.getElementById('hurtoponkrajid6').parentNode;
			
			
			
		nodeb1=document.getElementById('hurtoponregionid'+n).nextSibling;
		nodeb=nodeb1.nextSibling;
		var lpowiat=document.createElement('label');
		
		var selpowiat=document.createElement('select');

		selpowiat.name='hurtoponpowiatid'+n;
		

		selpowiat.id='hurtoponpowiatid'+n;
		selpowiat.style.display="block";
		add_opt(selpowiat);
		lpowiat.htmlFor=selpowiat.id;
		label_text=document.createTextNode('powiat');
		
		lpowiat.appendChild(label_text);
		
		lpowiat.style.fontWeight="bold";
		//labelpowiat.style.display="block";
		rootnode.insertBefore(lpowiat,nodeb);
		rootnode.insertBefore(add_sign(),nodeb);
		
		rootnode.insertBefore(selpowiat,nodeb);
		}
		
}
function usun_powiat(k)
{
		var noderm,nodeg;
	if(document.getElementById('hurtoponpowiatid'+k))
	{
		
		if(k<6)
			var rootnode=document.getElementById('fieldset'+k);
		else
			var rootnode=document.getElementById('hurtoponkrajid6').parentNode;
		
		for(n=0;n<rootnode.childNodes.length;n++)
		{
			if(rootnode.childNodes[n].nodeName=='LABEL')
			{
					//alert(rootnode.childNodes[n].innerHTML);
				if(rootnode.childNodes[n].innerHTML=='powiat')
				{
					noderm=rootnode.childNodes[n];
					while (noderm.firstChild)
 					{
	   					 noderm.removeChild(noderm.firstChild);
		
 					}
					nodeg=noderm.nextSibling;
					while (nodeg.firstChild)
 					{
	   					 nodeg.removeChild(nodeg.firstChild);
		
 					}
					rootnode.removeChild(nodeg);
					rootnode.removeChild(noderm);
					
				}
			}
			
		}
		//alert(document.getElementById('hurtoponpowiatid'+k));
		
		noderm=document.getElementById('hurtoponpowiatid'+k);
		
		while (noderm.firstChild)
 		{
	   		 noderm.removeChild(noderm.firstChild);
		
 		}
		rootnode.removeChild(noderm);
		
		
	}
}

function add_divclear()
{
		var divclear=document.createElement('div');
		divclear.id='divclear';
		divclear.className='clearboth';
		document.getElementById(gdiv).appendChild(divclear);
}
function rm_divclear()
{
//		alert(divclear.className);
	if(document.getElementById('divclear'))
	{
		var divclear=document.getElementById('divclear');
		//
		
		document.getElementById(gdiv).removeChild(divclear);
		
//		var rootNode = document.getElementById(gdiv).removeChild(;
		//rootNode.removeChild
	}
}

function rm_fldset()
{
		var fldsetnr=0,jestfiledset,k,fchild
		jestfieldset=false;
		for(k=1;k<6;k++)
		{
				if(document.getElementById('fieldset'+k))
				{
					if(k>fldsetnr)fldsetnr=k;
				}
		}
		//fldsetnr=window.event.srcElement.name.replace(re,'');
		
			//alert(fldsetnr);
		var rootNode = document.getElementById('fieldset'+fldsetnr);

  		while (rootNode.firstChild)
 		{
		fchild=rootNode.firstChild;
		while (fchild.firstChild)fchild.removeChild(fchild.firstChild);
	    rootNode.removeChild(rootNode.firstChild);
			
 		}
		document.getElementById(gdiv).removeChild(rootNode);
		for(k=0;k<6;k++)
			{
					if(document.getElementById('fieldset'+k))
						jestfieldset=true;
			}
		if(!jestfieldset)rm_divclear();

}
function add_opt(theSelect)
{
	var theOption=document.createElement("option");
		var theText=document.createTextNode("(wybierz z listy)");
		theOption.value="";
		theOption.appendChild(theText);
		theSelect.appendChild(theOption);
}



function add_fldset()
	{
		
		
		

		var liczba = 0;
		var ilosc = document.forms[0].elements.length;
		var ore=/^fieldset(\d){1}$/;
		
		for (var i = 0; i < ilosc; i++ )
		{
			
			if (ore.test(document.forms[0].elements[i].id))
			{
				liczba=liczba+ 1;
				
			}
		}
		if (liczba==6 )return;
		if(liczba>0)rm_divclear();
		var fset=document.createElement("fieldset");
		//fset.style.display="block";
		fset.id="fieldset"+liczba;
		fset.className='fieldsetadres'
		var legend=document.createElement('legend');
		legend.style.color='#aa0000';
		legend.innerHTML='Dane wysylki zamówionych opon '+liczba+'.'
		fset.appendChild(legend);
		//fset.style.float="left";
		//fset.style.width="300px";
		document.getElementById(gdiv).appendChild(fset);
		var lprofil=document.createElement('label');
		lprofil.htmlFor='nazwa_profilu'+liczba;
		lprofil.style.fontWeight='bold';
		//lprofil.style.display='block';
		label_text=document.createTextNode("nazwa profilu dostawy");
		lprofil.appendChild(label_text);
		document.getElementById(fset.id).appendChild(lprofil);
		document.getElementById(fset.id).appendChild(add_sign());
		var iprofil=document.createElement('input');
		iprofil.id='nazwa_profilu'+liczba;
		iprofil.name='nazwa_profilu'+liczba;
		iprofil.style.display='block';
		iprofil.type='text';
		document.getElementById(fset.id).appendChild(iprofil);
		var inazwafirmy=document.createElement('input');
		inazwafirmy.id='nazwa_firmy_wysylka'+liczba;
		inazwafirmy.name='nazwa_firmy_wysylka'+liczba;
		inazwafirmy.style.display='block';
		inazwafirmy.type='text';
		var lnazwafirmy=document.createElement('label')
		
		lnazwafirmy.htmlFor=inazwafirmy.id;
		label_text=document.createTextNode("Nazwa firmy:");
		lnazwafirmy.appendChild(label_text);
		lnazwafirmy.style.fontWeight='bold';
		//lnazwafirmy.style.display='block';
		document.getElementById(fset.id).appendChild(lnazwafirmy);
		document.getElementById(fset.id).appendChild(add_sign());
		document.getElementById(fset.id).appendChild(inazwafirmy);
		var lulica=document.createElement('label');
		lulica.htmlFor='ulica_wysylka'+liczba;
		lulica.style.fontWeight='bold';
		//lulica.style.display='block';
		label_text=document.createTextNode("ulica, nr domu");
		lulica.appendChild(label_text);
		document.getElementById(fset.id).appendChild(lulica);
		document.getElementById(fset.id).appendChild(add_sign());
		var iulica=document.createElement('input');
		iulica.id='ulica_wysylka'+liczba;
		iulica.name='ulica_wysylka'+liczba;
		 iulica.style.display='block';
		iulica.type='text';
		document.getElementById(fset.id).appendChild(iulica);
		var lkod=document.createElement('label');
		lkod.htmlFor='kod_pocztowy_wysylka'+liczba;
		lkod.style.fontWeight='bold';
		//lkod.style.display='block';
		label_text=document.createTextNode("kod pocztowy");
		lkod.appendChild(label_text);
		document.getElementById(fset.id).appendChild(lkod);
		document.getElementById(fset.id).appendChild(add_sign());
		var ikod=document.createElement('input');
		ikod.id='kod_pocztowy_wysylka'+liczba;
		ikod.name='kod_pocztowy_wysylka'+liczba;
		ikod.style.display='block';
		//ikod.style.size="10";
		ikod.type='text';
		document.getElementById(fset.id).appendChild(ikod);
		
		
		var lmiasto=document.createElement('label');
		lmiasto.htmlFor='miasto_wysylka'+liczba;
		lmiasto.style.fontWeight='bold';
		//lmiasto.style.display='block';
		label_text=document.createTextNode("miasto");
		lmiasto.appendChild(label_text);
		document.getElementById(fset.id).appendChild(lmiasto);
		document.getElementById(fset.id).appendChild(add_sign());
		var imiasto=document.createElement('input');
		imiasto.id='miasto_wysylka'+liczba;
		imiasto.name='miasto_wysylka'+liczba;
		imiasto.style.display='block';
		imiasto.type='text';
		document.getElementById(fset.id).appendChild(imiasto);
		
		
		
		var selregion=document.createElement('select');
		

		selregion.name='hurtoponregionid'+liczba;

		selregion.id='hurtoponregionid'+liczba;
		selregion.setAttribute('onchange', 'getpowiatName('+liczba+')');
		selregion.style.display="block";
		if(window.event)
			selregion.onchange=getpowiatName;
		add_opt(selregion);
		var lregion=document.createElement('label');
		
		lregion.htmlFor=selregion.id
		
		label_text=document.createTextNode('region');

		lregion.appendChild(label_text);
		
		lregion.style.fontWeight="bold";
		//labelregion.style.display="block";

		document.getElementById(fset.id).appendChild(lregion);
		document.getElementById(fset.id).appendChild(add_sign());
		

		
		document.getElementById(fset.id).appendChild(selregion);
		var selpowiat=document.createElement('select');

		selpowiat.name='hurtoponpowiatid'+liczba;
		

		selpowiat.id='hurtoponpowiatid'+liczba;
		selpowiat.style.display="block";
		add_opt(selpowiat);
		
		var lpowiat=document.createElement('label');
		
		
		lpowiat.htmlFor=selpowiat.id;
		label_text=document.createTextNode('powiat');
		
		lpowiat.appendChild(label_text);
		
		lpowiat.style.fontWeight="bold";
		//labelpowiat.style.display="block";
		document.getElementById(fset.id).appendChild(lpowiat);
		document.getElementById(fset.id).appendChild(add_sign());
		document.getElementById(fset.id).appendChild(selpowiat);
		var losoba=document.createElement('label');
		losoba.htmlFor='osoba_zamawiajaca'+liczba;
		losoba.style.fontWeight='bold';
		//losoba.style.display='block';
		/*label_text=document.createTextNode("osoba zamawiająca");
		losoba.appendChild(label_text);
		document.getElementById(fset.id).appendChild(losoba);
		document.getElementById(fset.id).appendChild(add_sign());
		var iosoba=document.createElement('input');
		iosoba.id='osoba_zamawiajaca'+liczba;
		iosoba.name='osoba_zamawiajaca'+liczba;
		iosoba.style.display='block';
		iosoba.type='text';
		document.getElementById(fset.id).appendChild(iosoba);
		var lemail=document.createElement('label');
		lemail.htmlFor='zamawiajacy_email'+liczba;
		lemail.style.fontWeight='bold';
		//lemail.style.display='block';
		label_text=document.createTextNode("email osoby zamawiającej");
		lemail.appendChild(label_text);
		document.getElementById(fset.id).appendChild(lemail);
		document.getElementById(fset.id).appendChild(add_sign());
		var iemail=document.createElement('input');
		iemail.id='zamawiajacy_email'+liczba;
		iemail.name='zamawiajacy_email'+liczba;
		iemail.style.display='block';
		iemail.type='text';
		
		document.getElementById(fset.id).appendChild(iemail);
		var ltelefon=document.createElement('label');
		ltelefon.htmlFor='zamawiajacy_telefon'+liczba;
		ltelefon.style.fontWeight='bold';
		//ltelefon.style.display='block';
		label_text=document.createTextNode("telefon osoby zamawiającej");
		ltelefon.appendChild(label_text);
		document.getElementById(fset.id).appendChild(ltelefon);
		document.getElementById(fset.id).appendChild(add_sign());
		var itelefon=document.createElement('input');
		itelefon.id='zamawiajacy_telefon'+liczba;
		itelefon.name='zamawiajacy_telefon'+liczba;
		itelefon.style.display='block';
		itelefon.type='text';
		
		document.getElementById(fset.id).appendChild(itelefon);*/
		var hurtoponkrajid=document.createElement('input');
		hurtoponkrajid.type='hidden';
		hurtoponkrajid.value=document.getElementById('hurtoponkrajid0').value;
		hurtoponkrajid.id='hurtoponkrajid'+liczba;
		hurtoponkrajid.name='hurtoponkrajid'+liczba;
		document.getElementById(fset.id).appendChild(hurtoponkrajid);
		if (liczba<5)
		{
		var bdodaj=document.createElement('input');
		bdodaj.type='button';
		bdodaj.value="dodaj adres";
		//bdodaj.setAttribute('onClick','add_fldset');
		bdodaj.onclick=add_fldset;
		//bdodaj.style.display='block';
		document.getElementById(fset.id).appendChild(bdodaj);
		};
		var busun=document.createElement('input');
		busun.type='button';
		busun.value="usun adres";
		busun.id="br"+liczba;
		busun.name="br"+liczba;
		busun.setAttribute("onClick","rm_fldset("+liczba+")");
		if(window.event)
			busun.onclick=rm_fldset;
		//busun.style.display='block';
		document.getElementById(fset.id).appendChild(busun);
		add_divclear();
		getregionName(liczba);
		
		

	}
