/*
   Copyright (C) 2009 
   All rights reserved.

   The computer program(s) herein are protected by copyright.
   The program(s) may be used and/or copied only respecting the 
   written terms and conditions stipulated in the agreement/contract
   under which the program(s) have been supplied.
*/

if (history.navigationMode)history.navigationMode = 'compatible'; //for Opera compatibility
jQuery(document).ready( 
                    function(){
                      document.browseBy.reset(); //reset browseBy selects, useful in case the Back button is clicked                                                                      
					  /*fixed copy of the initial dropdown that will be filtered*/
					  _COPY_INSTR_SELBOX=document.browseBy.instruments.cloneNode(true); 
					  _COPY_DATA_SELBOX =document.browseBy.datasetsId.cloneNode(true);
					  _COPY_INSTR_SELBOX.style.display="none";
					  _COPY_DATA_SELBOX.style.display="none";
					  _COPY_INSTR_SELBOX.setAttribute("id", "copyInstr");
					  _COPY_DATA_SELBOX.setAttribute("id", "copyData");
					  document.browseBy.appendChild(_COPY_INSTR_SELBOX);
					  document.browseBy.appendChild(_COPY_DATA_SELBOX);					                                              
                    }				
                 );

	
/*inserts a non duplicate option */	
function insertOption(text,value,selBox) {
	//check if the option already exists, if not insert it
	var i;	
	var bDuplicate=false;
	for(i=0;i<selBox.options.length;i++){
		if(selBox.options[i].value == value ){
			bDuplicate=true;	
		}		
	}
	if(!bDuplicate){
		selBox.options[selBox.options.length] = new Option(text,value);
	}
}

/*copies all options from the select source to destination*/
function copyAllOptions(source, destination){
	destination.options.length=0;
	for(i=0; i<source.options.length;i++){					
		insertOption(source.options[i].text,source.options[i].value,destination);						
	}
}

/*given a selected value and a select to be filtered, filters the options inside the select*/
function filterOptions(chosen,selBox){
	var selboxId=selBox.getAttribute("id");
	var copyDataSelbox=document.browseBy.copyData;
	var datasetIdSelbox = document.browseBy.datasetsId;
		
	/*If a mission has been selected, both Instruments and Datasets have to be filtered*/
	if (selboxId=="instruments"){
		selBox.options.length=0;//selbox is emptied
		var copyInstrSelbox = document.browseBy.copyInstr;//get the copy of Instruments				
		/*if the default option is chosen (-- Mission), show all options */
		if(chosen=='0'){
			copyAllOptions(copyInstrSelbox, selBox);//clone Instruments
			copyAllOptions(copyDataSelbox, datasetIdSelbox);//clone Datasets						
		}else{
			insertRelatedInstrumentsAndDatasets(chosen,copyInstrSelbox,selBox);//insert related Instruments
			//insertRelatedDatasets, If it is a new one, all Instr are inserted
			if(datasetIdSelbox.length==0){
				copyAllOptions(copyDataSelbox, datasetIdSelbox);//clone Datasets
			}			
		}		
	}/*If an Instrument has been selected only Datasets have to be filtered*/
	else if (selboxId=="datasetsId"){
		datasetIdSelbox.options.length=0;//reset selbox to zero
		insertOption('-- Dataset ID','0',datasetIdSelbox);
		insertRelatedDatasets(chosen,copyDataSelbox,datasetIdSelbox);
		//if selbox is 1, the default
		if(datasetIdSelbox.length==1){
			copyAllOptions(copyDataSelbox, datasetIdSelbox);//clone Datasets
		}				
	}	
}

/*given a chosen Instrument or DAP Version, populates datasets dropdown list*/
function insertRelatedDatasets(chosen,sourceSelbox,destinationSelbox){
	//start from simple case
	var i,j;	
	var instrumentsArray = new Array();
	if(chosen=='0'){
		copyAllOptions(sourceSelbox, destinationSelbox);
	}else{		 
		for(i=0; i<sourceSelbox.options.length;i++){			
			instrumentsArray = new Array();
			instrumentsArray=sourceSelbox.options[i].className.split(';'); //array of instruments/DAP_Version
			for(j=0;j<instrumentsArray.length;j++){//scan all instruments related to the dataset
				if(instrumentsArray[j]==chosen){
					insertOption(sourceSelbox.options[i].text,sourceSelbox.options[i].value, destinationSelbox);
				}
			}
		}
	}
}

/*given a chosen mission, populate instruments and datasets dropdown list*/
function insertRelatedInstrumentsAndDatasets(chosen,sourceSelbox,destinationSelbox){
	var i,j,k,h;
	var missionsArray = new Array();
	var missionsInstrumentsarray = new Array();
	var datasetIdSelbox = document.browseBy.datasetsId;
	datasetIdSelbox.options.length=0;
	var copyDataSelbox=document.browseBy.copyData;
	//Insert Instrumetns and related datasets: the relationship is in the className attribute
 	var bNoInstrAssociated=true;
 	insertOption('-- Instrument','0',destinationSelbox);
	for(i=0; i<sourceSelbox.options.length;i++){
		//IF class attribute contains the string insert the instrument and related options
		//array
		missionsArray=sourceSelbox.options[i].className.split(';'); //array of missions
		for(j=0;j<missionsArray.length;j++){
			if(missionsArray[j]==chosen){			
				insertOption(sourceSelbox.options[i].text,sourceSelbox.options[i].value, destinationSelbox);//insert the Instrument			
				//If is the first dataset, insert the default Option (-- Dataset ID)
				if(datasetIdSelbox.options.length==0){
					insertOption('-- Dataset ID','0',datasetIdSelbox);
				}
				insertRelatedDatasets(sourceSelbox.options[i].value,copyDataSelbox,datasetIdSelbox);//insert dataset related to the Instrument
				bNoInstrAssociated=false;//is not a new mission because there are instruments associated
			}
		}	
	}
	//Insert other datasets. Remember the Option 0
	for(k=0; k<copyDataSelbox.options.length;k++){
		missionsInstrumentsarray=copyDataSelbox.options[k].className.split(';');
		for(h=0;h<missionsInstrumentsarray.length;h++){
			if(missionsInstrumentsarray[h]==chosen){
				//If is the first dataset, insert the default Option (-- Dataset ID)
				if(datasetIdSelbox.options.length==0){
					insertOption('-- Dataset ID','0',datasetIdSelbox);
				}
				insertOption(copyDataSelbox.options[k].text,copyDataSelbox.options[k].value, datasetIdSelbox);
				bNoInstrAssociated=false;//is not a new mission
			}
		}		
	}
	
	//Sort datasets
	var soptions = jQuery.makeArray(datasetIdSelbox.options).sort(function(a,b) 
	   {
		 return (a.innerHTML > b.innerHTML) ? 1 : -1;
	   });
  
	//jQuery('select#datasetsId').attr('selectedIndex', 0);
	//jQuery('select#datasetsId').val('0'); 	
	jQuery('select#datasetsId').html(soptions);
	jQuery('select#datasetsId').val('0'); 	
	
	//If is a new mission(no Instrument associated yet) insert all Instruments and Datasets
	if(bNoInstrAssociated){
		/*the selected mission is new and not yet inserted in this javascript. all instruments have to be inserted*/			
		copyAllOptions(sourceSelbox,destinationSelbox);//clone Instruments
		/*reset to zero and reinsert datasets, In the case a mission is reset after having selected an Instrument*/
		copyAllOptions(copyDataSelbox, datasetIdSelbox);								
	}	
}

