// -*- coding: utf-8 -*- 

// find form
function findKontrola() {
    if (document.formFind.txt.value.length < 2) {

	if (document.formFind.txt.value.match(/^\d$/) ) {
	    document.formFind.action = 'fulltext.act';
	    document.formFind.submit();

	} else {
	    alert(RS_STRING_HAS_NOT_2_CHARS);
	    document.formFind.txt.focus();
	}

    } else {
	document.formFind.action = 'fulltext.act';
	document.formFind.submit();
    } 
}

// edit message (discussion)
function msgKontrola() {

    if (document.newDiscusss.sub.value.length == 0) {
	alert(RS_MESSAGE_HEADER_MISSING);
	document.newDiscusss.sub.focus();

    } else if (document.newDiscusss.txt.value.length == 0) {
	alert(RS_MESSAGE_MISSING);
	document.newDiscusss.txt.focus();

    } else if (document.newDiscusss.name.value.length == 0) {
	alert(RS_NAME_MISSING);
	document.newDiscusss.name.focus();

    } else if (document.newDiscusss.email.value.length == 0) {
	alert(RS_EMAIL_MISSING);
	document.newDiscusss.email.focus();

    } else if ((document.newDiscusss.email.value.search('@') == -1) || (document.newDiscusss.email.value.search('\\\.') == -1)) {
	alert(RS_EMAIL_INVALID);
	document.newDiscusss.email.focus();

    } else {
	document.newDiscusss.action = 'pripominka-new.act';
	document.newDiscusss.submit();
    }
}

// new password form
function newPswdKontrola() {
    if (document.mainForm.login.value.length < 5) {
	alert(RS_USERNAME_HAS_NOT_5_CHARS);
	document.mainForm.login.focus();

    } else {
	document.mainForm.action = 'new-passwd2.act';
	document.mainForm.submit();
    } 
}

// delete a bookmark
function delBookmark(name) {
    var msg = RS_CONF_DEL_TAB + name + "' ?";
    return confirm(msg);
}

// return to a hidden iframe
function hiddenSubmit(url) {
    document.getElementById('KTPiFrame').location = url;
}

// adding a bookmark
function bookmarkAdded(bm) {
    window.parent.window.alert(RS_TAB + bm + RS_TAB_ADDED);
}

function bookmarkExists(bm) {
    window.parent.window.alert(RS_TAB + bm + RS_TAB_EXISTS);
}

//hide, show tabs

// init for examples
function showInit(what) {
    if (what == 'p') { show(what); hide('r','s'); }
    if (what == 'r') { show(what); hide('p','s'); }
    if (what == 's') { show(what); hide('r','p'); }
}

// init for free places
function showInit2(what) {
    if (what == 'o') { show(what); hide('k'); }
    if (what == 'k') { show(what); hide('o'); }
}

function show(what)
{
    document.getElementById(what).style.visibility = "visible";
    document.getElementById(what).style.display = "block";
    document.getElementById(what + "_tab").style.background = "#3da73d";
    document.getElementById(what + "_a").style.color = "#ddffdd";
}

function hide(what1, what2)
{
    document.getElementById(what1).style.visibility = "hidden";
    document.getElementById(what1).style.display = "none";
    document.getElementById(what1 + "_tab").style.background = "#DDFFDD";
    document.getElementById(what1 + "_a").style.color = "#237931";

    if (document.getElementById(what2 + "_tab")) {
      document.getElementById(what2).style.visibility = "hidden";
      document.getElementById(what2).style.display = "none";
      document.getElementById(what2 + "_tab").style.background = "#DDFFDD";
      document.getElementById(what2 + "_a").style.color = "#237931";
    }
}

function showInline(what) //show popup
{ 
    if (document.getElementById(what)) {
	document.getElementById(what).style.display = "";
	if (document.getElementById('no-' + what)) {
	    document.getElementById('no-' + what).style.display = "none";
	}
    }
}
function hideInline(what) //hide popup
{
    if (document.getElementById(what)) {
	if (document.getElementById('no-' + what)) {
	    document.getElementById('no-' + what).style.display = "";
	}
	document.getElementById(what).style.display = "none";
    }
}


