// PPM STANDORT
// (c) 2006 by klickTel AG, Essen.
// All rights reserved. Unauthorized duplication and use prohibited.

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

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

function button_down (layername){
	return;
	image = $("btn_"+layername).childNodes[0];
	image.setAttribute("src", "/klickbar/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(
		ppmObject.ajaxPath+'standorte'+ppmObject.ajaxExtension, 
		{
			method: 'get', 
			parameters: 'action=clearCookie', 
			onComplete: function(req){return(true)},
			onError: function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}
		});
	Element.update($(ppmObject.parentNode.id+'_navi_dropdown_a_lastLocation'), "");
	Element.update($(ppmObject.parentNode.id+'_navi_dropdown_b_lastLocation'), "");
	Element.update($(ppmObject.parentNode.id+'_navi_dropdown_1_lastLocation'), "");
	Element.update($(ppmObject.parentNode.id+'_navi_dropdown_2_lastLocation'), "");
	Element.update($(ppmObject.parentNode.id+'_navi_dropdown_3_lastLocation'), "");
	Element.update($(ppmObject.parentNode.id+'_navi_dropdown_lastLocation'), "");
	cookiesKilled = true;
}

function showInfoLayer (layername){
	if ($(layername+"_infoLayer") == null ) {
		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&ouml;nnen Sie selbst erstellen und &auml;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&ouml;nnen Sie sich kostenlos anmelden!");
		infoLayer.appendChild(infoText);
		infoLayer.setAttribute("id", layername+"_infoLayer");
		$(layername+"_databaseList").appendChild(infoLayer);
	} else {
		infoLayer = $(layername+"_infoLayer");
		if(infoLayer != null) {
			infoLayer.style.visibility = 'visible';
		}
	}
	
}

function hideInfoLayer (layername){
	infoLayer = $(layername+"_infoLayer");
	if(infoLayer != null) {
		$(layername+"_infoLayer").style.visibility = 'hidden';
	}
}

function fillfields (streetfield, streetnumberfield, zipfield, cityfield, countryfield, longitudefield, latitudefield, streetvalue, streetnumbervalue, zipvalue, cityvalue, countryvalue, longitudevalue, latitudevalue, alortvalue, layername, name){

 
	if (streetfield==streetnumberfield){
		if (combinedStreetNumberField =  $(streetfield)){
			combinedStreetNumberValue = "";
			if (streetvalue != "")
				combinedStreetNumberValue = streetvalue+" ";
			if (streetnumbervalue != "")
				combinedStreetNumberValue += streetnumbervalue;
			combinedStreetNumberField.value = combinedStreetNumberValue;
		}
	}
	else {
		StreetField = $(streetfield);
		if (StreetField != null){
			StreetField.value = streetvalue;
		}
		StreetNumberField = $(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 = $(zipfield)){
			combinedZipCityValue = "";
			if (zipvalue != "")
				combinedZipCityValue = zipvalue+" ";
			if (cityvalue != "")
				combinedZipCityValue += cityvalue;
			combinedZipCityField.value = combinedZipCityValue;
		}
	}
	else {
		ZipField = $(zipfield);
		if (ZipField != null){
			ZipField.value = zipvalue;
		}
		CityField = $(cityfield);
		if (CityField != null){
			CityField.value = cityvalue;
		}
		
	}

	if ((zipvalue == "") && (cityvalue == "")) {
		ZipField = $(zipfield);
		ZipField.value = name;
	}
	
	CountryField = $(countryfield);
	if (CountryField != null && (countryvalue != "")){
		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;
		}
	} 
	
	longlat = longitudevalue.split('|');
	LongitudeField = $(longitudefield);
	if (LongitudeField != null){
		if (longitudefield.search('alort') == -1 && longitudefield != 'lid') {			
			longitudevalue = longlat[0];
			LongitudeField.value = longitudevalue;
		} else {
			LongitudeField.value = alortvalue;
		}
	}

	LatitudeField = $(latitudefield);
	if (LatitudeField != null){
		if (latitudefield.search('alort') == -1 && latitudefield != 'lid') {
			if(longlat.length > 1){
				latitudevalue = longlat[1];
			}
			LatitudeField.value = latitudevalue;
			
		} else {
			LatitudeField.value = alortvalue;
		}
	}
	
	$(layername).style.display="none";
	button_down(layername);
	visible[layername] = false;

}

