var bool2 = false;
var currentField = "textarea";
var selectionSeparator = "--$$$$--";
var tripleIDSeparator = "~~$$$$~~";
var allChecked = true;
var prems=true;


function addField() {
	form = getFieldId('frm');
	var fieldName = getField("field");
	fieldName = fieldName.value;
// alert(fieldName);
	var fieldValue = getFieldId("textarea");
	fieldValue = fieldValue.value;

	nbCarac = fieldValue.split("*")
	nbCarac = nbCarac.join("")
	nbCarac = nbCarac.split(" ")
	nbCarac = nbCarac.join("")
	nbCarac = nbCarac.length
	
	if (fieldName == "" || fieldValue ==""){
		if (fieldName == "")
			alert("Please choose a field name");
		else
			alert("Please fill the field");
		}
	else if ((fieldName == "Taxon" || fieldName == "Common" || fieldName =="unitKey") && nbCarac < 3){
		alert("Please enter at least 3 characters");
	}
	else{
	   /*	var tmp = "field"+fieldName;
	    var tmp1 ="value"+fieldName;
	    var tmp2 = "button"+fieldName;
		var tmp4="list"+fieldName;	*/	
		var tmp="value"+fieldName;
		var tmp1="value"+fieldName;
		

		getFieldId(tmp).style.display="inline";
		getFieldId(tmp1).style.display="inline";
		prec = getFieldId(tmp1).value
		if (prec != ""){
			getFieldId(tmp1).setAttribute("value",prec+" OR "+fieldValue);
			getFieldId(tmp1).value=prec+" OR "+fieldValue;}
		else{
		    getFieldId(tmp1).setAttribute("value",fieldValue); 
		    getFieldId(tmp1).value=fieldValue; }
// 		getFieldId(tmp2).style.display="inline";
// 		if(getFieldId(tmp4))
// 		getFieldId(tmp4).style.display="inline";

// 	    disableInput(form,fieldName,"true");
		reinit(form);
	    showHideListButton();
		i++;  		
  	}
}

function reinit(form){	
	var change = getFieldId("fieldList");
	change.options[0].selected = true;

	var textDefault = getFieldId('textarea');
	textDefault.setAttribute('value',"");
	textDefault.value="";
}

function disableInput(form,fieldName,val){
	var fieldToHandle = fieldName
	var liste = null; 
	 for (var j=0; j<form.getElementsByTagName("select").length; j++ ) {
		liste = form.getElementsByTagName("select")[j];
		if (liste.name == "field")
			for ( var k=0; k<liste.options.length; k++)
				if (liste.options[k].value == fieldToHandle)
					if(val == "true"){
						liste.options[k].setAttribute('disabled',"disabled");
						liste.options[k].disabled = "disabled";
						}
					else{
						liste.options[k].removeAttribute('disabled');
						liste.options[k].disabled = null;
						}
	}
}	

function removeField (fieldName){  
	form=getFieldId('frm');
	disableInput(form,fieldName,"false");
   	var tmp = "field"+fieldName;
    var tmp1 ="value"+fieldName;
    var tmp2 = "button"+fieldName;
	var tmp4="list"+fieldName;
	
	
	getFieldId(tmp).style.display="none";
	getFieldId(tmp1).style.display="none";
	getFieldId(tmp1).setAttribute("value","");
	getFieldId(tmp1).value="";
	getFieldId(tmp2).style.display="none";
	if(getFieldId(tmp4))
	getFieldId(tmp4).style.display="none";
}

function getField (fieldName) {
form=getFieldId('frm')
  if (!document.all)
    return form[fieldName];

  else  // IE has a bug not adding dynamically created field 
        // as named properties so we loop through the elements array 
    for (var e = 0; e < form.elements.length; e++)
      if (form.elements[e].name == fieldName)
        return form.elements[e];
  return null;
} 

function getFieldId (id) {
	 if (document.getElementById)
    {
        // this is the way the standards work
        var obj = document.getElementById(id);
    }
    else if (document.all)
    {
        // this is the way old msie versions work
        var obj = document.all[id];
    }
    else if (document.layers)
    {
        // this is the way nn4 works
        var obj = document.layers[id];
    }
    return obj
} 

