/*
**	This file contains functions that support dynamic behaviors implemented
**	by the dynamic forms UI.
**
**	Behaviors supported:
**
**		TextBox to DropDownList
**
**			The contents of a DynamicDropDown list will be set based upon the
**			value entered into a DynamicTextBox by the user.
**
*/

///	handle the change event for a control (DDL)
//
//	Supports:
//		DropDownList to Field (TextBox or HiddenField)

function DDLToField( sourceControl, targetControl, dataSource )
{
	var key = sourceControl.value;
	var index = searchKV( dataSource, key );
	
	if ( index == null )
		targetControl.value = '';
	else
		targetControl.value = dataSource[index].value;
}


///	handle the blur event for a text box (our source control)
//
//	Supports:
//		TextBox to DropDownList

function SourceControl_Blurred( sourceControl, targetControl, dataSource )
{
	var key = sourceControl.value;
	var startIndex;
	var options;
	var i;

	ClearDropDownList( targetControl );
	
	startIndex = searchKV( dataSource, key );
	
	if ( startIndex == null )
		return;

	options = GetDropDownItems( key, dataSource, startIndex );

	PopulateDropDownList( targetControl, options )
}


///	Remove all options from the given drop down list
//
//	Supports:
//		TextBox to DropDownList

function ClearDropDownList( ddl )
{
	var n;

	for ( n = ddl.length; n > 0; --n )
		ddl.remove( 0 );
}


///	Populate a dropdown list with options
//

function PopulateDropDownList( targetControl, options )
{
	for ( i = 0; i < options.length; ++i )
	{
		try
		{
			targetControl.options.add( options[i] );
		}
		catch ( exception )
		{
			alert( exception.message );
		}
	}
}


///	search an ordered array of JSON objects.
//
//	Each JSON object must include a 'key'.
//
//	Returns the index of the FIRST object in the array with
//	matching key, or null if no match found.
//
//	Supports:
//		TextBox to DropDownList
//		DropDownList to Field (TextBox or HiddenField)

function searchKV( arr, val )
{
	var hi = arr.length;
	var lo = -1;
	var mid;

	while( (hi - lo) > 1 )
		{
		mid = (hi + lo) >> 1;

		if ( arr[mid].key < val )
			lo = mid;
		else
			hi = mid;
		}

	return ( (arr[hi].key != val) ? null : hi );
}


///	search an ordered array of values.
//
//	Returns the index of the FIRST object in the array with
//	matching key, or null if no match found.
//

function searchArray( arr, val )
{
	var hi = arr.length;
	var lo = -1;
	var mid;

	while( (hi - lo) > 1 )
		{
		mid = (hi + lo) >> 1;

		if ( arr[mid] < val )
			lo = mid;
		else
			hi = mid;
		}

	return ( (arr[hi] != val) ? null : hi );
}


///	Given a key, data source, and start index, returns an
//	array of drop down list options (DOM 'option').
//
//	Supports:
//		TextBox to DropDownList

function GetDropDownItems( key, dataSource, startIndex )
{
	var options = new Array();
	var lastIndex;
	var i = 0;

	options[i++] = CreateOption( '', '' );
	
	lastIndex = dataSource.length - 1;

	while ( dataSource[startIndex].key == key )
	{
		options[i++] = CreateOption( dataSource[startIndex].value, dataSource[startIndex].text );
		
		if ( ++startIndex > lastIndex )
			break;
	}

	return ( options );
}


///	Create a DOM 'option'
//
//	Supports:
//		TextBox to DropDownList

function CreateOption( value, text )
{
	var opt = document.createElement( 'option' );

	opt.text = text;
	opt.value = value;
	
	return ( opt );
}


///	Get Key/Value pairs from the database
//

var GetKeyValuePairsCallback;

function GetKeyValuePairs( selector, key, callback )
{
	var url = "/collegefinder/ajax/GetLeadGenKeyValuePairs.aspx";

	GetKeyValuePairsCallback = callback;

	$.ajax( { type: "GET", url: url, dataType: "text", data: { selector: [ selector ], key: [ key ] },
		success: GetKeyValuePairsResult,
		error: function( xmlHttpRequest, textStatus, errorThrown )
		{
			return ( "ajax: " + textStatus );
		}
	} );

	return ( null );
}


function GetKeyValuePairsSorted( selector, key, sortColumn, sortOrder, callback )
{
	var url = "/collegefinder/ajax/GetLeadGenKeyValuePairs.aspx";

	GetKeyValuePairsCallback = callback;

	$.ajax( { type: "GET", url: url, dataType: "text", data: { selector: [ selector ], key: [ key ], sortcolumn: [ sortColumn ], sortorder: [ sortOrder ] },
		success: GetKeyValuePairsResult,
		error: function( xmlHttpRequest, textStatus, errorThrown )
		{
			return ( "ajax: " + textStatus );
		}
	} );

	return ( null );
}


function GetKeyValuePairsResult( data, textStatus )
{
	if ( textStatus != "success" )
	{
		GetKeyValuePairsCallback( "ajax: " + textStatus );
		return;
	}

	var result;

	try
	{
		result = eval( '(' + data + ')' );
	}
	catch ( exception )
	{
		GetKeyValuePairsCallback( "json parse error" );
		return;
	}

	GetKeyValuePairsCallback( result );
}
