/**
 * 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 - 162 + '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 + 190 + 'px';
        }
    }
}

/**
  * Funkcja pokazuje lub ukrywa zaawansowana czesc formularza wyszukiwania
  */
function ShowHideAdvSearch()
{
    var fs = document.getElementById('adv_form');
    var rl = document.getElementById('rollas');
    if(rl && fs)
    {
        if(fs.style.display == 'none')
        {
            rl.className = 'roll_sel';
            sCookie('show_search', true, 0);
        }
        else
        {
            rl.className = 'roll';
            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()
{
    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('page_content_center');
    var r3 = document.getElementById('page_content_tresc');
    var r4 = document.getElementById('map');
    if(rl && fs)
    {
        if(fshidden == 'none')
			
        {
            rl.className = 'roll';
            r2.className = 'page_content_center';
            r3.className = 'page_content_tresc';
            r4.className = 'mapa';
            //rl.innerHTML = txtc;
            document.getElementById('page_content_center').style.width = '509px';
            document.getElementById('page_content_tresc').style.width = '503px';
            document.getElementById('map').style.width = '503px';
            sCookie('show_right_mapa', '', -1);
        }
        else
        {
            rl.className = 'roll_sel';
            r2.className = 'page_content_center_roll';
            r3.className = 'page_content_tresc_roll';
            r4.className = 'mapa_roll';
            //rl.innerHTML = txto;
            document.getElementById('page_content_center').style.width = '749px';
            document.getElementById('page_content_tresc').style.width = '743px';
            document.getElementById('map').style.width = '743px';
            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 = '444px';
            document.getElementById('map').style.width = '444px';
            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 = txto;
            document.getElementById('mapa_content_left').style.width = '752px';
            document.getElementById('mapa_content_tresc').style.width = '750px';
            document.getElementById('map').style.width = '750px';
            sCookie('show_right_ofid', true, 0);
        }
    }
} 
 
function ShowHidePageContentRightGallery(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('page_content_center').style.width = '509px';
            document.getElementById('tresc_box').style.width = '100%px';
            sCookie('show_right_galp', '', -1);
        }
        else
        {
            rl.className = 'roll_sel';
            rl.innerHTML = txto;
            document.getElementById('page_content_center').style.width = '749px';
            document.getElementById('tresc_box').style.width = '100%px';
            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]) == '' && key != 'commit')
            {
                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=/'
    }

    if(typeof Position != 'undefined')
    {
        Position.getWindowSize = function(w)
        {
            var width, height;
            w = w ? w : window;
            this.width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
            this.height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
            return this;
        }
    }

    //Pokazuje baner popup
    function pokaz_baner(id)
    {
        var aid = 'ads_'+id;
        var iid = 'adsinterval_'+id;
        iid = setInterval("center_element('"+aid+"')", 100);
        document.getElementById(aid).style.display='block';
    }

    //Ukrywa baner popup
    function ukryj_baner(id)
    {
        document.getElementById('ads_'+id).style.display='none';
        clearInterval('adsinterval_'+id);
        sCookie('popup_'+id, 1, 0);
    }

    // Centruje poziomo i pionowo element "id"
    function center_element(id)
    {
        var wys = 0;
        var szer = 0;
        var dwys = 0;
        var dszer = 0;
        var offx = 0;
        var offy = 0;
        var left = 0;
        var top = 0;
        if(typeof(window.innerWidth)=='number')
        {
            szer = window.innerWidth / 2;
            wys = window.innerHeight / 2;
        }
        else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
        {
            szer = document.documentElement.clientWidth / 2;
            wys = document.documentElement.clientHeight / 2;
        }
        else if(document.body && (document.body.clientWidth || document.body.clientHeight))
        {
            szer = document.body.clientWidth / 2;
            wys = document.body.clientHeight / 2;
        }
        if(typeof(window.pageYOffset)=='number')
        {
            offx = window.pageXOffset;
            offy = window.pageYOffset;
        }
        else if(document.body && (document.body.scrollLeft || document.body.scrollTop))
        {
            offx = document.body.scrollLeft;
            offy = document.body.scrollTop;
        }
        else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
        {
            offx = document.documentElement.scrollLeft;
            offy = document.documentElement.scrollTop;
        }
        dszer = document.getElementById(id).clientWidth / 2;
        dwys = document.getElementById(id).clientHeight / 2;
        left = Math.round(szer - dszer + offx);
        top = Math.round(wys - dwys + offy);
        document.getElementById(id).style.position = 'absolute';
        document.getElementById(id).style.top = top+'px';
        document.getElementById(id).style.left = left+'px';
    }


    function checkAllBoxes(checked, className)
    {
        var boxes = $$('input.'+className+'[type="checkbox"]');
        boxes.each(function(box){
            box.checked = checked;
        })
    }

    function manageInput(element) {
        element.onfocus = function() {
            if(element.value == element.defaultValue)element.value = "";
        }
        element.onblur = function() {
            if(element.value == "")element.value = element.defaultValue;
        }
    }

    function copyElement(from_id, to_id, delete_source){
        if($(to_id) && $(to_id)){
            $(to_id).innerHTML = $(from_id).innerHTML;

            if(delete_source){
                $(from_id).remove();
            }
        }
    }

	//	ścieżka do pliku list.php
function listURL(id) {
	return ('/list.php');
}
function katalogZObrazkami(id) {
	return ('/images/baner/');
}
//	domena
function domenaURL(ID) {
	return top.location.host;
	//return 'http://zukowo.pl/';
}
//	data
function dayDate(id) {
	return ('dzień do wyświetlenia');
}
//	czy obrazki mają się scrollować
function scrollPictures(id) {
	return (true);
}
//	czy rozmiar obrazka ma być dopasowywany
function autoFitPictures(id) {
	return (true);
}
//	prędkość przesuwania obrazka
function scrollSpeed(id) {
	return (1.5);
}
//	granica miękkich zwrotów podczas scrollu
function scrollSwitchSmotthness(id) {
	return (30);
}
//	czas pomiÄ™dzy zmianami obrazków
function slideShowTime(id) {
	return (24000);
}
//	prędkość przejść zmian obrazków
function fadeSpeed(id) {
	return (0.02);
}
