﻿
////////////////////////////////////////////////////////////////////////////////////////////////////
// LOGIK FÜR HÄNDLERSUCHE
////////////////////////////////////////////////////////////////////////////////////////////////////

var vGlobDefaultSearchFunction = null; // Do not delete this -> important!

////////////////////////////////////////////////////////////////////////////////////////////////////
// ALLGEMEINE LOGIK FÜR HÄNDLERSUCHE 
////////////////////////////////////////////////////////////////////////////////////////////////////

// Session Aktualisieren
function UpdateSessionAndShowFormular(formname) {

    // Wichtige Felder initialisieren
    var vDsDisplayStateDisplayFunction = document.getElementById("DsDisplayStateDisplayFunction");
    if (vDsDisplayStateDisplayFunction != null) {
        vDsDisplayStateDisplayFunction = vGlobDefaultSearchFunction;
    }

    var vIsSessionUpToDate = document.getElementById("DsDisplayStateDisplayFunction");
    if (vIsSessionUpToDate != null) {
        vIsSessionUpToDate.value = formname;
    }

    var vSubButton = document.getElementById("btn_SessionUpdater");
    if (vSubButton != null) {
        vSubButton.click();
    }
}

// Wenn die Session erfolgreich aktualisiert wurde
function onSessionUpdated() {

    var vIsSessionUpToDate = document.getElementById("DsDisplayStateDisplayFunction");
    //alert(vIsSessionUpToDate.value);
    if (vIsSessionUpToDate != null) {
        showFormular(vIsSessionUpToDate.value);
    }
}

function showFormular(formname) {
    if (formname == "blockview") {
        if (pwbox != null) pwbox.Show();
        //blockview
        var vSubButton = document.getElementById("btn_fillBlockResultContent");
        if (vSubButton != null) {
            vSubButton.click();
        }
    } else if (formname == "listview") {
        if (pwbox != null) pwbox.Show();
        // listview
        var vSubButton = document.getElementById("btn_fillListResultContent");
        if (vSubButton != null) {
            vSubButton.click();
        }
    } else if (formname == "mapview") {
        if (pwbox != null) pwbox.Show();
        // google map view
        var vSubButton = document.getElementById("btn_fillGoogleMapsResultContent");
        if (vSubButton != null) {
            vSubButton.click();
        }
    }
}

// Zufällige Händler als Startseite anzeigen.
function DisplayStartPage() {
    var vSubButton = document.getElementById("btn_fillStartPageContent");
    if (vSubButton != null) {
        vSubButton.click();
    }
}

// Ergebnisse als "Blockübersicht"
function DisplaySearchResultBlockView() {

    UpdateSessionAndShowFormular("blockview");
}

// Ergebnisse als "Listenansicht"
function DisplaySearchResultListView() {

    UpdateSessionAndShowFormular("listview");
}

// BlockView
function BlockView() {
    vGlobDefaultSearchFunction = "blockview";
    DoSearch();
}

// MapView
function MapView() {
    vGlobDefaultSearchFunction = "mapview";
    DoSearch();
}


// ListView
function ListView() {
    vGlobDefaultSearchFunction = "listview";
    DoSearch();
}

// StartPage
function StartpageView() {
    //alert("StartpageView");
    vGlobDefaultSearchFunction = null;
    //StartpageView("StartpageView->calling DisplayStartPage");
    DisplayStartPage();
}

function DoSearchFromButton() {

    var vPageRange = document.getElementById("DsDisplayStateSelectedPageRange");
    if (vPageRange != null) {
        vPageRange.value = "";
    }
    
    DoSearch();
}

// Suchen
function DoSearch() {

    if (pwbox != null) pwbox.Show();

    UpdateAllFilterFields();
    
    if (vGlobDefaultSearchFunction == null || typeof (vGlobDefaultSearchFunction) == "undefined" || vGlobDefaultSearchFunction == "") {
        vGlobDefaultSearchFunction = "blockview";
    }

    UpdateSessionAndShowFormular(vGlobDefaultSearchFunction);
}

