function ec_set_table(pubID){
	var values = get_cookie('show');
	var pubInCookie = get_cookie('bts_ec');
	//start with a clean plate
	ec_collapse_all();
	
	//if returning to the same table, expand all previously expanded tables
	if(pubID==pubInCookie){
		if(values){
			document.cookie = 'show=' + values + '; path=/';
		}
		expand_previous();
	}
	else{
		document.cookie = 'bts_ec=' + pubID + '; path=/';
	}

	return false;
}

function ec_find_text(){
	var browser, range, found, cont;

	ec_expand_all();

	browser = navigator.appName;
	
	if(browser=="Microsoft Internet Explorer"){
		do{
			cont = find_ie();
		}while(cont);
/*
		range = document.body.createTextRange();
		found = range.findText(keyword,1,0);
	
		if(keyword!=''){
			if(!found){
				alert('Unable to find the text \'' + keyword + '\'');
			}
			while(found){
				range.select();
				range.collapse(false);
				found = range.findText(keyword,1,0);
				//alert user that there are more matches
				if(found){
					cont = confirm('Click OK to keep searching');
					if(!cont){
						break;
					}
				}
				else{
					cont = confirm('End of Search.\nClick OK to start a new search.');
					if(cont){
						this.location.href = '#toc_searchbox';	
					}
				}
			}
		}
		else{
			alert("Please input text to find.");
		}
*/
	}
	else{ //Netscape
		do{
			this.location.href = '#Skip_to_content'
			cont = find_nn();
		}while(cont);
	}

	return false;
}

//find text function for ie. returns true if user wants new search
//private
function find_ie(){
	var range, keyword, found, cont;
	var result=false;

	range = document.body.createTextRange();

	do{
		keyword = prompt('Please enter query','')
	}while(keyword=='');

	found = range.findText(keyword,1,0);

	if(found){
		do{
			range.select();
			range.collapse(false);
			if(range.findText(keyword,1,0)){
				cont = confirm('Keep searching?');
			}
			else{
				result = confirm('End of Search.\n<b>Start a new search?</b>');
				break;
			}
		}while(cont)
		
	}
	else{
		if(keyword!=null){
			alert('Unable to find any instances of \'' + keyword + '.\'');
		}
	}

	return result;
}

//netscape find text function. returns true if user wants new search
//private function
function find_nn(){
	var keyword, found, cont;
	var result=false;
	
	do{
		keyword = prompt('Please enter query','')
	}while(keyword=='');
		
	found =	window.find(keyword);
	if(found){
		cont = confirm('Keep searching?');
		while(cont){
			if(window.find(keyword)){
				cont = confirm('Keep searching?');
			}
			else{
				result = confirm('Unable to find anymore instances of \'' + keyword + '.\'\nStart a new search?');
				break;
			}
		}
	}
	else{
		if(keyword!=null){
			alert('Unable to find any instances of \'' + keyword + '.\'');
		}
	}
	
	return result;
}

//expand all ec rows
function ec_expand_all(){
	var rows, i, toCollapse, IDtoCollapse, underscore, cleanID;
	
	//check for javascript support
	if(document.getElementsByTagName){
		rows = document.getElementsByTagName('tr');

		//processing rows
		for(i=0;i<rows.length;i++){
			toCollapse = rows[i];
			IDtoCollapse = toCollapse.getAttribute('id');
			//expand if ec row
			if(IDtoCollapse){
				if(IDtoCollapse.match(/ec.+/)){
					toCollapse.style.display = ''; //collapse
					//get clean id
					underscore = IDtoCollapse.lastIndexOf('_');
					cleanID = IDtoCollapse.slice(0,underscore);
					//display plus sign
					switch_on_minus(cleanID);
					add_cookie(cleanID);
				}
			}
		}
	}
	return false;

}

//collapse all ec rows
function ec_collapse_all(){
	var rows, i, toCollapse, IDtoCollapse, underscore, cleanID;

	//check for javascript support
	if(document.getElementsByTagName){
		rows = document.getElementsByTagName('tr');

		//processing rows
		for(i=0;i<rows.length;i++){
			toCollapse = rows[i];
			IDtoCollapse = toCollapse.getAttribute('id');
			//collapse if ec row
			if(IDtoCollapse){
				if(IDtoCollapse.match(/ec.+/)){
					toCollapse.style.display = 'none'; //collapse
					//get clean id
					underscore = IDtoCollapse.lastIndexOf('_');
					cleanID = IDtoCollapse.slice(0,underscore);
					//display plus sign
					switch_on_plus(cleanID);
				}
			}
		}
		
		clean_cookie();
	}
	return false;
}

