var xmlhttp	

function loadXMLDoc(url)
{
	xmlhttp=null
	// code for Mozilla, etc.
	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}if (xmlhttp!=null){
		xmlhttp.onreadystatechange=state_Change
		xmlhttp.open("GET",url,true)
		xmlhttp.send(null)
	}else{
		alert("Your browser does not support XMLHTTP.")
	}
}

function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
	{
	  // if "OK"
		if (xmlhttp.status==200)
		{
			// xmlhttp.status
			// xmlhttp.statusText
			// xmlhttp.responseText
			try{
				loadDataToCookies(xmlhttp.responseText);
			}
			catch(err){
			}
		}
		else
		{
		}
	}
}

function loadDataToCookies(text){

	// this script is used to make shure that the profile information is directly available as cookie information 
	// without having to do a login.	
	
	// Create a DOM tree from the personalisation information in http://*.welt.de/?service=Login
	
	
	// convert string to an XML doc
	
	// code for IE
	if (window.ActiveXObject)
	{
		var doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
		doc.loadXML(text);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else
	{
		var parser=new DOMParser();
		var doc=parser.parseFromString(text,"text/xml");
	}
	
	// Loop trough the DOM tree to find the information for the cookie
	var root = doc.documentElement;
	 for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
		var node = root.childNodes.item(iNode);
		
		// remove #text nodes in firefox
		if (!window.ActiveXObject)
		{
			for (i = 0; i < node.childNodes.length; i++) {
				if(node.childNodes.item(i).nodeName == "#text"){
					node.removeChild(node.childNodes.item(i));
				}
			}
		}
		
		// loop trough all nodes to find RSSfeeds and sorted gallery list
		for (i = 0; i < node.childNodes.length; i++) {
			//debugString += " " + node.childNodes.item(i).nodeName + "\n";
			try{
				//debugString += " " + node.childNodes.item(i).childNodes[0].nodeValue + "\n";
				if(node.childNodes.item(i).childNodes[0].nodeValue == "RSSfeeds"){
				
					// quotes are there becouse access manager also puts quotes around its session cookie values
					createCookie("RSSfeeds","\"" + node.childNodes.item(i-1).childNodes[0].nodeValue + "\"",365);	
				}
				if(node.childNodes.item(i).childNodes[0].nodeValue == "sortedGalleryList"){

					// quotes are there becouse access manager also puts quotes around its session cookie values
					createCookie("sortedGalleryList","\"" + node.childNodes.item(i-1).childNodes[0].nodeValue + "\"",365);
					
					var browser = navigator.userAgent;
					if ( browser.indexOf("Safari")>=0 || browser.indexOf("Konqueror")>=0 ){
						eraseCookie("sortedGalleryList");
					}
				}
			}catch(err){
			}
		}
	}
}

function logoutButton(){
	if(readCookie('userId') != null){
		document.getElementById('LoginRegister').innerHTML = "<a class=\"arrowLink\" href=\"/action?action=logout\">Abmelden</a>";
	}
}
		

if(readCookie("userId") != null){
	loadXMLDoc('/?service=Login');
}else{
	if(readCookie("RSSfeeds") != null){
		eraseCookie("RSSfeeds");
	}
	if(readCookie("sortedGalleryList") != null){
		eraseCookie("sortedGalleryList");
	}
}



