var agt = navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf('msie') != -1) && (agt.indexOf('opera') == -1));

function clearNode(node){
	while (node.firstChild){
		node.removeChild(node.firstChild);
	}
	return node;
}
function setClass(element, className){
	if (element && element.className.indexOf(className) == -1){
		element.className += ' ' + className;
	}
}
function removeClass(element, className){
	if (element){
		element.className = element.className.replace(className, '');
	}
}

function AutoComplete(data, input, select,button,field_name){

	this.data = data;
	this.input = input;
	this.select = select;
	this.button = button;
	this.field_name = field_name;
	this.blur_s=0;
	this._init();
}
AutoComplete.prototype = {
	_init: function(){
        var _this = this;

		this.input.onkeypress = function(e){return _this.inputOnKey(e)};
		this.input.onkeydown = function(e){return _this.inputOnKey(e)};
		this.input.onkeyup = function(e){return _this.inputOnKey(e)};

		EventFactory.create('click', function(e){_this.buttonClick()}, this.button, true);
      EventFactory.create('keyup', function(e){_this.onKeyUp(e); _this.onaction(e);}, this.input, true);
		EventFactory.create('keyup', function(e){_this.documentKeyUp(e)}, document, true);
		EventFactory.create('blur', function(e){_this.onBlur1(e)}, this.input, true);
		EventFactory.create('blur', function(e){_this.onBlur1(e)}, this.button, true);
		EventFactory.create('blur', function(e){_this.select.onBlur2(e)}, this.select.object, true);
		EventFactory.create('blur', function(e){_this.select.onBlur2(e)}, this.button, true);

		this.select.onchange = function(e){
			_this.onselect();
			_this.doSelect(e);
		};
		this.select.onBlur2 = function(e){
			_this.blur_s=1;
		};
	},
	onaction: function(e){},
    onselect: function(){
    		//if(this.select.object.childNodes.item(this.select.getSelected())!=-1)
    		i=this.select.getSelected();
    		//alert(i.value);
    		if(i==null||(i!=null&&i.value!=-1)){
      		value1 = this.select.getSelectedText() || this.input.value;
				if(value1.indexOf(' (')>0)
					value1 = value1.substring(0,value1.indexOf(' ('));
				//alert(value1);
      		this.input.value = value1;
      	}
    },
	documentKeyUp: function(e){
		if (e.keyCode == 27){
			this.hide();
		}
	},
	inputOnKey: function(e){
		var evt = e || window.event;

		if (evt.keyCode == 13 /*|| evt.keyCode == 40*/|| evt.keyCode == 38 || evt.keyCode == 27){
			return false;
		}
		return true;
	},
	onKeyUp: function(e){
		var value = this.trim(this.input.value);
		if (!value&&e.keyCode!=40){
            this.hide();
            return;
		}
		switch (e.keyCode){
			case 27:
				return;
			case 38:
				this.moveUp();
				return;
			case 40:
				if(value==''&&this.field_name!="street"){
        			this.showAll();
				}
				else
					this.moveDown();
				return;
			case 13:
				this.onselect(e);
				this.doSelect(e);
				return;
		}

        var html = this.data.match(new RegExp('<o [^>]*>' + value + '[^<]*</o>', 'ig'));
        if (!html){
            this.hide();
            return;
        }
		this._fillSelect(html);
		if (html.length == 1 && (html[0].match(new RegExp('<o [^>]*>' + value + '</o>', 'ig'))||
							html[0].match(new RegExp('<o [^>]*>' + value + ' \([^>]*)</o>', 'ig')))){
			this.onselect();
			this.doSelect(e);
		}
	},
	moveUp: function(){
		this.select.moveUp();
	},
	moveDown: function(){
		this.select.moveDown();
	},
	onBlur1: function(e){
		var _this = this;
		window.setTimeout(function(){/*if(_this.blur_s==0)*/_this.hide()}, 1000);
			if(_this.blur_f>2) _this.hide();
	},
	hide: function(){
		this.select.display(false);
	},
	show: function(){
		this.select.display(true);
	},
	doSelect: function(){
		this.hide();
	},
	_fillSelect: function(html){
		this.select.fill(html.join(''));
		this.show();
	},
	trim: function (str){
		return str.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '');
	},
	showAll: function(){
		var html = this.data.match(new RegExp('<o [^>]*>' +  '[^<]*</o>', 'ig'));
        			if (!html){
            		this.hide();
            		return;
        			}
        			this._fillSelect(html);
        			if(this.input.value!=''){
        				for (var i = 0, l = html.length; i < l; i++){
							if (html[i].match(new RegExp('<o [^>]*>' + this.input.value + '</o>', 'ig'))||
							html[i].match(new RegExp('<o [^>]*>' + this.input.value + ' \([^>]*)</o>', 'ig'))){
								this.select.object.selectedIndex = i;
								this.select._changeSelected(i);
								this.onselect();
							}
						}
					}
					else
						this.onselect();
	},
	buttonClick: function(){
		var flag_button = this.select.object.style.display;
		if(this.select.object.innerHTML!=''){
			if(flag_button=='block')
				this.select.object.style.display='none';
			else if(flag_button=='none')
				this.select.object.style.display='block';
		}
		else{
			if(flag_button=='block')
				this.hide();
			else if(flag_button=='none'){
					this.showAll();
			}
		}
	}
}

if (typeof(Function.prototype.apply) == 'undefined')
{
	Function.prototype.apply = function(context)
	{
		var name = '__apply' + new Date().getTime() + Math.floor(Math.random() * 10000000) + '__';

		context = context || window;
		context[name] = this;

		var args = [];

		if (arguments[1]) {
			var len = arguments[1].length;
			args = new Array(len);
			for (var i = 0; i < len; i++) {
				args[i] = 'arguments[1][' + i + ']';
			}
		}

		var ret = eval('context[name](' + args.join(',') + ')');
		delete context[name];

		return ret;
	}
}
