﻿/*
* TODO: die rendering Functions könnte man eigentlich mit den callback functions zusammenlegen
* TODO: bie bundesland auswahl kann es theoretisch auch zu ungültigen auswahlen kommen, 
    wenn man zuvor eine immobileinart gewählt hat, und nach einer änderung des bundeslands ie immobilienart nicht mehr selektiert ist
       -> sollte auch mit invalid überprüft werden
*/

/*
* Every Type And Layer Ajax Request is Stored in the xmlRequests Array
*/
var xmlRequests = new Array();
xmlRequests.push(null); //0 - Land
xmlRequests.push(null); //1 - Bula
xmlRequests.push(null); //2 - reg
xmlRequests.push(null); //3 - group
xmlRequests.push(null); //4 - type
var paramXML = null;        // parameter List
var countXML = null;        // the XMLHttpObject to check if realestates exist for the selection
var initXML = null;         // XMLHttp Request to get the Elements for the Current Url (initialization of the url)

/* This Object Contains the Current Selection from the User*/
var currentSelection = new Selection();

/* Bit to Show the Debugging functions of the Ribbon Control */
var isRibbonDebug = false;

//---------------------------------------------------------------------------------------------------
// Templates
//---------------------------------------------------------------------------------------------------
var LandHeader = '<select id=\"ddlLand\" class="countrySelect" title="Länderauswahl" onchange="ChangeDropDown();">';
var LandItem = '<OPTION value="!!value!!|!!seoName!!">!!name!!</OPTION>';
var LandSelected = '<OPTION selected=\"selected\" value="!!value!!|!!seoName!!" id=!!id!!>!!name!!</OPTION>';
var LandSeperator = '';
var LandFooter = '</select>';

var BulaHeader = "<table cellpadding=\"0\" cellspacing=\"0\">";
var BulaItem = "<tr><td><span id=\"span_!!id!!\" onmouseout=\"HoverRibbon('!!value!!', 'out');\" onmouseover=\"HoverRibbon('!!value!!', 'in');return true;\"><input id=!!id!! type=\"checkbox\" value=\"!!value!!\" onclick=\"AddCheckBox('!!value!!', 1, 'Geo', true, this, false, '!!par!!', 'Geo', 3, ReRenderBundesland, '!!name!!', !!index!!, false, '!!seoName!!');\" /><Label for=\"!!id!!\">!!name!!</Label></span></td>";
var BulaSelected = "<tr><td><span id=\"span_!!id!!\"><input id=!!id!! checked=\"checked\" type=\"checkbox\" value=\"!!value!!\" onclick=\"AddCheckBox('!!value!!', 1, 'Geo', true, this, false, '!!par!!', 'Geo', 3, ReRenderBundesland, '!!name!!', !!index!!, false, '!!seoName!!');\" /><Label for=\"!!id!!\">!!name!!</Label></span></td>";
var BulaAlternating = "<td><span id=\"span_!!id!!\" onmouseout=\"HoverRibbon('!!value!!', 'out');\" onmouseover=\"HoverRibbon('!!value!!', 'in');return true;\"><input id=!!id!! type=\"checkbox\" value=\"!!value!!\" onclick=\"AddCheckBox('!!value!!', 1, 'Geo', true, this, false, '!!par!!', 'Geo', 3, ReRenderBundesland, '!!name!!', !!index!!, false, '!!seoName!!'); \" /><Label for=\"!!id!!\">!!name!!</Label></span></td></tr>";
var BulaSelectedAlternating ="<td><span id=\"span_!!id!!\"><input id=!!id!! checked=\"checked\" type=\"checkbox\" value=\"!!value!!\" onclick=\"AddCheckBox('!!value!!', 1, 'Geo', true, this, false, '!!par!!', 'Geo', 3, ReRenderBundesland, '!!name!!', !!index!!, false, '!!seoName!!');\" /><Label for=\"!!id!!\">!!name!!</Label></span></td></tr>";
var BulaEmptyAlternating = "<td>&nbsp;</td></tr>";
var BulaSeperator = "";
var BulaEmpty = "";
var BulaFooter = "</table>";
var BulaNonAustria = "<p class=\"Shade\">Suche in ganz !!name!!:<br />Wählen Sie im nächsten Schritt bitte eine Immobilienart!<p>";

var bulaImageItem = "<img src=\"http://www.immobilien.net/img/spacer.gif\" class=\"mapImage\" id=\"hover_!!value!!\" style=\"background-image:url(http://www.immobilien.net/!!hImg!!); visibility:hidden;\" alt=\"!!name!!\" />";;
var bulaImageSelected = "<img src=\"http://www.immobilien.net/img/spacer.gif\" class=\"mapImage\" id=\"hover_!!value!!\" style=\"background-image:url(http://www.immobilien.net/!!hImg!!); visibility:hidden;\" alt=\"!!name!!\" /> <img src=\"http://www.immobilien.net/img/spacer.gif\" class=\"mapImage\" id=\"active_!!value!!\" style=\"background-image:url(http://www.immobilien.net/!!aImg!!);\" alt=\"!!name!!\" />";
var bulaImageFooter = "<img src=\"http://www.immobilien.net/img/spacer.gif\" class=\"mapImage\" alt=\"Geosearch Clickmap\" usemap=\"#ImageMap\" /><img src=\"http://www.immobilien.net/img/maps/base_map.gif\" alt=\"Geosearch Basemap\" />";

var bulaImageMapHeader = "<map name=\"ImageMap\" id=\"ImageMap\">";
var bulaImageMapItem = "<area shape=\"poly\" id=\"area_!!value!!\" coords=\"!!imgMap!!\" href=\"#\" onclick=\"document.getElementById('!!id!!').click();\" alt=\"!!name!!\" title=\"!!name!!\" onmouseover=\"MapHover(this, 'in'); return true\" onmouseout=\"MapHover(this, 'out');\" />";
var bulaImageMapFooter = "</map>";

var RegionsHeader = "<table>";
var RegionsGroupHeader = "<h2><span class=\"Right\"><a href=\"javascript:SelectAllRegions('!!value!!', '!!par!!', true);\">Alle</a> | <a href=\"javascript:SelectAllRegions('!!value!!', '!!par!!', false);\">Keine</a></span>!!name!!</h2>";
var RegionsItem = "<tr><td><input id=!!id!! type=\"checkbox\" value=\"!!value!!\" onclick=\"ClickedTwoLayer('!!value!!', '!!parentvalue!!', 3, 'Geo', this, '!!parentsparent!!', '!!name!!', !!index!!, !!parentindex!!, '!!seoName!!', '!!parentseoName!!');\" /><Label for=\"!!id!!\">!!name!!</Label></td>";
var RegionsAlternating = "<td><input id=!!id!! type=\"checkbox\" value=\"!!value!!\" onclick=\"ClickedTwoLayer('!!value!!', '!!parentvalue!!', 3, 'Geo', this, '!!parentsparent!!', '!!name!!', !!index!!, !!parentindex!!, '!!seoName!!', '!!parentseoName!!');\" /><Label for=\"!!id!!\">!!name!!</Label></td></tr>";
var RegionsSelected = "<tr><td><input id=!!id!! checked=\"checked\" type=\"checkbox\" value=\"!!value!!\" onclick=\"ClickedTwoLayer('!!value!!', '!!parentvalue!!', 3, 'Geo', this, '!!parentsparent!!', '!!name!!', !!index!!, !!parentindex!!, '!!seoName!!', '!!parentseoName!!');\" /><Label for=\"!!id!!\">!!name!!</Label></td>";
var RegionsSelectedAlternating = "<td><input id=!!id!! checked=\"checked\" type=\"checkbox\" value=\"!!value!!\" onclick=\"ClickedTwoLayer('!!value!!', '!!parentvalue!!', 3, 'Geo', this, '!!parentsparent!!', '!!name!!', !!index!!, !!parentindex!!, '!!seoName!!', '!!parentseoName!!');\" /><Label for=\"!!id!!\">!!name!!</Label></td></tr>";
var RegionsEmptyAlternating = "<td>&nbsp;</td></tr>";
var RegionsNOObjectsItem = "<tr><td><span style=\"color:#999999\"> <input type=\"checkbox\" disabled=\"-1\" />!!name!!</span></td>";
var RegionsNOObjectsAlternating = "<td><span style=\"color:#999999\"><input type=\"checkbox\" disabled=\"-1\" />!!name!!</span></td></tr>";
var RegionsSeperator = "";
var RegionsGroupFooter = "</table>";
var RegionsFooter = "";

var GroupHeader = "<ul class=\"typeSelect\" id=\"ImmoGroupHeader\">";
var GroupItem = "<li><a href=\"#\" value=\"!!value!!\" onclick=\"AddCheckBox('!!value!!', 0, 'Type', false, this, false, null, 'Type', 1, ReRenderGroups, '!!name!!', !!index!!, false, '!!seoName!!');\" > !!name!!</a></li>";
var GroupSelected = "<li class=\"active\"><a href=\"#\" value=\"!!value!!\" onclick=\"AddCheckBox('!!value!!', 0, 'Type', false, this, false, null, 'Type', 1, ReRenderGroups, '!!name!!', !!index!!,false, '!!seoName!!');\"> !!name!!</a></li>";
var GroupSeperator = "";
var GroupFooter = "</ul><div class=\"refineArrow\"></div>";

