/*
These are functions required for MlsFullSearch.vm and Searches.vm. All functions are generic except for moreFields(),
which makes some assumptions about the html document that contains it.
*/

//Toggles the display of the detailed search screen.
//Assumptions:
//		-Name of table containing detailed search fields is called 'moreFieldsTable'
//		-A form called 'theForm' contains a hidden input element called 'showMoreFields'
//		-A link called 'showHideText' accesses this function.
function moreFields()
{
	if (document.getElementById('moreFieldsTable').style.display == null ||
		document.getElementById('moreFieldsTable').style.display == 'none' || 
		document.getElementById('moreFieldsTable').style.display == ''
	)
	{
		// show more search fields
		document.getElementById('moreFieldsTable').style.display = 'inline';
		document.theForm.showMoreFields.value = 'true';
		document.getElementById('showHideText').innerHTML = 'Hide More Search Fields';
	}
	else if (document.getElementById('moreFieldsTable').style.display == 'inline')
	{
		// hide more search fields
		document.getElementById('moreFieldsTable').style.display = 'none';
		document.theForm.showMoreFields.value = 'false';
		document.getElementById('showHideText').innerHTML = 'Show More Search Fields';
	}
}

function verifySelect(theSelectField)
{
	if (theSelectField.options[0].selected)
	{
		for (var i = 1; i < theSelectField.options.length; i++)
		{
			if (theSelectField.options[i].selected)
			{
				theSelectField.options[0].selected = false;
				break;
			}
		}
	}	
}

function emptySelect(selectCtrl)
{
	for (var i = selectCtrl.options.length; i >= 0; i--) 
	{
		selectCtrl.options[i] = null; 
	}
}
	
function appendArrayToSelect(selectCtrl, itemArray)
{
	var j = selectCtrl.length;
	if(itemArray != null)
	{
		for(var i = 0; i < itemArray.length; i++)
		{
			selectCtrl.options[j + i] = new Option(itemArray[i][1]);
			selectCtrl.options[j + i].value = itemArray[i][0]; 
		}
	}
}

function fillChild(parentChildHash, parentCtrl, childCtrl, fillAll)
{
	emptySelect(childCtrl);
	childCtrl.options[0] = new Option("Search All");
	childCtrl.options[0].value = " ";
	childCtrl.options[0].selected = true;
	
	if(fillAll && parentCtrl.options[0].selected){
	
		for(var i = 1; i < parentCtrl.options.length; i++)
		{
			var parentOption = parentCtrl.options[i];
			var childOptions = parentChildHash.get(parentOption.value);
			appendArrayToSelect(childCtrl, childOptions);
		}
		
	}else{

		for(var i = 1; i < parentCtrl.options.length; i++)
		{
			var parentOption = parentCtrl.options[i];
			if(parentOption.selected)
			{
				var childOptions = parentChildHash.get(parentOption.value);
				appendArrayToSelect(childCtrl, childOptions);
			}
		}
	
	}
}

function hasOptions(obj)
{
	if(obj!=null && obj.options!=null)
	{
		return true;
	}
	return false;
}

function sortSelect(obj)
{
	var o = new Array();
	if(!hasOptions(obj))
	{
		return;
	}
	for(var i=0;i<obj.options.length;i++)
	{
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
	}
	if(o.length==0)
	{
		return;
	}
	o = o.sort(
		function(a,b)
		{
			if((a.text+"") < (b.text+""))
			{
				return -1;
			}
			if((a.text+"") > (b.text+""))
			{
				return 1;
			}
			return 0;
		}
	);
	for(var i=0;i<o.length;i++)
	{
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	}
}

function fillChildSorted(parentChildHash, parentCtrl, childCtrl, fillAll)
{
	emptySelect(childCtrl);
	// Ensure this entry gets sorted to the top.
	// Note that splice doesn't work on options.
	childCtrl.options[0] = new Option("          ");
	childCtrl.options[0].value = " ";
	
	if(fillAll && parentCtrl.options[0].selected){
	
		for(var i = 1; i < parentCtrl.options.length; i++)
		{
			var parentOption = parentCtrl.options[i];
			var childOptions = parentChildHash.get(parentOption.value);
			appendArrayToSelect(childCtrl, childOptions);
		}
		
	}else{

		for(var i = 1; i < parentCtrl.options.length; i++)
		{
			var parentOption = parentCtrl.options[i];
			if(parentOption.selected)
			{
				var childOptions = parentChildHash.get(parentOption.value);
				appendArrayToSelect(childCtrl, childOptions);
			}
		}
	}
	sortSelect(childCtrl);

	childCtrl.options[0].text = "Select All";
	childCtrl.options[0].selected = true;
}
