//Wywołania jQuery wdesk
//Pocz±tek jQuery
$(function() {
	//Scroll dla top
	$('a[href*=#wtop]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {		
			var $target = $(this.hash);
			$target = $target.length && $target || $('[id=' + this.hash.slice(1) + ']');
			if ($target.length) {
				//var targetOffset = $target.offset().top;
				var targetOffset = -20;
				$('html,body').animate({scrollTop: targetOffset}, 800);
				return false;
			}
		}
	});
	//end scroll top
	//Przenikanie dowolnych elementów
	$('.wslideel').mouseover(function(){ 
		$(this).fadeOut(100);
		$(this).fadeIn(50);
	});
	//Obrazki w galerii 1
	$('img[class~="wslide"]')
	.mouseover(function() { 
		var src = $(this).attr("src").match(/[^\.]+/) + "x.jpg";
		$(this).attr("src", src);
		if (this.complete) $(this).fadeOut(300);
		if (this.complete) $(this).fadeIn(300);
	})
	.mouseout(function() {
		var src = $(this).attr("src").replace("x.jpg", ".jpg");
		$(this).attr("src", src);
	});
	//Obrazki w galerii 2
	$('img[class~="wslide1"]')
	.mouseover(function() { 
		if (this.complete) $(this).fadeOut(300);
		if (this.complete) $(this).fadeIn(300);
	});
});

$(document).ready(function(){
	//Animacje grafik
	if ($("a[rel='gallery-plants']").length > 0)
		$("a[rel='gallery-plants']").colorbox();
	if ($("a[rel='wdesk-gal1']").length > 0)
		$("a[rel='wdesk-gal1']").colorbox();
	if ($("a[rel='wdesk-gal2']").length > 0)
		$("a[rel='wdesk-gal2']").colorbox({transition:"fade"});
	if ($("a[rel='wdesk-gal3']").length > 0)
		$("a[rel='wdesk-gal3']").colorbox({slideshow:false});
	if ($("a[rel='wdesk-gal4']").length > 0)
		$("a[rel='wdesk-gal4']").colorbox({slideshow:true});
	//Dymki
	$('.wtooltip').mouseover(function(e) {
		//var tip=$(this).children("*:first").attr('title');
		var tip=$(this).attr('title');
		//$(this).children("img:first").attr('title','');
		if(tip && tip!=""){		
			$(this).attr('title','');
			$("body").append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');
			$('#tooltip').fadeIn('500');
			$('#tooltip').fadeTo('10',1.0);	
		}	
	}).mousemove(function(e) {
		if($('#tooltip').length>0){	
			var strw=$(window).width(); 
			var divw=$('#tooltip').width();
			var myszx=e.pageX;
			var top=e.pageY + 10;
			var left=e.pageX + 20;
			if(strw<divw+myszx+20){
				left=myszx-divw-20;
			}
			$('#tooltip').css('top',top);
			$('#tooltip').css('left', left);
		}			
	}).mouseout(function() {
		if($('#tooltip').length>0){	
			$(this).attr('title',$('.tipBody').html());			
			$("body").children('div#tooltip').remove();
		}		
	});
	//end Dymki nad linkami
	
	
	W_HideLoad();
	W_Init();
});
//Koniec jQuery

//Funkcja wywoływana w body po załadowaniu
function W_Init(){
	
}


//Google maps
var wmarkery=[];
function W_InitializeMap(){
	if(typeof wmapa_params != 'undefined'){
		W_WczytajGoogleMaps("map_canvas");
	}
}

function W_WczytajGoogleMaps(mapDiv){
	if(mapDiv!="")
		$('#'+mapDiv).html(wdesk_ajax_load_div);
	var script = document.createElement("script");
    script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=W_GenerujMapePunkty";
    script.type = "text/javascript";
    document.getElementsByTagName("head")[0].appendChild(script);
}

function W_GenerujMapePunkty(){
	for(var i=0; i<wmapa_params.length; i++){
		if(i===0){	
			var map=W_WczytajMape(wmapa_params[i]);
		}
		W_DodajMarker(wmapa_params[i], map);
	}
} 

function W_WczytajMape(mpar){
	var mapDiv="map_canvas";
	var latlng = new google.maps.LatLng(mpar["lat"], mpar["lng"]);
	eval('var typm='+"google.maps.MapTypeId."+mpar["mapTypeId"]);
    var myOptions = {
		zoom: mpar["zoom"],
		center: latlng,
		mapTypeId: typm,
		zoomControl: true,
		zoomControlOptions: {
			style: google.maps.ZoomControlStyle.SMALL,
		    position: google.maps.ControlPosition.LEFT_CENTER
		},
		panControl: true,
		zoomControl: true,
		mapTypeControl: true,
		scaleControl: true,
		streetViewControl: true,
		overviewMapControl: true
    };
    var map = new google.maps.Map(document.getElementById(mapDiv), myOptions);
    return map;
}

