// Event.observe(window, 'load', init);

// arg_InPlaceCollectionEditorId = salutation
// arg_method_name = edit_salutation
// arg_collection_array = ['Mr.','Mrs.','Ms.','Dr.']
// arg_edited_field = the field in the db that will be edited.
function create_InPlaceCollectionEditor(arg_InPlaceCollectionEditorId, arg_method_name, arg_collection_array, arg_edited_field,arg_extra_parameters)
{
	if ( $(arg_InPlaceCollectionEditorId) != null )
	{
		var old_value = $(arg_InPlaceCollectionEditorId).innerHTML;
	}
	else
	{
		var old_value = '';	
	}
	
	var editor = new Ajax.InPlaceCollectionEditor
	(
	 	arg_InPlaceCollectionEditorId,
		'/ajax.php?method=' + arg_method_name, 
   		{ 
			collection: arg_collection_array,
			cancelControl:"button",
			ajaxOptions: 
			{
				method: 'post'
			},
			callback: 
				function(form, value) 
				{
					return arg_edited_field + '=' + encodeURIComponent(value) + '&' + arg_extra_parameters + '&id=' + id_to_edit;
				},
			onComplete:
				function(transport, element) 
				{
					ajax_require_logged_in(transport.responseJSON.status);
					if (transport.responseJSON.status == 'success')
					{
						element.innerHTML = transport.responseJSON.data[arg_InPlaceCollectionEditorId];
					}
					else
					{
						var error = $(arg_InPlaceCollectionEditorId + '_error');
						error.innerHTML = transport.responseJSON.data_errors[arg_edited_field];
						error.style.display = 'block';
						element.innerHTML = old_value;
					}
				}
		}
	);
	return editor;
}



function create_InPlaceEditor(arg_InPlaceEditorId, arg_method_name, arg_edited_field, arg_extra_parameters, arg_after_update_function )
{
	// Clear all errors on the page when a user clicks on an inplace editor.
	$(arg_InPlaceEditorId).onclick = clear_all_errors;
	
	var old_value = $(arg_InPlaceEditorId).innerHTML;
	var editor = new Ajax.InPlaceEditor
	(
	 	arg_InPlaceEditorId,
		'/ajax.php?method=' + arg_method_name, 
   		{ 
			cancelControl:"button",
			ajaxOptions: 
			{
				method: 'post'
			},
			callback: 
				function(form, value) 
				{
					return arg_edited_field + '=' + encodeURIComponent(value) + '&' + arg_extra_parameters + '&id=' + id_to_edit;
				},
			onComplete:
				function(transport, element) 
				{
					if ( typeof transport == 'undefined' )
					{
						// 20090129:ehf
						// Firebug was throwing an error here.  If the user clicks 'cancel' in the InPlaceEditor,
						// onComplete is still triggered, but 'transport' is undefined.
						// So, if transport is undefined, the user has clicked 'cancel' and we don't need to do anything.
						return 0;
					}
					else
					{
						ajax_require_logged_in(transport.responseJSON.status);
						if (transport.responseJSON.status == 'success')
						{
							element.innerHTML = transport.responseJSON.data[arg_edited_field];
							$(element.id + '_error').style.display = 'none';
							$(element.id + '_error').innerHTML = '';
							
							if ( arg_after_update_function != '' ) window[arg_after_update_function]();
						}
						else
						{
							callback_show_error ( element.id + '_error', transport.responseJSON.data_errors[arg_edited_field] );						
							$(element.id).innerHTML = transport.responseJSON.data[arg_edited_field];
							$(element.id).innerHTML = old_value;
						}
					}
				}
		}
	);
	return editor;
}




