// JavaScript Document
// Called: jscript-DisplayGroup-Functions.js
// Project: REIWA Online
//
// Purpose: JScript Functions used by the Display Group pages:
//		
//		From Lib-Include-DisplayGroup-Functions.cfm
//			From cffunction Lib_DisplayGroup_SelectList_Display
//				SelectionResetListOptions ( form, field, labels, values )
//				SelectionAddToList ( form, field, newLabel, newValue, maxAllowed )
//				SelectionRemoveOption ( form, field )
//				SelectionClear ( form, field )
//				SelectionAlign ( form, field )
//
//			From cffunction Lib_DisplayGroup_SelectOptions_Display
//				SelectOptionsChanged ( form, thisField, listField, maxAllowed )
//				SelectOptionSelected ( form, thisField, selectValue, listField, maxAllowed )
//
//			From cffunction Lib_DisplayGroup_SelectOptionsList_Display
//				OnChangeSearchMode ( form, findType )			
//				OnSubmitFind ( form, findType )			
//				OnSubmitSelect ( form )
//				GetFilterValsForList ( nameListField, valueListField, formFind, name1, name2, namen )
//				AppendURLParameter ( urlString, nextLabel, nextValue )
//				DisplayGroupShowFilter ()
//				DisplayGroupHideFilter ()
//				ResetSavedFilter ( formFilters, newFilterName )
//				
//				
//				
// Developed by: JCostello
// Company: ALTVISION PTY LTD
// Date Created: 27th April 2005
// History:
//

var selectAllValue = "";
var selectAllLabel = "*** All ***";

function SelectionResetListOptions ( form, field, labels, values )
{
	// Set the options for the SelectList combo to the supplied list
	// As well as the form and filedName parameter, there will be a list of option labels and then an equal length list 
	// of option values.
	// For example:
	//  ResetSelectionListOptions ( document.MyForm, "Selection", 'AppleCross', 'Ardross', 'South Perth', 123, 235, 453 )
	
	// First work out how many options we have (in the above example it will be 3) 
	nonOptions = 2;
	argndx1=( SelectionResetListOptions.arguments.length -nonOptions ) / 2;
	argndx2=argndx1*2;
	subcount=0;
	
	// Now get the field to store the options into - it is the name of the supplied field 
	// with "_options" appended
	var optionsField = form.elements[field + "_options"];
	// Now store the list of options into the select box (override all existing options)
	// Do we need to prefix the supplied list with *** All *** - to detemine this, check if the supplied
	//		field allows for multiple selections
	// If multiple lines set, do not add the *** All *** option
	var selectField = form.elements[field];
	multiple = selectField.multiple 
	
	if ( ! multiple )
	{
		optionsField.options.length=subcount+1;
		optionsField.options[subcount].selected = true; 
		optionsField.options[subcount].value = selectAllValue;
		optionsField.options[subcount].text = selectAllLabel;
		subcount=subcount+1;
	}

	for(c=0;c<argndx1;c++)
	{
		optionsField.options.length=subcount+1;
		optionsField.options[subcount].selected = false; 
		optionsField.options[subcount].value = SelectionResetListOptions.arguments[c+argndx1+nonOptions];
		optionsField.options[subcount].text = SelectionResetListOptions.arguments[c+nonOptions];
		subcount=subcount+1;
	}

	SelectionClear ( form, field )
}
function SelectionAddToList ( form, field, newLabel, newValue, maxAllowed )
{
	// Add the supplied selection to the list of selected items - that is unless it is already in the list
	
	// Now get the field that shows the options - it is the name of the supplied field with "_show appended
	var showField = form.elements[field + "_show"];
	var hideField = form.elements[field];
	if ( hideField.multiple )
	{
		// Multiple entries is set - ensure that the max allowed is not exceeded
		if ( maxAllowed != -1 && maxAllowed <= hideField.options.length )
		{	
			alert ( "Cannot select more than " + maxAllowed + " entries" );
			return  
		}
	} else
	{
		// Multiple Entries are not set, so replace current selection with this new selection
		showField.options.length = 0;
	}

	// Is the currently select suburbs set to 'All Suburbs'
	if ( showField.options.length == 1 )
	{
		if ( showField.options[0].value == selectAllValue )
		{
			// Yes it is - remove it
			showField.remove(0);
		}
	}
	
	// Now find where to put it so the entries are in alphabetic ascending order
	putIt = 0;
	for ( c2=0; c2<showField.options.length; c2++ )
	{
		if ( showField.options[c2].text == newLabel )
		{
			putIt = -1; // Already in the list so we will ignore it
		}
		if ( showField.options[c2].text < newLabel )
		{
			putIt = c2+1; // It goes after this entry - not necesarilly straight after it
		}
	}
	
	// If we don't ignore it (already in the list), move all those that go after it, up one in the list
	if ( putIt > -1 ) 
	{	 
		subcount = showField.options.length;
		showField.options.length=subcount+1;
		// Loop backwards until we have move all that we have to move
		if ( subcount != putIt )
		{
			for(c2=showField.options.length-1;c2>putIt;c2--)
			{
				showField.selected = false; 
				//showField.value = showField.value;
				showField.options[c2].value = showField.options[c2-1].value;
				showField.options[c2].text  = showField.options[c2-1].text;
			}
		}
		// OK now put in the new entry
		showField.options[putIt].selected = false; 
		showField.options[putIt].value = newValue;
		showField.options[putIt].text = newLabel;
		
		SelectionAlign( form, field );
	}
}