function showHideListButton(paf){
//reinit values for suggest tool
CACHED = [];
sel_id = -1;
var change = getFieldId("fieldList");
var val = change.options[change.selectedIndex].value;

	elt=getFieldId('sugImg');
// 	if (val != "Taxon" && val!= "Genus" && val != "Family" && val!= "Country" && val!="HigherTaxon" && val!="Common" && val!="CollCode" && val!="InstCode"){
// 		elt.style.backgroundColor = "#ccc";
// 		onOff = 'OFF'	
// 		document.getElementById("searchresults").style.display = "none";
// 	}
// 	else {elt.style.backgroundColor = "transparent"; onOff = "ON";}
	onOff = "ON";
	title= elt.title.split(":")[0]+":"+onOff;
	elt.title=title;
	elt.setAttribute('title',title);
		

// 	if(val == "institutionId" || val == "collectionId" || val =="family" || val =="country"){
// 		getFieldId("dragDropList").disabled="none";
// 		getFieldId("dragDropList").removeAttribute('disabled');
// 		getFieldId("dragDropList").style.color="#000";
// 	}
// 	else{
// 		getFieldId("dragDropList").disabled="disabled";
// 		getFieldId("dragDropList").setAttribute('disabled',"disabled");
// 		getFieldId("dragDropList").style.color="#ccc";
// 	}
// 	if(val.search("date") != -1){
// 		getFieldId("dateHelpInfo").disabled="none";
// 		getFieldId("dateHelpInfo").removeAttribute('disabled');}
// 	else{
// 		getFieldId("dateHelpInfo").disabled="disabled";
// 		getFieldId("dateHelpInfo").setAttribute('disabled',"disabled");}
}    
function performAdvanced(){
displayLoadingGifAdvanced();
getFieldId('frm').submit();
}

function displayLoadingGifAdvanced(){
	var imageDiv = getFieldId("loadingAdvanced");
	var image = getFieldId("loadingGifAdvanced");

	if (imageDiv.style.display=="block"){
		document.getElementById("loadingAdvanced").style.display="none";
		var content = getFieldId( 'loading' ).innerHTML
		document.getElementById("page").style.backgroundImage = "url(/botany/static/images/bg.jpg)";
		}
	else{
	    document.getElementById('loadingAdvanced').style.display ="block";
	    var content = getFieldId( 'loadingAdvanced' ).innerHTML
		document.getElementById("page").style.backgroundImage = "url(/botany/static/images/bgbg.jpg)";
		
		}
	getFieldId( 'loadingAdvanced' ).innerHTML= content;
		
}
   
function displayList(){
	currentField = "textarea"
	var fieldName = getField("field");
	fieldName = fieldName.value;
	var page;
	if (fieldName == "institutionId" || fieldName == "collectionId"  )
		display = true;
	else
		display = false;
	if (display)
		window.open('/botany/search/units/popup/openPopup/'+fieldName,'List', 'resizable=yes, scrollbars=yes, toolbar=no, status=no, width=300, height=300, center');
	}




function redisplayList(fieldID){
	currentField = fieldID
	window.open('/botany/search/units/popup/openPopup/'+fieldID,'List', 'resizable=yes, scrollbars=yes, toolbar=no, status=no, width=300, height=300, center');
}	

function enableRecordBasis(){
	var checkbx = getFieldId("record_basis_flag");
	var records = getFieldId("record_basis");
	if ( ! checkbx.checked){
		records.setAttribute('disabled',"disabled");
		records.disabled = "disabled";
	}
	else{
		records.removeAttribute('disabled');
		records.disabled = null;
	}
}

 function insert_option(idToAdd)
  {
    if(currentField == "textarea"){
    	var champ = getFieldId(currentField)
    	if (champ.value == "")
    		champ.value = champ.value + idToAdd
    	else
    		champ.value = champ.value + ", " + idToAdd
    }
    else{
    	var champ = getFieldId("value"+currentField)
    	if (champ.value == "")
    		myvalue = champ.value + idToAdd
    	else
    		myvalue = champ.value + ", " + idToAdd
    	champ.setAttribute("value",myvalue);
    	champ.value = myvalue;
	}
  }
  
function idReporter(id) {
	id=encode(id);
	window.opener.insert_option(id);
	this.close();
}

function refineQuery(box,tosee){//box=page en cours
if (box=="commonName")box="commonBox";
if (box.indexOf('Box')==-1) box=box+"Box";

var liste = document.getElementsByName(box);
//  alert(box);
	var selection =[];
	var s = 0;
	for (var k=0; k<liste.length;k++){
		if (liste[k].checked){
			val = liste[k].value
			if (val.indexOf('<b>') != -1)
				val = (val.split('<b>'))[0]
			val = val.split("/");
	        val = val.join("%2F");
	        val = val.split(",");
	        val = val.join("%2C");
			selection[s] = val;
			s ++;
		}
	}
	var form = getFieldId("form");
	var boxField = getFieldId("box");
	boxField.setAttribute('value',box);
    boxField.value=box;	

	boxField = getFieldId("tosee");
	boxField.setAttribute('value',tosee);
    boxField.value=tosee;	
   
	form.action="/botany/search/units/preview/refineQuery/";
	selectionStr = selection.join(selectionSeparator);
	var fieldName = getFieldId("listStr");
	fieldName.setAttribute('value',selectionStr);
    fieldName.value=selectionStr;	

	
	
	if (s > 0 && liste.length>s)
		form.submit();
	else{
		var boxField = getFieldId("box");
		boxField.setAttribute('value',tosee);
    	boxField.value=tosee;
		form.action="/botany/search/units/preview/see/";
			form.submit();
	}
// 	else
// 	alert("Check at least one box");
}

