/*
$Id: dropupdate.js 1794 2006-02-16 01:56:09Z fesh $
*/
function update_dropdown(src_drop, target_drop, lookup, srcindex, targetindex, targettextindex) {
	if(!src_drop.length) {
		return;
	}
	var src_val = get_select_value(src_drop);
	var target_val = get_select_value(target_drop);
	var j = 1;

	if (src_val != '') {
		clear_select(target_drop);
		for(var i=0;i< lookup.length;i++) {
			if(lookup[i][srcindex] == src_val) {
				target_drop.length = j + 1;
				target_drop.options[j].text = lookup[i][targettextindex];
				target_drop.options[j].value = lookup[i][targetindex];
				j++;
			}
		}

		set_select_value(target_drop, target_val)
	}
	else { // no value selected, rebuild default dropdown so options dont get 'lost'

		clear_select(src_drop);

		for(var i=0;i< lookup.length;i++) {
			 in_array = false;
			 for(var j=1;j<src_drop.length;j++) {
				 if (src_drop[j].value == lookup[i][srcindex]) {
					 in_array = true;
				 }
			 }
			 if (in_array == false) {
				 src_drop.length = j + 1;
				 src_drop.options[j].text = lookup[i][srcindex];
				 src_drop.options[j].value = lookup[i][srcindex];
			 }
		}

		clear_select(target_drop);

		for(var i=0;i< lookup.length;i++) {
			 in_array = false;
			 for(var j=1;j<target_drop.length;j++) {
				 if (target_drop[j].value == lookup[i][targetindex]) {
					 in_array = true;
				 }
			 }
			 if (in_array == false) {
				 target_drop.length = j + 1;
				 target_drop.options[j].text = lookup[i][targettextindex];
				 target_drop.options[j].value = lookup[i][targetindex];
			 }
		}

	}
}

function get_select_value(select) {

	for(var i=0;i<select.length;i++) {
		if(select[i].selected) {
			return(select[i].value);
		}
	}
}

function set_select_value(select, value) {

	for(var i=0;i<select.length;i++) {
		if(select[i].value == value) {
			select[i].selected = true;
			return(true);
		}
	}
	select[0].selected = true;
}

function clear_select(select) {

	for(var i=1;i<select.length;i++) {
		select[i].value= '';
		select[i].text = '';
	}
	select.length = 1;
}

