function criterionSet(){
	if(document.frmSearch.filterLetter.value != null && document.frmSearch.filterLetter.value != ""){
		return true;
	}
	var num = 0;	
	var val_ = dijit.byId("cnameId").getValue();
	if(dojo.trim(dijit.byId("cnameId").getDisplayedValue()) != "" && val_ != "-1")
	{		
		return true;
	}
	
	num = num + (val_ != "" ? parseInt(val_) : -1);
	val_ = dijit.byId("styleId").getValue();
	num = num + (val_ != "" ? parseInt(val_) : -1);
	val_ = dijit.byId("spiritId").getValue();
	num = num + (val_ != "" ? parseInt(val_) : -1);
	val_ = dijit.byId("liqueurId").getValue();
	num = num + (val_ != "" ? parseInt(val_) : -1);
	val_ = dijit.byId("wineId").getValue();
	num = num + (val_ != "" ? parseInt(val_) : -1);
	val_ = dijit.byId("mixerId").getValue();
	num = num + (val_ != "" ? parseInt(val_) : -1);
	return num > -6;
}

function resetSerach()
{	
	dijit.byId('frmSearch').reset();
	//toggleSearchCriteria(false);
	dijit.byId('recipeList')._clearData();
	setDivContents('../main/cocktail.jsp','ceditor','diffordsguide');
	dojo.addClass('result_sorter', 'invisible');
	dojo.removeClass(dojo.byId('term'), 'invisible');
	dojo.addClass(dojo.byId('term'), 'visible');
	cocktailChange();
}
function submitSearch()
{		
	if(GLOBAL_SEARCHING)
	{
		return;
	}
	
	if(!criterionSet())
	{
		dijit.byId('recipeList')._clearData();
		dojo.addClass('result_sorter', 'invisible');
		dojo.byId("searchMessage").innerHTML="<i>No records match your search criteria. Please ensure that you specify at least one search value</i>";
		return;
	}
	
	showPleaseWait();
	var selCocktail_ = dijit.byId("cnameId").getDisplayedValue();
	var selStyle_ = dijit.byId("styleId").getDisplayedValue();
	var selSpirit_ = dijit.byId("spiritId").getDisplayedValue();
	var selLiqueur_ = dijit.byId("liqueurId").getDisplayedValue();
	var selWine_ = dijit.byId("wineId").getDisplayedValue();
	var selMixer_ = dijit.byId("mixerId").getDisplayedValue();
	
	document.frmSearch.cocktailName.value = selCocktail_;
	document.frmSearch.cocktailStyle.value = selStyle_;
	document.frmSearch.cocktailSpirit.value = selSpirit_;
	document.frmSearch.cocktailLiqueur.value = selLiqueur_;
	document.frmSearch.cocktailWine.value = selWine_;;
	document.frmSearch.cocktailMixer.value = selMixer_;

	//Cocktail
	var paramId_ = dijit.byId("cnameId").getValue();	
	if(paramId_ == "-1")
	{			
		document.frmSearch.cocktailName.value = null;
		document.frmSearch.cocktailNameId.value = paramId_;
	}
	else if(paramId_ =="" || paramId_ == null )
	{
		 //Accented character selected		
		var selId;	
		/* We don't really care what the id is - as we want to execute a global search */
		if(selCocktail_ != 'Type Cocktail Name'){
			document.frmSearch.cocktailNameId.value = 0;
		}else{			
			document.frmSearch.cocktailNameId.value = -1;
			document.frmSearch.cocktailName.value = null;
		}
		
	}else{		
		document.frmSearch.cocktailNameId.value = paramId_;
	}
	
	//Style
	paramId_ = dijit.byId("styleId").getValue();
	if(paramId_ == "-1")
	{
		document.frmSearch.cocktailStyle.value = null;		
		document.frmSearch.cocktailStyleId.value = paramId_;
	}
	else if(paramId_ =="" || paramId_ == null )
	{
		//Accented character selected		
		var selId;			
		var isValid =dojo.some
		(				
				dijit.byId('styleId').store._arrayOfTopLevelItems, 
				function(item)
				{					
					selId = item.catId; 
					return item.catName==selStyle_;
				}
		);
		
		if(isValid == true)
		{
			document.frmSearch.cocktailStyleId.value = selId;		
		}
		else
		{
			document.frmSearch.cocktailStyleId.value = null;			
		}
	}else{	
		document.frmSearch.cocktailStyleId.value = paramId_;
	}
	
	//Spirit
	paramId_ = dijit.byId("spiritId").getValue();
	if(paramId_ == "-1"){
		document.frmSearch.cocktailSpirit.value = null;	
		document.frmSearch.cocktailSpiritId.value = paramId_;
	}else if(paramId_ =="" || paramId_ == null ){
		//Accented character selected		
		var selId;			
		var isValid = dojo.some
		(				
				dijit.byId('spiritId').store._arrayOfTopLevelItems, 
				function(item)
				{
					selId = item.ingId; 
					return item.ingredient==selSpirit_;
				}
		);
		if(isValid == true)
		{
			document.frmSearch.cocktailSpiritId.value = selId;			
		}
		else
		{
			document.frmSearch.cocktailSpiritId.value = null;			
		}
	}else{
		
		document.frmSearch.cocktailSpiritId.value = paramId_;
	}
	
	// Liqueur
	paramId_ = dijit.byId("liqueurId").getValue();
	if(paramId_ == "-1"){
		document.frmSearch.cocktailLiqueur.value = null;	
		document.frmSearch.cocktailLiqueurId.value = paramId_;
	}
	else if(paramId_ =="" || paramId_ == null )
	{
		//Accented character selected		
		var selId;			
		var isValid = dojo.some
		(				
				dijit.byId('liqueurId').store._arrayOfTopLevelItems, 
				function(item)
				{
					selId = item.ingId; 
					return item.ingredient==selLiqueur_;
				}
		);
		
		if(isValid == true)
		{
			document.frmSearch.cocktailLiqueurId.value = selId;			
		}
		else
		{
			document.frmSearch.cocktailLiqueurId.value = null;			
		}
	}else{		
		document.frmSearch.cocktailLiqueurId.value = paramId_;
	}
	
	// Wine
	paramId_ = dijit.byId("wineId").getValue();
	if(paramId_ == "-1"){
		document.frmSearch.cocktailWine.value = null;
		document.frmSearch.cocktailWineId.value = null;			
	}else if(paramId_ =="" || paramId_ == null ){
		//Accented character selected		
		var selId;			
		var isValid =dojo.some
		(				
				dijit.byId('wineId').store._arrayOfTopLevelItems, 
				function(item)
				{
					selId = item.ingId; 
					return item.ingredient==selWine_;
				}
		);
		
		if(isValid == true)
		{
			document.frmSearch.cocktailWineId.value = selId;			
		}
		else
		{
			document.frmSearch.cocktailWineId.value = null;			
		}
	}else{		
		document.frmSearch.cocktailWineId.value = paramId_;
	}
	
	//Mixer
	paramId_ = dijit.byId("mixerId").getValue();
	if(paramId_ == "-1")
	{
		document.frmSearch.cocktailMixer.value = null;	
		document.frmSearch.cocktailMixerId.value = paramId_;
	}
	else if(paramId_ =="" || paramId_ == null )
	{
		//Accented character selected		
		var selId;			
		var isValid = dojo.some
		(				
				dijit.byId('mixerId').store._arrayOfTopLevelItems, 
				function(item)
				{
					selId = item.ingId; 
					return item.ingredient==selMixer_;
				}
		);
		
		if(isValid)
		{
			document.frmSearch.cocktailMixerId.value = selId;			
		}
		else
		{
			document.frmSearch.cocktailMixerId.value = null;			
		}
	}
	else{		
		document.frmSearch.cocktailMixerId.value = paramId_;
	}
	
	postForm('/SearchServlet','frmSearch');	
}