function W_DodajMarker(mpar, map){  
	var latlng = new google.maps.LatLng(mpar["lat"], mpar["lng"]);
	var image = new google.maps.MarkerImage('http://www.google.com/intl/en_ALL/mapfiles/marker.png',new google.maps.Size(20, 32),new google.maps.Point(0,0), new google.maps.Point(0, 32)); 
	if(mpar["image"] && mpar["image_s1"] && mpar["image_s2"] && mpar["image_p1"] && mpar["image_p2"])
       var image = new google.maps.MarkerImage(mpar["image"],new google.maps.Size(mpar["image_s1"], mpar["image_s2"]),new google.maps.Point(0,0), new google.maps.Point(mpar["image_p1"], mpar["image_p2"]));
    var shadow = new google.maps.MarkerImage('http://www.google.com/intl/en_ALL/mapfiles/shadow50.png', new google.maps.Size(37, 32), new google.maps.Point(0,0), new google.maps.Point(0, 32));
    if(mpar["shadow"] && mpar["shadow_s1"] && mpar["shadow_s2"] && mpar["shadow_p1"] && mpar["shadow_p2"])
        var shadow = new google.maps.MarkerImage(mpar["shadow"], new google.maps.Size(mpar["shadow_s1"], mpar["shadow_s2"]), new google.maps.Point(0,0), new google.maps.Point(mpar["shadow_p1"], mpar["shadow_p2"]));
	var shape = {coord: [1, 1, 1, 20, 18, 20, 18 , 1], type: 'poly'};
    var idx=wmarkery.length;
    wmarkery[idx] = new google.maps.Marker({
		position: latlng, 
		map: map,
		title:mpar["title"],
		shadow: shadow,
        icon: image,
        shape: shape
	});
	google.maps.event.addListener(wmarkery[idx], 'click', function() {
		wmarkery[idx].infowindow = new google.maps.InfoWindow({
			content: mpar["text"],
	        size: new google.maps.Size(mpar["sizex"],mpar["sizey"])
	    });
	    for(var i=0; i<wmarkery.length; i++){
	    	if(typeof wmarkery[i].infowindow!='undefined')
	    		wmarkery[i].infowindow.close();
	    }
    	wmarkery[idx].infowindow.open(map,wmarkery[idx]);
	});
}
//end Google maps

