debug = false;


/**
 * The Multiselect static class groups together functions for operating cascading arrays of multiselects
 *
 * @class Multiselect
 * @todo Get the onload attachment working!
 */
var Multiselect = {
	
	/**
	 * Sets up the multiselects by searching for multiselect class and attaching Multiselect.processValue to it's onchange handler
	 *
	 * @method setup
	 */
	'setup' : function()
	{
		multiselects=YAHOO.util.Dom.getElementsByClassName('multiselect', 'select');
		for (i in multiselects)
		{
			select=multiselects[i];
			YAHOO.util.Event.addListener(select, 'change', Multiselect.processValue, select);
			this.processValue(null, select);
		}
		
	},
	
	/**
	 * Handles the change in a select, auto-sensing it's position
	 *                       in the multiselect cascade structure
	 *                       and firing off the XHR (Ajax) call
	 *
	 * @method processValue
	 * @param  {Event}   event   The change event
	 * @param  {DOMNode} select  The select node that was change
	 */
	'processValue' : function(event,select)
	{
		selectOrder = parseInt(select.id.substring(select.id.lastIndexOf('_')+1,select.id.length), 10);
		selectName  = select.id.substring(0,select.id.lastIndexOf('_'));
		newSelectId = selectName+'_'+(++selectOrder);
		
		if (newSelectObj=document.getElementById(newSelectId))	// don't request anything from server if select is last in row
		{
			if (event != null || newSelectObj.innerHTML.replace(/^\s*$/,"") == "") {
				newSelectObj.setAttribute('disabled','disabled');
				newSelectObj.innerHTML='';
            
				// erase and disable all subsequent selects
				while (childSelectNode = document.getElementById(selectName+'_'+(++selectOrder)))
				{
					childSelectNode.innerHTML='';
					childSelectNode.setAttribute('disabled','disabled');
				}

				//microsites
				advertiser_id = '';
				pipe_index = select.title.lastIndexOf('|');
				if (pipe_index > -1) { //microsites
					dbClassName=select.title.substring(select.title.lastIndexOf(' ')+1, pipe_index); // find out which database table to fetch results from
					advertiser_id=select.title.substr(pipe_index+1, select.title.length);
				} else {
					dbClassName=select.title.substr(select.title.lastIndexOf(' ')+1); // find out which database table to fetch results from
				}

				if (select.value != '')	// if something meaningfull is currently selected
				{
					newSelectObj.innerHTML='<option>...loading...</option>';
					YAHOO.util.Connect.asyncRequest('GET',
													'/ajax.php?select='+escape(newSelectId)+'&dbclass='+escape(dbClassName)+'&value='+select.value+'&advertiserid='+advertiser_id,
													{
														'success': Multiselect.handleResponse,
														'failure': null
													});
				}
			}
		}
	},
	
	/**
	 * Receives response from the XHR call containing the list of options to populate into a field
	 *
	 * @method handleResponse
	 * @param {XmlHttpRequest} response The XHR object from which to retrieve the response
	 */
	'handleResponse' : function(response)
	{
		responseObject = eval(response.responseText);	// unsafe

		for (selectId in responseObject)
		{
			selectNode = document.getElementById(selectId);
			if (selectNode==null) continue; // if respective select doesn't exists (must be an error) then skip it
			Multiselect.populateSelect(selectNode,responseObject[selectId]);
		}
	},
	
	/**
	 * Ensures the population of a select node with a provided array of values
	 *
	 * @method populateSelect
	 * @param {DOMNode} selectNode
	 * @param {Array} values 
	 */
	'populateSelect' : function(selectNode, values)
	{
		// if (console && debug) console.log(values);
		selectNode.innerHTML='';
      dbClassName='';

		//microsites
      advertiser_id = '';
		pipe_index = selectNode.title.lastIndexOf('|');
		if (pipe_index > -1) {
			dbClassName=selectNode.title.substring(selectNode.title.lastIndexOf(' ')+1, pipe_index); // find out which database table to fetch results from
			advertiser_id=selectNode.title.substr(pipe_index+1, selectNode.title.length);
		} else {
			dbClassName=selectNode.title.substr(selectNode.title.lastIndexOf(' ')+1); // find out which database table to fetch results from
		}

		if (dbClassName=='Area') allLabel='Areas';
		else if (dbClassName=='Occupation') allLabel='Skills';
		else allLabel='Options';

		for(name in values)
		{
			newOption = document.createElement('option');
			newOption.value=values[name];
			newOption.innerHTML=name;
			if (name=='&nbsp;' && values[name]=='') newOption.innerHTML='All '+allLabel+'';
			selectNode.appendChild(newOption); // reverted back to appendChild instead of innerHTML+= for IE compatibility
		}
		
		selectNode.removeAttribute('disabled');
	}
};

Multiselect.setup();
// not working!!!! YAHOO.util.Event.addListener(YAHOO.util.Dom.get('body'), 'load', );

