﻿/**
 * Konstruktor
 */
var OptionenLayer = new function() {
    this.timerId = null;
    var isResetting = false;

    this.GetIsResetting = function() { return isResetting; }
    this.SetIsRestting = function(value) { isResetting = value; };
}

/******************************************************************************
***                              Hauptfunktionen                            ***
******************************************************************************/

/**
 * Zeigt die Detailssuche an bzw. blendet sie aus.
 * @param boolean closeQueryBuilder Gibt an ob der QueryBuilder (Profisuche) geschlossen werden soll.
 */
OptionenLayer.toggleOptionenlayer = function(closeQueryBuilder) {
    var optionenlayer = $get('optionenlayer');

    if (optionenlayer == null || optionenlayer.childNodes.length == 0) {
        LoadXml('1', null, "collectFromSession=true&details=on", null, null, true, OptionenLayer.onLoadedXml);
    } else {
        ToggleElement('optionenlayer');

        if (optionenlayer != null) {
            if (optionenlayer.style.display == 'none')
                optionenlayer.style.display = 'block';

            if (optionenlayer.style.visibility == 'visible')
                AddHiddenField('details', 'on');
            else
                AddHiddenField('details', '');
        }
    }
    if (closeQueryBuilder)
        QueryBuilder.close();
}

/**
 * Setzt die Suche komplett zurück
 */
OptionenLayer.clearSearch = function() {
    var wordsInput = $get('words');
    if (wordsInput != null) {
        wordsInput.value = '';
    }

    this.reset();
}

OptionenLayer.activatechkdoktyp = function() {
    var chkDokType = $get('chkdoktyp');
    if (chkDokType == null)
        return;

    if (chkDokType.checked == true) {
        document.getElementById("chkkomm").checked = false;
        document.getElementById("chkrspr").checked = false;
        document.getElementById("chkaufs").checked = false;
        document.getElementById("chkges").checked = false;
        document.getElementById("chkform").checked = false;
        document.getElementById("chkMeld").checked = false;
        document.getElementById("chkVerw").checked = false;
        document.getElementById("chklexika").checked = false;
        document.getElementById("txtopus").value = '';
    }
}

OptionenLayer.deactivatechkdoktyp = function() {
    document.getElementById("chkdoktyp").checked = false;
}

/******************************************************************************
***                          Funktionen der Buttons                         ***
******************************************************************************/

/**
 * Der Hilfe Button
 */
OptionenLayer.showHelp = function() {
    ShowHelp('SearchFormControl');
}

/**
* Der Reset Button
*/
OptionenLayer.reset = function() {
    this.SetIsRestting(true);
    var optionenlayer = $get('optionenlayer');
    var inputlist = optionenlayer.getElementsByTagName("input");

    for (i = 0; i < inputlist.length; i++) {
        if (inputlist[i].type == 'text')
            inputlist[i].value = '';
        else if (inputlist[i].type == 'checkbox')
            inputlist[i].checked = false;
    }

    if ($get('chkdoktyp') != null)
        $get('chkdoktyp').checked = true;

    if ($get('txtrgebiet') != null)
        $get('txtrgebiet').value = 'Rechtsgebiete';

    if ($get('txtopus') != null)
        $get('txtopus').value = 'Publikationen';

    OptionenLayer.activatechkdoktyp();
    OptionenLayer.refresh(0);
}

/**
 * Der Schließen Button
 */
OptionenLayer.close = function() {
    entferneAktuelleSuchOption();
    var optionenlayer = $get('optionenlayer');

    if (optionenlayer == null || optionenlayer.style.visibility != 'visible') {
        return;
    }
    this.toggleOptionenlayer(false);
}

/******************************************************************************
***                              Hilfsmethoden                              ***
******************************************************************************/

/**
 * Zeigt das Control erstmalig an
 */
OptionenLayer.init = function() {    
    this.toggleOptionenlayer(true);
    markiereAktuelleSuchOption(0);
}

/**
 * Aktualisiert die Liste
 * @param integer time Wird eine Zeitspanne angegeben, so wird das Laden der Liste verzögert
 */
OptionenLayer.refresh = function(time) {
    if (this.timerId != null) {
        clearTimeout(this.timerId);
        this.timerId = null;
    }

    if (typeof time != "undefined") {
        this.timerId = LoadXmlWithTimer('1', null, null, null, null, true, OptionenLayer.onLoadedXml, time);
    } else {
        LoadXml('1', null, null, null, null, true, OptionenLayer.onLoadedXml);
    }
}

/**
 * Wird nach dem fertig ausgeführten Ajax-Request ausgeführt
 */
OptionenLayer.onLoadedXml = function(executor, eventArgs) {
    OnLoadedXml(executor, eventArgs);
    if (OptionenLayer.GetIsResetting() == true) {
        SetFocus('words');
        OptionenLayer.SetIsRestting(false);
    }
    // Textboxen auf enabled setzen
    for (i = 0; i < document.forms[0].elements.length; i++) {
        if (document.forms[0].elements[i].type == 'text' && document.forms[0].elements[i].value != '') {
            document.forms[0].elements[i].disabled = false;
        }
    }
}