
/**
 * funkcja sprawdzająca poprawność adresu email
 * 
 * @param string
 *            email
 */
function checkEmail(email) {
	var reg_test = /^[\w\.-]{2,}@[\w\.-]+\.[a-z]{2,5}$/i.test(email);
	if (!reg_test)
		return false;
	return true;
}

/**
 * funkcja sprawdzająca poprawność godziny
 * 
 * @param string
 *            h
 */
function checkHour(h) {
	/* var reg_test = /^[0-9]{1,2}\:[0-9]{2}\:[0-9]{2}$/i.test(h); */
	var reg_test = /^[0-9]{1,2}\:[0-9]{2}$/i.test(h);
	if (!reg_test)
		return reg_test;
	var val = h.split(':');
	if (val[0] > 23 || val[0] < 0 || val[1] > 59 || val[1] < 0 || val[2] > 59
			|| val[2] < 0)
		return false;
	return true;
}

/**
 * funkcja sprawdzająca poprawność numeru telefonu
 * 
 * @param string
 *            pN
 */
function checkPhoneNumber(pN) {
	var reg_test = /^[0-9]{10}$/i.test(pN);
	if (!reg_test)
		return false;
	return true;
}

/**
 * funkcja sprawdzająca poprawność adresu WWW
 * 
 * @param string
 *            www
 */
function checkWWW(www) {
	var reg_test = /^(http:\/\/|www\.).*/i.test(www);
	if (!reg_test)
		return false;
	return true;
}

/**
 * funkcja sprawdzająca poprawność kodu pocztowego
 * 
 * @param string
 *            zC
 */
function checkZipCode(zC) {
	var reg_test = /^[0-9]{2}\-[0-9]{3}$/i.test(zC);
	if (!reg_test)
		return false;
	return true;
}

/**
 * funkcja sprawdzająca, czy długość string-a zawiera się w przedziale
 * 
 * @param string
 *            str
 * @param int
 *            from
 * @param int
 *            to
 */
function checkLength(str, from, to) {
	if (str.length >= from && str.length <= to)
		return true;
	return false;
}

/**
 * funkcja sprawdzająca poprawność loginu uzytkownika
 * 
 * @param string
 *            login
 */
function checkLogin(login) {
	var reg_test = /^[\w\.-]{8,50}$/i.test(login);
	if (!reg_test)
		return false;
	return true;
}

/**
 * funkcja sprawdzająca zgodność z typem int
 * 
 * @param {Object}
 *            value
 */
function checkInt(value) {
	var reg_test = /^[\-]{0,1}[0-9]{1,8}$/i.test(value);
	if (!reg_test)
		return false;
	return true;
}

/**
 * Backtrace dla funkcji JavaScript
 * 
 * @param {Object}
 *            func
 */

function getStackTrace(func) {

	if (!func)
		return "not a function!";
	var trace = getFuncName(func);
	var args = "(";
	for ( var arg in func.arguments) {
		if (args && args.length && args.length > 1)
			args += ",";
		args += func.arguments.toString();
	}

	trace += args + ")\n";
	return trace + getStackTrace(func.caller);
}

/**
 * funcka wychwytująca błędy
 */

function handleErrors(errStr, url, lineNum) {

	document.getElementById('JSError').innerHTML = getStackTrace();
}
// window.onerror=handleErrors;

/**
 * funkcja potwierdzająca wykonaną akcję
 * 
 */
function confirmSubmit(str) {
	var agree = confirm(str);

	if (agree)
		return true;
	return false;
}

/**
 * funkcja zwalnia aktywne zakladki, przyciski i aktywuje wybrana jako aktywna
 * (newsy na stronie glownej, box galerii strony glownej) ile - calkowita ilosc
 * zakladek cur - numer zakladki do aktywacji id - tekstowy przedrostek id
 * zakladek, przyciskow
 */
function activateGroup(ile, cur, id) {
	for ( var i = 0; i <= ile; i++) {
		if (document.getElementById(id + i))
			document.getElementById(id + i).className = '';
	}
	;
	document.getElementById(id + cur).className = 'sel';
}