var TypeHeader = "<ul class=\"Checks\">";
var TypeItem = "<li><input id=!!id!! type=\"checkbox\" value=\"!!value!!\" onclick=\"AddCheckBox('!!value!!', 1, 'Type', true, this, false, '!!par!!', 'Type', 1, null, '!!name!!', !!index!!,  '!!isProject!!', '!!seoName!!');\" /><Label for=\"!!id!!\">!!name!! <span class=\"Shade\">(!!cnt!!)</span></Label></li>";
var TypeSelected = "<li><input id=!!id!! checked=\"checked\" type=\"checkbox\" value=\"!!value!!\" onclick=\"AddCheckBox('!!value!!', 1, 'Type', true, this, false, '!!par!!', 'Type', 1, null, '!!name!!', !!index!!, '!!isProject!!', '!!seoName!!');\" /><Label for=\"!!id!!\">!!name!! <span class=\"Shade\">(!!cnt!!)</span></Label></li>";
var TypeSeperator = "";
var TypeEmptyItem = "<li class=\"checkAlike disabled\"><input id=\"!!id!!\" type=\"checkbox\" disabled=\"-1\" value=\"!!value!!\" /><Label for=\"!!id!!\">!!name!!(!!cnt!!)</Label></li>";
var TypeEmpty = "<p class=\"Shade\">Bitte wählen Sie zuerst zumindest ein Bundesland aus, um sich für eine Immobilienart entscheiden zu können.</p>";
var TypeFooter = "</ul>";

var paramHeader = "<p>";
var paramItem = "<span><input id=\"!!id!!\" type=\"checkbox\" onclick=\"AddParameter(this, '!!value!!');\" /><Label for=\"!!id!!\">!!name!!</Label></span>";
var paramSelected = "<span><input checked=\"checked\" id=\"!!id!!\" type=\"checkbox\" onclick=\"AddParameter(this, '!!value!!');\" /><Label for=\"!!id!!\">!!name!!</Label></span>";
var paramEmpty = "<p><input type=\"checkbox\" disabled=\"disabled\" /><label>Miete</label><input type=\"checkbox\" disabled=\"disabled\" /><label>Kauf</label><input type=\"checkbox\" disabled=\"disabled\" /><label>Erstbezug</label></p>";
var paramFooter = "</p>";

//var autoChangedRegionText = "Ihre Regionenauswahl hat sich aufgrund der von Ihnen ausgewählten Immobilienarten geändert.";


//---------------------------------------------------------------------------------------------------
// End Templates
//---------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------
// DataTypes
//---------------------------------------------------------------------------------------------------

/*
* DataType for the CurrentSelectionObject
*/
function Selection()
{
    this.Geo = Array();             // the List of Selected Geographys (SelectionItem DataType)
    this.Type = Array();            // the List of Selected Types       (SelectionItem DataType)
    this.Parameters = new Array();  // the List of Selected Parameters  (list of strings)        
    this.isProject = false;         // gibt an ob die Suche eine Projekt suche ist
}

/*
* DataType for one Selection
*/
function SelectionItem()
{
    this.layer = -1;    // the layer of the item
    this.key = "";      // the key of the Item
    this.parentkey = "";    // the key of the parent of the item
    this.Bezeichnung = "";
    this.index = -1;         // The index in the layer of the item
    this.isValid = true;     // this value is used to determine if the item is valid for the Current selection and should be used for Refinement Requests (back Refinement from types)
    this.seoName = "";      //Gets the name for UrlPath
}

//---------------------------------------------------------------------------------------------------
//END DataTypes
//---------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------
// OnClick Functions
//---------------------------------------------------------------------------------------------------

/*
* Change Function for the Country DropDown
*/
function ChangeDropDown()
{   
    var land = document.getElementById("ddlLand");
    var selDDL = land.options[land.selectedIndex].value;
    
    // Clear the Selection when the Country has Changed:
    currentSelection.Geo = new Array();
    currentSelection.Type = new Array();
    currentSelection.Parameters = new Array();
    var param = selDDL.split('|');
    AddSelection(param[0], 0, 'Geo', null, param[1], 0, param[1]);
    var selUrl = GetCurrentUrl();  
      
    GetRibbon(selUrl, 0, 'Geo', false, true, false);        
}

/*
* Adds a Parameter to the Current Selection
* Sender: the CheckBox which called the Parameter Change
* Key: the Parameter Key which has to be de/selected
*/
function AddParameter(sender, key)
{   
    if(sender.checked)
    {
        for(var i = 0; i < currentSelection.Parameters.length; i++)
        {            
            if(currentSelection.Parameters[i] == key)
                return;
        }
        
        currentSelection.Parameters.push(key);            
    }
    else
    {
        var tmpArr = new Array();
        
        for(var i = 0; i < currentSelection.Parameters.length; i++)
        {
            if(currentSelection.Parameters[i] != key)
                tmpArr.push(currentSelection.Parameters[i]);
        }
        currentSelection.Parameters = tmpArr;
    }
    
    // when the Parameter is Changed: Check if we have RealEstates in the CUrrent Search:
    CheckCount(GetCurrentUrl());
}

/*
* Special OnClick Function for the Regions - Selects the given Region
* key: the Key of the Region
* parentKey: the  key of the Parent of the Region
* layer: the Layer of the Region
* type: the Type (normally Geo)
* sender: the Checkbox which was clicked
* parentsParentKey: the parent Key of the parent of the Current Selection
*/
function ClickedTwoLayer(key, parentKey, layer, type, sender, parentsParentKey, bezeichnung, index, parentindex, seoName, parentSeoName)
{   
    if(isRibbonDebug)
        ShowSearchRibbonInfo("Aktualisiere...");
    
    if(sender.checked)
    {       
        // Add the Selection        
        AddSelection(parentKey, layer -1, type, parentsParentKey, "Parent Element", parentindex, parentSeoName);
        AddSelection(key, layer, type, parentKey, bezeichnung, index, seoName);
    
        GetRibbon(GetCurrentUrl(), 0, 'Type', false, true, false);
    }
    else
    {
        // Remove the  Selection
        RemoveSelection(key, layer, type);
        var hasElementsAtLayer = false;
        
        // Check if there are other elements of the Same type at the Same Layer and the same parent
        if(type == 'Geo')
        {
            for(var i = 0; i < currentSelection.Geo.length; i++)
            {
                if(currentSelection.Geo[i] == null)
                    continue;
    
                if(currentSelection.Geo[i].layer == layer && currentSelection.Geo[i].parentkey == parentKey)
                {
                    hasElementsAtLayer = true;
                    break;
                }
            }
        }
        else if(type == 'Type')
        {
            for(var i = 0; i < currentSelection.Type.length; i++)
            {
                if(currentSelection.Type[i] == null)
                    continue;
                    
                if(currentSelection.Type[i].layer == layer && currentSelection.Type[i].parentkey == parentKey)
                {
                    hasElementsAtLayer = true;
                    break;
                }
            }
        }
        
        if(!hasElementsAtLayer)
        {   
            // if no elements at the Same layer exist -> remove the parent
            RemoveSelection(parentKey, layer -1, type);
        }
        
        // Get the Types Asociated with the Region
        GetRibbon(GetCurrentUrl(), 0, 'Type', false, true, false);
    }    
}

/*
* selects all the Regions for the given type by calling the Click function of the Checkboxes
* parentKey: the Key of the RegionsGroup to be selected
* parentsParent: the parentKey of the RegionsGroup
* shouldBeSelected: checks if it's a "all" or "None" click (deselect or select)
*/
function SelectAllRegions(parentKey, parentsParent, shouldBeSelected)
{   
    if(isRibbonDebug)
        ShowSearchRibbonInfo("Aktualisiere...");

    var items = xmlRequests[2].responseXML.getElementsByTagName("element");     // Get the Current Rendered Regions
    
    for(var i = 0; i < items.length; i++)
    {
        if(items[i].attributes.getNamedItem("value").value == parentKey)    // Check if the parent of the Region is the on selected
        {
            items[i].attributes.getNamedItem("sel").value = "True";            // Update the Selection Status fro Rendering
            var children = items[i].getElementsByTagName("element");
            
            for(var j = 0; j < children.length; j++)
            {
                var cb = document.getElementById("id_Reg_" + children[j].attributes.getNamedItem("value").value);  // Get the CheckBox associated with the Item
                
                if(cb != null)
                {
                    if((!cb.checked && shouldBeSelected) || (cb.checked && !shouldBeSelected))  // Click it if it should be clicked
                    {
                        cb.click();                    
                    }
                }                    
            }
            
            if(!shouldBeSelected)   //Workaround for Deselection - otherwise the parent would not be deselected
            {                   
                RemoveSelection(parentKey, 2, 'Geo');
            }
        }
    }    
}

