/**
 * JavaScripte für die DND-Warenkörbe
 */
var isIE = (navigator.appName.indexOf("Microsoft")!=-1)?true:false;
var consolenBreak;

function wk_list()
{
    // ID des Ziels
    this.target             = '';
    this.wId                = 0;
 
    // Merker ob gerade eine Ajax-Abfrage läuft 
    this.running            = false;
    // Ajax-Verbindung
    this.conn               = null;
    // Userdefiniert Parameter, welche beim Abfragen der Suchvorschläge 
    // mitgesendet werden
    // Diese Werte lassen sich mit der Funktion addParam(name, wert)
    // hinzufügen
    this.addParamStr        = '';
    this.addParamArr        = new Object();      
    this.ajaxScript         = 'ajax_warenkorb.php';
    this.sessId             = '';
    this.sessNr             = '';
    this.returned           = '';


    this.getWarenkorb = function(target, wId)
    {
        this.viewLoading('Der Warenkorb wird geladen');
        this.target = target;
        this.wId    = wId;
        this.addParam('func', 'getWarenkorb');
        this.addParam('afterfunc', 'wkList.setWarenkorb();');
        this.addParam('target', target);
        this.addParam('wId', wId);
        this.sendAjax();
    };


    this.addSessId = function(val)
    {
        this.sessId = val;
//         window.setInterval("// console.log('"+getElem(dZones1[4]).style.zIndex+"')", 800);
    };

    
    this.addSessNr = function(val)
    {
        this.sessNr = val;
    };
    
    
    this.setWarenkorb = function()
    {
        getElem(this.target).innerHTML = this.returned;
        
        if(this.wId == 1)
            this.getWarenkorb('warenkorb2', '2');
        
        this.getDndAreasZones();
    };

    
    this.getDndAreasZones = function()
    {
        nodes1  = getElem('warenkorb1').childNodes;
        nodes2  = getElem('warenkorb2').childNodes;
        
        dZones1 = new Array();
        dZones2 = new Array();
        x       = 0;
        
        dndDelete();
        
        // Register DropZones
        if(nodes1 && getElem('warenkorb1').getAttribute('separat') != 'true')
        {
            for(i=0; i!=nodes1.length; i++)
            {
                if(nodes1[i].id.substring(0, 7) != 'subgrp_')
                    continue;
                
                dZones2[x] = nodes1[i].id; 
                x++;
            }
        }
        // console.log(dZones2.length+' DropZones in Warenkorb 1 gefunden');
         
        if(nodes2 && getElem('warenkorb2').getAttribute('separat') != 'true')
        {
            for(i=0; i!=nodes2.length; i++)
            {
                if(nodes2[i].id.substring(0, 7) != 'subgrp_')
                    continue;
                
                dZones2[x] = nodes2[i].id; 
                x++;
            }
        }
        // console.log(dZones2.length+' DropZones in Warenkorb 1 & 2 gefunden');

        x = 0;
        // Register DragElements
        for(i=0; i!=dZones2.length; i++)
        {
            if(!getElem(dZones2[i]))
                continue;
            
            nodes = getElem(dZones2[i]).childNodes;
            
            for(a=0; a!=nodes.length; a++)
            {
                if(!getElem(nodes[a]) || getElem(nodes[a]).id.substring(0, 7) != 'artikel')
                    continue;

                dZones1[x] = getElem(nodes[a]).id;
                x++;
            }
        } 
        // console.log(dZones1.length+' DragElemente in Warenkorb 1 & 2 gefunden');
        dndInit();
    };
    
    
    
    this.changeName = function(id, grpId)
    {
        if(getElem('editIcon_'+id).src.indexOf('pencil') > 0)
        {
            // console.log('Eingabefeld einblenden');        
            var grpName     = getElem('grpName_'+id).getAttribute('grpName');
            var grpNewName  = grpName;
            
            var inp = '<input type="text" name="grpEdit_'+id+'" ';
            inp += 'value="'+grpNewName+'" id="grpEdit_'+id+'" maxlength="20" '; 
            inp += 'grpOldName="'+grpName+'" class="grpEdit" />';
            
            getElem('grpName_'+id).innerHTML = inp;
            getElem('grpEdit_'+id).focus();
            getElem('editIcon_'+id).src = 'images/warenkorb/icons/tick.png';
            getElem('editIconAbbruch_'+id).style.display = 'block';
        }
        else
        {
            this.viewLoading('Die Gruppe wird umbenannt');
            // console.log('Gruppe soll in "'+getElem('grpEdit_'+id).value+'" umbenannt werden');
            this.addParam('func', 'renameGroup');
            this.addParam('afterfunc', 'wkList.renameGroup();');
            this.addParam('grpName', getElem('grpEdit_'+id).value);
            this.addParam('grpId', grpId);
            this.addParam('grpHtmlId', id);
            this.sendAjax();
        }
    };

    
    this.renameGroup = function()
    {
        tmp = this.returned.split('¤');
        if(tmp[1] == '')
        {
            alert("Die Gruppe konnte nicht umbenannt werden");
            // console.log("Die Gruppe konnte nicht umbenannt werden");
        }
        else if(tmp[1] == 'EXISTS')
        {
            alert("Es existiert bereits eine Gruppe mit dem eingegeben Namen");
            this.changeNameAbbruch(tmp[0]);
        }
        else
        {  
            getElem('grpName_'+tmp[0]).setAttribute('grpName', tmp[1]);
            getElem('grpName_'+tmp[0]).innerHTML     = tmp[1];
            // console.log("Die Gruppe wurde umbenannt");
        }
        getElem('editIcon_'+tmp[0]).src = 'images/warenkorb/icons/pencil.png';
        getElem('editIconAbbruch_'+tmp[0]).style.display = 'none';
    };

    
    this.changeNameAbbruch = function(id)
    {
        var grpName     = getElem('grpEdit_'+id).getAttribute('grpOldName');
        getElem('grpName_'+id).innerHTML                = grpName;
        getElem('editIcon_'+id).src = 'images/warenkorb/icons/pencil.png';
        getElem('editIconAbbruch_'+id).style.display    = 'none';
    };        
    
    
    this.renameWk = function(wk, id1, id2)
    {
        var newName = '';
        while(newName == '')
        {
            newName = prompt('Bitte geben Sie einen Namen für den Warenkorb an:');
            
            if(newName == null)
            {
                // console.log('Abbruch durch Benutzer');
                return;
            }
            newName = newName.trim();
        }
        // console.log('Neuer Name des Warenkorbes: '+newName);
        this.viewLoading('Der Warenkorb wird umbenannt');
        
        this.addParam('func', 'renameWk');
        this.addParam('afterfunc', 'wkList.setWkName();');
        this.addParam('wk', wk);

        if(wk == 1)
            this.addParam('wkId', id1);
        else
            this.addParam('wkId', id2);
            
        this.addParam('wkName', newName);
        this.sendAjax();
    };

    
    this.setWkName = function()
    {
        tmp = this.returned.split('¤');
        getElem('warenkorbHeader'+tmp[0]).childNodes[1].innerHTML = tmp[1];
    };

    
    this.scrollGroup = function(id)
    {
        var maxSize = 0;
            
        if(getElem('subgrp_'+id).getAttribute('oHeight') == '')
        {
            maxSize = getElem('subgrp_'+id).offsetHeight;
            if(maxSize < 1)
            {                    
                nodes = getElem('subgrp_'+id).childNodes;
                maxSize = nodes.length * 47;
            }
        }
        else
            maxSize = parseInt(getElem('subgrp_'+id).getAttribute('oHeight'));
             
        // console.log("Ermittelte Gruppenhöhe: "+maxSize);
        
        var minSize = 0;
        var speed   = 500;
        
        speed = Math.round(20000 / maxSize);
        
        getElem('subgrp_'+id).setAttribute('oHeight', maxSize);
    
        if(getElem('subgrp_'+id).style.display == 'none')
        {
            // console.log("Artikel in Gruppe einblenden");
        
            getElem('grpArrow_'+id).src = 'images/warenkorb/icons/minus.gif';
            scrollElement('subgrp_'+id, minSize, maxSize, speed, true);
            this.addParam('func', 'changeGroupDisplay');
            this.addParam('set', '1');
            this.addParam('grpId', getElem('grp_'+id).getAttribute('grpId'));
            this.sendAjax();
        }
        else
        {
            // console.log("Artikel in Gruppe ausblenden");
        
            getElem('grpArrow_'+id).src = 'images/warenkorb/icons/plus.gif';
            scrollElement('subgrp_'+id, maxSize, minSize, speed, true);
            this.addParam('func', 'changeGroupDisplay');
            this.addParam('set', '0');
            this.addParam('grpId', getElem('grp_'+id).getAttribute('grpId'));
            this.sendAjax();
        }
    };
    	
	
	this.artikelDetails = function(id)
	{
        // console.log('ArtikelDetails für den Artikel: '+id+' aufrufen');
        
        this.addParam('entryId', id);
        this.addParam('func', 'getArtikelDetails');
        this.addParam('afterfunc', 'wkList.viewArtikelDetails();');
        this.sendAjax();
    };

    
    this.activateWarenkorbGroup = function(wkId, grpId, wk1, wk2, userId, wkNr, el)
    {
        if(el && el.childNodes[0] && typeof(el.childNodes[0].tagName) == 'string')
            return;
        
        this.viewLoading('Die Gruppe wird aktiviert');
        
        this.addParam('wk1', wk1);
        this.addParam('wk2', wk2);
        this.addParam('wkId', wkId);
        this.addParam('grpId', grpId);
        this.addParam('userId', userId);
        this.addParam('wk', wkNr);
        this.addParam('func', 'setActiveWkGroup');
        this.addParam('afterfunc', 'wkList.setAll();');
        
        this.sendAjax();
    };


    this.setAll = function()
    {
        tmp = this.returned.split('¤');
        if(tmp[0] != '')
        {
            alert(tmp[0]);
        }
        else
        {
            // console.log(tmp[1]);
            if(tmp[1] == 'ERROR')
            {
                alert(tmp[2]);
                return;
            }
            if(tmp[1] == 'GOTO')
            {
                window.location.href = tmp[2];
                return;
            }
            
            
            getElem('warenkoerbe').innerHTML = tmp[1];
            this.getDndAreasZones();
        }
        if(tmp[2] != '' && tmp[2] != undefined)
            alert(tmp[2]);
    };

    
    this.viewArtikelDetails = function()
    {
        // console.log('Fenster mit den Artikeldetails einblenden');

        var tmp = this.returned.split('¤');
        getElem('artikelDetails').innerHTML = tmp[1];
        
        if(tmp[2] == 'empty')
            explodeElement('artikelDetails', 'artikel_'+tmp[0], 600, 300, 'center', 'center');
        else
            explodeElement('artikelDetails', 'artikel_'+tmp[0], 600, 500, 'center', 'center');
            
            
        getElem('artikelDetailsShaddow').style.height = window.outerHeight+'px';
        
        window.setTimeout("fadeElement('artikelDetailsShaddow', 0, 65, 450, true);", 800);
    };

	
	this.closeArtikelDetails = function()
	{
        // console.log('Fenster mit den Artikeldetails schliessen');

        fadeElement('artikelDetailsShaddow', 65, 0, 450, true);
        fadeElement('artikelDetails', 100, 0, 250, true);
    };

    
    this.changeMenge = function(art, artikelId, aktWert)
    {
        // console.log('Artikel-ID: '+ artikelId);
        // console.log('Menge: '+ art+' 1');
        if(art == '+')
            this.addParam('plusminus', "plus");
        else if(art == '-' && aktWert == 1)
        {
            if(confirm("Wenn Sie die Menge auf 0 setzen, wird der Artikel aus dem Warenkorb entfernt.\nMöchten Sie dennoch fortfahren?") == false)
                return;
            
            this.addParam('plusminus', "minus");
        }
        else if(art == '-')
            this.addParam('plusminus', "minus");
            
        this.viewLoading('Die Menge wird aktualisiert');
        this.addParam('func', 'aktualisiereMenge');
        this.addParam('afterfunc', 'wkList.setAll();');
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.addParam('artikelId', artikelId);
        this.sendAjax();
    };

    
    this.changeArtikelVar = function(wkArtikelId, newArtikelId)
    {
        // console.log('Ändern der Variationsnummer');
        // console.log('ID des Artikels im Warenkorb: '+ wkArtikelId);
        // console.log('VariationsId: '+ newArtikelId);
        this.viewLoading('Die Variation wird geändert');
        
        this.addParam('func', 'changeArtikelVar');
        this.addParam('afterfunc', 'wkList.reloadPage();');
        this.addParam('newArtikelId', newArtikelId);
        this.addParam('wkArtikelId', wkArtikelId);
        this.sendAjax();
    };

    
    this.reloadPage = function()
    {
        // console.log('Seite neu laden');
        this.viewLoading('Warenkorb wird aktualisiert');
        window.location.reload();
    };


    this.moveGroup = function(direction, grpId, wkId, wk)
    {
        // console.log('Richtung: '+ direction);
        // console.log('GruppenId: '+ grpId);
        // console.log('Warenkorb: '+ wk);
        // console.log('Warenkorb-ID: '+ wkId);
        this.viewLoading('Die Gruppe wird verschoben');
        
        nodes = getElem('warenkorb'+wk).childNodes;
        liste = new Array();
        akt = 0;
        
        for(var i=0; i!=nodes.length; i++)
        {
            if(nodes[i].id.substr(0, 3) != 'grp')
                continue;
            
            if(liste[akt] && liste[akt] != '')
                akt++;
                
            liste[akt] = nodes[i].getAttribute('grpId'); 
            
            if(nodes[i].getAttribute('grpId') == grpId && direction == 'up')
            {
                liste[akt]      = liste[(akt-1)];
                liste[(akt-1)]  = nodes[i].getAttribute('grpId');
            }
            else if(nodes[i].getAttribute('grpId') == grpId && direction == 'down')
            {
                liste[(akt+2)]  = nodes[i].getAttribute('grpId');
            }
            
            akt++;
        }
    
        for(var i=0; i!=liste.length; i++)
        {
            // console.log('GruppenId '+ liste[i]+ ' an Position: '+i);
            this.addParam('move['+liste[i]+']', i);        
        }
    
        this.addParam('func', 'moveGroup');
        this.addParam('afterfunc', 'wkList.setAll();');
        this.addParam('ERROR', 'Die Gruppe konnte nicht umbenannt werden.');
        this.addParam('grpId', grpId);
        this.addParam('direction', direction);
        this.addParam('wk', wk);
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.sendAjax();
    };


    this.changeArtikelOrder = function(direction, artikelId, grpId, wkId, secondwkId, wk)
    {
        // console.log('Richtung: '+ direction);
        // console.log('ArtikelId: '+ artikelId);
        // console.log('GruppenId: '+ grpId);
        // console.log('Warenkorb: '+ wk);
        // console.log('Warenkorb-ID 1: '+ wkId);
        // console.log('Warenkorb-ID 2: '+ secondwkId);
        this.viewLoading('Der Artikel wird verschoben');

        nodes = getElem(grpId).childNodes;
        liste = new Array();
        akt = 0;
        for(var i=0; i!=nodes.length; i++)
        {
            if(nodes[i].id.substr(0, 8) != 'artikel_')
                continue;
            
            if(liste[akt] && liste[akt] != '')
                akt++;
                
            liste[akt] = nodes[i].getAttribute('artikelId'); 
            
            if(nodes[i].getAttribute('artikelId') == artikelId && direction == 'up')
            {
                liste[akt]      = liste[(akt-1)];
                liste[(akt-1)]  = nodes[i].getAttribute('artikelId');
            }
            else if(nodes[i].getAttribute('artikelId') == artikelId && direction == 'down')
            {
                liste[(akt+2)]  = nodes[i].getAttribute('artikelId');
            }
            
            akt++;
        }
    
        for(var i=0; i!=liste.length; i++)
        {
            // console.log('Artikel '+ liste[i]+' an Position: '+i);
            this.addParam('move['+liste[i]+']', i);
        }
        
        this.addParam('func', 'moveArtikel');
        this.addParam('afterfunc', 'wkList.setAll();');
        this.addParam('ERROR', 'Der Artikel konnte nicht verschoben werden.');
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.sendAjax();
    };
    
    
    this.copyArtikelSelect = function(artikelId, grpId)
    {
        this.viewLoading('Auswahl wird geladen');
        
        this.addParam('func', 'getCopyArtikelSelect');
        this.addParam('afterfunc', 'wkList.setCopyArtikelSelect();');
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.addParam('artikelId', artikelId);
        this.addParam('grpId', grpId);
        this.addParam('userId', getElem('warenkorbHeader1').getAttribute('userId'));
        this.sendAjax();        
    };
    

    this.setCopyArtikelSelect = function()
    {
        getElem('copySelectWindow').innerHTML = this.returned;
        getElem('copySelectWindow').style.left = (parseInt(mouseposX)+10)+'px';
        getElem('copySelectWindow').style.top = (parseInt(mouseposY)+10)+'px';
        this.hideLoading(true);
        fadeElement('copySelectWindow', 0, 100, 250, true);
    };
    

    this.copyArtikel = function(artikelId, grpId)
    {
        fadeElement('copySelectWindow', 100, 0, 250, true);
        this.viewLoading('Artikel wird kopiert');
        
        this.addParam('func', 'copyArtikel');
        this.addParam('afterfunc', 'wkList.setAll();');
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.addParam('artikelId', artikelId);
        this.addParam('grpId', grpId);
        this.sendAjax();        
    };

    
    this.insertWarenkorb = function()
    {
        tmp = this.returned.split('¤');
        if(tmp[1] == '')
            alert(tmp[0]);
        else
            getElem(tmp[2]).innerHTML = tmp[1];
        
        this.getDndAreasZones();
    };

    
    this.insertWarenkoerbe = function()
    {
        tmp = this.returned.split('¤');
        if(tmp[0] != '')
            alert(tmp[0]);
        else
        {
            getElem('warenkorb1').innerHTML = tmp[1];
            getElem('warenkorb2').innerHTML = tmp[2];
            this.getDndAreasZones();
        }
    };


    this.newGroup = function(wkNr)
    {
        // console.log("Erstellen in Warenkorb "+ wkNr);
        // console.log("ID Warenkorb 1: "+ this.getWkId(1));
        // console.log("ID Warenkorb 2: "+ this.getWkId(2));
        
        name = '';
        while(name == '')
        {
            name = prompt('Bitte geben Sie einen Namen für die Gruppe an:', '');
            if(name == null)
            {
                // console.log("Abbruch durch User");
                return;
            }
            
            name = name.trim();
        }
        // console.log("Neuer Name: "+ name);
        this.viewLoading('Gruppe wird erstellt');
        this.addParam('func', 'newGroup');
        this.addParam('afterfunc', 'wkList.setAll();');
        this.addParam('ERROR', 'Die Gruppe konnte nicht erstellt werden.');
        this.addParam('grpName', name);
        this.addParam('wk', wkNr);
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.sendAjax();
    };


    this.deleteWk = function(wk, wk1, wk2)
    {
        // console.log("Löschen des Warenkorbs "+ wk);
        // console.log("ID Warenkorb 1: "+ wk1);
        // console.log("ID Warenkorb 2: "+ wk2);
        
        if(!confirm('Möchten Sie wirklich diesen Warenkorb, mit allen enthaltenen Gruppen und Artikeln entfernen?'))
        {
            // console.log("Abbruch durch Benutzer");
            return;
        }
        this.viewLoading('Warenkorb wird gelöscht');
        this.addParam('func', 'deleteWk');
        this.addParam('afterfunc', 'wkList.setAll();');
        this.addParam('wk', wk);
        this.addParam('wk1', wk1);
        this.addParam('wk2', wk2);
        this.sendAjax();
    };    


    this.deleteGroup = function(grpId, wkId, wk)
    {
        // console.log("Löschen der Gruppe "+ grpId);
        // console.log("Warenkorb "+ wk);
        // console.log("WarenkorbId: "+ wkId);
        
        if(!confirm('Möchten Sie wirklich diese Gruppe, mit allen enthaltenen Artikeln aus Ihrem Warenkorb entfernen?'))
        {
            // console.log("Abbruch durch Benutzer");
            return;
        }
        this.viewLoading('Gruppe wird gelöscht');
        this.addParam('func', 'deleteGroup');
        this.addParam('afterfunc', 'wkList.setAll();');
        this.addParam('grpId', grpId);
        this.addParam('wk', wk);
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.sendAjax();
    };    

    
    this.getWkId = function(nr)
    {
        if(nr == 1)
            return getElem('warenkorbHeader1').getAttribute('wkId');
        else
            return getElem('warenkorbHeader1').getAttribute('secondwkId');
    };

	
    this.deleteArtikel = function(artikelId, wkId, secondwkId, wk)
    {
        // console.log("Löschen des Artikels "+ artikelId);
        // console.log("Warenkorb "+ wk);
        // console.log("WarenkorbId 1: "+ this.getWkId(1));
        // console.log("WarenkorbId 2: "+ this.getWkId(2));
        
        if(!confirm('Möchten Sie wirklich diesen Artikel löschen?'))
        {
            // console.log("Abbruch durch Benutzer");
            return;
        }
        this.viewLoading('Artikel wird gelöscht');
        this.addParam('func', 'deleteArtikel');
        this.addParam('afterfunc', 'wkList.setAll();');
        this.addParam('artikelId', artikelId);
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.addParam('wk', wk);
        this.sendAjax();
    };    
	

	this.moveArtikel = function(artikelId, grpId, wk, wkId)
	{
        // console.log("Verschieben des Artikels "+artikelId);
        // console.log("Warenkorb "+wk);
        // console.log("WarenkorbId: "+ wkId);
        // console.log("ZielGruppenId: "+ grpId);
        this.viewLoading('Artikel wird verschoben');
        
	    getElem('artikel_'+artikelId).setAttribute('firstartikel', 'false');
	    getElem('artikel_'+artikelId).setAttribute('lastartikel', 'true');
	    getElem('artikel_'+artikelId).setAttribute('gruppenid', grpId);
	    getElem('artikel_'+artikelId).setAttribute('wk', grpId);
	    getElem('artikel_'+artikelId).setAttribute('wkId', grpId);
	
        this.addParam('func', 'changeArtikelGroup');
        this.addParam('afterfunc', 'wkList.setAll();');
        this.addParam('artikelId', artikelId);
        this.addParam('grpId', grpId);
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.sendAjax();
    };
	

	this.loadWk = function(wk, wk1, wk2, userId, top, left)
	{
        // console.log("Warenkorb "+ wk);
        // console.log("Warenkorb-ID 1"+ this.getWkId(1));
        // console.log("Warenkorb-ID 2"+ this.getWkId(2));
        // console.log("UserId: "+ userId);
        // console.log("Oben: "+ top);
        // console.log("Links: "+ left);
        this.viewLoading('Auswahl wird geladen');
        
        this.addParam('func', 'getWkSelect');
        this.addParam('afterfunc', 'wkList.viewWkSelect();');
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.addParam('userId', userId);
        this.addParam('wkselect', wk);
        this.addParam('top', top);
        this.addParam('left', left);
        this.sendAjax();
    };
	

	this.viewWkSelect = function()
	{
	    tmp = this.returned.split('¤');
	
	    if(!getElem(getElem('wkSelect')))
            document.getElementsByTagName('body')[0].innerHTML += tmp[0];
            
        getElem('wkSelect').style.left = (parseInt(tmp[1])-170)+'px';
        getElem('wkSelect').style.top  = (parseInt(tmp[2]) + 10)+'px';
        
        // console.log("Position des Fensters > top='"+getElem('wkSelect').style.top+"' left='"+getElem('wkSelect').style.left+"'");
        getElem('wkSelect').style.top  = (parseInt(tmp[2]) + 10)+'px';
        
        getElem('wkSelectAuswahl').innerHTML  = tmp[3];
        fadeElement('wkSelect', 0, 100, 250, true);
    };

    
    this.inserted = function()
    {
        tmp = this.returned.split('¥');
        
        if(getElem('hideWarenkorbInfoWindow') && getElem('hideWarenkorbInfoWindow').style.display == 'none')
            fadeElement('hideWarenkorbInfoWindow', 0, 100, 500, true);
            
        if(getElem('anzArtikelBasket') && tmp[1] != 'ERROR')
            getElem('hideWarenkorbInfoWindow').innerHTML = tmp[1];
        
        var div = getElem('wkInsertedPopup');
        if(div == false)
        {
            var htm                     = document.createElement('div');
            htm.id                      = 'wkInsertedPopup';
            htm.style.border            = '1px solid #444';
            htm.style.backgroundColor   = '#fff';
            htm.style.padding           = '10px';
            htm.style.position          = 'absolute';
            
            if(isIE)
            {
                htm.style.marginTop         = '100px';
                htm.style.marginLeft        = '-400px';
            }
            else
            {
                htm.style.marginTop         = '200px';
                htm.style.marginLeft        = '200px';
            }
            htm.style.width             = '200px';
            htm.style.zIndex            = '99999';
            htm.style.display           = 'none';
            getElem('content').appendChild(htm);
            
            
            var htm                     = document.createElement('div');
            htm.id                      = 'wkInsertedPopupBg';
            htm.style.backgroundColor   = '#fff';
            htm.style.position          = 'absolute';
            htm.style.left              = '0px';
            htm.style.right             = '0px';
            htm.style.top               = '0px';
            htm.style.bottom            = '0px';
            htm.style.height            = '100%';
            htm.style.zIndex            = '99998';
            htm.style.display           = 'none';
            document.getElementsByTagName('body')[0].appendChild(htm);
        }
        var div = getElem('wkInsertedPopup');
        if(div == false)
            alert(tmp[0]);
        else
        {
            div.innerHTML = tmp[0];
            fadeElement('wkInsertedPopupBg', 0, 60, 100, true);
            window.setTimeout("fadeElement('wkInsertedPopup', 0, 100, 500, true);", 200);
            
            if(tmp[1] != 'ERROR')
            {
                div.style.width = '200px';
                window.setTimeout("fadeElement('wkInsertedPopup', 100, 0, 200, true);", 2700);
                window.setTimeout("fadeElement('wkInsertedPopupBg', 60, 0, 100, true);", 2900);
            }
            else
            {
                var close = '<br><br><center><a href="#" onclick="';
                close += 'fadeElement(\'wkInsertedPopup\', 100, 0, 200, true);';
                close += 'fadeElement(\'wkInsertedPopupBg\', 60, 0, 100, true);';
                close += 'return false;">Schliessen</a></center>';
            
                div.innerHTML += close;
                div.style.width = '350px';
            }
        }
    };
	
	
	this.insert = function(artikelId, varId, button, userId)
	{
        // console.log('User-ID: '+ userId);
        // console.log('Artikel-ID: '+ artikelId);
        // console.log('Var-ID: '+varId);
        // console.log('Button-ID: '+button);
        
	    this.addParam('func', 'addToBasket');
        this.addParam('artikelId', artikelId);
        this.addParam('varId', varId);
        this.addParam('userId', userId);
        this.addParam('button', button);
        this.sendAjax();
    };

    
    this.selectInsertWarenkorb = function()
    {        
        tmp = this.returned.split('¤');
        if(getElem('wkInsertSelect'))
        {
            setToScreenCenter('wkInsertSelect', document.getElementsByTagName('body')[0]);
            fadeElement(getElem('wkInsertSelect'), 0, 100, 450, true);
            return;
        }
        
        var div = '<div id="wkInsertSelect" style="display: none;">';
        div += '<div id="wkInsertSelectHeader">';
            div += '<div id="wkInsertSelectHeaderText">Auswahl</div>';
            div += '<div id="wkInsertSelectHeaderIcon">';
            div += '<img src="images/warenkorb/icons/cross.png" onclick="';
            div += 'fadeElement(\'wkInsertSelect\', 100, 0, 450, ';
            div += 'true); return false;" style="cursor: pointer;"/></div>';
        div += '</div>';
        div += '<div id="wkInsertSelectBody">';
        div += tmp[1];
        div += '</div>';
        div += '</div>';
        
        document.getElementsByTagName('body')[0].innerHTML += div;
        setToScreenCenter('wkInsertSelect', document.getElementsByTagName('body')[0]);
        fadeElement(getElem('wkInsertSelect'), 0, 100, 450, true);
    };


	this.viewHelp = function(helpFile)
	{
	    var link = 'hilfe.php?sessionnr='+this.sessNr+'&sessionid=';
        link += this.sessId+'&wk1='+this.getWkId(1)+'&wk2='+this.getWkId(2);
        
        if(helpFile != undefined)
            link += '&hlpId='+helpFile;
        
        this.viewLoading('Hilfe wird aufgerufen');
        
        window.location.href = link;
    };

	
	this.copyGruppe = function(grpId, wkNr, art, newName)
	{     
        if(getElem('copySettings'))
            fadeElement('copySettings', 100, 0, 10, true);
        if(getElem('copySettingsBg'))
            fadeElement('copySettingsBg', 100, 0, 10, true);
        
	
	    if(newName == undefined)
	       newName = '';
	    if(art == undefined)
	       art = '';
	
	    this.viewLoading('Gruppe wird kopiert');
        var copyTo;
        
        if(wkNr == 1)
            copyTo = this.getWkId(2);
        else
            copyTo = this.getWkId(1);
        
        // console.log('grpId: '+ grpId);
        // console.log('In Warenkorb: '+ copyTo);
        
	    this.addParam('func', 'copyGruppe');
        this.addParam('afterfunc', 'wkList.copyGruppe2();');
        this.addParam('grpId', grpId);
        this.addParam('wkId', copyTo);
        this.addParam('wkNr', wkNr);
        this.addParam('userId', userId);
        this.addParam('wk1', this.getWkId(1));
        this.addParam('wk2', this.getWkId(2));
        this.addParam('newName', newName);
        this.addParam('art', art);
        this.sendAjax();
    };
    
    this.copyGruppe2 = function()
    {
        tmp = this.returned.split('¤');
	    if(tmp[0] == 1)
	    {
	        this.returned = '¤'+tmp[1]+'¤'+tmp[2]
            wkList.setAll();
            return;
        }
        if(getElem('copySettings') == false)
        {
            console.log('wird erstellt');
            div = document.createElement('div');
            div.id = 'copySettings';
            div.style.display = 'none';
            div.style.position = 'absolute';
            div.style.marginTop = '50px';
            div.style.marginLeft = '50px';
            div.style.width = '300px';
            div.style.textAlign = 'center';
            div.style.padding = '5px';
            div.style.zIndex = '999';
            div.style.border = '2px solid #999';
            div.style.color = '#0a0359';
            div.style.fontSize = '14px';
            div.style.fontWeight = 'bold';
            div.style.backgroundColor = '#efefef';
            getElem('warenkoerbe').appendChild(div);
        }
        if(getElem('copySettingsBg') == false)
        {
            div = document.createElement('div');
            div.id = 'copySettingsBg';
            div.style.display = 'none';
            div.style.position = 'absolute';
            div.style.top = '0px';
            div.style.left = '0px';
            div.style.right = '0px';
            div.style.bottom = '0px';
            div.style.width = '100%';
            div.style.height = '100%';
            div.style.zIndex = '998';
            div.style.backgroundColor = '#fff';
            document.getElementsByTagName('body')[0].appendChild(div);
        }        
        if(!getElem('copySettings'))
            console.log('irgendwatt stimmt nit');
        getElem('copySettings').innerHTML = tmp[1];
        fadeElement('copySettingsBg', 0, 60, 150, true);
        fadeElement('copySettings', 0, 100, 250, true);
	};

    this.getMenu = function(art, el)
    {
        // console.log("Menu für: "+ art);

        var i = 0;
        var def = new Array();
        helpId = el.getAttribute('helpName');
        switch(art)
        {
            // -----------------------------------------------------------------
            case 'wk':
                wk      = el.getAttribute('wk');
                wk1     = el.getAttribute('wkId');
                wk2     = el.getAttribute('secondwkId');
                userId  = el.getAttribute('userId');
                def[i]  = new Object({'ANZEIGE': 'Warenkorb laden','ICON': 'warenkorb/icons/basket_go.png','FUNCTION': 'wkList.loadWk(\''+wk+'\', \''+wk1+'\', \''+wk2+'\', \''+userId+'\', \''+mouseposX+'\', \''+mouseposY+'\');'}); i++;
                
                if((wk2 != '' && wk == 2) || (wk1 != '' && wk == 1))
                {
                    def[i] = new Object({'ANZEIGE': 'Warenkorb umbenennen','ICON': 'warenkorb/icons/pencil.png','FUNCTION': 'wkList.renameWk(\''+wk+'\', \''+wk1+'\', \''+wk2+'\');'}); i++;
                    def[i] = new Object({'ANZEIGE': 'Warenkorb l&ouml;schen','ICON': 'warenkorb/icons/basket_delete.png','FUNCTION': 'wkList.deleteWk(\''+wk+'\', \''+wk1+'\', \''+wk2+'\', \''+userId+'\', \''+mouseposX+'\', \''+mouseposY+'\');'}); i++;
                }
                
                def[i] = 'line'; i++; 
                def[i] = new Object({'ANZEIGE': 'Hilfe','ICON': 'warenkorb/icons/information.png','FUNCTION': 'wkList.viewHelp(\'wk_laden\');'}); i++;
                break;           
            // -----------------------------------------------------------------
            case 'wkAuswahl':
                wkId = el.getAttribute('wkId');
                def[i] = new Object({'ANZEIGE': 'Warenkorb laden','ICON': 'warenkorb/icons/basket_go.png','FUNCTION': 'wk.loadWk(\''+wkId+'\');'}); i++;
                def[i] = new Object({'ANZEIGE': 'Warenkorb l&ouml;schen','ICON': 'warenkorb/icons/basket_delete.png','FUNCTION': 'wk.deleteWk(\''+wkId+'\');'}); i++;
                def[i] = 'line'; i++;
                def[i] = new Object({'ANZEIGE': 'Vergleichen','ICON': 'warenkorb/icons/reload.png','FUNCTION': 'wk.vergleiche(\''+wkId+'\',null);'}); i++;
                
                def[i] = 'line'; i++; 
                def[i] = new Object({'ANZEIGE': 'Hilfe','ICON': 'warenkorb/icons/information.png','FUNCTION': 'wkList.viewHelp(\'wk_laden\');'}); i++;
                break;            
            // -----------------------------------------------------------------
            case 'grp':
                id      = el.getAttribute('id');
                grpId   = el.getAttribute('subgrp');
                wkNr    = el.getAttribute('wk');
                aktiv   = el.getAttribute('aktiv');
                gruppenId   = el.getAttribute('grpId');
                wkId        = getElem('grp_'+grpId).getAttribute('wkId');
                secondwkId  = getElem('grp_'+grpId).getAttribute('secondwkId');
                userId  = getElem('warenkorbHeader1').getAttribute('userId');
                
                wk1   = getElem('warenkorbHeader1').getAttribute('wkid');
                wk2   = getElem('warenkorbHeader1').getAttribute('secondwkid');
                
//                 if(getElem('subgrp_'+grpId).style.display == 'block')
//                     def[i] = new Object({'ANZEIGE': 'Artikel der Gruppe ausblenden','ICON': 'warenkorb/icons/minus.gif','FUNCTION': 'wkList.scrollGroup(\''+grpId+'\');'});
//                 else
//                     def[i] = new Object({'ANZEIGE': 'Artikel der Gruppe einblenden','ICON': 'warenkorb/icons/plus.gif','FUNCTION': 'wkList.scrollGroup(\''+grpId+'\');'});
//                 i++;
                if(aktiv == "0")
                {
                    def[i] = new Object({'ANZEIGE': 'Gruppe aktivieren','ICON': 'warenkorb/icons/bullet_green.png','FUNCTION': 'wkList.activateWarenkorbGroup(\'\', \''+gruppenId+'\', \''+wk1+'\', \''+wk2+'\', \''+userId+'\', \''+wkNr+'\');'});
                    i++;
                    def[i] = 'line'; 
                    i++;
                }                
                
                if(getElem('grp_'+grpId).getAttribute('firstGroup') != 'true')
                {
                    def[i] = new Object({'ANZEIGE': 'Gruppe nach oben schieben','ICON': 'warenkorb/icons/moveup.png','FUNCTION': 'wkList.moveGroup(\'up\', \''+gruppenId+'\', \''+wkId+'\', \''+wkNr+'\');'});
                    i++;
                }
                if(getElem('grp_'+grpId).getAttribute('lastGroup') != 'true')
                {
                    def[i] = new Object({'ANZEIGE': 'Gruppe nach unten schieben','ICON': 'warenkorb/icons/movedown.png','FUNCTION': 'wkList.moveGroup(\'down\', \''+gruppenId+'\', \''+wkId+'\', \''+wkNr+'\');'});
                    i++;
                }
                def[i] = 'line'; i++;
                
//                 def[i] = new Object({'ANZEIGE': 'Gruppe umbenennen','ICON': 'warenkorb/icons/folder_edit.png','FUNCTION': 'wkList.changeName(\''+grpId+'\', \''+gruppenId+'\');'}); i++;
                def[i] = new Object({'ANZEIGE': 'Gruppe l&ouml;schen','ICON': 'warenkorb/icons/folder_delete.png','FUNCTION': 'wkList.deleteGroup(\''+gruppenId+'\', \''+wkId+'\', \''+wkNr+'\');'}); i++;
                
                if(wkNr == 2 || (wkNr == 1 && wk2 > 0))
                {
                    def[i] = new Object({'ANZEIGE': 'Gruppe und Artikel kopieren','ICON': 'warenkorb/icons/folder_go.png','FUNCTION': 'wkList.copyGruppe(\''+gruppenId+'\', \''+wkNr+'\');'}); i++;
                }
//                 def[i] = new Object({'ANZEIGE': 'Gruppe erstellen','ICON': 'warenkorb/icons/folder_add.png','FUNCTION': 'wkList.newGroup(\''+wkId+'\', \''+secondwkId+'\', \''+wkNr+'\');'}); i++;
//                 def[i] = 'line'; i++;
//                 def[i] = new Object({'ANZEIGE': 'Artikel aus der Gruppe l&ouml;schen','ICON': 'warenkorb/icons/row_delete.png','FUNCTION': 'wkList.deleteGroupArtikel(\''+grpId+'\');'}); i++;
//                 def[i] = new Object({'ANZEIGE': 'Artikel kopieren','ICON': 'warenkorb/icons/reload.png','FUNCTION': 'wk.vergleiche(\''+grpId+'\',null);'}); i++;

                def[i] = 'line'; i++; 
                def[i] = new Object({'ANZEIGE': 'Hilfe','ICON': 'warenkorb/icons/information.png','FUNCTION': 'wkList.viewHelp(\'wk_gruppenerstellen\');'}); i++;
                break;            
            // -----------------------------------------------------------------
            case 'artikel':
                id          = el.getAttribute('id');
                artikelId   = el.getAttribute('artikelId');
                wk          = el.getAttribute('wk');
                
                wk1   = getElem('warenkorbHeader1').getAttribute('wkid');
                wk2   = getElem('warenkorbHeader1').getAttribute('secondwkid');

                def[i] = new Object({'ANZEIGE': 'Artikeldetails ansehen','ICON': 'warenkorb/icons/information.png','FUNCTION': 'wkList.artikelDetails(\''+artikelId+'\');'}); i++;
                def[i] = new Object({'ANZEIGE': 'Artikel l&ouml;schen','ICON': 'warenkorb/icons/book_delete.png','FUNCTION': 'wkList.deleteArtikel(\''+artikelId+'\', \''+wk1+'\', \''+wk2+'\', \''+wk+'\');'}); i++;
                def[i] = 'line'; i++;
                            
                grpId       = el.parentNode.id;
                gruppenId   = el.getAttribute('gruppenId');
                var viewLine = false;
                if(el.getAttribute('firstArtikel') != 'true')
                {
                    def[i] = new Object({'ANZEIGE': 'Artikel nach oben schieben','ICON': 'warenkorb/icons/moveup.png','FUNCTION': 'wkList.changeArtikelOrder(\'up\', \''+artikelId+'\', \''+grpId+'\', \''+wk2+'\', \''+wk2+'\', \''+wk+'\');'});
                    viewLine = true;
                    i++;
                }
                if(el.getAttribute('lastArtikel') != 'true')
                {
                    def[i] = new Object({'ANZEIGE': 'Artikel nach unten schieben','ICON': 'warenkorb/icons/movedown.png','FUNCTION': 'wkList.changeArtikelOrder(\'down\', \''+artikelId+'\', \''+grpId+'\', \''+wk1+'\', \''+wk2+'\', \''+wk+'\');'});
                    viewLine = true;
                    i++;
                }
                if(viewLine == true)
                {
                    def[i] = 'line'; 
                    i++;
                }
                
                def[i] = new Object({'ANZEIGE': 'Artikel in andere Gruppe kopieren','ICON': 'warenkorb/icons/copy.png','FUNCTION': 'wkList.copyArtikelSelect(\''+artikelId+'\', \''+gruppenId+'\');'}); i++;
                
                def[i] = 'line'; i++; 
                def[i] = new Object({'ANZEIGE': 'Hilfe','ICON': 'warenkorb/icons/information.png','FUNCTION': 'wkList.viewHelp(\'PrWkArtikelDetails\');'}); i++;
            // -----------------------------------------------------------------
        }
        
        // console.log(def);
        return def;         
    };



    
    /**
     * AJAX Funktionen
     **************************************************************************/
    
    /**
     * AjaxFunktionen ausführen
     */
    this.sendAjax = function(sec)
    {
        // Beim ersten aufruf den Aufrufzähler auf 0 setzen 
        if(sec == undefined)
            sec = 0;
        
        // Wenn die Funktion 3 mal aufgerufen wurde, ohne das die Abfrage 
        // Gesendet wurde, dann abbrechen
        if(sec == 3)
        {
            // console.log('! ! ! TIMEOUT ! ! !');
            return;
        }

        // Wenn zur zeit noch eine Abfrage läuft, dann 0,2 sekunden warten und
        // die Funktion erneut aufrufen
        if(this.running == true)
        {
            sec++;
            window.setTimeout("wkList.sendAjax("+sec+")", 200);
            
            // console.log('Ajax läuft noch ....');
            return;
        }
                
        // Verbindung initialisieren
        if(window.XMLHttpRequest)
        {        
            this.conn = new XMLHttpRequest();
        }
        else if(window.ActiveXObject)
        {
            // IE
            try
            {
                this.conn = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                try
                {
                    this.conn = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e)
                { }
            }
        }

        // Prüfen ob eine Verbindung hergestellt wurde
        if(!this.conn)
        {
            // console.log('Es konnte keine Verbindung hergestellt werden');
            return false;
        }
        
        // Merker setzen, das zur Zeit eine Abfrage läuft
        this.running = true;
        
        // Funktion zum abfragen der Antwort setzen
        this.conn.onreadystatechange = wkList.changeState;
        
        // POST-Verbindung erstellen
        this.conn.open('POST', this.ajaxScript, true);
        
        // console.log("Senden an: "+ this.ajaxScript);
        
        
        // Header der Verbindung setzen (Daten werden als post gesendet)
        this.conn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        
        this.addParam('sessionnr', this.sessNr);
        this.addParam('sessionid', this.sessId);
        
        // console.log("Parameter: "+ this.addParamArr);
                
        // Skript aufrufen
        this.conn.send(this.addParamStr);
        this.addParamStr = '';
        
        return true;       
    };


    /** 
     * Antworten der Verbindung abfragen
     */         
    this.changeState = function()
    {        
        // Prüfen welcher readyState vorliegt       
        if(wkList.conn.readyState == 4)
        { 
            // Status Prüfen
            // console.log('readyState: 4');
            if(wkList.conn.status == 200)
            {   
                // console.log('status: 200');
                
                // Rückgabe abfangen
                wkList.returned = wkList.conn.responseText;
                
                // Merker das zur Zeit eine Abfrage läuft zurücksetzen
                wkList.running = false;
                
                len         = wkList.returned.indexOf('¤');
                funcName    = wkList.returned.substring(0, len); 
                wkList.returned = wkList.returned.substring((len+1));
                // console.log("Nachfolgende Funktion: "+funcName);
//                 // console.log("Erhaltene Daten: ");
//                 // console.log(wkList.returned);
                eval(funcName);
                
                wkList.hideLoading();
            }
            else
            {
                // Beim Abfragen der Daten ist ein Fehler aufgetreten
                // console.log('Bei dem Request ist ein Problem aufgetreten.');
                
                // Merker das zur Zeit eine Abfrage läuft zurücksetzen
                wkList.running = false;
            }
        }
    };

    
    this.viewLoading = function(txt)
    {
        if(!getElem('loadingOverlayBg'))
            return;

        if(txt == undefined)
            txt = 'Daten werden geladen';

        getElem('loadingOverlayText').innerHTML = txt; 
        fadeElement('loadingOverlayBg', 0, 60, 150, true);
        fadeElement('loadingOverlay', 0, 100, 150, true);
    };


    this.hideLoading = function(wait)
    {
        if(!getElem('loadingOverlayBg') || getElem('loadingOverlayBg').style.display == 'none')
            return;
            
        t = 0;
        if(wait == undefined)
            t = 500;
        window.setTimeout("fadeElement('loadingOverlayBg', 60, 0, 250, true);", t);
        window.setTimeout("fadeElement('loadingOverlay', 100, 0, 250, true);", t);
    };
    
    
    /**
     * Parameter hinzufügen, welcher bei der Abfrage mitgesendet werden
     */             
    this.addParam = function(name, wert)
    {
        this.addParamStr += "&"+name+"="+wert;
        this.addParamArr[name] = wert;
    };
    
    
    /** 
     * Hinzufügen von Debugzeilen
     */         
    this.debug = function(name, str)
    {
        // Wenn das Element mit der ID "debug" existiert, dann wird dort die 
        // neue Zeile eingetragen
//         if(getElem('debug'))
//             getElem('debug').innerHTML = "<b>wkList."+name+"</b><br>"+str+"<hr>"+getElem('debug').innerHTML;
    };
    
}