function W_Rotate2(){
	this.zm=0;
	this.dodaj=function(){++this.zm;}
	this.pokaz=function(){alert("Zm:"+this.zm);}
	this.uruchom=function(){this.dodaj();this.pokaz();}
	
}
//Rotator funkcja główna
function W_Rotate(czas, vin, vout, hin, headline, zanikt, pojawt, iddiv, lw, pr, st) {
	this.headline=headline;
	this.headline_count=$("div."+headline).size();
	this.old_headline = 0;
	this.current_headline = 0;
	this.vin=vin;
	this.vout=vout;
	this.hin=hin;
	this.zanikt=zanikt;
	this.pojawt=pojawt;
	this.iddiv=iddiv;
	this.lw=lw;
	this.pr=pr;
	this.st=st;
	this.czas=czas;
	this.rotuje=0;
	this.idsi=null;
	this.id = W_Rotate.Instances.length;
  	W_Rotate.Instances[this.id] = this;
	//this.hlt=$("div."+headline+":eq("+this.current_headline+")").css('top').split('px')[0];
	this.hlp=$("div."+headline+":eq("+this.current_headline+")").position();
	//this.hlt=this.htl[0];
	handle=this;
	/*
	$("#"+this.clst).bind('click',function(){
		alert("Klik");
		
	});
	*/
	$("div."+headline+":eq("+this.current_headline+")").css('top','5px');
	//$("#"+this.iddiv).bind('mouseover',function(){handle.Stop();});
	//$("#"+this.iddiv).bind('mouseout',function(){handle.Wznow();});
}
W_Rotate.Instances = new Array();
W_Rotate.prototype={
	Inicjalizuj: function(){},	
	Rotuj: function(){		
		this.current_headline = (this.old_headline + 1) % this.headline_count; 
		var hl="div."+this.headline+":eq(" + this.old_headline + ")";
		var chl="div."+this.headline+":eq(" + this.current_headline + ")";
		//Znikanie
		if(this.zanikt==="ab" || this.zanikt==="at"){
			var w=$("#"+this.iddiv).height();
			if(this.zanikt==="at")
				w=w*-1;
			$(hl).animate({top: w},this.vout, function() {
				//alert("Zm: "+handle.hlt);
				$(this).css('top',handle.hlp.top+'px');
				$(this).css('left',handle.hlp.left+'px');
		   	});
		}
		else if(this.zanikt==="al" || this.zanikt==="ar"){
			var w=$("#"+this.iddiv).width();
			if(this.zanikt==="al")
				w=w*-1;
			$(hl).animate({left: w},this.vout, function(){				
				$(this).css('top',handle.hlp.top+'px');
				$(this).css('left',handle.hlp.left+'px');
		   	});			
		}
		else{
			$(hl).fadeOut(this.vout, function(){
				//Headline
				//alert($(this).attr("class"));
				$(this).css('top',handle.hlp.top+'px');
			});
		} 
		//Przesun na pierwotne miejsce po zniknięciu - nie działa
		//$(hl).css('top',this.hlp.top+'px');
		//$(hl).css('left',this.hlp.left+'px');
		//Pojawianie
		if(this.pojawt==="ab" || this.pojawt==="at"){
			var w=this.hin;
			//Wlot od góry do dołu, czyli ustawic nad divem
			if(this.zanikt==="ab"){
				//w=w*-1;
			}
			//$(chl).css("top","300px");
			$(chl).show().animate({top: w},this.vin, function() {});
		}
		else if(this.pojawt==="al" || this.pojawt==="ar"){
			var w=this.hin;			
		}
		else{
			$(chl).hide().css('top', this.hin+'px').fadeIn(this.vin);
		}  
		this.old_headline = this.current_headline;
	},
	//Nigdzie nie uruchamiane jeszcze.
	Init: function(){
		this.Binduj();
	},
	Start: function(){
		if(this.rotuje===0){
			this.rotuje=1; 
			this.idsi=setInterval(function(){handle.Rotuj.call(handle);},this.czas);
		}
		//W_Rotate.Instances[this.id].idsi=window.setInterval ('W_Rotate.Instances['+this.id+'].Rotuj()', this.czas);
	},
	Stop: function(){this.rotuje=0;
		clearInterval(this.idsi);
		//clearInterval(W_Rotate.Instances[this.id].idsi);
	},
	Wznow: function(){this.Rotuj();this.Start();},
	Binduj: function(){
		//$("#"+this.iddiv).bind('mouseover',function(){W_Rotate.Instances[this.id].Stop();});
		//$("#"+this.iddiv).bind('mouseout',function(){W_Rotate.Instances[this.id].Wznow();});
	},
	Prawo: function(){
		this.Stop();
		this.Rotuj();
	},
	Lewo: function(){
		this.Stop();
		//Ukryj aktualny
		$("div."+this.headline+":eq(" + this.old_headline + ")").css('top',this.hlp.top+'px');
		//$("div."+this.headline+":eq(" + this.current_headline + ")").css('top',this.hlp.top+'px');
		if(this.old_headline===1){
			this.old_headline=this.headline_count-1;
		}
		else if(this.old_headline===0){
			this.old_headline=this.headline_count-2;
		}
		else{
			this.old_headline-=2;
		}
		$("div."+this.headline+":eq(" + this.old_headline + ")").css('top', this.hin+'px').hide();
		//this.current_headline = (this.old_headline + 1) % this.headline_count;
		this.Rotuj();
	},
	UstawPunkt: function(ind){
		this.Stop();
		$("div."+this.headline+":eq(" + this.old_headline + ")").css('top',this.hlp.top+'px');
		if(ind===0)
			this.old_headline=this.headline_count-1;
		else
			this.old_headline=ind-1;
		this.Rotuj();
		
	},
	bprklik: function(){$("#"+this.iddiv).bind('mouseover',function(){this.Stop();})}
	
}
//end rotator