function refineMetaQuery(box){
	var liste = document.getElementsByName(box);
	var selection =[];
	var s = 0;
	for (var k=0; k<liste.length;k++){
		if (liste[k].checked){
			val = liste[k].value
			if (val.indexOf('<b>') != -1)
				val = (val.split('<b>'))[0]
			val = val.split("/");
	        val = val.join("%2F");
	        val = val.split(",");
	        val = val.join("%2C");
	        val = val.split("(");
	        val = val.join("~$par$~");
	        val = val.split(")");
	        val = val.join("~$parf$~");
			selection[s] = val;
			s ++;
		}
	}
	var form = getFieldId("form");
	var boxField = getFieldId("box");
	boxField.setAttribute('value',box);
    boxField.value=box;	
   
	form.action="/botany/search/metadata/browsing/refineQuery/"
	selectionStr = selection.join(selectionSeparator);
	var fieldName = getFieldId("listStr");
	fieldName.setAttribute('value',selectionStr);
    fieldName.value=selectionStr;	
	
	if (s > 0)
		form.submit();
	else
	alert("Check at least one box");
}

function fuzzySearch(box){
	var liste = document.getElementsByName(box);
	var selection =[];
	var s = 0;
	for (var k=0; k<liste.length;k++){
		if (liste[k].checked){
			val = liste[k].value
			if (val.indexOf('<b>') != -1)
				val = (val.split('<b>'))[0]
			val = val.split("/");
	        val = val.join("%2F");
	        val = val.split(",");
	        val = val.join("%2C");
	        val = val.split("(");
	        val = val.join("~$par$~");
	        val = val.split(")");
	        val = val.join("~$parf$~");
			selection[s] = val;
			s ++;
		}
	}
if (s>10) {alert("Please do not select more than 10 names");return;}	
if (s==0) {alert("Check at least one box (max. 10)");return;}

if (s>3){
	Check = confirm("The expanded search can take a while.\n Do you want to go further?");
	if(Check == false) return;
}	
	var form = getFieldId("form");
	var boxField = getFieldId("box");
	boxField.setAttribute('value',box);
    boxField.value=box;	
   
	form.action="/botany/search/units/preview/performFuzzySearch/"
	selectionStr = selection.join(selectionSeparator);
	var fieldName = getFieldId("listStr");
	fieldName.setAttribute('value',selectionStr);
    fieldName.value=selectionStr;	
	
	if (s > 0)
		form.submit();
	
}


function seeResult(box){
if (box=="commonName")box="commonBox";
if (box.indexOf('Box')==-1) box=box+"Box";
	var liste = document.getElementsByName(box);
	var selection =[];
	var s = 0;
	for (var k=0; k<liste.length;k++){
		if (liste[k].checked){
			val = liste[k].value
			if (val.indexOf('<b>') != -1)
				val = (val.split('<b>'))[0]
			val = val.split("/");
	        val = val.join("%2F");
	        val = val.split(",");
	        val = val.join("%2C");
	        val = val.split("(");
	        val = val.join("~$par$~");
	        val = val.split(")");
	        val = val.join("~$parf$~");
			selection[s] = val;
			s ++;
		}
	}
    
    var form = getFieldId("form");
	var boxField = getFieldId("box");
	boxField.setAttribute('value',box);
    boxField.value=box;	
    
	form.action="/botany/search/units/preview/seeResult/"
	selectionStr = selection.join(selectionSeparator);
	var fieldName = getFieldId("listStr");
	fieldName.setAttribute('value',selectionStr);
   fieldName.value=selectionStr;	
	
	if (s > 0)
		form.submit();
	else
	alert("Check at least one box");
}


function seeMetaResult(box){
	var liste = document.getElementsByName(box);
	var selection =[];
	var s = 0;
	for (var k=0; k<liste.length;k++){
		if (liste[k].checked){
			val = liste[k].value
			if (val.indexOf('<b>') != -1)
				val = (val.split('<b>'))[0]
			val = val.split("/");
	        val = val.join("%2F");
	        val = val.split(",");
	        val = val.join("%2C");
	        val = val.split("(");
	        val = val.join("~$par$~");
	        val = val.split(")");
	        val = val.join("~$parf$~");
			selection[s] = val;
			s ++;
		}
	}
    
    var form = getFieldId("form");
	var boxField = getFieldId("box");
	boxField.setAttribute('value',box);
    boxField.value=box;	
    
	form.action="/botany/search/metadata/browsing/seeResult/"
	selectionStr = selection.join(selectionSeparator);
	var fieldName = getFieldId("listStr");
	fieldName.setAttribute('value',selectionStr);
   fieldName.value=selectionStr;	
	
	if (s > 0)
		form.submit();
	else
	alert("Check at least one box");
}


function encode(liste){
        liste = liste.split("/");
        liste = liste.join("%2F");
        liste = liste.split(",");
        liste = liste.join("%2C");
        liste = liste.split("(");
	    liste = liste.join("~$par$~");
	    liste = liste.split(")");
	    liste = liste.join("~$parf$~");
        return liste;
}

