
// LOCATION DROPDOWN
// (c) 2006 by klickTel AG, Essen.
// All rights reserved. Unauthorized duplication and use prohibite

var visible = new Array;
var initialized = new Array();
var cookiesKilled = false;
var openButton;

function button_up (layername){
	image = document.getElementById("btn_"+layername).childNodes[0];
	image.setAttribute("src", "/global/images/standort_up.gif");
}

function button_down (layername){
	image = document.getElementById("btn_"+layername).childNodes[0];
	image.setAttribute("src", "/global/images/standort_down.gif");
}

function isInternal (){
	if (self.location.href.search (/\.int/) != -1){
		return true;
	}
	else
		return false;
}

function isKlickUrl (){
	if (self.location.href.search (/\.klickurl\.de/) != -1){
		return true;
	}
	else
		return false;
}

function clear_kt_locationCookie (){

	var myAjax = new Ajax.Request(
        	'/standort_ajax_handler.php',
		    {
	            method: 'get',
	            parameters: 'action=clearCookie',
	            onError: function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}
	         });

	lists = document.getElementsByTagName("ul");
	for (var j=0; j<= lists.length-1; j++){
		if (lists[j].getAttribute("class") == "cookieList"){
			thisList = lists[j];
			for (var i=thisList.childNodes.length-1; i>=0; i--){
				thisList.removeChild(thisList.childNodes[i]);
			}
		}
	}

	cookiesKilled = true;
}

function showInfoLayer (layername){
	infoLayer = document.createElement("div");
	infoLayer.style.backgroundColor = "#ffffff";
	infoLayer.style.border = "1px solid #999999";
	infoLayer.style.position = "absolute";
	infoLayer.style.padding = "5px";
	infoLayer.style.width="170px";
	infoLayer.style.height="120px";
	infoLayer.style.fontWeight = "normal";

	infoText = document.createTextNode("Ihre Standorte können Sie selbst erstellen und ändern. Klicken Sie dazu auf das 'myklickTel' Icon oben in der Navigation und loggen Sie sich ein. Wenn Sie noch keinen klickTel Account haben, können Sie sich kostenlos anmelden!");
	infoLayer.appendChild(infoText);
	infoLayer.setAttribute("id", layername+"_infoLayer");
	document.getElementById(layername+"_databaseList").appendChild(infoLayer);
}

function hideInfoLayer (layername){
	infoLayer = document.getElementById(layername+"_infoLayer");
	document.getElementById(layername+"_databaseList").removeChild(infoLayer);
}

