// JavaScript Document
function getChr(tax, val) {
	var warning = $('message_text');
	var tpf_list = $('tpf_list');
	if(tax == "none"){
	//clear menu and print warning text if no taxid is specified
		while(warning.firstChild){
			warning.removeChild(warning.firstChild);
		}
	warning.appendChild(document.createTextNode("Please select an organism to continue"));
			return false;
		}
	while(warning.firstChild){
		warning.removeChild(warning.firstChild);
	}
	//clear tpf menu
	clear_list(tpf_list);
	//if a real taxid is specified, but the chromosomes that go with it
	var url = "/projects/genome/assembly/overlap/getchr.cgi?taxid=";
	var chr_data = new RemoteDataProvider(url);
	chr_data.onSuccess = function(obj){populateChrMenu(obj, val);};
	chr_data.Get(tax);
	return true;
}

function clear_list(list){
	var j;
	//alert("clearing list" + list.length);
	for(j=list.length-1; j>0; j--){
		list.remove(j);
	}
}

function populateChrMenu(obj, val){
	var chr_label = $('chr_label');
	var chr_select = $('chr');
	var chr_list = new Array;
	chr_list = obj.responseText.split(',');
	clear_list(chr_select);
	//populate the chr menu
	var option = document.createElement('option');
	option.text = "choose one";
	option.value = "none";
	chr_select.options[0] = new Option(option.text, option.value);
	for(i=0; i<chr_list.length; i++){
		var text = "chr" + chr_list[i];
		var value = chr_list[i];
		var isSelected = (value == val);
		//new Option([text], [value], [defaultSelected], [selected])
		chr_select.options[i+1] = new Option(text, value, false, isSelected);
	}
	utils.removeClass(chr_label, 'hide');
	utils.removeClass(chr_select, 'hide');
	return true;
}

function getTpf(chr, val){
	var taxid = $('org').value;
	var warning = $('message_text');
	var tpf_list = $('tpf_list');
	clear_list(tpf_list);
	if(taxid == "none"){
		//clear add a warning message if no taxid
		while(warning.firstChild){
			warning.removeChild(warning.firstChild);
		}
		warning.appendChild(document.createTextNode("Please select an organism to continue"));
		return false;
	}
	while(warning.firstChild){
		warning.removeChild(warning.firstChild);
	}
	//get chr data
	var url = "/projects/genome/assembly/overlap/gettpf.cgi?taxid=" + taxid + "&chr="
	var tpf_data = new RemoteDataProvider(url);
	tpf_data.onSuccess = function(obj) {populateTPFMenu(obj, val); };
	tpf_data.Get(chr);
}
	
function populateTPFMenu(obj, val){
	var tpf_label = $('tpf_list_label');
	var tpf_list = $('tpf_list');
	var resp = new Array;
	resp = obj.responseText.split(',');
	var option = document.createElement('option');
	option.text = "Choose One";
	option.value = "none ";
	tpf_list.options[0] = new Option(option.text, option.value);
	for(i=0; i<resp.length; i++){
		var text = resp[i];
		var value = resp[i];
		var isSelected = (value == val);
		tpf_list.options[i+1] = new Option(text, value, false, isSelected);
	}
	utils.removeClass(tpf_label, 'hide');
	utils.removeClass(tpf_list, 'hide');
}
	
function goToTpfView(){
	var chr=$('chr').value;
	var taxid=$('org').value;
	var assembly=$('tpf_list').value;
	var page=$('page_view').value;
	if(taxid == "none"){
		alert("Please select an organism");
		return false;
	}
	if(assembly == "none"){
		alert("Please select a TPF to view");
		return false;
	}
	var url = "/projects/genome/assembly/overlap/tpfview.cgi?chr=" + chr + "&taxid=" + taxid + "&assembly=" + assembly + "&page=" + page;
	window.location.href = url;
}

function setMenus (){
	var loc=location.search.substring(1, location.search.length);
	var params = loc.split("&");
	var i;
	var param_name;
	var param_value;
	var tax;
	var chrom;
	var assembly;
	var page;
	for(i=0; i<params.length; i++){
		//need to explictly get the param values and then deal with them- otherwise, you are dependent on param order- BAD!
		param_name = params[i].substring(0,params[i].indexOf('='));
		if(param_name == "taxid"){
			param_value = params[i].substring(params[i].indexOf('=')+1);
			tax=param_value;
		}
		if(param_name == "chr"){
			param_value = params[i].substring(params[i].indexOf('=')+1);
			chrom=param_value;
		}
		if(param_name == "assembly"){
			param_value = params[i].substring(params[i].indexOf('=')+1);
			assembly = param_value;
		}
		if(param_name == "page"){
			param_value = params[i].substring(params[i].indexOf('=')+1);
			page = param_value;
		}
	}
	var j;
	var tax_list = new Array;
	tax_list=$('org');
	for(j=0; j<tax_list.length; j++){
		if(tax_list.options[j].value == tax){
			tax_list.options[j].selected="selected";
		}
	}
	getChr(tax, chrom);
	getTpf(chrom, assembly);
	var chr_list = new Array;
	chr_list = $('chr');
	var file_list = new Array;
	file_list =$('tpf_list');
	setSelect(chr_list, chrom);
	setSelect(file_list, assembly);
	var page_view=$('page_view');
	setSelect(page_view, page);
}

function setSelect(list, value){
	//alert("set Select to " + value);
	var j;
	for(j=0; j<list.length; j++){
		if(list.options[j].value == value){
			list.options[j].selected="selected";
		}
	}
}	
