//
// Copyright(c) 2005 Gammakurve Corporation. All rights reserved.
//

function setHeightToParent(elt)
{
  var e = document.getElementById(elt);
  var h = e.parentNode.offsetHeight;
  h -= 50;

  if (h > 0) {
    e.style.height = h + 'px';
  }
}

function setHeight(setto, myElement)
{
  var boxElement = document.getElementById(myElement);
  boxElement.style.height = setto;
}

function setCursor(setto, myElement)
{
  var boxElement = document.getElementById(myElement);
  boxElement.style.cursor = setto;
}

function adjustHeight(elements, minHeight, maxHeight)
{
  if (elements == null) {
    return false;
  }

  var elt = new Array();
  var tmpMaxHeight = 0;
  for (var i = 0; i < elements.length; i++) {
    elt[i] = document.getElementById(elements[i]);
    if (elt[i] != null && elt[i].offsetHeight > tmpMaxHeight) {
      tmpMaxHeight = elt[i].offsetHeight;
    }
  }

  if (tmpMaxHeight < minHeight) {
    tmpMaxHeight = minHeight;
  }

  if (tmpMaxHeight > maxHeight) {
    tmpMaxHeight = maxHeight;
  }

  for (var i = 0; i < elements.length; i++) {
    if (elt[i] != null) {
      elt[i].style.height = tmpMaxHeight + 'px';
    }
  }

  return true;
}

function adjustRoundedboxToSidebar()
{
  var sb = document.getElementById('sidebarGrip');
  var rb = document.getElementById('roundedbox');
  var rbc = document.getElementById('roundedbox-content');

  if (typeof(sb) == 'undefined' || sb == null) {
    return false;
  }

  var sbh = sb.offsetHeight;
  var rbt = rb.offsetTop;
  var rbo = getXY(rb);
  var rbco = getXY(rbc);
  var rboh = sbh - rbt - (rbco.y - rbo.y) - 10;
  rbc.style.height = rboh + 'px';

  return true;
}

function trim(str)
{
  return ltrim(rtrim(str));
}

function ltrim(str)
{
  for (var j = 0; j < str.length; j++) {
    var c = str.charCodeAt(j);
    if (c > 32 && c < 127) {
      return str.substring(j, str.length+1);
    }
  }

  return "";
}

function rtrim(str)
{
  for (var j = str.length; j >= 0; j--) {
    var c = str.charCodeAt(j);
    if (c > 32 && c < 127) {
      return str.substring(0,j+1);
    }
  }

  return "";
}

function strip(str)
{
  var x = "";
  var space = false;

  for (var j = 0; j < str.length; j++) {
    var c = str.charCodeAt(j);
    // Gather ASCII printables only...Unicode?
    if (c > 31 && c < 127) {
      x += str.charAt(j);
    } else {
      if (!space) {
	x += ' ';
	space = true;
      }
    }
  }

  return x;
}

function getXY(o)
{
  var thisO = o;
  var defx = 0;
  var defy = 0; 
  if (thisO.offsetParent) {
    while (thisO) {
      defx += parseInt(thisO.offsetLeft);
      defy += parseInt(thisO.offsetTop);
      thisO = thisO.offsetParent || null;
    }
  } else if (thisO.x) {
    defx = thisO.x;
    defy = thisO.y;
  }

  var r = new Object();
  r.x = defx;
  r.y = defy;

  return r;
}
