function showHide(myElement) {
    var flip = document.getElementById(myElement);
    var flipimg = document.getElementById(myElement + "img");
    if (flip.style.display == "none") {
	flip.style.display = "block";
	flipimg.src = "/images/open.png";
    } else {
	flip.style.display = "none";
	flipimg.src = "/images/closed.png";
    }
}

function moveOptions(src, dest) {
    src = document.getElementById(src);
    dest = document.getElementById(dest);
    i=0;
    while (i < src.length) {
	if (src.options[i].selected) {
	    dest.options[dest.length] = new Option(src.options[i].text, src.options[i].value, false, true);
	    src.options[i] = null;
	} else {
	    i++;
	}
    }
}

function submitAdvanced()
{
    var tissue = document.getElementById('tissue');
    var nottissue = document.getElementById('nottissue');
    for (i = 0; i < tissue.length; i++) {
	tissue.options[i].selected = true;
    }
    for (i = 0; i < nottissue.length; i++) {
	nottissue.options[i].selected = true;
    }
    
    document.forms[1].submit();
}

function validateSearch(genesearch) {
    var status = true;

    if ((genesearch == 0 && document.search.keywords.value == "") || 
	    (genesearch == 1 && document.genesearch.keywords.value == "") ||
	    (genesearch == 2 && document.allsearch.keywords.value == "")) {
	alert('You must enter at least one search term...');
	status = false;
    } else {
	var key = document.search.keywords.value;
	if (genesearch == 1) {
	    key = document.genesearch.keywords.value;
	} else if (genesearch == 2) {
	    key = document.allsearch.keywords.value;
	}
	var searchtype = document.search.SearchType.value;
	if (genesearch == 1) {
	    searchtype = document.genesearch.SearchType.value;
	} else if (genesearch == 2) {
	    searchtype = "allsearch";
	}
	if (searchtype == "ClusterID") {
	    var newID = validPrefixNum(key, "APD", 5);
	    if (newID == "") {
		newID = validPrefixNum(key, "APE", 5);
	    }
	    if (newID == "") {
		newID = validPrefixNum(key, "APG", 5);
	    }
	    if (newID == "") {
		newID = validPrefixNum(key, "APH", 5);
	    }
	    if (newID == "") {
		newID = validPrefixNum(key, "SCAFFOLD", -1);
	    }
	    if (newID == "") {
		status = false;
		alert('Cluster IDs should be of the form APDnnnnn');
	    } else {
		document.search.keywords.value = newID;
	    }
	} else if ((searchtype == "ESTid") || (searchtype == "ESTname") || 
		(searchtype == "BLASThit") || (searchtype == "FullText")) {
	    status = true;
	} else if (searchtype == "DrosHit") {
	    if ((key.substring(0,2) != "CG") && (key.substring(0,4) != "FBgn" )) {
		status = false;
		alert('Drosophila accession should begin "CG" or "FBgn"');
	    }
	} else if (searchtype == "GOID") {
	    var newID = validPrefixNum(key, "GO:", 7);
	    if (newID == "") {
		status = false;
		alert('GO identifiers (accession numbers) should be of the form GO:nnnnnnn');
	    } else {
		document.search.keywords.value = newID;
	    }
	} else if (searchtype == "allsearch") {
	    return true;
	} else if (searchtype == "ACYPI") {
	    var newID = validPrefixNum(key, "ACYPI", 5);
	    if (newID == "") {
		status = false;
		alert('ACYPI numbers should be in the form ACYPInnnnnnn with or without the -Rx or -Px suffix');
	    } else {
		if (newID.substr(5, 1) < "2") {
		    newID = validPrefixNum(key, "ACYPI", 6);
		}
		if (genesearch) {
		    document.genesearch.keywords.value = newID;
		} else {
		    document.search.keywords.value = newID;
		}
	    }
	} else {
	    status = false
	}
    }
    return(status);
}

