disKeyTrap = false;
document.onkeydown = CtrlLeftRightHandler;

function CtrlLeftRightHandler(evt)
 {
  if (!evt) evt = window.event;

  if (evt.ctrlKey && !disKeyTrap)
   {
    switch(evt.keyCode)
     {
      case 37:
       if (a = $('previous_page')) location.href = a.href;
       break;
      case 39:
       if (a = $('next_page')) location.href = a.href;
       break;
     }
   }
 }

function ReloadCaptcha(id, url)
 {
  $(id).src = url + '&' + Math.random();

  return false;
 }

function Helper(id)
 {
  width = 400;
  height = 350;

  var left = Math.round((document.body.clientWidth - width) / 2);
  var top = Math.round((document.body.clientHeight - height) / 2);

  var win = window.open(absbaseurl + 'helper.html?id=' + id,
                        'printhelper', 
                        'toolbar=no,' +
                        'scrollbars=yes,' +
                        'status=no,' +
                        'height=' + height + ',' +
                        'width=' + width + ',' + 
                        'top=' + top + ',' +
                        'left=' + left);

  win.focus();

  return false;
 }

/* votes */

function SubmitPollVote(cb, postID, variantID, full_body, tags_prefix)
 {
  params = 'variantID=' + variantID + '&' + 
           'postID=' + postID + '&' + 
           'digest=' + digest + '&' +
           'tags_prefix=' + encodeURIComponent(tags_prefix) + '&' +
           'full_body=' + full_body;

  new Ajax.Updater('post' + postID,
                   '/ajax/poll_vote.html?' + Math.random(),
                   {asynchronous: true,
                    method: 'post',
                    parameters: params });

  cb.disabled = true;

  return false;
 }

/* textarea's shit support routines */

function taTwin(id, b, open, close)
 {
  var ta = $(id);
  ta.focus();

  if (document.selection)
   {
    theSelection = document.selection.createRange().text;

    if (theSelection) 
     {
      ta.focus();

      document.selection.createRange().text = open + theSelection + close;

      theSelection = '';

      return false;
     }
   }
  else
   {
    ta.value += open + close;

    return false;
   }

  if (b.tagged = !b.tagged)
   document.selection ? document.selection.createRange().text += open : ta.value += open;
  else
   document.selection ? document.selection.createRange().text += close : ta.value += close;

  return false;
 }

function taPut(ta, s)
 {
  ta.focus();

  document.selection ? document.selection.createRange().text += s : ta.value += s;

  return false;
 }

function taBold(ta, b) { return taTwin(ta, b, '<b>', '</b>'); }
function taItalic(ta, b) { return taTwin(ta, b, '<i>', '</i>'); }
function taUnderline(ta, b) { return taTwin(ta, b, '<u>', '</u>'); }
function taStrike(ta, b) { return taTwin(ta, b, '<s>', '</s>'); }
function taCut(ta, b) { return taPut($(ta), '<cut>'); }
function taHelp(kind) { Helper(kind); return false; }

function taURL(ta)
 {
  var ta = $(ta);

  var FoundErrors = '';

  var enterURL = prompt("Введите адрес ссылки", "http://");

  if (!enterURL) 
   {
    FoundErrors += "\nВы не ввели адрес ссылки.";
   }
  else
   {
    var enterTITLE = prompt("Введите текст ссылки", "Текст ссылки");

    if (!enterTITLE) 
     FoundErrors += "\nВы не ввели текст ссылки.";
   }
 
  if (FoundErrors)
   alert("Ошибка!" + FoundErrors);
  else
   taPut(ta, '<a href="' + enterURL + '">' + enterTITLE + '</a>');

  return false;
 }

/* comments */

function LockCommentControls()
 {
  if (s = $('s1')) s.disabled = true;
  if (s = $('s2')) s.disabled = true;
  if (s = $('s3')) s.disabled = true;
 }

function UnlockCommentControls()
 {
  if (s = $('s1')) s.disabled = false;
  if (s = $('s2')) s.disabled = false;
  if (s = $('s3')) s.disabled = false;
 }

function PreviewCommentLoaded(r)
 {
  UnlockCommentControls();
 }

function PreviewComment()
 {
  LockCommentControls();

  new Ajax.Updater('CommentPreview',
                   '/ajax/comment_preview.html?' + Math.random(),
                   {asynchronous: true, 
                    parameters: 'digest=' + digest + '&' + Form.serialize($('EditorForm')), 
                    onSuccess: PreviewCommentLoaded });
 }

/* pictures loader */

function PicturesLoader(id)
 {
  width = 600;
  height = 220;

  var left = Math.round((document.body.clientWidth - width) / 2);
  var top = Math.round((document.body.clientHeight - height) / 2);

  var win = window.open('/misc/pictures_addict.html?ta=' + id, 
                        '', 
                        'toolbar=no,' +
                        'scrollbars=yes,' + 
                        'status=no,' +
                        'height=' + height + ',' + 
                        'width=' + width + ',' + 
                        'top=' + top + ',' + 
                        'left=' + left);

  win.focus();

  return false;
 }

