
/**
 * 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 zaawansowana czesc formularza wyszukiwania
  */
function ShowHideAdvSearch()
{
	var fs = document.getElementById('adv_form');
	var rl = document.getElementById('roll');
 	if(rl && fs)
 	{
 		if(fs.style.display == 'none')
 		{
 			rl.className = 'roll_sel';
 			rl.innerHTML = 'zwiń';
 			sCookie('show_search', true, 0);
 		}
 		else
 		{
 			rl.className = 'roll';
 			rl.innerHTML = 'rozwiń';
 			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 + '%';

	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);
}

/**
 * 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=/'
}

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()
 {
 	var fs = document.getElementById('right_panel');
 	var fshidden = fs.style.display;
 	Element.toggle('right_panel');
 	var rl = document.getElementById('roll');
 	
 	if(rl && fs)
 	{
 		if(fshidden == 'none')
 		{
 			rl.className = 'roll';
 			document.getElementById('belka').style.width = '500px';
 			document.getElementById('mapa_content').style.width = '500px';
 			document.getElementById('map').style.width = '498px';
 			sCookie('show_right', true, 0);
 		}
 		else
 		{
 			rl.className = 'roll_sel';
 			document.getElementById('belka').style.width = '690px';
 			document.getElementById('mapa_content').style.width = '690px';
 			document.getElementById('map').style.width = '688px';
 			sCookie('show_right', '', -1);
 		}
 	}
 } 
 
function ShowHidePageContentRightGallery()
 {
 	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 = 'rozwiń';
 			document.getElementById('index_content_left').style.width = '446px';
 			document.getElementById('tresc_box').style.width = '432px';
 			sCookie('show_right', true, 0);
 		}
 		else
 		{
 			rl.className = 'roll_sel';
 			rl.innerHTML = 'zwiń';
 			document.getElementById('index_content_left').style.width = '658px';
 			document.getElementById('tresc_box').style.width = '644px';
 			sCookie('show_right', '', -1);
 		}
 	}	
 } 
function ShowHidePageContentRightGalleryFilm()
{
	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 = 'rozwiń';
			document.getElementById('index_content_left_film').style.width = '446px';
			document.getElementById('tresc_box_film').style.width = '432px';
			sCookie('show_right', true, 0);
		}
		else
		{
			r1.className = 'roll_sel';
			r1.innerHTML = 'zwiń';
			document.getElementById('index_content_left_film').style.width = '658px';
			document.getElementById('tresc_box_film').style.width = '644px';
			sCookie('show_right', '', -1);
		}
	}
}

/** rozwijanie artykulu Niepelnosprawni */

function ShowHidePageContentRightMion()
{
	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 = 'rozwiń';
			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 = 'zwiń';
			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';
}