function getDetailsNum(unitID,collectionID,institutionID,resourceKey,num,biorecordKey){
  
        while (collectionID.match("  ")){
                collectionID = collectionID.replace("  "," ");
        }
        while (institutionID.match("  ")){
        	institutionID = institutionID.replace("  "," ");
        }
        collectionID = encode(collectionID);
        institutionID = encode(institutionID);
        unitID = encode(unitID);
        
		if (collectionID == " "|| collectionID == "")
        	collectionID = null;
        if (institutionID == " "|| institutionID =="")
            institutionID = null;

	displayLoadingGif();
	
	collectionField = getFieldId('collectionID');
	institutionField = getFieldId('institutionID');	
	unitIDField = getFieldId('unitID');
	resourceField = getFieldId('resourceKey');
	posField = getFieldId('pos');
	
	collectionField.setAttribute('value',collectionID);	
	collectionField.value=collectionID;
	
	institutionField.setAttribute('value',institutionID);	
	institutionField=institutionID;
	
	unitIDField.setAttribute('value',unitID);
	unitIDField.value=unitID;
	

	resourceField.setAttribute('value',resourceKey);
	resourceField.value=resourceKey;
	
	posField.setAttribute('value',num);
	posField.value=num;

	if (biorecordKey != null && biorecordKey !=''){
	getFieldId('detailsFrm').innerHTML+='<input type="hidden" id="bioKey" name="bioKey" value="'+biorecordKey+'"/>' ;
	}
	
	getFieldId('detailsFrm').submit()
}


function getBrowseAlphabetic(form,browser,abc){
displayLoadingGif();
	var fieldName = getFieldId("alphabetical");
	    fieldName.setAttribute('value',abc);
		fieldName.value=abc;	
		
	var fieldName2 = getFieldId("browser");
	fieldName2.setAttribute('value',browser);
	fieldName2.value=browser;
form.submit()  ;

}

function hideShow(id){
elt=getFieldId(id);
if (elt.style.display != 'block')
elt.style.display='block';
else{elt.style.display='none';}

}

function getDetails(unitID,collectionID,institutionID,resourceKey,biorecordKey){

         while (collectionID.match("  ")){
                collectionID = collectionID.replace("  "," ");
        }
        while (institutionID.match("  ")){
        	institutionID = institutionID.replace("  "," ");
        }
        collectionID = encode(collectionID);
        institutionID = encode(institutionID);
        unitID = encode(unitID);
        
		if (collectionID == " "|| collectionID == "")
        	collectionID = null;
        if (institutionID == " "|| institutionID =="")
            institutionID = null;
	displayLoadingGif();
	
	collectionField = getFieldId('collectionID');
	institutionField = getFieldId('institutionID');	
	unitIDField = getFieldId('unitID');
	resourceField = getFieldId('resourceKey');
	posField = getFieldId('pos');
	
	
	collectionField.setAttribute('value',collectionID);	
	collectionField.value=collectionID;
	
	institutionField.setAttribute('value',institutionID);	
	institutionField=institutionID;
	
	unitIDField.setAttribute('value',unitID);
	unitIDField.value=unitID;

	resourceField.setAttribute('value',resourceKey);
	resourceField.value=resourceKey;

	if (biorecordKey != null && biorecordKey !=''){
	try{
		bioField=getFieldId('bioKey');
		bioField.setAttribute('value',biorecordKey);
		bioField.value=biorecordKey;
	}
	catch(e){
		getFieldId('detailsFrm').innerHTML+='<input type="hidden" id="bioKey" name="bioKey" value="'+biorecordKey+'"/>';}
	}
	getFieldId('detailsFrm').submit()
}

function getMetaDetails(metadataID,name){
	metaField = getFieldId('metadataID');
	metaField.setAttribute('value',metadataID);
	metaField.value=metadataID;
	displayLoadingGif();
	getFieldId('detailsFrm').submit()	
}

function getMetaDetailsNum(metadataID,num){
	metaField = getFieldId('metadataID');
	posField = getFieldId('pos');	

	metaField.setAttribute('value',metadataID);
	metaField.value=metadataID;
	
	posField.setAttribute('value',num);
	posField.value=num;
	displayLoadingGif();
	getFieldId('detailsFrm').submit()
}


function checkAll(box){
if (box == "avaBox"){
if (allChecked && prems) {allChecked=false; prems =false;}
else if (!allChecked && prems) {allChecked=true; prems = false;}
}
	var boxes = document.getElementsByName(box);
	if (! allChecked){
		//check everything
		for (var k=0; k<boxes.length;k++){
			boxes[k].setAttribute('checked',"checked");
			boxes[k].checked = true;
			
		}
		allChecked = true
	}
	else{
	//uncheck everything
		for (var k=0; k<boxes.length;k++){
			boxes[k].removeAttribute('checked');
			boxes[k].checked = false;
			
		}
		allChecked = false
	}
}

