function MultiSelect (object,divId,values) {
	this.object = object;
	this.selectId = object + 'Select';
	this.selectMethod = this.object+'.select()';
	this.removeMethod = this.object+'.remove';
	this.divId = divId;
	this.index = 0;
	this.values = values;
	this.selected = new Array( values.length );
	this.hideSelected = true;
	this.setInitial = function(initial) {
		//alert( initial );
		if( typeof( initial ) == 'object' && null != initial ) {
			for( var i=0; i < this.values.length; i++ ) {
				for( var j=0; j < initial.length; j++ ) {
					if( this.values[i] == initial[j] ) {
						this.selected[i] = true;
						break;
					}
				}
			}
		}
	}
	this.display = function() {
		var html = "<div class='MultiSelect'>";
		var noSelected = 0;
		for( var i = 0; i < this.selected.length; i++ ) {
			if( this.selected[i] ) {
				html += this.createItem( values[i],i );
				noSelected++;
			}
		}
		if( noSelected < values.length ) {
			html += this.displayCombo();
		}
		html += "</div>";
		document.getElementById(this.divId).innerHTML = html;
	};
	this.createItem = function(value,i) {
		var html = "<div class='MultiSelectValue'>";
		html += value;
		html += "<a href='javascript:"+this.removeMethod+"("+i+");' border=0 >"
		html += "<img src='scripts/cross.png' /></a>";
		html += "</div>\n"
		return html;
	};
	this.displayCombo = function() {
		var html = "<select id='"+this.selectId+"' onChange='"+this.selectMethod+"'>";
		html += "<option>Add value</option>";
		for( var i = 0; i < this.values.length; i++ ) {
			if( !(this.hideSelected && this.selected[ i ] )) {
				html += "<option"
				if( i+1 == this.index ) html += " selected";
				html += ">"+values[i]+"</option>";
			}
		}
		html += "</select>"
		//alert( html );
		return html;
	};
	this.select = function() {
		this.updateSelected();
		if( this.index != 0 ) this.display();
	};
	this.updateSelected = function() {
		//alert( "oldSelected" );
		var selectObj = document.getElementById( this.selectId );
		this.index = selectObj.selectedIndex;
		if( this.index != 0 )
			this.selected[ values.indexOf( selectObj.options[ this.index ].text ) ] = true;
	};
	this.remove = function( value ) {
		this.selected[ value ] = false;
		this.display();
	};
	this.getValues = function() {
		var vals = new Array()
		for( var i = 0; i < this.values.length; i++ ) {
			if( this.selected[i] ) vals.push( this.values[i] );
		}
		return vals;
	};
	this.getIndices = function() {
		var vals = new Array()
		for( var i = 0; i < this.values.length; i++ ) {
			if( this.selected[i] ) vals.push( i );
		}
		return vals;
	};
}