function create_Autocompleter ( arg_textbox, 
							   arg_div_choices, 
							   arg_after_update_function, 
							   arg_div_formatted_label, 
							   arg_switch_function, 
							   arg_cancel_button_id,
							   arg_type,
							   arg_indicator,
							   arg_min_chars,
							   arg_li_element,
							   arg_save_method,
							   arg_lookup_chars_param_name,
							   arg_lookup_method,
							   arg_update_element_id,    // update this element with the selected id.
							   arg_extra_params)
{
	
	if ( arg_extra_params != '' && typeof ( arg_extra_params ) != 'undefined' && arg_extra_params != null )
	{
		var extra_lookup_params = arg_extra_params;	
	}
	else
	{
		var extra_lookup_params = '';
	}
	
	
	var editor = new Ajax.Autocompleter( arg_textbox, 
					   	   				 arg_div_choices, 
					   	   				 "/lookup.php?method=" + arg_lookup_method + '&' + extra_lookup_params, 
										{
											afterUpdateElement : after_update_element,
											indicator: arg_indicator,
											paramName: arg_lookup_chars_param_name,
											minChars: arg_min_chars
										});  // end Ajax.Autocompleter.
	
	function after_update_element ( text, li )
	{
		// Update the update_element with the selected id.
		if ( typeof arg_update_element_id != 'undefined' && arg_update_element_id != '' && arg_update_element_id != null)
		{
			$(arg_update_element_id).value = li.id;	
		}

		if ( li.id != '' )
		{
			new Ajax.Request( absolute_link_path + 'ajax.php', 
			{
				method: "post",
				evalJSON: true, 
				sanitizeJSON: true, 
				parameters: arg_li_element + "=" + li.id + "&type=" + arg_type + "&method=" + arg_save_method + '&id=' + id_to_edit + '&' + arg_extra_params,
				onSuccess: function(transport)
				{
					ajax_require_logged_in(transport.responseJSON.status);	
					
					$(arg_textbox).style.display = 'none';
					                    
					if ( arg_cancel_button_id != '' && typeof arg_cancel_button_id != 'undefined' && arg_cancel_button_id != null )
					{
						$(arg_cancel_button_id).style.display = 'none';
					}
					
					if ( arg_div_formatted_label != '' && typeof arg_div_formatted_label != 'undefined' && arg_div_formatted_label != null && $(arg_div_formatted_label).innerHTML != '' && typeof transport.responseJSON.data['location_name'] != 'undefined')
					{
						$(arg_div_formatted_label).innerHTML = transport.responseJSON.data['location_name'];
						$(arg_div_formatted_label).style.display = 'block';
					}
					
					if ( typeof arg_after_update_function !== 'undefined' &&  typeof arg_extra_params === 'undefined' && transport.responseJSON.status == 'success' )
					{
						window[arg_after_update_function](li.id);
					}
					else if ( typeof arg_after_update_function !== 'undefined' &&  typeof arg_extra_params !== 'undefined' && transport.responseJSON.status == 'success' )
					{
						// Pass back an array of extra param values.
						// event_id=11&other=6
						var a_extra_params_1 = new Array();
						var extra_params = new Array();
						var a_extra_params = new Array();
						a_extra_params_1 = arg_extra_params.split('&');
						for ( var i=0; i < a_extra_params_1.length; i++ )
						{
							extra_params = a_extra_params_1[i].split('=');
							a_extra_params[i] = extra_params[1];
						}				
						window[arg_after_update_function](li.id, a_extra_params);
					}
					
				}  // end onSuccess function.
			});  // end Ajax.Request.
		}  // endif li.id blank.
	} // end afterUpdateElement function.
		
	if ( arg_div_formatted_label != '' )
	{
		// When the user clicks the text, we need to display the textbox and cancel button.
		$(arg_div_formatted_label).onclick = function ()
		{
			$(arg_textbox).value = '';
			$(arg_textbox).style.display = 'block';
			if ( arg_cancel_button_id != '' ) $(arg_cancel_button_id).style.display = 'block';
			$(arg_textbox).focus();
			$(arg_div_formatted_label).style.display = 'none';
		}
	}


	if ( arg_cancel_button_id != '' && typeof arg_cancel_button_id != 'undefined' && arg_cancel_button_id != null )
	{
		$(arg_cancel_button_id).onclick = function ()
		{
			$(arg_textbox).style.display = 'none';
			$(arg_cancel_button_id).style.display = 'none';
			if ( arg_div_formatted_label != '' )
			{
				$(arg_div_formatted_label).style.display = 'block';
			}
		}
	}  // endif arg_cancel_button_id != ''. 
	
	
	// 20090408:ehf
	// If arg_div_formatted_label has been passed in, but there is no text ( such as 'Click to Edit' ) inside that div,
	// we'll assume that we just want to keep the textbox displayed.
	if ( arg_div_formatted_label != '' && $(arg_div_formatted_label).innerHTML != '' )
	{
	   $(arg_textbox).style.display = 'none';
	}
	
	
	if ( arg_cancel_button_id != '' && typeof arg_cancel_button_id != 'undefined' && arg_cancel_button_id != null )
	{
		$(arg_cancel_button_id).style.display = 'none';
	}



	return editor;
}  // end create_Autocompleter.