function checkAllConcept(box,tocheck){
if (box == 'taxonBox'){
try{
var boxes = document.getElementsByTagName('input');

	if (tocheck){
		//check everything
		for (var k=0; k<boxes.length;k++){
			if (boxes[k].type=="checkbox"){
			boxes[k].setAttribute('checked',"checked");
			boxes[k].checked = true;
			}
			
		}
		allChecked = true
	}
	else{
	//uncheck everything
		for (var k=0; k<boxes.length;k++){
			if (boxes[k].type=="checkbox"){
			boxes[k].removeAttribute('checked');
			boxes[k].checked = false;
			}
			
		}
		allChecked = false
	}
}catch(e){}
}
else{
try{
	var boxes = document.getElementsByTagName('input');
	references = Array()
	i=0;
	for (var k=0; k<boxes.length;k++){
			if (boxes[k].name.search('refBox') !=-1){
				if (boxes[k].checked== false){
					references[i]=(boxes[k].name.split('_')[1]);
					i++;
				}
			}
			
		}

	if (tocheck){
		//check everything
		for (var k=0; k<boxes.length;k++){
		ok=true
			if (boxes[k].name.search(box) !=-1){
				for (var i=0; i<references.length;i++){
					if (boxes[k].name.search(references[i]) !=-1)
						ok=false;
				}
			
			if (ok == true){
			boxes[k].setAttribute('checked',"checked");
			boxes[k].checked = true;}
			}
		}
			
		
		allChecked = true
	}
	else{
	//uncheck everything
		for (var k=0; k<boxes.length;k++){
			ok=true
			if (boxes[k].name.search(box) !=-1){
				for (var i=0; i<references.length;i++){
					if (boxes[k].name.search(references[i].split('_')[1]) !=-1)
						ok=false;
				}
			
			if (ok == true){
			boxes[k].removeAttribute('checked');
			boxes[k].checked = false;}
			}
			
		}
		allChecked = false
	}
}
catch(e){}
}

}

function getFormatedSelection(){
//unitID,collectionID,institutionID,resourceKey
	var liste = document.getElementsByName('avaBox');
	var corb = [];
	var corbU =[];
	var i =0;
	var m =0;
	for (var k=0; k<liste.length;k++){
		value = liste[k].value.slice(0,(liste[k].value).length);
		value = value.split(tripleIDSeparator);
			collectionID = value[1];
        while (collectionID.match("  ")){
                collectionID = collectionID.replace("  "," ");
        }
        collectionID = encode(collectionID);
		if (collectionID == " " || collectionID == "")
        	collectionID = null;
        institutionID = value[2]
        while (institutionID.match("  ")){
        	institutionID = institutionID.replace("  "," ");
        }
        institutionID = encode(institutionID);
		if (institutionID == " " || institutionID =="")
        	institutionID = null;
		resourceKey = value[3];
		if (resourceKey == "")
				resourceKey = null;
			unitID = value[0];
	    unitID = encode(unitID);
		name = value[4];
		
		biorecordKey = value[5];
        while (biorecordKey.match("  ")){
                biorecordKey = biorecordKey.replace("  "," ");
        }
        biorecordKey = encode(biorecordKey);
		if (biorecordKey == " " || biorecordKey == "")
        	biorecordKey = null;
		
		if (liste[k].checked){		
		corb[i] = unitID+tripleIDSeparator+collectionID+tripleIDSeparator+institutionID+tripleIDSeparator+resourceKey+tripleIDSeparator+name+tripleIDSeparator+biorecordKey
		i ++;
		}
		else{
		corbU[m] = unitID+tripleIDSeparator+collectionID+tripleIDSeparator+institutionID+tripleIDSeparator+resourceKey+tripleIDSeparator+name+tripleIDSeparator+biorecordKey
		m ++;
		}
	}
	checkStr = corb.join(selectionSeparator);
	uncheckStr = corbU.join(selectionSeparator);
	if (checkStr.length==0)
	return ["empty","empty"];
	else
	return [checkStr,uncheckStr];
}


function getMetaFormatedSelection(){
//metadataID,metadataName
	var liste = document.getElementsByName('avaBox');
	var corb = [];
	var corbU =[];
	var i =0;
	var m =0;
	for (var k=0; k<liste.length;k++){
		value = liste[k].value.slice(0,(liste[k].value).length);
		value = value.split(tripleIDSeparator);
	    metadata = value[0];
		name = value[1];
		
		if (liste[k].checked){		
		corb[i] = metadata+tripleIDSeparator+name;
		i ++;
		}
		else{
		corbU[m] = metadata+tripleIDSeparator+name;
		m ++;
		}
	}
	checkStr = corb.join(selectionSeparator);
	uncheckStr = corbU.join(selectionSeparator);
	return [checkStr,uncheckStr];
}

