function libLoginHelper(form) {
    var elemLogin = form.user;
    var elemLoginValue = "";
    var elemPass = form.pass;
    var elemPassValue = "";
    
    if (elemLogin && elemPass) {
        elemLoginValue = elemLogin.value;
        elemPassValue = elemPass.value;
        //console.debug("found login-field: " + elemLoginValue);
        //console.debug("login length: " + elemLoginValue.length);
        //console.debug("pass  length: " + elemPassValue.length);
        elemPassValue = Ext.util.MD5(m_session + elemPassValue);
        //console.debug("pass as salted md5: " + elemPassValue);
    }
    
    if (!elemLoginValue.length) {
        return false;
    }
    
    var postdata = {
        method : 'libLoginHelper',
		id : 0,
		params : {
            'userid': elemLoginValue,
            'pass': elemPassValue
        }
	};
    
    //console.debug(postdata);
    
    Ext.Ajax.request({
        url: myinterface_lib,
        success: function (trans) {
				var data; 
				try {
					data = eval('('+trans.responseText+')'); // JSON "parsen"
                    //debugger;
                    if (data['_data']['userid']) {
                        //console.debug("gültiges login: " + data['_data']['userid'] + ", src: " + data['_data']['info']);
                        elemLogin.value = data['_data']['userid'];
                    } else {
                        //console.debug("ungültiges login!!!");
                    }
                    form.submit();
                    return true;
				} catch(e) {
                    //console.debug("ERROR1!");
					return false;   
				} 
                
                //console.debug(data);
        },
        failure: function () {
            //console.debug("ERROR2!");
        },
        params: Ext.util.JSON.encode(postdata)
    });
    //console.debug("RETURN2");
    return false;
}

function libDlgChgCartPosition(script, sessionid, itemid, posidx, oldcount) {
    var newcount = oldcount;
    
    var dlg = new Ext.Window({
        //id: id,
        title: const_other_changecartcount_wndtitle,
        closeAction : 'close',
        cls:   'mydoformat',
        modal:true,
        layout: 'fit',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'right',
        resizable: false,
        
        items: [
            new Ext.form.NumberField({
                fieldLabel: '',
                allowBlank: false,
                disabled: false,
                decimalSeparator: ',',
                value: oldcount
            })
        ],
        buttons: [
            {
                text     : 'OK',
                handler  : function(){
                    //console.debug(this.ownerCt.items.items[0].getValue());
                    Ext.Msg.wait(const_html_printingform_wait);
                    libChgCartPosition(script, sessionid, itemid, posidx, this.ownerCt.items.items[0].getValue())
                }
            },{
                text     : 'Abbrechen',
                handler  : function(){
                    //console.debug(this);
                    this.ownerCt.close();
                }
            }
        ]
    });
    dlg.show();
    return;
    
    /*
    Ext.Msg.prompt('Menge ändern', 'Bitte geben Sie die gewünschte Menge ein:', function(btn, text){
        if (btn == 'ok'){
            if (parseInt(text) != text) {
                alert("ungültig...");
                return;
            }
            alert(text);
            newcount = text;
        }
    }, this, false, oldcount);
    
    return;
    //Ext.Msg.wait(const_html_printingform_wait);
    var params = {
        'posidx': posidx
    };
    params[itemid] = newcount;
    
    Ext.Ajax.request({
        url: script + "?chgcart," + sessionid + "," + itemid,
        params: params,
        success: function (trans) {
                //window.location.reload();
                
        },
        failure: function () {
            console.log("ERROR2!");
        }
    });
    */
}

function libChgCartPosition(script, sessionid, itemid, posidx, newcount) {
    //Ext.Msg.wait(const_html_printingform_wait);
    var params = {
        'posidx': posidx
    };
    params[itemid] = newcount;
    
    Ext.Ajax.request({
        url: script + "?chgcart," + sessionid + "," + itemid,
        params: params,
        success: function (trans) {
                Ext.Msg.wait(const_html_printingform_wait);
                window.location.reload();
                /*console.debug(trans);
				var data; 
				try {
                    console.log("ok1");
					data = eval('('+trans.responseText+')'); // JSON "parsen"
                    
                    console.log("ok2");
                    
                    window.location.reload();
                    
                    return true;
				} catch(e) {
                    //console.debug("ERROR1!");
                    console.debug(e);
					return false;   
				} 
                
                console.debug(data);*/
        },
        failure: function () {
            console.log("ERROR2!");
        }
    });
}