/*
* Default OnClick function to Select a checkbox or a MultiSelect linkButton
* Key: the key to be selected
* layer: the layer to be selected
* type: the type of the selection (geo / type)
* Multiselect: declares if Multiple selections Allowed
* sender: the sender Control (Checkbox / linkbutton)
* isTwoLayer: check if the click was a TwoLayer Click (deprecate?)
* parentKey: the key of the parent of the selection
* refineType: the type which has to be refined after the Click
* refineLayer: the layer which has to be refined after the Click
* renderFunction: the Function which has to be called to Re - Render the Selection (f.i. an ImageMap)
*/
function AddCheckBox(key, layer, type, Multiselect, sender, isTwoLayer, parentKey, refineType, refineLayer, renderFunction, bezeichnung, index, isProject, seoName)
{   
    if(isRibbonDebug)
        ShowSearchRibbonInfo("Aktualisiere...");
    
    if(renderFunction != null)  // if the Rendering is set -> reRender the Selection
        renderFunction(key, (sender.checked == undefined || sender.checked)); 
    
    if(isProject != null)
    {
        if(isProject == "True")
            currentSelection.isProject = true;
        else
            currentSelection.isProject = false;
    } 
    
    if(sender.checked == undefined || sender.checked)   // if it's checked or a Linkbutton
    {
        if(!Multiselect)    // if it's not a SingleSelect -> clear the selection of the layer
        {
            if(type == 'Type')
            {
                for(var i = 0; i< currentSelection.Type.length; i++)
                {   
                    if(currentSelection.Type[i] == null)
                        continue;
                    RemoveSelection(currentSelection.Type[i].key, layer, 'Type');
                }                   
            }            
            if(type == 'Geo')
            {
                for(var i = 0; i< currentSelection.Geo.length; i++)
                {   
                    if(currentSelection.Geo[i] == null)
                        continue;
                    RemoveSelection(currentSelection.Geo[i].key, layer, 'Geo');
                }   
            }
        }
        
        
        AddSelection(key, layer, type, parentKey, bezeichnung, index, seoName);     
        var getEmpty = false;   // todo: add as parameter
        
        if(layer == 1 && type == 'Geo') // Two Layer region hack -> show the Popup and set TwoLayer to true
        {
            ShowPLZPopup('plzPopup');   
            getEmpty = true;
            isTwoLayer = true;                     
        }        
        
        if(layer == 0 && type == 'Type')
        {
            getEmpty = true;
        }
        
        if(layer == 1 && type == 'Type')
        {
            // otherwise the plz popup would hide the parameters
            HidePLZPopup('plzPopup');
            getEmpty = true;
        }
        
        if(refineType != null && refineLayer > 0)   // Make an Ajax Call for the Refined Selection
        {
            
            GetRibbon(GetCurrentUrl(), refineLayer, refineType, isTwoLayer, true, getEmpty);
        }
    }
    else
    {   
        // DeSelection:
        
        RemoveSelection(key, layer, type);
        var shouldRefine = true;
        var getEmpty = false;        
        
        if(layer == 1 && type == 'Geo') // Two Layer region hack
        {   
            getEmpty = true;
            isTwoLayer = true;
            if(!HasSelectedLayer(1, 'Geo')) // if no Bula is Selected: close the Popup, and don't make the default refinement
            {
                shouldRefine = false;
                HidePLZPopup('plzPopup');                        
            }
        }
        
        if(layer == 0 && type == 'Type')
        {
            getEmpty = true;
        }
        
        
        if((layer == 1 && type == 'Type'))
        {
            getEmpty = true;
        }
        
        if(shouldRefine)    // default: refine with the given parameters
        {
            if(refineType != null && refineLayer > 0)
                GetRibbon(GetCurrentUrl(), refineLayer, refineType, isTwoLayer, true, getEmpty);    
        }
        else{
            // if no bula was selected: make an empty rendering for the Regions and update the Types
            RenderRegions(new Array());
            HidePLZPopup('plzPopup');  
            GetRibbon(GetCurrentUrl(false), 0, 'Type', false, true, false);
        }
    }    
}
//---------------------------------------------------------------------------------------------------
// END OnClick Functions
//---------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------
// Ajax Calling Functions
//---------------------------------------------------------------------------------------------------

/*
* Makes an Ajax request to retrieve a List of values for a Range of Values
*/
function GetInitialisationValues(url)
{
    if (window.XMLHttpRequest)
	{
		initXML= new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		initXML=new ActiveXObject('MSXML2.XMLHTTP.3.0');
	}
	
	if (initXML!= null)
	{			
		try
		{   
			initXML.open("GET", url, true);
			initXML.onreadystatechange= InitCallBack;		//Set the Response Method			
			initXML.send(null);				//Start the XML Http Request				
		}
		catch(e)
		{		
			ShowSearchRibbonInfo("Es ist ein Fehler beim Laden der Suche aufgetreten.");
		}
	}
	else
	{
		ShowSearchRibbonInfo("Ihr Browser unterstützt diese Funktionalität nicht!");		
	}	    
}
/*
* Makes an Ajax Request to check if there are realestates for the Selection
*/
function CheckCount(url)
{
    if (window.XMLHttpRequest)
	{
		countXML= new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		countXML=new ActiveXObject('MSXML2.XMLHTTP.3.0');
	}
	
	if (countXML  != null)
	{			
		try
		{   
			countXML.open("GET", url + "&CheckCount=true", true);
			countXML.onreadystatechange= CountCallBack;		//Set the Response Method			
			countXML.send(null);				//Start the XML Http Request				
		}
		catch(e)
		{		
			ShowSearchRibbonInfo("Es ist ein Fehler beim Laden der Suche aufgetreten.");
		}
	}
	else
	{
		ShowSearchRibbonInfo("Ihr Browser unterstützt diese Funktionalität nicht!");		
	}	    
}

/*
* Makes an Ajax Request to get the paramters for the Current selection
*/
function GetParameters(url, parameterType)
{   
    if (window.XMLHttpRequest)
	{
		paramXML  = new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		paramXML =new ActiveXObject('MSXML2.XMLHTTP.3.0');
	}
	
	if (paramXML  != null)
	{			
		try
		{   
			paramXML.open("GET", url + "&isp=true&pGrp=" + parameterType, true);
			paramXML.onreadystatechange= RenderParameters;		//Set the Response Method			
			paramXML.send(null);				//Start the XML Http Request				
		}
		catch(e)
		{		
			ShowSearchRibbonInfo("Es ist ein Fehler beim Laden der Suche aufgetreten.");
		}
	}
	else
	{
		ShowSearchRibbonInfo("Ihr Browser unterstützt diese Funktionalität nicht!");		
	}	
}

/*
* Starts an Ajax request for the Type / Geography
* url: the url of the Current Search
* layer: the layer to request
* type: the Type to request
* isTwoLayer: is it a Region to request (two layered list)
* isUserTriggered: is the Request triggered from the User, or automatically by a different selection (f.i. TypeSelection calls automatically the Regions)
*/
function GetRibbon(url, layer, type, isTwoLayer, isUserTriggered, getempty)
{  
    var index = 0;
    
    if(type == 'Type')
    {
        if(layer == 0)
        {   
            index = 3;
        }
        else if(layer == 1)
        {   
            index = 4;
        }
    }   
    else if(type == 'Geo')   // Get the Index of the xmlHTTP Requests
    {
        if(layer == 0)
        {
            index = 0;
        }else if(layer == 1)
        {     
            index = 1;
        }else if(layer == 2 || layer == 3)
        {                   
            index = 2;
        }
    }

    /*
    * test for FF2
    */
    /*
    if(xmlRequests[index] != null)
    {
        xmlRequests[index].abort();
    } */   
        
	if (window.XMLHttpRequest)
	{
		xmlRequests[index] = new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlRequests[index]=new ActiveXObject('MSXML2.XMLHTTP.3.0');
	}
	
	if (xmlRequests[index] != null)
	{		
	    xmlRequests[index].open("GET", url + "&layer=" +layer + "&type="+type +"&twolayer=" + (isTwoLayer ? "true" : "false") + "&usrTrigg=" + (isUserTriggered ? "true" : "false") + "&getempty=" + (getempty ? "true" : "false"), true);
		
		try
		{  
		    //Set the Response Method (CallBack) for the given type and layer
		    if(type == 'Type')
            {
                if(layer == 0)
                {   
                    xmlRequests[index].onreadystatechange = GroupCallBack;
                }
                else if(layer == 1)
                {   
                    xmlRequests[index].onreadystatechange = TypeCallBack;                    
                }
            }   			
		    else if(type == 'Geo')
            {
                if(layer == 0)
                {
                    xmlRequests[index].onreadystatechange = LandCallback;
                }else if(layer == 1)
                {     
                    xmlRequests[index].onreadystatechange = BulaCallBack;
                    
                    //window.location.href = url + "&layer=" +layer + "&type="+type +"&twolayer=" + (isTwoLayer ? "true" : "false") + "&usrTrigg=" + (isUserTriggered ? "true" : "false");
                }else if(layer == 2 || layer == 3)
                {                   
                    xmlRequests[index].onreadystatechange = RegionenCallBack;                    
                    //window.location.href = url + "&layer=" +layer + "&type="+type +"&twolayer=" + (isTwoLayer ? "true" : "false") + "&usrTrigg=" + (isUserTriggered ? "true" : "false");
                }
            }
			xmlRequests[index].send(null);				//Start the XML Http Request				
		}
		catch(e)
		{		
			ShowSearchRibbonInfo("Es ist ein Fehler beim Laden der Suche aufgetreten.");
		}
	}
	else
	{
		ShowSearchRibbonInfo("Ihr Browser unterstützt diese Funktionalität nicht!");		
	}	
}

//---------------------------------------------------------------------------------------------------
// END Ajax Calling Functions
//---------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------
// CallBacks
//---------------------------------------------------------------------------------------------------

