/*
DHTML List Library
Copyright 2004 David Dean
USE WITHOUT WRITTEN PERMISSION IS STRICTLY FORBIDDEN

inline = IE
table-row = Mozilla
*/
function ObjectVis(id, vis){
	if(document.getElementById(id)){
		document.getElementById(id).style.display = vis;
	}
}
function FillOptions(itm,array,resetAll,firstItem){
	var ops = document.getElementById(itm+'_select');
	var l = ops.options.length;
	if(resetAll){
		//var l = ops.length;
		for (m=ops.length-1;m>=0;m--) ops.options[m]=null;	
		l = 0;
		if(firstItem){
			ops.options[0] = new Option(firstItem,null);
			ops.options[1] = new Option('', null);
			l = 2;
		}
	} 		
	if(array){
		for(i=0; i<array.length; i++){
			ops.options[l] = new Option(array[i][1], array[i][0]);
			l++;
		}
		ObjectVis(itm+'_select', 'block');
	} else {
		ObjectVis(itm+'_select', 'none');
	}
	//ops.selectedIndex = 0;
}


function SetDefault(id, ops, ai){
	//alert(id);
	if (DEFAULTS[id]){
		var _ids = DEFAULTS[id];
		if(_ids[ai])
			document.getElementById(ops+'_select').options.selectedIndex = _ids[ai];
	}
}

function SetOption(itm,SI,mode){
	var ops = document.getElementById(itm+'_select').options;
	switch(mode){
		case 'value':
			for(i=0; i<ops.length; i++){
				if(ops[i].value == SI) return i;
				ops.selectedIndex = i+1;
			}
			
		break;
	}
}
function InArray(itm, arr){
	//alert(arr.length);
	for(z=0;z<arr.length;z++){
		//alert(arr[z]+'--'+itm);
		if(arr[z] == itm) return z;
	}
	return 'false';
}
function replaceImage(img, src){
	obj = document.getElementById(img);
	obj.src = src;
}
function collapseAll(id){
	obj = document.getElementById('tbl_'+id);
	if (obj.style.display == 'none'){
		obj.style.display = 'block';
		replaceImage('nav_'+id, '../images/nav/collapse_ff.gif');
	} else if(obj.style.display == '' || obj.style.display =='block'){
		obj.style.display = 'none';
		replaceImage('nav_'+id, '../images/nav/expand_ff.gif');
	}
}
	
	//var num = CATEGORIES[id].length;
	//for(i=1;i<=num;i++){
		//collapse(CATEGORIES[id][i][0])
	//}


function collapse(id){
	var num = SUB_CATEGORIES[id].length;
	//alert(COLLAPSED);
	if (InArray('c'+id, COLLAPSED) == 'false'){
		for(i=1;i<=num;i++){
			ObjectVis(id+'_'+i, 'none');
		}
		COLLAPSED.push('c'+id);
		replaceImage('nav_'+id, root+'images/nav/expand_sm_cc.gif');
	} else{
		for(i=1;i<=num;i++){
			if (_browser == 'mz'){
				ObjectVis(id+'_'+i, 'table-row');
			} else {
				ObjectVis(id+'_'+i, 'inline');
			}
		}
		aid = InArray('c'+id, COLLAPSED);
		COLLAPSED.splice(aid, 1);
		replaceImage('nav_'+id, root+'images/nav/collapse_sm_cc.gif');
	}
}
function checkAllSub(id, chk, frm){
	if (!frm) var frm = 'listings';
	setActiveForm(frm+'_form');
	if (!chk){
		chk = (ACTIVE[id].allchk) ? false : true;
	}
	//var chk = true;
	var num = ACTIVE[id].length;
	var ids = ACTIVE[id];
	//alert(ids);
	for(i=0;i<num;i++){
		activeForm.elements['cbx_'+ids[i]].checked = chk;
	}
	ACTIVE[id].allchk = chk;
}

function hideCB(id, frm){
	var chk = (ACTIVE[id].allchk) ? false : true;
	var disp = chk ? 'none' : 'block';
	/*
	setActiveForm(frm+'_form');
	
	var num = ACTIVE[id].length;
	var ids = ACTIVE[id];
	for(i=0;i<num;i++){
		//activeForm.elements['cbx_'+ids[i]].style.display = 'none';
		activeForm.elements['cbx_'+ids[i]].readonly = chk;
	}
	*/
	ObjectVis(id+'_cb_div', disp);
	ACTIVE[id].allchk = chk;
}

function checkAll(id, frm){
	//setActiveForm(itm+'_form');
	if (!frm) var frm = 'listings';
	setActiveForm(frm+'_form');
	//alert(ACTIVE);
	var chk = (ACTIVE[id].allchk) ? false : true;
	var num = ACTIVE[id].length;
	var ids = ACTIVE[id];
	for(z=0;z<num;z++){
		checkAllSub(ids[z], chk);
	}
	ACTIVE[id].allchk = chk;
}

function ResetPanel(panel, id){
	switch (panel){
		case 'analysis':
			document.getElementById('type_id_select').selectedIndex = SetOption('type_id', id, 'value');
			FillOptions('id', CATEGORIES[id], true, false);
			SetDefault(id, 'id', 0);
			var sid = document.getElementById('id_select').value;
			FillOptions('sub_id', SUB_CATEGORIES[sid], true, false);
			SetDefault(id, 'sub_id', 1);
		break;
		case 'accred':
			document.getElementById('lab_id_select').selectedIndex = SetOption('lab_id', id, 'value');
			var sid = document.getElementById('lab_id_select').value;
			FillOptions('branch_id', BRANCHES[sid], true, false);
		break;
	}
}