function showPleaseWait()
{	
	var loadingDiv = dojo.byId('loading');
	if(loadingDiv){
		dojo.addClass('loading', 'visible');
		dojo.addClass('result_sorter', 'invisible');		
	}
}


function updateFilter(filter)
{
	if(GLOBAL_SEARCHING)
	{
		/* No need to update the filter since there is still a search in progress */
		return;
	}
	
	if(dojo.hasClass(filter, 'disableAlpha')){
		/* The alpha link is currently disabled */
		return;
	}
	
	if(dojo.hasClass(filter, 'selectAlpha'))
	{	
		//Already selected
		dojo.removeClass(filter, 'selectAlpha');
		document.frmSearch.filterLetter.value = null;		
	}
	else{
		dojo.query('.alphabet').removeClass('selectAlpha');
		dojo.addClass(filter, 'selectAlpha');
		document.frmSearch.filterLetter.value = filter;
		submitSearch();
	}
}

function criteriaChange(nameId) {
	if(GLOBAL_SEARCHING){
		return;
	}
	
	var aSel_val = dijit.byId(nameId).getValue();
	if(aSel_val && aSel_val > -1 && aSel_val != ""){
		submitSearch();
	}
}

function focusAction(nameId){	
	var data_ = dijit.byId(nameId).getValue();
	if(data_ == "-1" || data_ == ""){		
		dijit.byId(nameId)._setValue('');
	}
}
function cocktailChange()
{	
	if(GLOBAL_SEARCHING)
	{
			return;
	}	
	var aSel_val = dijit.byId("cnameId").getValue();
	var aDisplay_val = dojo.trim(dijit.byId("cnameId").getDisplayedValue());
	
	if(aDisplay_val != 'Type Cocktail Name' || aSel_val && aSel_val > -1 && aSel_val != "")
	{	
		toggleSearchCriteria(true);				
		if(aDisplay_val != "")
		{
			submitSearch();
		}
	}
	else
	{	
		toggleSearchCriteria(false);				
	}
}