function W_RequestAjaxGET(obj, adres){
	obj.open("GET", adres, true);
	obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset="+wkonfiguracja.strona_kodowa);
	obj.setRequestHeader("Content-Language", "pl");
	obj.send(null);
}
function W_RequestAjaxPOST(obj, adres, poststr){
	obj.open('POST', adres, true);
	obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset="+wkonfiguracja.strona_kodowa);
	obj.setRequestHeader("Content-length", poststr.length);
	obj.setRequestHeader("Connection", "close");
	obj.send(poststr);	
}
//Wgrywa do podanego div plik SWF
//div_id - id div, //typ - np. application/x-shockwave-flash, //sciezka - sciezka do pliku swf, //width, height - rozmiary, //wmode - np. transparent
function W_LadujSwf(div_id, typ, sciezka, width, height, wmode){
	var swf='<object type="application/x-shockwave-flash" data="'+sciezka+'" width="'+width+'" height="'+height+'"><param name="movie" value="'+sciezka+'" /><param name="wmode" value="'+wmode+'" /></object>';
	W_PiszDiv(div_id, swf);
}


//Pokazuje podkategorie dla danej kategorii
//grz - grupy zawodowe - tabela
function W_PokazPodGrupe(id_kat, id_pkat, tab_grz, kom_brak){
	var select_kat=document.getElementById(id_kat);
	var select_pkat=document.getElementById(id_pkat);
	var index_kat=select_kat.selectedIndex;
	//var id_akt_kat=select_kat.options[index_kat].value;
	var ilosc_opcji=select_pkat.options.length-1;
	for (var i=ilosc_opcji; i>=0; i--){
		select_pkat.remove(i);
	}
	var licznik=1;
	var opcja=new Option(kom_brak, 0);
	if(index_kat!=0){
		var podkat=tab_grz[index_kat]["stanowiska"];
		for(var i in podkat){
			var tekst=podkat[i]["stanowisko"];
			opcja=new Option(tekst, podkat[i]["id"]);
			select_pkat.options[licznik]=opcja;
			++licznik;
		}
		opcja=new Option(kom_brak, 0);
		select_pkat.options[0]=opcja;
	}
}

function W_ShowMessageBox(komunikat){
	PokazDiv("wblok_info");
	W_PiszDiv("wblok_info_txt", komunikat);

}

function W_ShowLoad(komunikat){
	PokazDiv("ajax_load");
	W_PiszDiv("ajax_load_txt", komunikat);
}

function W_HideLoad(){
	UkryjDiv("ajax_load");
}

function W_PrzekierujAdres(adres, loader, opoznienie, komunikat){
	if(loader==1){
		var k=komunikat;
		if(komunikat==null)
			k=fw_kom_przeladowanie;
		W_ShowLoad(k);
	}
	if(opoznienie>1)
		setTimeout("document.location.href='"+adres+"';", opoznienie);
	else
		document.location.href=adres;
}

function W_CzytajWartoscSelect(el){
	var obj=document.getElementById(el);
	if(obj){		
		return id_div=obj[obj.selectedIndex].value;
	}
}

//element - id elementu, wartosc - wartosc koloru, kod - kod koloru
function W_WpiszInputTextKolor(element, wartosc, kod){
	var obj=document.getElementById(element);
	if(obj)
		obj.value=wartosc;
	var obj2=document.getElementById(element+"_kod");
	if(obj2)
		obj2.value=kod;
	W_WpiszStyl(element+"_wybrany", "background", "#"+kod);
}

function W_PrzeladujStrone(adres){
	if(!adres)
		document.location.href=document.location.href;
	else
		document.location.href=adres;
}

function W_WpiszInput(id_element, wartosc){
	var obj=document.getElementById(id_element);
	if(obj)
		obj.value=wartosc;
}

//Aktywuje grupe o podobnych id
function W_WlaczElementy(rodzaj, fragment){	
	var q=rodzaj+"[id^='"+fragment+"']";
	$(q).removeAttr("disabled");
}

//Deaktywuje grupe o podobnych id
function W_WylaczElementy(rodzaj, fragment){
	var q=rodzaj+"[id^='"+fragment+"']";
	$(q).removeAttr("checked");
	$(q).attr("disabled", true);
}

function W_OdznaczElementy(rodzaj, fragment, wywolal, ilosc){
	var zaznacz=0;
	if(wywolal.checked==true)
		zaznacz=1;
	var q=rodzaj+"[id^='"+fragment+"']";
	var licz=0;
	$(q).each(function(i){
			if(this.checked==true)
				++licz;
		}
	);
	if(licz>ilosc && zaznacz==1)
		W_ZaznaczOdznaczRadio(wywolal.id, 0);
	
	/*$(q).removeAttr("checked");
	if(zaznacz==1)
		W_ZaznaczOdznaczRadio(wywolal.id, 1);
	*/
}

function W_ZaznaczOdznaczRadio(id_elementu, czynnosc){
	if(czynnosc==1)
		$("#"+id_elementu).attr("checked", true);
	else
		$("#"+id_elementu).removeAttr("checked");
}