function DualMultiSelect (object,divId,values,address) {
	this.object = object;
	this.selectId = this.object + "OuterSelect";
	this.selectMethod = object + ".outerSelect()";
	this.removeMethod = object + ".outerRemove";
	this.divId = divId;
	this.address = address;
	this.values = Array();
	this.noValues = 0;
	this.selected = new Array( values.length );
	this.setInitial = function(initial) {
		if( typeof( initial ) == 'object' && null != initial ) {
			for( var i=0; i < initial.length; i++ ) {
				var i2 = initial[i].indexOf(" ->- ");
				var sector = "";
				var subsector = "";
				if( i2 > 0 ) {
					sector = initial[i].slice( 0,i2 );
					subsector = initial[i].slice( i2+5 );
				} else {
					sector = initial[i];
					subsector = "__All__";
				}
				var index = this.innerSelect.values.indexOf( sector );
				if( index > -1 ) {
					if( typeof( this.selected[ index ] ) != 'object' ) this.selected[ index ] = Array();
					this.selected[ index ].push( subsector );
				}
			}
		}
		//alert(this.selected);
	}
	this.innerSelect = new MultiSelect ( object,divId + "Inner",values );
	this.innerSelect.hideSelected = false;
	this.innerSelect.outerSelect = this;
	this.innerSelect.updateSelected = function() { 
		//alert( "newSelected" );
		var selectObj = document.getElementById( this.selectId );
		this.index = selectObj.selectedIndex;
		if( this.index != 0 ) {
			var outerSelected = this.outerSelect.selected[ this.index-1 ];
			//if( typeof( outerSelected ) == 'object' ) alert( outerSelected + " " + outerSelected.length + " / " + this.outerSelect.noValues );
			if( typeof( outerSelected ) == 'object' && outerSelected.length > this.outerSelect.noValues ) 
				this.selected[ values.indexOf( selectObj.options[ this.index ].text ) ] = true;
			//alert( this.selected[ values.indexOf( selectObj.options[ this.index ].text ) ] );
			//alert( this.index );
		}
		this.outerSelect.display();
	};
	this.displayCombo = function() {
		//alert( "hiya" );
		var html = "<select id='"+this.selectId+"' onChange='"+this.selectMethod+"' ";
		if( this.values.length == 0 ) html += "disabled = 'disabled'";
		html += "><option>Add value</option>";
		if( this.values.length != 0 ) {
			var iSelected = this.selected[ this.innerSelect.index ];
			//alert( iSelected );
			if( typeof( iSelected ) == 'object' ) {
				for( var i=0; i < values.length; i++ )
					if( !iSelected[ i ] ) html += "<option>"+values[i]+"</option>";
			}
		}
		html += "</select>";
		//alert( html );
		return html;
	};
	this.createItem = function(value,i,j) {
		var html = "<div class='MultiSelectValue'>";
		html += value;
		html += "<a href='javascript:"+this.removeMethod+"("+i+", "+j+");' border=0 >"
		html += "<img src='scripts/cross.png' /></a>";
		html += "</div>\n"
		return html;
	};
	this.outerRemove = function( a,b ) {
		if( b == -1 ) {
			for( var i=0; i < this.selected[a].length; i++ ) {
				if( this.selected[a][i] == "__All__" ) {
					this.selected[a] = this.selected[a].slice(0,i).concat( this.selected[a].slice(i+1) );
					break;
				}
			}
		} else {
			this.selected[a] = this.selected[a].slice(0,b).concat( this.selected[a].slice(b+1) );
		}
		this.display();
	}
	this.display = function() {
		var html = "<div class='MultiSelect'>";
		for( var i=0; i < this.selected.length; i++ ) {
			if( typeof( this.selected[ i ] ) == 'object' && this.selected[ i ].length ) {
				for( var j=0; j < this.selected[ i ].length; j++ ) {
					if( this.selected[ i ][ j ] == '__All__' )
						html += this.createItem( this.innerSelect.values[ i ],i,-1 )
					else
						html += this.createItem( this.innerSelect.values[ i ] + " - " + this.selected[ i ][ j ], i,j ); 
				}
			}
		}
		html += "<div>";
		html += this.innerSelect.displayCombo();
		html += this.displayCombo();
		html += "</div></div>";
		//alert( html );
		document.getElementById(divId).innerHTML = html;
		if( this.innerSelect.index != 0 ) this.loadOptions();
	};

	this.select = function() {
		this.innerSelect.updateSelected();
		this.loadOptions();
		//this.innerSelect.display();
	};
	
	this.loadOptions = function() {
		var selectObj = document.getElementById( this.innerSelect.selectId );
		var subSelectObj = document.getElementById( this.selectId );
		var obj = this; //strange scope hack
		//alert( selectObj.options[ selectObj.selectedIndex ].text );
		new Ajax.Request( this.address, { method:'get',
			parameters: { sector: selectObj.options[ selectObj.selectedIndex ].text },
    	onSuccess: function(oReq){
    		data = eval(oReq.responseText);
    		//alert( data );
   			subSelectObj.options.length = 0;
   			subSelectObj.options[ subSelectObj.options.length ] 
    							= new Option( "--Select Subsector--","default" );
    		subSelectObj.options[ subSelectObj.options.length ] 
    							= new Option( "All","All" );
    		subSelectObj.disabled=false;
    		obj.noValues = data.length;
    		if( data.length > 0 ) {
    			for (var i = 0; i < data.length; i++) {
      			subSelectObj.options[ subSelectObj.options.length ] 
      							= new Option( data[i], data[i] );
      		}
      	} 
    	},
    	onFailure: function() { 
    		alert('Something went wrong...');
    	}
  	});
	};
	
	this.outerSelect = function() {
		this.addSelected( document.getElementById( this.innerSelect.selectId ).selectedIndex,
											document.getElementById( this.selectId ).selectedIndex );
		//alert( this.selected );
	};
	
	this.addSelected = function( i,i2 ) {
		if( i == 0 ) return;
		if( !(typeof( this.selected[ i-1 ] ) == 'object' && this.selected[ i-1 ].length ))
			this.selected[ i-1 ] = new Array();
		if( i2 > 1 ) {
			var text = document.getElementById( this.selectId ).options[ i2 ].text;
			if( this.selected[ i-1 ].indexOf( text ) == -1 ) this.selected[ i-1 ].push( text );
		}
		else if( i2 == 1 && this.selected[ i-1 ].indexOf( '__All__' ) == -1 )
			this.selected[ i-1 ].push( '__All__' );
		//alert( this.selected + " " + this.selected.length );
		this.display();
	};
	
	this.getValues = function() {
		var result = Array();
		//alert( this.selected );
		for( var i=0; i < this.selected.length; i++ ) {
			if( typeof( this.selected[i] ) == 'object' ) {
				for( var j=0; j < this.selected[i].length; j++ ) {
					if( this.selected[i][j] == '__All__' ) result.push( this.innerSelect.values[i] );
					else result.push( this.innerSelect.values[i] + " ->- " + this.selected[i][j] );
				}
			}
		}
		return result;
	}
}
