isNS = (navigator.appName == 'Netscape');
isNS4 = (isNS && parseInt(navigator.appVersion) <= 4);

function fth()
{
  document.writeln('<tr><td class="FTH">Konference</td>');
    document.writeln('<td class="FTH" width="10%" align="center">Tēmas</td>');
    document.writeln('<td class="FTH" width="10%" align="center">Atbildes</td>');
    document.writeln('<td class="FTH" width="18%" align="center">Pēdējā&nbsp;atbilde</td>');
    document.writeln('<td class="FTH" width="10%" align="center">Moderātors</td></tr>');
}

function dg(name, desc)
{
  if( desc != '')
    name += " (" + desc + ")";
  document.writeln('<tr><td colspan="5" class="GROUP">' + name + '</td></tr>');
}

function df(id, name, desc, msglimit, msgcnt, repcnt, msubj, mauth, mdate, mods, fav_mode)
{
    document.writeln('<tr><td class="C1">&nbsp;&nbsp;&nbsp;<b><a href="forum.php?id=' + id + '">'
        + name + '</a></b><span class="DESC1">');
    if( msglimit > 0)
        document.writeln('&nbsp;[' + msglimit + ']');
    switch (fav_mode)
    {
        case 0:
            break;
        case 1:
            document.writeln('&nbsp;[<a style="text-decoration: none" href="ffav.php?func=add&id=' + id + '">+</a>]');
            break;
        case 2:
            document.writeln('&nbsp;[<a style="text-decoration: none" href="ffav.php?func=del&id=' + id + '">-</a>]');
            break;
        default:            
            break;
    }
    document.writeln('</span><div class="DESC1">&nbsp;&nbsp;&nbsp;' + desc + '</span></td>');
    document.writeln('<td class="C2" align="center">' + msgcnt + '</td>');
    document.writeln('<td class="C1" align="center">' + repcnt + '</td>');
    text = "&nbsp;";
    c_class = "SMALL2";
    if( msubj.length != 0)
        text = msubj + ' (' + mauth + ')<br>' + mdate;
    else if (mdate.length != 0)
    {
        text = mdate; //c_class = "C2";
    }
    document.writeln('<td class="' + c_class + '" align="center">' + text + '</td>');
    if( mods == '')
        mods = '&nbsp;';
    document.writeln('<td class="C1" align="center">'+ mods +'</td></tr>');
}

function makePagesLine(forumId, id, last)
{
    s = "";
    t1 = Math.min(4, last); t2 = Math.max(t1 + 1, last - 2);
    url = ' <a class="PAGENUM" href="topic.php?id=' + forumId + ':' + id + '&page=';
    for( i = 2; i <= t1; i++) {
      s += url + i + '">' + i + '</a>';
    }
    if (t1 + 1 != t2)
    {
      s += ' ...';
    }
    for( i = t2; i <= last; i++) {
      s += url + i + '">' + i + '</a>';
    }
    return s;
}


function topicTableHeader(author, subject)
{
  document.write('<tr><td class="FTH">&nbsp;</td>');
  document.write('<td class="FTH" width="79%"><nobr>Tēma</nobr></td>');
  document.write('<td class="FTH" width="5%" align="center"><nobr>Autors</nobr></td>');
  document.write('<td class="FTH" width="5%" align="center"><nobr>Atbildes</nobr></td>');
  document.write('<td class="FTH" width="5%" align="center"><nobr>Skatīts</nobr></td>');
  document.write('<td class="FTH" width="10%" align="center"><nobr>Pēdējā&nbsp;atbilde</nobr></td>');
  document.writeln('</tr>');
}

var m_images = new Array(
        'pinpoint.gif', 13, 16,
        'lock.gif', 9, 11,
        'message.gif', 13, 16
    );

function t(id, subject, preview, author, replNum, lastRep, forumId, img, viewNum)
{
  document.write('<tr><td class="C2" align="center">');
  document.write('<img src="images/' + m_images[3*img] + '" width="');
  document.write(m_images[3*img+1] + '" height="' + m_images[3*img+2] + '"></td>');
  document.write('<td class="C1"><b><a href="topic.php?id=' + forumId + ':' + id);
  document.write('">' + subject + '</a></b>');

  numPages = Math.floor((replNum+10)/10);
  if( numPages > 1) {
    document.write("&nbsp;&nbsp;");
    document.write(makePagesLine(forumId, id, numPages));
  }

  if( preview != '')
      document.write('<div class="DESC1">' + preview + '</div>');
  document.write('</td>');
  document.write('<td class="SMALL2" align="center">' + author + '</td>');
  document.write('<td class="C1" align="center">' + replNum + '</td>');
  document.write('<td class="C1" align="center">' + viewNum + '</td>');
  document.write('<td class="SMALL2" align="center"><nobr>' + lastRep + '</nobr></td>');
  document.writeln('</td></tr>');
}