function inArray(array, id){
	if (array.length > 0){
		for (i=0; i<array.length;i++){
			if (array[i][0] == id){
				return(i);
			}
		}
	}
	return false;
}
	
function ResetItem(itm){
	var array = eval(itm.toUpperCase());
	var l = array.length;
	array.splice(0,l);
	document.getElementById(itm+'_html').innerHTML = '';
	document.getElementById(itm+'_ids').value = '';
	document.getElementById(itm+'_actions').innerHTML = '';
}	
function AddMulti(from, to){
	var a_from = eval(from.toUpperCase());
	var a_to = eval(to.toUpperCase());
	var l = a_to.length;
	var i=0;
	for(i=0; i<a_from.length; i++){
		var aid = inArray(a_to, a_from[i][0]);
		if (aid === false){
			a_to.push(a_from[i]);
		}
	}
}

function AddOptions(from, to, css){
	var ops = document.getElementById(from+'_select').options;
	var a_to = eval(to.toUpperCase());
	var i=0;
	var txt = '';
	var val = '';
	for(i=0; i<ops.length; i++){
		if(ops[i].value != "null"){
			val = ops[i].value;
			var aid = inArray(a_to, val);
			if (aid === false){
				switch (css){
					case 'bold':
						txt = '<b>'+ops[i].text+'</b>';
					break;
					default:
						txt = ops[i].text;
				}
				a_to.push(new Array(val, txt));
			}
		}
	}
}
	
function MultiFill(source, to, is_options, css, resetArr){
	var sources = source.split('|');
	for(s=0; s<sources.length; s++){
		if(is_options){
			AddOptions(sources[s], to, css);
		} else {
			AddMulti(sources[s], to);
		}
		if (resetArr){
			eval(sources[s].toUpperCase()).splice(0);
		}
	}
	//alert(eval(to.toUpperCase()));
	MakeList(to, 'remove');
}
				

function MoveItem(from, to, id){
}

function RemoveItem(span,itm){
	var array = eval(span.toUpperCase());
	aid = inArray(array, itm);
	if (aid !== false){
		array.splice(aid, 1);
		MakeList(span, 'remove');
	}	
}

function AddItem(obj, span, type){
	var ops = obj.options;
	var si = obj.selectedIndex;
	var array = eval(span.toUpperCase());
	var name = ops[si].text;	
	switch (type){
		case 'group':
			name = '<b>'+name+'</b>';
		break;	
	}
	var itm = ops[si].value;
	if (si != 0){
		if (itm != 'null'){
			for (i=0; i<array.length; i++){
				if (array[i][0] == itm){
					obj.selectedIndex = 0;
					return false;
				}
			}
			array.push(new Array(itm,name));
			MakeList(span, 'remove');	
		}
	}
	obj.selectedIndex = 0;
	return;
}

function CopyItem(from, to, itm){
	a_from = eval(from.toUpperCase());
	a_to = eval(to.toUpperCase());
	var fid = inArray(a_from, itm);
	if (fid !== false){
		var tid = inArray(a_to, itm);
		if(tid === false){
			a_to.push(a_from[fid]);
			MakeList(to, 'remove');
		}
	}		
}
function TextList(itm){
	var items = eval(itm.toUpperCase());
	var html = '';
	if (items.length > 0){
		html += items[0][1];
		for (i=1; i<items.length; i++){
			html += delim+items[i][1];
		}
	}
	return html;
}
function MakeList(itm, action, target){
	var span = itm+'_html';
	var arr = itm.toUpperCase();
	if (target) t_arr = target.toUpperCase();
	var items = eval(arr);
	var html = '';
	var values = new Array();
	if (items.length > 0){
		switch(action){
			case 'copy':
				html += '<a href="javascript:CopyItem(\''+itm+'\',\''+target+'\',\''+items[0][0]+'\')">'+items[0][1]+'</a>';
				for(i=1; i<items.length; i++){
					html += delim+'<a href="javascript:CopyItem(\''+itm+'\',\''+target+'\',\''+items[i][0]+'\')">'+items[i][1]+'</a>';
				}
			break;
			case 'remove':
				html += '<a href="javascript:RemoveItem(\''+itm+'\',\''+items[0][0]+'\')">'+items[0][1]+'</a>';
				values.push(items[0][0]);
				for(i=1; i<items.length; i++){
					html += delim+'<a href="javascript:RemoveItem(\''+itm+'\',\''+items[i][0]+'\')">'+items[i][1]+'</a>';
					values.push(items[i][0]);
				}		
				document.getElementById(itm+'_ids').value = values.join(form_delim);		
				if (document.getElementById(itm+'_actions') != null){
					document.getElementById(itm+'_actions').innerHTML = '[ <a href="javascript:ResetItem(\''+itm+'\')">clear</a> ]';
				}
			break;
			case 'add':
				html += '<a href="javascript:AddItem(\''+target+'\',\'\''+items[0][0]+'\'\')">'+items[0][1]+'</a>';
				for(i=1; i<items.length; i++){
					html += delim+'<a href="javascript:AddItem(\''+target+'\',\''+items[i][0]+'\')">'+items[i][1]+'</a>';
				}
			break;
		}				
	} else if (action != 'copy'){
		document.getElementById(itm+'_actions').innerHTML = '';		
	}
	document.getElementById(span).innerHTML = html;
}	
