//  Array of functions to call on window.setTimeout 
var _loadFunctionArray = [];

//  The current load function that is being executed 
var _currentLoadFunction = 0;



//  =================================================================== 
//  DateAjaxControl 
//  ================================================================== 



function dateAjaxControl_YearTextBox_OnKeyUp( dateAjaxControlId )
{ 
	var keyCode = 0;
	var idString = "";
	var control = null;
	
	try
	{ 
		keyCode = window.event.keyCode ? window.event.keyCode : window.event.which ? window.event.which : window.event.charCode;
		//  keyCode = evt.which ? evt.which : window.event.keyCode; 
	}
	catch( e )
	{ 
		return;
	}
	
	if( ( keyCode != 8 ) &&								//  Backspace 
		( keyCode != 46 ) &&							//  Delete 
		( 
			( keyCode < 48 ) ||							//  0-9 
			( 57 < keyCode ) 
		) )
	{ 
		return;
	}
	
	idString = 
		dateAjaxControlId + 
		"__yearTextBox";
	control = document.getElementById( idString );
	if( control )
	{ 
		if( 3 < control.value.length )
		{ 
			idString = 
				dateAjaxControlId + 
				"__monthTextBox";
			control = null;
			control = document.getElementById( idString );
			if( control )
			{ 
				control.focus();
			}
		}
	}
}



function dateAjaxControl_MonthTextBox_OnKeyUp( dateAjaxControlId )
{ 
	var keyCode = 0;
	var idString = "";
	var control = null;
	
	try
	{ 
		keyCode = window.event.keyCode ? window.event.keyCode : window.event.which ? window.event.which : window.event.charCode;
	}
	catch( e )
	{ 
		return;
	}
	
	if( ( keyCode != 8 ) &&								//  Backspace 
		( keyCode != 46 ) &&							//  Delete 
		( 
			( keyCode < 48 ) ||							//  0-9 
			( 57 < keyCode ) 
		) )
	{ 
		return;
	}
	
	idString = 
		dateAjaxControlId + 
		"__monthTextBox";
	control = document.getElementById( idString );
	if( control )
	{ 
		if( 1 < control.value.length )
		{ 
			idString = 
				dateAjaxControlId + 
				"__dayTextBox";
			control = null;
			control = document.getElementById( idString );
			if( control )
			{ 
				control.focus();
			}
		}
	}
}



//  =================================================================== 
//  DateTimeAjaxControl 
//  ================================================================== 



function dateTimeAjaxControl_Load( dateTimeAjaxControlId )
{ 
	//  Hide the showCalendarCheckBox 
	showHide( 
		dateTimeAjaxControlId + 
		"__showCalendarCheckBox" , 
		false 
		);
	
	//  Load current state and do not invert 
	dateTimeAjaxControl_UpdateCalendarVisible( 
		dateTimeAjaxControlId , 
		false 
		);
}

//  Show or hide Calendar based on the previous display 
function dateTimeAjaxControl_CalendarLink_OnClick( dateTimeAjaxControlId )
{ 
	//  Load current state and invert 
	dateTimeAjaxControl_UpdateCalendarVisible( 
		dateTimeAjaxControlId , 
		true 
		);
}

//  Function to load current state from checkBox and either invert (show>hide,hide>show) the state or to reload it 
function dateTimeAjaxControl_UpdateCalendarVisible( 
	dateTimeAjaxControlId , 
	invert )
{ 
	var showCalendarCheckBox = null;
	var calendar = null;
	var visibleBoolean = null;
	
	showCalendarCheckBox = document.getElementById( 
		dateTimeAjaxControlId + 
		"__showCalendarCheckBox" 
		);
		
	if( !showCalendarCheckBox )
	{ 
		return;
	}
		
	visibleBoolean = showCalendarCheckBox.checked;
	if( invert )
	{ 
		visibleBoolean = !visibleBoolean;
		showCalendarCheckBox.checked = visibleBoolean;
	}
	
	showHide( 
		dateTimeAjaxControlId + 
		"_calendar" , 
		visibleBoolean 
		);
}



//  ================================================================== 
//  EnumOtherAjaxControl 
//  ================================================================== 



//  Gets called on page initialization 
function enumOtherAjaxControl_Load( enumOtherAjaxControlId )
{ 
	enumOtherAjaxControl_EnumDropDownList_OnChange( enumOtherAjaxControlId );
}