/*
* Ajax CallBack for Initialization from a Url which had ranges
*/
function InitCallBack()
{
    if(checkReadyStateRibbon(initXML))
    {
        var response = initXML.responseXML;    
        
        var GeoRoot = response.getElementsByTagName("Geo");
        var TypeRoot = response.getElementsByTagName("Type");
        var params = response.getElementsByTagName("Params");
        
        var GeoElements = GeoRoot[0].getElementsByTagName("element")
        var TypeElements = TypeRoot[0].getElementsByTagName("element");
        var paramElements = params[0].getElementsByTagName("param");
        
        for(var i = 0; i < GeoElements.length; i++)
        {
            var parent = null;
            if(GeoElements[i].attributes.getNamedItem("par") != null)
            {
                parent = GeoElements[i].attributes.getNamedItem("par").value;
            }
            var index = -1;
            if(GeoElements[i].attributes.getNamedItem("index") != null)
            {
                index = GeoElements[i].attributes.getNamedItem("index").value;
            }
            
            AddSelection(
                GeoElements[i].attributes.getNamedItem("value").value, 
                GeoElements[i].attributes.getNamedItem("layer").value, 
                'Geo',
                parent, 
                GeoElements[i].attributes.getNamedItem("name").value, 
                index,
                GeoElements[i].attributes.getNamedItem("seoName").value
            );
        }
        
        for(var i = 0; i < TypeElements.length; i++)
        {
            var parent = null;
            if(TypeElements[i].attributes.getNamedItem("par") != null)
            {
                parent = TypeElements[i].attributes.getNamedItem("par").value;
            }
            var index = -1;
            if(TypeElements[i].attributes.getNamedItem("index") != null)
            {
                index = TypeElements[i].attributes.getNamedItem("index").value;
            }
            AddSelection(
                TypeElements[i].attributes.getNamedItem("value").value, 
                TypeElements[i].attributes.getNamedItem("layer").value, 
                'Type',
                parent,
                TypeElements[i].attributes.getNamedItem("name").value, 
                index,
                TypeElements[i].attributes.getNamedItem("seoName").value
            );
        }
        
        for(var i = 0; i < paramElements.length; i++)
        {
            var value = paramElements[i].attributes.getNamedItem("value").value;
            currentSelection.Parameters.push(value);
        }        
        
        GetRibbon(GetCurrentUrl(true), 0, 'Geo', false, false, false);
    }
}
/*
* Ajax CallBack for a Request on country level
*/
function LandCallback()
{
    //Check if The XML is OK
    if(checkReadyStateRibbon(xmlRequests[0]))			
	{	
	    // Get Values
        var response = xmlRequests[0].responseXML;
        
        if(response.getElementsByTagName("def").length == 0)    // TODO: deutet auf unnötigen ServerCallback hin
            return;
            
        var definition = response.getElementsByTagName("def")[0];
        var items = response.getElementsByTagName("element");        
        
        RenderLand(items);      // render the Land DropDown
        GetRibbon(GetCurrentUrl(), 1, 'Geo', false, false, false);   // Automatically re Render Bula
	}
}

/*
* Ajax CallBack for a Request on bula level
* Makes an Ajax Request either for regions or for types
*/
function BulaCallBack()
{
    //Check if The XML is OK
    if(checkReadyStateRibbon(xmlRequests[1]))			
	{	
	    // Get Values
        var response = xmlRequests[1].responseXML;
        
        if(response.getElementsByTagName("def").length == 0)    // TODO: deutet auf unnötigen ServerCallback hin
            return;
            
        var definition = response.getElementsByTagName("def")[0];
        var items = response.getElementsByTagName("element");        
        
        RenderBundesland(items);        // render the Bundesland         
                                
        if(HasSelectedLayer(1, 'Geo'))      // if he has selected a Bundesland -> make request for the Regions
        {        
            GetRibbon(GetCurrentUrl(), 3, 'Geo', true, true, true);                
        }
        else
        {   
            RenderRegions(new Array());             // Otherwise: clear the Regions and make a request for the Type
            GetRibbon(GetCurrentUrl(), 0, 'Type', false, false, false);                
        } 
	}
}
/*
* Ajax CallBack for a Request on region level
* Makes eiter an Ajax Request for the Types or the parameters
*/
function RegionenCallBack()
{
    //Check if The XML is OK
    if(checkReadyStateRibbon(xmlRequests[2]))			
	{	
	    // Get Values
        var response = xmlRequests[2].responseXML;
        
        if(response.getElementsByTagName("def").length == 0)    // TODO: deutet auf unnötigen ServerCallback hin
            return;
            
        var definition = response.getElementsByTagName("def")[0];                
        var userTriggered = definition.attributes.getNamedItem("userTriggered").value;                
        
        var root = response.childNodes[0];                                 
          
        RenderRegions(root.childNodes);     // Render the Regions
        
        if(userTriggered == "True") // Only refine type when clicked by user
        {
            GetRibbon(GetCurrentUrl(), 0, 'Type', false, false, false);
        }     
        else
        {
            GetParameters(GetCurrentUrl(), 'SearchRibbon');   // if Not user Triggered it was a Type click wich automatically refined the regions and we can refine the parameters
        }
	}
}

/*
* Ajax CallBack for a Request on group level
*/
function GroupCallBack()
{
    //Check if The XML is OK
    if(checkReadyStateRibbon(xmlRequests[3]))			
	{	
	    // Get Values
        var response = xmlRequests[3].responseXML;
        
        if(response.getElementsByTagName("def").length == 0)    // TODO: deutet auf unnötigen ServerCallback hin
            return;
            
        var definition = response.getElementsByTagName("def")[0];
        var items = response.getElementsByTagName("element");        
        
        RenderGroups(items);        // Render the Groups           
        if(HasSelectedLayer(1, 'Geo') || !IsAustriaSelected())  // Only austria needs Bula Selection (show info to select bula first)
        {                
            GetRibbon(GetCurrentUrl(), 1, 'Type', false, false, true);   // Make Ajax request for Types          
               
            document.getElementById("refineArrow").style.display="";
            document.getElementById("ImmoGroupHeader").className = "typeSelect";            
            document.getElementById("noBulaSelWarning").style.display = "none";
            document.getElementById("TypeScrollBox").style.display="";
        }
        else
        {   
            document.getElementById("noBulaSelWarning").innerHTML = TypeEmpty;
            document.getElementById("noBulaSelWarning").style.display = "";
            document.getElementById("TypeScrollBox").style.display="none";
            document.getElementById("refineArrow").style.display="none";
            document.getElementById("ImmoGroupHeader").className = "typeSelect inactive";                        
            
            // Set parameter Empty Box:
            var parameterContainer = document.getElementById("parameterContainer");
            parameterContainer.innerHTML = paramEmpty;	        
	        parameterContainer.className = "mieteKauf Checks disabled";	        
	        
	        if(isRibbonDebug)
	            ShowSearchRibbonInfo("Aktualsierung abgeschlossen.");                   
        }
	}
}
/*
* Ajax CallBack for a Request on type level
* makes an AJax request to Regions or the parameters
*/
function TypeCallBack()
{
    //Check if The XML is OK
    if(checkReadyStateRibbon(xmlRequests[4]))			
	{	
	    if(!HasSelectedLayer(1, 'Geo') && IsAustriaSelected())
            return;
            
	    // Get Values
        var response = xmlRequests[4].responseXML;
        
        if(response.getElementsByTagName("def").length == 0)    // TODO: deutet auf unnötigen ServerCallback hin
            return;
            
        var definition = response.getElementsByTagName("def")[0];
        var items = response.getElementsByTagName("element");        
        var userTriggered = definition.attributes.getNamedItem("userTriggered").value;                
        
        RenderTypes(items);  // Render the types
        
        if(IsAustriaSelected() && userTriggered == "True")  // if it was clicked by the user (no auto refinement)
        {   
            GetRibbon(GetCurrentUrl(), 3, 'Geo', true, false, true); // Make an request for the regions (NOTE: this shall not be UserTriggered, otherwise we would have an endless loop!!)
        }
        else
            GetParameters(GetCurrentUrl(), 'SearchRibbon'); // if it's not austria (no Regions) or it was not user Triggered -> get the parameters
	}
}

/*
* Ajax CallBack for checking if realestates exist for the current selection
*/
function CountCallBack()
{
    if(checkReadyStateRibbon(countXML))
    {
        var response = countXML.responseXML;
        var hasObjects = response.getElementsByTagName("Count")[0].attributes.getNamedItem("HasElements").value;
        
        /*
        * Make rendering of the arrows and the NoRealestatesFound image
        */
        if(HasSelectedLayer(1, 'Type'))
        {
            if(hasObjects == "True")
            {
                // objects exist -> everything ok
                document.getElementById("submitArrow").style.display="";
                document.getElementById("noResFound").style.display="none";
                document.getElementById("detailLink").className="searchButton";
            }
            else
            {
                // no objects exist -> show no re. found image
                document.getElementById("submitArrow").style.display="none";
                document.getElementById("noResFound").style.display="";
                document.getElementById("detailLink").className = "searchButton inactive";
            }
        }
        else
        {
            // he hasn't even selected a type, so show nothing and disable the click
            document.getElementById("noResFound").style.display="none";
            document.getElementById("submitArrow").style.display="none";
            document.getElementById("detailLink").className = "searchButton inactive";
        }
        
        if(isRibbonDebug)
            ShowSearchRibbonInfo("Aktualsierung abgeschlossen.");      
    }
}

//---------------------------------------------------------------------------------------------------
// END CallBacks
//---------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------
// Rendering Functions
//---------------------------------------------------------------------------------------------------

/*
* Updates the Rendering of the Bundesland (imageMaps) after a Selection
* key: the newly selected key
* isSelected: checks if the new Item is Selected
*/
function ReRenderBundesland(key, isSelected)
{
    var items = xmlRequests[1].responseXML.getElementsByTagName("element");   
    for(var i = 0; i < items.length; i++)   // Update the xmlRequest Item with the Selection
    {
        var kennung = items[i].attributes.getNamedItem("value").value;
        
        if(kennung == key)
        {
            if(isSelected)
                items[i].attributes.getNamedItem("sel").value = "True";            
            else
                items[i].attributes.getNamedItem("sel").value = "False";            
        }
    }
    // call default Rendering with the Updated XML
    RenderBundesland(items);
}

