﻿/**
 * Konstruktor
 */
var NormControl = new function() {
    this.timerId = null;
}

/** Das JSON-Objekt */
NormControl.json = null;

/******************************************************************************
***                              Hauptfunktionen                            ***
******************************************************************************/

/**
* Fügt eine Norm der Auswahl hinzu bzw. entfernt es wieder daraus
* @param string value Die ausgewählte Nor,
* @param boolean checked Gibt an, ob die Norm selektiert oder deselektiert wurde
*/
NormControl.addItem = function(value, checked) {
    if (checked == true) {
        if (!Array.contains(this.json.Values, value)) {
            Array.enqueue(this.json.Values, value);
        }
    }
    else {
        if (Array.contains(this.json.Values, value)) {
            Array.remove(this.json.Values, value);
        }
    }
}

/******************************************************************************
***                          Funktionen der Buttons                         ***
******************************************************************************/

/**
 * Der Hilfe Button
 */
NormControl.showHelp = function() {
    ShowHelp('normencontrol');
}

/**
* Der Reset Button
*/
NormControl.reset = function() {
    if ($get('txtnorm') != null) {
        $get('txtnorm').value = "";
    }

    this.resetJson();
    this.refresh(undefined);
}

/**
 * Der Schließen Button
 */
NormControl.close = function() {
    RemoveAllChilds('NormControl');
    this.resetJson();
}

/**
 * Der Übernehmen Button
 */
NormControl.uebernehmen = function() {
    if ($get('txtnorm') != null) {
        $get('txtnorm').value = SerializeArray(this.json.Values);
    }
    RemoveAllChilds('NormControl');
    this.resetJson();
}

/******************************************************************************
***                              Hilfsmethoden                              ***
******************************************************************************/

/**
 * Zeigt das Control erstmalig an
 */
NormControl.init = function() {
    this.refresh(undefined);
}

/**
 * Aktualisiert die Liste
 * @param integer time Wird eine Zeitspanne angegeben, so wird das Laden der Liste verzögert
*/
NormControl.refresh = function(time) {
    if (this.timerId == null) {
        clearTimeout(this.timerId);
        this.timerId = null;
    }

    if (typeof time != "undefined") {
        this.timerId = LoadXmlWithTimer('4', null, null, null, this.json, true, NormControl.onLoadedXml, time);
    } else {
        LoadXml('4', null, null, null, this.json, true, NormControl.onLoadedXml);
    }
}

/**
 * Setzt das JSON-Objekt zurück
 */
NormControl.resetJson = function() {
    this.json = null;
}

/**
 * Wird nach dem fertig ausgeführten Ajax-Request ausgeführt
 */
NormControl.onLoadedXml = function(executor, eventArgs) {
    this.timerId = null;
    OnLoadedXml(executor, eventArgs);
    if ($get('json') != null) {
        try {
            var value = $get('json').value;
            value = unescape(value);
            NormControl.json = Sys.Serialization.JavaScriptSerializer.deserialize(value);
        }
        catch (e) {
            alert(e.message);
        }
    }
    SetFocus('txtnormsearch');
}