/*
Under the Copyright laws, this software may not be copied, translated,
reduced to any electronic or machine readable form or used, in whole or in part
without the prior written consent of Micro Outsource.com, Inc.  
(c)Copyright 2000  Micro Outsource.com,Inc. All rights reserved.
Permission has been granted to Helium.com to use this and modify as necessary.
*/

function LB_remove (index)
{
   var l = this.control.options.length;
   // move all the options from index to the length
   for (var i = index; i < l-1; i++)
   {
      var opt = this.control.options[i+1];
      this.control.options[i] = new Option(opt.text, opt.value, false, false);
   }
   this.control.options[l-1] = null;
   this.control.options.length = l-1;
}


function LB_removeSelected ()
{
	var i = this.control.selectedIndex;
	if (i >= 0)
	{
		this.remove(i);
		this.setSelected(i);
	}
}


function LB_removeAllSelected ()
{
   var l = this.control.options.length;
   var i = 0;
   var j = 0;
   
   // move all the options from index to the length
   for (; i < l; i++)
   {
      var opt = this.control.options[i];
      if (opt.selected)
      {
			//skip this option.
      }
      else
      {
			this.control.options[j] = new Option(opt.text, opt.value, false, false);
			j++;
		}
   }
   this.control.options.length = j;
}





function LB_removeAll ()
{
   var l = this.control.options.length-1;
   for (; l >= 0; l--)
   {
       this.control.options[l] = null;
   }
   this.control.options.length = 0;
}


function LB_addItem (text, value)
{
   var l = this.control.options.length;
	this.control.options[l] = new Option(text, value, false, false);
	return l;
}

function LB_insertItem (text, value, index)
{
   var l = this.control.options.length;
	if (index > l) index = l;
	else if (index < 0) index = 0;
	
   if (l == index)
   {
      this.control.options[index] = new Option(text, value, false, false);
   }
   else
   {
      // move all the options from index to the length
      for (var i = l-1; i >= index; i--)
      {
         var opt = this.control.options[i];
         this.control.options[i+1] = new Option(opt.text, opt.value, false, false);
      }
      this.control.options[index] = new Option(text, value, false, false);
   }
	return index;
}


function LB_getValue (index)
{
	if (index >= 0 && index < this.control.options.length)
		return this.control.options[index].value;
	else
		return null;
}

function LB_getText (index)
{
	if (index >= 0 && index < this.control.options.length)
		return this.control.options[index].text;
	else
		return null;
}

function LB_getSelectedIndex ()
{
	return this.control.options.selectedIndex;
}

function LB_getSelectedValue ()
{
	var index = this.control.options.selectedIndex;
	if (index >= 0 && index < this.control.options.length)
		return this.control.options[index].value;
	else
		return null;
}

function getSelectedValueFromField (obj)
{
	if (obj == null) return null;
	if (obj.type == "INPUT") return obj.value;
	if (obj.type == "LISTBOX" || obj.type == "select-one")
	{
		return new ListBox(obj).getSelectedValue();
	}
	return null;
}

function LB_getSelectedText ()
{
	var index = this.control.options.selectedIndex;
	if (index >= 0 && index < this.control.options.length)
		return this.control.options[index].text;
	else
		return null;
}


function LB_findPreviousAlpha (text)
{
	if (this.control.options.length == 0)
		return -1;

	var j = 0;
   for (; j < this.control.options.length; j++)
   {
      var o = this.control.options[j];
      var currenttext = o.text;
      if (currenttext > text)
      {
			if (j > 0)
				return j - 1;
			else
				return -1;
      }
   }
   return this.control.options.length;
}


function LB_findValue (v)
{
	var i = 0;
   for (; i < this.control.options.length; i++)
   {
      var opt = this.control.options[i];
      if (v == opt.value)
      {
         return i;
      }
   }
   return -1;	
}


function LB_setSelected (index)
{
	var i = 0;
   for (; i < this.control.options.length; i++)
   {
      var opt = this.control.options[i];
		opt.selected = i == index;
   }
}

function LB_setAdditionalSelected (index)
{
   if (index < this.control.options.length)
   {
      var opt = this.control.options[index];
		opt.selected = true;
   }
}

function LB_selectAll ()
{
	var i = 0;
	for (; i < this.control.options.length; i++)
	{
		var opt = this.control.options[i];
		opt.selected = true;
	}
}

function LB_unselectAll ()
{
	var i = 0;
	for (; i < this.control.options.length; i++)
	{
		var opt = this.control.options[i];
		opt.selected = false;
	}
}

function LB_getSize ()
{
	return this.control.options.length;
}

function LB_getMultipleValues ()
{
	var i = this.getSize();
	var values = "";
	var j = 0;
	for (; j < i; j++)
	{
		var v = this.getValue(j);
		if (values.length != 0) values += ",";
		values += v;
	}
	return values;
}

function LB_getSelectedValues ()
{
	var i = this.getSize();
	var values = "";
	var j = 0;
	for (; j < i; j++)
	{
      var opt = this.control.options[j];
		if (opt.selected)
		{
			var v = this.getValue(j);
			if (values.length != 0) values += ",";
			values += v;
		}
	}
	return values;
}