/*
// User clicks the cancel button.
function cancel_autocomplete (arg_div_formatted_label, arg_textbox, arg_cancel_button )
{
	$(arg_textbox).style.display = 'none';
	$(arg_cancel_button).style.display = 'none';
	$(arg_div_formatted_label).style.display = 'block';
}
*/


// NOTE: arg_ajaxOptions_parameters should be passed in the ajaxOptions section, but its not working.
// As a solution, until prototype fixes this code is to urlencode the parameters and pass them in the url
// that we are sending this ajax post to.
// It's the first parameter when you create a CollectionEditor.
// arg_ajaxOptions_parameters should be a string like: 
// foo=bar&tom=jerry&fark=cool
// arg_callback_function:  user-defined function to be called after a selection is made from the dropdown.
// arg_callback_function_params:  parameters passed to the user-defined function above.
// arg_loadCollectionURL_parameters:  params to be sent to the lookup file in case we need it filtered (country_code=150)
function create_InPlaceColletionURLEditor(arg_InPlaceCollectionEditorId, arg_method_name, arg_edited_field, arg_ajaxOptions_parameters, arg_callback_function, arg_callback_function_params, arg_loadCollectionURL_parameters)
{
	if ( arg_loadCollectionURL_parameters === undefined ) var arg_loadCollectionURL_parameters = '';
	
	var old_value = $(arg_InPlaceCollectionEditorId).innerHTML;

	if ( arg_callback_function === undefined || arg_callback_function == '' )
	{
		mycallback = function(form, value) 
		{
			return arg_edited_field + '=' + value + '&method=' + arg_method_name + '&' + arg_ajaxOptions_parameters + '&id=' + id_to_edit;
		}
	}
	else
	{
		mycallback = function (form, value)
		{
			window[arg_callback_function]( value, arg_callback_function_params );
			return arg_edited_field + '=' + value + '&method=' + arg_method_name + '&' + arg_ajaxOptions_parameters + '&id=' + id_to_edit;
		}
	}


	var editor = new Ajax.InPlaceCollectionEditor
	(
	 	arg_InPlaceCollectionEditorId,
		'/ajax.php',
   		{ 
			// loadCollectionURL: "/lookup.php?method=" + arg_method_name + arg_loadCollectionURL_parameters,
			loadCollectionURL: "/lookup.php?method=" + arg_method_name + arg_loadCollectionURL_parameters,
			cancelControl:"button",
			ajaxOptions: 
			{
				method:'post'
			},
			callback: mycallback,
			onComplete:
				function(transport, element) 
				{					
					//eval(transport.responseText);
					ajax_require_logged_in(transport.responseJSON.status);
					
					if (transport.responseJSON.status == 'success')
					{
						element.innerHTML = transport.responseJSON.data[arg_InPlaceCollectionEditorId];
					}
					else
					{
						var error = $(arg_InPlaceCollectionEditorId + '_error');
						error.innerHTML = transport.responseJSON.data_errors[arg_edited_field];
						error.style.display = 'block';
						element.innerHTML = old_value;
					}
				}
		}
	);
	return editor;
}