function SelectionRemoveOption ( form, field )
{
	// Get the user to confirm that the selected option is to be removed from the Selected Options list
	// 	If they do confirm, remove it.
	// If it is the "Select All" option just ignore it

	// Get the field that shows the options  - it is the name of the supplied field with "_show" appended to it
	var showField = form.elements[field + "_show"];
	// Get the selected option code and label
	index = showField.selectedIndex;
	if ( index < 0 ) return;
	selectedText = showField.options[index].text 
	selectedValue = showField.options[index].value 

	// Is it the "Select All Suburbs" option?
	if ( index == 0 && selectedValue == selectAllValue )
	{ // Yes - do nothing
	} else
	{
		showField.remove(index);
		if (showField.options.length == 0 )
		{
			SelectionClear ( form, field );
		}else
		{
			SelectionAlign( form, field );
		}
	}
}

function SelectionClear ( form, field )
{
	// Clear all options out of the sselection and add the select ALl option 
	
	// Get the field that shows the options  - it is the name of the supplied field with "_show" appended to it
	var showField = form.elements[field + "_show"];
	
	// Remove all entries from the Selected Suburbs list and replace with "Select All Suburbs"
	showField.options.length=1;
	showField.options[0].selected = false; 
	showField.options[0].value = selectAllValue;
	showField.options[0].text = selectAllLabel;

	SelectionAlign( form, field );
}
			
function SelectionAlign ( form, field )
{
	// Align the hidden selection field to the show selection field however select all options 
	// (unless there is Select All) so the post will work 

	// Get the field that shows the options  - it is the name of the supplied field with "_show" appended to it
	var showField = form.elements[field + "_show"];
	// Get the hidden selection field - it is the name of the supplied field with nothing appended to it
	var hideField = form.elements[field];

	for ( c2=0; c2<showField.options.length; c2++ )
	{
		hideField.options.length=c2+1;
		if ( showField.options[c2].value == selectAllValue )
		{
			hideField.options[c2].selected 	= false; 
		}else
		{
			hideField.options[c2].selected 	= true; 
		}
		hideField.options[c2].value 		= showField.options[c2].value;
		hideField.options[c2].text 		= showField.options[c2].text;
	}
}
			
function SelectOptionsChanged ( form, thisField, listField, maxAllowed )
{
	// The user has just clicked on an option - get the selected value and label and pass to the List Fields 
	// Get the field to retrieve the selection from
	var optionsField = form.elements[thisField];
	// Now get the selected value and label
	index 			= optionsField.selectedIndex;
	if ( index < 0 ) return;
	selectedText 	= optionsField.options[index].text 
	selectedValue 	= optionsField.options[index].value 
	// And pass it to the List fields
	SelectionAddToList ( form, listField, selectedText, selectedValue, maxAllowed )
}

function SelectOptionSelected ( form, thisField, selectValue, listField, maxAllowed )
{
	// The user has just clicked on an option - get the selected value and label and pass to the List Fields 
	// Get the field to retrieve the selection from
	var optionsField = form.elements[thisField];
	for ( c2=0; c2<optionsField.options.length; c2++ )
	{
		if ( optionsField.options[c2].value == selectValue )
		{
			optionsField.options[c2].selected 	= true; 
			SelectionAddToList ( form, listField, optionsField.options[c2].text, selectValue, maxAllowed )
			return
		}
	}
}

function OnChangeSearchMode ( form, findType )
{
	OnSubmitFind ( form, findType );
	form.submit();
}

function OnSubmitFind ( form, findType )
{
	if ( !ValidateFilterForm ( form ) ) return false;
	urlString = AppendURLParameter ( form.action, 'Action', findType );
	form.action = urlString; 			// form.action + "?Action=" + findType;
	form.Search.disabled = true;
	return true;
}

function OnSubmitSelect ( form )
{
	return true;
}