//expand if collapsed and vice versa
function ec_toggle(id,num_of_rows){
	var i, end, row, toToggle;
	end = num_of_rows + 1;
	
	//check for javascript support
	if(document.getElementById){
		//processing rows
		for(i=1;i<end;i++){
			row = id + '_' + i.toString();
			toToggle = document.getElementById(row);
			//show if hidden and vice versa
			if(toToggle.style.display=='none'){ //hidden, so show
				toToggle.style.display = '';
				add_cookie(id);
				switch_on_minus(id);
			}
			else{ //shown, so hide
				toToggle.style.display = 'none';
				remove_cookie(id);
				switch_on_plus(id);
			}
		}
	}
	return false;
}

//switch on plus sign. switch off minus sign
//only to be called internally
function switch_on_plus(id){
	document.getElementById(id+'_plus').style.display = '';
	document.getElementById(id+'_min').style.display = 'none';
	return false;
}

//switch on minus sign, switch off plus sign
//only to be called internally
function switch_on_minus(id){
	document.getElementById(id+'_min').style.display = '';
	document.getElementById(id+'_plus').style.display = 'none';
	return false;
}

//add 'id' to cookie
//private
function add_cookie(id){
	var i, values;
	values = get_cookie('show');
	//check if cookie 'show' exists
	if(values){
		//check if 'id' is not in 'show' already
		if(!(values.match(id))){
			//first entry
			if(values.match('ec')){
				values = values + ',' + id; //add row name to list of values
     		document.cookie = 'show=' + values + '; path=/';
				}
			//end entry
			else{
     			document.cookie = 'show=' + id + '; path=/'; //add row name
			}
		}
	}
	//no 'show', add new cookie
	else{
     	document.cookie = 'show=' + id + '; path=/'; //add row name
	}
	
	return false;
}

//remove 'id' from cookie
//private
function remove_cookie(id){
	var i, values, rows;
	values = get_cookie('show');
	//check if cookie 'show' exists
	if(values){ 
		//check if 'id' is in 'show'
		if(values.match(id)){
			rows = values.split(',');
			values = "";
			//remove row 'id'
			for(i=0;i<rows.length;i++){

				//skip row 'id'
				if(!rows[i].match(id)){
					values = values + "," + rows[i];
				}
			}

			//add values back to cookie
			if(values.charAt(0)==','){
				//remove trailing comma
				values = values.slice(1,values.length);
				document.cookie = 'show=' + values + '; path=/'; //add row 			
			}
			//remove cookie if last one
			else{
				document.cookie = 'show=; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT';
			}
		}
	}
	return false;
}

//return the values of the cookie with the name
//private
function get_cookie(name){
   var i, temp, values, all_cookies;
   
   values = null;
   
   if(document.cookie){
     all_cookies = document.cookie.split('; ');
   
     for(i=0;i<all_cookies.length;i++){
       temp = all_cookies[i].split('=');
       if(temp[0] == name){
         values = temp[1];
       }
     }
   }
   
   return values;
}

//remove 'show' cookie
//private
function clean_cookie(){
	document.cookie = 'show=; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT';
	return false;
}

//use 'show' cookie to set table
//private
function expand_previous(){
	var values, toExpand, i;
	values = get_cookie('show');
	
	//check if cookie 'show' exists
	if(values){
		toExpand = values.split(',');
		
		for(i=0;i<toExpand.length;i++){
			//expand rows
			ec_toggle(toExpand[i], get_num_of_rows(toExpand[i]));
		}
		
	}

	return false;
}

//get the number of rows related with 'id'
//private
function get_num_of_rows(id){
	var result, rows, i, row, temp, fullrow;
	result = 0;
	//get all rows
	rows = document.getElementsByTagName('tr');
	
	//look for the row with id 'id'
	for(i=0;i<rows.length;i++){
		fullrow = rows[i].getAttribute('id');
		if(fullrow){
			row = (rows[i].getAttribute('id')).split('_');
			//if row id is 'id' count the rows
			if(row[0].match(id)){
				temp = parseInt(row[1]);
				if(result<temp){
					result = temp;
				}
			}
		}
	}

	return result;
}