/*----------------------------------------------------------
  LineSelect Class
  
  
  Basic function:
    * SHows and hides lines in the select lit according to their company
  

  Requires the utilities.js 

  @package NTP
  @author Arnd Beyer, arnd.beyer@wmdata.fi
  @copyright WM-data Novo 2006
  @version $Id: UserSettings.php,v 1.0 2005/07/26 12:44:42 arnd Exp $
  ---------------------------------------------------------*/

/*
*  parameter: objectVariableName String Name of the variable 
*									where the Object will be stored in
*  Used for the setTimeout Function
*/
function LineSelect(objectVariableName){

	var nbsp = String.fromCharCode(160);
	var lines = new Array();
	
	this.filterLines = filterLines;
	this.addLine = addLine;
	
	function addLine(entry,value,companyId, group){
		var line = { "entry"   : entry,
					 "value"   : value,
					 "company" : companyId,
					 "group"   : group};
		
		lines.push(line);
	}
	
	function filterLines(conditionId, listName, listId, className, tabindex, size){
	  	//var intCompanyId = document.search.tt_company.value;
	  	var intCompanyId = attach(conditionId).value;
	  	var list = attach(listId);
	  	var div = attach(listId +"_div");
		var selectedLine ='';

	  	if (list.value != '')
	  		selectedLine = list.value;
		list.options.length = 0; //Delete entries
		list.innerHTML ='';

		var html = '<select name="'+listName+'" id="'+listId+'" ';
		html += '  multiple="multiple" ';
		html += ' tabindex="'+tabindex+'"';
	    html += ' size="'+size+'"';
		html += ' class="'+ className+'" ';
		html +='>';
        var old_group ='';
        var optgroup_open = false;
		for(var i=0;i < lines.length;i++) {
			if(intCompanyId==0 || lines[i].company == intCompanyId) {
			    if (old_group != lines[i].group ) {
			      if (old_group != '') {
			        html += '</optgroup>';
			      } 
			      html += '<optgroup label="'+lines[i].group+'">';
			      optgroup_open = true;
			      old_group=lines[i].group;
			    }
				html += '<option value="'+lines[i].value+'">';
  				html += lines[i].entry +'</option>';
			}
		}
	    if (optgroup_open) {
	    	html += '</optgroup>';
	    }
	    html += '</select>';
		div.innerHTML = html;
		if(selectedLine != ''){
			list = attach(listId);
			list.value = selectedLine;
		}
		
	}



}
