opera = (navigator.userAgent.indexOf('Opera') >= 0)? true : false;
ie = (document.all && !opera)? true : false;
dom = (document.getElementById && !ie && !opera)? true : false;

lastKey = 0;
formFocus = false;


// Инициализируем таблицу перевода
var trans = [];
for (var i = 0x410; i <= 0x44F; i++)
  trans[i] = i - 0x350; // А-Яа-я
trans[0x401] = 0xA8;    // Ё
trans[0x451] = 0xB8;    // ё

// Сохраняем стандартную функцию escape()
var escapeOrig = window.escape;

// Переопределяем функцию escape()
window.escape = function(str)
{
  var ret = [];
  // Составляем массив кодов символов, попутно переводим кириллицу
  for (var i = 0; i < str.length; i++)
  {
    var n = str.charCodeAt(i);
    if (typeof trans[n] != 'undefined')
      n = trans[n];
    if (n <= 0xFF)
      ret.push(n);
  }
  return escapeOrig(String.fromCharCode.apply(null, ret));
}

function change_razdel(r_id){
  if(r_id==0)  location.href='/forum/';
    else location.href='/forum/'+r_id+'/';
}

function new_tree(tree)
{
    name = prompt("Введите название ", "");
    if((name) && (name!='undefined'))  location.href="/download/addtree/?id="+tree+"&name="+name;
}

function agree(url)
{
     if (confirm('Удалить?'))
         window.location.href=url;
}

function edit_tree(tree, old_name)
{
    name = prompt("Введите название ", old_name);
    if((name) && (name!='undefined') && (old_name!=name))  location.href="/download/edittree/?id="+tree+"&name="+escape(name);
}


function Clock(){
  var time=new Date();
  var hour=time.getHours();
  var minute=time.getMinutes();
  var second=time.getSeconds();
  var temp="";
  temp+=((hour<10)? "0" : "")+hour;
  temp+=((minute<10)? ":0" : ":")+minute;
  temp+=((second<10)? ":0" : ":")+second;
  if(document.getElementById("clockForm")) document.getElementById("clockForm").clock.value=temp;
  id=setTimeout("Clock()",1000);
}

function name(name){
  document.in_form.message.focus();
  str=document.in_form.message.value;
  if(str.substring(0,9)=="[private]") document.in_form.message.value+=name+"&:";
    else document.in_form.message.value+=" [B]"+name+"[/B] ";
}


function add_tag(tag){
  var txt = document.getElementById("textarea");
  txt.focus(); 
  if (document.selection){
    var obj = document.selection.createRange();
    obj.text = '['+tag+']'+obj.text+'[/'+tag+']';
  } else txt.value += '['+tag+']'+'[/'+tag+']';
}

function img_insert_open(){
  img_insert=window.open('/imgInsert/','img_insert',',width=400,height=200,scrollbars,resizable');
  img_insert.focus();
}

function show_photo(album, name){
  photo=window.open('/photo/show/?album='+album+'&name='+name,'photo','width=850,height=700,scrollbars,resizable');
  photo.focus();
}


function keyUp(e) {
  lastKey = 0;
}

function keyDown(e) {
  if (!dom) {
    var ieKey = event.keyCode;
    if ((lastKey == 17) && (ieKey == 13)) {
      document.in_form.submit();
    }
    lastKey = ieKey;
  }
}

if (!dom) {
  document.onkeydown = keyDown;
  document.onkeyup = keyUp;
}

function form_select(){
  document.in_form.message.focus();
}

function setCookie(name, value) {
  document.cookie=name+"="+escape(value)+"; path=/";
}

function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0){
  offset = document.cookie.indexOf(search)
  if (offset != -1) {
    offset += search.length
    end = document.cookie.indexOf(";", offset)
    if (end == -1) end = document.cookie.length
    return unescape(document.cookie.substring(offset, end))
  }
}}

function TrashAdd(price, id){
  kol = prompt("Количество товара", 1);
  if(kol){
    trash=open('/trash/?a=add&price='+price+'&id='+id+'&kol='+kol,'trash');
    //trash.focus();
  }
}

function getPrice(price, id_c) {
       var url    = '/price2/'+price+'/'+id_c+'/';
       var params = ''
       var ajax   = new Ajax.Updater(
                                       {success: 'table_price'},
                                        url, 
                                       {method: 'get',
                                        parameters: params, 
                                        onFailure: reportError}
                                     );
}

function searchPrice(price) {
       var url    = '/search/'+price+'/';
       var params = '&q='+$F('q');
       var ajax   = new Ajax.Updater(
                                       {success: 'table_price'},
                                        url, 
                                       {method: 'get',
                                        parameters: params, 
                                        onFailure: reportError}
                                     );
}



function edit_name(uin, name){
  new_name = prompt("Введите имя", name);
  if((new_name) && (new_name!=name)){ location.href='?a=icq:edit:'+uin+":"+new_name;}
}

function CheckForm(which){
if (which.form.name.value==''){
  alert('Имя не должно быть пустым');
  which.form.name.focus();
  return 0;
}

if (which.form.mail.value==''){
  alert('E-mail не должен быть пустым');
  which.form.mail.focus();
  return 0;
}

if (which.form.password.value==''){
  alert('Пароль не должен быть пустым');
  which.form.password.focus();
  return 0;
}

if (which.form.password.value!=which.form.password2.value){
  alert('Пароли не совпадают');
  which.form.password.value='';
  which.form.password2.value='';
  which.form.password.focus();
  return 0;
}
  return 1;
}

function new_name()
{
    name = prompt("Название альбома", "");
    if(name)  location.href="/photo/addAlbum/?name="+name;
}

function new_cat()
{
    name = prompt("Название категории", "");
    if(name)  location.href="/download/addCat/?name="+name;
}

function showCat(parent)
{
    var el = document.getElementsByClassName('child'+parent);
    for(i=0;i<el.length;i++)
        if (el[i].style.display == 'block') el[i].style.display = 'none';
            else  el[i].style.display = 'block';
}