function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function trim (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function trim2(value) {	
	var string2 = value.replace(/^\s*/, '').replace(/\s*$/, '');	
	var obj = / /g;	
	return string2.replace(obj,"")
}

function changeConstraint() {
	
	var entrypoint = document.entryPointForm.entryPoint.value
	document.entryPointForm.constraint.options.length = 0;
	
	if (entrypoint == "Germplasm") {
		
		// give constraint for germplasm
		var option0 = new Option("by Germplasm Name", "by Germplasm Name")
		var option1 = new Option("by Country Name", "by Country Name")
		
		for (var i=0; i < 2; i++) {      
		  eval("document.entryPointForm.constraint.options[i]=option" + i)      
		  if (i==0) {         
		    document.entryPointForm.constraint.options[i].selected=true      
		  }   
		}
   		
   		//history.go(0)
				
	} else if (entrypoint == "Trait") {
		
		// give constraint for Trait
		var option0 = new Option("by Trait Name", "getTrait")
				
		for (var i=0; i < 1; i++) {      
		  eval("document.entryPointForm.constraint.options[i]=option" + i)      
		  if (i==0) {         
		    document.entryPointForm.constraint.options[i].selected=true      
		  }   
		}
   		   		
   		
	} else if (entrypoint == "Mutant") {
		window.location.href = "/ICIS5/action/mutant?method=viewTerm"   		
   	} else if (entrypoint == "Genotype") {
		window.location.href = "/ICIS5/action/genotype?method=getMain" 
   		
	} else {
		// give default constraint 
		var option0 = new Option("----- No Constraint -----", "-1")
				
		for (var i=0; i < 1; i++) {      
		  eval("document.entryPointForm.constraint.options[i]=option" + i)      
		  if (i==0) {         
		    document.entryPointForm.constraint.options[i].selected=true      
		  }   
		}
   		   		
	}
	
	//history.go(0)
}

/**
function viewConstraint() {

	var entrypoint = entryPointForm.entryPoint.value	
	
	if (entrypoint == "Markers") {		
		window.location.href = "/ICIS5/action/markers"	
	} else if (entrypoint == "Mutant") {	
		window.location.href = "/ICIS5/action/mutant?method=viewTerm"	
	} else {
		entryPointForm.submit()
	}
		
}
*/

function searchIris() {		
	if (document.entryPointForm.entryPoint.value == -1) {
		alert("Please select Entry Point")
		return false
	}
	
	if (trim(document.entryPointForm.searchString.value) != "") {
		
		var entrypoint = document.entryPointForm.entryPoint.value
		var constraint = document.entryPointForm.constraint.value		
		var searchText = trim(document.entryPointForm.searchString.value)
		
		
		//replace the value of percent to its Ascii equivalent
		var percent = /%/g
		var res = searchText.match(percent)
		if (res) {			
			var newSearch = searchText.replace(percent, '%25');
			searchText = newSearch
			//alert("newSearch: " + newSearch)
		}
					
		//check for action selected
		if (entrypoint == "Germplasm") {
			if (constraint == "by Country Name") {
				document.entryPointForm.action = "/ICIS5/action/gms?method=getListByCountry&search=" + searchText
			} else {
				document.entryPointForm.action = "/ICIS5/action/gms?method=getGmsList&search=" + searchText
			}
			
		} else if (entrypoint == "Accession") {
			
			document.entryPointForm.action = "/ICIS5/action/gcp?method=get&search=" + searchText
		} else if (entrypoint == "Trait") {
			
			document.entryPointForm.action = "/ICIS5/action/dms?method=getTrait&search=" + searchText
		}
				
		return true
	} else {
		
		if (document.entryPointForm.entryPoint.value == "Trait") {			
			document.entryPointForm.action = "/ICIS5/action/dms?method=getTraitGroups" 
			return true
		} else {
		
			alert("No search text specified.")
			document.entryPointForm.searchString.focus()
			return false
		}
	}	

}

//gmsList
function gotoPage(button, method) {
		
	document.gmsForm.action = "/ICIS5/action/gms?method=" + method
		
	var name = button.name		
	var page = parseInt(document.gmsForm.pageNum.value)
	var totalPages = parseInt(document.gmsForm.noOfPages.value)
	var gotoPage = parseInt(document.gmsForm.goToPage.value)
	
	var prev = document.gmsForm.prevPage.name
	var next = document.gmsForm.nextPage.name		
	
	if (name == prev) {
		if (page != 1) { 
			document.gmsForm.pageNum.value = page - 1				
			document.gmsForm.submit()
		}
	} else if (name == next) {
		if (page != totalPages) {
			document.gmsForm.pageNum.value = page + 1			
			document.gmsForm.submit()	
		}
	} else {  //Go to Page is clicked
		if (!isNaN(gotoPage)) {
			
			if ( (gotoPage < 1) || (gotoPage > totalPages) ) {
				alert("Invalid page")
			} else {				
				document.gmsForm.pageNum.value = gotoPage
				document.gmsForm.submit()
			}
		} else {
			alert("Invalid page")
		}
	}
			
}

function searchGms() {
	//gmsForm.action.value = "/gms?method=getGmsList"
	document.gmsForm.submit()
}

function viewInfo(id)  { 		
	//test for pedigreeTree
	myWindow = window.open(contextPath + "/action/gms?method=getGmsInfoTree&gmsId=" + id ,"GmsInfoTree","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,top=0,width=850,height="+(screen.height-90) );	
	myWindow.focus()
}

function loadIt(load, id) {	
	//alert("obj: " + obj)	
	var link = ""
	var suffixInfo = contextPath + "/action/gms?method=getGmsInfo&gmsId="
    var suffixInfoTree = contextPath + "/action/gms?method=getGmsInfoTree&parent=true&gmsId="
    // 0 stands for info, 1 stands for tree
	if (load == 0) {
		link = suffixInfo + id
	} else {
		link = suffixInfoTree + id				
	}
	//alert("link: " + link)
	parent.location.href = link
}

function gotogramene(id) {
	
	var gramenelink = "http://www.gramene.org/db/markers/marker_view?marker_name="
	var grameneWindow = window.open(gramenelink + id ,"Gramene","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=900,height=800");
	
	grameneWindow.focus()
}

function grameneNameSearch(name) {
	
	var gramenelink = "http://www.gramene.org/db/searches/browser?query="		
	var grameneWindow = window.open(gramenelink + name ,"Gramene","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=800,height=800");
	
	grameneWindow.focus()
}

function grinNameSearch(name) {
	
	var grinlink = "http://www.ars-grin.gov/cgi-bin/npgs/swish/accboth?si=0&query="	
	var grinWindow = window.open(grinlink + name ,"Grin","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=800,height=800");
	
	grinWindow.focus()
}

function viewStudy(id) {
	
	var link = "/ICIS5/action/dms?method=getStudy&gmsId=" + id
	parent.location.href = link
}


function viewEvalData(id, name) {
	evalData = window.open("/ICIS5/action/dms?method=getEvaluationData&studyid=" + id + "&name=" + name ,"ViewEvalData","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,width=800,height=800");	
	evalData.focus()
}

function viewStudyPerl(id) {
	myWindow = window.open("http://www.iris.irri.org/cgi-bin/gcp/Study.pl?GID=" + id ,"ViewStudy","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,width=800,height=800");	
	myWindow.focus()
}

function checkforAccession() {
	
	if (document.gcpForm.search.value == "") {
		alert("Please choose an Accession.")
		return false;
	} else {
		return true;
	}
}

function viewMutantInfo(mutantName)  { 
		
	myWindow = window.open("/ICIS5/action/mutant?method=getInfo&mutant=" + mutantName ,"MutantInfo","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=750,height=450");	
	myWindow.focus()
}

function grameneOntology(id) {
	var link = "http://www.gramene.org/db/ontology/search_term?id="
	
	grameneWindow = window.open(link + id ,"OntologyInfo","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=800,height=800");	
	grameneWindow.focus()
}

function addAll() {
/**
	alert("here in addAll()")
	alert(mutantOntologyForm.attribute.value)
	alert(mutantOntologyForm.value.value)
	alert(mutantOntologyForm.value.options.size)
	var optionSize = mutantOntologyForm.value.options.size
	for (var i = 1; i <= optionSize; i++) {
		
	}
	*/
	
	document.mutantOntologyForm.value.value = "-1"  //reset 
	
	if (document.mutantOntologyForm.attribute.value == "-1") {
		alert("Please select one attribute")
		return false
	} else {	
		//check for values of Value box	
		//alert(document.mutantOntologyForm.value.options.length)
		if (document.mutantOntologyForm.value.options.length > 1) {
			var link = "/ICIS5/action/mutant?method=add"			
			document.mutantOntologyForm.action = link		
			return true
		} else {
			return false
		}
	}	
}

function addValue() {		
	if (document.mutantOntologyForm.value.value != "-1") {
	
		var link = "/ICIS5/action/mutant?method=add"
		
		var searchText = document.mutantOntologyForm.value.options[document.mutantOntologyForm.value.selectedIndex].text
		//replace the value of ampersand to its Ascii equivalent
		var ampersand = /&/g
		var res = searchText.match(ampersand)
		//alert(res)
		if (res != null) {			
			var newSearch = searchText.replace(ampersand, '%26');
			searchText = newSearch
			//alert("newSearch: " + newSearch)
		}
		
		document.mutantOntologyForm.action = link + "&valueDesc=" + searchText	
		document.mutantOntologyForm.submit()
	}	
}



function remove() {
	if (document.mutantOntologyForm.phenotypeToRemove.value) {
		var link = "/ICIS5/action/mutant?method=remove"
		//alert(mutantOntologyForm.phenotypeToRemove.value)
		document.mutantOntologyForm.action = link
		return true
	} else {
		return false
	}
}

function removeAll() {
	var optionLen = document.mutantOntologyForm.phenotypeToRemove.options.length
	
	if (optionLen != 1) {
		var link = "/ICIS5/action/mutant?method=removeAll"
		document.mutantOntologyForm.action = link
		return true
	} else {
		return false
	}
}

function findMutants() {
	//alert(mutantOntologyForm.phenotypeToRemove.options.length)
	var optionSize = document.mutantOntologyForm.phenotypeToRemove.options.length
	if (! (optionSize < 2)) {
		var link = "/ICIS5/action/mutant?method=getMutants"
		document.mutantOntologyForm.action = link
		return true
	} else {
		return false
	}
}

function loadAttribute(name) {
	var link = "/ICIS5/action/mutant?method=viewTerm&name=" + name 	
	parent.location.href = link
}

function loadTraitValues() {
	
	var attribute = document.mutantOntologyForm.attribute.options[document.mutantOntologyForm.attribute.selectedIndex].text	
	//var phenoId = mutantOntologyForm.attribute.options[mutantOntologyForm.attribute.selectedIndex].value
	if (document.mutantOntologyForm.attribute.value != "-1") {
		
		var link = "/ICIS5/action/mutant?method=getValues&attri=" + attribute
		parent.location.href = link
	}
}

function gotoStudyPage(page) {
	var currentPage = parseInt(document.dmsForm.currentPage.value)
	var totalPages = parseInt(document.dmsForm.totalPages.value)	
	var temp;
	if (page == 'next') {
		temp = currentPage + 1
	} else {
		temp = currentPage - 1
	}
	//alert("page " + temp + "  of " +  totalPages)
	if (! ((temp > totalPages) || (temp <= 0)) ) {
		document.dmsForm.currentPage.value = temp
		document.dmsForm.submit()
	}
}

function getTraitStudies(trname, id) {
	
	var link = "/ICIS5/action/dms?method=getTraitStudies&id=" + id + "&trait=" + trname
	parent.location.href = link
}

function getStudiesForTrait(studyid) {
	myWindow = window.open("/ICIS5/action/dms?method=getGermplasmByTraitInStudy&studyid=" + studyid,"StudiesForTrait","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,width=800,height=800");	
	myWindow.focus()
}

function addCategory(id, name) {
	var link = "/ICIS5/action/genotype?method=add&id=" + id + "&name=" + name
	parent.location.href = link
}

function removeAllCategory() {
	
	var optionLen = document.genotypeForm.categoryToRemove.options.length
	//alert(optionLen)
	if (optionLen != 1) {
		var link = "/ICIS5/action/genotype?method=removeAll"
		document.genotypeForm.action = link
		return true
	} else {
		return false
	}
}

function removeCategory() {
	//alert(document.genotypeForm.categoryToRemove.value)
	
	if (document.genotypeForm.categoryToRemove.value) {
		var link = "/ICIS5/action/genotype?method=remove"
		//alert(genotypeForm.categoryToRemove.value)
		document.genotypeForm.action = link
		return true
	} else {
		return false
	}
}

function checkForList(method) {
	var optionLen = document.genotypeForm.categoryToRemove.options.length
    if (optionLen != 2) {
    	alert("Please select List(s) from Country")
    } else {
    	var link = "/ICIS5/action/genotype?method=" + method
		parent.location.href = link
    }
}

	