// ==========================================
// Check All boxes
// ==========================================
function CheckAll(fmobj)
{
	for(var i=0; i<fmobj.elements.length; i++)
	{
		var e=fmobj.elements[i];
		if((e.type == 'checkbox') && (!e.disabled) && (e.id != 'search_select_all') && (e.id != 'search_aktualne') && (e.id != 'search_archiwalne'))
		{
			e.checked = document.getElementById('search_select_all').checked;
		}
	}
}

// ==========================================
// Check all or uncheck all?
// ==========================================
function CheckCheckAll(fmobj)
{
	var TotalBoxes = 0;
	var TotalOn = 0;
	for(var i=0; i<fmobj.elements.length; i++)
	{
		var e=fmobj.elements[i];
		if((e.type == 'checkbox') && (e.id != 'search_select_all') && (e.id != 'search_aktualne') && (e.id != 'search_archiwalne'))
		{
			TotalBoxes++;
			if(e.checked) TotalOn++;
		}
	}
	if(TotalBoxes == TotalOn)
		document.getElementById('search_select_all').checked = true;
	else
		document.getElementById('search_select_all').checked = false;
}

/**
 * funkcja ulubionych
 */
function ulubione(obj) {
	var title = document.title;
	var url = document.location;
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
		obj.href = "#";
		return false;
	} else if (window.external) {
		window.external.AddFavorite(url, title);
		obj.href = "#";
		return false;
	} else if (window.opera && window.print) {
		obj.rel = 'sidebar';
		obj.title = title;
		obj.href = url;
		return true;
	}
	return false;
}

/**
 * Funkcja wyświetla obiekt flash w miejscu wywołania konieczna by zachowac
 * walidacje xhtml
 */
function flashFix(o)
{
	document.write(o)
}


/**
 * Funkcja pokazuje lub ukrywa zawartosc zakladek w menu glownym panelu
 * administracyjnego
 */
function ShowHideMenuItems(id)
{
	if(document.getElementById(id))
	{
		new Effect.toggle(id, 'blind', {duration: 0.5});
		if(document.getElementById(id).style.display == 'none')
			sCookie(id, true, 365);
		else
			sCookie(id,'',-1);
	}
}

/**
 * Funkcja pokazuje lub ukrywa menu admina i rozszerza drzewko struktury portalu
 */
function ShowHideMainMenu()
{
	var mm_id='cms_left_panel'; //id menu glownego
	var mm = document.getElementById(mm_id); //menu glowne
	var ct = document.getElementById('content'); //content
	var cp = document.getElementById('cms_center_panel'); //panel centralny
	var kl = document.getElementById('kat_list'); //lista struktury portalu
	var gl = document.getElementById('cms_center_panel_content'); //lista struktury galerii
	var il = document.getElementById('info_list'); //lista struktury informatora

	if(mm)
	{
		if(mm.style.display == 'none')
		{
			mm.style.display = 'block';
			if(ct) ct.style.width = ct.clientWidth - 175 + 'px';
			if(cp) cp.style.width = cp.clientWidth - 175 + 'px';
			if(kl) kl.style.width = kl.clientWidth - 178 + 'px';
			if(gl) gl.style.width = gl.clientWidth - 178 + 'px';
			if(il) il.style.width = il.clientWidth - 178 + 'px';
		}
		else
		{
			mm.style.display = 'none';
			if(ct) ct.style.width = ct.clientWidth + 175 + 'px';
			if(cp) cp.style.width = cp.clientWidth + 175 + 'px';
			if(kl) kl.style.width = kl.clientWidth + 170 + 'px';
			if(gl) gl.style.width = gl.clientWidth + 170 + 'px';
			if(il) il.style.width = il.clientWidth + 170 + 'px';
		}
	}
}

 /**
  * Funkcja pokazuje lub ukrywa zaawansowana czesc formularza wyszukiwania
  */
