function popupDialog(url, name) {
  var imWin = window.open(url, name, "scrollbars=yes");
  if (imWin) {
    imWin.focus();
  }
}
function popupWindow(url, name) {
  var imWin = window.open(url, name, "scrollbars=no");
  if (imWin) {
    imWin.focus();
  }
}

function showHideWithImg(id, imgId) {
  var item = document.getElementById(id);
  if (item) {
    if (item.style.display == "none") Effect.Appear(item);
    else Effect.Fade(item);
  }
  var imgItem = document.getElementById(imgId);
  if (imgItem) {
    if (item.style.display == "none") {
      imgItem.src = '/images/meta/minus.gif';
    } else {
      imgItem.src = '/images/meta/plus.gif';
    }
  }
  return false;
}

function showHide(id) {
  var item = document.getElementById(id);
  if (item) {
    if (item.style.display == "none") Effect.Appear(item);
    else Effect.Fade(item);
  }

  return false;
}

function slideInOut(id) {
  var item = document.getElementById(id);
  if (item) {
    if (item.style.display == "none") {
      Effect.SlideDown(item);
      setCookie('s_' + id, '1');
    } else {
      Effect.SlideUp(item);
      setCookie('s_' + id, '0');
    }
  }

  return false;
}

function showHideAll(el, prefix, show) {
  if (el.id && el.id.indexOf(prefix) != -1) {
    if (show) {
      el.style.display = "block";
    } else {
      el.style.display = "none";
    }
  }
  if (el.nodeType == 1 || el.nodeType == 11) {
    for (var i = el.firstChild; i; i = i.nextSibling) {
      showHideAll(i, prefix, show);
    }
  }
  return false;
}

function checkCB(el) {
  if (el.nodeName == "INPUT" && el.type == "checkbox") {
    el.checked = !el.checked;
  }
  if (el.nodeType == 1 || el.nodeType == 11) {
    for (var i = el.firstChild; i; i = i.nextSibling) {
      checkCB(i);
    }
  }
  return false;
}
;

function doClick(actionVal) {
  var action = document.getElementById("action");
  var form = document.getElementById("form");
  if (action && form) {
    action.value = actionVal;
    form.submit();
  }
}

function pviiClassNew(obj, new_style) {
  obj.className = new_style;
}

function jumpTo(target, select) {
  eval(target + ".location='" + select.options[select.selectedIndex].value + "'");
}

var rusChars = new Array('А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', '\я');
var transChars = new Array('A', 'B', 'V', 'G', 'D', 'E', 'YO', 'ZH', 'Z', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'H', 'C', 'CH', 'SH', 'SHCH', '\`', 'Y', '\'', 'E', 'YU', 'YA', 'a', 'b', 'v', 'g', 'd', 'e', 'yo', 'zh', 'z', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'shch', '\`', 'y', '\'', 'e', 'yu', 'ya');
function convert(from) {
  var to = new String();
  var len = from.length;
  var character, isRus;
  for (var i = 0; i < len; i++) {
    character = from.charAt(i, 1);
    isRus = false;
    for (var j = 0; j < rusChars.length; j++) {
      if (character == rusChars[j]) {
        isRus = true;
        break;
      }
    }
    to += (isRus) ? transChars[j] : character;
  }
  return to;
}

var feedbackWindow;
function openFeedbackWindow() {
  feedbackWindow = new Window("feedback", {className: "spread", title: "FeedBack", bottom:10, right:10, width:500, height:420, resizable: true, minimizable: false, maximizable:false, url: "/popup/feedback.do?from=" + document.location});
  feedbackWindow.setDestroyOnClose();
  feedbackWindow.show();
}

function setCookie(name, value) {
  var options = {};
  if (value === null) {
    value = '';
    options.expires = -1;
  }
  var expires = '';
  if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
    var date;
    if (typeof options.expires == 'number') {
      date = new Date();
      date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
    } else {
      date = options.expires;
    }
    expires = '; expires=' + date.toUTCString();
    // use expires attribute, max-age is not supported by IE
  }
  var path = '/';
  var domain = options.domain ? '; domain=' + options.domain : '';
  var secure = options.secure ? '; secure' : '';
  document.cookie = [name, '=', value, expires, path, domain, secure].join('');
}

function smartTextInput(obj, name, activeColor, inactiveColor) {
  if (obj.value == '') {
    obj.value = name;
  }
  obj.onfocus = function () {
    if (obj.value == name) {
      obj.value = '';
      obj.style.color = activeColor;
    }
  };
  obj.onblur = function () {
    if (obj.value == '') {
      obj.value = name;
      obj.style.color = inactiveColor;
    }
  };
}

function expertSelected(text, element) {
  var id = element.id.split('_')[1];
  $('expertId').value = id;
}

function addChampionship(select, box) {
  id = select.options[select.selectedIndex].value;
  text = select.options[select.selectedIndex].innerHTML;
  html = '<span><input type="hidden" name="championships" value="' + id + '"/>' + text + ' <a href="#" onclick="$(this).up().remove();return false;">x</a></span>';
  box.insert(html);
  return false;
}