function LB_getSelectedValuesArray ()
{
	var i = this.getSize();
	var values = new Array();
	var j = 0;
	for (; j < i; j++)
	{
      var opt = this.control.options[j];
		if (opt.selected)
		{
			var v = this.getValue(j);
			values[values.length] = v;
		}
	}
	return values;
}

function LB_getSelectedIndexes ()
{
	var result = new Array();
	var i = 0;
   for (; i < this.control.options.length; i++)
   {
      var opt = this.control.options[i];
		if (opt.selected)
		{
			result[result.length] = i;
		}
   }
	return result;
}

function LB_focus ()
{
	if (this.control != null)
	{
		this.control.focus();
	}
}

function ListBox (control)
{
   this.control = control;
   this.remove = LB_remove;
   this.removeSelected = LB_removeSelected;
   this.removeAllSelected = LB_removeAllSelected;
   this.removeAll = LB_removeAll;
   
   this.addItem = LB_addItem;
   this.insertItem = LB_insertItem;
   
   this.getValue = LB_getValue;
   this.getText = LB_getText;

   this.getSelectedIndex = LB_getSelectedIndex;
   this.getSelectedValue = LB_getSelectedValue;
   this.getSelectedValues = LB_getSelectedValues;
   this.getSelectedText = LB_getSelectedText;
   
   this.findValue = LB_findValue;
   this.findPreviousAlpha = LB_findPreviousAlpha;
   
   this.setSelected = LB_setSelected;
   this.setAdditionalSelected = LB_setAdditionalSelected;
   this.selectAll = LB_selectAll;
   this.unselectAll = LB_unselectAll;
   
   this.getSize = LB_getSize;
   
   this.getMultipleValues = LB_getMultipleValues;
   this.getSelectedValuesArray = LB_getSelectedValuesArray;
   this.getSelectedIndexes = LB_getSelectedIndexes;
   this.focus = LB_focus;
}

function RB_setSelectedValue (newValue)
{
	if (this.arrayp)
	{
		var l = this.control.length;
		for (var i = 0; i < l; i++)
		{
			var c = this.control[i];
			c.checked = c.value == newValue;
		}
	}
	else
	{
		this.control.checked = this.control.value == newValue;
	}
}

function RB_getSelectedValue ()
{
	if (this.arrayp)
	{
		var l = this.control.length;
		for (var i = 0; i < l; i++)
		{
			var c = this.control[i];
			if (c.checked)
				return c.value;
		}
		return null;
	}
	else
	{
		if (this.control.checked)
		{
			return this.control.value;
		}
		else
		{
			return null;
		}
	}
}

function RB_setSelectedIndex (index)
{
	if (this.arrayp)
	{
		var l = this.control.length;
		for (var i = 0; i < l; i++)
		{
			var c = this.control[i];
			c.checked = false;
		}
		if (0 <= index && index < l)
		{
			var c = this.control[index];
			c.checked = true;
		}
	}
	else if (index == 0)
	{
		this.control.checked = true;
	}
	else
	{
		this.control.checked = false;
	}
}

function RB_getCount ()
{
	if (this.arrayp)
	{
		return this.control.length;
	}
	else
		return 1;
}

function RB_getSelectedIndex ()
{
	if (this.arrayp)
	{
		var l = this.control.length;
		for (var i = 0; i < l; i++)
		{
			var c = this.control[i];
			if (c.checked)
				return i;
		}
		return -1;
	}
	else
	{
		if (this.control.checked)
		{
			return 0;
		}
		else
		{
			return -1;
		}
	}
}

function RB_getSelectedValues ()
{
	var values = "";
	if (this.arrayp)
	{
		var l = this.control.length;
		for (var i = 0; i < l; i++)
		{
			var c = this.control[i];
			if (c.checked)
			{
				if (values.length != 0) values += ",";
				values += c.value;
			}
		}
	}
	else
	{
		if (this.control.checked)
		{
			values = this.control.value;
		}
	}
	return values;
}

function RB_getSelectedValuesArray ()
{
	var values = new Array();
	if (this.arrayp)
	{
		var l = this.control.length;
		for (var i = 0; i < l; i++)
		{
			var c = this.control[i];
			if (c.checked)
			{
				values[values.length] = c.value;
			}
		}
	}
	else
	{
		if (this.control.checked)
		{
			values[values.length] = this.control.value;
		}
	}
	return values;
}

function RB_getSelectedCount ()
{
	var result = 0;
	if (this.arrayp)
	{
		var l = this.control.length;
		for (var i = 0; i < l; i++)
		{
			var c = this.control[i];
			if (c.checked)
			{
				result++;
			}
		}
	}
	else
	{
		if (this.control.checked)
		{
			result = 1;
		}
	}
	return result;
}


function RadioButton (control, arrayp)
{
	if (typeof(arrayp) == "undefined")
	{
		this.arrayp = control.length > 0;
	}
	else
	{
		this.arrayp = arrayp;
	}
	this.control = control;
	
	
	this.getSelectedValue = RB_getSelectedValue;
	this.setSelectedValue = RB_setSelectedValue;
	this.getSelectedValues = RB_getSelectedValues;
	this.getSelectedValuesArray = RB_getSelectedValuesArray;
	this.getSelectedIndex = RB_getSelectedIndex;
	this.setSelectedIndex = RB_setSelectedIndex;
	this.getCount = RB_getCount;
	this.getSelectedCount = RB_getSelectedCount;
}