function getListDetails(form){	
	corbs = getFormatedSelection();
	corbStr = corbs[0]
	corbUstr= corbs[1]
	if (corbStr=="empty") return alert("Check at least one box");
		displayLoadingGif();
		var fieldName = getFieldId("corbStr");
	    fieldName.setAttribute('value',corbStr);
		fieldName.value=corbStr;	
		
		var fieldName = getFieldId("corbUstr");
	    fieldName.setAttribute('value',corbUstr);
		fieldName.value=corbUstr;			
		
		form.submit()   

}

function sortBy(col,top){
 corbs = getFormatedSelection();
 	corbSortStr = corbs[0]
 	corbSortUstr= corbs[1]
	
	var form=getFieldId('sortPageWithBoxes');


	var fieldName = getFieldId("corbSortStr");
    fieldName.setAttribute('value',corbSortStr);
	fieldName.value=corbSortStr;	
	
	var fieldName = getFieldId("corbSortUstr");
    fieldName.setAttribute('value',corbSortUstr);
	fieldName.value=corbSortUstr;
	
 	var fieldName = getFieldId("col");
     fieldName.setAttribute('value',col);
 	fieldName.value=col;
 	
 	var fieldName = getFieldId("top");
     fieldName.setAttribute('value',top);
 	fieldName.value=top;			
 	
 	form.submit()   
 }

function sortByMeta(col,top){
	corbs = getMetaFormatedSelection();
	checkStr = corbs[0]
	uncheckStr= corbs[1]
	

	var form=getFieldId('sortPageWithBoxes');


	var fieldName = getFieldId("corbSortStr");
    fieldName.setAttribute('value',checkStr);
	fieldName.value=checkStr;	
	
	var fieldName = getFieldId("corbSortUstr");
    fieldName.setAttribute('value',uncheckStr);
	fieldName.value=uncheckStr;
	
	var fieldName = getFieldId("col");
    fieldName.setAttribute('value',col);
	fieldName.value=col;
	
	var fieldName = getFieldId("top");
    fieldName.setAttribute('value',top);
	fieldName.value=top;			
	
	form.submit()   

}

function setCurrentPageWithBoxes(page){
var liste = document.getElementsByName('avaBox');
	var corb = [];
	var corbU= []
	var i = 0;
	var m = 0;
	for (var k=0; k<liste.length;k++){
		if (liste[k].checked){
			corb[i]=liste[k].value
			i++;
		}
		else{
		corbU[m]=liste[k].value
			m++;		
		}
	}
	checkStr = corb.join(selectionSeparator);
	uncheckStr = corbU.join(selectionSeparator);
	
	var fieldName = getFieldId("checkedBoxes");
	    fieldName.setAttribute('value',checkStr);
		fieldName.value=checkStr;
		
	var fieldName = getFieldId("uncheckedBoxes");
	    fieldName.setAttribute('value',uncheckStr);
		fieldName.value=uncheckStr;
		
	var fieldName = getFieldId("cpage");
	    fieldName.setAttribute('value',page);
		fieldName.value=page;

	var form = getFieldId("currentPageWithBoxes");
	form.submit()   ;
	
}


function getMetaListDetails(form){	
	corbs = getMetaFormatedSelection();
	corbStr = corbs[0]
	corbUstr= corbs[1]


		displayLoadingGif();
		var fieldName = getFieldId("corbStr");
	    fieldName.setAttribute('value',corbStr);
		fieldName.value=corbStr;	
		
		var fieldName = getFieldId("corbUstr");
	    fieldName.setAttribute('value',corbUstr);
		fieldName.value=corbUstr;
		
		
		form.submit()   ;
	
}

function getBrowse(form,browser){
	displayLoadingGif();
	
	var fieldName2 = getFieldId("browser");
	fieldName2.setAttribute('value',browser);
	fieldName2.value=browser;	
	
	fieldName2 = getFieldId("alphabetical");
	fieldName2.setAttribute('value','');
	fieldName2.value='';
	
	form.submit()  ; 
}


function expandCollapseList(ulID){
	var elt = getFieldId(ulID);
	if (elt.style.display == 'block'){
		elt.style.display="none";
	}
	else{
		elt.style.display="block";
	}
}

function dlListDetails(form){
	corbs = getFormatedSelection();
	corbStr = corbs[0]
	corbUstr= corbs[1]
	if (corbStr=="empty") return alert("Check at least one box");

		var fieldName = getFieldId("corbStrDL");
	    fieldName.setAttribute('value',corbStr);
		fieldName.value=corbStr;
		
		var fieldName = getFieldId("corbUstrDL");
	    fieldName.setAttribute('value',corbUstr);
		fieldName.value=corbUstr;	
		form.submit()   

}





function getFullMetaList(form,what){
	displayLoadingGif();
	
	var fieldName2 = getFieldId("what");
	fieldName2.setAttribute('value',what);
	fieldName2.value=what;	

	form.submit()  ; 

}

