
var sel;    // variable for holding an HTML DOM select object
var opt;    // variable for holding an HTML DOM option object
var img;    // variable for holding an HTML DOM image object
var swap;   // variable for holding an HTML DOM image object
var newimg; // variable for holding an HTML DOM image object
var elem;   // variable for holding an HTML DOM element object
var redir;  // variable for holding an alternerate id

/*
** this function finds and returns an element by id or false if not 
** found.
*/
function find_id(id)
{
  if (('' + id) && (elem = document.getElementById(id)))
  {
    return elem;
  }
  return false;
}

/*
** this function hides an element by id.
*/
function hide_id(id)
{
  if (find_id(id))
  {
    elem.style.display    = 'none';
    elem.style.visibility = 'hidden';

    return elem;
  }
  return false;
}

/*
** this function shows an element by id (and by display style -- default
** is inline).
*/
function show_id(id, display)
{
  if (!display)
  {
    display = 'inline'; 
  }
  if (find_id(id))
  {
    elem.style.display    = display;
    elem.style.visibility = 'visible';

    return elem;
  }
  return false;
}

/*
** this function disables changes to a form element, and keeps its
** value from passing to the form handler.
*/
function disable(id)
{
  if (find_id(id))
  {
    elem.value	  = '';
    elem.readOnly = 'ReadOnly';
    elem.disabled = 'disabled';

    return elem;
  }
  return false;
}

/*
** function pending disables a button.
*/
function pending(id)
{
  if (find_id(id))
  {
    elem.readOnly = 'ReadOnly';
    elem.disabled = 'disabled';

    return elem;
  }
  return false;
}

/*
** this function enables changes to a form element, and makes sure its
** value will pass to the form handler.
*/
function enable(id)
{
  if (find_id(id))
  {
    elem.readOnly = '';
    elem.disabled = '';

    return elem;
  }
  return false;
}

/*
** this function puts a value in a form element as selected by id, just
** to make the interface a little simpler
*/
function input_value(id, value)
{
  if (find_id(id))
  {
    return elem.value = value;
  }
  return 'NOT FOUND';
}

/*
** Specific page helpers for select.
*/
function alt_options(list, item)
{
  len = list.length;
  i   = 0;

  for (i = 0; i < len; i++)
  {
    if (item == list[i][0])
    {
       return list[i];    
    }
  }
  return new Array();
}

/*
** fill_select (select, options, clear_one)
**
** required:  select	-> which select box to modify
**	      options	-> data to fill box with.
** optional:  clear_one	-> T/F default false, on true removes all old
**			    options for overwrite.  Otherwise leaves the
**			    first option. **** IMPORTANT ****
**			    if you want to completely erase the old
**			    version of the select you must pass a true
**			    value for this field!
*/
function fill_select(select, options, clear_one)
{
  clear_one	= ((clear_one) ? 0 : 1);
  len		= options.length;
  select.length = len + clear_one;

  for (i = 0; i < len; i++)
  {
    select.options[i + clear_one].value = options[i][0];
    select.options[i + clear_one].text  = options[i][1];
  }
}


/*
** this function stores a value in an input field and launches a form
*/
function confirmed_submit(id, input_id, form_id)
{
  input_value('' + input_id, id);
  if (find_id(form_id))
  {
    elem.submit();
  }
}


/*
** this function is for coloring a select to match the option, and 
** changing a display image.  Specifically designed for use in the
** publitapias site.
*/
function color_me(id, indir)
{
  if (find_id(id))
  {
    sel = elem;

    if (opt = sel.options[sel.selectedIndex])
    {
      redir = opt.id;

      if (!indir)
      {
	redir = opt.value;
      }
      if (!swap)
      { 
	swap = document.getElementById('flag_swap');
      }
      if (find_id('img_' + id))
      {
	img = elem;

	if (find_id('flag_id_' + redir))
	{
	  newimg      = elem;
	  swap.src    = img.src;
	  img.src     = newimg.src;
	  newimg.src  = swap.src;
	}
      }
      sel.style.backgroundColor = opt.style.backgroundColor;
      sel.style.color		= opt.style.color;
    }
  }
}

/*
** this function checks a select box's options for a match against value
** if found, resets the selectedIndex to the matching option, if not
** sets selectedIndex = -1.
*/
function get_option(sel_find, id_find, value)
{
  if ((!sel_find) && (!id_find))
  {
    return;
  }
  if (!sel_find)
  {
    sel = find_id('' + id_find);
  }
  else
  {
    sel = sel_find;
  }
  var opt = sel.options;
  var num = opt.size;
  var ind = 0;

  sel.selectedIndex = -1;

  while (ind < num)
  {
    if (value == opt[ind].value)
    {
      sel.selectedIndex = ind;
      break;
    }
    ind++;
  }
}

/*
** function deref
** This function  dereferences a hash by
** first array element (id)
** No previsions for multiple matching hash entries.
*/
function deref(source, id)
{
  for (i = 0; i < source.length; i++)
  {
    if (source[i][0] == id)
    {
      return source[i];
    }
  }
  return new Array();
}

