function textLimit(obj, limit, msg) 
{
	if (obj.value.length > limit) {
		obj.value = obj.value.substring(0,limit);
		alert(msg);
	}
}
function addToFavorite() 
{
	if(window.opera && window.print)
	{ 
		var mbm = document.createElement('a');
		mbm.setAttribute('rel', 'sidebar');
		mbm.setAttribute('href',  document.location.href);
		mbm.setAttribute('title', document.title);
		mbm.click();
	}
	else
	if(document.all)
	{
		window.external.AddFavorite(document.location.href, document.title);
	}
	else
	if(window.sidebar) 
	{ 
		window.sidebar.addPanel(document.title, document.location.href, ""); 
	}
}

// obliczanie pozostalych znakow w textarea
function oblicz(pole, licznik, limit) 
{
	var pole = findObj(pole);
	
	var licznik = findObj(licznik);
	if (pole.value.length > limit) {
		pole.value = pole.value.substring(0,limit);
		alert ("Przekroczono dozwoloną długość tekstu!");
	}
	var ilosc_znakow = limit-pole.value.length;
	if (ilosc_znakow < 0) ilosc_znakow = 0
	licznik.innerHTML = 'pozostało znaków: '+ilosc_znakow;
}

function findObj(obj, d)
{
	var fo = '';
	if(typeof(d) != 'undefined')
	{
		fo = d.getElementById(obj);
	}
	else
	{
		fo = document.getElementById(obj);
		if(fo == null)
		{
			fo = window.top.document.getElementById(obj);
			if(fo == null)
			{
				for(i=0; i<window.frames.length; i++)
				{
					fo = window.frames[i].document.getElementById(obj);
					if(fo != null) break;
				}
			}
		}
	}
	if(fo == null)
		return typeof IsNotThere;
	else
		return fo;
}

// parki technologiczne
function parkiWojewodztwo(id_woj) 
{
	var woj = id_woj;
	
	if(woj==1) //dolnośląskie
	{
		var lng = 16.4300537109375;
		var lat = 51.089722918116344;
		var zoom = 8;
	}
	else if(woj==2) //kujawsko - pomorskie
	{
		var lng = 18.446044921875;
		var lat = 53.097322593577;
		var zoom = 8;
	}
	else if(woj==3) //lubelskie
	{
		var lng = 22.598876953125;
		var lat = 51.282534682474655;
		var zoom = 8;
	}
	else if(woj==4) //lubuskie
	{
		var lng = 15.1885986328125;
		var lat = 52.089632613639715;
		var zoom = 8;
	}
	else if(woj==5) //łódzkie
	{
		var lng = 19.44580078125;
		var lat = 51.72022261695929;
		var zoom = 8;
	}
	else if(woj==6) //małopolskie
	{
		var lng = 20.126953125;
		var lat = 49.89463439573421;
		var zoom = 8;
	}
	else if(woj==7) //mazowieckie
	{
		var lng = 21.0498046875;
		var lat = 52.16719363541221;
		var zoom = 8;
	}
	else if(woj==8) //opolskie
	{
		var lng = 17.8857421875;
		var lat = 50.666872321810715;
		var zoom = 8;
	}
	else if(woj==9) //podkarpackie
	{
		var lng = 22.0220947265625;
		var lat = 49.96888926176222;
		var zoom = 8;
	}
	else if(woj==10) //podlaskie
	{
		var lng = 23.170166015625;
		var lat = 53.1335898292448;
		var zoom = 8;
	}
	else if(woj==11) //pomorskie
	{
		var lng = 17.962646484375;
		var lat = 54.2539938682326;
		var zoom = 8;
	}
	else if(woj==12) //śląskie
	{
		var lng = 18.96240234375;
		var lat = 50.38400527636708;
		var zoom = 8;
	}
	else if(woj==13) //świętokrzyskie
	{
		var lng = 20.85205078125;
		var lat = 50.83369767098071;
		var zoom = 8;
	}
	else if(woj==14) //warmińsko - mazurskie
	{
		var lng = 20.467529296875;
		var lat = 53.77955794100294;
		var zoom = 8;
	}
	else if(woj==15) //wielkopolskie
	{
		var lng = 16.9244384765625;
		var lat = 52.41247205962487;
		var zoom = 8;
	}
	else if(woj==16) //zachodnio - pomorskie
	{
		var lng = 15.51544189453125;
		var lat = 53.494581793167185;
		var zoom = 8;
	}
	else
	{
		var lng = 18.8525390625;
		var lat = 52.17393169256846;
		var zoom = 6;
	}

	$("#lng").val(lng);
	$("#lat").val(lat);
	$("#zoom").val(zoom);
	
	$("#wojewodztwo option").removeAttr('selected').filter('[value='+woj+']').attr('selected','selected');
	$("#miejscowosc option").removeAttr('selected');
	
	google.load("maps", "2", {"callback" : load});
	
}

function parkiMiasto(id_miej) 
{
	var miej = id_miej;
	var woj = $("#wojewodztwo").val();
	
	if(miej>0)
	{
		
		$.ajax({
			type: "POST",
			url: '/parkiMiasto.php',
			data: { miejscowosc: miej},
			dataType: "xml",
			success: 
				function(xml){
					$("#lng").val($('x', xml).text());
					$("#lat").val($('y', xml).text());
					$("#zoom").val($('zoom', xml).text());
					
					google.load("maps", "2", {"callback" : load});
				}
			});
	}
	else
	{
		if(woj>0)
		{
			parkiWojewodztwo(woj);
		}
		else
		{
			var lng = 18.8525390625;
			var lat = 52.17393169256846;
			var zoom = 6;
			
			$("#lng").val(lng);
			$("#lat").val(lat);
			$("#zoom").val(zoom);
			
			google.load("maps", "2", {"callback" : load});
		}
	}
}

function parkiMiastoWoj(id_woj) 
{
	var woj = id_woj;

	$.ajax({
		type: "POST",
		url: '/parkiMiasto.php',
		data: { wojewodztwo: woj},
		dataType: "xml",
		success: 
			function(xml){
				$("#miejscowosc").removeOption(/./);
				$("miasto", xml).each(function(){
				$("#miejscowosc").addOption( $("id",this).text() , $("nazwa",this).text(), false ); 
			});
		}
		});
}

function parkiSzczegoly(id) 
{
	var id = id;

	$.ajax({
	type: "POST",
	url: '/parkiMiasto.php',
	data: { id: id},
	dataType: "html",
	success: 
		function(html){
				$('<div id="mapa_'+id+'">'+
				'<div class="map_overlay" onclick="javascript:parkiukryj('+id+')"></div>'+
				'<div id="map_tresc_overlay">'+
					html+
					'<p><input type="button" value="Zamknij" onclick="javascript:parkiukryj('+id+');" /></p>'+
				'</div>'+
				'</div>'
			).appendTo('body');
			
			var pathsize = getPageSizemap();
			var pageScrollmap = getPageScrollmap();
			
			$('#map_tresc_overlay').css('top', pageScrollmap[1] + 50 +'px');
			$('div.map_overlay').css('width', pathsize[2] +'px');
			$('div.map_overlay').css('height', pathsize[1] +'px');
		}
	});
}
function parkiukryj(id)
{
	$('#mapa_'+id).remove();
	$("#map_tresc_overlay").remove();
}
function getPageSizemap() {
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScrollmap() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
};