function displayLoadingGif(){
	var imageDiv = getFieldId("loading");
	var image = getFieldId("loadingGif");

	if (imageDiv.style.display=="block"){
		document.getElementById("loading").style.display="none";
		var content = getFieldId( 'loading' ).innerHTML
		document.getElementById("page").style.backgroundImage = "url(/botany/static/images/bg.jpg)";
		}
	else{
	    document.getElementById('loading').style.display ="block";
	    var content = getFieldId( 'loading' ).innerHTML
		document.getElementById("page").style.backgroundImage = "url(/botany/static/images/bgbg.jpg)";
		
		}
	getFieldId( 'loading' ).innerHTML= content;
		
}

function seeSelectionMap(form){
	corbs = getFormatedSelection();
	corbStr = corbs[0]
	corbUstr= corbs[1]
	if (corbStr=="empty") return alert("Check at least one box");

		var fieldName = getFieldId("corbMap");
	    fieldName.setAttribute('value',corbStr);
		fieldName.value=corbStr;	
		
		var fieldName = getFieldId("corbUmap");
	    fieldName.setAttribute('value',corbUstr);
		fieldName.value=corbUstr;	
		form.submit()   

}

function seeRecordBasis(){
document.getElementById("recordBasis").style.display="block"; 
window.location.href='/botany/search/units/preview/seeRecordBasis'
}


function surveille(touche){
  if (document.title.indexOf("advanced") != -1){
    if(touche==13){
    	var textarea = getFieldId('textarea');
    	if (textarea.value != "" && getFieldId('fieldList').value != ""){
    		addField('frm');
    	}
    }
  }
  return;
}

function performFuzzySearch(){
Check = confirm("The expanded search can take a while.\n Do you want to go further?");
if(Check == true) window.location.href='/botany/search/units/preview/performFuzzySearch';
}
function explainFuzzy(){
elt = getFieldId('fuzzyNameList');
if (elt.style.display == 'block')
	elt.style.display='none';
else{ elt.style.display='block'; }
}

function expandThesaurus(){
elt =getFieldId('thesaurus_options');
if (elt.style.display == 'block')
	elt.style.display='none';
else{ elt.style.display='block'; }
}

function enableSuggestion(){
elt=getFieldId('sugImg');
if (elt.style.backgroundColor.indexOf('204') == -1 && elt.style.backgroundColor.indexOf('ccc') == -1){
	elt.style.backgroundColor = "#ccc";
	onOff = 'OFF'	
	document.getElementById("searchresults").style.display = "none";
	if (document.getElementById("unitName") != null) document.getElementById("unitName").setAttribute("autocomplete","on");
	else document.getElementById("textarea").setAttribute("autocomplete","on");
	}
else {elt.style.backgroundColor = "transparent"; onOff = "ON";
	if (document.getElementById("unitName") != null) document.getElementById("unitName").setAttribute("autocomplete","off");
	else document.getElementById("textarea").setAttribute("autocomplete","off");}

title= elt.title.split(":")[0]+":"+onOff;
elt.title=title;
elt.setAttribute('title',title);
}
function enableExpendedSearch(){
activated = getFieldId('thesaurusActivated');
thesauImg=getFieldId('thesauImg');
quicksearch = getFieldId('straight');

if (activated.value.toString() == 'True'){
activated.value='False';
thesauImg.className='thesauImgOff';
thesauImg.title='Click to activate the expanded search';
thesauImg.setAttribute('title','Click to activate the expanded search');
if (getFieldId('advanced'))
quicksearch.setAttribute('onclick',"javascript:getFieldId('unitFrm').action='/botany/search/units/advancedSearch/performAdvancedSearchStraight'; javascript:getFieldId('frm').submit(); ");
else
quicksearch.setAttribute('onclick',"javascript:getFieldId('unitFrm').action='/botany/search/units/simpleSearch/queryStraight'; javascript:getFieldId('unitFrm').submit(); ");
quicksearch.setAttribute('src',"/botany/static/images/straight.png");
quicksearch.setAttribute('onmouseover',"this.style.cursor='pointer';");
}
else{
activated.value='True';
thesauImg.className='thesauImgOn';
thesauImg.title='Click to deactivate the expanded search';
thesauImg.setAttribute('title','Click to deactivate the expanded search');
if (getFieldId('advanced'))
quicksearch.setAttribute('onclick',"javascript:getFieldId('unitFrm').action='/botany/search/units/advancedSearch/performAdvancedSearch'; javascript:getFieldId('frm').submit(); ");
else
quicksearch.setAttribute('onclick',"javascript:getFieldId('unitFrm').action='/botany/search/units/simpleSearch/query1'; javascript:getFieldId('unitFrm').submit();");
quicksearch.setAttribute('src',"/botany/static/images/straightD.png");
quicksearch.setAttribute('onmouseover',"this.style.cursor='';");
}
}

function dlAllFromCache(nbResults){
Check = confirm("It can take a while and the size of the file can reach several Mb .\n Do you want to go further?");
if(Check == true) window.location.href='/botany/search/units/preview/dlAllFromCache';
}

