/*

Copyright 2003-2005 Conclusive Systems, LLC
All code herein may not be reproduced in full or in part under penalty of law.

*/

/********************************************************************************/
/* MISC */
/********************************************************************************/

/* simple string search */
function str_in_str(needle, haystack)
{
  if (haystack.indexOf(needle) != -1) return true;
  else return false;
}

function trim(str)
{
	return str.replace(/^\s*|\s*$/g, '');
}

/* turns an assoc array into a url-encoded string */
function array_to_string(array, nesting)
{
  if (!nesting) nesting = '';
  var str = '';
  for (var i in array)
  {
    if (typeof(array[i]) == 'object')
    {
      str += array_to_string(array[i], nesting + '[' + i + ']');
    }
    else if (typeof(array[i]) == 'string')
    {
      if (nesting) str += nesting + '[' + i + ']=' + escape(array[i]).replace(/\+/g, '%2B') + '&';
      else str += i + '=' + array[i] + '&';
    }
  }
  return str;
}

function popup(url, win_width, win_height, win_name)
{
  if (!win_width) win_width = 400;
  if (!win_height) win_height = 400;
  if (!win_name) win_name = 'popup';

  win_options = 'toolbar=0, status=1, scrollbars=1, resizable=1, width=' + win_width + ', height=' + win_height + ', left=2, top=2, screenX=2, screenY=2';
  window.open(url, win_name, win_options);
  return false;//false to stop the <a href> from firing
}

/********************************************************************************/
/* ALIASES */
/********************************************************************************/
function get_by_id(id) {return document.getElementById(id);}
function get_by_tag(tag) {return document.getElementsByTagName(tag);}

/********************************************************************************/
/* STYLES */
/********************************************************************************/

function hide(obj) {obj.style.display = 'none';}
function show(obj) {obj.style.display = '';}
function invisible(obj) {obj.style.visibility = 'hidden';}
function visible(obj) {obj.style.visibility = 'visible';}

/********************************************************************************/
/* EVENTS - from www.quirksmode.org */
/********************************************************************************/

function add_event(obj, ev, funct)
{
  if (obj.addEventListener) obj.addEventListener(ev, funct, false);
  else if (obj.attachEvent) obj.attachEvent('on' + ev, funct);
}

function remove_event(obj, ev, funct)
{
  if (obj.removeEventListener) obj.removeEventListener(ev, funct, false);
  else if (obj.detachEvent) obj.detachEvent('on' + ev, funct);
}

function get_target(e)
{
  var targ;
  if (!e) e = window.event;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  else targ = e;
  //if (targ.nodeType && targ.nodeType == 3) targ = targ.parentNode;//for Safari bug
  return targ;
}

/********************************************************************************/
/* CLASSES */
/********************************************************************************/

function add_class(obj, class_str)
{
  /* if the class isn't already attached, attach it */
  if (!str_in_str(' ' + class_str + ' ', ' ' + obj.className + ' ')) obj.className = obj.className + ' ' + class_str;
}

function has_class(obj, class_str)
{
  if (!obj.className) return false;
  var classes = new Array();
  classes = obj.className.split(' ');
  for (var j = 0; j < classes.length; j++)
  {
    if (classes[j] == class_str) return true;
  }
  return false;
}

function remove_class(obj, class_str)
{
  if (str_in_str(class_str, obj.className))
  {
    var new_classes = new Array();
    var current_classes = obj.className.split(' ');
    for (var j = 0; j < current_classes.length; j++)
    {
      if (current_classes[j] != class_str) new_classes[j] = current_classes[j];
    }
    obj.className = new_classes.join(' ');
  }
}

/****************************************************************************************/
/** Mouse behavior **/
/****************************************************************************************/
//rightclick code from http://www.quirksmode.org/js/events_properties.html#button (code in open domain)
/* nothing uses this
function rightclick(e)
{
  var rightclick;
  if (!e) var e = window.event;
  if (e.button) rightclick = (e.button == 2);
  else if (e.which) rightclick = (e.which == 3);
  return rightclick;
}
*/
//end copied code
