


// This is the callback method for the
// reverse ajax date

// If data is valid this function
// updates the date element with
// the data, otherwise an informative
// alert is displayed
function updateDate(data) {
	
	
	if(data != null && typeof data == "object") {
	
		alert(DWRUtil.toDescriptiveString(data, 2));
	}
	else {
		
		DWRUtil.setValue("date", DWRUtil.toDescriptiveString(data, 1));
	}
	
}


function updateNames(data) {


	if(data != null && typeof data == "object") {
	
		alert(DWRUtil.toDescriptiveString(data, 2));
	}
	else {
		
		DWRUtil.setValue("names", DWRUtil.toDescriptiveString(data, 1));
	}
}


// Saves the value in the name element,
// sets the value to "",
// and sets the focus
function saveAndCleanName() {
	
	
	var nameElement = $("name");
	NameService.saveName( nameElement.value );
	nameElement.value = "";
	nameElement.focus();
}



// modified from https://dwr.dev.java.net/servlets/ReadMsg?list=users&msgNo=2629


// Callback for autocompleter that updates the autocompleter with NameBeans from
// the NameService based on the token (prefix) passed.
//
// autocompleter - the autocompleter that called this method.  Calls its setChoices(data) method
// token - the string to autocomplete
function updateNameList(autocompleter, token) {
	
	
    NameService.getNameBeansWithNameLikePrefix( token, 
		function(data) { autocompleter.setChoices(data) } );
}



// this function is fired when a user selects a name from the list
// Callback for autocompleter that does nothing
function nameSelected(inputElement, selectedElement, selectedNameBeanObject) {
	
	// alert(selectedNameBeanObject.id + " " + selectedNameBeanObject.name)
}



// Extracts the name string from a nameBean object
function nameBeanValueSelector(nameBeanObj) {

	return nameBeanObj.name;
}



// Handles the functionality for the hide div
function hideDivOnClick() {
	
	new Effect.SlideUp("nameFormDiv");
	$("show").style.display = "inline";
	$("hide").style.display = "none";
}



// Handles the functionality for the show div
function showDivOnClick() {
	
	new Effect.SlideDown("nameFormDiv");
	$("hide").style.display="inline";
	$("show").style.display="none";
}



// Makes initial call to NameService
// Makes initial call to TimeService
// Makes "date" draggable
// Initializes name autocompleter
function startAjax() {

	NameService.getAllNames(updateNames);

	TimeService.getTime(updateDate);
    new Draggable("dateDialog");
    
    new Autocompleter.DWR("name", "selectList", updateNameList, 
		{ afterUpdateElement: nameSelected, valueSelector: nameBeanValueSelector , handleNonSelectEnter: saveAndCleanName } );
	
	DWREngine.setReverseAjax(true);
}