//  Enum changed 
function enumOtherAjaxControl_EnumDropDownList_OnChange( enumOtherAjaxControlId )
{ 
	var enumDropDownList = null;
	var selectedEnum = null;
	var visibleBool = null;
	
	enumDropDownList = document.getElementById( 
		enumOtherAjaxControlId + 
		"__enumDropDownList" 
		);
		
	if( !enumDropDownList )
	{ 
		return;
	}
	
	//  Getting the selected referral type from the enumDropDownList 
	if( enumDropDownList.selectedIndex == -1 )
	{ 
		selectedEnum = 0;
	}
	else
	{ 
		selectedEnum = enumDropDownList.options[ enumDropDownList.selectedIndex ].value;
	}
	
	visibleBool = ( selectedEnum == 254 );
	
	showHide( 
		enumOtherAjaxControlId + 
		"__otherLabel" , 
		visibleBool 
		);
	showHide( 
		enumOtherAjaxControlId + 
		"__otherTextBox" , 
		visibleBool 
		);
	showHide( 
		enumOtherAjaxControlId + 
		"__otherCustomRequiredFieldValidator" , 
		visibleBool 
		);
}



//  ================================================================== 
//  BooleanDescriptionAjaxControl 
//  ================================================================== 



//  Gets called on page initialization 
function booleanDescriptionAjaxControl_Load( booleanDescriptionAjaxControlId )
{ 
	booleanDescriptionAjaxControl_DropDownList_OnChange( booleanDescriptionAjaxControlId );
}

//  Enum changed 
function booleanDescriptionAjaxControl_DropDownList_OnChange( booleanDescriptionAjaxControlId )
{ 
	var dropDownList = null;
	var selected = false;
	var stateInput = null;
	var visibleBool = null;
	
	dropDownList = document.getElementById( 
		booleanDescriptionAjaxControlId + 
		"__dropDownList" 
		);
	
	if( !dropDownList )
	{ 
		return;
	}
	
	//  Getting the selected referral type from the enumDropDownList 
	if( dropDownList.selectedIndex == -1 )
	{ 
		selected = false;
	}
	else
	{ 
		selected = ( dropDownList.options[ dropDownList.selectedIndex ].value == "1" );
	}
	
	//  Setup state input 
	stateInput = document.getElementById( 
		booleanDescriptionAjaxControlId + 
		"__stateHtmlInputHidden" 
		);
	
	if( !stateInput )
	{ 
		return;
	}
	
	if( stateInput.value == "0" )
	{ 
		visibleBool = !selected;
	}
	else
	{ 
		visibleBool = selected;
	}
	
	showHide( 
		booleanDescriptionAjaxControlId + 
		"__descriptionLabel" , 
		visibleBool 
		);
	showHide( 
		booleanDescriptionAjaxControlId + 
		"__descriptionTextBox" , 
		visibleBool 
		);
}



//  =================================================================== 
//  DbEntityFilterAjaxControl 
//  =================================================================== 



//  Gets called on page initialization 
function dbEntityFilterAjaxControl_Load( dbEntityFilterAjaxControlId )
{ 
	dbEntityFilterAjaxControl_ShowCriteriaCheckBox_OnClick( dbEntityFilterAjaxControlId );
}

//  Show or hide dayTimePanel basde on value of allTimeCheckBox 
function dbEntityFilterAjaxControl_ShowCriteriaCheckBox_OnClick( dbEntityFilterAjaxControlId )
{ 
	var visibleBool = false;
	var showCriteriaCheckBox = null;
	
	showCriteriaCheckBox = document.getElementById( 
		dbEntityFilterAjaxControlId + 
		"__showCriteriaCheckBox" 
		);
	
	if( !showCriteriaCheckBox )
	{ 
		return;
	}
	
	visibleBool = showCriteriaCheckBox.checked;
		
	showHide( 
		dbEntityFilterAjaxControlId + 
		"_criteriaDiv" , 
		visibleBool 
		);
}



//  =================================================================== 
//  ClickOnceButton 
//  ================================================================== 



function clickOnceButton_OnClick( 
	clickOnceButtonId , 
	postBackReference , 
	message , 
	messageControlId )
{ 
	var idString = "";
	var messageControl = null;
	
	//  Disable the button 
	idString = clickOnceButtonId;
	control = document.getElementById( idString );
	if( control )
	{ 
		control.disabled = true;
	}
	
	//  Show the message 
	idString = messageControlId;
	control = document.getElementById( idString );
	if( control && message != "" )
	{ 
		control.innerHTML = message;
	}
	
	//  Launch the post back 
	eval( postBackReference );
}



//  =================================================================== 
//  General Methods 
//  =================================================================== 



function addLoadFunction( functionString )
{ 
	for( i = 0 ; i < _loadFunctionArray.length ; i ++ )
	{ 
		if( _loadFunctionArray[ i ] == functionString )
		{ 
			//  Already contains the load function 
			return;
		}
	}
	
	if( _currentLoadFunction == _loadFunctionArray.length )
	{ 
		window.setTimeout( loadLoadFunctionArray , 250 );
	}
	
	_loadFunctionArray.push( functionString );
}