//Kasuje zawartosc formularza
function W_CzyscForm(id_form){
	var form=id_form;
	for(el in document.forms[form].elements){
		if(document.forms[form].elements[el]==null){continue;}
		var typ=document.forms[form].elements[el].type;
		if(typ!="checkbox" && typ!="text" && typ!="hidden" && typ!="password" && typ!="radio" && typ!="select" && typ!="checkbox" && typ!="textarea"){
			continue;
		}
		if(typ=="checkbox" || typ=="radio"){
			document.forms[form].elements[el].checked=false;
		}
		document.forms[form].elements[el].value="";
	}
}


//Przetwarza pole zwrócone z Ajax'a na tabele danych js
//Struktura danych wejsciowych:
// key::value:|:key::value:|:key::value ...
function W_PrzetwarzajTabeleAjax(strr){
	var dane=strr.split(":|:");
	var rekord=Array();
	var tab_wyn=Array();
	for(i in dane){
		rekord=dane[i].split("::");
		tab_wyn[rekord[0]]=rekord[1];
	}
	return tab_wyn;
}

//Waliduje pola formularza, jesli pola nie ma na liscie - zwraca true, domyslnie true
//tekst - sprawdzany tekst, nazwa - jakie pole sprawdzamy, obj-obiekt opcja przy zaawansowanym sprawdzaniu
function W_SprawdzFormularz(tekst, nazwa, obj){
	if(nazwa=="login"){
		if(!/^[a-zA-Z0-9]+[\-\._@a-zA-Z0-9]*[a-zA-Z0-9]+$/.test(tekst))
			return false;
	}
	else if(nazwa=="email"){
		return W_SprawdzEmail(tekst);
	}
	else if(nazwa=="haslo"){
		if(tekst.length<wkonfiguracja["klienci_dlugosc_hasla"])
			return false;
	}
	else if(nazwa=="liczba_float"){
		if(!/^[0-9]+[\,\.0-9]*[0-9]+$/.test(tekst))
			return false;
	}
	else if(nazwa=="cena"){
		if(!/^[1-9]+[\,\.0-9]*[0-9]+$/.test(tekst))
			return false;
	}
	else if(nazwa=="telefon"){
		if(!/^\+[1-9]{2,2}[\(\)\ 0-9]+[\ 0-9]+$/.test(tekst))
			return false;
	}
	else if(nazwa=="imie" || nazwa=="nazwisko"){			
		if(!/^[Ł¦Ć¬ˇŻa-zA-Z]/.test(tekst))
			return false;
	}
	else if(nazwa=="ulica" || nazwa=="miejscowosc"){			
		if(!/^[\ Ł¦Ć¬ˇŻa-zA-Z0-9]/.test(tekst))
			return false;
	}
	else if(nazwa=="lista"){
		if(!obj)
			return false;
		if(obj.selectedIndex=="" || obj.selectedIndex=="0" || obj.selectedIndex=="-1")
			return false;
	}
	else if(nazwa=="plik"){
		if(!obj)
			return false;
		if(obj.value=="")
			return false;
	}
	else if(nazwa=="checkbox"){
		if(!obj)
			return false;
		if(obj.checked==false)
			return false;
	}
	else{
		if(tekst=="")
			return false;
	}
	return true;
}