/*
** Tools for Multi-lists
*/

/*
** change_lists()
** This is the action for onChange action
** of the list select box.
*/
function change_lists()
{
   document.getElementById('select_list').submit();
}

/*
** function fix_to(box)
** This function fix the current passed box
** droping out all the selected items
** (It's suppossed that this items where sent
** to another list.)
*/

function fix_to(box)
{
  for (ind = 0; ind < box.options.length; ind++)
  {
    option = box.options[ind];

    if (option.selected)
    {
      option2 = box.options[ind + 1]
      option  = option2;
      box.remove(ind);
      ind--;
    }
  }
}

/*
** function rebuild_txt()
** This function rebuilds the str_curr field
** with the values in the current select box
*/
function rebuild_txt() 
{
  box	    = find_id('edit_sel_current');
  txt_field = find_id('str_curr');
  txt	    = "";

  for (ind = 0; ind < box.length; ind++)
  {
    if (txt != '')
    {
      txt = txt + "|";
    }
    txt = txt + box.options[ind].value;
  }

  txt_field.value = txt;
}

/*
** is_ancestor -- needed to roll my own, since yui's dom didn't cover
**		  enough cases correctly. 
** @param   {HTMLElement} haystack The possible ancestor
** @param   {HTMLElement} needle The possible descendent
** @return  {Boolean}	  Whether or not the haystack antecedes needle
*/
function is_ancestor(haystack, needle)
{
  var parent; 

  if ((haystack) && (needle))
  {
    if (haystack == needle) // not sure if this test should be here ...
    {			    // but cuts down code elsewhere.
      return true;
    }
    for (parent = needle.parentNode; parent; parent = parent.parentNode)
    {
      if      (parent	      == haystack)  { return true;  }
      else if (parent.tagName == 'HTML')    { return false; }
    }
  }
  return false;
}

/*
** function move_elemnts(from, to)
** This function moves objects from "from"
** to "to"
*/
function move_elemnts(from, to)
{
  from = find_id('' + from);
  to   = find_id('' + to);

  for (ind = 0; ind < from.length; ind++)
  {
    option = from.options[ind];

    if (option.selected)
    {
      to.length			      = to.length + 1;
      to.options[to.length - 1].value = option.value;
      to.options[to.length - 1].text  = option.text;
    }
  }
  
  fix_to(from);
  rebuild_txt();
}

/*
** function is_element_in(elem, box, select)
** This function checks if the element "elem"
** existe in the select box "box".
** If so, "elem" will be marked as true
** (for future call of the function fix to)
** Otherwise, it does nothing 
*/

function is_element_in(elem, box, select)
{
  if (box.length > 0)
  {
    for (i = 0; i < box.length; i++)
    {
      option = box.options[i];
    
      if (option.value == elem.value)
      {
	elem.selected = select;
	return true;
      }
    }
  }

  return false;
}

/*
** function fix_possible()
** This function fixes the select box
** possible, right after filling the 
** select box current up with the
** values the list contains.
*/
function fix_possible()
{
  current  = find_id('edit_sel_current');
  possible = find_id('edit_sel_possible');

  for (ind = 0; ind < possible.length; ind++)
  {
    option = possible.options[ind];
    
    is_element_in(option, current, true);
  }

  fix_to(possible);
}

/*
** function replace_char()
** This functions replaces one character for another
** inside a string, and returns it back.
*/
function replace_char(curr_text, character, replace)
{
  var res	= "";
  var curr_char = '';
  for (i = 0; i < curr_text.length; i++)
  {
    curr_char = curr_text[i];
    
    if (curr_char == character)
    {
      curr_char = replace; 
    }
    res = res + curr_char; 
  }

  return res;
}

/*
** function change_class()
** either adds or tries to remove a class from an objects class list.
*/
function change_class(obj, cls, add)
{
  if (add)
  {
    obj.className += ' ' + cls;
  }
  else
  {
    obj.className = obj.className.replace(cls, '').replace(/ ?$/, '');
  }
}



/*
** FOR THE MOMENT
**
*/

var images = new Array();

function preload(name, ext)
{
  path                = 'imgs/';
  /*
  temp_array          = new Array();
  temp_array['a']     = new Image();
  temp_array['a'].src = path + name + '_a.' + ext;
  temp_array['b']     = new Image();
  temp_array['b'].src = path + name + '_b.' + ext;
  */
  images[name]        = new Image();
  images[name].src    = path + name + '.' + ext;
}

function toggle_image(name, which)
{
  img_elem        = document.getElementById(name);
  img_elem.src    = images[which].src;
}

/*
NEW WINDOW
*/

function popUp(url, pageWidth, pageHeight)
{
    var args = "width=" + pageWidth + ",height=" + pageHeight + ",scrollbars,resizable";
    var date = new Date();
    var now = date.getTime();
    var name = (now).toString(10);
    
    window.open(url,name,args);
}