// Setzt die Anzahl der Datensätze pro Seite auf einen bestimmten Wert
function UpdateNumRecordsPerPage(numrecs) {
   
    if (isNaN(numrecs) == true && numrecs != null && numrecs != "") {
        alert("INTERNAL ERROR!");
        return;
    }

    // Wert des hidden fields setzen
    var vNumRecVar = document.getElementById("DsDisplayStateSelectedNumResultsPerPage");
    if (vNumRecVar != null) {
        vNumRecVar.value = numrecs;
    }


    var vPageRange = document.getElementById("DsDisplayStateSelectedPageRange");
    if (vPageRange != null) {
        vPageRange.value = "1-" + numrecs;
    }


    // Style des entsprechenden "Auswahlfeldes" setzen.
    var vLoopStartVal = 12;
    var vFirstRecsPageEle = document.getElementById("_recs_page_ele_" + vLoopStartVal);

    while (vFirstRecsPageEle != null) {

        if (vLoopStartVal == numrecs) {
            vFirstRecsPageEle.className = "recsperpagelink_selection";
        } else {
            vFirstRecsPageEle.className = "recsperpagelink_noselection";
        }

        if (vLoopStartVal > 50) {
            return; // Nur zur Sicherheit damit kein endlos Loop zusammenkommt
        }

        vLoopStartVal += 12;
        vFirstRecsPageEle = document.getElementById("_recs_page_ele_" + vLoopStartVal);
    }


    var vSelectedDisplayFunction = document.getElementById("DsDisplayStateDisplayFunction");
    if (vSelectedDisplayFunction != null) {
        DoSearch();
    }
}


function RemoveAllTempObjects() {
    if (pwbox != null) pwbox.Hide();
}

// ShowHideGoogleMapSymbols
function ShowHideGoogleMapSymbols(chkfcturl, countryfldid) {
	
    // DealerSearchAdvancedController -> DisplayCheckGoogleMapsSymbolsDisplay
	$.get(chkfcturl + "?CountryId=" + document.getElementById(countryfldid).value, function(data) {
		
		var visstyle = "hidden";
		var dispstyle = "none";

		if (data == '1') {
			visstyle = "visible";
			dispstyle = "inline";
		} else {

			if (vGlobDefaultSearchFunction == "mapview") {

				var vStatFctEle = document.getElementById("DsDisplayStateDisplayFunction");
				vGlobDefaultSearchFunction = "blockview";
				if (vStatFctEle != null) {
					vStatFctEle.value = "blockview";
				}
			}
		}

		var v_dealersearch_filter_worldmap = document.getElementById("_dealersearch_filter_worldmap");
		if (v_dealersearch_filter_worldmap != null) {
			v_dealersearch_filter_worldmap.style.visibility = visstyle;
		}

		var _dealersearch_filter_displaytype_map = document.getElementById("_dealersearch_filter_displaytype_map");
		if (_dealersearch_filter_displaytype_map != null) {
			_dealersearch_filter_displaytype_map.style.display = dispstyle;
		}

		var _dealerdetail_map_view = document.getElementById("_dealerdetail_map_view");
		if (_dealerdetail_map_view != null) {
			_dealerdetail_map_view.style.visibility = visstyle;
		}
	}
    );
}

function StartOverwViewCompleted() {

    RemoveAllTempObjects();

    ClearAllFilterFields();
    UpdateAllFilterFields();

    var vDisp = document.getElementById("DsDisplayStateDisplayFunction");
    if (vDisp != null) {
        vDisp.value = "";
    }

    var vSubButton = document.getElementById("btn_SessionUpdater");
    if (vSubButton != null) {
        vSubButton.click();
    }

    ShowHideGoogleMapSymbols(mapsdisplaycheckurl, mapsdisplaycheckid);
    
}

function BlockOverwViewCompleted() {
    RemoveAllTempObjects();
    deselectOverviewCategory();

    ShowHideGoogleMapSymbols(mapsdisplaycheckurl, mapsdisplaycheckid);
    
}

function ListOverwViewCompleted() {
    RemoveAllTempObjects();
    deselectOverviewCategory();

    ShowHideGoogleMapSymbols(mapsdisplaycheckurl, mapsdisplaycheckid);
    
}

function GoogleMapsOverwViewCompleted() {

	
    RemoveAllTempObjects();
    deselectOverviewCategory();

    doGoogleMapHandling("googlemap");
}



// Aktualisiert das Kategorie DropDown nach auswahl einer Kategorie
// und startet die Suche
function RefresDropDownCategoryAnStartSearch(catId) {
    //alert("test");
    var vSelectedRange = document.getElementById("DsDisplayStateSelectedPageRange");
    if (vSelectedRange != null) {

        vSelectedRange.value = "";
    }
    
    setSelectedCategoryLink(catId);
    
    var vCatEle = document.getElementById("SearchKategory");
    if (vCatEle != null) {

        for (o = 0; o < vCatEle.options.length; ++o) {

            if (vCatEle.options[o].value == catId) {
                vCatEle.selectedIndex = o;
                return;
            }
        }

        vCatEle.selectedIndex = o;
    }
}