function mth(author, subject)
{
  document.write('<table border="0" width="100%" cellpadding="3"><tr><td class="FTH">');
  document.writeln(author + ': ' + subject + '</td></tr></table>');
}

function addOnTrue(cond, color, link, str, targetw, buf)
{
  if( cond) {
    if( buf.length > 0)
      buf += "&nbsp;&#8226;&nbsp;";
    buf += '<a href="' + link + '" class="MSGI' + color + '"';
    if( targetw != '')
      buf += ' target="' + targetw + '"';
    buf += '>' + str + '</a>';
  }
  return buf;
}

function m(id, fid, color, author, status, date, flags, text, avatar)
{
  document.writeln('<a name="#' + id + '"></a><table border="0" cellpadding="3" width="100%">');
  document.write('<tr><td width="18%" valign="top" class="C' + color + '" cellpadding="2">');
  document.write('<b><a class="NAME' + color + '" href="javascript:putNick(\'' + author +
        '\')">' + author + '</a></b><div class="DESC' + color + '">' + status);
  document.write('<br><div style="margin-top: 7px">');
  if (avatar)
      document.write('<img style="margin-left: 1px; margin-bottom: 5px;" alt="' + author + '" src="avatar.php?name=' + author + '"  width="48" height="48">');
  document.write('</div></div>');
  document.write('</td><td valign="top" align="center" class="C' + color + '">');

  colspan = 2;

  document.write('<table width="99%" border="0" width="100%" cellpadding="0" cellspacing="0"');
  document.write(' class="DESC' + color + '"><tr><td class="DESC' + color + '">');
  document.write('<a href="#' + id + '" class="MSGI' + color +'">Nosūtīts</a>: ' + date);
  if( flags & 16) {
    document.write('</td><td align="center" class="DESC' + color + '"><b>[IZDZĒSTS]</b>');
    colspan = 3;
  }
  document.write('</td><td align="right" class="DESC' + color + '">');

  tid = '' + fid + ':' + id; 
  s = addOnTrue(flags & 2, color, 'ip.php?id='+tid, 'IP', '',
        addOnTrue(flags & 8, color, 'delete.php?id='+tid, 'Izdzēst', '',
        addOnTrue(flags & 4, color, 'editpost.php?id='+tid, 'Grozīt', '',
        addOnTrue(flags & 1, color, 'reginfo.php?name='+author, 'Info', 'reginfo', ''
      ))));
  document.write(s + '</td></tr>');
  document.write('<tr><td colspan="'+colspan+'" class="C' + color + '"><hr size="1">' + text + '</td></tr></table>');

  document.writeln('</td></tr>');
  document.writeln('</table>');
}

function insertAtCaret(textEl,text)
{
    textEl.value += text;
  textEl.focus();
}


function pasteSel()
{
  text = "";
  if( isNS)
    text = document.getSelection();
  else {
    r = document.selection.createRange();
    if( r)
      text = r.text;
  }

  if( text != '')
    insertAtCaret(document.msgform.message, '[i]' + text + '[/i]\n');
}

function putNick(nick)
{
  insertAtCaret(document.msgform.message, "[b]" + nick + "[/b]\n");
}

function ins(code)
{
  insertAtCaret(document.msgform.message, code);
}

function goToForum()
{
  ctl = document.gotoForm.gotoCtrl;
  if (ctl.selectedIndex >= 0 && ctl.options(ctl.selectedIndex).value != '')
    location.href = 'forum.php?id=' + ctl.options(ctl.selectedIndex).value;
}

function trim(s)
{
  var re = new RegExp("(^\\s*)|(\\s*$)", "g");
  return s.replace(re, '');
}

function checkIfEmpty(field)
{
  field.value = trim(field.value);
  if( field.value.length > 0)
    return true;
  field.focus();
  alert("Ko īsti Jūs vēlējāties pateikt?");
  return false;
}

function checkLength(field)
{
  if( isNS4)
    return true;
  var re = /[\S]{120,}/g;
  s = '' + field.value;
  if( s.match(re)) {
    field.focus();
    alert("Kāds vārds ir pārāk garš");
    return false;
  }
  return true;
}
