var is_ie = ( (navigator.userAgent.toLowerCase().indexOf("msie") != -1) &&
        (navigator.userAgent.toLowerCase().indexOf("opera") == -1) );
var is_compat = (document.compatMode == "BackCompat");

function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string') element = find_obj(element);
    if (arguments.length == 1) return element;
    elements.push(element);
  }
  return elements;
}

function find_obj(n, d) { //v4.01
  if (typeof n != 'string' && !(n instanceof Array) )  return n;
  if (n instanceof Array){
    var collection = [];
    for (var i = 0; i<n.length;++i)
      collection[collection.length] = find_obj(n[i], d);
    return collection;
  }
  if (typeof n == 'string'){
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent&&parent.frames.length) {
      d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(d.getElementById) x=d.getElementById(n);
    if(!x&&!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    if(x&&x.length&&x[0].form) for (i=0;x&&x.length&&i<x.length;i++) if(x[i].checked) x=x[i];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=find_obj(n,d.layers[i].document);
    return x;
  }
  return null;
}

function add_node(obj, tagName, n){
  if (tagName) obj = find_parent(obj, tagName, n);
  nnode = obj.parentNode.insertBefore(obj.cloneNode(true), obj.nextSibling);
  copy_values(obj, nnode);
  return nnode;
}

function del_node(obj, tagName, n){
  if (tagName) obj = find_parent(obj, tagName, n);
  //if (obj.previousSibling.id=="added" || obj.nextSibling.id=="added"  )
  obj.parentNode.removeChild(obj)
}

function add_ptag(obj, tag, after){
 while (obj.tagName!=tag) obj = obj.parentNode
 var newobj;
 if (after) newobj = obj.parentNode.insertBefore(obj.cloneNode(true), obj.nextSibling)
   else newobj = obj.parentNode.insertBefore(obj.cloneNode(true), obj)
 copy_values(obj, newobj);
 return newobj;
}

function del_ptag(obj, tag, onlynotlast){
  while (obj.tagName!=tag) obj = obj.parentNode
  if (!onlynotlast || (obj.previousSibling && obj.previousSibling.className=="added") ||
      (obj.nextSibling && obj.nextSibling.className=="added"))
    obj.parentNode.removeChild(obj)
}

function hide_obj(obj, recursive){
  //alert(obj.outerHTML)
  if(obj.style){
    obj.style.display="none";
    obj.style.visibility = 'hidden';
    obj.disabled=true;
    if ("function" == typeof(addClass)) addClass(obj, "disabled");
  }
  if (recursive && obj.hasChildNodes())
    for (var i=0; i<obj.childNodes.length; i++)
      hide_obj(obj.childNodes[i], recursive)

  return obj.disabled;
}

function hide_id(id, recursive){
  hide_obj(find_obj(id), recursive);
}

function show_obj(obj, recursive){
  if(obj.style){
    obj.style.display="";
    obj.style.visibility = "visible";
    obj.disabled=false;
    if ("function" == typeof(removeClass)) removeClass(obj, "disabled");
  }
  if (recursive && obj.hasChildNodes())
    for (var i=0; i<obj.childNodes.length; i++)
      show_obj(obj.childNodes[i], recursive)
  return !obj.disabled;
}

function show_id(id, recursive){
  show_obj(find_obj(id), recursive);
}


function vis_tr(tr, delta, n, vis){
  n=parseInt(n);
  if (tr.tagName.toUpperCase()!="TR") tr = find_parent(tr, "TR");
  //alert(tr.tagName);
  while(delta!=0){
    if (delta>0) {
      if (!tr.nextSibling) return false;
      tr = tr.nextSibling;
      delta--;
    }
    if (delta<0) {
      if (!tr.previousSibling) return false;
      tr = tr.previousSibling;
      delta++;
    }
  }
  while(n!=0){
    if (vis) var ok = show_obj(tr, true);
      else var ok = hide_obj(tr, true);
    if (!tr.nextSibling) return false;
    tr = tr.nextSibling;
    if (ok ) n--;
  }
}


function disable_obj(obj, dis, recursive){
  //alert(di)
  if (obj.style) obj.disabled=dis;
  if ("function" == typeof(addClass)) addClass(obj, "disabled");
  if (recursive && obj.hasChildNodes())
    for (var i=0; i<obj.childNodes.length; i++)
      disable_obj(obj.childNodes[i], dis, recursive)
  return obj.disabled;

}

function group_func(group, func, params){
  if(typeof(func) == "function" && (group instanceof Array))
    for(var i=0;i<group.length;i++) func(group[i], params);
}

function proc_children(obj, func, params, recursive){
  if ( typeof(func) == "function" && obj.tagName) func(obj, params);
  if (recursive && obj.hasChildNodes())
    for (var i=0; i<obj.childNodes.length; i++)
      proc_children(obj.childNodes[i], func, params, recursive);
}

function is_att(obj, att, att_name){
//  if (typeof(xobj)!="object") return false;
  if (!att_name) att_name = "tagName";
  if ((att_name=="tagName" && obj.tagName==att) ||
      (obj.getAttribute && obj.getAttribute(att_name)==att)) return true;
  return false;
}

// Найти n-го предка с тегом tagName
function find_parent(obj, att, n, att_name){
  var c = 0
  if (!att_name) att_name = "tagName";
  if (!n) n = 1;
  while (obj && c!=n){
    obj = obj.parentNode;
    //alert(obj.tagName)
    if (is_att(obj, att, att_name)) c++
  }
  return obj;
}

function find_prev(obj, att, n, att_name, nn){
  if (!att_name) att_name = "tagName";
  if (!n) n = 1;
  if (nn==null) nn=0;
  while(n>nn) {
    if (obj.previousSibling){
      obj=obj.previousSibling;
    } else {
      if(obj.parentNode && obj.parentNode.previousSibling){
        obj = obj.parentNode.previousSibling;
        while(obj.lastChild) obj=obj.lastChild;
      } else if (obj.parentNode) obj.parentNode;
               else return false;
    }
    if (is_att(obj, att, att_name)) nn++
  }
  return obj;
}

function find_next(obj, att, n, att_name, nn){
  if (!att_name) att_name = "tagName";
  if (!n) n = 1;
  if (nn==null) nn=0;
  while(n>nn) {
    if (obj.nextSibling){
      obj=obj.nextSibling;
    } else {
      if(obj.parentNode && obj.parentNode.nextSibling){
        obj = obj.parentNode.nextSibling;
        while(obj.firstChild) obj=obj.firstChild;
      } else if (obj.parentNode) obj.parentNode;
               else return false;
    }
    if (is_att(obj, att, att_name)) nn++
  }
  return obj;
}


function find_children(a_obj, att, n, att_name, nn) {
  var xobj;
  if (!att_name) att_name = "tagName";
  if (!n) n = 1;
  if (!nn) nn = 1;
  //alert(a_obj.tagName)
  if (is_att(a_obj, att, att_name))
    if (n==nn)
      return a_obj;
    else nn++;
  if (a_obj.hasChildNodes()) {
    var i = 0
    while (i<a_obj.childNodes.length && a_obj.childNodes[i]){
      xobj = find_children(a_obj.childNodes[i], att, n, att_name, nn)
      if (typeof(xobj)=="object")
        return xobj;
      if (typeof(xobj)=="number") nn = xobj;
      i++
    }
  }
  if (nn==1) return false;
  return nn;
}

function find_all_children(obj, att, att_name, result) {
  if (!att_name) att_name = "tagName";
  if (!result) result = new Array();
  if ((att_name=="tagName" && obj.tagName==att) ||
      (obj.getAttribute && obj.getAttribute(att_name)==att)) result[result.length] = obj;
  if (obj.hasChildNodes()) {
    var i = 0
    while (i<obj.childNodes.length && obj.childNodes[i]){
      result = find_all_children(obj.childNodes[i], att, att_name, result);
      i++
    }
  }
  return result;
}

function copy_values(from, to){
  if (from.type){
    if (from.type == "text" || from.type == "password" || from.type == "textarea") to.value = from.value;
    else if (from.type.indexOf("select") != -1) to.selectedIndex = from.selectedIndex;
    else if (from.type == "checkbox" || from.type == "radio") to.checked = from.checked;
  }
  if (from.hasChildNodes())
    for (var i=0; i<from.childNodes.length; i++)
      copy_values(from.childNodes[i], to.childNodes[i]);
}

function setXvalue(from, name, value){
  if (from.type && ((from.id && from.id.indexOf(name) != -1) || (from.name && from.name.indexOf(name) != -1))){
    if (value && (from.type == "text" || from.type == "password" || from.type == "textarea"|| from.type == "hidden")) from.value = String(value);
    else if (from.type.indexOf("select") != -1) set_select(from, value);
    else if (from.type == "checkbox" || from.type == "radio") from.checked=Boolean(value);
  }
  if (from.hasChildNodes())
    for (var i=0; i<from.childNodes.length; i++)
      setXvalue(from.childNodes[i], name, value);
}


function inArray(value, arr) {
  for (var i=0; i < arr.length; i++)
    if (arr[i] === value) return true;
  return false;
}

/*
function set_select(sel, key){
  for (j=0;j<sel.options.length;j++)
    if (sel.options[j].value==key)
      sel.options[j].selected=true
}
*/

// Cookie handling
function setCookie(sName, sValue, nDays ) {
  var expires = "";
  if ( nDays ) {
    var d = new Date();
    d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
    expires = "; expires=" + d.toGMTString();
  }
  document.cookie = sName + "=" + sValue + expires + "; path=/";
};

function getCookie(sName) {
  var re = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
  var res = re.exec( document.cookie );
  return res != null ? res[3] : null;
};

function removeCookie( name ) {
  setCookie( name, "", -1 );
};

// Javascript Console
function xd(){
  if (arguments.length==1) var params = arguments[0];
  else {
    var params = [];
    for (var i = 0; i < arguments.length; i++)
      params[i]=arguments[i]
  }
  alert(Dump(params,1,4));

}
function Dump(d,l,r) {
    if (typeof(l) == "undefined") l = 1;
    if (typeof(r) == "undefined") r = 3;
    if (l>r) return "***Recursed***\n";
    var s = '';
    var c=0;
    if (typeof(d) == "object" || d instanceof Object) {
        if (typeof(d["Dump"]) == "function") return "[window]\n";
        if (typeof(d["location"]) == "object") return "[document]\n";
        s += typeof(d) + " {\n";
        try{
          for (var k in d) {
              //if (l>1 && c++>3) continue;
              try{
                var v = d[k];
                if (v == null || typeof(v)=="function" || ((v.constructor||{}).prototype||{})[k]) continue;
              }catch (e) {}
              for (var i=0; i<l; i++) s += "  ";
              s += k+": " + Dump(v,l+1,r);
          }
        }catch (e) {}
        for (var i=0; i<l-1; i++) s += "  ";
        s += "}\n"
    } else {
        s += "" + d + "\n";
    }
    return s;
}

function loadJS(href) {
  with (document) {
    var span = this._span;
    if (span) {
        this._span = null;
        setTimeout(function() {
            // without setTimeout - crash in IE 5.0!
            span.parentNode.removeChild(span);
        }, 50);
    }
    span = null;
    span = body.appendChild(createElement("SPAN"));
    span.style.display = 'none';
    span.innerHTML = 'Text for stupid IE.<s'+'cript></' + 'script>';
    setTimeout(function() {
        var s = span.getElementsByTagName("script")[0];
        s.language = "JavaScript";
        if (s.setAttribute) s.setAttribute('src', href); else s.src = href;
    }, 10);
    this._span = span;
  }
}

function getTarget(e){
  return window.event ?
    window.event.srcElement : e.target.tagName
      ? e.target : e.target.parentNode;
}

function getClientWidth(){
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight(){
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function getBodyScrollTop(){
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft(){
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function getDocumentHeight(){
  return (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
}

function getDocumentWidth(){
  return (document.body.scrollWidth > document.body.offsetWidth)?document.body.scrollWidth:document.body.offsetWidth;
}

function getClientCenterX(){
  return parseInt(getClientWidth()/2)+getBodyScrollLeft();
}

function getClientCenterY(){
  return parseInt(getClientHeight()/2)+getBodyScrollTop();
}

function stretchToFullSize(stretchMe) {
  stretchMe.style.width = '100%';
  stretchMe.style.height = getDocumentHeight() + 'px';
}

function centerOnScreen(centerMe) {
  centerMe.style.left = (getClientCenterX() - centerMe.offsetWidth/2) + 'px';
  centerMe.style.top =  (getClientCenterY() - centerMe.offsetHeight/2) + 'px';
}