// Setzt die anzuzeigende Seite
function SetSelectedPageRange(range) {

    //alert(range);
    var vPageRange = document.getElementById("DsDisplayStateSelectedPageRange");
    if (vPageRange != null) {
        vPageRange.value = range;
    }

    DoSearch();
}

function ClearAllFilterFields() {
  
    // Suchfeld
    var vKeyWordsEle = document.getElementById("SearchKey");
    if (vKeyWordsEle != null) {
        vKeyWordsEle.value = "";
    }

    // Location FIlter
    var vLocationEle = document.getElementById("SearchLocation");

    if (vLocationEle != null) {
        vLocationEle.value = "";
    }

    // Kategorie
    var vCatEle = document.getElementById("SearchKategory");
    if (vCatEle != null) {
        vCatEle.value = "";
        vCatEle.selectedIndex = 0;
    }

    // Cashback Varianten
    var vCurrentId = 2;
    var varVariantField = document.getElementById("chkVariant" + vCurrentId);

    while (varVariantField != null) {

        if (varVariantField.checked == true) {
            varVariantField.checked = false;
        }

        vCurrentId = vCurrentId * 2;
        varVariantField = document.getElementById("chkVariant" + vCurrentId);
    }

    setDsDisplaySpecialSearchFilterActionToNull();

    fillAllInfoTexts();
}

// Aktualisiert alle Filterfelder im zu sendenden Formular für Ajax
function UpdateAllFilterFields() {

    // Such - Keywörter
    var vKeyWordsEle = document.getElementById("SearchKey");
    var vKeyWordsSubmEle = document.getElementById("DsFilterKeywords");

    if (vKeyWordsEle != null && vKeyWordsSubmEle != null) {
        vKeyWordsSubmEle.value = vKeyWordsEle.value;
    }

    // Location FIlter
    var vLocationEle = document.getElementById("SearchLocation");
    var vLocationSubmEle = document.getElementById("DsFilterSearchLocation");

    if (vLocationEle != null && vLocationSubmEle != null) {
        vLocationSubmEle.value = vLocationEle.value;
    }

    // Kategoriefilter
    var vCatEle = document.getElementById("SearchKategory");
    var vCatSubmEle = document.getElementById("DsFilterSearchKategory");
    var vCatNameSubmEle = document.getElementById("DsFilterSearchKategoryName");

    if (vCatEle != null && vCatSubmEle != null && vCatNameSubmEle != null) {
        
        vCatSubmEle.value = vCatEle.value;

        if (vCatEle.selectedIndex >= 0) {
            vCatNameSubmEle.value = vCatEle.options[vCatEle.selectedIndex].text;
        } else {
            vCatNameSubmEle.value = vCatEle.options[0].text;
        }
    }

    // Landfilter
    var vCountryEle = document.getElementById("SearchCountry");
    var vCountrySubEle = document.getElementById("DsFilterSearchCountry");
    if (vCountryEle != null && vCountrySubEle != null) {
        vCountrySubEle.value = vCountryEle.value;
    }

    //alert(vCountryEle.value);
    // BundeslandFilter
    var vSearchState = document.getElementById("SearchState");
    var vSearchStateSubEle = document.getElementById("DsFilterSearchState");

    if (vSearchState != null && vSearchStateSubEle != null) {
        vSearchStateSubEle.value = vSearchState.value;
    }
    
    // BezirkFilter
    var vSearchArea = document.getElementById("SearchArea");
    var vSearchAreaSubEle = document.getElementById("DsFilterSearchArea");

    if (vSearchArea != null && vSearchAreaSubEle != null) {
        vSearchAreaSubEle.value = vSearchArea.value;
    }
    

    // Cashback Varianten
    var vCurrentId = 2;
    var vOrSum = 0;
    var maxCount = 15;
    var cnt = 0;
    var varVariantField = document.getElementById("chkVariant" + vCurrentId);
    var DsFilterChkCasbackVariante = document.getElementById("DsFilterChkCasbackVariante");

    if (DsFilterChkCasbackVariante != null) {
    	
    	while (cnt < maxCount) {
    		cnt++;
    		if (varVariantField != null && varVariantField.checked == true) {
                vOrSum = vOrSum | vCurrentId;
            }

            vCurrentId = vCurrentId * 2;
            varVariantField = document.getElementById("chkVariant" + vCurrentId);
            
        }

        DsFilterChkCasbackVariante.value = vOrSum;
    }
   
   
    // DsDisplayStateDisplayFunction
    var dispfctField = document.getElementById("DsDisplayStateDisplayFunction");
    if (dispfctField != null) {
        dispfctField.value = vGlobDefaultSearchFunction;
    }
    
}

