/* 
Preemptive Ajax Search: v1.3 - 21 Digital Web Solutions cc, Werner Rottcher
Date: 2009-07-13
minut_name: Any input field name
mdrop_down_name: drop down box loaded from Ajax Loader
mphp_loader: Ajax PHP file, called by Ajax Loader
mlayer_name: Name of layer where AJAX function returns content to.
msubmit_method: Method will be evaluated and can be any JavaScript!
* DEPENDANCIES
- AJAX Loader Script
- findObj() - Global system function

OBJECT SAFE!, create as many as you need!

Version 1.3
------------
1. Added exception handeling to setInputValue: function()
2. Added isDropDownEventHandlersSet; preventing event handlers being set more times than needed.

Version 1.2
------------
1. Some bug fixes

*/

function preemptive_search(minput_name, mdrop_down_name, mphp_loader, mlayer_name, msubmit_method) {
	var obj = this; //Setting Bind variable
	this.input_name = minput_name;
	input = findObj(this.input_name);
	this.drop_down_name = mdrop_down_name;		
	this.keyPressed = "";
	this.php_loader = mphp_loader; 
	this.layer = mlayer_name;
	this.submit_method = msubmit_method;
	this.isDropDownEventHandlersSet = false;

	if(input.addEventListener) { // DOM2
		input.addEventListener('keyup', function(e) {			
			var theTarget = e.target ? e.target : e.srcElement;
			obj.processKey(e, theTarget);
		}, false);
		input.addEventListener('blur', function(e) {
			obj.getDropDown();
		}, false);
	} else if(input.attachEvent ) { //IE
		input.attachEvent('onkeyup', function(e) {			
			var theTarget = e.target ? e.target : e.srcElement;
			obj.processKey(e, theTarget);
		});
		input.attachEvent('onblur', function(e) {
			obj.getDropDown();
		});
	}		
}	

preemptive_search.prototype={
	
	//now create the event handler function to process the event
	processKey: function (e, object) {			
		if( !e ) {
			//if the browser did not pass the event information to the
			//function, we will have to obtain it from the event register
			if( window.event ) {
				//Internet Explorer
				e = window.event;
			} else {
				//total failure, we have no way of referencing the event
				return;
			}
		}
		if( typeof( e.keyCode ) == 'number'  ) {
			//DOM
			e = e.keyCode;
		} else if( typeof( e.which ) == 'number' ) {
			//NS 4 compatible
			e = e.which;
		} else if( typeof( e.charCode ) == 'number'  ) {
			//also NS 6+, Mozilla 0.9+
			e = e.charCode;
		} else {
			//total failure, we have no way of obtaining the key code
			return;
		}
		this.keyPressed = e;
		this.keyNavigation(e, object);
	},
	
	getDropDown: function () {
		var drop_down = findObj(this.drop_down_name);	
		var obj = this;
		if (obj.isDropDownEventHandlersSet == false) {
			if (drop_down != null) {
				if(drop_down.addEventListener) { //Others DOM2
				  drop_down.addEventListener('keyup', function (e) {
					  obj.processKey(e, drop_down)
				  }, false);
				  drop_down.addEventListener('click', function (e) {
					  obj.setInputValue();	
					  if (drop_down.selectedIndex > 0) {
					  	obj.hideLayer();
					  }
				  }, false);			  
				} else if(drop_down.attachEvent ) { //IE Safe
				  drop_down.attachEvent('onkeyup', function (e) {
					  obj.processKey(e, drop_down)
				  });
				  drop_down.attachEvent('onclick', function (e) {
					  obj.setInputValue();
					  if (drop_down.selectedIndex > 0) {
					  	obj.hideLayer();
					  }
				  });
				}
				obj.isDropDownEventHandlersSet = true;
			}			
		}
	},
	
	setInputValue: function() {
		var input = findObj(this.input_name);
		var drop_down = findObj(this.drop_down_name);
		if ((input != null) && (drop_down != null)) {
            try {
                if (drop_down.options.length > 0) {
                    input.value = drop_down.options[drop_down.selectedIndex].text;
                }
            } catch (exception) {
				//alert(exception);
            }
		}
	},
	
	hideLayer: function() {
		var layer = findObj(this.layer);
		var obj = this;
		if (layer != null) {
			layer.style.visibility = "hidden";
			layer.style.height = "0px";
			layer.innerHTML = "";			
		}
		obj.isDropDownEventHandlersSet = false;
	},
	
	keyNavigation: function(e, object) {
		this.getDropDown();
		var drop_down = findObj(this.drop_down_name);			
		var input = findObj(this.input_name);
		var layer = findObj(this.layer);
		if (this.keyPressed == 40) { //Down Arrow
			if (drop_down != null) {
				drop_down.focus();
			}
		} else if (this.keyPressed == 18) {
          //Must stop drop-down from refreshing when alt is pressed
        } else if(this.keyPressed == 38) { //Up Arrow
		//	input.focus();
		} else if(this.keyPressed == 13) { //Return Key
			try {
				if (object.name == input.name) {
					eval(this.submit_method);
				} else {
					this.setInputValue();
					input.focus();
					this.hideLayer();
				}
			} catch(err) {}
			event.returnValue=false;
			event.cancel = true;
		} else if(this.keyPressed == 8) { //Backspace
			//Hide Drop Down
			if (input.value == "") {
				this.hideLayer();
			}
		} else {
			layer.style.visibility = "visible";
			layer.style.height = "auto";
			this.ajaxLoad();			
		}			
	},
		
	ajaxLoad: function () {
		var input = findObj(this.input_name);
		try {
			this.isDropDownEventHandlersSet = false;
			setLoadingImage(this.layer);
			xmlHttp = new ajaxObject(this.php_loader, this.layer, updateLayer);
			xmlHttp.update("phrase="+input.value+"&input_name="+this.drop_down_name, 'GET');
		} catch(err) {
			alert(err);
		}
	}	
}