function toggleSearchCriteria(disable)
{		
		if(disable)
		{
			/* Gray-out Components */
			dojo.query('#liqueurId').style('color','#DCDCDC');
			dojo.query('#styleId').style('color','#DCDCDC');
			dojo.query('#wineId').style('color','#DCDCDC');
			dojo.query('#spiritId').style('color','#DCDCDC');
			dojo.query('#mixerId').style('color','#DCDCDC');
			
			dojo.query('.alphabet').addClass('disableAlpha');	
			dojo.query('.alphabet').removeClass('selectAlpha');
			
			/* Reset Components Data */
			dijit.byId('liqueurId').reset();
			dijit.byId('styleId').reset();
			dijit.byId('wineId').reset();
			dijit.byId('spiritId').reset();
			dijit.byId('mixerId').reset();			
			/* Disable Components */
			dijit.byId('liqueurId').disabled=true;
			dijit.byId('styleId').disabled=true;
			dijit.byId('wineId').disabled=true;
			dijit.byId('spiritId').disabled=true;
			dijit.byId('mixerId').disabled=true;			
		}else{
			/* Un-gray out Components */
			dojo.query('#liqueurId').style('color','#666362');
			dojo.query('#styleId').style('color','#666362');
			dojo.query('#wineId').style('color','#666362');
			dojo.query('#spiritId').style('color','#666362');
			dojo.query('#mixerId').style('color','#666362');
			/* Enable Components */
			dijit.byId('liqueurId').disabled=false;
			dijit.byId('styleId').disabled=false;
			dijit.byId('wineId').disabled=false;
			dijit.byId('spiritId').disabled=false;
			dijit.byId('mixerId').disabled=false;	
			
			dojo.query('.alphabet').removeClass('disableAlpha');	
			dojo.query('.alphabet').removeClass('selectAlpha');
			
		}
		
		document.frmSearch.filterLetter.value = null;				
}