// Kategorie wird über das DropDown geändert
function CategoryDDChanged() {

    var vCatEle = document.getElementById("SearchKategory");
    if (vCatEle != null) {
        setSelectedCategoryLink(vCatEle.value);
    }
}

function setSelectedCategoryLinkStartup() {
    
    var vCatEle = document.getElementById("SearchKategory");
    if (vCatEle != null) {
        setSelectedCategoryLink(vCatEle.value);
    }
}

function deselectOverviewCategory() {

    var vOverviewCat = document.getElementById("categorylink_overview");
    if (vOverviewCat != null) {
        vOverviewCat.className = "cashbackcategorylink";
    }
}


// Setzt den Style der gerade aktuell ausgewählten Kategorie
function setSelectedCategoryLink(idToSelect) {

    // Alte Markierung löschen
    if (vCurrentSelectedIdCategory != null && vCurrentSelectedIdCategory != "") {
        var vCurrentSelected = document.getElementById("categorylink_" + vCurrentSelectedIdCategory);
        if (vCurrentSelected != null) {
            vCurrentSelected.className = "cashbackcategorylink";
        }
    }

    // Neue Markierung setzen
    var vNewSelection = document.getElementById("categorylink_" + idToSelect);
    if (vNewSelection != null) {
        vNewSelection.className = "cashbackcategorylinkbold";
    }

    var vSelectedRange = document.getElementById("DsDisplayStateSelectedPageRange");
    if (vSelectedRange != null) {
        
        vSelectedRange.value = "";
    }

    vCurrentSelectedIdCategory = idToSelect;

}


// Hakt eine checkbox an wenn Sie nicht angehakt ist,
// ansonnsten wird sie deselektiert.
function SelectDeSelectCheckbox(idele) {

    var vEle = document.getElementById(idele);
    if (vEle != null) {

        if (vEle.checked == true) {
            vEle.checked = false;
        } else {
            vEle.checked = true;
        }
    }
}

function fillAllInfoTexts() {
    fillSearchKeyFieldInfoText();
    fillSearchKeyFieldLocation();
}

// Befüllt das Feld mit den Suchbegriffen mit einen Info (Hilfe) Text
function fillSearchKeyFieldInfoText() {
    
    var vKeyWordsEle = document.getElementById("SearchKey");
    if (vKeyWordsEle != null) {
        if (vKeyWordsEle.value == null || vKeyWordsEle.value == "") { vKeyWordsEle.value = vSearchKeyFieldInfoText; }
    }
}

// Leert das Feld mit den Suchbegriffen mit einen Info (Hilfe) Text
function clearSearchKeyFieldInfoText() {
    var vKeyWordsEle = document.getElementById("SearchKey");
    if (vKeyWordsEle != null) {
        if (vKeyWordsEle.value == vSearchKeyFieldInfoText) {
            vKeyWordsEle.value = "";
        }
    }
}



// Befüllt das Feld mit den SearchLocation mit einen Info (Hilfe) Text
function fillSearchKeyFieldLocation() {

    var vKeyWordsEle = document.getElementById("SearchLocation");
    if (vKeyWordsEle != null) {
        if (vKeyWordsEle.value == null || vKeyWordsEle.value == "") { vKeyWordsEle.value = vLocationKeyFieldInfoText; }
    }
}

// Leert das Feld mit den SearchLocation mit einen Info (Hilfe) Text
function clearSearchKeyFieldLocation() {
    var vKeyWordsEle = document.getElementById("SearchLocation");
    if (vKeyWordsEle != null) {
        if (vKeyWordsEle.value == vLocationKeyFieldInfoText) {
            vKeyWordsEle.value = "";
        }
    }
}


//DsDisplaySpecialSearchFilterAction
function setDsDisplaySpecialSearchFilterAction(action) {

    var vEle = document.getElementById("DsDisplaySpecialSearchFilterAction");
    if (vEle != null) {
        vEle.value = action;
    }
}

