
/*********************************************************************************
 * 
 * 							EDIT AJAX LAUNCHER
 * 
 * @author Rhonda Kammer
 *********************************************************************************
 * This class is meant to function as a general ajax request launcher or in plain
 * text act as a bridge between javascript tools and emporis php logic. This class
 * eliminates the need to reformulate an ajax call inside the edit tool.
 * 
 * The ajax launcher sends the information to the proper requesting javascript method.
 * It knows which method because this is brought in through parameter method. It also
 * knows which tool class is instantiated because in the future all tools will have
 * the handle tool_handle when they are instantiated by the tool factory.
 *
 *
 * @param string url should be 
 * @param array input_data which the edit_ajax should share with the edit_ajax_processor
 * @param string tool the name of the tool making the request
 * @param string method the name of the method making the call. This will most likely
 * 					be from an edit tool. Any method taken in this parameter must be 
 * 					public. See edit_callout_class.prototype.make_edit_callout in edit_callout
 * *******************************************************************************
 * NOTES:
 * 
 * [9oct2009 -rlk] extra post ajax call built to handle large input texts submitted coming
 *                 from tranlation.js
 * 
 * 
 *
 ********************************************************************************/

function ajax_launcher( url, input_data, tool, method ){
	
	//************ PRIVATE CLASS VARIABLES ****************
	var _ajax_url 			= url;
	var _input_data			= input_data;
	var _response_data 		= "";
	var _tool_name 			= tool;
	var _tool_handle 		= "";
	var _calling_method		= method;
	var _function_name 		= "";
	
	//for future work
	//TODO (1) values from edit callout that are never set by children
	//TODO (2) bring rest through input_data as array
	//TODO (3) once complete for every ajax call in every class, remove url parameter
    var _script_uri             = ec.get_script_uri();
    var _session_id             = ec.get_session_id();
	
	//alert('ajax launcher: ' + tool );
	//if( method == "get_object_id" ) alert( "ajax launcher called: tool: " + tool + " method: " + method + " uri: " + _script_uri );
		
	/********************************
	 * 
	 *   ajax_launcher constructor
	 * 
	 *******************************/	
	set_function_name();
	
	var _answer_function = new Function( _function_name);
	 	
	if(tool == 'edit_translation'){		
		 $.post(url, input_data,
				  function(response_data){
			 		eval(_function_name);
				  }, "json");
	}else{
		
		//move inside of process query string
		do_ajax_call();
	}
	 

	/*********************************
	 * 
	 *  private function definitions
	 * 
	 ********************************/

	function do_ajax_call( ){ 
		$.ajax({			
		   type: "POST",
		   url: _ajax_url,
		   contentType: "text/xml",
		   data: _input_data,
		   dataType: "json",
		   success: function( response_data, textStatus ){
			
						if( typeof _answer_function == "function" && typeof ec == "object" ){
													
							//alert('AJAX error: status: ' + textStatus  );          
                        
							eval(_function_name);
							
						}			 				 					     				     		
		   			}
		//comment below (4) lines out for live. Uncomment for debugging
//		,
//			error: function (XMLHttpRequest, textStatus, errorThrown){
//		   	alert('AJAX error: status: ' + textStatus + ' error: ' + errorThrown + ' request: ' + XMLHttpRequest.readyState );
//		   	      }

		});
		return true;
	}
	
	
	function set_function_name(){
		
		// TODO calling tool can append its own handle to method name
		if( _tool_name == "edit_callout" ){			
			_function_name = "ec." + _calling_method + "(response_data)";			
		}
		if( _tool_name == "edit_search" ){			
			_function_name = "es." + _calling_method + "(response_data)";			
		}
		if( _tool_name == "edit_pinpoint" ){			
			_function_name = _calling_method + "(response_data)";
		}
		if(_tool_name == "edit_translation"){		
			_function_name = _calling_method + "(response_data)";
		}
		if(_tool_name == "edit_dropdown"){
			_function_name = _calling_method + "(response_data)";
		}
		if(_tool_name == "edit_value"){
			_function_name = _calling_method + "(response_data)";
		}
		if(_tool_name == "city_page"){
			_function_name = _calling_method + "(response_data)";
		}
		return true;
	}	
	
	
}//cl al