function fillfields (streetfield, streetnumberfield, zipfield, cityfield, countryfield, longlatfield, streetvalue, streetnumbervalue, zipvalue, cityvalue, countryvalue, longlatvalue, alortvalue, layername){

	btns = document.getElementsByClassName ("locationbutton");
	if (btns.length > 0) {
		for (i=0; i<btns.length; i++) {
			btns[i].style.backgroundImage = 'url(/global/images/dropdown.png)';
		}	
	}
	
	if (streetfield==streetnumberfield){
		if (combinedStreetNumberField =  document.getElementById(streetfield)){
			combinedStreetNumberValue = "";
			if (streetvalue != "")
				combinedStreetNumberValue = streetvalue+" ";
			if (streetnumbervalue != "")
				combinedStreetNumberValue += streetnumbervalue;
			combinedStreetNumberField.value = combinedStreetNumberValue;
		}
	}
	else {
		StreetField = document.getElementById(streetfield);
		if (StreetField != null){
			StreetField.value = streetvalue;
		}
		StreetNumberField = document.getElementById(streetnumberfield);
		if (StreetNumberField != null){
			StreetNumberField.value = streetnumbervalue;
		}
	}


	if (layername == 'menuLayerBadeSearch')
		if (streetvalue != '')
			showHide('show','SPREADLOCATION');
		else
			showHide('hide','SPREADLOCATION');

	if (layername == 'menuLayerBadeSearch')
		if (cityvalue != '')
			showHide('show','SPREADLOCATION');
		else
			showHide('hide','SPREADLOCATION');

	if (layername == 'menuLayerBadeSearchTP')
		if (streetvalue != '')
			showHide('show','SPREADLOCATION_TP');
		else
			showHide('hide','SPREADLOCATION_TP');

	if (layername == 'menuLayerBadeSearchTP')
		if (cityvalue != '')
			showHide('show','SPREADLOCATION_TP');
		else
			showHide('hide','SPREADLOCATION_TP');

	if (zipfield==cityfield){
		if (combinedZipCityField = document.getElementById(zipfield)){
			combinedZipCityValue = "";
			if (zipvalue != "")
				combinedZipCityValue = zipvalue+" ";
			if (cityvalue != "")
				combinedZipCityValue += cityvalue;
			combinedZipCityField.value = combinedZipCityValue;
		}
	}
	else {
		ZipField = document.getElementById(zipfield);
		if (ZipField != null){
			ZipField.value = zipvalue;
		}
		CityField = document.getElementById(cityfield);
		if (CityField != null){
			CityField.value = cityvalue;
		}
	}
	CountryField = document.getElementById(countryfield);
	if (CountryField != null){
		if (CountryField.type == 'select-one'){
			var options = CountryField.options;
			for(var i = 0; i < options.length; i++) {
				if (CountryField.options[i].value == countryvalue){
					CountryField.options.selectedIndex = i;
				}
			}
		} else {
			CountryField.value = countryvalue;
		}
	}
	LonglatField = document.getElementById(longlatfield);
	if (LonglatField != null){
		if (longlatfield.search('alort') == -1 && longlatfield != 'lid') {
			LonglatField.value = streetvalue+" "+streetnumbervalue+"::"+zipvalue+" "+cityvalue+"::"+countryvalue+"::"+longlatvalue;
		} else {
			LonglatField.value = alortvalue;
		}
	}
	document.getElementById(layername).style.display="none";
	visible[layername] = false;

}

