/**
 * Init
 */
    var WEBROOT = '';
    var DOCROOT = '';
    var interval_messageBox;

    function Init(_webroot,_docroot)
    {
        WEBROOT = _webroot;
        DOCROOT = _docroot;
        interval_messageBox = window.setInterval("closeMessageBox()",5000);
    }

/**
 * MessageBox automatisch schließen
 */
    function closeMessageBox()
    {
        if (document.getElementById('box_msg_ok'))
        {
            document.getElementById('box_msg_ok').style.display = 'none';
        }
        if (document.getElementById('box_msg_error'))
        {
            //document.getElementById('box_msg_error').style.display = 'none';
        }
        window.clearInterval(interval_messageBox);
    }

/**
 * Cookie setzen
 */
    function setCookie(name,value,time)
    {
        var expHour = time;
        var exp = new Date();
        var result;
        exp.setTime(exp.getTime() + (expHour*60*60*1000));
        //document.cookie = name + "=" + value + "; expires=" + exp.toGMTString();

        AjaxRequest.get(
          {
            'url':WEBROOT+'setcookie.php',
            'parameters':{ 'name':name, 'value':value },
            'onError':function(req) { alert('Cookie konnte nicht gesetzt werden') },
            'onSuccess':function(req)
            {
                result = req.responseText;
                if (result != 'OK') alert('Cookie konnte nicht gesetzt werden ('+result+')');
            }
          }
        );

    }

/**
 * Import
 */
    function startImport(go,importValue)
    {
        var result;
        AjaxRequest.get(
          {
            'url':'../admin/import.ajax.php',
            'async':true,
            'parameters':{ 'go':go,'importValue':importValue },
            'onError':function(req) { document.getElementById('importState').innerHTML = 'StatusText='+req.statusText+'\nContents='+req.responseText; },
            'onSuccess':function(req)
            {
                result = req.responseText;

                if (go == 1)
                {
                    document.getElementById('importState').innerHTML = result;
                    if (result.substr(0, 2) == 'OK')
                    {
                        startImport(1,importValue);
                    }
                    else
                    {
                        startImport(2,importValue);
                        document.getElementById('importState').innerHTML = 'Import durchgeführt! (Schritt 1) (ImportValue: '+importValue+')<br />Permalinks werden erzeugt...';
                        if (req.responseText)
                            document.getElementById('importState').innerHTML+= '<hr>'+req.responseText;
                    }
                }
                if (go == 2)
                {
                    document.getElementById('importState').innerHTML = result;
                    if (result.substr(0, 2) == 'OK')
                    {
                        document.getElementById('importState').innerHTML = 'Import durchgeführt! (Schritt 2) (ImportValue: '+importValue+')';
                        if (req.responseText)
                            document.getElementById('importState').innerHTML+= '<hr>'+req.responseText;
                    }
                }



            }
          }
        );
    }

/**
 * Klickeffekt für Mausklick auf Button
 */
    function clickButton(element)
    {
        if (element.style.borderStyle != 'inset inset inset inset')
            element.style.borderStyle = 'inset'
        else
            element.style.borderStyle = 'outset';
    }

/**
 * Ladeinfo
 */
    function showLoading(errmsg)
    {
        var result;

        result = '<div id="content_main">';
        result = result + '<div class="title"><h1>Lade...</h1></div>';
        result = result + '<div class="content">';
        if (!errmsg)
        {
            result = result + '<img src="gfx/icon_loading.gif" alt="" />';
        }
        else
        {
            result = result + '<b>Fehler: '+errmsg+'</b>';
        }
        result = result + '</div>';
        result = result + '</div>';
        return result;
    }