function W_SprawdzWysylke(obj_form){
	var form=obj_form.id;
	var walidacja=true;
	for(el in document.forms[form].elements){
		if(document.forms[form].elements[el]==null || document.forms[form].elements[el].id==""){continue;}
		var id=document.forms[form].elements[el].id;
		var obj=document.getElementById(id);
		if(obj===null){continue;}
		var typ=document.forms[form].elements[el].type;
		var nz=document.forms[form].elements[el].name;
		var val=document.forms[form].elements[el].value;
		var obw=document.forms[form].elements[el].getAttribute('o');
		var obj_pole_spr=document.getElementById(id+"_str");		
		if(!obj_pole_spr)
			var obj_pole_spr=document.getElementById(id+"_spr");
		if(obj_pole_spr && obj_pole_spr.type=="hidden"){
			obw=obj_pole_spr.value;
		}		
		var wal_tmp=true;
		if(obw==1){			
			if(nz=="dane_os" || typ=="checkbox"){
				wal_tmp=W_SprawdzFormularz(val, "checkbox", obj);
				if(wal_tmp==false){
					var obj2=document.getElementById('div'+id);
					if(nz=="dane_os")
						var obj2=document.getElementById('divdos');
					if(obj2)
						obj2.className="wyroz";
				}
				else{
					var obj2=document.getElementById('div'+id);
					if(nz=="dane_os")
						var obj2=document.getElementById('divdos');
					if(obj2)
						obj2.className="normal";
				}		
			}
			else if(nz=="doc"){
				wal_tmp=W_SprawdzFormularz(val, "plik", obj);
				if(obj.value==""){
					var obj2=document.getElementById('divdoc');
					if(obj2)
						obj2.className="wyroz";
				}
				else{
					var obj2=document.getElementById('divdoc');
					obj2.className="normal";
				}
			}
			else if(typ=="select-one"){
				wal_tmp=W_SprawdzFormularz("", "lista", obj);				
			}
			else
				wal_tmp=W_SprawdzFormularz(val, nz, obj);		
			if(wal_tmp==true){
				obj.className="";
				if(typ=="checkbox")
					obj.className=typ;
			}
			else{				
				obj.className="wyroz";
				if(typ=="checkbox")
					obj.className+=" "+typ;
				walidacja=false;
			}
		}
	}
	if(walidacja==false){
		W_HideLoad();
		W_ShowMessageBox(fw_kom_niepoprawny_formularz);		
		obj_wys=document.getElementById("wyslij");
		if(obj_wys){
			obj_wys.disabled=false;
			obj_wys.className="submit";
		}		
	}
	else{
		obj_wys=document.getElementById("wyslij");
		if(obj_wys){
			obj_wys.className="submit1";
			obj_wys.disabled=true;
		}
		W_ShowLoad(fw_kom_wysylanie_formularza);
	}
	//return false;
	return walidacja;
}


//Przekazujemy string w ktrym wpisano email
function W_SprawdzEmail(email){
	var walidacja=true;
	apos=email.indexOf("@");
	dotpos=email.lastIndexOf(".");
	edl=email.length;
	r=edl-1-dotpos;
	if (apos<1||dotpos-apos<2||r<2){
		walidacja=false;
	}
	if(!/^[a-zA-Z0-9]+[\-\._@a-zA-Z0-9]*[a-zA-Z0-9]+$/.test(email))
		walidacja=false;
	return walidacja;
}

function W_WyslijFormularz(id_form){
	var obj_form=document.getElementById(id_form);
	if(obj_form)
		obj_form.submit();
}

//Zmienie rozmiar czcionki tekstu po kliku w AAA
function W_ZmienRozmiar(nazwa_elementu, nazwa_klasy, numer){
	W_ZmienClass(nazwa_elementu, nazwa_klasy);
	W_ZmienClass("taaa1", nazwa_klasy);
	W_ZmienClass("taaa2", nazwa_klasy);
	W_ZmienClass("taaa3", nazwa_klasy);	
	document.getElementById('litera'+numer).className='aaas';
	if(numer==1){
		document.getElementById('litera2').className='aaa2';
		document.getElementById('litera3').className='aaa3';
	}
	if(numer==2){
		document.getElementById('litera1').className='aaa1';
		document.getElementById('litera3').className='aaa3';
	}
	if(numer==3){
		document.getElementById('litera2').className='aaa2';
		document.getElementById('litera1').className='aaa1';
	}
}

//Sprawadza czy w polu formularza nie ma niebezpiecznych fraz SQL Injection
function W_SqlInjectionFilter(id_obj){
	var obj=document.getElementById(id_obj);
	if(obj){
		var frazy=Array('--;', "';", "`;", '";', ' or ');
		for (x in frazy){
			var spr=new RegExp(frazy[x]);
			if(spr.test(obj.value))
				return false;
		}
	}
	return true;

}

function W_UkryjElement(elementID){
	var obj = document.getElementById(elementID);
	if(obj){
       obj.style.visibility = "hidden";
       if(obj.style.display == "")
			obj.style.display = "none";
	}
}

function W_PokazElement(elementID){
	var obj = document.getElementById(elementID);
	if(obj){
		obj.style.visibility = "visible";
		if(obj.style.display == "none")
			obj.style.display = "";
	}	
}

function W_CzytajDiv(obj){
	var div=document.getElementById(obj);
	if(div){
		var tresc=div.innerHTML;
		if(tresc)
			return tresc;
	}
}

function W_PiszDiv(obj, tresc){
	var div=document.getElementById(obj);
	if(div)
		div.innerHTML=tresc;
}

function W_ZmienClass(nazwap, nazwak){
	var kl="."+nazwap+"";
	$(kl).removeClass(nazwap).addClass(nazwak);
}

