// Copyright 2003,2004,2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xSelect( sId, oldContainerId, s1Id, s2Id, newContainerId )
{
  //// Properties
  
  this.ready = false;
  
  //// Constructor

  // Check for required browser objects
  var s0 = xGetElementById(sId);
  var s1 = xGetElementById(s1Id);
  var s2 = xGetElementById(s2Id);
  
  if (!s0 || !s0.firstChild || !s0.nodeName || !document.createElement || !s0.form || !s0.form.appendChild)
  {
    return;
  }
  
  s1.defaultValue = -1;
  s2.defaultValue = -1;
  
//  alert( xGetElementById( containerId ).appendChild );
  
  // Create main category SELECT element
  s1.xSelObj = this;
  s1.xSelData = new Array();
  // append s1 to s0's form
  //s0.form.appendChild(s1);
  //s0.form.removeChild(s0);

  // Iterate thru s0 and fill array.
  // For each OPTGROUP, a[og][0] == OPTGROUP label, and...
  // a[og][n] = innerHTML of OPTION n.
  var v, ig=0, io, op, og, a = s1.xSelData;
  og = s0.firstChild;
  while (og) {
    if (og.nodeName.toLowerCase() == 'optgroup') {
      io = 0;
      a[ig] = new Array();
	  a[ig][io] = new Object();
      a[ig][io].text = og.label;
      a[ig][io].value = og.title;
      op = og.firstChild;
      while (op) {
        if (op.nodeName.toLowerCase() == 'option') {
          io++;
		  a[ig][io] = new Object();
		  a[ig][io].text = op.text;
		  a[ig][io].value = op.value;
		  op.value = '';
		  if( op.selected ) {
		  	s1.defaultValue = og.title;
		  	s2.defaultValue = a[ig][io].value;
		  }
        }
        op = op.nextSibling;
      }
      ig++;
    }
    og = og.nextSibling;
  }

  // in s1 insert a new OPTION for each OPTGROUP in s0
  for (ig=0; ig<a.length; ++ig) {
  	v = a[ig][0].value;
    op = new Option(a[ig][0].text, v );
    s1.options[ig] = op;
	op.selected = ( v == s1.defaultValue );
  }
  
  // Create sub-category SELECT element
  s2.xSelMain = s1;
  s1.xSelSub = s2;
  // append s2 to s0's form
  //s0.form.appendChild(s2);
  
  // Add event listeners
  s1.onchange = xSelectMain_OnChange;
  //s2.onchange = fnSubOnChange;
  // hide original select
  //s0.style.display = 'none';
  xGetElementById(oldContainerId).style.display = 'none';
  xGetElementById(newContainerId).style.display = '';
  
  s1.onchange();
  // Ready to rock!
  this.ready = true;
  
} // end xSelect object prototype

function xSelectMain_OnChange()
{
  var v, io, s2 = this.xSelSub;
  // insert new
  var a = this.xSelData, ig = this.selectedIndex;
  for (io=1; io<a[ig].length; ++io) {
  	v = a[ig][io].value;
    op = new Option( a[ig][io].text, v );
    s2.options[io-1] = op;
	op.selected = ( v == s2.defaultValue );
  }
  s2.length = io-1;
}

function xGetElementById(e)
{
  if(typeof(e)!='string') return e;
  if(document.getElementById) e=document.getElementById(e);
  else if(document.all) e=document.all[e];
  else e=null;
  return e;
}
