var idx;
var dropDown = [];

function buildXMLDropDown(xmlUrl, dropDownId, xmlNode, xmlAtt){
	//filter different domains
	var hName = window.location.hostname;
	var domain = "goethe.de";
	var endPos = xmlUrl.indexOf(domain) + domain.length;
	if(endPos != -1) xmlUrl = "http://" + hName + xmlUrl.substring(endPos);	
	//create dropdown instance if not within 'cms.goethe'
	var host = hName.substring(0,3);	
	if(host != 'cms') dropDown[idx] = new XMLDropDown(xmlUrl, dropDownId, xmlNode, xmlAtt);
	idx ++;
}

function XMLDropDown(xmlUrl, dropDownId, xmlNode, xmlAtt) {
	this.isIE;
	this.req;
	this.url = xmlUrl;
	this.id = dropDownId;
	this.node = xmlNode;
	this.att = xmlAtt;
	this.loadXMLDoc(this.url);
}

XMLDropDown.prototype.loadXMLDoc = function(u) {
    //add timestamp to prevent caching in IE
    var d = new Date();
    var url= u + "?d="+d.getDate()+d.getHours()+d.getMinutes()+d.getMilliseconds();

    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        this.req = new XMLHttpRequest();
        this.req.onreadystatechange = bind(this.processReqChange,this);
        this.req.open("GET", url, true);
        this.req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        this.isIE = true;
        this.req = new ActiveXObject("Microsoft.XMLHTTP");
        if (this.req) {
            this.req.onreadystatechange = bind(this.processReqChange,this);
            this.req.open("GET", url, true);
            this.req.send();
        }
    }
}

// handle onreadystatechange event of req object
XMLDropDown.prototype.processReqChange = function() {
    // only if req shows "loaded"
    if (this.req.readyState == 4) {
        // only if "OK"
        if (this.req.status == 200) {
	     //alert(this.req.getAllResponseHeaders());
           this.buildDropDown();
         } else {
            //alert("There was a problem retrieving the XML data:\n" + this.req.statusText);
         }
    }
}

XMLDropDown.prototype.getNodeValue = function(node){
	if (node) {
        	if (node.childNodes.length > 1) {
            	return node.childNodes[1].nodeValue;
	      } else {
    	        return node.firstChild.nodeValue;    		
        	}		
	} else {
     	   return "n/a";
	}
}

XMLDropDown.prototype.buildDropDown = function() {
    var select = document.getElementById(this.id);
    var items = this.req.responseXML.getElementsByTagName(this.node);
    for (var i = 0; i < items.length; i++) {
    	var opt;
    	opt = document.createElement("option");
    	opt.value = items[i].getAttribute(this.att);
    	opt.appendChild(document.createTextNode(this.getNodeValue(items[i])));
    	select.appendChild(opt);
    }
}


function bind (fn, scope) {
    var scope = scope || window;
    var args = [];
    for (var i=2, len = arguments.length; i < len; ++i) {
        args.push(arguments[i]);
    };
    return function() {
	    fn.apply(scope, args);
    };
}
