
var iisIE = false; //global flag;
var ireq;  //global request and XML document objects;


window.onload = init;


function init() {
	var initURL = 'ringo/all.xml';	//input all diseases XML file 'all.xml';
	initLoadXMLDoc(initURL);
}


function initLoadXMLDoc(url) {
    if (window.XMLHttpRequest) { //branch for native XMLHttpRequest object.
		ireq = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { // branch for IE/Windows ActiveX version;
        iisIE = true;
        ireq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if (ireq.overrideMimeType) { //for browser override MimeType;
			ireq.overrideMimeType("text/xml");
        }
    
    ireq.onreadystatechange = initProcessReqChange;
    ireq.open("GET", url, true);
    ireq.send(null);
}


function initProcessReqChange() {
    if (ireq.readyState == 4) {   //only if ireq shows "loaded";
        if (ireq.status == 200) { //only if "OK";
			initShowSummary();
			initAppendToTableV3('geneList','geneInfo');	//show genes Info;
			//initAppendToTableV3('qtlList','rat-qtl');	//show QTLs Info;
			iAppendQtlTabV4('qtlList','ratsym','ratid','rat-qtl');
			iAppendQtlTabV4('qtlList','humansym','humanid','human-qtl');
			initAppendToTableV3('strainList','strainInfo');	//show strains Info;
				
		} else {
			alert("There was a problem retrieving the XML data: \n" +
				ireq.statusText);
        }
    }
}


function initShowSummary() {
	var divGeneSum = document.getElementById('genes-sum');
	var divQtlSum = document.getElementById('qtls-sum');
	var divStrainSum = document.getElementById('strains-sum');
	
	var theName = ' ';	//Neurological Disease;

	var geneCount = new Array();
	var genelist = ireq.responseXML.getElementsByTagName('geneList');
	for (i=0; i<genelist.length; i++){
		var genes = genelist[i].getElementsByTagName('ratid'); //get rat's gene number;
		var getGeneIDs = genes[0].firstChild.nodeValue;
		geneCount.push(getGeneIDs);
	}
	geneCount.sort();
	var thisGeneLength = geneCount.length - 1;

	for (r=0; r<thisGeneLength; r++) {
		for (i=0; i<thisGeneLength; i++) {
			var compa = geneCount[i+1];
			if (geneCount[i] == compa) {
				geneCount.splice(i,1);
				thisGeneLength = geneCount.length - 1;
			}
		}
	}
	var theGeneSum = geneCount.length;

	var qtlCount = new Array();
	var qtllist = ireq.responseXML.getElementsByTagName('qtlList');
	for (i=0; i<qtllist.length; i++){
		var qtls = qtllist[i].getElementsByTagName('ratid'); //get rat's gene number;
		if (qtls[0]){
			var getQtlIDs = qtls[0].firstChild.nodeValue;
			qtlCount.push(getQtlIDs);
		}else {continue;}
	}
	qtlCount.sort();
	var thisQtlLength = qtlCount.length - 1;
	
	for (r=0; r<thisQtlLength; r++) {
		for (i=0; i<thisQtlLength; i++) {
			var compaq = qtlCount[i+1];
			if (qtlCount[i] == compaq) {
				qtlCount.splice(i,1);
				thisQtlLength = qtlCount.length - 1;
			}
		}
	}
	var theQtlSum = qtlCount.length;

	var strainCount = new Array();
	var strainlist = ireq.responseXML.getElementsByTagName('strainList');
	for (i=0; i<strainlist.length; i++){
		var strains = strainlist[i].getElementsByTagName('ratid'); //get rat's gene number;
		if (strains[0]){
			var getStrainIDs = strains[0].firstChild.nodeValue;
			strainCount.push(getStrainIDs);
		}else {continue;}
	}
	strainCount.sort();
	var thisStrainLength = strainCount.length - 1;
	
	for (r=0; r<thisStrainLength; r++) {
		for (i=0; i<thisStrainLength; i++) {
			var compas = strainCount[i+1];
			if (strainCount[i] == compas) {
				strainCount.splice(i,1);
				thisStrainLength = strainCount.length - 1;
			}
		}
	}
	var theStrainSum = strainCount.length;

	divGeneSum.innerHTML = theGeneSum;
	divQtlSum.innerHTML = theQtlSum;
	divStrainSum.innerHTML = theStrainSum;
	
	var selectedGenes = geneCount.concat();
	for(i=0; i<selectedGenes.length;i=i+3) { selectedGenes.splice(i,2); } //remove 2/3 genes;
	var selectedIDs = selectedGenes.join('__') + '__' + qtlCount.join('__');
	//var allIDs = geneCount.join('__') + '__' +  qtlCount.join('__'); //print out 'allIDs';
	
	icreateFlashTab(selectedIDs);	//show GViewer for this category;
	
	var allTermName = 'Ontology:Nervous System Diseases';
	icreateGbrowserTab(allTermName);	//show GBrowser for this category;
}


function initAppendToTableV3(thisItem,div) {
	var baseLink,trailLink,linkURL;

	var thisx = ireq.responseXML.getElementsByTagName(thisItem);
	
	var thisArray = new Array();
	var unsortedArray = new Array();
	var sortedArray = new Array();

	for (i=0;i<thisx.length;i++)
	{
		var getRatNodeName = thisx[i].getElementsByTagName('ratsym');
		if (getRatNodeName[0]) { //if this node exists;
			var getRatNodeNameValue = getRatNodeName[0].firstChild.nodeValue; }
		else {
			var getRatNodeNameValue = ''; } //doesn't exist;
		var getRatNodeID = thisx[i].getElementsByTagName('ratid');
		if (getRatNodeID[0]) {
			var getRatNodeIDValue = getRatNodeID[0].firstChild.nodeValue; }
		else { 
			var getRatNodeIDValue = ''; }
		var ratArray = new Array();
		ratArray.push(getRatNodeNameValue);
		ratArray.push(getRatNodeIDValue);

		var getHumanNodeName = thisx[i].getElementsByTagName('humansym');
		var getHumanNodeID = thisx[i].getElementsByTagName('humanid');
		if (getHumanNodeName[0]) {
			var getHumanNodeNameLength = getHumanNodeName.length;
			var humanArray = new Array();
			for (m=0;m<getHumanNodeNameLength;m++) {
				var getHumanNodeNameValue = getHumanNodeName[m].firstChild.nodeValue;
				var getHumanNodeIDValue = getHumanNodeID[m].firstChild.nodeValue;
				humanArray.push(getHumanNodeNameValue);
				humanArray.push(getHumanNodeIDValue);
			}
		} else {
			var getHumanNodeNameValue = '';
			var getHumanNodeIDValue = '';
			var humanArray = new Array();
			humanArray.push(getHumanNodeNameValue);
			humanArray.push(getHumanNodeIDValue);
		}

		var getMouseNodeName = thisx[i].getElementsByTagName('mousesym');
		var getMouseNodeID = thisx[i].getElementsByTagName('mouseid');
		if (getMouseNodeName[0]) {
			var getMouseNodeNameLength = getMouseNodeName.length;
			var mouseArray = new Array();
			for (m=0;m<getMouseNodeNameLength;m++) {
				var getMouseNodeNameValue = getMouseNodeName[m].firstChild.nodeValue;
				var getMouseNodeIDValue = getMouseNodeID[m].firstChild.nodeValue;
				mouseArray.push(getMouseNodeNameValue);
				mouseArray.push(getMouseNodeIDValue);
			}
		} else {
			var getMouseNodeNameValue = '';
			var getMouseNodeIDValue = '';
			var mouseArray = new Array();
			mouseArray.push(getMouseNodeNameValue);
			mouseArray.push(getMouseNodeIDValue);
		}
		
		thisArray.push([ratArray,humanArray,mouseArray]); 
		
	} //end i-loop;

	unsortedArray = thisArray.concat();
	sortedArray = thisArray.sort();
	
	thisLength = sortedArray.length - 1;
	
	//remove repeated items;
	for (r=0; r<thisLength; r++) {
		for (i=0; i<thisLength; i++) {
			var compa = sortedArray[i+1][0][1];
			var compahuman = sortedArray[i+1][1][1];
			if (sortedArray[i][0][1]==compa && sortedArray[i][1][1]==compahuman) {
				sortedArray.splice(i,1);
				thisLength = sortedArray.length - 1;
			}
		}
	}

	// print table;
	var newTable = document.createElement('TABLE');
	newTable.setAttribute('cellPadding',1);
	var tbody = document.createElement('TBODY');
	newTable.appendChild(tbody);
	
	for (i=0; i<sortedArray.length; i++) {
		
		var row = document.createElement('TR');
		var subArray = 3; //because we only have gene, qtl, strain 3 sub-array;
		for (n=0; n<subArray; n++) {
		var container = document.createElement('TD');
		container.setAttribute('width', 100);
			for (j=0; j<sortedArray[i][n].length-1; j=j+2) {
				var linker = document.createElement('A');
			
				//get theItem, theItemID 
				var theTrans = sortedArray[i][n][j];
				var theItem = document.createTextNode(theTrans);
				var theItemID = sortedArray[i][n][j+1];
			
				//define baseLink, trailLink;
				if (thisItem == 'geneList'){
					if (n==0) {
						var baseLink = 'http://rgd.mcw.edu/tools/genes/genes_view.cgi?id=';
						var trailLink = '';
					} else if (n==1) {
						var baseLink = 'http://rgd.mcw.edu/tools/homologs/homologs_view.cgi?id=';
						var trailLink = '&species=1';
					} else if (n==2) {
						var baseLink = 'http://rgd.mcw.edu/tools/homologs/homologs_view.cgi?id=';
						var trailLink = '&species=2';
					}
				}
				else if (thisItem == 'qtlList'){
					var baseLink = 'http://rgd.mcw.edu/objectSearch/qtlReport.jsp?rgd_id=';
					var trailLink = '';
				}
				else { //strain
					baseLink = 'http://rgd.mcw.edu/tools/strains/strains_view.cgi?id='; //strain: rat;
					trailLink = '';
				}
				
				linkURL = baseLink + theItemID + trailLink;
				linker.setAttribute('href', linkURL);
				linker.setAttribute('target', 'new');
				linker.appendChild(theItem);
				container.appendChild(linker);
				var thisSpace = document.createTextNode(' ');
				container.appendChild(thisSpace);
			}
			row.appendChild(container);
		}
		tbody.appendChild(row);
	}
	
	iclearItem(div);
	document.getElementById(div).appendChild(newTable);
}


function icreateFlashTab(thisIDs){
	var url = 'GViewer/GViewer2.swf?titleBarText=Neurological Disease&baseMapURL=GViewer/data/rgd_rat_ideo.xml&browserURL=http://mcnally.hmgc.mcw.edu/gb/gbrowse/rgd_903/?name=Chr&annotationURL=/tools/GViewerFlashV2/getPositionForRGD.cgi?dbIds=' + thisIDs;
		//	  http://rgd.mcw.edu/diseases/GViewer....;

	var embed = document.createElement('embed');
	embed.setAttribute('width', 550);
	embed.setAttribute('height', 400);
	embed.setAttribute('src', url);
	
	var div = document.getElementById('gviewer');
	div.innerHTML = ' ';
	div.appendChild(embed);
}


function icreateGbrowserTab(thisTermname){
	var url = 'http://mcnally.hmgc.mcw.edu/gb/gbrowse/rgd_903?name=' + thisTermname;
	var myIframe = document.createElement('iframe');
	var thewidth = '100%';
	myIframe.setAttribute('width', thewidth);
	myIframe.setAttribute('height', 345);
	myIframe.setAttribute('src', url);

	var div = document.getElementById('gbrowser');
	div.innerHTML = ' ';
	div.appendChild(myIframe);
	
	openGBwindow(thisTermname,url);	//display a link to open a new GBrowse window;
}


function iclearItem(idName) {
    document.getElementById(idName).innerHTML = ' ';
}


// call iAppendQtlTabV4('qtlList','ratsym','ratid','rat-qtl');
//		iAppendQtlTabV4('qtlList','humansym','humanid','human-qtl');
function iAppendQtlTabV4(thisItem,symbol,id,div) {
	var baseLink,trailLink,linkURL;
	baseLink = 'http://rgd.mcw.edu/objectSearch/qtlReport.jsp?rgd_id=';
	trailLink = '';

	var thisx = ireq.responseXML.getElementsByTagName(thisItem);
	
	var thisArray = new Array();

	for (i=0;i<thisx.length;i++)
	{	
		var getNodeName = thisx[i].getElementsByTagName(symbol);
		var getNodeID = thisx[i].getElementsByTagName(id);
		
		if (getNodeName[0]) { //if this node exists??;
			var getNodeNameLength = getNodeName.length;
			var subArray = new Array();
			for (m=0;m<getNodeNameLength;m++) { //for case: [name1,id1,name2,id2];
				var getNodeNameValue = getNodeName[m].firstChild.nodeValue;
				var getNodeIDValue = getNodeID[m].firstChild.nodeValue;
				if (getNodeNameValue != ''){ //if this node value is null??;
					subArray.push(getNodeNameValue);
					subArray.push(getNodeIDValue);
				 }
			 }
			 
			 thisArray.push([subArray]);
		 }
		 
	} //end for-i;
	
	var sortedthisArray = thisArray.sort();

	// function removeRepeat(sortedthisArray);
	var thisLength = sortedthisArray.length - 1;
	
	for (r=0; r<thisLength; r++) {
		for (i=0; i<thisLength; i++) {
			var compa = sortedthisArray[i+1][0][0];  //huamn qtl??;
			if (sortedthisArray[i][0][0]==compa) {
				sortedthisArray.splice(i,1);
				thisLength = sortedthisArray.length - 1;
			}
		}
	}
	
	var newTable = document.createElement('TABLE');
	newTable.setAttribute('cellPadding',1);
	var tbody = document.createElement('TBODY');
	newTable.appendChild(tbody);
	
	for (i=0; i<sortedthisArray.length; i++) {
		
		var row = document.createElement('TR');
		var container = document.createElement('TD');	
		container.setAttribute('width',100);
		
		for (j=0; j<sortedthisArray[i][0].length-1; j=j+2) { //for case: [name1,id1,name2,id2];
			var linker = document.createElement('A');
				
			//get theItem, theItemID;
			var theTrans = sortedthisArray[i][0][0];
			var theItem = document.createTextNode(theTrans);
			var theItemID = sortedthisArray[i][0][1];

			linkURL = baseLink + theItemID;
			linker.setAttribute('href', linkURL);
			linker.setAttribute('target', 'new');
			linker.appendChild(theItem);
			container.appendChild(linker);
			var thisSpace = document.createTextNode(' ');
			container.appendChild(thisSpace);
		}

		row.appendChild(container);
		tbody.appendChild(row);
	}
	
	iclearItem(div);
	document.getElementById(div).appendChild(newTable);
}

