Portal.Portlet.ResultsSearchController = Portal.Portlet.extend({

	init: function(path, name, notifier) {
		var oThis = this;
		console.info("Created ResultsSearchController");
		this.base(path, name, notifier);
	},	
		
	send: {
		'ResetSendTo': null,
		'LastQueryKey': null,
		'needSavedSelectedItemCount': null
	},
		
	listen: {
		
		/* messages from message bus*/
		
		'Cmd' : function(sMessage, oData, sSrc) {
			this.ProcessCmd(sMessage, oData, sSrc);
		},		
		
		// when result citations are selected, the list of selected ids are intercepted here
		'SelectedItemCountChanged' : function(sMessage, oData, sSrc){
			Portal.Portlet.ResultsSearchController.selectedItemCount = oData.count * 1;
		},
		
		// for SendTo File (processing after getting any initial selected item count from DbConnector)
		'SavedSelectedItemCount' : function(sMessage, oData, sSrc) {
			var SavedSelectedItemCount = oData.count;
			
			this.ProcessSavedSelectedItemCount(SavedSelectedItemCount, sMessage, oData, sSrc);
		},
		
		'RunLastQuery' : function(sMessage, oData, sSrc){
			if (this.getInput("RunLastQuery")){
				this.setValue ("RunLastQuery", 'true');
			}
		}
		
	},//listen
	
	ProcessCmd: function(sMessage, oData, sSrc){
    	Portal.Portlet.ResultsSearchController.cmd = oData.cmd;
	
		if (oData.cmd == 'Link'){
			if (this.getInput("LastQueryKey")){
				this.send.LastQueryKey({'qk': this.getValue("LastQueryKey")});
			}
		}
		else if (oData.cmd == 'File'){
			// asks for initial selected items count from dbconnector
			this.send.needSavedSelectedItemCount();
		}
		else if (oData.cmd == 'My NCBI Collections'){
			// asks for initial selected items count from dbconnector
			this.send.needSavedSelectedItemCount();
		}
	}, // ProcessCmd
	
	ProcessSavedSelectedItemCount: function(SavedSelectedItemCount, sMessage, oData, sSrc){
	    // get the count of items about to be processed 
	    var Count;
		if (Portal.Portlet.ResultsSearchController.selectedItemCount > 0){
			Count = Portal.Portlet.ResultsSearchController.selectedItemCount;
		}
		else if (SavedSelectedItemCount > 0){
			Count = SavedSelectedItemCount;
		}
		else{
			Count = this.getValue("ResultCount");
		}
		
	    if (Portal.Portlet.ResultsSearchController.cmd == 'My NCBI Collections'){
	        this.ProcessSavedItemsForCollections (Count, oData);
	    }
		else if (Portal.Portlet.ResultsSearchController.cmd == 'File'){
			this.ProcessSavedItemsForFile (Count, oData);
		}
	}, // ProcessSavedSelectedItemCount
	
	ProcessSavedItemsForCollections: function(Count, oData){
		// show message if item count is above limit
		if(Count > Portal.Portlet.ResultsSearchController.upperLimit){
		    // create message
			var message = "The maximum number of records that can be added to a My NCBI Collection is " 
			+ Portal.Portlet.ResultsSearchController.upperLimitText 
			+ ".\n"
			+ "Are you sure you want to save the first "
			+ Portal.Portlet.ResultsSearchController.upperLimitText
			+ " records to a Collection?\n";
			
			// if user confirms they want to add items, let them proceed to MY NCBI, otherwise cancel
    		if(confirm(message)){
    			Portal.requestSubmit(); 
    		}
    		else{
    			this.send.ResetSendTo({'value': ''});
    		}
		}
		else{
		    Portal.requestSubmit(); 
		}
		
	}, //ProcessSavedItemsForCollections
	
	ProcessSavedItemsForFile: function(Count, oData){
	    
		console.info("Count before send to file: " + Count);
		if (Count > Portal.Portlet.ResultsSearchController.upperLimit){
			if(confirm('Are you sure you want to download ' + Count + ' records?')){
				Portal.requestSubmit(); 
			}
			else{
				this.send.ResetSendTo({'value': ''});
			}
		}
		else{
			Portal.requestSubmit(); 
		}
	} //ProcessSavedItemsForFile

},
{
	selectedItemCount: 0,
	upperLimit: 5000,
	upperLimitText: '5,000',
	cmd: ''
});

