// Funktion holt die aufbereiteten Daten per XMLRequest, schreibt sie in die HTML Seite und ersetzt die toc-Links durch js Funktion "onClick"  
function getNewHttpObject() { 
	var objType = false; 

	try {
		objType = new ActiveXObject('Msxml2.XMLHTTP');
	} catch(e){
		try {
			objType = new ActiveXObject('Microsoft.XMLHTTP');
		} catch(e){
			objType = new XMLHttpRequest();
		}
	}

	return objType;
}


function changeTabs(context,container) {
	var tabs;
	container = "tabcontainerReiter";
	if(document.getElementById(container)) {
		tabs = document.getElementById(container).getElementsByTagName('li');
		
		for(i=0; i<tabs.length; i++){				
			var count = i;
			if(tabs[i].firstChild) {
				if(tabs[i].firstChild.nodeName == 'A') {				
					tabs[i].firstChild.onclick = function(e) {	
					
						getAjaxData(context,this,container);	//Anpassen je nach dem wie die URL aussehen soll
					}								
				}				
			}		
		}		
	}	
}

//function getAjaxData(context, aid, type, realm, language, rdid)
function getAjaxData(context, e, container) {

	var id = -1;
	var aid = e.getAttribute("id");

	e.removeAttribute("href");
	
	//Daten sind bereits geladen
	if(document.getElementById('tabContainer'+aid)) {	
		
		showTabContainer(aid, container);
		return false;
	
	} 
	else //Daten per Ajax nachladen
	{	
		var req = getNewHttpObject();
		var url = "" + context + aid + ".html?view=renderFull";
	
		//Daten holen
		req.open("GET", url, true);
		req.onreadystatechange = function() {
				// Prüfen des Fortschritts 	
				if(req.readyState === 0) {		  // 0= "uninitialized"  
					// nothing to do
				} else if (req.readyState == 1) { // 1=Request wird gestartet, aber noch nicht abgeschickt
					// nothing to do
				} else if (req.readyState == 2) { // 2=Request wurde ausgeführt, auf Server-Antwort warten
					// nothing to do
				} else if (req.readyState == 3) { // 3=Request ausgeführt und Teile der Antwort sind schon im Puffer
					// nothing to do
				} else if (req.readyState == 4) { // 4=Request ausgeführt und Antwort des Servers erhalten, Up-/Download abgeschlossen
					// Wenn Statuscode = 200 oder "OK", Anfrage erhalten und Daten sind in Serverantwort	
				if (req.status == 200 || req.status == "OK") {
									
						//Tabconatiner in den DOM Baum einhängen 						
						var tabContainer = document.createElement("div");
						tabContainer.className = 'tabBoxContent';
						tabContainer.setAttribute('id', 'tabContainer' + aid);
						tabContainer.innerHTML = req.responseText;	
						
						
						var tabLink = getElementsByClassName("tabBox");
						tabLink = tabLink[0].firstChild;


						if (tabLink.insertBefore) {							
							var tl = getElementsByClassName("tabBox");
							tl = tabLink;
							tl.parentNode.insertBefore(tabContainer,((tl.nextSibling).nextSibling).nextSibling);
						}	
						
						document.getElementById(aid).onclick = function(e){
							showTabContainer(aid,container);
							return false;
						};
				
						showTabContainer(aid,container);
							return false;
		
						
					} else {
						//document.getElementById(elementContainer).innerHTML = "ERROR: "+req.statusText; 
					}
				}
			};

			// Anfrage gesendet
			req.send(null);
		}
	}
		




function showTabContainer(e, container) {
	var tabBoxes = getElementsByClassName("tabBoxContent");
	var tabBox = getElementsByClassName("tabBox");
	if(document.getElementById(container)) {
		tabs = document.getElementById(container).getElementsByTagName('li');

		for(i=0; i<tabs.length; i++){				
			if(tabs[i].firstChild) {
				if(tabs[i].firstChild.nodeName == 'A') {					
					tabs[i].firstChild.setAttribute("class", "none");
					tabs[i].firstChild.setAttribute("className", "none");			
				}				
			}		
		}		
	}	
	
	
	for (i=0; i < tabBoxes.length;i++){		
		tabBoxes[i].style.visibility = "hidden";
		tabBoxes[i].style.display = "none";		
	}
	


	tabBoxID = document.getElementById('tabContainer' + e);
	tabBoxID.style.visibility = "visible";
	tabBoxID.style.display = "block";
	tabBoxTab = document.getElementById(e);
	tabBoxTab.setAttribute("class", "current");
	tabBoxTab.setAttribute("className", "current");
	tabBoxTab.blur();
}


 
function getElementsByClassName(class_name) {
	var all_obj;
	var ret_obj=new Array();
	var j=0;
	var teststr;

	if (document.all) {
		all_obj=document.all;
	} else if (document.getElementsByTagName && !document.all) {
		all_obj=document.getElementsByTagName("*");
	}

  	
	for (i = 0; i < all_obj.length; i++) {
		if(all_obj[i].className){
		if (all_obj[i].className.indexOf(class_name) != -1) {
			teststr = "," + all_obj[i].className.split(" ").join(",") + ",";
	
			if (teststr.indexOf("," + class_name+",") != -1) {
				ret_obj[j] = all_obj[i];
				j++;
			}
		}
		}
		
	}	

	return ret_obj;
}


