//Scripts by Huaming Chen

function ajax_new (html, id)
{
	document.getElementById(id).innerHTML = "";
	ajax_text (html, id);
}

function ajax_proj (html, id)
{
	if (html.indexOf("?")!=-1)
		html+= "&Project="+Project;
	else html+="?Project="+Project; 
	ajax_new(html, div);
}

function ajax_form (html, myform, id)
{
	var params="";
	var x = document.getElementById(myform);

	for (var i=0;i<x.length;i++)
	{
		if (x.elements[i].type == "radio")
		{
			if (x.elements[i].checked) 
				params = params + "&" + x.elements[i].name.toString() + "=" + encodeURI(x.elements[i].value) ;
		}
		else if (x.elements[i].type == "checkbox")
		{
			if (x.elements[i].checked)
				params = params + "&" + x.elements[i].name.toString() + "=" + encodeURI(x.elements[i].value) ;
		}
		else params = params + "&" + x.elements[i].name.toString() + "=" + encodeURI(x.elements[i].value) ;
	}

	if (html.indexOf("?")==-1) html+="?";
	html += params;

	ajax_new (html, id);
}


function ajax_text (html, id)
{
	var http;

	try
	{
		// Firefox, Opera 8.0+, Safari
		http=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			http=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				http=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	http.onreadystatechange=function()
	{
		if(http.readyState==4)
		{
			document.getElementById(id).innerHTML += http.responseText;
		}
	}
	http.open('GET', html, true);
	http.send (null);
}

function updatediv ()
{
	Chromosome = document.geView.Chromosome.value;
	Position = document.geView.Position.value;
	Zoom = document.geView.Zoom.value;
	Code = document.geView.Code.value;
	Width = document.geView.Width.value;
	View = document.geView.View.value;
	//Coords = document.geView.Coords.value;

	//document.getElementById('adminTable').style.display='none';
	//document.getElementById('innerRightGEBrowser').style.display='';

	var div;


	for (var i=0; i<div_a.length; i++)
	{
		div = document.getElementById(div_a[i]);
		div.parentNode.removeChild(div);
	}
	div_a.splice(0,div_a.length);

	var rano=Math.floor(Math.random()*10001)

	var vhtml = "gbView.php?Strain="+Reference+"&Chromosome="+Chromosome+"&Position="+Position;
	vhtml += "&Zoom="+Zoom+"&Code="+Code+"&Width="+Width+"&Version="+Version+"&Output=Map&Random="+rano;
	ajax_new (vhtml, "itrackmap");


	var ihtml = "<span style='cursor:pointer' onClick='{ document.geView.List.value=\"\"; updatediv(); return false;}'><img src=Images/Atg1001_Close.jpg width=16></span> ";
	ihtml += "<img id='AtG1001_MAP_IMG' src=\"gbView.php?Strain="+Reference+"&Chromosome="+Chromosome+"&Position="+Position;
	ihtml += "&Zoom="+Zoom+"&Code="+Code+"&Width="+Width+"&Version="+Version+"&Output=Image\" ";
	ihtml += "width="+Width+" name='AtG1001_MAP_IMG' usemap=#AtG1001_MAP_TD_"+rano+" LINK=aaaadd VLINK=aaaadd>";

	document.getElementById("itrack").innerHTML = ihtml;


	loaddiv ();
}
						    
function deldiv (divid, strain)
{
	var div = document.getElementById(divid);
	div.parentNode.removeChild(div);

	var list_s = document.geView.List.value;
	var list_a = new Array();
	list_a=list_s.split(" ");

	for (var i=list_a.length-1; i>=0; i--)
	{
		if (list_a[i]==" " || list_a[i]=="" || list_a[i]=="\n") list_a.splice(i,1);
	}

	for (i=0; i<list_a.length; i++)
	{
		if (list_a[i]==strain && div_a[i]==divid)
		{
			list_a.splice(i,1);
			div_a.splice(i,1);
		}
	}
	document.geView.List.value = list_a.join(" ");
}

function adddiv (strain, add)
{
	var viewlink = "gbView.php?Strain="+strain+"\&Chromosome="+Chromosome+"&Position="+Position+"&Zoom="+Zoom+"&Code="+Code;

	viewlink += "&Width="+Width;
	//if (Coords!="") viewlink += "&Coords="+Coords;
	if (View!="Grahpy") viewlink += "&View="+View;
	//if (document.geOptions.SNPDisplay[1].checked==true) viewlink +="&SNPDisplay=CDS";

	var divid = document.getElementById("num").innerHTML;
	if (View=="Text")
	{
		var html = "<span style='cursor:pointer' onClick='deldiv(\""+divid+"\",\""+strain+"\")'><img src=Images/Atg1001_Close_Round.png></span>";
	}
	else
	{
		var html = "<div ><nobr><span style='cursor:pointer' onClick='deldiv(\""+divid+"\",\""+strain+"\")'><img src=Images/Atg1001_Close_Round.png></span> <img src=\""+viewlink+"\"></nobr></div>\n";
	}

	document.getElementById("num").innerHTML = parseInt(divid)+1;
	var div = document.createElement("DIV");
		div.id = divid;
	document.getElementById("itrack2").appendChild(div);
	document.getElementById(divid).innerHTML = html;
	if (add==1)
	{
		if (document.geView.List.value == "") document.geView.List.value += strain;
		else document.geView.List.value += " "+strain;
	}
	if (View=="Text") ajax_text (viewlink, divid);

	div_a.push(divid);
}

function loaddiv ()
{
	var list_s = document.geView.List.value;
	var list_a = new Array();
	list_a=list_s.split(" ");

	for (var i=list_a.length-1; i>=0; i--)
	{
		if (list_a[i]==" " || list_a[i]=="" || list_a[i]=="\n") list_a.splice(i,1);
	}

	for (i=0; i<list_a.length; i++)
	{
		if (list_a[i]!=" " && list_a[i]!="" && list_a[i]!="\n") adddiv (list_a[i], 0);
	}
	document.geView.List.value = list_a.join(" ");
}

function addBookmarks ()
{
	var html = "./gebrowser_Bookmarks.php?Bookmarks=Add&Value=";
	html += document.geBook.bName.value+"+";
	html += document.geView.Chromosome.value+"+";
	html += document.geView.Position.value+"+";
	//html += document.geBook.Coords.value+"+";
	html += document.geView.List.value;

	ajax_new (html, "bookmarkContent");
}

function autoBookmarks (gn, chr, pos, cor)
{
	if (document.geOptions.AutoBookmarks.checked==true)
	{
		var html = "./gebrowser_Bookmarks.php?Bookmarks=Add&Value=";
		html += gn+"+"+chr+"+"+pos+"+"+cor+"+"+document.geView.List.value;
		ajax_new (html, "bookmarkContent");
	}
}

function goBookmarks (gn, chr, pos, cor, lst)
{
	document.geView.Chromosome.value=chr;
	document.geView.Position.value=pos;
	document.geView.Coords.value=cor;
	document.geView.List.value=lst;

	updatediv();
}

function goSearchs (gn, chr, pos, ln, cor)
{
	document.geView.Chromosome.value=chr;
	document.geView.Position.value=pos;
	document.geView.Coords.value=cor;

	var zi = ln/document.geView.Width.value;
	for (var j=0;j<6;j++)
	{
		if (zi<=zoom_a[j]) break;
	}
	document.geView.Zoom.value=zoom_a[j];
	if (document.geOptions.LocusDisplay.checked==true)
	{
		if (zi!=zoom_a[j])
		{
			document.geView.Zoom.value=1;
			document.geView.Width.value = ln;
		}
	}

	document.geBook.bName.value=gn;
	//document.geBook.Coords.value=cor;

	updatediv();
	autoBookmarks (gn, chr, pos, cor);
}


function displayMenu(object)
{
	if (document.getElementById('systemContent')) document.getElementById('systemContent').style.display='none';
	if (document.getElementById('searchContent')) document.getElementById('searchContent').style.display='none';
	if (document.getElementById('maniContent')) document.getElementById('maniContent').style.display='none';
	if (document.getElementById('trackContent')) document.getElementById('trackContent').style.display='none';
	if (document.getElementById('bookmarkContent')) document.getElementById('bookmarkContent').style.display='none';
	if (document.getElementById('optionContent')) document.getElementById('optionContent').style.display='none';
	if (document.getElementById('creditContent')) document.getElementById('creditContent').style.display='none';
	if (document.getElementById('tutorContent')) document.getElementById('tutorContent').style.display='none';
	document.getElementById(object).style.display='';
}