/* popup */

var LastPopup = '';
var BackImage = new Image();
BackImage.src = '/images/screen.png';

function AppearPopupDo(r)
 {
  $(LastPopup).style.display = 'block';
 }

function AppearPopup(id, url)
 {
  var s = $('screen');
  var y = Position.cumulativeOffset($('bottomspot'));
  s.style.display = 'block';
  s.style.height = y[1] + 'px';

  new Ajax.Updater(id, url, {asynchronous: true, method: 'get', evalScripts: true, onSuccess: AppearPopupDo});

  var i = $(id);
//  i.innerHTML = '<div align="center" class="PopupProgress"><img class="logo" src="/images/logo6.gif" width="82" height="64"><img class="progress" width="190" height="14" src="/images/progress.gif"></div>';
//  i.style.display = 'block';

  LastPopup = id;
 }

function DetachPopup()
 {
  var s = $('screen');
  s.style.display = 'none';

  var i = $(LastPopup);
  i.style.display = 'none';
 }

function DoLogin()
 {
  AppearPopup('PopupForm', '/login/?ajax=1');
  return false;
 }

function DoRegister()
 {
  AppearPopup('PopupForm', '/register/?ajax=1');
  return false;
 }

function Selector(src, dest, kind)
 {
  target = $(dest);

  o = new Option();  
  o.text = '...';
  o.value = '0';

  target.length = 0;
  target.options[target.length] = o;

  new Ajax.Request(baseurl + 'selector.html',
                   {method: 'get',
                    parameters: 'src=' + escape(src) + 
                                '&dest=' + escape(dest) + 
                                '&kind=' + escape(kind) + 
                                '&value=' + escape($(src).value) + 
                                '&' + Math.random(),
                    onComplete: GotSelector});
 }

function GotSelector(s)
 {
  s = s.responseText;
  s = s.split("\x0D\x0A");

  if (s.length == 0) return;
  if (!(target = $(s[0]))) return;

  target.length = 0;

  for (k = 1; k < s.length - 1; k++)
   {
    y = s[k].split("\<\.\>");

    o = new Option();
    o.text = y[1];
    o.value = y[0];
    target.options[target.length] = o;
   }

  if (target.onchange) target.onchange();
 }

function rowOver(r)
 {
  r.className_ = r.className;
  r.className = 'hover';
 }

function rowOut(r)
 {
  r.className = r.className_;
 }

function rowClick(r, cb)
 {
  $(cb).checked = !$(cb).checked;
 }

var
 lfiTimeout = 0;
 lfiPrevValue = '';

function LoadFirmInfo(inp, id)
 {
  if (lfiPrevValue != inp.value)
   {
    if (lfiTimeout) clearTimeout(lfiTimeout);

    lfiTimeout = setTimeout("LoadFirmInfoDo('" + inp.value + "', '" + id + "');", 500);
   }

  lfiPrevValue = inp.value;
 }

function LoadFirmInfoDo(firmID, id)
 {
  new Ajax.Updater(id,
                   '/ajax.html?kind=firm_info&firmID=' + encodeURIComponent(firmID), 
                   {asynchronous: true, method: 'get', evalScripts: true});
 }

function FixSearch(i)
 {
  if (i.value == 'Найти')
   {
    i.value = '';
    i.style.color = '#000000';
   }
 }

function UnfixSearch(i)
 {
  if (i.value == '')
   {
    i.value = 'Найти';
    i.style.color = '#d0d0d0';
   }
 }

function PriceFoto(priceID, width, height)
 {
  width = width + 50;
  height = height + 80;

  if (height > 700)
   height = 700;

  var left = Math.round((document.body.clientWidth - width) / 2);
  var top = Math.round((document.body.clientHeight - height) / 2);

  var win = window.open('/price/foto.html?priceID=' + priceID,
                        'pricefoto', 
                        'toolbar=no,' +
                        'scrollbars=yes,' +
                        'status=no,' +
                        'height=' + height + ',' +
                        'width=' + width + ',' + 
                        'top=' + top + ',' +
                        'left=' + left);

  win.focus();

  return false;
 }

function ForumFocusTA()
 {
  $('ta').focus();
 }

function ForumReply(where)
 {
  var topc = where.parentNode;
  var replyform = $('replyform');

  replyform.parentNode.removeChild(replyform);
  topc.appendChild(replyform);

  where.style.display = 'none';

  if (prev_to_show)
   prev_to_show.style.display = 'block';

  prev_to_show = where;

  setTimeout('ForumFocusTA();', 50);

  $('replyto').value = where.id;

  return false;
 }