function dndInit(t)
{  
    for(a in dZones1)
    {
        if(getElem(dZones1[a]))
        {            
            // console.log('Add DragElement: '+dZones1[a]);
            dndMgr.registerDraggable(new Rico.Draggable('rico-dnd',dZones1[a]));
        }
    }
    
    for(a in dZones2)
    {
        if(getElem(dZones2[a]))
        {
            // console.log('Add DropZone: '+dZones2[a]);
            dndMgr.registerDropZone(new Rico.Dropzone(dZones2[a]));
        }
    }
}

function dndDelete()
{
    dndMgr.initialize();
}


function reorgArtikels()
{
    for(a in dZones2)
    {
        if(getElem(dZones2[a]))
        {
            nodes = getElem(dZones2[a]).childNodes;
            
            for(i=0; i!=nodes.length; i++)
            {
                // console.log("Artikel: "+nodes[i].getAttribute('artikelid'));
                // console.log("Pos: "+nodes[i].style.position);
                
                if(nodes[i].style.removeProperty)
                {         
                    nodes[i].style.removeProperty('position');
                    nodes[i].style.removeProperty('background-color');
                    nodes[i].style.removeProperty('top');
                    nodes[i].style.removeProperty('left');
                    nodes[i].style.removeProperty('cssText');
                }
                else
                {   
                    nodes[i].style.position = '';
                    nodes[i].style.top = '';
                    nodes[i].style.left = '';
                    nodes[i].style.cssText = '';
                    nodes[i].style.backgroundColor = 'transparent';
                }
                // console.log("Pos: "+nodes[i].style.position);
                
            }
            
            h = 45;
            if(isIE)
                h = 47;

            h = (nodes.length*h);
            
            if(h < 45)
                h = 45;
                
            getElem(dZones2[a]).style.height = h;
        }
    }
}

function addDebug(str)
{
    if(!window.console)
        return;
}