function GetFilterValsForList ( nameListField, valueListField, formFind, name1, name2, namen )
{
	// NameListField is the form field to add the list of filter variable names to
	// valueListField is the form field to add the list of filter variable values to
	// formFind is the form item where the filter paramaters are
	// name1, name2, .... namen are the names of each filter variable
	
	// If formFind does not exist, do nothing
	if ( ! formFind ) 
	{
		return;
	}

	if ( ! valueListField ) 
	{
			return;
	}

	if ( ! nameListField ) 
	{
		return;
	}

	argndx=GetFilterValsForList.arguments.length;
	var valueList = "";
	var nameList  = "";
	
	for(c=3;c<argndx;c++)
	{
		itemName = GetFilterValsForList.arguments[c];
		itemNo = -1
		for ( c2=0; c2<formFind.elements.length; c2++ )
		{
			if ( formFind.elements[c2].name == itemName ) 
			{
				if ( formFind.elements[c2].type == "radio" )
				{
					if ( formFind.elements[c2].checked ) itemNo = c2;
				} else 
				{
					itemNo = c2; 
				}
			}
		}
		if ( itemNo == -1 )
		{
			// Not all WHERE items are now added to the form, so just ignore any that we do not find
			// alert ( 'filter Item ' + itemName + ' not found on the form' );
		}else
		{
			if ( formFind.elements[itemNo].type == "select-multiple" )
			{
				// We have a multiple select box so we need to build the list of selected items
				itemValue = '';
				for ( c3=0; c3<formFind.elements[itemNo].length; c3++ )
				{	
					if ( formFind.elements[itemNo].options[c3].selected ) 
					{ 
						// Got one that is selected, add the value of the option to the list
						if ( itemValue == '' )
						{
							itemValue =  formFind.elements[itemNo].options[c3].value;
						}else
						{
							itemValue =  itemValue + elementSeparator + formFind.elements[itemNo].options[c3].value;
						}
					}
				}
			}else
			{
				itemValue = formFind.elements[itemNo].value;
			}
			if ( itemValue.indexOf ( listSeparator ) != -1 )
			{
				alert ( "Current value for " + itemName + " cannot be saved" );
				return false;
			}
			if ( nameList == '' )
			{
				// Force a blank in front of the first entry, just in case it is a null length string
				nameList  = ' ' + itemName;
				valueList = ' ' + itemValue;
			}else
			{
				nameList  = nameList  + listSeparator + itemName;
				valueList = valueList + listSeparator + itemValue;
			}
		}
	}	
	nameListField.value  = nameList;
	valueListField.value = valueList;
}

function AppendURLParameter ( urlString, nextLabel, nextValue )
{
	// Append the next URL label and valuye to the URL string 
	//		- if the existing string already has a '?' append using '&'
	//		- else append using'?'
if ( urlString.indexOf ( '?' ) == -1 ) 
	{
		urlString = urlString + '?' + nextLabel + '=' + nextValue;
	}else
	{
		urlString = urlString + '&' + nextLabel + '=' + nextValue;
	}
	return urlString;
}
											
function DisplayGroupShowFilter ()
{
	FilterDisplay = document.getElementById("FilterDisplay_ID")
	topOfPage_id  = document.getElementById("nav_topOfPage_id" ) // Should be the very first td in Nav-include-std-layout.cfm
	if (FilterDisplay) 
	{
		FilterDisplay.style.display='';
		if ( topOfPage_id )
		{	
			// This will take us to the very top left of the page
			topOfPage_id.scrollIntoView(false);
		}else
		{
			// But as that has failed, this will take us to the top left of the content page 
			//		the vertical menu will e scrolled off to the left 
			// 	and the header and horizontal menu will be scrolled off to the top
			FilterDisplay.scrollIntoView(false);
		}
	}
	SearchRefine = document.getElementById("SearchRefine_ID")
	if (SearchRefine) SearchRefine.style.display='none';
	HideRefine = document.getElementById("HideRefine_ID")
	if (HideRefine) HideRefine.style.display='';

}

function DisplayGroupHideFilter ()
{
	FilterDisplay = document.getElementById("FilterDisplay_ID")
	if (FilterDisplay) FilterDisplay.style.display='none';
	SearchRefine = document.getElementById("SearchRefine_ID")
	if (SearchRefine) SearchRefine.style.display='';
	HideRefine = document.getElementById("HideRefine_ID")
	if (HideRefine) HideRefine.style.display='none';
}

function ResetSavedFilter ( formFilters, newFilterName )
{
	// This function will be called from the Filter Save popup window so we can reset the Filter 
	// 	options in the Filter selector, and/or the Current selection
	
	if ( ! formFilters.Filter_List ) 
	{
		alert ( "Cannot find the filter list"); 
		return;
	}
	
	if ( formFilters.Filter_List.options.length == 0 )
	{
		// No options yet so just add it in posn 0			
		posn = 0;
	}else	
	{
		for(c=0;c<formFilters.Filter_List.options.length;c++)
		{
			if ( newFilterName == formFilters.Filter_List.options[c].value )
			{
				// Already in the list - make it the selected one and return
				formFilters.Filter_List.options[c].selected = true;
				return;
			}
		}
		// Not in the list - add it to the end
		posn = formFilters.Filter_List.options.length;
	}
	// Add it in
	formFilters.Filter_List.options.length=posn+1;
	formFilters.Filter_List.options[posn].selected = true; 
	formFilters.Filter_List.options[posn].value = newFilterName;
	formFilters.Filter_List.options[posn].text = newFilterName;			
	
	// Ensure that the filter selector is now visible
	filterSelector = document.getElementById("showFilterSect_ID")
	if (filterSelector) 
	{
		filterSelector.style.display='';
	} 
} 