/**
 * LiveSuche!
 */
    function isLiveSearch()
    {
        return document.getElementsByName('chkLiveSearch')[0].checked;
    }

    function searchType(element)
    {
        var type = element.options[element.selectedIndex].value;
        setCookie('selSearch',type,24);
        document.getElementById('strSearch').name = 'strSearch'+type;
        if ((type != 'Pzn') && (type != 'Fam'))
        {
            document.getElementById('chkLiveSearch').disabled  = false;
            if (document.getElementById('strSearch').value != '')
            {
                if (isLiveSearch())
                {
                     submitSearch(document.getElementsByName('formSearch')[0]);
                }
            }
        }
        else
        {
            document.getElementById('chkLiveSearch').checked = false;
            document.getElementById('chkLiveSearch').disabled  = true;

        }
    }

    function submitSearch(theform)
    {
        var selSearch = document.getElementsByName('selSearch')[0];

        if (selSearch.options[selSearch.selectedIndex].value == 'Pzn')
        {
            var searchstr = document.getElementById('strSearch').value;
            if (searchstr.length < 7)
            {
                for (var a=0; a<(7-searchstr.length); a++)
                {
                    document.getElementById('strSearch').value = '0'+document.getElementById('strSearch').value;
                }
            }
        }

        if (document.getElementById('strSearch').value.length > 2)
        {
            document.getElementById('strSearch').readOnly = true;
            document.getElementById('content').innerHTML = showLoading('');
            if (isLiveSearch())
            {
                if (selSearch.options[selSearch.selectedIndex].value == 'Pbl')
                    theform.action = WEBROOT+'content/product_list.inc.php';
                if (selSearch.options[selSearch.selectedIndex].value == 'Mbl')
                    theform.action = WEBROOT+'content/manufacturer_list.inc.php';
                if (selSearch.options[selSearch.selectedIndex].value == 'Pzn')
                    theform.action = WEBROOT+'content/product_list.inc.php';
                if (selSearch.options[selSearch.selectedIndex].value == 'Fam')
                    theform.action = WEBROOT+'content/product_list.inc.php';

                var status = AjaxRequest.submit
                (theform,
                    {
                    'parameters':{ 'isAx':'1' },
                    'timeout':10000,
                    'onTimeout':function(req) { document.getElementById('strSearch').readOnly = false; document.getElementById('content').innerHTML = showLoading('Timed Out!'); },
                    'onError':function(req) { document.getElementById('strSearch').readOnly = false; document.getElementById('content').innerHTML = showLoading('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);},
                    'onSuccess':function(req) { document.getElementById('strSearch').readOnly = false; document.getElementById('content').innerHTML = req.responseText; }
                    }
                );
                return false;
            }
            else
            {
                var selSearch = document.getElementsByName('selSearch')[0];
                if (selSearch.options[selSearch.selectedIndex].value == 'Pbl')
                    theform.action = WEBROOT+'index.php?c=product_list';
                if (selSearch.options[selSearch.selectedIndex].value == 'Mbl')
                    theform.action = WEBROOT+'index.php?c=manufacturer_list';
                if (selSearch.options[selSearch.selectedIndex].value == 'Pzn')
                    theform.action = WEBROOT+'index.php?c=product_list';
                if (selSearch.options[selSearch.selectedIndex].value == 'Fam')
                    theform.action = WEBROOT+'index.php?c=product_list';
                return true;
            }
        }
        else
        {
            alert('Bitte max. 3 Zeichen eingeben');
            return false;
        }
    }

    var inputIntervall = 0;
    var inputDelayInc  = 0;

    function inputStartSearch()
    {
        if (inputDelayInc==20)
        {
            window.clearInterval(inputIntervall);
            inputDelayInc = 0;
            document.getElementById('statsLiveSearch').innerHTML = 'Suchbegriff eingeben:';
            submitSearch(document.getElementsByName('formSearch')[0]);
        }
        else
        {
            inputDelayInc++;
            var text = '';
            for (var a=0; a<inputDelayInc; a++)
            {
                text = text + '|';
            }
            document.getElementById('statsLiveSearch').innerHTML = '<span style="color:#E0691A;">'+text+'</span>';
        }
    }

    function inputSearch(element)
    {
        if (document.getElementById('strSearch').value.length > 2)
        {
            if (document.getElementById('strSearch').readOnly != true)
                if (isLiveSearch())
                {
                    inputDelayInc  = 0;
                    window.clearInterval(inputIntervall);
                    inputIntervall = window.setInterval("inputStartSearch()", 70);
                }
        }
    }

    function chkSearch(element)
    {
        setCookie('chkLiveSearch',element.checked,24);
    }

    function chkNoPayment(element)
    {
        setCookie('chkNoaddpay',element.checked,24);
    }


/**
 * Suche nach Alphabet
 */
    function searchByLetter(element)
    {
        if (element.options[element.selectedIndex].value == 'divByLetter_products')
        {
            document.getElementById('divByLetter_products').style.display = 'block';
            document.getElementById('divByLetter_manufacturer').style.display = 'none';
        }
        if (element.options[element.selectedIndex].value == 'divByLetter_manufacturer')
        {
            document.getElementById('divByLetter_products').style.display = 'none';
            document.getElementById('divByLetter_manufacturer').style.display = 'block';
        }
        setCookie('selByLetter',element.options[element.selectedIndex].value,24);
    }