function ShowHideAdvSearch(txtc,txto)
{
	var fs = document.getElementById('adv_form');
	var rl = document.getElementById('rollas');
 	if(rl && fs)
 	{
 		if(fs.style.display == 'none')
 		{
 			rl.className = 'roll_sel';
 			rl.innerHTML = txto;
 			sCookie('show_search', true, 0);
 		}
 		else
 		{
 			rl.className = 'roll';
 			rl.innerHTML = txtc;
 			sCookie('show_search', '', -1);
 		}
 	}
	new Effect.toggle('adv_form', 'blind', {duration: 0.5});
}

/**
 * Funkcja do powiekszania i zmniejszania czcionki
*/
var pointer=0;
function FontSize(mode)
{
	var levels=new Array(100,115,130);
	var pointerold=parseInt(gCookie("textsize"));
	if(mode=='up')
	{
		if(pointer<2)pointer++;
	}
	else if(mode=='down')
	{
		if(pointer>0)pointer--;
	}
	else if(mode=='def')
	{
		pointer=0;
	}
	else
	{
		 pointer=parseInt(gCookie("textsize"));
		 if(isNaN(pointer)) pointer=0;
	}
	var fontval = levels[pointer];

	//if (document.getElementById('news_box')) document.getElementById('news_box').style.fontSize = fontval + '%';
	//if (document.getElementById('tresc_box')) document.getElementById('tresc_box').style.fontSize = fontval + '%';
	//if (document.getElementById('zajawki_box')) document.getElementById('zajawki_box').style.fontSize = fontval + '%';
	//if (document.getElementById('category_box')) document.getElementById('category_box').style.fontSize = fontval + '%';
	//if (document.getElementById('promoted_box')) document.getElementById('promoted_box').style.fontSize = fontval + '%';
	//if (document.getElementById('help_box')) document.getElementById('help_box').style.fontSize = fontval + '%';
	//if (document.getElementById('menu_box')) document.getElementById('menu_box').style.fontSize = fontval + '%';
	
	var elements = $$(
			'#navigator', 
			//dodaj klase resize_font np do zajawek
			'.resize_font',
			//elementy tresci tworzonej za pomoca tinymce
			'p',
			'._Lista_punktowana',
			'._Lista_numerowana',
			'._Lista_punktowana2',
			'._Lista_numerowana2'
	);
	
	if(elements)
	{
		for(var i = 0; i < elements.length; i++)
		{
			elements[i].style.fontSize = fontval + '%';
		}
	}
	
	if (!isNaN(pointer) && pointer != pointerold)
	{
		sCookie('textsize', '', -1);
		sCookie('textsize', pointer, 1);
	}
}

function FontSizeRestore()
{
	 var pointer=parseInt(gCookie("textsize"));
	 if(isNaN(pointer)) pointer=0;
	 FontSize();
}

function SetContrast()
{
	 var contrast=gCookie("contrast");
	 if(contrast == 'true')
		 sCookie('contrast','',0);
	 else
		 sCookie('contrast',true,0);
	 location.reload(true);
}


function replEmail(user, site)
{
	document.write('<a href=\"mailto:' + user + '@' + site + '\">');
	document.write(user + '@' + site + '<\/a>');
}
/**
 * Funkcja do wyboru ikonki przy dodawaniu obiektu od strony internauty
 * Drugie wywołanie jest w admin.js
*/
function changeIcon(src,target, self){
	document.getElementById(target).value = src;
	document.getElementById('selected_icon').src = self.src;
}
 /**
 * Funkcja pokazuje lub ukrywa prawy panel
 */
