/*
 The SuggestionManager class can make using the SuggestionMaker class easier. If
the SuggestionMaker class is used in a page, there must be one variable to each
text input element. Using SuggestionManager, each page only needs one variable!

 SuggestionManager keeps an array of SuggestionMaker objects, and uses the text
input element ids to find each one. So, most of the methods of SuggestionManager
take an id argument, and then call a method of the corresponding SuggestionMaker
object. So, all the page needs to know is the id of the text element! 

 This example is rewritten from SuggestionMaker.js:

<script>
 var suggestionManager=new SuggestionManager();
 suggestionManager.makeSuggestion(
           'suggestDiv1',// Div id.
           'textInput1', // Text input id.
           'states.php'),// Request URL, without query string.
 suggestionManager.makeSuggestion('suggestDiv2','textInput2','states.php');
 </script>
 <!-- A form for choosing states... -->
 <form
    action='submit.php'
    method='GET'
    name='form1'>
    <!-- First input. -->
    <input
       id='textInput1'
       name='state1'
       onblur='suggestionManager.hide(this.id);'
       onclick='suggestionManager.toggle(this.id);'
       ondblclick='suggestionManager.sendRequest(this.id,"state="+escape(this.value),"GET");'
       type='text'
       value='State Name'/><br/>
    <!-- Second input, still using suggestionManager! -->
    <input
       id='textInput2'
       name='state2'
       onblur='suggestionManager.hide(this.id);'
       onclick='suggestionManager.toggle(this.id);'
       ondblclick='suggestionManager.sendRequest(this.id,"state="+escape(this.value),"GET");'
       type='text'
       value='Another State Name'/><br/>
       <!-- Make div elements. -->
       <div id='suggestDiv1'/><div id='suggestDiv2'/>
       <!-- ... -->
 </form>

Joshua A. S. Allen 6/1/2009
*/

function SuggestionManager()
{// Make SuggestionMaker array.
this.suggestions=new Array();

// Set CSS class names.
// These classes will be passed to each SuggestionMaker.
this.invisibleClass='invisibleSuggestions';
this.visibleClass='visibleSuggestions';
this.suggestionClass='suggestion';
// Item handler callback name.
this.itemHandler='';

// Call this function for each text input.
this.makeSuggestion=function(divId,inputId,requestURL,popupOnRes,resHandler)
{if(arguments.length==3){
	this.suggestions[inputId]=new SuggestionMaker(divId,inputId,requestURL);}
if(arguments.length==4){
	this.suggestions[inputId]=new SuggestionMaker(divId,inputId,requestURL,popupOnRes);}
if(arguments.length>=5){
	this.suggestions[inputId]=new SuggestionMaker(divId,inputId,requestURL,popupOnRes,resHandler);}
this.suggestions[inputId].invisibleClass=this.invisibleClass;
this.suggestions[inputId].visibleClass=this.visibleClass;
this.suggestions[inputId].suggestionClass=this.suggestionClass;
this.suggestions[inputId].itemHandler=this.itemHandler;
this.suggestions[inputId].suggestionManager=this;}

// Show drop-down list for text input with id id.
this.show=function(id)
{return(this.suggestions[id]&&this.suggestions[id].show());}

// Hide drop-down list for text input with id id.
this.hide=function(id)
{return(this.suggestions[id]&&this.suggestions[id].hide());}

// Toggle list on and off.
this.toggle=function(id)
{return(this.suggestions[id]&&this.suggestions[id].toggle());}

// Is list visible for text input?
this.isVisible=function(id)
{return(this.suggestions[id]&&this.suggestions[id].isVisible());}

// Set name of item handler function.
this.setItemHandler=function(id,h)
{if(this.suggestions[id]){
	this.suggestions[id].itemHandler=h;
	return(true);}
return(false);}

// Return SuggestionMaker object, or null.
this.getSuggestion=function(id)
{return(this.suggestions[id]?this.suggestions[id]:null);}

// Send HTTP request for text input with id id, and optional query string and 
// HTTP method. Returns true if request sent, false otherwise.
// Only the first 2 arguments are needed, the rest are optional HTTP method,
// popup flag, and response handler.
this.sendRequest=function(id,qs,meth,p,r)
{qs=arguments.length>1?qs:'';
meth=arguments.length>2?meth:'GET';
if(arguments.length==2){
	return(this.suggestions[id]&&this.suggestions[id].sendRequest(qs));}
if(arguments.length==3){
	return(this.suggestions[id]&&this.suggestions[id].sendRequest(qs,meth));}
if(arguments.length==4){
	return(this.suggestions[id]&&this.suggestions[id].sendRequest(qs,meth,p));}
if(arguments.length>=5){
	return(this.suggestions[id]&&this.suggestions[id].sendRequest(qs,meth,p,r));}}}