Portal.Portlet.Unists_ResultsSearchController = Portal.Portlet.ResultsSearchController.extend({
        init: function(path, name, notifier) {
            console.info("Created derived ResultsSearchController - Unists");
            this.base(path, name, notifier);
        }
    });
Portal.Portlet.Entrez_DisplayBar = Portal.Portlet.extend({

	init: function(path, name, notifier) {
		console.info("Created DisplayBar");
		this.base(path, name, notifier);
		
		// save the original display/presentation value when page loads
		Portal.Portlet.Entrez_DisplayBar.originalPresentation = this.getValue("LastPresentation");
		console.info("Original Presentation is: " + Portal.Portlet.Entrez_DisplayBar.originalPresentation);
		
	},
	
	
	send: {
   
   		'PresentationChange': null,
		'TabCmd': null,
		'Cmd': null, 
		'PageSizeChanged': null
	},
	
	
	
	listen: {
		
		/* browser events */
			
		"sPresentation<change>": function(e, target, name){
			//get attribute values of selected option
			var dbfrom = target.options[target.selectedIndex].getAttribute('dbfrom');
			var dbto = target.options[target.selectedIndex].getAttribute('dbto');
			var cmd = target.options[target.selectedIndex].getAttribute('cmd');
			var readablename = target.options[target.selectedIndex].getAttribute('readablename');
			
			//call function to process
			this.ProcessPresentationChange(dbfrom, dbto, cmd, readablename, e, target, name);
		},
		
		
		"sPageSize<change>": function(e, target, name){		
			this.ProcessPageSizeChange(e, target, name);
		},
		
		
		"sSort<change>": function(e, target, name){
			this.ProcessSortChange(e, target, name);
		},
		
		
		"sSendTo<change>": function(e, target, name){
			var sendto = target.value;
			this.setValue("SendTo", sendto);
			
			this.SendToChanged(sendto, e, target, name); 
		},
		
		
		/* messages from message bus*/
		
		'Cmd' : function(sMessage, oData, sSrc) {
		    this.ProcessCmdChange(sMessage, oData, sSrc);
		},
		
		'ResetSendTo' : function(sMessage, oData, sSrc) {
			this.setValue("SendTo", oData.value);
				// reset sendto in selector
			var sSendToInputs = this.getInputs("sSendTo");
			for (var j = 0; j < sSendToInputs.length; j++){
				sSendToInputs[j].value = '';
			}
		},
		
		'newPresentation' : function(sMessage, oData, sSrc) {
			this.setValue("Presentation", oData.value);
		},
		
		'newPageSize' : function(sMessage, oData, sSrc) {
			this.setValue("PageSize", oData.value);
		},
		
		'newSort' : function(sMessage, oData, sSrc) {
			this.setValue("Sort", oData.value);
		}
	
	},
	
	ProcessPresentationChange: function(dbfrom, dbto, cmd, readablename, e, target, name) {		
		//send Cmd
		this.send.Cmd({
			'cmd': cmd
		});
		
		//Send link information and cmd
		this.send.PresentationChange({
			'dbfrom': dbfrom,
			'dbto': dbto,
			'linkname': target.value,
			'readablename': readablename
		});	
		
		// set Presentation
		this.setValue("Presentation", target.value);
	},
	
	
	ProcessPageSizeChange: function(e, target, name){
		//send Cmd
		this.send.Cmd({
			'cmd': target.getAttribute('cmd')
		});	
		//send PageSizeChanged
		this.send.PageSizeChanged({
			'size': target.value
		});	
		//set PageSize
		this.setValue("PageSize", target.value);
	},
	
	
	ProcessSortChange: function(e, target, name){
		//send Cmd
		this.send.Cmd({
			'cmd': target.getAttribute('cmd')
		});	
		//set Sort
		this.setValue("Sort", target.value);
	},
	
	
	SendToChanged: function(sendto, e, target, name) {
		if(sendto == 'Remove from Clipboard'){
			if(confirm('Are you sure you want to delete these items from Clipboard?')){
				this.send.Cmd({'cmd': sendto});
				document.forms[0].submit();
			}
			else{
				this.setValue("SendTo", '');
				// reset sendto in selector
				var sSendToInputs = this.getInputs("sSendTo");
				for (var j = 0; j < sSendToInputs.length; j++){
					sSendToInputs[j].value = '';
				}
			}
		}
		else if(sendto == 'File'){
			this.send.Cmd({'cmd': sendto});
		}
		else if(sendto == 'Mail'){
			this.send.Cmd({'cmd': sendto});
			this.send.TabCmd({'tab': 'E-mail'});
			document.forms[0].submit();
		}
		else if(sendto == 'My NCBI Collections'){
			this.send.Cmd({'cmd': sendto});
		}
		else if(sendto == ''){
			this.send.Cmd({'cmd': sendto});
		}
		else{
			this.send.Cmd({'cmd': sendto});
			document.forms[0].submit();
		}
	},
	
	'ProcessCmdChange': function(sMessage, oData, sSrc){
	    if (oData.cmd == 'PageChanged'){
			/* Reset Send To on page change to avoid sendto having previous value due to 
			using browser back button after viewing text format reports.*/
			this.setValue("SendTo", '');
				// reset sendto in selector
			var sSendToInputs = this.getInputs("sSendTo");
			for (var j = 0; j < sSendToInputs.length; j++){
				sSendToInputs[j].value = '';
			}
		}
		
		this.BackButtonCompatibilityForCmdChange(oData.cmd);
	},
	
	'BackButtonCompatibilityForCmdChange': function(cmd){
	    // for back button compatibility, keep presentation to old value for any cmds 
			// except when cmd indicates value was changed from DisplayBar.
		if (cmd != 'DisplayChanged' && cmd != 'Link' && cmd != 'SendMail'){
			// set Presentation
			this.setValue("Presentation", Portal.Portlet.Entrez_DisplayBar.originalPresentation);
		}
	}
},
{
	originalPresentation: ''
});