function updateMenuLayer (user_id, responseXML, locationcookie, streetfield, streetnumberfield, zipfield, cityfield, countryfield, longitudefield, latitudefield, layername, lastLocationId, saveLocationId, helpBubble)
{	
	Element.update($(lastLocationId),"");
	Element.update($(saveLocationId),"");
	
	var myAjax = new Ajax.Request(
		'/kt_map/ajax/cookie.php', 
		{
			method: 'get', 
			pars: "action=get&area=ppm&type=help", 
			onComplete: function(req){ 

	a = document.cookie;
	cookies = a.split('; ');
	for (i = 0; i < cookies.length; i++) {
		cookiename = cookies[i].substr(0, cookies[i].search('='));

		if(cookiename.indexOf('kt_locationCookie') != '-1') {

			locationcookie = unescape(cookies[i].substr(cookies[i].search('=')+1, cookies[i].length));
			for (i = 0; i < locationcookie.length; i++)  {
				locationcookie = locationcookie.replace(/\+/, " ");
				
			}
		
		}
	}

	cookielocations = locationcookie.split("||");
	
	for (var i=0; i<cookielocations.length-1; i++) {
		locationelements = cookielocations[i].split(";;");
		var textnode = " ";
		count = 0;
		for (var j=0; j<5; j++) {
			if (count>0 && locationelements[j] != '') {
				textnode += ", ";
			}
			if (locationelements[j] != '') {
				textnode += locationelements[j];
				count++;
			}
		}
		
		var strasse = locationelements[0];
		var hausnummer = locationelements[1];
		var plz = locationelements[2];
		var ort = locationelements[3];
		var land = locationelements[4];
		var longitude = locationelements[5];
		var latitude = locationelements[6];
		if (locationelements[7] != null) {
			var alort = locationelements[7];
		} else {
			var alort = 0;
		}
		
		var linkaction = "javascript:fillfields('"+streetfield+"', '"+streetnumberfield+"', '"+zipfield+"', '"+cityfield+"', '"+countryfield+"', '"+longitudefield+"', '"+latitudefield+"', '"+strasse+"', '"+hausnummer+"', '"+plz+"', '"+ort+"', '"+land+"', '"+longitude+"', '"+latitude+"', '"+alort+"', '"+layername+"', '')";
		a = document.createElement("a");
		a.setAttribute ("href", linkaction);
		entry = document.createTextNode(textnode);
		a.appendChild(entry);
		$(lastLocationId).appendChild(a);
	}
		
	// Now the database list
	if (user_id != '' && user_id != 0 && user_id != 'USER_ID') {
		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;
				name = name.substring(0, name.length-3);
			}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("longitude")[0].hasChildNodes())
				var longitude = e.getElementsByTagName("longitude")[0].firstChild.data;
			else
				var longitude = "";
			if (e.getElementsByTagName("latitude")[0].hasChildNodes())
				var latitude = e.getElementsByTagName("latitude")[0].firstChild.data;
			else
				var latitude = "";
	//		if (e.getElementsByTagName("alort")[0].hasChildNodes())
	//			var alort = e.getElementsByTagName("alort")[0].firstChild.data;
	//		else
			var alort = 0;
			
			var linkaction = "javascript:fillfields('"+streetfield+"', '"+streetnumberfield+"', '"+zipfield+"', '"+cityfield+"', '"+countryfield+"', '"+longitudefield+"', '"+latitudefield+"', '"+strasse+"', '"+hausnummer+"', '"+plz+"', '"+ort+"', '"+land+"', '"+longitude+"', '"+latitude+"', '"+alort+"', '"+layername+"', '"+name+"')";
			a = document.createElement("a");
			a.setAttribute ("href", linkaction);
			entry = document.createTextNode(" "+name);			
			a.appendChild(entry);
			$(saveLocationId).appendChild(a);
		}
				
	}
	
	savedLocationsP3 = document.createElement("p");
	savedLocationsP3.className = "delete";
	savedLocationsP3.style.whiteSpace = "nowrap";
	
	if (user_id != '' && user_id != 0 && user_id != 'USER_ID'){
		a = document.createElement("a");
		if (isInternal())
			a.setAttribute("href", "http://v2.4.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");
		}

		linktext = document.createTextNode("Standorte bearbeiten");
		a.appendChild(linktext);
		savedLocationsP3.appendChild(a);
		$(helpBubble).style.display = "none";
	}
	else {	

		a = document.createElement("a");
		if (isInternal())
			a.setAttribute("href", "http://v2.4.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);
		}
	

		a = document.createElement("a");
		if (isInternal())
			a.setAttribute("href", "http://v2.4.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);
		}

		linktext = document.createTextNode("Login/Anmeldung");
		a.appendChild(linktext);
		savedLocationsP3.appendChild(a);		
		
	}
	$(saveLocationId).appendChild(savedLocationsP3);
	
	
	
	},
			onError: function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}
		});
	
}

