String.prototype.toXmlDocument = function() {
	if (window.ActiveXObject) {
		// Fuer IE:
		var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
		xmldoc.loadXML(this);
		return xmldoc;
	} else {
		return (new DOMParser()).parseFromString(this, "text/xml");
	}
}
function getXPos(elem) {
    var x = 0;
    while (elem && (typeof(elem) == 'object') && (typeof(elem.tagName) != 'undefined')) {
        x += elem.offsetLeft;
        elem = elem.offsetParent;
    }
    return x;
}

Ajax.Dropdown = Class.create();
Ajax.Dropdown.prototype = Object.extend(new Ajax.Request(), {
    initialize: function(url, param, options, source, target) {
        this.ajax       = this; // Because IE needs a special reference.
        this.transport  = Ajax.getTransport();
        this.dataUrl    = url;
        this.dataParam  = param;
        this.sourceElem = source;
        this.targetElem = target;
        this.setOptions(options);
        //alert(this.dataParam);

        // Save previous onComplete-method and create new one:
        var onComplete = this.options.onComplete || Prototype.emptyFunction;
        this.options.onComplete = (function(transport, object) {
            this.ajax.update();
            onComplete(transport, object);
        }).bind(this);
        this.createTarget(this.targetElem);
    },
    setZipCity: function(zip, city, country) {
        this.ajax.zipElem     = zip;
        this.ajax.cityElem    = city;
        this.ajax.countryElem = country;
        /*Hongyi: add two onblur events, to close */
        /*
        zip.onblur=function(){this.ajax.targetElem.ajax.close();};
		city.onblur=function(){this.ajax.targetElem.ajax.close();};
		*/
    },
    getUrl: function() {
        var country = this.ajax.countryElem.options[this.ajax.countryElem.selectedIndex].value;
        var url     = this.ajax.dataUrl;
        url = url.replace(/ctry=.+?(&|$)/, 'ctry='+country);
        return url;
    },
    run: function() {
        //var temp = new Array();
        //for (i in this) { temp.push(i); }
        //temp.sort();
        //alert(this.ajax);
        //alert(temp);
        /*if (this.ajax.countryElem && this.ajax.countryElem.options[this.ajax.countryElem.selectedIndex].value != 'germany') {
            return;
        }*/
        if (this.ajax.sourceElem.value) {
            this.ajax.options.parameters = this.ajax.dataParam+'='+escape(this.ajax.sourceElem.value);
            this.ajax.request(this.ajax.getUrl());
        } else {
            this.ajax.close();
        }
    },
    update: function() {
        if (!this.ajax.responseIsSuccess()) { return; }
        var source  = this.ajax.sourceElem;
        var target  = this.ajax.targetElem;
        var result  = this.ajax.transport.responseText.toXmlDocument();
        var impData = result.getElementsByTagName('importantItems');
        var data    = result.getElementsByTagName('items');


        if (result.getElementsByTagName('item').length) {
            this.open();
            while (target.hasChildNodes()) { target.removeChild(target.lastChild); }
            var closer       = target.appendChild(document.createElement('span'));
            closer.className = 'ajaxCloser';
            closer.onclick   = function() { target.ajax.close(); }
            closer.title     = 'Schließen';
            var filler = target.appendChild(document.createElement('ul'));
            if (impData.length) {
                this.updateBox(impData.item(0).getElementsByTagName('item'), source, target, filler);
            }
            if (data.length) {
                this.updateBox(data.item(0).getElementsByTagName('item'), source, target, filler);
            }
        } else {
            this.close();
        }
    },
    updateBox: function(items, source, target, filler) {
        for (var i=0; i<items.length; i++) {
            var li = filler.appendChild(document.createElement('li'));
            var item = items[i];
            //li.onclick = new Function('',
            //    'document.getElementById("'+source.id+'").value = "'+item.getAttribute('zip')+'";\n' +
            //    'document.getElementById("'+target.id+'").value = "'+item.firstChild.nodeValue+'";'
            //)
            li.item = item;
            li.ajax = target.ajax;
            li.onclick = new Function('',
                'document.getElementById("'+this.zipElem.id+'").value  = this.item.getAttribute("zip");\n' +
                'document.getElementById("'+this.cityElem.id+'").value = this.item.firstChild.nodeValue;\n' +
                'this.ajax.close();'
            );
            if (1||typeof(document.all) != 'undefined') {
                li.onmouseover = new Function('', 'this.className="hover"');
                li.onmouseout  = new Function('', 'this.className=""');
            }
            var html = ' '+(item.getAttribute('zip')+'.....').substr(0,5)+' '+(item.getAttribute('code') ? item.getAttribute('code')+' ' : '')+item.firstChild.nodeValue;
            var re   = new RegExp(' ('+source.value+')', 'ig');
            if (html.match(re)) {
            	html = html.replace(re, ' <b>'+RegExp.$1+'</b>');
            }
            li.innerHTML = html;
        }
    },
    open: function() {
        this.targetElem.style.display = 'inline';
    },
    close: function() {
        this.targetElem.style.display = 'none';
        return false;
    },
    createTarget: function(target) {
        /*
        var outer = document.createElement('div');
        outer.className = 'ajaxParent';
        target.parentNode.insertBefore(outer, target.nextSibling.nextSibling);
        outer.appendChild(target);
        var inner = document.createElement('div');
        inner.className = 'ajaxResult';
        //inner.style.left = getXPos(target)+'px';
        //alert(inner.style.left);
        */
        target.className = 'ajaxResult';
        target.ajax = this;
        //target.appendChild(inner);
        return target;
    }
});