function W_WpiszClass(id_el, klasa){
	var el=document.getElementById(id_el);
	if(el)
		el.className=klasa;
}

function W_WpiszStyl(obj, styl, wartosc){
	var el=document.getElementById(obj);
	if(el)
		el.style[styl]=wartosc;
}


//Parametry 
//adres - adres serwra sond,
//idw - węzeł, 
//div - div do którego wczytane będą wyniki, 
//szer_tab - ustawai szerokosc tabel,
//wys_pkt - wysokosc w px kropki
//p - formularz z pytaniem,
//s - id sondy
//rodzaj_wyn - rodzaj wysw wynikow p: procenty
//sonda - w: pokazuje wynik, f: pokazuje formularz

function W_PobierzSonde(idw, div, szer_tab, wys_pkt,rodz_wyn, sonda, wyn_text, glos_text, sonda_text, ankieta_text, p, s){
	var pyt=0;
	if(p!=undefined){
		var form_sonda=document.getElementById(p);
		for(i=0; i<form_sonda.glos.length; ++i){
			if(form_sonda.glos[i].checked){
				pyt=i+1;
			}
		}
	}	
	var obraz=new Image();
	obraz.src="/szablon/sonda_loader.gif";
	var wpis="<img id='w_img_sonda_load' src='"+obraz.src+"' align='middle' alt='' />";
	W_PiszDiv(div, wpis);
	$.ajax({
		url: "/serwery_ajax_front/serwer_sondy/serwer_sondy.php",
		data: {'idw':idw,'p':pyt,'s':s},
		success: function(data, textStatus, jqXHR) {
			var odpowiedz=data.split("||");
			var idx_obr;
			var glos;
			var liczw=0;
			var liczp=0;
			var nazwa="";
			var opis="";
			var ids="";
			var wyniki=Array();
			var pytania=Array();
			for(x in odpowiedz){
				if(x<=3){
					if(x==0)
						nazwa=odpowiedz[x];
					else if(x==1)
						opis=odpowiedz[x];
					else if(x==2)
						ids=odpowiedz[x];
					else
						glos=odpowiedz[x];							
				}
				else{
					if(x%2==0){
						pytania[liczw]=odpowiedz[x];
						++liczp;
					}
					else{
						wyniki[liczw]=odpowiedz[x];
						++liczw;
					}
				}
			}
			var i=0;
			var zaw="<h4>"+ankieta_text+"</h4><div class='sonda1'>";
			if(glos==1 || sonda=="w"){
				zaw+="<h5>"+nazwa+"</h5><table width='"+szer_tab+"'>";					
				var suma=0;
				var max=0;
				for(i=0; i<wyniki.length; ++i){
					suma+=parseInt(wyniki[i]);
					if(parseInt(wyniki[i])>max)
						max=parseInt(wyniki[i]);
				}
				var szer=0;
				for(i=0; i<wyniki.length; ++i){
					szer=Math.floor((wyniki[i]/max)*100);	
					szer=Math.floor(szer*szer_tab/100);
					if(rodz_wyn=="p"){
						ilosc=Math.floor(wyniki[i]/suma*100);
						ilosc+="%";
					}
					else{
						ilosc=wyniki[i];
					}
					zaw+="<tr><td align='left'>"+pytania[i]+" ("+ilosc+")</td></tr>";
					zaw+="<tr><td align='left'><img src='/szablon/point.gif' width='"+szer+"' height='"+wys_pkt+"'></td></tr>";
					zaw+="<tr><td align='left'> </td></tr>";
				}
				if(glos!=1){
					zaw+="<tr><td align='left'><span class='wiecej_sonda'><a href=\"javascript:W_PobierzSonde("+idw+",'"+div+"',"+szer_tab+","+wys_pkt+",'"+rodz_wyn+"', 'f', '"+wyn_text+"', '"+glos_text+"', '"+sonda_text+"', '"+ankieta_text+"')\">&laquo;&nbsp;"+sonda_text+"</a></td></tr>";
				}
				zaw+="</table>";
			}
			else{
				zaw+="<h5>"+nazwa+"</h5><form id='sonda_form_"+ids+"'><table width='"+szer_tab+"'>";
				for(i=0; i<pytania.length; ++i){
					var nrp=i+1;
					zaw+="<tr><td class='radio' valign='top'><input type='radio' name='glos' value='"+nrp+"'></td><td>"+pytania[i]+"</td></tr>";
				}
				zaw+="<tr><td><div class='wiecej_sonda'><a href=\"javascript:W_PobierzSonde("+idw+",'"+div+"',"+szer_tab+","+wys_pkt+",'"+rodz_wyn+"','w', '"+wyn_text+"', '"+glos_text+"', '"+sonda_text+"', '"+ankieta_text+"')\">"+wyn_text+"&nbsp;&raquo;</a></span></td>";
				zaw+="<td><input style='float: right;' type='submit' class='submit' value='"+glos_text+"' onclick=\"W_PobierzSonde("+idw+",'"+div+"',"+szer_tab+","+wys_pkt+",'"+rodz_wyn+"','"+sonda+"', '"+wyn_text+"', '"+glos_text+"', '"+sonda_text+"', '"+ankieta_text+"' ,'sonda_form_"+ids+"',"+ids+");\"></td></tr>";
				
				zaw+="</table></form>";
			}
			zaw+="</div>";
			if(ids>0)
				W_PiszDiv(div, zaw);
			else
				W_PiszDiv(div, "");
		}
	});
}

