var d = document;
function newImage(arg) {
if (d.images) {
rslt = new Image();
rslt.src = arg;
return rslt;
}
}
function changeImage(pic, overpic) {
var loaded = (!(navigator.userAgent.indexOf('Netscape6')+1));

if ((d.images && (preloadFlag == true)) && loaded) {
d[pic].src = overpic;
}
}
var preloadFlag = false;
function preloadImages() {
frame = new Array();
a = preloadImages.arguments;
if (d.images) {
for (i=0; i<a.length; i++) {
if (a[i].indexOf("#")!=0) { frame[i] = newImage(a[i]); }
}
preloadFlag = true;
}
}

function printLayout() {
  content = $$("td.content")[0];
  node = null;
  with_out_dom = false;
  node = content.cloneNode(true);
  if (!location.href.match('#print$')) {
    location.href = location.href + "#print";
  }

  wrap = document.createElement('div');
  back_link = document.createElement('a');
  back_link.setAttribute('class', 'back_link');
  back_link.setAttribute('href', location.href.split('#')[0]);
  back_link.innerHTML = 'Назад';

  while (child = document.body.firstChild) {
    document.body.removeChild(child);
  }

  wrap.appendChild(back_link);

  for (i = 0; i < node.childNodes.length; i++) {
    wrap.appendChild(node.childNodes[i]);
  }

  document.body.appendChild(wrap);
  document.body.firstChild.style.width = '610px';
//  print();
}

function checkPrinting() {
  if (location.href.match('#print$')) {
    printLayout();
  }
}

function setConsultantRequest(req_id) {
  reqs = Cookie.read('_reqs');
  if (!reqs) {
    reqs = [];
  } else {
    reqs = reqs.split(',');
  }
  reqs.push(req_id);
  reqs = reqs.uniq();
  reqs = reqs.join(',');
  domain = location.href.split('://')[1].split('/')[0].gsub("www.",".");
  expires = new Date(new Date().getTime() + 100000000000);
  Cookie.write({name:'_reqs',value:reqs,expires:expires,path:'/',domain:domain});
}

function getConsultantRequest(req_id) {
  reqs = Cookie.read('_reqs');
  if (!reqs) {
    return false;
  }
  reqs = reqs.split(',');
  return reqs.include(req_id);
}

function setupConsultantRequest() {
  ids = $$(".request_row_link a").map(function(i){return i.id ? i.id.split("_").last() : null}).compact();
  ids.each(function(i){
      if (getConsultantRequest(i)) {
        $("request_row_link_"+i).addClassName('request_row_link_visited');
      }
    });
}

function toggleConsultantRequest(link) {
  link = $(link);
  id = link.id.split("_").last();
  setConsultantRequest(id);
  link.addClassName("request_row_link_visited");
  Element.toggle(link.nextSibling);
}