
    //Javacript Template Checker für PiLinx/Apt
    //2009.05.07-08 programed by Ernst A.N. Raidl, Fa. Wolfgang Höfler
    //2009.05.12 revised by Alexander Hauf
    //    Fertigstellung Initialisierung; Korrektur von FeldNamen (Alignemt)
    //2009.05.12 revised by Ernst A.N. Raidl
    //    Set: Unterscheidung von TextBox und Checkbox
    //    Methode setFirstTemplate() und checkInitialization() für Initialisierung
    //VERSION 2009.05.12 11:36 (Storefront-List)
    //2009.06.?? Layout-Initialisierung geändert
    //2009.06.18 erweitertes Debugging
    //           sf-JS aufrufen (statt postback); siehe span über sf-Button "Update"
    //           2-Teilung in LayoutObject & sfEventHandler und Initializer
    
    //Storefront-Felder
    var FieldName_format = "Format";
    var FieldName_layout_quer = "Layouts_quer";
    var FieldName_layout_hoch = "Layouts_hoch";
    var FieldName_Status = "JS_TemplateStatus";
    
    //Konstanten
    var QUERFORMAT = "quer";
    var HOCHFORMAT = "hoch";
    var TEXTBOX = "TextBox";
    var CHECKBOX = "CheckBox";
            
    var debug = false;
    var registrationMode = false;
    var jsEventSet = false;

    //debugMethode
    function printControl(msg, aControl) {
        if (aControl != null) {
            alert(msg + " Control: id=" + aControl.id + ", name=" + aControl.name + ", type=" + aControl.type + ", value=" + aControl.value + ", text=" + aControl.text);
        }
    }
    
   //Konstruktor für LayoutTemplate
   function LayoutTemplate(Format, Layout, PageWidth, PageHeight, 
   NameFont, NameSize, NameBold, NameItalic, NameUnderline, NameAlignment, NameColor, NameTint, 
   Namecolor_C, Namecolor_M, Namecolor_Y, Namecolor_K,
   MyTextFont, MyTextSize, MyTextBold, MyTextItalic, MyTextUnderline, MyTextAlignment, MyTextColor, MyTextTint, 
   MyTextcolor_C, MyTextcolor_M, MyTextcolor_Y, MyTextcolor_K)
   {
      this.Format = Format;
      this.Layout = Layout;
      this.PageWidth = PageWidth;
      this.PageHeight = PageHeight;
      this.NameFont = NameFont;
      this.NameSize = NameSize;
      this.NameBold = NameBold;
      this.NameItalic = NameItalic;
      this.NameUnderline = NameUnderline;
      this.NameAlignment = NameAlignment;
      this.NameColor = NameColor;
      this.NameTint = NameTint;
      this.Namecolor_C = Namecolor_C;
      this.Namecolor_M = Namecolor_M;
      this.Namecolor_Y = Namecolor_Y;
      this.Namecolor_K = Namecolor_K;
      this.MyTextFont = MyTextFont;
      this.MyTextSize = MyTextSize;
      this.MytextBold = MyTextBold;
      this.MyTextItalic = MyTextItalic;
      this.MyTextUnderline = MyTextUnderline;
      this.MyTextAlignment = MyTextAlignment;
      this.MyTextColor = MyTextColor;
      this.MyTextTint = MyTextTint;
      this.MyTextcolor_C = MyTextcolor_C;
      this.MyTextcolor_M = MyTextcolor_M;
      this.MyTextcolor_Y = MyTextcolor_Y;
      this.MyTextcolor_K = MyTextcolor_K;   
      this.setValues = doSetValues;
      this.printKey = doPrintKey;
   }
   
   //Objekt-Methode zum Übertragen der Werte in die Storefront-Felder
   function doSetValues()
   {
          setTemplateValue('_sys_PageWidth', this.PageWidth);
		  setTemplateValue('_sys_PageHeight', this.PageHeight);
		  setTemplateValue('NameFont', this.NameFont);
		  setTemplateValue('NameSize', this.NameSize);
		  setTemplateValue2('NameBold', CHECKBOX, this.NameBold);
		  setTemplateValue('NameSize', this.NameSize);
		  setTemplateValue2('NameItalic', CHECKBOX, this.NameItalic);
		  setTemplateValue2('NameUnderline', CHECKBOX, this.NameUnderline);
		  setTemplateValue('NameAlignment', this.NameAlignment);
		  setTemplateValue('NameColor', this.NameColor);
		  setTemplateValue('NameTint', this.NameTint);
		  setTemplateValue('Namecolor_C', this.Namecolor_C);
		  setTemplateValue('Namecolor_M', this.Namecolor_M);
		  setTemplateValue('Namecolor_Y', this.Namecolor_Y);
		  setTemplateValue('Namecolor_K', this.Namecolor_K);
		  setTemplateValue('MyTextFont', this.MyTextFont);
		  setTemplateValue('MyTextSize', this.MyTextSize);
		  setTemplateValue2('MyTextBold', CHECKBOX, this.MyTextBold);
		  setTemplateValue2('MyTextItalic', CHECKBOX, this.MyTextItalic);
		  setTemplateValue2('MyTextUnderline', CHECKBOX, this.MyTextUnderline);
		  setTemplateValue('MyTextAlignment', this.MyTextAlignment);
		  setTemplateValue('MyTextColor', this.MyTextColor);
		  setTemplateValue('MyTextTint', this.MyTextTint);
		  setTemplateValue('MyTextcolor_C', this.MyTextcolor_C);
		  setTemplateValue('MyTextcolor_M', this.MyTextcolor_M);
		  setTemplateValue('MyTextcolor_Yt', this.MyTextcolor_Y);
		  setTemplateValue('MyTextcolor_K', this.MyTextcolor_K);
   }
   
   //Objekt-Debug-Methode
   function doPrintKey()
   {
        alert("Format="+this.Format+",Layout="+this.Layout);
   }
   
   //Variable zur Speicherung der Templates
   var myTemplates = new Array(); 

    
    //Methode zum Setzen der ersten Vorlage (beim ersten Lade-Vorgang)
    //Achtung: darf nur vom "Schluss" der Seite aufgerufen werden, da sonst die Felder noch nicht registriert sind!
    function setFirstTemplate()
    {
       registrationMode = true; //event-Behandlung verhindern
       setTemplateValue(FieldName_format, QUERFORMAT);
       setTemplateValue(FieldName_layout_quer, "layout04");
       setTemplateValue(FieldName_Status,"initialized"); //Initialisierung anzeigen
       registrationMode = false;
       setTemplate(true); //Update auslösen
    }
    
    //überprüfen, ob bereits eine Vorlage ausgewählt wurde; wenn nicht, die erste Setzen
    function checkInitialization()
    {
        var initFieldValue = getTemplateValue(FieldName_Status);
        if ((initFieldValue == null) || (initFieldValue == ""))
        {
            if (debug) alert("initializing form ...");
            setFirstTemplate();
        }
        else
        {
            if (debug) alert("bereits initialisiert: Status="+initFieldValue);
        }
    }

    //einen Wert aus einem Storefront-Feld lesen
    function getTemplateValue(sfFieldName) 
    {   var fieldValue;
    
        fieldValue = null;
        if ((sfFieldName != null) && (sfFieldName != "")) 
        {
            searchFieldName = "FIELD_" + FieldIDs[sfFieldName];
            var control2 = document.getElementById(searchFieldName);
            if (control2 != null) {
                   fieldValue = control2.value;
            }
        }
        return fieldValue;
    }

    //einen neuen Wert in ein Storefront-Feld übernehmen (TextBox)
    function setTemplateValue(sfFieldName, newFieldValue) 
    {
        setTemplateValue2(sfFieldName, TEXTBOX, newFieldValue);
    }
    
     //einen neuen Wert in ein Storefront-Feld übernehmen (Typ-abhängig)
    function setTemplateValue2(sfFieldName, fieldType, newFieldValue) 
    {
        var searchFieldName;
        if ((sfFieldName != null) && (sfFieldName != ""))
        {
            if (newFieldValue == null)
               newFieldValue = ""; //Konvertierung null -> Leerstring
            searchFieldName = "FIELD_" + FieldIDs[sfFieldName];
            var control2 = document.getElementById(searchFieldName);
            if (control2 != null) 
            {
                if (fieldType == TEXTBOX)
                     control2.value = newFieldValue;
                if ((fieldType == CHECKBOX) && (newFieldValue != null))
                     control2.checked = (newFieldValue.toLowerCase() == "true");
            }
        }
    }
   
   //nach einem Template suchen (Key: Format, Layout)
   function searchTemplate()
   {
      var foundTemplate = null;
      var aTemplate;
      var currentLayout = null;
      var currentFormat = null;
      
      //aktuelle Werte für Format und Layout holen.
      if (myTemplates.length == 0)
      {
        alert("keine Vorlagen vorhanden!");
        return null;
      } 
      currentFormat = getTemplateValue(FieldName_format);
      if (currentFormat == QUERFORMAT)
      {
            currentLayout = getTemplateValue(FieldName_layout_quer);
      }
      else
      {
            currentLayout = getTemplateValue(FieldName_layout_hoch);
      }
      
      if ((currentFormat != null) && 
          (currentLayout != null))
      {   //passendes Template suchen
            
            if (debug) alert("suche Vorlage mit Format="+currentFormat+", Layout="+currentLayout);
            for (var i=0; i < myTemplates.length; i++) 
            {
                aTemplate = myTemplates[i];
                if (debug) 
                    aTemplate.printKey();
                if ((aTemplate.Format == currentFormat) &&
                   (aTemplate.Layout == currentLayout))
                {
                    foundTemplate = aTemplate;
                    if (debug) alert("template found!");
                    break;
                }
            }
      }
      if (foundTemplate == null)
      {
        if (debug) alert("Vorlage für Format ("+currentFormat+"), Layout ("+currentLayout+") nicht gefunden!");
      }
      return foundTemplate;
   }
  
  
    
    //eine Vorlage suchen und die Werte übernehmen
    function setTemplate(update)
    {
	  var myTemp;
	  var postB = false;
	  if (!registrationMode)
      {
	      myTemp = searchTemplate(); //das richtige Template suchen;
          if (myTemp != null)
	      {   //Template gefunden
              myTemp.setValues(); //die Werte übertragen
              //im nicht-registrationMode postBack auslösen
              postB = true;
              
              //NEU: statt postback, AjaxUpdateForm verwenden
              //onclick-Handler von span über update-Button
              PFSF_ShowErrors=true; 
              if (typeof StorefrontValidatorHook!= typeof StorefrontUndefined_283981501AD46548 && !StorefrontValidatorHook()) 
              {
                  //return false; 
              }
              else
              {
                  PFSF_AjaxUpdateForm('showErrors=t&'); 
                  //return false
              }
              
              //ALT:
              // if (debug) alert("calling postback ...");
              // __doPostBack('StepArea$btnUpdate',''); //Update auslösen;
	      }
	      
	      if (debug) alert("calling PFSF_ShowHideConditionalFields ...");
	      PFSF_ShowHideConditionalFields(1); //die Felder aktualisieren (Ein-/Ausblendung)
	   }
    }
    
    function setOnChange_Format() {
       if (debug) alert("on change of format!");
       setTemplate(true); //Bei einer Format-Änderung muss die andere Layout-Liste eingeblendet werden
    }
    
    function setOnChange_Layout() {
        if (debug) alert("on change of layout!");
        setTemplate(false);
    }

    function setOnClick_Format() {
        if (debug) alert("on click of format!");
        setTemplate(false);
    }
    
    function setOnClick_Layout() {
        if (debug) alert("on click of layout");
        setTemplate(false);
    }
   
    //Das onchange-event bei den Storefront-Felder anmelden
    function registerEvent(calledBy) {
        var searchFieldName
        var searchFieldName2
        var searchFieldName3

        if (debug) alert("registerEvent BEGIN called by "+calledBy);

        if (!jsEventSet)
        {
            initialize();
            searchFieldName = "FIELD_" + FieldIDs[FieldName_format];
            searchFieldName2 = "FIELD_" + FieldIDs[FieldName_layout_hoch];
            searchFieldName3 = "FIELD_" + FieldIDs[FieldName_layout_quer];
            var control = document.getElementById(searchFieldName);
            var control2 = document.getElementById(searchFieldName2);
            var control3 = document.getElementById(searchFieldName3);
            if ((control != null) && (control2 != null) && (control3 != null))
             {
                //falls das Event unmittelbar ausgelöst werden sollte -> unterbinden
                registrationMode = true;
                control.onchange = setOnChange_Format;
                control2.onchange = setOnChange_Layout;
                control3.onchange = setOnChange_Layout;
                //control.onclick = setOnClick_Format;
                //control2.onclick = setOnClick_Layout;
                //control3.onclick = setOnClick_Layout;
                registrationMode = false;
                jsEventSet = true;
                
                if (debug)
                {   //Debugging
                    if (control.onchange != undefined)
                    {
                        alert("change of event: Control=" + searchFieldName + ", onchange=" + control.onchange);
                        alert("change of event: Control2=" + searchFieldName2 + ", onchange=" + control2.onchange);
                        alert("change of event: Control3=" + searchFieldName3 + ", onchange=" + control3.onchange);
                    }
                    else alert ("onchange undefined!");
//                    if (control.onclick != undefined)
//                    {
//                        alert("change of event: Control=" + searchFieldName + ", onclick=" + control.onclick);
//                        alert("change of event: Control2=" + searchFieldName2 + ", onclick=" + control2.onclick);
//                        alert("change of event: Control3=" + searchFieldName3 + ", onclick=" + control3.onclick);
//                    }
//                    else alert ("onclick undefined!");
                }
            }
            else //bestimmte Felder nicht gefunden!
            {
                if (control == null)
                    alert("Control mit ID " + searchFieldName + " nicht gefunden!");
                if (control2 == null)
                    alert("Control mit ID " + searchFieldName2 + " nicht gefunden!");
                if (control3 == null)
                    alert("Control mit ID " + searchFieldName3 + " nicht gefunden!");
            }
        } 
        if (debug) alert("registerEvent END");
    }