Portal.Portlet.Unists_DisplayBar = Portal.Portlet.Entrez_DisplayBar.extend({

	init: function(path, name, notifier) {
		console.info("Created inherited DisplayBar");
		this.base(path, name, notifier);
	}
});


Portal.Portlet.MessageBar = Portal.Portlet.extend ({
	init: function (path, name, notifier)
	{
		this.base (path, name, notifier);
	},
	
	send: {
		"TabCmd": null,
		"Cmd": null,
		"Term": null
	},
	
	listen: {
		//upon clicking 'see details', sends name of details tab.
		"Details<click>":  function(e, target, name) {
			this.send.TabCmd({'tab': this.getValue("Details:tab")});
		},
		
		"CorrectedQuery<click>": function(e, target, name) {
		    this.send.Term ({'term': target.getAttribute('term')})
		    this.send.Cmd ({'cmd': 'CorrectSpelling'})
		}
	}
});


Portal.Portlet.SearchController = Portal.Portlet.extend({
   
   init: function(path, name, notifier) {
      console.info("Created SearchController");
      this.base(path, name, notifier);
   },
   
   listen: {
   
		'QueryKey' : function(sMessage, oData, sSrc) {
			this.ReceivedQueryKey(sMessage, oData, sSrc);
		},
        
        'scTerm' : function(sMessage, oData, sSrc) {
			this.ReceivedTerm(sMessage, oData, sSrc);
        }
    },
   
    ReceivedQueryKey : function (sMessage, oData, sSrc){
       this.setValue('QueryKey', oData.qk);
    },
   
    ReceivedTerm : function (sMessage, oData, sSrc){
       this.setValue('Term', oData.term);
    }
});