function loadLoadFunctionArray()
{ 
	var loadFunctionString = "";
	
	if( _currentLoadFunction == _loadFunctionArray.length )
	{ 
		_currentLoadFunction = 0;
	}
	
	for( _currentLoadFunction ; _currentLoadFunction < _loadFunctionArray.length ; _currentLoadFunction ++ )
	{ 
		loadFunctionString = _loadFunctionArray[ _currentLoadFunction ];
		eval( loadFunctionString );
	}
}

// Show or hide element based on visibleBool value
function showHide( 
	elementIdString , 
	visibleBool )
{ 
	var element = null;
	
	element = document.getElementById( elementIdString );
	
	if( !element )
	{ 
		return;
	}
	
	if ( visibleBool )
	{ 
		element.style.display = "block";
	}
	else
	{ 
		element.style.display = "none";
	}
}

function clearDropDownList( dropDownList )
{ 
	//  Clears the dropDownList contents 
	for( var i = dropDownList.options.length - 1 ; -1 < i ; i-- )
	{ 
		dropDownList.options[ i ] = null;
	}
}



//  ================================================================== 
//  Old Code 
//  ================================================================== 

/*
//  =================================================================== 
//  DateAjaxControl 
//  ================================================================== 



function dateAjaxControl_YearTextBox_OnKeyUp( dateAjaxControlId )
{ 
	var idString = "";
	var control = null;
	
	if( ( event.keyCode != 8 ) &&							//  Backspace 
		( event.keyCode != 46 ) &&							//  Delete 
		( 
			( event.keyCode < 48 ) ||						//  0-9 
			( 57 < event.keyCode ) 
		) )
	{ 
		return;
	}
	
	idString = 
		dateAjaxControlId + 
		"__yearTextBox";
	control = document.getElementById( idString );
	if( control )
	{ 
		if( 3 < control.value.length )
		{ 
			idString = 
				dateAjaxControlId + 
				"__monthTextBox";
			control = null;
			control = document.getElementById( idString );
			if( control )
			{ 
				control.focus();
			}
		}
	}
}



function dateAjaxControl_MonthTextBox_OnKeyUp( dateAjaxControlId )
{ 
	var idString = "";
	var control = null;
	
	if( ( event.keyCode != 8 ) &&							//  Backspace 
		( event.keyCode != 46 ) &&							//  Delete 
		( 
			( event.keyCode < 48 ) ||						//  0-9 
			( 57 < event.keyCode ) 
		) )
	{ 
		return;
	}
	
	idString = 
		dateAjaxControlId + 
		"__monthTextBox";
	control = document.getElementById( idString );
	if( control )
	{ 
		if( 0 == control.value.length )
		{ 
			idString = 
				dateAjaxControlId + 
				"__yearTextBox";
			control = null;
			control = document.getElementById( idString );
			if( control )
			{ 
				control.focus();
			}
		}
		else if( 1 < control.value.length )
		{ 
			idString = 
				dateAjaxControlId + 
				"__dayTextBox";
			control = null;
			control = document.getElementById( idString );
			if( control )
			{ 
				control.focus();
			}
		}
	}
}



function dateAjaxControl_DayTextBox_OnKeyUp( dateAjaxControlId )
{ 
	var idString = "";
	var control = null;
	
	if( ( event.keyCode != 8 ) &&							//  Backspace 
		( event.keyCode != 46 ) &&							//  Delete 
		( 
			( event.keyCode < 48 ) ||						//  0-9 
			( 57 < event.keyCode ) 
		) )
	{ 
		alert( event.keyCode );
		return;
	}
	
	idString = 
		dateAjaxControlId + 
		"__dayTextBox";
	control = document.getElementById( idString );
	if( control )
	{ 
		if( 0 == control.value.length )
		{ 
			idString = 
				dateAjaxControlId + 
				"__monthTextBox";
			control = null;
			control = document.getElementById( idString );
			if( control )
			{ 
				control.focus();
			}
		}
	}
}
*/


/*
//  Load method for TimeSheetAjaxUserControl called by onLoadImage 
function dateTimeAjaxControl_OnLoad( dateTimeAjaxControlId )
{ 
	//  alert( "timeSheetAjaxUserControl_OnLoad" ); 
	
	//  Hide the showCalendarCheckBox 
	showHide( 
		dateTimeAjaxControlId + 
		"__showCalendarCheckBox" , 
		false 
		);
	
	//  Load current state and do not invert 
	dateTimeAjaxControl_UpdateCalendarVisible( 
		dateTimeAjaxControlId , 
		false 
		);
}
*/