/*
* Updates the Rendering of the ImmoGroups after a Selection
* key: the newly selected key
* isSelected: checks if the new Item is Selected
*/
function ReRenderGroups(key, isSelected)
{
    var items = xmlRequests[3].responseXML.getElementsByTagName("element");   
    
    for(var i = 0; i < items.length; i++)                    // Update the xmlRequest Item with the Selection
    {
        var kennung = items[i].attributes.getNamedItem("value").value;
        
        if(kennung == key)
        {
            if(isSelected)
                items[i].attributes.getNamedItem("sel").value = "True";            
            else
                items[i].attributes.getNamedItem("sel").value = "False";            
        }
        else
        {
            items[i].attributes.getNamedItem("sel").value = "False";            
        }
    }
    
    // call default Rendering with the Updated XML
    RenderGroups(items);
}

/*
* Renders the DropDown for the COuntrys
* items: the list of items from an Ajax Request
*/
function RenderLand(items)
{
    var land = document.getElementById("landContainer");
    
    var outPut = "";
    outPut += LandHeader;
    var NonAustriaText = BulaNonAustria;
                    
    for(var i = 0; i < items.length; i++)   // Generate the HTML for the Request
    {   
        var isSelected = items[i].attributes.getNamedItem("sel").value;             
        var text = "";
        
        if(isSelected == "True")            
            text = LandSelected;
        else
            text = LandItem;
        
        
        for(var j = 0; j < items[i].attributes.length; j++)
        {
            //DEBUG
            /*if(i<2)
            {
                alert('Text: ' + text + ' | Name: ' + items[i].attributes[j].name + ' | Value: ' + items[i].attributes[j].value);
            }*/                   
            text = FillText(text, items[i].attributes[j].name, items[i].attributes[j].value);        
            if(isSelected == "True")
                NonAustriaText = FillText(NonAustriaText, items[i].attributes[j].name, items[i].attributes[j].value);        
        }
        
        text = FillText(text, "id", "id_Land_"+i);
        
        outPut += text;
    }    
    
    var isAustria = false;          // Check if the CUrrent selection is Austria
    for(var i = 0; i < currentSelection.Geo.length; i++)
    {
        if(currentSelection.Geo[i] == null)
            continue;
        if (currentSelection.Geo[i].key == "39926")
        {
            isAustria = true;
            break;
        }
    }
    
    var nonAustriaPanel = document.getElementById("ForeignCountryWarning");
    var austriaPanel = document.getElementById("austriaCountryPanel");

    if(!isAustria)  // make the divs visible or invisible depending on the selection (austria or not austria)
    {    
        nonAustriaPanel.innerHTML = NonAustriaText;
        nonAustriaPanel.style.display = "";
        austriaPanel.style.display = "none";
        HidePLZPopup('plzPopup');
        document.getElementById("plzOrtTextBoxContainer").style.display = "none";
    }
    else{
        document.getElementById("plzOrtTextBoxContainer").style.display = "block";
        nonAustriaPanel.style.display = "none";
        austriaPanel.style.display = "";
    }
    
    outPut+= LandFooter;
    land.innerHTML = outPut;    
}

/*
* Renders the ImageMap and CheckBoxes for the Bulas
* items: the list of items from an Ajax Request
*/

function RenderBundesland(items)
{
    var bulageoImageMap = document.getElementById("geoImageMap");   // the ImageMap Container
    var bulaImageMap = document.getElementById("ImageMapContainer");         // the Imge Container used by the ImageMap
    
    var bula = document.getElementById("bundeslandContainer");
    
    if(items.length == 0)   // Don't render if no items
    {  
       bulageoImageMap.innerHTML = "";
       bulaImageMap.innerHTML = "";
       return;
    }
    
    var outPut= BulaHeader;
    var imgGeoOutPut = "";
    var imageMapOutPut = bulaImageMapHeader;
    
    for(var i = 0; i < items.length; i++)       // Generate the HTML for the Request
    {        
        var isSelected = items[i].attributes.getNamedItem("sel").value;                     
        var text = "";
        var imgText = "";
        var imageMapText = bulaImageMapItem;
        
        if(isSelected == "True")  // Get the Template for the Text (selected / alternating) 
        {   
            imgText = bulaImageSelected;
            if((i % 2) == 0)
                text = BulaSelected;
            else
                text = BulaSelectedAlternating;
        }
        else
        {
            imgText = bulaImageItem;
            if((i % 2) == 0)
                text = BulaItem;
            else
                text = BulaAlternating;
        }
        
        
        
        for(var j = 0; j < items[i].attributes.length; j++) // Fill the Template with the values from the XML
        {                    
            
            var text = FillText(text, items[i].attributes[j].name, items[i].attributes[j].value);
            imgText = FillText(imgText ,  items[i].attributes[j].name, items[i].attributes[j].value);
            imageMapText = FillText(imageMapText,  items[i].attributes[j].name, items[i].attributes[j].value);            
        }
        
        text = FillText(text, "id", "id_BULA_"+i);                  // Add the ClientIds to the Checkboxes and imagemaps
        imageMapText = FillText(imageMapText, "id", "id_BULA_"+i);
        
        outPut += text;
        imageMapOutPut += imageMapText;
        imgGeoOutPut += imgText;
    }
    
    if(items.length % 2 != 0)
    {
        outPut += BulaEmptyAlternating;
    }
    
    imgGeoOutPut += bulaImageFooter;
    
    bulageoImageMap.innerHTML = imgGeoOutPut;
    
    imageMapOutPut += bulaImageMapFooter;    
    bulaImageMap.innerHTML = imageMapOutPut;
    
    outPut += BulaFooter;    
    bula.innerHTML =outPut;     
}
/*
* Renders the Regions and there parents for the given Ajax request
* items: the list of items from an Ajax Request
*/
function RenderRegions(items)
{
    // erste ebene: gruppen, zweite checkboxen
    var regions = document.getElementById("regionContainer");
    var outPut = "";
    var renderedSelection = new Array();
        
    for(var i = 1; i < items.length; i++)           // First one is the definition -> start loop at 1
    {     
        var parent = items[i].attributes.getNamedItem("par").value;                               
        var parentIndex = items[i].attributes.getNamedItem("index").value;
        
        if(items[i].childNodes.length == 0) // if it has no child Elements -> don't render anything
            continue;
                    
        var isSelected = items[i].attributes.getNamedItem("sel").value;                    
         
        // Render the Group:
        var text = "";
        if(isSelected == "True")            
            text = RegionsGroupHeader;
        else
            text = RegionsGroupHeader;
                 
        for(var j = 0; j < items[i].attributes.length; j++)
        {                    
            var text = FillText(text, items[i].attributes[j].name, items[i].attributes[j].value);                                                                                                                        
        }        
        
        outPut += text;
        outPut += RegionsHeader;       
        
        // Render the Regions for the Group:
        for(var j = 0; j < items[i].childNodes.length; j++)
        {
            var key = items[i].childNodes[j].attributes.getNamedItem("value").value;                    
              
            var isChildSelected = items[i].childNodes[j].attributes.getNamedItem("sel").value;                    
            var isChildEmpty = (items[i].childNodes[j].attributes.getNamedItem("cnt").value == "0");                    
            var childIndex = items[i].childNodes[j].attributes.getNamedItem("index").value;
            var childText = "";
            var isClientSelected = CheckIsItemSelected(key, 3, 'Geo')
            
            if(isChildEmpty)
            {   
                if((j % 2) == 0)
                    childText = RegionsNOObjectsItem;            
                else
                    childText = RegionsNOObjectsAlternating;
            }
            else if(isChildSelected == "True" || isClientSelected)               //|| CheckIsItemSelected(key, 3, 'Geo')
            {   
                renderedSelection.push(key);   
                if((j % 2) == 0)
                    childText = RegionsSelected;            
                else
                    childText = RegionsSelectedAlternating;
            }
            else
            {   
                renderedSelection.push(key);   
                if((j % 2) == 0)
                    childText = RegionsItem;
                else
                    childText = RegionsAlternating;
            }
                
            for(var n = 0; n <  items[i].childNodes[j].attributes.length; n++)
                childText = FillText(childText, items[i].childNodes[j].attributes[n].name, items[i].childNodes[j].attributes[n].value);
            
            
            var childText = FillText(childText, "id", "id_Reg_" + items[i].childNodes[j].attributes.getNamedItem("value").value);
            var childText = FillText(childText, "parentsparent", parent);
            
            childText = FillText(childText, "parentvalue", items[i].attributes.getNamedItem("value").value);
            childText = FillText(childText, "index", childIndex);
            childText = FillText(childText, "parentindex", parentIndex);
            
            outPut += childText;                     
        }
        
        if((j % 2) != 0)
            outPut += RegionsEmptyAlternating;
            
        outPut += RegionsFooter;        
        
        outPut += RegionsGroupFooter;        
    }
    
    // Hide the Message of Changed Regions if they are open
    /*if(document.getElementById("ribbonInfoPanel") != null && document.getElementById("ribbonInfoPanel").innerHTML == autoChangedRegionText)
        HideRibbonInfo();
    */
    
    // ensure that only visible items are valid (shown and used for refinement Requests)
    for(var i = 0; i < currentSelection.Geo.length; i++)
    {   
        if(currentSelection.Geo[i] == null || currentSelection.Geo[i].layer != 3)
            continue;
            
        var foundSelection = false;
        for(var j = 0; j < renderedSelection.length; j++)       // Check if it was rendered
        {
            if(currentSelection.Geo[i].key == renderedSelection[j])
            {
                foundSelection = true;
                break;
            }
        }
        
        if(!foundSelection) // if it was Not rendered -> set the item to invisible
        {    
            currentSelection.Geo[i].isValid = false;     
            //ShowSearchRibbonInfo(autoChangedRegionText);
                  
            var parentKey = currentSelection.Geo[i].parentkey;
            
            var hasElementsAtLayer = false;
        
            // Check if there are other elements of the Same type at the Same Layer and the same parent which are valid
            for(var j = 0; j < currentSelection.Geo.length; j++)
            {
                if(currentSelection.Geo[j] == null || !currentSelection.Geo[j].isValid)
                    continue;
    
                if(currentSelection.Geo[j].layer == 3 && currentSelection.Geo[j].parentkey == parentKey)
                {
                    hasElementsAtLayer = true;
                    break;
                }
            }
            
            if(!hasElementsAtLayer) // if no valid Childs where found -> set the parent item to invalid
            {   
                for(var j = 0; j < currentSelection.Geo.length; j++)
                {
                    if(currentSelection.Geo[j] == null)
                        continue;
                        
                    if(currentSelection.Geo[j].key == parentKey)
                        currentSelection.Geo[j].isValid = false;
                }                
            }
        }
        else        
        {
            // if the Item was rendered -> ensure that it (and it's parent) is valid
            currentSelection.Geo[i].isValid = true; 
            var parentKey = currentSelection.Geo[i].parentkey;
            
            for(var j = 0; j < currentSelection.Geo.length; j++)
            {
                if(currentSelection.Geo[j] == null)
                    continue;
                        
                if(currentSelection.Geo[j].key == parentKey)
                    currentSelection.Geo[j].isValid = true;
            }
        }
    }
    regions.innerHTML = outPut;
}