function libPreprocessSearchterm() {
    var elem = document.getElementById('suchbegriff');
    if (elem) {
        elem.value = elem.value.replace(/\-/, " ");
        return true;
    }
    
    return false;
}


var win = 0;
function libShowZoomImg(title, file, small) {
    var width = 712;
    var height = 750;
    if (small) {
        width = 350 + 12;
        height = 350 + 50;
    }
    win = new Ext.Window({
        title: title,
        html: '<img onclick="win.close();" style="cursor:pointer;" src="'+file+'">',
        modal: true,
        x: 30,
        y: Ext.getBody().getScroll().top + 10,
        width: width,
        height: height,
        resizable: false,
        id: 'zoomwindow'
    });
    
    win.show();
}



function libAjaxShowcart() {
    var elem = document.getElementById('minicart');
    if (!elem) {
        return;
    }
    
    var postdata = {
        method: 'myajaxshowcart'
    };

    try {
        Ext.Ajax.request({
            url: myinterface_lib,
            success: function (trans) {
                var data; 
                try {
                    data = eval('('+trans.responseText+')'); // JSON "parsen"
                    
                    elem.innerHTML = data['_output'];
                    
                    return true;
                } catch(e) {
                    //debugger;
                    return false;   
                } 
            },
            failure: function () {
                //debugger;
            },
            params: Ext.util.JSON.encode(postdata)
        });
    } catch(e) {
        //debugger;
    } 
}

var progressDlg = 0;
var LIB_WKAUTOCHANGED = 0;
function libCloseWaitingWnd() {
    if (document.getElementById('ajaxwaitinginwk')) {
        //document.getElementById('ajaxwaitinginwk').style.visibility = "hidden";
        document.getElementById('ajaxwaitinginwk').style.display = "none";
    }
    
    if (progressDlg) {
        progressDlg.hide();
    }
    
    if (LIB_WKAUTOCHANGED) {
        LIB_WKAUTOCHANGED = 0;
        Ext.Msg.show({
           title: const_html_placecart_info_title,
           msg: const_html_placecart_autowkpack1,
           buttons: Ext.Msg.OK,
           icon: Ext.MessageBox.INFO
        });
    }
}