function updateMenuLayer (user_id, responseXML, locationcookie, streetfield, streetnumberfield, zipfield, cityfield, countryfield, longlatfield, layername){
	headerspan = document.createElement("div");
	headerspan.className = "header";

	headerspanTable = document.createElement("table");
	headerspanTBody = document.createElement("tbody");
	headerspanTableRow = document.createElement("tr");
	headerspanTableLeftCell = document.createElement("td");
	headerspanTableRightCell = document.createElement("td");
	headerspanTable.setAttribute("width", "100%");
	headerspanTable.setAttribute("cellspacing", "0");
	headerspanTable.setAttribute("cellpadding", "0");
	headerspanTable.setAttribute("border", "0");
	headerspanTableLeftCell.setAttribute("align", "left");
	headerspanTableRightCell.setAttribute("align", "right");
	header = document.createTextNode("Zuletzt gesuchte Orte");
	historyClear = document.createElement("a");
	linktext = document.createTextNode("Historie l"+String.fromCharCode(246)+"schen");
	historyClear.appendChild(linktext);
	headerspanTableLeftCell.appendChild(header);
	headerspanTableRightCell.appendChild(historyClear);
	headerspanTableRow.appendChild(headerspanTableLeftCell);
	headerspanTableRow.appendChild(headerspanTableRightCell);
	headerspanTBody.appendChild(headerspanTableRow);
	headerspanTable.appendChild(headerspanTBody);
	headerspan.appendChild(headerspanTable);
	document.getElementById(layername).appendChild(headerspan);

	cookielocations = locationcookie.split("||");
	cookieList = document.createElement("ul");
	for (var i=0; i<cookielocations.length-1; i++){
		li = document.createElement("li");
		locationelements = cookielocations[i].split(";;");
		
		var textnode = " ";
		var count = 0;
		
		for (var j=0; j<5; j++) {
			if (count>0 && locationelements[j] != '') {
				textnode += ", ";
			}
			if (locationelements[j] != '') {
				textnode += locationelements[j];
				count++;
			}
		}
		
		strasse = locationelements[0];
		hausnummer = locationelements[1];
		plz = locationelements[2];
		ort = locationelements[3];
		land = locationelements[4];
		longlat = locationelements[5];
		if (locationelements[6] != null) {
			alort = locationelements[6];
		} else {
			alort = 0;
		}
		var linkaction = "javascript:fillfields('"+streetfield+"', '"+streetnumberfield+"', '"+zipfield+"', '"+cityfield+"', '"+countryfield+"', '"+longlatfield+"', '"+strasse+"', '"+hausnummer+"', '"+plz+"', '"+ort+"', '"+land+"', '"+longlat+"', '"+alort+"', '"+layername+"')";
		a = document.createElement("a");
		a.setAttribute ("href", linkaction);

		bullet = document.createElement("img");
		bullet.setAttribute ("src", "/global/images/standort_bullet_blau.gif");
		bullet.setAttribute ("border", 0);
		bullet.setAttribute ("width", "3");
		bullet.setAttribute ("height", "6");
		a.appendChild(bullet);

		entry = document.createTextNode(textnode);
		a.appendChild(entry);
		li.appendChild(a);
		cookieList.appendChild(li);
		cookieList.setAttribute ("class", "cookieList");
	}
	
	historyClear.setAttribute("href", "javascript:clear_kt_locationCookie()");
	document.getElementById(layername).appendChild(cookieList);

		//Subheader
		headerspan = document.createElement("div");
		headerspan.setAttribute("id", layername+"_databaseList");
		headerspan.className = "header";
		header = document.createTextNode("Gespeicherte Standorte");
		headerspan.appendChild(header);

		infoicon = document.createElement("img");
		infoicon.style.marginLeft = "10px";
		infoicon.setAttribute("src", "/global/images/infoicon.gif");
		infoicon.onmouseover = function(){showInfoLayer(layername)};
		infoicon.onmouseout = function(){hideInfoLayer(layername)};
		headerspan.appendChild(infoicon);

		document.getElementById(layername).appendChild(headerspan);

	//Now the database list
	if (user_id != '' && user_id != 0 && user_id != 'USER_ID'){
		ul = document.createElement("ul");
		var locations = responseXML.getElementsByTagName("standort");
		for(var i = 0; i < locations.length; i++) {
			var e = locations[i];
			if (e.getElementsByTagName("name")[0].hasChildNodes())
				var name = e.getElementsByTagName("name")[0].firstChild.data;
			else
				var name = "";
			if (e.getElementsByTagName("strasse")[0].hasChildNodes())
				var strasse = e.getElementsByTagName("strasse")[0].firstChild.data;
			else
				var strasse = "";
			if (e.getElementsByTagName("hausnummer")[0].hasChildNodes())
				var hausnummer = e.getElementsByTagName("hausnummer")[0].firstChild.data;
			else
				var hausnummer = "";
			if (e.getElementsByTagName("plz")[0].hasChildNodes())
				var plz = e.getElementsByTagName("plz")[0].firstChild.data;
			else
				var plz = "";
			if (e.getElementsByTagName("ort")[0].hasChildNodes())
				var ort = e.getElementsByTagName("ort")[0].firstChild.data;
			else
				var ort = "";
			if (e.getElementsByTagName("land")[0].hasChildNodes())
				var land = e.getElementsByTagName("land")[0].firstChild.data;
			else
				var land = "";
			if (e.getElementsByTagName("longlat")[0].hasChildNodes())
				var longlat = e.getElementsByTagName("longlat")[0].firstChild.data;
			else
				var longlat = "";
		if (e.getElementsByTagName("alort")[0].hasChildNodes())
			var alort = e.getElementsByTagName("alort")[0].firstChild.data;
		else
				var alort = 0;
			li = document.createElement("li");
			a = document.createElement("a");
			var linkaction = "javascript:fillfields('"+streetfield+"', '"+streetnumberfield+"', '"+zipfield+"', '"+cityfield+"', '"+countryfield+"', '"+longlatfield+"', '"+strasse+"', '"+hausnummer+"', '"+plz+"', '"+ort+"', '"+land+"', '"+longlat+"', '"+alort+"', '"+layername+"')";
			a.setAttribute ("href", linkaction);

			bullet = document.createElement("img");
			bullet.setAttribute ("src", "/global/images/standort_bullet_blau.gif");
			bullet.setAttribute ("border", 0);
			bullet.setAttribute ("width", "3");
			bullet.setAttribute ("height", "6");
			a.appendChild(bullet);

			if (e.getElementsByTagName("wohnort")[0].firstChild.data == 'true') {
				wohnort = document.createElement("img");
				wohnort.src="/global/images/wohnort_icon_small.gif";
				wohnort.height="10";
				wohnort.widht="11";
				wohnort.border="0";
				wohnort.style.marginTop="3px";
				wohnort.style.marginLeft="5px";
				a.appendChild(wohnort);
			}

			entry = document.createTextNode(" "+name);
			a.appendChild(entry);


			li.appendChild(a);
			ul.appendChild(li);
		}
	document.getElementById(layername).appendChild(ul);
	}


	footerspan = document.createElement("div");
	footerspan.className = "header";

	footerspanTable = document.createElement("table");
	footerspanTBody = document.createElement("tbody");
	footerspanTableRow = document.createElement("tr");
	footerspanTableLeftCell = document.createElement("td");
	footerspanTableRightCell = document.createElement("td");
	footerspanTable.setAttribute("width", "100%");
	footerspanTable.setAttribute("cellspacing", "0");
	footerspanTable.setAttribute("cellpadding", "0");
	footerspanTable.setAttribute("border", "0");
	footerspanTableLeftCell.setAttribute("align", "left");
	footerspanTableLeftCell.setAttribute("valign", "top");
	footerspanTableRightCell.setAttribute("align", "right");
	footerspanTableRightCell.setAttribute("valign", "top");


	if (user_id != '' && user_id != 0 && user_id != 'USER_ID'){
		a = document.createElement("a");
		if (isInternal())
			a.setAttribute("href", "http://my.klicktel.int/standorte.php");
		else {
			if (isKlickUrl())
				a.setAttribute("href", "http://myde.release.klickurl.de/standorte.php");
			else
				a.setAttribute("href", "http://my.klicktel.de/standorte.php");
		}

		bullet = document.createElement("img");
		bullet.setAttribute ("src", "/global/images/standort_bullet_schwarz.gif");
		bullet.setAttribute ("border", 0);
		bullet.setAttribute ("width", "3");
		bullet.setAttribute ("height", "6");
		a.appendChild(bullet);

		linktext = document.createTextNode(" Standorte bearbeiten");
		a.appendChild(linktext);
		footerspanTableLeftCell.appendChild(a);

		linktext = document.createTextNode(" ");
		footerspanTableRightCell.appendChild(linktext);
	}
	else {
		a = document.createElement("a");
		if (isInternal())
			a.setAttribute("href", "http://my.klicktel.int/standort_login.php?target="+window.location.protocol+"//"+window.location.hostname);
		else {
			if (isKlickUrl())
				a.setAttribute("href", "http://myde.release.klickurl.de/standort_login.php?target="+window.location.protocol+"://"+window.location.hostname);
			else
				a.setAttribute("href", "http://my.klicktel.de/standort_login.php?target="+window.location.protocol+"://"+window.location.hostname);
		}

		bullet = document.createElement("img");
		bullet.setAttribute ("src", "/global/images/standort_bullet_schwarz.gif");
		bullet.setAttribute ("border", 0);
		bullet.setAttribute ("width", "3");
		bullet.setAttribute ("height", "6");

		linktext = document.createTextNode(" Kostenlos bei myklickTel.de anmelden");
		a.appendChild(linktext);
		footerspanTableLeftCell.appendChild(a);

		a = document.createElement("a");
		if (isInternal())
			a.setAttribute("href", "http://my.klicktel.int/standort_login.php?target="+window.location.protocol+"//"+window.location.hostname);
		else {
			if (isKlickUrl())
				a.setAttribute("href", "http://myde.release.klickurl.de/standort_login.php?target="+window.location.protocol+"//"+window.location.hostname);
			else
				a.setAttribute("href", "http://my.klicktel.de/standort_login.php?target="+window.location.protocol+"//"+window.location.hostname);
		}

		bullet = document.createElement("img");
		bullet.setAttribute ("src", "/global/images/standort_bullet_schwarz.gif");
		bullet.setAttribute ("border", 0);
		bullet.setAttribute ("width", "3");
		bullet.setAttribute ("height", "6");

		linktext = document.createTextNode(" Login");
		a.appendChild(linktext);
		footerspanTableRightCell.appendChild(a);

	}


	footerspanTableRow.appendChild(footerspanTableLeftCell);
	footerspanTableRow.appendChild(footerspanTableRightCell);
	footerspanTBody.appendChild(footerspanTableRow);
	footerspanTable.appendChild(footerspanTBody);

	footerspan.appendChild(footerspanTable);

	document.getElementById(layername).appendChild(footerspan);
	initialized[layername]=true;
}