/*
* Renders the Groups for the given Ajax request
* items: the list of items from an Ajax Request
*/
function RenderGroups(items)
{
    var groups = document.getElementById("groupContainer");
    var outPut = GroupHeader
    
    for(var i = 0; i < items.length; i++)
    {         
        var isSelected = items[i].attributes.getNamedItem("sel").value;                    
        var text = "";
        if(isSelected == "True")            
            text = GroupSelected;
        else
            text = GroupItem;
        
        for(var j = 0; j < items[i].attributes.length; j++)
        {                    
            var text = FillText(text, items[i].attributes[j].name, items[i].attributes[j].value);                                                                                                                        
        }
        
        var text = FillText(text, "id", "id_GROUP_"+i); // Set the ClientID
        
        outPut+= text;
    }
    outPut+= GroupFooter;    
    groups.innerHTML = outPut;
}

/*
* Renders the Types for the given Ajax request
* items: the list of items from an Ajax Request
*/
function RenderTypes(items)
{
    var types = document.getElementById("typeContainer");
    var outPut = TypeHeader;
    
    for(var i = 0; i < items.length; i++)
    {         
        var isSelected = items[i].attributes.getNamedItem("sel").value;                    
        var cnt = items[i].attributes.getNamedItem("cnt").value;
        
        var text = "";
        if(cnt == "0")
        {
            text = TypeEmptyItem;
        }
        else if(isSelected == "True")            
            text = TypeSelected;
        else
            text = TypeItem;
        
        for(var j = 0; j < items[i].attributes.length; j++)
        {                    
            var text = FillText(text, items[i].attributes[j].name, items[i].attributes[j].value);                                                                                                                        
        }
        var text = FillText(text, "id", "id_TYPE_"+i);    
        
        outPut  += text;
    }
    
    outPut += GroupFooter;
    types.innerHTML = outPut;    
}

/*
* Renders the Parameters for the Current selection
* makes an ajax request to check the count
*/
function RenderParameters()
{
    if(checkReadyStateRibbon(paramXML))			//Check if The XML is OK
    {

        var response = paramXML.responseXML;
	    var items = response.getElementsByTagName("element");
	    var parameterContainer = document.getElementById("parameterContainer");
	    var outPut = paramHeader;

	    if(items.length == 0)       // If no parameters are available -> render the empty template
	    {   
	        currentSelection.Parameters = new Array();	        
	        outPut = paramEmpty;	        
	        parameterContainer.className = "mieteKauf Checks disabled";
	    }
	    else
	        parameterContainer.className = "mieteKauf Checks";
	        
	    for(var i = 0; i < items.length; i++)
	    {
	        var isSelected = items[i].attributes.getNamedItem("sel").value;   
	        var text = "";
	        
            if(isSelected == "True")   
            {   
                text = paramSelected;
            }
            else
                text = paramItem;  
                
            for(var j = 0; j < items[i].attributes.length; j++)
            {                    
                var text = FillText(text, items[i].attributes[j].name, items[i].attributes[j].value);                                                                                                                        
            }
            var text = FillText(text, "id", "id_PARAM_"+i);    
        
            outPut  += text;                   
	    }
	    
	    if(items.length > 0)
	        outPut += paramFooter;
	        
	    parameterContainer.innerHTML = outPut;  // set the HTML
	    
	    // check if realestates exist
	    CheckCount(GetCurrentUrl());
	}	
}

//---------------------------------------------------------------------------------------------------
// END Rendering Functions
//---------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------
// Selection Helpers
//---------------------------------------------------------------------------------------------------

/*
* executes a Search and referes the user to the result Page (if results are available)
*/
function PerformRibbonSearch(nextPage)
{
    if(countXML == null)
        return;
    
    var response = countXML.responseXML;
    
    if(response == null)
        return;
        
    var hasObjects = "False";
    
    if(response.getElementsByTagName("Count").length > 0)
    {
       hasObjects = response.getElementsByTagName("Count")[0].attributes.getNamedItem("HasElements").value;
    }
    
    if(hasObjects == "True" && HasSelectedLayer(1, 'Type'))
    {
        var params = ""
        for(var i = 0; i< currentSelection.Parameters.length; i++)
        {
        if(currentSelection.Parameters[i]=="ismiete" || currentSelection.Parameters[i]=="iskauf"||currentSelection.Parameters[i]=="isbauprojekt"){
            params  += "&";
            params +=currentSelection.Parameters[i] +"=1";
       }}
       // Be sure to add all Items (also the Invalid Items) to ensure the Selection is the same on the ResultList

       //findmich
       window.location.href = LinkBaseDir + GetCurrentSearchPartUrl(true, false, false, nextPage) + params;
   }
}

/*
* initializes the Ribbon Control from the Url Parameters, and if none are available -> set default values (austria -> living)
*/
function InitRibbonFromUrl()
{   
    if(isRibbonDebug)
        ShowSearchRibbonInfo("Initialisiere...");
        
    var Regex = new RegExp("/", "gim");
    var RegexSub = new RegExp("-", "gim");
    var curGeoLayer = 0;
    var curTypeLayer = 0;
    var segments = window.location.pathname.split(Regex)
    var SearchSegments = new Array();
    var isSearch = false;
    
    for(var i = 0; i < segments.length; i++)
    {   
        if(segments[i].charAt(0) == 'G' || segments[i].charAt(0) == 'L')
        {
            SearchSegments.push(segments[i]);
        }
        if(segments[i].length > 2 && segments[i].substr(0, 2) == "DB")
        {
            SearchSegments.push(segments[i-1]);
            SearchSegments.push(segments[i]);
        }
        if (segments[i] == "treffer.aspx") 
        {
            isSearch = true;
        }    
    }
    
    
    
    var tmpQuery = window.location.search.substring(1);        
	var tmpVars = tmpQuery .split("&");
	
	for(var i = 0; i < tmpVars.length; i++)
	{
	    if(tmpVars[i] == "")
	        continue;
	    var pair = tmpVars[i].split("=");		    
		    
		if(pair.length != 2 || pair[0] == "")
		    continue;
		    
	    if(pair[0] == "eDB" || pair[0] == "eCult" || pair[0].substr(0, 4) == "eGeo" || pair[0].substr(0, 5) == "eType")
	    {
	        isSearch = true;
	    }
	}
	   
    if(SearchSegments.length == 0 && !isSearch)
    {
        // If nothing was found: initialize default:
        AddSelection('39926', 0, 'Geo', null, "Österreich", 0, "Oesterreich"); // default austria
        AddSelection('346', 0, 'Type', null, "Wohnen", 0, "Wohnen"); // default wohnen
        GetRibbon(GetCurrentUrl(true), 0, 'Geo', false, false, false);   
    }
    else 
    {   
        var url = LinkBaseDir;
        
        for(var i = 0; i < SearchSegments.length; i++)
        {
            url += SearchSegments[i] +"/";
        }
    
        url = window.location.pathname.replace("treffer.aspx", "");
        url += "init.srh?initSelection=true&db=Immobiliensuche&getempty=true";
        
        var query = window.location.search.substring(1);        
	    var vars = query.split("&");
	
	    for(var i = 0; i< vars.length; i++)
	    {
	        if(vars[i] == "")
	            continue;
	            
		    var pair = vars[i].split("=");		    
		    
		    if(pair.length != 2 || pair[0] == "")
		        continue;
		        
			url += "&" +pair[0] + "=" + pair[1];		    
	    }         
        GetInitialisationValues(url);        
    }
}

/*
* Checks if an item exists in the current selection for the given type
*/
function HasSelectedType(type)
{
    if(type == 'Type')
    {
        for(var i = 0; i < i < currentSelection.Type.length; i++)        
            if(currentSelection.Type[i] != null)
                return true;
    }
    else if(type == 'Geo')
    {
        for(var i = 0; i < i < currentSelection.Geo.length; i++)
            if(currentSelection.Geo[i] != null)
                return true;
    } 
    
    return false;
}
/*
* Checks if the user has selected a specific layer
* layer: the layer to check
* type: the type to check
*/
function HasSelectedLayer(layer, type)
{   
    if(type == 'Type')
    {
        for(var i = 0; i < currentSelection.Type.length; i++)
        {
            if(currentSelection.Type[i] == null)
                continue;
                
            if(currentSelection.Type[i].layer == layer)
                return true;
        }
    }
    else if(type == 'Geo')
    {
        for(var i = 0; i < currentSelection.Geo.length; i++)
        {
            if(currentSelection.Geo[i] == null)
                continue;
                
            if(currentSelection.Geo[i].layer == layer)
                return true;
        }
    }
    return false;
}

