//  $Id: insite.js,v 1.11 2010/07/05 11:04:51 jonasw Exp $

//  Main toolbar tab popup menu
var h4_props = new PopupProperties(68, 0);
h4_props.setHide2ndClick();
h4_props.hide_callback = function(id) {
  return new Function('', 'return button_hide_' + id + '()')();
};


//  ---------------------------------------------------------------
//  Overrides for Popup.js and CrossPlatform.js
//  ---------------------------------------------------------------
function get_vertical_offset()
{
  var vertical_offset =
    ((window.body_vertical_offset + "") != "undefined") ?
    window.body_vertical_offset : 0;
  return vertical_offset;
}
function getScrollTop()  {
  if (isNav4||isNav5) return window.pageYOffset;
  var div = document.getElementById("comp_edit");
  if (div && !window.opera) {
    return div.scrollTop;
  }
  return document.documentElement.scrollTop;
}
function getClientHeight()  {
  if (isNav4||isNav5) return innerHeight;
  var div = document.getElementById("comp_edit");
  if (div && !window.opera)
    return div.clientHeight;
  return document.documentElement.clientHeight;
}
function getEventY(e)
{
  if(isNav4||isNav5) {
    return e.pageY;
  }
  var div = document.getElementById("comp_edit");
  if (div) {
    var offset = window.opera ?
      document.documentElement.scrollTop : div.scrollTop;
    return window.event.clientY + offset;
  }
  // IE6
  if(document.documentElement && document.documentElement.scrollTop)
    return window.event.clientY + document.documentElement.scrollTop;
  // IE5
  if(document.body)
    return window.event.clientY + document.body.scrollTop;

  handle_incompatible_browser();
}
//  ---------------------------------------------------------------


//  Log message stuff
function show_logmsg_options()
{
  //  Hide clicked image and show the table row
  var toggle = document.getElementById("logmsg_toggle");
  var table = document.getElementById("logmsg_options");
  if (toggle && table) {
    toggle.style.display = "none";
    table.style.display = "block";

    //  Select the message input field
    var inp = document.getElementById('logmsg');
    if (inp) {
      inp.focus();
      inp.select();
    }
  }
}


//  Dirname stuff
var dirname_enabled = 0;
var dirname_old_txt = "";
var dirname_callout = 0;
var dirname_xml_req = 0;
var dirname_query;

function show_dirname_options(no_select)
{
  //  Hide clicked image and show the table row
  var toggle = document.getElementById("dirname_toggle");
  var cell1 = document.getElementById("dirname_options1");
  var cell2 = document.getElementById("dirname_options2");
  if (toggle && cell1 && cell2) {
    toggle.style.display = "none";
    cell1.style.display = "block";
    cell2.style.display = "block";
    dirname_enabled = 1;

    //  Select the title input field again
    if (!no_select) {
      var inp = document.getElementById('new_title');
      if (inp) {
        inp.focus();
        inp.select();
      }
    }

    //  Trigger an update of the directory name entry
    dirname_update_name_internal();
  }
}


function dirname_in_progress()
{
  return dirname_enabled && (dirname_callout || dirname_xml_req);
}
   
function dirname_spinning_indicator(on)
{
  var o = document.getElementById("spinner");
  if (o) {
    o.style.display = "inline";
    var cur_on = o.style.visibility == "visible";
    if (cur_on != on)
      o.style.visibility = on ? "visible" : "hidden";
  }
}


function dirname_kill_request()
{
  if (dirname_xml_req) {
    dirname_xml_req.abort();
    dirname_xml_req = 0;
  }
}


function dirname_display_result(xml)
{
  var input = document.getElementById("new_dir");
  if (!xml)
    input.value = "";
  else {
    var dirname_node = xml.firstChild;
    var text_node = dirname_node && dirname_node.firstChild;
    var txt = text_node && text_node.nodeValue;
    input.value = txt ? txt : "";
  }
}


function dirname_send_request()
{
  //  Flag that we're no longer waiting for a callout
  if (dirname_callout) {
    window.clearTimeout(dirname_callout);
    dirname_callout = 0;
  }
  
  //  Indicate to user that request will be sent to server
  dirname_spinning_indicator(1);

  //  Kill any other request that may be pending
  dirname_kill_request();

  //  Create a new one. We'll get rid of any Unicode characters in the
  //  query string since some browsers escape them incorrectly.
  var new_query = "";
  for (var i = 0; i < dirname_query.length; i++)
    if (dirname_query.charCodeAt(i) <= 255) {
      new_query += dirname_query.charAt(i);
    }
  var src = dirname_search_base + escape(new_query);
  
  //  If Insite Editor is running with RH_CUSTOMER enabled, there's
  //  an additional checkbox that controls whether a subdirectory should
  //  be created. We'll append this in the query as well.
  var checkbox = document.getElementById("create_subdir");
  if (checkbox) {
    src += "&create_subdir=" + (checkbox.checked ? "on" : "off");
  }
  
  dirname_xml_req = get_ajax_req();
  if (dirname_xml_req) {
    dirname_xml_req.onreadystatechange = function() {
      if (dirname_xml_req.readyState == 4) {
        //  Stop spinning indicator
        dirname_spinning_indicator(0);        
        
        //  Safari 1.3/2.0 reports "undefined" for repeated
        //  requests to the same URL. It's also over-cached even
        //  if the server sets expire headers correctly, but not
        //  much we can do about that.
        if (dirname_xml_req.status == 200 ||
            dirname_xml_req.status == undefined) {
          dirname_display_result(dirname_xml_req.responseXML);
          dirname_xml_req = 0;
        }
      }
    }
    dirname_xml_req.open("GET", src, true);
    dirname_xml_req.send(null);
  }
}


function dirname_update_name(event, force)
{
  if (force)
    dirname_old_txt = "";
  window.setTimeout(dirname_update_name_internal, 10);
  return true;
}


function dirname_update_name_internal()
{
  if (!dirname_enabled) {
    //  Scan string to see if there's any Unicode characters
    var inp = document.getElementById("new_title");
    var txt = inp.value;
    if (txt.length > 0) {
      var some_unicode = 0;
      for (var i = 0; i < txt.length; i++) {
        if (txt.charCodeAt(i) >= 128)
          some_unicode = 1;
      }
      if (some_unicode) {
        show_dirname_options(1);
      }
    }
  }
  if (!dirname_enabled)
    return;
  
  //  Get current dirname string and check whether it's changed
  //  compared to the last time.
  var inp = document.getElementById("new_title");
  var cur_txt = inp.value;
  if (cur_txt != dirname_old_txt) {
    //  Yes, string has changed. We want to send it to the server,
    //  but to avoid excessive amounts of requests we'll postpone
    //  it for 0.5 seconds and only continue if the text field is
    //  left unchanged for this period. Otherwise we reset the timer
    //  and keep waiting.
    dirname_old_txt = cur_txt;
    if (dirname_callout) {
      window.clearTimeout(dirname_callout);
      dirname_callout = 0;
    }
    if (cur_txt == "") {
      //  No need to send anything. We also kill any outstanding
      //  request that's been made so far.
      dirname_display_result(0);
      dirname_spinning_indicator(0);
      dirname_kill_request();
    } else {
      //  Schedule a request 0.5 seconds from now
      dirname_query = cur_txt;
      dirname_callout =
        window.setTimeout(dirname_send_request, 500);
    }
  }
  return true;
}