function log (text) {
	if (Logger != null) {
		Logger.log(text);
	}
}

function dropdown (user_id, streetField, streetnumberField, zipField, 
	cityField, countryField, longlatField, buttonID, locationCookie, xDirection, yDirection) {

	if (user_id == '') {
		user_id = 0;
	}

	layername = buttonID+"layer";
	button = $(buttonID);
	//Hide other menu layers
	layers = document.getElementsByClassName ("menuLayer");
	if (layers.length > 0) {
		for (i=0; i<layers.length; i++) {
			if (layers[i].id != layername) {
				Element.hide(layers[i]);
			}
		}
	}

	
	btns = document.getElementsByClassName ("locationbutton");
	if (btns.length > 0) {
		for (i=0; i<btns.length; i++) {
			btns[i].style.backgroundImage = 'url(/global/images/dropdown_grey.png)';
		}	
	}
	
	if ($(layername) == null) { //Standort Layer not initialized
		layer = document.createElement ("div");
		layer.id = layername;
		layer.style.position="absolute";
		layer.style.visibility = "hidden";
		Element.addClassName(layer, "menuLayer");
		
		if (sessionID == null || sessionName == null) {
			var sessionID = '';
			var sessionName = '';
		}
	}
	if ($(layername) == null || Element.empty ($(layername)) == true) {
		var myAjax = new Ajax.Request(
        	'/standort_ajax_handler.php',
		    {
	            method: 'get',
	            parameters: 'user_id='+user_id+'&'+sessionName+'='+sessionID,
	            onComplete: function(req){ 
	                Element.update($(layername), ''); 
	            	updateMenuLayer(user_id, req.responseXML, locationCookie, streetField, streetnumberField, zipField, cityField, countryField, longlatField, layername);
				    dimensionsLayer = Element.getDimensions(layer);
					dimensionsButton = Element.getDimensions($(buttonID));
					if (xDirection == 0) {
						layer.style.left = (buttonPosition[0]-dimensionsLayer.width+dimensionsButton.width)+"px";
					} else {
						layer.style.left = (buttonPosition[0])+"px";
					}
					
					if (yDirection == 0) {
						layer.style.top = (buttonPosition[1]+dimensionsButton.height)+"px";
					} else {
						layer.style.top = (buttonPosition[1]-dimensionsLayer.height)+"px";
					}
					layer.style.visibility = "visible";	            
	            },
	            onError: function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}
	         });
	    Element.makePositioned ($(buttonID));
		buttonPosition = Position.cumulativeOffset ($(buttonID));
		
		document.body.appendChild(layer);
		
	} else {
		Element.toggle ($(layername));
	}
	if (Element.visible($(layername))) {
			button.style.backgroundImage = 'url(/global/images/dropdown_up_grey.png)';
	} else {
		button.style.backgroundImage = 'url(/global/images/dropdown_grey.png)';
	}
}