function ShowHidePageContentRight(txtc,txto)
{
	var fs = document.getElementById('page_content_right');
	var fshidden = fs.style.display;
	Element.toggle('page_content_right');
	var rl = document.getElementById('roll');
	var r2 = document.getElementById('mapa_content_left');
	var r3 = document.getElementById('mapa_content_tresc');
	var r4 = document.getElementById('map');
	if(rl && fs)
	{
		if(fshidden == 'none')
			
		{
			rl.className = 'roll';
			r2.className = 'mapa_content_left';
			r3.className = 'mapa_content_tresc';
			r4.className = 'mapa';
			rl.innerHTML = txtc;
			document.getElementById('mapa_content_left').style.width = '446px';
			document.getElementById('mapa_content_tresc').style.width = '432px';
			document.getElementById('map').style.width = '430px';
			sCookie('show_right_mapa', '', -1);
		}
		else
		{
			rl.className = 'roll_sel';
			r2.className = 'mapa_content_left_roll';
			r3.className = 'mapa_content_tresc_roll';
			r4.className = 'mapa_roll';
			rl.innerHTML = txtc;
			document.getElementById('mapa_content_left').style.width = '752px';
			document.getElementById('mapa_content_tresc').style.width = '738px';
			document.getElementById('map').style.width = '736px';
			sCookie('show_right_mapa', true, 0);
		}
	}
} 

function ShowHidePageContentRightOfid(txtc,txto)
{
	var fs = document.getElementById('page_content_right');
	var fshidden = fs.style.display;
	Element.toggle('page_content_right');
	var rl = document.getElementById('roll');
	var r2 = document.getElementById('mapa_content_left');
	var r3 = document.getElementById('mapa_content_tresc');
	var r4 = document.getElementById('map');
	if(rl && fs)
	{
		if(fshidden == 'none')
			
		{
			rl.className = 'roll';
			r2.className = 'mapa_content_left';
			r3.className = 'mapa_content_tresc';
			r4.className = 'mapa';
			rl.innerHTML = txtc;
			document.getElementById('mapa_content_left').style.width = '446px';
			document.getElementById('mapa_content_tresc').style.width = '432px';
			document.getElementById('map').style.width = '430px';
			sCookie('show_right_ofid', '', -1);
		}
		else
		{
			rl.className = 'roll_sel';
			r2.className = 'mapa_content_left_roll';
			r3.className = 'mapa_content_tresc_roll';
			r4.className = 'mapa_roll';
			rl.innerHTML = txtc;
			document.getElementById('mapa_content_left').style.width = '752px';
			document.getElementById('mapa_content_tresc').style.width = '738px';
			document.getElementById('map').style.width = '736px';
			sCookie('show_right_ofid', true, 0);
		}
	}
} 
 
function ShowHidePageContentRightGallery(txtc,txto)
 {
 	var fs = document.getElementById('index_content_right');
 	var fshidden = fs.style.display;
 	Element.toggle('index_content_right');
 	var rl = document.getElementById('roll');

 	if(rl && fs)
 	{
 		if(fshidden == 'none')
 		{
 			rl.className = 'roll';
 			rl.innerHTML = txtc;
 			document.getElementById('page_content_center').style.width = '516px';
 			document.getElementById('tresc_box').style.width = '516px';
 			sCookie('show_right_galp', '', -1);
 		}
 		else
 		{
 			rl.className = 'roll_sel';
 			rl.innerHTML = txto;
 			document.getElementById('page_content_center').style.width = '744px';
 			document.getElementById('tresc_box').style.width = '744px';
 			sCookie('show_right_galp', true, 0);
 		}
 	}	
 } 

