// dynamic dropdown - global variables...
var first_option_index = 2;		// first option (0 is 'choose a ...' 1 is a dashed line)

// object constructors /////////////////////////////////////////////////
function dyn_dropdown(form_name, lyr){
	this.form = form_name;
	this.lyr = lyr;
	this.form_reference = form_reference;
	this.brands = new Array();
	this.add_brand = add_brand;
	this.load_brands = load_brands;
	this.change_brand = change_brand;
	this.load_choices = load_choices;
	this.clear_choices = clear_choices;
}

function form_reference(){
	var form_obj = null;
	if (isNav4){
		if (this.lyr) form_obj = eval("document." + this.lyr + ".document." + this.form);
		else form_obj = eval("document." + this.form);
	}
	else form_obj = eval("document." + this.form);			
	return form_obj;
}

function add_brand(name){
	this.brands[this.brands.length] = new brand(name);
}		

function load_brands(){
	var i = first_option_index;
	var form_obj = this.form_reference();
	for (var j = 0; j < this.brands.length; j++){
		form_obj.brand.options[i] = new Option(this.brands[j].name, this.brands[j].name);
		i++;
	}
}

function change_brand(){
	var form_obj = this.form_reference();
	this.clear_choices();
	this.load_choices(form_obj.brand.selectedIndex);      
}		

function clear_choices(){
	var form_obj = this.form_reference();
	total_choices = form_obj.elements[1].length;
	for (var i = first_option_index; i < total_choices; i++){	
		form_obj.elements[1].options[i] = new Option('','');
	}
	form_obj.elements[1].options.length = 2;
}

function load_choices(brand_index){
	var form_obj = this.form_reference();
	if (brand_index < first_option_index)	form_obj.brand.selectedIndex = 0;
	else {
		var selected_brand = eval(this.brands[brand_index-first_option_index]);
		var i = first_option_index;
		for (var j = 0; j < selected_brand.choices.length; j++){
			form_obj.choice.options[i] = new Option(selected_brand.choices[j].name, selected_brand.choices[j].id);
			i++;
		}
	}
}		

function brand(name){
	this.name = name;			// assign brand name
	this.choices = new Array();		// create an array of choices
	this.add_choice = add_choice;
	return this;
}		

function choice(name, id){
	this.name = name;
	this.id = id;
	return this;
}

function add_choice(name, id){
	this.choices[this.choices.length] = new choice(name, id);
}
