// generic AJAX implementation handler
var msh_ajax_handler = function () {
		// create a new instance of the underlying ajax library
		this.ajax								= new msh_ajax();
		
		// object implementation - properties
		this.requestMethod 			= "GET";		// default on GET
		this.requestHandler			= "/servlet/AjaxHandlerServlet";
		this.callback						= null;
		
		this.targetElementId		= "";					// where should data be written in?
		this.targetElementType	= "select"; 	// the DOM form element type 

		this.status							= -1;					// status codes -1 = error - 0 = OK
		
		// object implementation - methods
		this.addParam				= function(key, value) {
			try {
				this.ajax.add_param(key, value);
				this.status = 0;
			}
			catch(e) {
				this.status = -1;
				alert("Ein Fehler trat in msh_ajax_handler auf. addParam verursachte einen Fehler.\n"+e.message);
			}
			finally {
			};
		};

		this.addData			= function(data) {
			try {
				this.ajax.add_data(data);
				this.status = 0;
			}
			catch(e) {
				this.status = -1;
				alert("Ein Fehler trat in msh_ajax_handler auf. addData verursachte einen Fehler.\n"+e.message);
			}
			finally {
			};
		};
				
		this.read	= function() {
			// main read method which fires the event handler (callback)
			try {
				// prepare the main ajax lib with the provided information
				this.ajax.xml_request_method   	= this.requestMethod;
				this.ajax.xml_request_handler  	= this.requestHandler;
				this.ajax.xml_callback_function	= this.callback;
				this.ajax.read();
				this.status = 0;
			}
			catch(e) {
				this.status = -1;
				alert("Ein Fehler trat in msh_ajax_handler auf. read verursachte einen Fehler.\n"+e.message);
			}
			finally {
			};			
		};
		
		this.getRequest = function() {
			// main read method which returns the current XML HTTP Request Object
			try {
				return this.ajax.xml_http_request;
			}
			catch(e) {
				this.status = -1;
				alert("Ein Fehler trat in msh_ajax_handler auf. getRequest verursachte einen Fehler.\n"+e.message);
			}
			finally {
			};				
		};
		
		this.putData = function (rootNode,withReset)
		{	
				var xml 			= this.getRequest().responseXML;
				var xml_data	= xml.getElementsByTagName(rootNode);
				var el_out		= "";
				var el_value	= "";
				var el_text		= "";
				var el_count	= "";
				var el_obj		= document.getElementById(this.targetElementId);
				
				switch ( this.targetElementType ) {
					case "select":
							
							if ( withReset ) {
								// removes the dropdown elements prior to start the new request
								for ( var i = 0; i < 20; i++ ) {
									for ( var j = 0; j < el_obj.childNodes.length; j++ ) {
										el_obj.removeChild(el_obj.childNodes[0]);
									}
								}
							}

						 for ( var i = 0; i < xml_data.length; i++ ) {
						 		
						 		el_value = "";
						 		el_text  = "";
						 		el_count = "";
	
						 	for ( var j = 0; j < xml_data[i].childNodes.length; j++ ) {
						 		
						 		switch ( xml_data[i].childNodes[j].nodeName ) {
						 			case "key":
						 				el_value = xml_data[i].childNodes[j].firstChild.nodeValue;
						 			break;
				
						 			case "name":
						 				el_text = xml_data[i].childNodes[j].firstChild.nodeValue;
						 			break;
				
						 			case "counter":
						 				el_count = xml_data[i].childNodes[j].firstChild.nodeValue;
						 			break;
						 		}
						 	} 
																	 	
						 	// add to dropdown element
						 	var opt = new Option( el_text + " (" + el_count + ")", el_value );
						 	el_obj.options[i] = opt;		
						}					
						
						break;
					}
		};			
};