/*
* Recursively Removes the Selection for the given parameters
* key: the key of the Item to remove
* layer: the layer of the Item to remove
* type: from which type is the item which has to be removed
*/
function RemoveSelection(key, layer, type)
{   
    if(type == 'Type')
    {
        for(var i = 0; i < currentSelection.Type.length; i++)
        {  
            if(currentSelection.Type[i] == null)
                continue;
                
            if(currentSelection.Type[i].key == key)  //Remove the item itself
               currentSelection.Type[i] = null;
            else if(currentSelection.Type[i].parentkey == key)  // if there is an item which's parent is the removed item -> recursively remove the item
            {
               //Recursive Removement
               RemoveSelection(currentSelection.Type[i].key, currentSelection.Type[i].layer, type);
            }
        }        
    }
    else if(type == 'Geo')
    {            
        for(var i = 0; i < currentSelection.Geo.length; i++)
        {  
            if(currentSelection.Geo[i] == null)
                    continue;
                    
           if(currentSelection.Geo[i].key == key)       //Remove the item itself
                currentSelection.Geo[i] = null;
                
           else if(currentSelection.Geo[i].parentkey == key)    // if there is an item which's parent is the removed item -> recursively remove the item
           {
               //Recursive Removement
               RemoveSelection(currentSelection.Geo[i].key, currentSelection.Geo[i].layer, type);
           }
        }        
    }
}

/*
* Adds an Item to the Current Selection
* key: the key of the item to add
* layer: the layer of the item to add
* parent: the key of the parent of the item to add
*/
function AddSelection(key, layer, type, parent, bezeichnung, index, seoName)
{
    var isSelected = false;    
    
    if(type == 'Type')
    {
        
        for(var i = 0; i < currentSelection.Type.length; i++)
        {
            if(currentSelection.Type[i] == null)
                continue;
                
            if(currentSelection.Type[i].key == key && currentSelection.Type[i].layer == layer)
                return;
        }
        
        // generate a new SelectionItem and push it to the selection
        var it = new SelectionItem();
        it.layer = layer;
        it.key = key;        
        it.parentkey = parent;
        it.Bezeichnung = bezeichnung;
        it.index = index;
        it.seoName = seoName;
        currentSelection.Type.push(it);
    }    
    else if(type == 'Geo')
    {
        for(var i = 0; i < currentSelection.Geo.length; i++)
        {
             if(currentSelection.Geo[i] == null)
                continue;
             if(currentSelection.Geo[i].key == key && currentSelection.Geo[i].layer == layer)
                return;
        }
        
        // generate a new SelectionItem and push it to the selection
        var it = new SelectionItem();
        it.layer = layer;
        it.key = key;
        it.parentkey = parent;
        it.Bezeichnung = bezeichnung;
        it.index = index;
        it.seoName = seoName;
        currentSelection.Geo.push(it);
    }
}

/*
* gets the Path for the Result refering and the Ajax Requests for the Current Selection
* @ignoreValid: bit to check if the validity of items should be ignored (default: false)
*/
function GetCurrentSearchPartUrl(ignoreInvalid, isLandInitilisation, sEOUrl, nextPage) {

    
    var db = "Immobiliensuche";    
    var lang = "at";    
    
    url = "?eCult=at";
    url += "&eDB=DB" + db;

    var sArr = SortArray(currentSelection.Type);  //Sort the array by layer
    var prevLayer = -1;
    var tmpUrl = ""; //=QueryString
    var BeforeUrl = "";
    
    

    //Type (Immoart)
    prevLayer = -1;

    var rangedItems = new Array();
    var cLayer = -1;
    for(var i = 0; i<sArr.length; i++)
    {   
        if(sArr[i] == null)
            continue;
        
        if(!sArr[i].isValid && !ignoreInvalid)  // make sure to add only parameters which are valid if required
            continue;
        
        if(isLandInitilisation && sArr[i].layer > 0)    // initialisation from url for land
            continue;
            
        if(sArr.length == 1) {
               tmpUrl += "&eType" + sArr[i].layer + "=";
               tmpUrl += sArr[i].key;       
        }       
        else {
            var nextItem = null;    // get the next item in the Collection
            
            nextItem = GetNextItem(i, sArr);
                
            if(nextItem != null && nextItem.layer == sArr[i].layer) // next item is a Ranged item -> push it to the array
            {
                rangedItems.push(sArr[i]);                
            }
            else {
            
                //Ab hier ist es eine Range
                if(rangedItems.length > 0)  // do we have ranged Items
                {
                    rangedItems.push(sArr[i]);
                    var tmpArr = SortArrayByIndex(rangedItems);
                    var firstObject = null;
                    if(sEOUrl) {
                        tmpUrl += "&eMultiType"+sArr[i].layer+"=L";
                        BeforeUrl += "immobilien" + sArr[i].layer + "/" ;
                    }
                    else
                    {
                        tmpUrl += "&eType"+sArr[i].layer+"="; 
                    }
                    for(var j = 0; j < tmpArr.length; j++) 
                    {
                        tmpUrl += tmpArr[j].key;
                        if(j < tmpArr.length -1)
                            tmpUrl += "-";
                    }
                    
                    rangedItems = new Array();
                }
                else {
                    //Ab hier ist es keine Range und eine neuer Layer
                    // Changed layer:
                    if (sEOUrl) {
                        BeforeUrl += sArr[i].seoName + "/";
                    }
                    else {
                        tmpUrl += "&eType" + sArr[i].layer + "=";
                        tmpUrl += sArr[i].key;
                        rangedItems = new Array();
                    }
                }
            }
           
        }                                            
        
        prevLayer = sArr[i].layer;        
    }
    
    url += tmpUrl;
    
    //Type (Immoart) End
    rangedItems = new Array();
    //Geo
    sArr = SortArray(currentSelection.Geo); //Sort the array by layer
    for(var i = 0; i<sArr.length; i++)
    {   
        if(sArr[i] == null)
            continue;
        
        if(!sArr[i].isValid && !ignoreInvalid)  // make sure to add only parameters which are valid if required
            continue;
        
        if(isLandInitilisation && sArr[i].layer > 0)    // initialisation from url for land
            continue;
            
        if(sArr.length == 1) {
               tmpUrl += "&eGeo" + sArr[i].layer + "=";
               tmpUrl += sArr[i].key;       
        }       
        else {
            var nextItem = null;    // get the next item in the Collection
            
            nextItem = GetNextItem(i, sArr);
                
            if(nextItem != null && nextItem.layer == sArr[i].layer) // next item is a Ranged item -> push it to the array
            {
                rangedItems.push(sArr[i]);                
            }
            else {
            
                //Ab hier ist es eine Range
                if(rangedItems.length > 0)  // do we have ranged Items
                {
                    rangedItems.push(sArr[i]);
                    var tmpArr = SortArrayByIndex(rangedItems);
                    var firstObject = null;
                    
                    
                    if (sEOUrl) {
                        tmpUrl += "&eMultiGeo"+sArr[i].layer+"=G";
                        BeforeUrl += "geografie" + sArr[i].layer + "/" ;
                    }
                    else {
                        tmpUrl += "&eGeo"+sArr[i].layer+"=";
                    }
                    for(var j = 0; j < tmpArr.length; j++) 
                    {
                        tmpUrl += tmpArr[j].key;
                        if (j < tmpArr.length - 1)
                            tmpUrl += "-";
                    }
                    
                    rangedItems = new Array();
                }
                else {
                    //Ab hier ist es keine Range und eine neuer Layer
                    // Changed layer:
                    if (sEOUrl) {
                        BeforeUrl += sArr[i].seoName + "/";
                    }
                    else {
                        tmpUrl += "&eGeo" + sArr[i].layer + "=";
                        tmpUrl += sArr[i].key;
                        rangedItems = new Array();
                    }
                }
            }
           
        }                                            
        
        prevLayer = sArr[i].layer;        
    }
    
    url += tmpUrl;
    //Geo End
    
    if(currentSelection.isProject)
        url += "&isPrSrh=true";
    else
        url += "&isPrSrh=false";
    if (sEOUrl) {
        return BeforeUrl + nextPage + url;
    }
    else if(nextPage != null) {
        
        return nextPage + url;
    }  
    else
    {
        return url;
    }
}

function GetElementWithIndex(index, coll)
{
    for(var i = 0; i < coll.length; i++)
    {
        if(coll[i] == null)
            continue;
            
        if(coll[i].index == index)
            return coll[i];
    }
    
    return null;
}

function IsLastIndex(index, coll)
{
    var cntr = 0;
    for(var i = 0; i < coll.length; i++)
        if(coll[i] != null)
            cntr ++;

    return index == cntr;
}

function ExceedsLength(index, coll)
{
    var cntr = 0;
    for(var i = 0; i < coll.length; i++)
        if(coll[i] != null)
            cntr ++;
            
    return index > cntr;
}

function GetNextItem(index, coll)
{
    for(var i = index+1; i < coll.length; i++)
        if(coll[i] != null)
            return coll[i];
    return null;
}
/*
* Gets the Current Url for the Ajax Requests
*/
function GetCurrentUrl(isLandInitialisation)
{
    var db = "Immobiliensuche";        
    url = LinkBaseDir + "ribbon.srh";
    url += GetCurrentSearchPartUrl(false, isLandInitialisation);    
    
    for(var i = 0; i< currentSelection.Parameters.length; i++)
    {  if(currentSelection.Parameters[i]=="ismiete" || currentSelection.Parameters[i]=="iskauf"||currentSelection.Parameters[i]=="isbauprojekt"){ 
        url  += "&";
        url +=currentSelection.Parameters[i] +"=1";
        }
    }
    
    if(url.indexOf("?") == -1)
        url += "?";
    else
        url += "&";
        
    url += "db="+db;
        
    return url;
}

