function initPage() {
  new AjaxTiny("text1", { toolbarLocation: 'external' });
  new AjaxTiny("TopRight", { toolbarLocation: 'external' });
}

var ftimeout = null;
var ctimeout = null;
var stimeout = null;
var searching = false;
var lastid;

function showDebug(q) {
  var div = document.getElementById('debug');
  var h = div.innerHTML;
  div.innerHTML = h + " - " + q;
}

function NodeSelected(id) {
  document.getElementById("Filter").value = "";
  if (stimeout != null)
    clearTimeout(stimeout);
  stimeout = setTimeout("doSearch('" + id + "')", 1000);
}

function doSearch(id) {
  stimeout = null;
  if (searching) {
    stimeout = setTimeout("doSearch('" + id + "')", 1000);
  }
  else {
    searching = true;
    startLengthy();
    var filter = document.getElementById("Filter").value;
    if (id == null)
      id = tree_selected_id;
    if (id == null && filter.length > 0)
      id = "0";
    CompanySearch.GetCompanies(id, filter, doneSearch);
  }
}

function doneSearch(response) {
  stimeout = null;
  searching = false;
  if (!checkResponse("GetCompanies", response)) return;
  document.getElementById("Companies").innerHTML = response.value;
  document.getElementById("filterdiv").style.display = (response.value.indexOf("Inga ") == 0 ? "none" : "block");
  ftimeout = null;
  doneLengthy();
}

function doFilter() {
  ftimeout = null;
  var filter = document.getElementById("Filter").value;
  var id = tree_selected_id;
  if (id == null && filter.length > 0)
    id = "0";
  var response = CompanySearch.GetCompanies(id, filter);
  document.getElementById("Companies").innerHTML = response.value;
  document.getElementById("filterdiv").style.display = (response.value.length > 0 ? "block" : "none");
}

function filter() {
  if (ftimeout != null)
    clearTimeout(ftimeout);
  ftimeout = setTimeout("doFilter()", 1000);
}

function doSearchCat() {
  // Set this for TreeView to ba able to select a higher node
  ob_iar = 0;
  ctimeout = null;
  var search = document.getElementById("catsearch").value;
  if (search.length > 2) {
    var response = CompanySearch.SearchCategory(search);
    if (response.value.length > 0)
      ob_SelectedId(response.value);
  }
}

function searchcat() {
  if (ctimeout != null)
    clearTimeout(ctimeout);
  ctimeout = setTimeout("doSearchCat()", 500);
}

function selectItem(itemid, sel) {
  var item = document.getElementById(itemid);
  if (item) item.style.color = (sel ? "#cc3333" : "");
}

function companySelected(id) {
  var response = CompanySearch.GetCompanyInfo(String(id));
  if (response.error != null) {
    alert("Fel i GetCompanyInfo: " + response.error.Message);
    return;
  }
  var res = response.value.split('|');

  var html = (res[3].length > 0 ? "<div><img src='" + res[3] + "' alt='" + res[0] + "' /></div>" : "") +
    "<h4 id='cname'>" + res[0] + "</h4>" +
    "<table cellspacing='0' cellpadding=0 style='margin-top:10px'>" +
    (res[1].length > 0 ? "<tr><td>Adress: </td><td>" + res[1] + "</td></tr>" : "") +
    (res[2].length > 0 ? "<tr><td>Postadress: </td><td>" + res[2] + "</td></tr>" : "") +
    (res[4].length > 0 ? "<tr><td>Telefon: </td><td>" + res[4] + "</td></tr>" : "") +
    (res[5].length > 0 ? "<tr><td>Fax: </td><td>" + res[5] + "</td></tr>" : "") +
    (res[6].length > 0 ? "<tr><td>E-post: </td><td>" + res[6] + "</td></tr>" : "") +
    (res[7].length > 0 ? "<tr><td>Hemsida: </td><td><a href='http://" + res[7] + "' target='_blank' onfocus='this.blur()'>" + res[7] + "</a></td></tr>" : "") +
    "</table><p>&nbsp;</p>" +
    "<h5>Kategorier</h5>" + res[8];
  document.getElementById("companyinfo").innerHTML = html;

  selectItem("a_" + id, true);
  selectItem("at_" + id, true);
  selectItem("a_" + lastid, false);
  selectItem("at_" + lastid, false);

  lastid = id;

  contentDiv = document.getElementById("companyinfodiv");
  contentDiv.style.display = "block";

  setTimeout("startGrow()", 100);
}

function hlClose(img, hl) {
  if (hl) img.src = "/gfx/close_over.gif";
  else img.src = "/gfx/close.gif";
}

function closeCompany() {
  if (contentDiv) contentDiv.style.overflow = "hidden";
  shrinkDiv();
}


var contentHeight = 0;
var contentHeightMax = 200;
var contentGrowSpeed = 20;
var contentGrowIncY = 6;
var contentDiv;

function startGrow() {
  contentHeightMax = document.getElementById("companyinfo").offsetHeight + 50;
  growDiv();
}

function growDiv() {
  var cont = false;
  if (contentHeight < contentHeightMax) {
    contentHeight += contentGrowIncY;
    if (contentHeight > contentHeightMax)
      contentHeight = contentHeightMax;
    else
      cont = true;
    contentDiv.style.height = contentHeight + "px";
  }
  if (cont) setTimeout("growDiv()", 1);
  else contentDiv.style.overflow = "visible";
}

function shrinkDiv() {
  var cont = false;
  if (contentHeight > 0) {
    contentHeight -= contentGrowIncY;
    if (contentHeight < 0)
      contentHeight = 0;
    else
      cont = true;
    contentDiv.style.height = contentHeight + "px";
  }
  if (cont) setTimeout("shrinkDiv()", 1);
  else if(contentDiv) contentDiv.style.display='none';
}

function checkResponse(method, response) {
  if (response.error != null) {
    alert("Fel i " + method + ": " + response.error.Message);
    return false;
  }
  return true;
}

function startLengthy() {
  var mess = document.getElementById("searchmess");
  mess.style.display = "block";
}

function doneLengthy() {
  var mess = document.getElementById("searchmess");
  mess.style.display = "none";
}