function libAjaxPlacecart(itemid, hashvariants, elForm) {
    /*console.log("libAjaxPlacecart *** " + itemid + " - " + hashvariants);
    console.debug(elForm);
    console.log(elForm.elements["a__"+itemid]);
    console.log(parseInt(elForm.elements["a__"+itemid].value));*/
    
    
    
    var selectedItems = new Array();
    
    if (!hashvariants) {
        wktest(itemid);
        
        selectedItems.push(itemid);
        
        var elCount = elForm.elements["a__"+itemid];
        var count = 1;
        
        if (elCount) {
            count = parseInt(elCount.value);
        }
        
        if (!count) {
            Ext.Msg.show({
               title: const_html_placecart_info_title,
               msg: const_ajaxplacecart_countinvalid1,
               buttons: Ext.Msg.OK,
               icon: Ext.MessageBox.ERROR
            });
            return;
        }
        
        var list1 = ""; 
        var list2 = ""; 
        if (elForm.elements["l1__" + itemid]) {
            var idx = elForm.elements["l1__" + itemid].selectedIndex;
            if (idx > 0) {
                list1 = elForm.elements["l1__" + itemid].options[idx].value;
            } else {
                Ext.Msg.show({
                   title: const_html_placecart_info_title,
                   msg: const_search_items_select_list,
                   buttons: Ext.Msg.OK,
                   icon: Ext.MessageBox.INFO
                });
                return;
            }
        }
        if (elForm.elements["l2__" + itemid]) {
            var idx = elForm.elements["l2__" + itemid].selectedIndex;
            if (idx > 0) {
                list2 = elForm.elements["l2__" + itemid].options[idx].value;
            } else {
                Ext.Msg.show({
                   title: const_html_placecart_info_title,
                   msg: const_search_items_select_list,
                   buttons: Ext.Msg.OK,
                   icon: Ext.MessageBox.INFO
                });
                return;
            }
        }
        
        selectedItems.push({ 
            itemid: itemid, 
            count: count,
            list1: list1,
            list2: list2
        });
        
    } else {
        wktest(itemid);
        
        // Einzelne Varianten eintragen!
        for (var i = 0; i < elForm.elements.length; i++) {
            var el = elForm.elements[i];
            if (el.name) {
                if (el.name.search(/^a__/) == 0) {
                    var id = el.name.substr(3);
                    var count = parseInt(el.value);
                    var list1 = ""; 
                    var list2 = ""; 
                    if (count > 0) {
                        selectedItems.push({ 
                            itemid: id, 
                            count: count,
                            list1: list1,
                            list2: list2
                        });
                    }
                }
            }
        }
       
    }
    
    if (!selectedItems.length) {
        Ext.Msg.show({
           title: const_html_placecart_info_title,
           msg: const_ajaxplacecart_countinvalid2,
           buttons: Ext.Msg.OK,
           icon: Ext.MessageBox.WARNING
        });
        return;
    }
    
    if (0) {
        if (document.getElementById('ajaxwaitinginwk')) {
            //document.getElementById('ajaxwaitinginwk').style.visibility = "visible";
            document.getElementById('ajaxwaitinginwk').style.display = "block";
        }
        
    } else {
        progressDlg = Ext.Msg.wait(const_ajaxplacecart_waitmsg, const_html_printingform_wait);
    }
    window.setTimeout("libCloseWaitingWnd()",2000);
    
    /*var loadingDlg = Ext.Window.show({
           title: const_html_placecart_info_title,
           html: "Bitte bei mindestens einem Artikel eine gültige Menge eintragen!",
           icon: 'mycustomwaitgif'
        });
    */
    var postdata = {
        method: 'myajaxplacecart',
        params: {
            items: [
            ]
        }
    };
    
    for (var i = 0; i < selectedItems.length; i++) {
        postdata['params']['items'].push(
            {
                'itemid': selectedItems[i]['itemid'],
                'count': selectedItems[i]['count'],
                'price': '', // Nicht unterstützt!!!
                'list1': selectedItems[i]['list1'],
                'list2': selectedItems[i]['list2'],
                'myajaxplacecartobj': '', // = normaler Artikel
                'orderfields': { // benutzerdef. Felder für Auftragspositionen
                    //'ORINGITEMID': ''
                }
            }
        );
    }
    
    try {
        Ext.Ajax.request({
            url: myinterface_lib,
            success: function (trans) {
                var data; 
                try {
                    data = eval('('+trans.responseText+')'); // JSON "parsen"
                 
                    //var title = language::myget('html_placecart_info_title');
                    //var msg = language::myget('html_placecart_info_message');
                    var title = const_html_placecart_info_title;
                    var msg = const_html_placecart_info_message;
                    
                    var elemwk = document.getElementById('miniwk');
                    if (elemwk) {
                        elemwk.setAttribute("class", "box_miniwk_ne"); // FF
                        elemwk.setAttribute("className", "box_miniwk_ne"); // IE...
                    }

                    /*Ext.Msg.show({
                       title: title,
                       msg: msg,
                       buttons: Ext.Msg.OK,
                       icon: Ext.MessageBox.INFO
                    });*/
                    //var progressDlg = Ext.Msg.wait(const_html_printingform_wait);
                    libAjaxShowcart();
                    //progressDlg.hide();
                    
                    //return true;
                } catch(e) {
                    //debugger;
                    //return false;   
                } 
            },
            failure: function () {
                //debugger;
            },
            params: Ext.util.JSON.encode(postdata)
        });
    } catch(e) {
        //debugger;
    } 
    
}