// function makeVisible(){	
// 	root=getFieldId('frm');
// 	var children = root.getElementsByTagName('input'); 							
// 	var len = children.length;
// 	for(var node = 0; node< len; node++){							
// 		children[node].style.display = "inline";
// 	}
// 	children = root.getElementsByTagName('img'); 							
// 	len = children.length;
// 	for(var node = 0; node< len; node++){							
// 		children[node].style.display = "inline";
// 	}
// }

function clearForm(){
	var trouve=0;
	root=getFieldId('formTable');
	var grandparents = root.getElementsByTagName('tr'); 
	var nbGParents = grandparents.length;
	for(var pn=0;pn<nbGParents;pn++){
		while (pn<nbGParents && grandparents[pn].id.indexOf('start') ==-1 && trouve ==0){
			pn++;
			}
		if (pn > nbGParents) break;
		if (grandparents[pn].id.indexOf('start') !=-1)
			trouve=1;
		var children = grandparents[pn].getElementsByTagName('input'); 							
		var len = children.length;
		for(var node = 0; node< len; node++){							
			if (children[node].id.indexOf('value') != -1) {
				children[node].value="";
				children[node].setAttribute("value","");
			}
// 			children[node].style.display = "none";
			
		}
// 		children = grandparents[pn].getElementsByTagName('img'); 							
// 		len = children.length;
// 		for(var node = 0; node< len; node++){							
// 			children[node].style.display = "none";
// 		}
	}
	var grandparents = root.getElementsByTagName('tr'); 
	var nbGParents = grandparents.length;
	for(var pn=0;pn<nbGParents;pn++){
		var children = grandparents[pn].getElementsByTagName('input'); 
		var len = children.length;
		for(var node = 0; node< len; node++){							
			if (children[node].id.indexOf('value') != -1) {
				children[node].value="";
				children[node].setAttribute("value","");
			}
		}
	}
// 	for (var j=0; j<document.getElementsByTagName("select").length; j++ ) {
// 			liste = document.getElementsByTagName("select")[j];
// 			if (liste.name == "field")
// 				for ( var k=0; k<liste.options.length; k++){
// 					liste.options[k].removeAttribute('disabled');
// 					liste.options[k].disabled = null;
// 				}
// 		}	
}

function searchNamesConcept(){
	var liste = document.getElementsByTagName('input');
	var selection =[];
	var notselection=[];
	var s = 0;var n=0;
	for (var k=0; k<liste.length;k++){
		if (liste[k].checked && liste[k].name.indexOf('rel_') ==-1 && liste[k].name.indexOf('refBox') ==-1 && liste[k].type =="checkbox"){
			val = liste[k].value
			if (val.indexOf('<b>') != -1)
				val = (val.split('<b>'))[0]
			val = val.split("/");
	        val = val.join("%2F");
	        val = val.split(",");
	        val = val.join("%2C");
	        val = val.split("(");
	        val = val.join("~$par$~");
	        val = val.split(")");
	        val = val.join("~$parf$~");
			selection[s] = val;
			s ++;
		}else{
			if (!liste[k].checked && liste[k].name.indexOf('rel_') ==-1 && liste[k].name.indexOf('refBox') ==-1 && liste[k].type =="checkbox"){
			val = liste[k].value
			if (val.indexOf('<b>') != -1)
				val = (val.split('<b>'))[0]
				val = val.split("/");
				val = val.join("%2F");
				val = val.split(",");
				val = val.join("%2C");
				val = val.split("(");
		        val = val.join("~$par$~");
		        val = val.split(")");
		        val = val.join("~$parf$~");
			notselection[n] = val;
			n ++;
		}}
	}
    
    var form = getFieldId("form");
	var boxField = getFieldId("box");
	boxField.setAttribute('value','taxonBox');
    boxField.value='taxonBox';	
    
	form.action="/botany/search/units/syncon/search/"
	selectionStr = selection.join(selectionSeparator);
	var fieldName = getFieldId("listStr");
	fieldName.setAttribute('value',selectionStr);
   	fieldName.value=selectionStr;	

	notselectionStr = notselection.join(selectionSeparator);
	var notfieldName = getFieldId("withoutlistStr");
	notfieldName.setAttribute('value',notselectionStr);
   	notfieldName.value=notselectionStr;	
	
	if (s > 0)
		form.submit();
	else
	alert("Check at least one box");
}

function expColl(elt){
	
	var count = 0;
	var toCollapse = [];
	var wrapper = elt.parentNode;
	if (wrapper.tagName !='DIV') wrapper=wrapper.parentNode;
	var child1 = wrapper.firstChild;
	while(child1){	
		if(child1.tagName == 'UL'){
			if (child1.style.display != 'none')
			child1.style.display='none';
			else
			child1.style.display='block';	
			
		}
		else{
			try{
				var child2 = child1.firstChild;
if(child2.tagName == 'IMG' ){
			url=child1.innerHTML
			if (url.indexOf('relations.png') !=-1)url = url.replace("relations.png","relationsF.png")
			else url=url.replace("relationsF.png","relations.png")
			child1.innerHTML=url;
		}
}
				catch(e){}}
 		child1 = child1.nextSibling;
}}