function validPrefixNum(cid, prefix, numlen) {
    var ID = "";

    cid.replace(/^\s*|\s*$/g, '');
    cid = cid.toUpperCase();

    if (cid.substr(0,prefix.length) == prefix) {
	var tail = cid.substring(prefix.length);
	var cnum = parseInt(tail, 10);
	if (!isNaN(cnum)) {
	    if (numlen == -1) {
		ID = prefix + cnum;
	    } else if (cnum < Math.pow(10, numlen)) {
		cnum = cnum + "";
		if (cnum.length < numlen) {
		    cnum = "0000000000000000".substr(0,numlen-cnum.length) + cnum;
		}
		ID = prefix + cnum;
	    }
	}
    } else if (!isNaN(parseInt(cid, 10))) {
	var cnum = parseInt(cid, 10);
	cnum = cnum + "";
	if (cnum.length < numlen) {
	    cnum = "000000000000000000".substr(0,numlen-cnum.length) + cnum;
	}
	ID = prefix + cnum;
    }
    
    return ID;
}

function showDrosophilaTip() {
    document.getElementById("drostip").style.display = "block";
}
function hideDrosophilaTip() {
    document.getElementById("drostip").style.display = "none";
}

function checkTip() {
    if (document.getElementById("SearchType").value == "DrosHit") {
	showDrosophilaTip();
    } else {
	hideDrosophilaTip();
    }
}

var roots = [];
var treelevel = 3;
var tree = {};
function setupDetailsPage() {
    var numLeaves = countLeavesFromRoot(topparent);

    var currtarget = target;
    for (var i=0; i < 10; i++) {
	roots[i] = parents[currtarget];
	currtarget = parents[currtarget];
    }
    drawTree(tree);
}

var row;
var leaves = [];
var canvas;

function changetreesize() {
    treelevel = document.getElementById("treesize").value;
    drawTree();
}

var lineheight = 1;
function drawTree() {
    var currLeaves = countLeavesFromRoot(roots[treelevel]);
    canvas = document.createElement("canvas");
    var oldCanvas = document.getElementById("exptree");
    var mydiv = document.getElementById("NN");
    if (oldCanvas !== null) {
	mydiv.replaceChild(canvas, oldCanvas);
    } else {
	mydiv.appendChild(canvas);
    }
    canvas['onmouseover'] = "document.body.style.cursor=\"hand\";";
    canvas['onmouseout'] = "document.body.style.cursor=\"default\";";
    canvas.id = "exptree";
    canvas.onmouseup = clickOnCanvas
    canvas.width = 50*(treelevel+50);
    ctx = canvas.getContext('2d');
    ctx.font = "12pt Arial";
    lineheight = ctx.measureText('m').width * 1.4;
    lineheight = parseInt(lineheight) + 1;
    canvas.height = lineheight * (currLeaves + 1);
    row = 1;

// Walk the tree from the current root and put on the leaves

    var rootnodeposition = walkTree(roots[treelevel], ctx, lineheight, 0);
}

function clickOnCanvas(e) {
    var x = e.pageX - canvas.offsetLeft;
    var y = e.pageY - canvas.offsetTop;
    var myrow = y/lineheight + 1;
    var newURL = "http://www.aphidests.org/cgi-bin/genes.cgi?action=search&SearchType=ACYPI&keywords=" + leaves[parseInt(myrow)];
    window.location.href = newURL;
}

function walkTree(root, ctx, lineheight, depth) {
    var steplength = 300 / treelevel;
    if (root.substr(0,5) == "ACYPI") {
	ctx.fillStyle = "rgb(0,64,0)";
	ctx.font = "10pt Arial";
	if (root == target) {
	    ctx.fillStyle = "rgb(128,0,0)";
	    ctx.font = "bold 11pt Arial";
	}
	ctx.fillText(root, 50*depth, row*lineheight);
	ctx.fillText(neighbourgenes[root][0], 50*depth+150, row*lineheight);
	var words = root.split("-");
	leaves[row] = words[0];
	row++;
	return (row - 1.5) * lineheight;
    } else {
	var topchild = walkTree(children[root][1], ctx, lineheight, depth+1);
	var bottomchild = walkTree(children[root][0], ctx, lineheight, depth+1);
	var thisnodeY = (topchild + bottomchild) / 2;
	ctx.beginPath();
	ctx.moveTo(50*(depth+1), topchild);
	ctx.lineTo(50*depth, topchild);
	ctx.lineTo(50*depth, bottomchild);
	ctx.lineTo(50*(depth+1), bottomchild);
	ctx.stroke();
	return thisnodeY;
    }
}

function countLeavesFromRoot(root) {
    if (root.substr(0,5) == "ACYPI") {
	return 1;
    } else {
	return countLeavesFromRoot(children[root][0]) + countLeavesFromRoot(children[root][1]);
    }
}
