
/**
 * Diese Klasse dient zur Kontrolle der konfortablen Suche 
 * mit Hilfe eines Textfeldes und einer Auswahlliste.
 *
 */
function TextboxControl(){
	XPlatform.call(this);
	// Code nur einmal ausfhren
	//
	if( typeof TextboxControl._initialized == "undefined" ){
	
		TextboxControl.prototype.onEnterSubmitFormAndClose = function(oEvent, ctrlID, act, regex){
			var event = (typeof oEvent == "undefined") ? window.event : oEvent;
            event.returnValue=true;
            var tgt = this.getEventTarget(event);
			var key = event.keyCode;
			var sControlerId = this.getCrtlId(tgt.id);
			var sType = this.getType(tgt.id);
			if(key == 13){ // Enter
				submitForm(ctrlID, act, regex);
				oCM.closeLayer();
			}else{
				return true;
			}
			return true;
		}
	
		TextboxControl.prototype.onEnterSubmitForm = function(oEvent, ctrlID, act, regex){
			var event = (typeof oEvent == "undefined") ? window.event : oEvent;
            event.returnValue=true;
            var tgt = this.getEventTarget(event);
			var key = event.keyCode;
			var sControlerId = this.getCrtlId(tgt.id);
			var sType = this.getType(tgt.id);
			if(key == 13){ // Enter
				submitForm(ctrlID, act, regex);
				oCM.closeLayer();
			}else{
				return true;
			}
			return true;
		}	
	
		/**
		 * Methode als Event-Listener für Tastatureingaben
		 * des Input-Feldes. 
		 */
		TextboxControl.prototype.keyDown = function(oEvent){
			var event = (typeof oEvent == "undefined") ? window.event : oEvent;
            event.returnValue=true;
            var tgt = this.getEventTarget(event);
			var key = event.keyCode;
			var sControlerId = this.getCrtlId(tgt.id);
			var sType = this.getType(tgt.id);
			if(key == 40){       // Down arrow
				if(this.hasList(sControlerId)){
					this.selectNextElement(sControlerId);
				}else{
					sendMessage(sControlerId, 'actDownArrow');
				}
			}else if(key == 38){ // Up arrow
				if(this.hasList(sControlerId)){
					this.selectPrevElement(sControlerId);
				}else{
					sendMessage(sControlerId, 'actUpArrow');
				}
			}else if(key == 13){ // Enter
				if(this.hasList(sControlerId)){
					var sId = this.getSelectedElement(sControlerId).getAttribute("id");
					this.deselectAll(sControlerId);			
					sendMessage(sControlerId, 'actSelect', sId);
					//alert("sendMessage('"+sControlerId+"','actSelect', '" + sId + "')");
				}else{
					sendMessage(sControlerId, 'actEnter', tgt.value);
				}
			}else if((key >= 65 && key <=95) || (key >= 48 && key <= 59) || key == 0 || key == 8){ // a-z o-9 Backspace öüäß
     			if(tgt.value != '')
	     			sendMessage(sControlerId, 'actKeyDown', tgt.value);
				//alert("sendMessage('"+sControlerId+"','actKeyDown', '" + tgt.value + "')");
			}else if(key == 27){
					sendMessage(sControlerId, 'actEscape');
			}else{
				//alert(key);
			}
			return true;
		}	
		
		/**
		 * Methode löscht sämliche Selektionen der Auswahlliste 
		 */
		TextboxControl.prototype.deselectAll = function(sId){
			if(this.hasList(sId)){
				var n = document.getElementById(sId+".listNotEmpty");
				for(var i=0; i< n.childNodes.length; i++){
					if(n.childNodes[i].nodeName == "DIV"){
						var divNode = n.childNodes[i];
						divNode.setAttribute('selected', "false");
						divNode.setAttribute('class', 'mouse_out');
					}
				}
			}
		}
		
		/**
		 * Methode zur Auswahl des nächste Listenelements in der 
		 * Auswahlliste.
		 */
		TextboxControl.prototype.selectNextElement = function(sId){
			var n = document.getElementById(sId+".listNotEmpty");
			var markNext = false;
			for(var i=0; i< n.childNodes.length; i++){
				if(n.childNodes[i].nodeName == "DIV"){
					var divNode = n.childNodes[i];
					if(markNext==true || this.getSelectedElement(sId)==null){
						divNode.setAttribute('selected', "true");
						divNode.setAttribute('class', 'mouse_over');
						divNode.focus();
						divNode.scrollIntoView();
						break;
					}
					if(divNode == this.getSelectedElement(sId)){
						divNode.setAttribute('selected', "false");
						divNode.setAttribute('class', 'mouse_out');
						markNext = true;
					}
				}
			}
		}

		/**
		 * Methode zur Auswahl des vorherigen Listenelements in der 
		 * Auswahlliste.
		 */
		TextboxControl.prototype.selectPrevElement = function(sId){
			var n = document.getElementById(sId+".listNotEmpty");
			var markNext = false;
			for(var i=n.childNodes.length-1; i>=0; i--){
				if(n.childNodes[i].nodeName == "DIV"){
					var divNode = n.childNodes[i];
					if(markNext==true || this.getSelectedElement(sId)==null){
						divNode.setAttribute('selected', "true");
						divNode.setAttribute('class', 'mouse_over');
						divNode.focus();
						divNode.scrollIntoView();
						break;
					}
					if(divNode == this.getSelectedElement(sId)){
						divNode.setAttribute('selected', "false");
						divNode.setAttribute('class', 'mouse_out');
						markNext = true;
					}
				}
			}
		}


		/**
		 * Diese Methode liefert einen Zeiger auf das selektierte 
		 * List-Element, falls eins selektiert wurde. Ansonsten wird
		 * null zurück geliefert.
		 *
		 */		
		TextboxControl.prototype.getSelectedElement = function(sId){
			var n = document.getElementById(sId+".listNotEmpty");
			var oNode = null;
			for(var i=0; i< n.childNodes.length; i++){
				if(n.childNodes[i].nodeName == "DIV"){
					var divNode = n.childNodes[i];
					try{
						if(divNode.getAttribute('selected') == "true"){
							oNode = divNode;
							break;
						}
					}catch(e){
						oNode=null;
					}
				}
			}
			return oNode;
		}
		
		
		/**
		 * Methode wird aufgerufen, wenn der Benutzer mit der 
		 * Maus ein Listelement überfährt.
		 */
		TextboxControl.prototype.over = function(tgt){
			tgt.setAttribute('class', 'mouse_over');
		}
		
		/**
		 * Methode wird aufgerufen, wenn der Benutzer mit 
		 * der Maus ein Listelement verläßt.
		 */
		TextboxControl.prototype.out = function(tgt){
			tgt.setAttribute('class', 'mouse_out');
		}
		
		/**
		 * Methode ermittelt die Klasse aus einem id Attribut 
		 * eines HTML-Tags
		 */
		TextboxControl.prototype.getCrtlId = function(sId){
			var aSplit = sId.split(".");
			return aSplit[0];		
		}
		
		/**
		 * Methode ermittelt die Methode aus einem id Attribut 
		 * eines HTML-Tags
		 */
		TextboxControl.prototype.getType = function(sId){
			var aSplit = sId.split(".");
			return aSplit[1];
		}
		
		/**
		 * Methode liefert true, fals eine Auswahlliste vorhanden ist,
		 * ansonsten wird false zurück geliefert.
		 */
		TextboxControl.prototype.hasList = function(sId){
			n = document.getElementById(sId+".listNotEmpty");
			if(typeof(n) == 'undefined' || n == null){
				return false;
			}
			return true;
		}
		
	TextboxControl._initialized = true;
	}// END if(typeof DateCrtl._initialized == "undefined")
	
}// END TextboxControl

TextboxControl.prototype = new XPlatform();
oTextboxControl = new TextboxControl();