function openISTP(url) {
    window.location.href = url;
    /*
    self.open(url, 'istp', 
                    'resizable=yes,toolbar=yes,menubar=yes,location=yes,status=yes,titlebar=yes,scrollbars=yes,directories=yes');
    */
}

// kontrola, zda se admin nechce sam odstrelit
function checkRoles(r) {
    var lh = document.mainForm.lh.value;
    var lhu = document.mainForm.lhu.value;
    var r50 = document.getElementById('R50').checked;

    if (!r50 && lh == lhu) {
	alert(RS_IS_ADMIN);
	document.getElementById('R50').checked = true;

    } else { 
	document.mainForm.submit();
    }
}

// check date:  DD.MM.YYYY  or  YYYY, 'year'  or  MM, 'month'
function checkDate(d) {
    return checkDate(d, 'date');
}

function checkDate(d, t) {
    if (typeof d == 'string') { // check string
        if (d.match(/^\s*$/)) { // empty
            alert(t == 'year' ? RS_YEAR_EMPTY :
                  t == 'month' ? RS_MONTH_EMPTY : RS_DATE_EMPTY);
            return false;
        } else if (t == 'year') { // year
            if (! d.match(/^\d+$/)) { // wrong
                alert(RS_YEAR_WRONG + d);
                return false;
            }
        } else if (t == 'month') { // month
            if (! (d.match(/^\d+$/) && 1 <= d * 1 && d * 1 <= 12)) { // wrong
                alert(RS_MONTH_WRONG + d);
                return false;
            }
        } else { // date
            if (! d.match(/^\s*(\d+)[.]\s*(\d+)[.]\s*(\d+)\s*$/)) { // wrong
                alert(RS_DATE_WRONG + d);
                return false;
            } else {
                var day = RegExp.$1;
                var month = RegExp.$2;
                var year = RegExp.$3;
                if (! (1 <= day * 1 &&
                       day * 1 <= (month.match(/^0*(1|3|5|7|8|10|12)$/) ? 31 :
                                   month.match(/^0*(4|6|9|11)$/) ? 30 :
                                   year * 1 % 4 == 0 && year * 1 % 100 != 0 ||
                                   year * 1 % 400 == 0 ? 29 : 28) &&
                       1 <= month * 1 && month * 1 <= 12)) { // wrong number(s)
                    alert(RS_DATE_WRONG + d);
                    return false;
                }
            }
        }
    } else if (typeof d == 'object') { // check document.getElementById(id)
        if (! checkDate(d.value, t)) {
            d.focus();
            return false;
        }
    } else { // err?!
        return false;
    }
    return true;
}

//==============================
// obarvovani navstivenych linku

function getCookie(name) {
    if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(name + "=");
        if (start != -1) {
            start = start + name.length + 1;
            var end = document.cookie.indexOf(";", start);
            if (end == -1) end = document.cookie.length;
            return document.cookie.substring(start, end);
        }
    }
    return '';
}

//namisto <a href="{$url}" pouzit href="javascript:gotoLink('url','id')",
// do elementu 'a' je treba doplnit atribut id="{$savedcookie}" 
function gotoLink(url, savedcookie, cookiename)
{
    if (savedcookie) {
	var tgt = cookiename || 'KTP_VISITEDLINKS';
        var val = getCookie(tgt);
        var forsave = encodeURIComponent(savedcookie);
        if (val.indexOf(forsave) == -1) {
             if (val != '') val = val + ',';
             document.cookie = tgt + '=' + val + forsave + ';Path=/';
        }
    }
    window.location.href = url;
}

function updateVisitedLinks(visitedclassname, cookiename) {
    var tgt = cookiename || 'KTP_VISITEDLINKS';
    var visited = getCookie(tgt);
    var ids = visited.split(',');
    var obj;
    for (var i = 0; i < ids.length; i++) {
	obj = document.getElementById(decodeURIComponent(ids[i]));
	if (obj) obj.className = visitedclassname;
    }
}
