// JavaScript Document

function ItemSet( tag )
{
	this.tag = tag;
	this.itemList = new Array();
}

ItemSet.prototype.add = function( n )
{
	this.itemList[this.itemList.length] = n;
}

ItemSet.prototype.getLength = function()
{
	return this.itemList.length;
}

ItemSet.prototype.get = function( n )
{
	return this.itemList[n];
}

ItemSet.prototype.getById = function( id )
{
	for ( var n = 0; n < this.itemList.length; n++ )
	{
		var i = this.itemList[n];
		if ( ( i.id != null ) && ( i.id == id ) )
		{
			return i;
		}
	}
	
	return null;
}

ItemSet.prototype.addLabel = function( label, style )
{
	var i = new LabelItem( label );
	this.add( i );
}

ItemSet.prototype.addSelect = function( id, label, list )
{
	var i = new SelectItem( id, label, list );
	this.add( i );
}

ItemSet.prototype.addCheckbox = function( id, label )
{
	var i = new CheckboxItem( id, label );
	this.add( i );
}

ItemSet.prototype.encode = function()
{
	var sMap = new HashMap();
	for ( var n = 0; n < this.getLength(); n++ )
	{
		var i = this.get( n );
		if ( i.id == null ) { continue; }
		
		var elementName = "e_" + i.id;
		var elementList = document.getElementsByName( elementName );
		if ( ( typeof elementList ) == 'undefined' )
		{
			alert( "Couldn't find element: " + elementName );
			continue;
		}
		
		// should only be one element in the collection
		var element = elementList[0];
		switch ( i.type )
		{
		case 'boolean':
			sMap.put( element.name, element.checked );
			break;
			
		case 'select':
			sMap.put( element.name, element.value );
			break;
		}
	}
	
	return sMap.toString();
}

function LabelItem( label )
{
	this.type = "label";
	this.id = null;
	this.label = label;
}

function SelectItem( id, label, list )
{
	this.type = "select";
	this.id = id;
	this.label = label;
	this.list = list;
}

function CheckboxItem( id, label  )
{
	this.id = id;
	this.type = "boolean";
	this.label = label;
}

function SelectList()
{
	this.defaultLabel = "&lt;select&gt;";
	this.list = new Array();
}

SelectList.prototype.add = function( value, label )
{
	this.list[this.list.length] = new SelectEntry( value, label );
}

SelectList.prototype.getLength = function()
{
	return this.list.length;
}

SelectList.prototype.get = function( n )
{
	return this.list[n];
}

SelectList.prototype.getLabel = function( value )
{
	for ( var n = 0; n < this.list.length; n++ )
	{
		var l = this.list[n];
		if ( l.value == value )
		{
			return l.label;
		}
	}
	
	return null;
}

function SelectEntry( value, label )
{
	this.value = value;
	this.label = label;
}

SelectEntry.prototype.label = "";
SelectEntry.prototype.value = "";
