// set up vars used in HTML code
// leave at top, so they are registered with browser early on!
arr_mis = new Array;
arr_mmis = new Array;
s_arr_man_mask = new Array;
s_str_sa = 'MG';
int_hl_timeout_id = 0;


// set colour function - useful for setTimeout calls (passing objects)
function set_colour(obj_item, str_colour) {
	if(obj_item) {
		obj_item.style.color = str_colour;
	}
}

// set colour function - useful for setTimeout calls (passing objects)
function set_style(obj_item, str_style) {
	if(obj_item) {
		// browser check
		if(document.all) {
			str_att = 'className';
		} else {
			str_att = 'class';
		}
		obj_item.setAttribute(str_att, str_style);
	}
}

// quick-name functions
function hm(arr_masks, str_sa) {
	if(str_sa.substr(1, 2) == 'G') {
		str_alt_sa = str_sa.substr(0, 1) + 'M';
	} else {
		str_alt_sa = str_sa.substr(0, 1) + 'G';
	}
	xs_highlight_menu(new Array, str_alt_sa);
	xs_highlight_menu(arr_masks, str_sa);
}
function rhm() {
	revert_highlights(s_arr_man_mask, s_str_sa);
}

// rhm with values
function rhmv(arr_man_mask, str_sa) {
	s_arr_man_mask = arr_man_mask;
	s_str_sa = str_sa;
	rhm();
}


// revert highlights (delayed)
function revert_highlights(arr_man_mask, str_sa) {
	int_hl_timeout_id = setTimeout(function () {revert_now(arr_man_mask, str_sa);}, 250);
}

// revert now!
function revert_now(arr_man_mask, str_sa) {
	// clear Manufacturer highlights
	xs_highlight_menu(new Array, 'MM');
	xs_highlight_menu(new Array, 'WM');
	xs_highlight_menu(new Array, 'BM');
	xs_highlight_menu(new Array, 'GM');
	xs_highlight_menu(new Array, 'LM');
	xs_highlight_menu(new Array, 'XM');
	xs_highlight_menu(new Array, 'YM');
	
	// clear Group highlights
	xs_highlight_menu(new Array, 'WG');
	xs_highlight_menu(new Array, 'MG');
	xs_highlight_menu(new Array, 'BG');
	xs_highlight_menu(new Array, 'GG');
	xs_highlight_menu(new Array, 'LG');
	xs_highlight_menu(new Array, 'XG');
	xs_highlight_menu(new Array, 'YG');
	
	// Highlight groups
	xs_highlight_menu(arr_man_mask, str_sa, 'style');

	// set Manufactuer
	obj_cur_item = document.getElementsByName('cur_item');
	obj_cur_item = obj_cur_item[0];
	if (obj_cur_item && obj_cur_item.style.color!='#ff7373') {
		set_colour(obj_cur_item, 'black');
	}
	set_style(obj_cur_item, 'current_menu_item');

}

// highlight menu (eXtenSible - XS)
function xs_highlight_menu(arr_masks, str_sa, str_action) {

	// halt any delayed actions
	clearTimeout(int_hl_timeout_id);
	
	// first, set the current item to 'no colour' (allows a:hover highlights)
	obj_cur_item = document.getElementsByName('cur_item');
	obj_cur_item = obj_cur_item[0];
	if (obj_cur_item && obj_cur_item.style.color!='#ff7373') {
		set_colour(obj_cur_item, '');
	}
	
	// get the array of menu items to loop
	arr_mis = arr_mmis[str_sa];
	int_arr_size = arr_mis.length - 1;
	
	// check each menu item
	// for(n=1; n<=2; n++) {
	for(n=1; n<=int_arr_size; n++) {

		obj_target = document.getElementById(str_sa+'ida'+n);
		
		if (obj_target && obj_target.style.color=='#ff7373') {
			continue;
		}

		// check if a match
		if(xs_check_item(arr_masks, arr_mis[n])) {
			set_colour(obj_target, 'black');
			if(str_action=='style') {
				set_style(obj_target, 'current_menu_item');
			}
		} else {
			set_colour(obj_target, '');
			if(str_action=='style') { 
				set_style(obj_target, 'normal');
			}
		}
	}
}

// multiple 32-bit bitmask check (eXtenSible - XS)
function xs_check_item(arr_masks, int_check) {
	// get number of required masks
	int_masks = required_masks(int_check);

	// set up masks
	arr_checks = zeroed_array(int_masks);

	// set check val
	arr_checks = set_bin_mask(arr_checks, int_check);
	
	// now loop and check the masks against the checks
	return check(arr_masks, arr_checks);
}

function check(arr_masks, arr_checks) {
	int_anum = arr_checks.length;
	for(bn=1; bn<=int_anum; bn++) {
		if(arr_checks[bn - 1] & arr_masks[bn - 1]) {
			return true;
		}
	}
	return false;
}

function required_masks(int_check) {
	int_masks = Math.ceil(int_check / 32);
	return int_masks;
}

function zeroed_array(int_masks) {
	arr_checks = new Array(int_masks);
	// set to 0
	for(an=1; an<=int_masks; an++) {
		arr_checks[an-1] = 0;
	}
	return arr_checks;
}

function set_bin_mask(arr_checks, int_check) {
	// last element of the mask
	int_num = arr_checks.length - 1;
	arr_checks[int_num] = Math.pow(2, (int_check - (32 * int_num) - 1));
	return arr_checks;
}