//DsDisplaySpecialSearchFilterAction
function setDsDisplaySpecialSearchFilterActionToNull() {

    var vEle = document.getElementById("DsDisplaySpecialSearchFilterAction");
    if (vEle != null) {
        vEle.value = "";
    }

    var vCatEle = document.getElementById("SearchKategory");
    if (vCatEle != null) {
        setSelectedCategoryLink(vCatEle.value);
    }
}

function clearBlAndStateDropDown() {

    var vSearchState = document.getElementById("SearchState");
    if (vSearchState != null) vSearchState.value = "";

    var vDsFilterSearchState = document.getElementById("DsFilterSearchState");
    if (vDsFilterSearchState != null) vDsFilterSearchState.value = "";

    var vSearchArea = document.getElementById("SearchArea");
    if (vSearchArea != null) vSearchArea.value = "";

    var vDsFilterSearchArea = document.getElementById("DsFilterSearchArea");
    if (vDsFilterSearchArea != null) vDsFilterSearchArea.value = "";

    return true;
}

// Befüllt das DropDown mit den Bundesländern
function fillBlDropDownBySelectedCountry(ValueSet) {
	
    var vCountry = document.getElementById("SearchCountry");
    var vSearchState = document.getElementById("SearchState");
    var vSearchArea = document.getElementById("SearchArea");

    var FrmStateState = document.getElementById("FrmStateState");
    var FrmStateCountryID = document.getElementById("FrmStateCountryID");
    
    var vSubmitButton = document.getElementById("btn_fillBlDropDownByCountry");


    if (vCountry != null && FrmStateCountryID != null && vSubmitButton != null) {

        if (vSearchState != null) {
            vSearchState.disabled = true;
        }
        if (vCountry != null) {
            vCountry.disabled = true;
        }
        if (vSearchArea != null) {
            vSearchArea.disabled = true;
        }

        if (ValueSet) {
        	FrmStateCountryID.value = vCountry.value;
            FrmStateState.value = "";
            
           
        }
        vSubmitButton.click();
        return true;
    }

    return false;
}

function fillBlDropDownBySelectedCountry_finished() {

	
    var vCountry = document.getElementById("SearchCountry");
    var vSearchState = document.getElementById("SearchState");
    var vSearchArea = document.getElementById("SearchArea");
   
    if (vCountry  != null) {

        if (vCountry != null) {
            vCountry.disabled = false;
        }
        if (vSearchState != null) {
            vSearchState.disabled = false;
        }
        if (vSearchArea != null) {
            vSearchArea.disabled = false;
        }
    }
    //var x = Y;
    //alert(document.getElementById("bl_dropdown"))
}

// Befüllt das DropDown mit den Bundesländern
function fillAreaDropDownBySelectedState(ValueSet) {

	
    var vCountry = document.getElementById("SearchCountry");
    var vSearchState = document.getElementById("SearchState");
    var vSearchArea = document.getElementById("SearchArea");

    var FrmAreaCountryID = document.getElementById("FrmAreaCountryID");
    var FrmAreaState = document.getElementById("FrmAreaState");
    var FrmAreaArea = document.getElementById("FrmAreaArea");

    
    var vSubmitButton = document.getElementById("btn_fillAreaDropDownByState");
    
    if (vCountry != null && FrmAreaCountryID != null && vSubmitButton != null) {

        if (vSearchState != null) {
            vSearchState.disabled = true;
        }
        if (vCountry != null) {
            vCountry.disabled = true;
        }
        if (vSearchArea != null) {
            vSearchArea.disabled = true;
        }

        if (ValueSet) {
            FrmAreaCountryID.value = vCountry.value;

            if (vSearchState != null && FrmAreaState != null) {
                FrmAreaState.value = vSearchState.value;
            }
            FrmAreaArea.value = "";
        }
        
        vSubmitButton.click();

        return true;
    }


    return false;
}
function fillAreaDropDownBySelectedState_finished() {
	
    var vCountry = document.getElementById("SearchCountry");
    var vSearchState = document.getElementById("SearchState");
    var vSearchArea = document.getElementById("SearchArea");

    if (vCountry != null) {

        if (vCountry != null) {
            vCountry.disabled = false;
        }
        if (vSearchState != null) {
            vSearchState.disabled = false;
        }
        if (vSearchArea != null) {
            vSearchArea.disabled = false;
        }
    }

    //var x = Y;
     //alert(document.getElementById("area_dropdown").innerText);
   }