function dropdown (user_id, streetfield, streetnumberfield, zipfield, cityfield, countryfield, longitudefield, latitudefield, layername, locationcookie, lastLocationId, saveLocationId, helpBubble)

{	
	if($(layername) == null) {
		localLog('ERROR - DROPDOWN: '+layername+' is null!');
		return false;
	}
	
	if (!visible[layername]){
		// Make other menu layers invisible
		layers = document.getElementsByTagName("div");
		for (i=0; i<=layers.length-1;i++){
			testLayer = layers[i];
			if (testLayer.className == "menuLayer"){
				testLayer.style.display="none";
				visible[testLayer.getAttribute("id")]=false;
				tempId = testLayer.getAttribute("id");
				button_down(tempId);
			}
		}
		
		layerDim = Element.getDimensions($(layername));
		mapDim   = Element.getDimensions(this.ppmObject.parentNode.id);
		leftDim  = Element.getDimensions(this.ppmObject.parentNode.id+'_optionsleiste_links');
		rightDim = Element.getDimensions(this.ppmObject.parentNode.id+'_optionsleiste_rechts');

		this.ppmObject.hideInfoWindow();

		centerDimWidth = mapDim.width - leftDim.width - rightDim.width;		
		marginLeft = Math.round((centerDimWidth - layerDim.width)/2);

		newX = leftDim.width + marginLeft - 6;
		newY = 0;
		
		$(layername).style.position = "absolute";
		$(layername).style.left=newX+5+'px';
		$(layername).style.top=newY+'px';		
		$(layername).style.display="block";
		$(layername).style.zIndex = 2049;
		ppmObject.parentNode.appendChild ($(layername));
		var doNotClose = new Array(ppmObject.parentNode.id+'_zstations_controller'); 
		ppmObject.makeModal(layername, doNotClose);


		//if (1){//!initialized[layername]){
			if (cookiesKilled)
				locationcookie = "";
			initialized[layername]=true;

			if(sessionID == null || sessionName == null || sessionID == '{SESSION_ID}' || sessionName == '{SESSION_NAME}') {
				sessionID='';
				sessionName='';
			}
			
			var myAjax = new Ajax.Request(
			'/kt_map/ajax/standorte.php', 
			{
				method: 'get', 
				parameters: 'user_id='+user_id+'&'+sessionName+'='+sessionID, 
				onComplete: function(req){updateMenuLayer(user_id, req.responseXML, locationcookie, streetfield, streetnumberfield, zipfield, cityfield, countryfield, longitudefield, latitudefield, layername, lastLocationId, saveLocationId, helpBubble);},
				onError: function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}
			});

		//}
		
		button_up(layername);
		
	}
	else {
		$(layername).style.display="none";
		button_down(layername);
	}
	visible[layername] = !visible[layername];
}