Portal.Portlet.CommandTab = Portal.Portlet.extend ({
	init: function (path, name, notifier)
	{
		 console.info ("Created CommandTab");
		this.base (path, name, notifier);
	},
	
	send: {
		"Cmd": null,
		"TabCmd": null
	},
	
	listen: {
		//upon click on tab, sends name of tab that was clicked,
		// and the command that indicates that a tab was clicked.
		"Tab<click>":  function(e, target, name) {
			this.send.TabCmd({'tab': target.getAttribute("tab")});
			this.send.Cmd({'cmd': 'CommandTabClicked'});
		},
		
	
		"LimitCheckBox<click>":  function(e, target, name) {
			console.info ("LimitsActive: " + this.getValue("LimitsActive"));
			if (target.checked){
				this.setValue("LimitsActive", 'true');
				console.info ("LimitsActive: " + this.getValue("LimitsActive"));
			}
			else{
				this.setValue("LimitsActive", 'false');
				console.info ("LimitsActive: " + this.getValue("LimitsActive"));
			}
				
		},
		
		'PreserveTabCmd' : function(e, target, name) {
			this.send.TabCmd({'tab': this.getValue("LastTabCmd")});	
		}
	}
});

Portal.Portlet.SearchBar = Portal.Portlet.extend ({
  
	init: function (path, name, notifier) {
		console.info ("Created SearchBar"); 
		this.base (path, name, notifier);

        Portal.Portlet.SearchBar.originalTerm = this.getValue("Term");
        Portal.Portlet.SearchBar.originalDb = this.getValue("Db");
        
        // listen for autocomplete selection event 
        var AutoCompSelectFnc = function(){ 
            Portal.$send('AutoCompSelect'); 
        } 
        jQuery("#search_term").bind("ncbiautocompleteenter", AutoCompSelectFnc ).bind("ncbiautocompleteoptionclick", AutoCompSelectFnc ); 

	},

	// Define message sender methods here.
	// If you just want to send a message the standard way, simply
	// supply "null" as the implementation. 
	// 
	send: {
		"Cmd": null,
		"Term": null,
		"TabCmd": null,
		"DbChanged": null, 
		"SearchBarSearch": null,
		"AutoCompSelect": null
	},

	// Define messages and events
	listen: {

		// Global message bus
		'IndexLogicalOp': function(sMessage, oData, sSrc) {
		    this.ReceivedIndexLogicalOp(sMessage, oData, sSrc);
		},

		'TermLogicalOp': function(sMessage, oData, sSrc) {
		    this.ReceivedTermLogicalOp(sMessage, oData, sSrc);
		},

		'LimitsGoClicked': function(sMessage, oData, sSrc) {
            this.ReceivedLimitsGoClicked(sMessage, oData, sSrc);
		},

		'Cmd': function(sMessage, oData, sSrc) {
		    this.ReceivedCmd(sMessage, oData, sSrc);
		},
		
		'AppendTerm': function(sMessage, oData, sSrc) {
		    this.ReceivedAppendTerm(sMessage, oData, sSrc);
		},
		
		'ClearSearchBarTerm': function(sMessage, oData, sSrc) {
			this.setValue("Term", '');
		}, 
		
		'AutoCompleteControl': function(sMessage, oData, sSrc) {
		    this.ChangeAutoCompleteState(sMessage, oData, sSrc);
        },
        
        'AutoCompSelect': function(sMessage, oData, sSrc) {
		    this.AutoCompleteOptionSelected();
        },
		
		// Browser events
		"Term<keypress>": function(event, target, name) {
		    this.TermKeyPress(event, target, name);
		},
      
		// Cmd is set to Go, so ResultsView of other database can choose component based 
		// on value of Cmd. The existing search term is also passed down.
		"Go<click>": function(e, target, name) {
            this.GoClick(e, target, name);
		},
		
		"Preview<click>": function(e, target, name) {
		 	this.PreviewClick(e, target, name);
		},
	  
		// to indicate that the db field was the submitter, this will also set future 
		// TabCmd to current TabCmd and keep the existing search term.
		"Db<change>": function(e, target, name) {
		    this.DbChange(e, target, name);
		},
		
		// On Clear button click, set focus to search box and clear the term
		"Clear<click>": function (e, target, name) {
		    this.ClearClick(e, target, name);
		}
    
		
	}, //listen
	
	/* other functions */
	
	'ReceivedIndexLogicalOp': function(sMessage, oData, sSrc){
		var sbTerm = this.getValue("Term");
		var oTerm = this.getInput("Term");
		var newValue = oData.key;

		console.info ("In Indexlogocalop in SearchBar");

		if (sbTerm != "") {
			newValue = sbTerm + ' ' + oData.op + ' ' + oData.key;
		}
		this.setValue("Term", newValue);
		if (oTerm) {
		   oTerm.focus();
		}
	},
	
	'ReceivedTermLogicalOp': function(sMessage, oData, sSrc){
	    var sbTerm = this.getValue("Term");
		var bNotBlank = sbTerm != "";
		
		if (bNotBlank)
		   sbTerm = '(' + sbTerm + ') ' + oData.op + ' (';
		
		sbTerm += oData.key;
		
		if (bNotBlank)
		   sbTerm += ')';
 
		this.setValue("Term", sbTerm);  
		this.send.Term({'term' : sbTerm});
		this.getInput("Term").focus();
	},
	
	'ReceivedLimitsGoClicked': function(sMessage, oData, sSrc){
		this.send.Cmd({ 'cmd' : this.getValue("Go:cmd") });
		this.send.Term({ 'term' : this.getValue("Term") });
		// for back button fix, when go is clicked, also send db in searchbar, for checking against dbconnector
		this.send.SearchBarSearch({ 'db' : this.getValue("Db") });
		Portal.requestSubmit();
	},
	
	'ReceivedCmd': function(sMessage, oData, sSrc){
	    if (oData.cmd == 'CommandTabClicked'){
	        this.send.Term({'term': this.getValue("Term") });
			// for back button fix, when Tab is clicked, also send db in searchbar
			if (this.getValue("Db")){
				this.send.DbChanged({ 'db' : this.getValue("Db") });
			}
		}
	},
	
	'ReceivedAppendTerm': function(sMessage, oData, sSrc){
	    var newTerm = Portal.Portlet.SearchBar.originalTerm;
	    if (Portal.Portlet.SearchBar.originalTerm != '' && oData.op != ''){
	        newTerm += ' ' + oData.op + ' ';
	    }
	    newTerm += oData.term;
	    //this.setValue("Term", newTerm); 
		this.send.Term({'term': newTerm });
		// for back button fix, send original db
		this.send.SearchBarSearch({ 'db' : Portal.Portlet.SearchBar.originalDb });
	},
	
	'TermKeyPress': function(event, target, name){
	    event = event || utils.fixEvent (window.event);
		if ((event.keyCode || event.which) == 13) 
		{
			// Emulate Go command.
    
			console.info ("In term keypress: CMD  in SearchBar");
			this.send.Cmd({ 'cmd' : this.getValue("Term:cmd") });

			// In History Tab, Term field should send command Preview
			if (this.getValue("Term:cmd") == 'Preview')
			{
				this.send.TabCmd({ 'tab' : this.getValue("Db:tab") });
			}
    
			console.info ("In term keypress : TERM in SearchBar");
			this.send.Term({ 'term': this.getValue("Term") });

		    // for back button fix, when go is clicked, also send db in searchbar, for checking against dbconnector
		    this.send.SearchBarSearch({ 'db' : this.getValue("Db") });
        
			event.returnValue = false;
			if (event.stopPropagation != undefined)
                  event.stopPropagation ();   
			if (event.preventDefault != undefined)
                  event.preventDefault ();   
			Portal.requestSubmit (); 
			return false;
		}
	},
	
	'GoClick': function(e, target, name){
	 	this.send.Cmd({ 'cmd' : this.getValue("Go:cmd") });
	   	this.send.Term({ 'term' : this.getValue("Term") });
		// for back button fix, when go is clicked, also send db in searchbar, for checking against dbconnector
	 	this.send.SearchBarSearch({ 'db' : this.getValue("Db") });
	},
	
	'PreviewClick': function(e, target, name){
        this.send.Cmd({ 'cmd' : 'Preview' });
	   	this.send.Term({ 'term' : this.getValue("Term") });
	   	this.send.TabCmd({ 'tab' : this.getValue("Preview:tab") });
		// for back button fix, when go is clicked, also send db in searchbar, for checking against dbconnector
	 	this.send.SearchBarSearch({ 'db' : this.getValue("Db") });
	},
	
	'DbChange': function(e, target, name){
	 	this.send.Cmd({ 'cmd' : this.getValue("Db:cmd") });
		console.info ("In DB SearchBar: " + this.getValue("Db:cmd"));
		this.send.TabCmd({ 'tab' : this.getValue("Db:tab") });
		this.send.DbChanged({ 'db' : this.getValue("Db") });
		this.send.Term({ 'term' : this.getValue("Term") });
		
		if (this.getValue("Db:cmd") == ''){
    		if (this.getValue("Term:suggest") == 'true'){
    		    // change to if the current database has a dictionary 
    		    this.EnableDisableAutocomplete();
            }
        }
	},
	
	'ClearClick': function(e, target, name){
	    this.setValue ("Term", "");
        var term = this.getInput ("Term");
        if (term) 
            term.focus ();
    },
    
    'ChangeAutoCompleteState': function(sMessage, oData, sSrc){
        this.setValue("Term:suggest", 'false');
        var site = document.forms[0]['p$st'].value;
        var resp = xmlHttpCall(site, this.realname, "AutoCompleteControl", {"ShowAutoComplete": 'false'}, this.receive, {}, this);
    },        
        
    'receive': function(responseObject, userArgs) {
    },
    
    'AutoCompleteOptionSelected': function(){
        /*if (this.getInput("AutoSuggestUsed")){
            this.setValue("AutoSuggestUsed", 'true');
        }*/
        
	    this.send.Cmd({ 'cmd' : this.getValue("Go:cmd") });
	   	this.send.Term({ 'term' : this.getValue("Term") });
		// for back button fix, when go is clicked, also send db in searchbar, for checking against dbconnector
	 	this.send.SearchBarSearch({ 'db' : this.getValue("Db") });
	 	Portal.requestSubmit(); 
	},
	
	'EnableDisableAutocomplete': function(){
	    var site = document.forms[0]['p$st'].value;
        var resp2 = xmlHttpCall(site, this.realname, "SetAutoCompleteDictionary", {"Db": this.getValue("Db")}, this.receiveDictionary, {}, this);
	},
	
	'receiveDictionary': function(responseObject, userArgs){ 
        try {
            // deserialize the string with the JSON object
            var response = '(' + responseObject.responseText + ')';
            var JSONobject = eval(response);
            
            var dict = JSONobject.Dictionary || "";
            
            // turn autocomplete off or on if database is changed in selector.
            if(dict != ''){
               jQuery("#search_term").ncbiautocomplete("option","isEnabled",true).ncbiautocomplete("option","dictionary",dict);
            }
            else{
               jQuery("#search_term").ncbiautocomplete("turnOff",true);    
            }
        }
        catch (e){
        
        }
    }
	
}, // portlet instance

{
	originalTerm: '',
	originalDb: ''
});