/*
* Checks if the Item with the given parameters is in the Current Selection
* key: the key of the item to look for
* layer: the layer of the item to look for
* type: the type of the item to look for
*/
function CheckIsItemSelected(key, layer , type)
{
    if(type == 'Type')
    {
        for(var i = 0; i < currentSelection.Type.length; i++)
        {
            if(currentSelection.Type[i] == null)
                continue;
            if(currentSelection.Type[i].layer == layer && currentSelection.Type[i].key == key)
                return true;
        }
    }
    else if(type == 'Geo')
    {
        for(var i = 0; i < currentSelection.Geo.length; i++)
        {
            if(currentSelection.Geo[i] == null)
                continue;
            if(currentSelection.Geo[i].layer == layer && currentSelection.Geo[i].key == key)
                return true;
        }
    }
    return false;
}

/*
* checks if the selected Country is austria
*/
function IsAustriaSelected()
{
    var landddl = document.getElementById("ddlLand");                
    var selLand= landddl.options[landddl.selectedIndex].value;
    return selLand == "39926|Oesterreich";
}

//---------------------------------------------------------------------------------------------------
// END Selection Helpers
//---------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------
// HELPER Functions
//---------------------------------------------------------------------------------------------------
/*
* Helper Function to Remove an Item from an Array
* deprecated??
*/
function RemoveArray(collection, from, to)
{
    var rest = collection.slice((to || from) + 1 || collection.length);
    collection.length = from < 0 ? collection.length + from : from;
    return collection.push.apply(collection, rest);
}

/*
* Helper function for Sorting an Array
*/
function SortArray(array)
{
    return array.sort(SelectionComparer);
}

function SortArrayByIndex(array)
{
    return array.sort(IndexComparer);
}

/*
* A Comparer for the Sorting of two SelectionItems
*/
function SelectionComparer(a, b)
{
    if(a == null)
        return 1;
    else if(b == null)
        return -1;
    else
    {
        return a.layer - b.layer;
    }
}


function IndexComparer(a, b)
{
      if(a == null)
        return 1;
    else if(b == -1)
        return false;
    else
    {
        return a.index - b.index;
    }
}


/*
* Checks if the XMLHTTPRequest object is ready
*/
function checkReadyStateRibbon(obj)
{   
  if(obj.readyState == 4)
  {		
    if(obj.status == 200)
    {    
        return true;
    }
    else
    {           
        ShowSearchRibbonInfo("Es ist ein Fehler beim Laden der Suche aufgetreten!<br /> Wenn sie Internet Explorer 5 oder 6 installiert haben, stellen sie Sicher dass ActiveX erlaubt ist! ");
    }
  }  
         
}

/*
* TODO: doppelt
*/
function FillText(text, tagname, value)
{        
    var Regex = new RegExp("!!" + tagname +"!!", "gim");
    
    return text.replace(Regex, value);            
}

/*
* Shows the PLZPopup
*/
function ShowPLZPopup(globalPopUp)
{
    if (document.getElementById(globalPopUp) != null) {
		document.getElementById(globalPopUp).style.display='block';
	}
}

// The initial value of the PLZDropDown before anything else is selected
var initialPLZDropDownText = null;

/*
* Hides the PLZPopup, and updates the text5 of the PLZ - DropDown
*/
function HidePLZPopup(globalPopUp)
{
    if (document.getElementById(globalPopUp).style.display=='block')	{
			document.getElementById(globalPopUp).style.display='none';
	}
	
	if(document.getElementById('plzOrtTextBox') != null)
	{
	    if(initialPLZDropDownText  == null)
        {
            initialPLZDropDownText  = document.getElementById('plzOrtTextBox').getElementsByTagName('option')[0].innerHTML;
        }
        
	    window.setTimeout('document.getElementById("plzPopup").style.display="none"',10);
	    var Inhalt = document.getElementById('plzPopup').getElementsByTagName('input');
        
        var SelectedString = ' ';
    	
	    var checkedCounter = 0;    
    	
	    for (i=0; i<Inhalt.length; i++) {
	        if(Inhalt[i].checked)
	        {
	            if(navigator.appName == "Microsoft Internet Explorer")
                    SelectedString += Inhalt[i].parentNode.innerText + ', ';	            
                else
                    SelectedString += Inhalt[i].parentNode.textContent+ ', ';
                    
                checkedCounter++;
	        }
	    }
    	    
	    SelectedString = SelectedString.substring(0,30)+'...'
    	
	    if(checkedCounter == 0)
	        SelectedString = initialPLZDropDownText;
    	    
	    if (SelectedString != ' ...')
		    document.getElementById('plzOrtTextBox').getElementsByTagName('option')[0].innerHTML = SelectedString;
	}
}

/*
* debug helper for showing the Current Selection
*/
function ShowCurrentSelection()
{
    var outPut = document.getElementById("debugOutput");
    var tmpDebug = "<table border=\"1\"><th>Key</th><th>Layer</th><th>Name</th><th>Parent</th><th>index</th><th>IsValid</th>";
    
    for(var i = 0; i < currentSelection.Type.length; i++)
    {
        if(currentSelection.Type[i] == null)
            continue;
            
        tmpDebug += "<tr><td>" + currentSelection.Type[i].key +"</td><td>" + currentSelection.Type[i].layer +"</td>";
        tmpDebug += "<td>";
        if(currentSelection.Type[i].Bezeichnung != null)
        {
            tmpDebug += currentSelection.Type[i].Bezeichnung;
        }
        else
            tmpDebug += "&nbsp";
            
        tmpDebug += "</td>";
        tmpDebug += "<td>";
        if(currentSelection.Type[i].parentkey != null)
        {
            tmpDebug += currentSelection.Type[i].parentkey;
        }
        else
            tmpDebug += "&nbsp";
        tmpDebug += "</td>";
        
        tmpDebug += "<td>";
        if(currentSelection.Type[i].index != null)
        {
            tmpDebug += currentSelection.Type[i].index;
        }
        else
            tmpDebug += "&nbsp";
            
        tmpDebug += "</td>";
        
        tmpDebug += "<td>"+currentSelection.Geo[i].isValid+"</td";
        
        tmpDebug += "</tr>";
        
    }
    
    tmpDebug += "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
    
    for(var i = 0; i < currentSelection.Geo.length; i++)
    {
        
        if(currentSelection.Geo[i] == null)
        {
            tmpDebug += "<tr><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td></tr>";
             continue;
        }
                    
        tmpDebug  += "<tr><td>" + currentSelection.Geo[i].key +"</td><td>" + currentSelection.Geo[i].layer +"</td>";
        
        tmpDebug += "<td>";
        
        if(currentSelection.Geo[i].Bezeichnung != null)
        {
            tmpDebug += currentSelection.Geo[i].Bezeichnung;
        }
        else
            tmpDebug  += "&nbsp";
            
        tmpDebug  += "</td>"
        tmpDebug  += "<td>";
        
        if(currentSelection.Geo[i].parentkey != null)
        {
            tmpDebug  += currentSelection.Geo[i].parentkey ;
        }
        else
            tmpDebug += "&nbsp";            
        tmpDebug += "</td>"

        tmpDebug  += "<td>";
        
        if(currentSelection.Geo[i].index != null)
        {
            tmpDebug  += currentSelection.Geo[i].index;
        }
        else
            tmpDebug += "&nbsp";            
        tmpDebug += "</td>"
        
        tmpDebug += "<td>"+currentSelection.Geo[i].isValid+"</td";
        
        tmpDebug += "</tr>";
    }
      
    tmpDebug += "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
        
    for(var i = 0; i < currentSelection.Parameters.length; i++)
    {
        tmpDebug +="<tr><td>"+ currentSelection.Parameters[i] +"</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
    }
    
    tmpDebug += "</table>";
    tmpDebug += "<br />";
    tmpDebug += GetCurrentSearchPartUrl();
    tmpDebug += "<br />";
    tmpDebug += GetCurrentSearchPartUrl(true);
    tmpDebug += "<br />";
    tmpDebug += currentSelection.isProject;
    
    outPut.innerHTML = tmpDebug;
}

/*
* Shows an Informational message to the user
*/
function ShowSearchRibbonInfo(message)
{
    document.getElementById("ribbonInfoPanel").innerHTML = message;
    document.getElementById("ribbonInfoContainer").style.display = "block";
}

/*
* hides the Container for the messages for the User
*/
function HideRibbonInfo()
{
    document.getElementById("ribbonInfoContainer").style.display = "none";
}


// ShowRibbon blendet SearchRibbon ein und setzt SucheEinblendenButton auf display:none
function ShowRibbbon()
{
    document.getElementById("ribbonContainer").style.display = "";
    document.getElementById("panel_HideSearch").style.display="none";
    document.getElementById("panel_ShowSearch").style.display="";
}

// HideRibbon blendet SearchRibbon aus und setzt SucheEinblendenButton auf display:block
function HideRibbon()
{
    document.getElementById("ribbonContainer").style.display = "none";
    document.getElementById("panel_HideSearch").style.display="";
    document.getElementById("panel_ShowSearch").style.display="none";
}





//---------------------------------------------------------------------------------------------------
// END HELPER Functions
//--------------------------------------------------------------------------------------------------