function ShowHidePageContentRightIGallery(txtc,txto)
{
	var fs = document.getElementById('index_kalimp_content_center');
	var fshidden = fs.style.display;
	Element.toggle('index_kalimp_content_center');
	var rl = document.getElementById('roll');

	if(rl && fs)
	{
		if(fshidden == 'none')
		{
			rl.className = 'roll';
			rl.innerHTML = txtc;
			document.getElementById('index_content_left').style.width = '446px';
			document.getElementById('tresc_box').style.width = '432px';
			sCookie('show_right_galpi', '', -1);
		}
		else
		{
			rl.className = 'roll_sel';
			rl.innerHTML = txto;
			document.getElementById('index_content_left').style.width = '658px';
			document.getElementById('tresc_box').style.width = '644px';
			sCookie('show_right_galpi', true, 0);
		}
	}	
} 
function ShowHidePageContentRightGalleryFilm(txtc,txto)
{
	var fs = document.getElementById('index_kalimp_content_center_film');
	var fshidden = fs.style.display;
	Element.toggle('index_kalimp_content_center_film');
	var r1 = document.getElementById('roll1');
	
	if(r1 && fs)
	{
		if(fshidden == 'none')
		{
			r1.className = 'roll';
			r1.innerHTML = txtc;
			document.getElementById('index_content_left_film').style.width = '446px';
			document.getElementById('tresc_box_film').style.width = '432px';
			sCookie('show_right_medi', '', -1);
		}
		else
		{
			r1.className = 'roll_sel';
			r1.innerHTML = txto;
			document.getElementById('index_content_left_film').style.width = '658px';
			document.getElementById('tresc_box_film').style.width = '644px';
			sCookie('show_right_medi', true, 0);
		}
	}
}

/** rozwijanie artykulu Niepelnosprawni */

function ShowHidePageContentRightMion(txtc,txto)
{
	var fs = document.getElementById('page_content_right');
	var fshidden = fs.style.display;
	Element.toggle('page_content_right');
	
	var rl = document.getElementById('roll');
	if(rl && fs)
	{
		if(fshidden == 'none')
		{
			rl.className = 'roll';
			rl.innerHTML = txtc;
			document.getElementById('mion_content_left').style.width = '446px';
			document.getElementById('index_content_tresc').style.width = '432px';
			sCookie('show_right_mion', true, 0);
		}
		else
		{
			rl.className = 'roll_sel';
			rl.innerHTML = txto;
			document.getElementById('mion_content_left').style.width = '752px';
			document.getElementById('index_content_tresc').style.width = '738px';
			sCookie('show_right_mion', '', -1);
		}
	}
	
} 


/**
* Funkcja do zmiany obrazka captcha jezeli nie da sie odczytac
*/
function reloadCaptcha(url)
{
	var losowa = Math.round(Math.random(0)*1000)+1;
	$("captcha_img").src=url.substr(0, url.length-5)+'/id/'+losowa+'.html';
}

/**
* Otwiera nowe okno o podanych wymiarach a w nim podany adres
*/
function NoweOkno(url,szer,wys)
{
	if(typeof(szer)!='number') szer=640;
	if(typeof(wys)!='number') wys=360;
	noweOkno = window.open(url, 'okienko', 'menubar=no, toolbar=no, location=no, scrollbars=no, resizable=yes, status=no, width='+szer+', height='+wys+', left=150, top=150');
	noweOkno.focus();
}

function sprawdzAnkiete(params,num,btxt)
{
var ile_pyt = 0;

for (key in params){
  if(typeof(params[key]) == 'string')
  {
	  if(trim(params[key]) == '')
	  {
	  		alert(btxt);
	  		return 0;//pusty input text
	  }
  }
  ile_pyt++;
}
ile_pyt = ile_pyt-3;//nie wazne 3 parametry, czyli id, vote, i commit bo nie sa pytaniami

	if(ile_pyt != num)
	{
		alert(btxt);
		return 0;//puste pytanie
	}
	return 1;
}

function trim(str) {
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
/**
 * Funkcja odczytuje cookie
 * @param N - nazwa cookie
 */
function gCookie(N,x)
{
	if((x=document.cookie)&&(N=(';'+x).match(new RegExp('(;| )'+N+'=[^;]+'))))
	return unescape(N[0].split(/=/g)[1])
}

/**
 * Funkcja ustawia cookie
 * @param N - nazwa cookie
 * @param V - wartosc cookie
 * @param D - czas waznosci w dniach cookie ([0]-wazne do zamkniecia przegladarki, [-1]-usuwa cookie)
 */
function sCookie(N,V,D)
{
	document.cookie=N+'='+escape(V)+(D?'; expires='+new Date(+new Date()+D*864e5).toGMTString():'')+'; path=/'
}