function EntrezSearchBarAutoComplCtrl(){
    Portal.$send('AutoCompleteControl');
}


Portal.Portlet.DbConnector = Portal.Portlet.extend({

	init: function(path, name, notifier) {
		var oThis = this;
		console.info("Created DbConnector");
		this.base(path, name, notifier);
		
		// reset Db value to original value on page load. Since LastDb is the same value as Db on page load and LastDb is not changed on
		// the client, this value can be used to reset Db. This is a fix for back button use.
		if (this.getValue("Db") != this.getValue("LastDb")){
		    this.setValue("Db", this.getValue("LastDb"));
		}
     
		// the SelectedIdList and id count from previous iteration (use a different attribute from IdsFromResult to prevent back button issues)
		Portal.Portlet.DbConnector.originalIdList = this.getValue("LastIdsFromResult");
		console.info("originalIdList " + Portal.Portlet.DbConnector.originalIdList);
		// if there is an IdList from last iteration set the count
		if (Portal.Portlet.DbConnector.originalIdList != ''){
			Portal.Portlet.DbConnector.originalCount = Portal.Portlet.DbConnector.originalIdList.split(/,/).length;
		}

		notifier.setListener(this, 'HistoryCmd', 
        	function(oListener, custom_data, sMessage, oNotifierObj) {
           		var sbTabCmd = $N(oThis.path + '.TabCmd');
           		sbTabCmd[0].value = custom_data.tab;
        	}
    		, null);
    
	},

	send: {
   		'SelectedItemCountChanged': null,
   		'newUidSelectionList': null,
   		'SavedSelectedItemCount': null
	},

	listen: {
	
		//message from Display bar on Presentation change 
		'PresentationChange' : function(sMessage, oData, sSrc){
			
			// set link information only if it exists
			if (oData.dbfrom){
				console.info("Inside PresentationChange in DbConnector: " + oData.readablename);
				this.setValue("Db", oData.dbto);
				this.setValue("LinkSrcDb", oData.dbfrom);
				this.setValue("LinkName", oData.linkname);
				this.setValue("LinkReadableName", oData.readablename);
			}
			//document.forms[0].submit();
		},
		
		// various commands associated with clicking different form control elements
		'Cmd' : function(sMessage, oData, sSrc){
			console.info("Inside Cmd in DbConnector: " + oData.cmd);
			this.setValue("Cmd", oData.cmd);
			
			// back button fix, clear TabCmd
			if (oData.cmd == 'Go' || oData.cmd == 'PageChanged' || oData.cmd == 'FilterChanged' || 
			oData.cmd == 'DisplayChanged' || oData.cmd == 'HistorySearch' || oData.cmd == 'Text' || 
			oData.cmd == 'File' || oData.cmd == 'Printer' || oData.cmd == 'Order' || 
			oData.cmd == 'Add to Clipboard' || oData.cmd == 'Remove from Clipboard' || 
			oData.cmd.toLowerCase().match('details')){
				this.setValue("TabCmd", '');
				console.info("Inside Cmd in DbConnector, reset TabCmd: " + this.getValue('TabCmd'));
			}

		},
		
		
		// the term to be shown in the search bar, and used from searching
		'Term' : function(sMessage, oData, sSrc){
			console.info("Inside Term in DbConnector: " + oData.term);
			this.setValue("Term", oData.term);
		},
		
		
		// to indicate the Command Tab to be in
		'TabCmd' : function(sMessage, oData, sSrc){
			console.info("Inside TABCMD in DbConnector: " + oData.tab);
			this.setValue("TabCmd", oData.tab);
			console.info("DbConnector TabCmd: " + this.getValue("TabCmd"));
		},
		
		
		// message sent from SearchBar when db is changed while in a Command Tab
		'DbChanged' : function(sMessage, oData, sSrc){
			console.info("Inside DbChanged in DbConnector");
			this.setValue("Db", oData.db);
		},
		
		// Handles item select/deselect events
		// Argument is { 'id': item-id, 'selected': true or false }
		'ItemSelectionChanged' : function(sMessage, oData, oSrc) {
			var sSelection = this.getValue("IdsFromResult");
			var bAlreadySelected = (new RegExp("\\b" + oData.id + "\\b").exec(sSelection) != null);
	       	var count =0;
	       	
			if (oData.selected && !bAlreadySelected) {
				sSelection += ((sSelection > "") ? "," : "") + oData.id;
			   	this.setValue("IdsFromResult", sSelection);
			   	if (sSelection.length > 0){
			   		count = sSelection.split(',').length;
			   	}
			   	this.send.SelectedItemCountChanged({'count': count});
			   	this.send.newUidSelectionList({'list': sSelection});
		   	} else if (!oData.selected && bAlreadySelected) {
				sSelection = sSelection.replace(new RegExp("^"+oData.id+"\\b,?|,?\\b"+oData.id+"\\b"), '');
		   	   	this.setValue("IdsFromResult", sSelection);
				console.info("Message ItemSelectionChanged - IdsFromResult after change:  " + this.getValue("IdsFromResult"));
			   	if (sSelection.length > 0){
			   		count = sSelection.split(',').length;
			   	}
				console.info("Message ItemSelectionChanged - IdsFromResult length:  " + count);   
				this.send.SelectedItemCountChanged({'count': count});
			   	this.send.newUidSelectionList({'list': sSelection});
		   	}
		},
				
		// FIXME: This is the "old message" that is being phased out.
		// when result citations are selected, the list of selected ids are intercepted here,
		// and notification sent that selected item count has changed.
		'newSelection' : function(sMessage, oData, sSrc){
		
			// Check if we already have such IDs in the list
			var newList = new Array();
			var haveNow = new Array();
			if(Portal.Portlet.DbConnector.originalIdList){
				haveNow = Portal.Portlet.DbConnector.originalIdList.split(',');
				newList = haveNow;
			}
			
			var cameNew = new Array();
			if (oData.selectionList.length > 0) {
				cameNew = oData.selectionList;
			}
			
			if (cameNew.length > 0) {
				for(var ind=0;ind<cameNew.length;ind++) {
					var found = 0;
					for(var i=0;i<haveNow.length;i++) {
						if (cameNew[ind] == haveNow[i]) {
							found = 1;
							break;
						}
					}
						//Add this ID if it is not in the list
					if (found == 0) {
						newList.push(cameNew[ind]);
					}
				}
			}
			else {
				newList = haveNow;
			}

				// if there was an IdList from last iteration add new values to old
			var count = 0;
			if ((newList.length > 0) && (newList[0].length > 0)){
				count = newList.length;
			}
			
			console.info("id count = " + count);
			this.setValue("IdsFromResult", newList.join(","));
			
			this.send.SelectedItemCountChanged({'count': count});
			this.send.newUidSelectionList({'list': newList.join(",")});
		},


		// empty local idlist when list was being collected for other purposes.
		//used by Mesh and Journals (empty UidList should not be distributed, otherwise Journals breaks)
		// now used by all reports for remove from clipboard function.
		'ClearIdList' : function(sMessage, oData, sSrc){
			this.setValue("IdsFromResult", '');
			this.send.SelectedItemCountChanged({'count': '0'});
			this.send.newUidSelectionList({'list': ''});
		}, 


		// back button fix: when search backend click go or hot enter on term field,
		//it also sends db. this db should be same as dbconnector's db
		'SearchBarSearch' : function(sMessage, oData, sSrc){
			if (this.getValue("Db") != oData.db){
				this.setValue("Db", oData.db);
			}
		},
		
		// back button fix: whrn links is selected from DisplayBar,
		//ResultsSearchController sends the LastQueryKey from the results on the page
		// (should not be needed by Entrez 3 code)
		'LastQueryKey' : function(sMessage, oData, sSrc){
			if (this.getInput("LastQueryKey")){
				this.setValue("LastQueryKey", oData.qk);
			}
		},
		
		'QueryKey' : function(sMessage, oData, sSrc){
			if (this.getInput("QueryKey")){
				this.setValue("QueryKey", oData.qk);
			}
		},
		
		
		//ResultsSearchController asks for the initial item count in case of send to file 
		'needSavedSelectedItemCount' : function(sMessage, oData, sSrc){
			var count = 0;
			if(this.getInput("IdsFromResult")){
				if (this.getValue("IdsFromResult").length > 0){
					count = this.getValue("IdsFromResult").split(',').length;
				}
				console.info("sending SavedSelectedItemCount from IdsFromResult: " + count);
			}
			else{
				count = Portal.Portlet.DbConnector.originalCount;
				console.info("sending SavedSelectedItemCount from OriginalCount: " + count);
			}
			this.send.SavedSelectedItemCount({'count': count});
		},
		
		// Force form submit, optionally passing db, term and cmd parameters
		'ForceSubmit': function (sMessage, oData, sSrc)
		{
		    if (oData.db)
    			this.setValue("Db", oData.db);
		    if (oData.cmd)
    			this.setValue("Cmd", oData.cmd);
		    if (oData.term)
    			this.setValue("Term", oData.term);
    		Portal.requestSubmit ();
		},
		
		'LinkName': function (sMessage, oData, sSrc){
		    this.setValue("LinkName", oData.linkname);
		}
		
	}, //listen
	
	/* other portlet functions */
	
	// DisplayBar in new design wants selected item count
	'SelectedItemCount': function(){
	    var count = 0;
		if(this.getInput("IdsFromResult")){
			if (this.getValue("IdsFromResult") != ''){
				count = this.getValue("IdsFromResult").split(',').length;
			}
		}
		else{
			count = Portal.Portlet.DbConnector.originalCount;
		}
		return count;
	}
		
},
{
	originalIdList: '',
	originalCount: 0
});

function getEntrezSelectedItemCount() {
    return $PN('DbConnector').SelectedItemCount();
}