function okno_param(url, name, param){
	window.open(url,name,param);
}

function okno(url, name, width, height){
	window.open(url,name,'align=center,toolbar=no,status=no,location=no,directories=no,resizable=no,scrollbars=yes,width='+width+',height='+height+',menubar=no');
}

function UkryjElement(element){
	var obiekt=document.getElementById(element);
	if(obiekt)
		obiekt.style.display = 'none';
}

function PokazElement(element){
	var obiekt=document.getElementById(element);
	if(obiekt)
		obiekt.style.display = '';
}

function PokazUkryj(element){
	if(element.style.display == 'none'){
		element.style.display = '';
	}
	else{
		element.style.display = 'none';
	}
}

function UkryjDiv(elementID){
	var obj = document.getElementById(elementID);
	if(obj)
        obj.style.visibility = "hidden";
}

function PokazDiv(elementID){
	var obj = document.getElementById(elementID);
	if(obj)
        obj.style.visibility = "visible";
}

function PokazUkryjDiv(element){
	if(element.style.visibility == 'hidden')
		element.style.visibility = 'visible';
	else
		element.style.visibility = 'hidden';
}

function W_ColapseObiekty(obj, rodzaj){
	var d=document.getElementById(obj);
	l=d.getElementsByTagName('div');
	wc_cont=[];
	wc_head=[];
	for(i=0;i<l.length;i++){
		h=l[i].id;
		if(h.substr(h.indexOf('-')+1,h.length)=='content'){
			wc_cont.push(h);
		}
		if(h.substr(h.indexOf('-')+1,h.length)=='header'){
			wc_head.push(h);
		}
	}	
	if(rodzaj=="h")
		return wc_head;
	else
		return wc_cont;
}

//Zastosowanie - menu rozwijalne
function W_Colapse(obj, id){
	if(id==null)
		id=0;
	var d=document.getElementById(obj);
	l=d.getElementsByTagName('div');
	//l=$(obj).getElementsByTagName('div');	
	wc_cont=[];
	wc_head=[];
	for(i=0;i<l.length;i++){
		h=l[i].id;
		if(h.substr(h.indexOf('-')+1,h.length)=='content'){
			wc_cont.push(h);
		}
		if(h.substr(h.indexOf('-')+1,h.length)=='header'){
			wc_head.push(h);
		}
	}
	
	for(j=0;j<wc_cont.length;j++){
		if(j==id){
			var el = document.getElementById(wc_cont[j]);
			el.style.display = '';
		}
		else{
			var el1 = document.getElementById(wc_cont[j]);
			el1.style.display = 'none';
		}
	}
	
	//zdarzenie onclick
	for(i=0;i<wc_head.length;i++){
		h=document.getElementById(wc_head[i]);
		h.onclick = function(){
			var wct_head=W_ColapseObiekty(this.parentNode.id, "h");
			var wct_cont=W_ColapseObiekty(this.parentNode.id, "c");
			var j=0;
			for(j=0;j<wct_cont.length;j++){
				if(this.id==wct_head[j]){
					var el = document.getElementById(wct_cont[j]);
					if ( el.style.display != "none" ) {
						var idx=j+1;
						var adr=W_CzytajDiv("dadr-"+idx);
						if(adr){
							location.href=adr;
						}
						else{
							el.style.display = 'none';
						}
					}
					else {
						el.style.display = '';
					}
				}
				else{
					var el1 = document.getElementById(wct_cont[j]);
					el1.style.display = 'none';
				}
			}
		}
	}
}

