
//### browserdetection
var opera = navigator.userAgent.toLowerCase().indexOf('opera') >= 0 ? true : false;
var safari = navigator.userAgent.toLowerCase().indexOf('safari') >= 0 ? true : false;
var ie7 = navigator.userAgent.toLowerCase().indexOf('msie 7') >= 0 ? true : false;
var ie6 = navigator.userAgent.toLowerCase().indexOf('msie 6') >= 0 ? true : false;
var IE = (document.all) ? true : false;
var timerOn = false;
var timerID = null;

//### zeigt oder versteckt elemente
function showHide(objID, action, pfeil) {
    var obj = document.getElementById(objID);
    if (obj == undefined) return false;
    if (action == undefined || action == '') action = 'toggle';
    if (pfeil == undefined || pfeil == '') pfeil = false;
    switch (action) {
        case 'toggle':
            if (obj.style.display == 'block') showHide(objID, 'hide');
            else showHide(objID, 'show');
            break;
        case 'show':
            obj.style.display = 'block';
            einAusblenden(objID, 1);
            break;
        case 'hide':
            obj.style.display = 'none';
            einAusblenden(objID, 0);
            break;
    }
    if (pfeil) togglePfeil(objID);
}

//### speichert zustand im cookie von aufklappbaren menu
function einAusblenden(objID, val) {
    if (!safari) {
        var theCookie = getCookieEinAusblenden();
        var theCookieValues = theCookie.split(',');
        var theCookieValue;
        var i;
        var cookieStr;
        if (val == 0) {
            cookieStr = theCookie.replace(objID + ':1', objID + ':0');
        } else {
            cookieStr = theCookie.replace(objID + ':0', objID + ':1');
        }
        document.cookie = cookieStr;
    }
}

//### holt gespeicherte werte aus cookie zum ein/ausblenden oder legt diesen wenn nicht vorhanden
function getCookieEinAusblenden() {
    var theCookies;
    var theCookie = '-';
    var i;
    var objs;
    var cookieStr;
    if (document.cookie) {
        theCookies = document.cookie.split(';');
        for (i = 0; i < theCookies.length; i++) {
            if (theCookies[i].match(/spAdmMnu/)) theCookie = theCookies[i];
        }
        if (theCookie == '-' || theCookie == 'spAdmMnu=') {
            cookieStr = 'spAdmMnu=';
            objs = document.getElementsByTagName('div');
            for (i = 0; i < objs.length; i++) {
                if (objs[i].id.match(/lm/)) {
                    cookieStr += objs[i].id + ':0,';
                }
            }
            document.cookie = cookieStr;
            theCookie = getCookieEinAusblenden();
        }
    }
    return theCookie;
}

function highlight(objID) {
    addRemoveClass(objID, 'highlightLink');
}

//### fügt eine class hinzu oder entfernt diese wenn vorhanden
function addRemoveClass(objID, strClass, anzahl) {
    // 3. parameter ist optional
    if (anzahl == undefined) anzahl = 0;
    // nur ausführen wenn beide parameter angegeben sind
    if (objID == '' || strClass == '') { }
    else {
        // nur einzelne objekte
        var i = 0;
        var nr = '';
        for (i = 0; i <= anzahl; i++) {

            if (anzahl > 0) { var obj = document.getElementById(objID + i); }
            else { var obj = document.getElementById(objID); }

            if (obj == undefined || obj == null) continue;
            if (obj.className.search(strClass) > -1) { obj.className = obj.className.replace(strClass, ''); }
            else {
                if (obj.className == '') { obj.className = strClass; }
                else { obj.className = obj.className + ' ' + strClass; }
            }
        }
    }
}

function showHideClass(id, action, saveit, cookiename) {
    if (action == 'show') {
        document.getElementById('showHideClass_' + id).innerHTML = '<img src="//static.afterbuy.de/afterbuy/images/abroot/closesmall.gif" border="0" width="14" height="14" style="cursor:pointer;" onclick="javascript:showHideClass(' + "'" + id + "', 'hide', 1, '" + cookiename + "');" + '">';
        if (IE) changeCssClass('showHideClass_' + id, 'display', '');
        else changeCssClass('showHideClass_' + id, 'display', 'table-row');
        if (saveit == 1)
            tajax('/afterbuy/interfaces/general_interface.aspx', 'art=saveCookie&cname=' + cookiename + '&vname=showHideClass_' + id + '&cvalue=show', 'trashhere', '', 'HTML', '', 1, 0, '');
    }
    else {
        document.getElementById('showHideClass_' + id).innerHTML = '<img src="//static.afterbuy.de/afterbuy/images/abroot/opensmall.gif" border="0" width="14" height="14" style="cursor:pointer;" onclick="javascript:showHideClass(' + "'" + id + "', 'show', 1, '" + cookiename + "');" + '">';
        changeCssClass('showHideClass_' + id, 'display', 'none');
        if (saveit == 1)
            tajax('/afterbuy/interfaces/general_interface.aspx', 'art=saveCookie&cname=' + cookiename + '&vname=showHideClass_' + id + '&cvalue=hide', 'trashhere', '', 'HTML', '', 1, 0, '');
    }
}

function isDate(dateStr) 
{
    var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) 
    {
        //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[3]; // p@rse date into variables
    day = matchArray[1];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("Month "+month+" doesn`t have 31 days!");
        return false;
    }

    if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap)) {
        //alert("February " + year + " doesn`t have " + day + " days!");
        return false;
    }
    }
    return true; // date is valid
}


/*
function changeCssClass(className, styleName, styleValue) {
    //alert("Start");

    var i = 0, j = 0;
    var cssRules = 'cssRules';
    if (document.all) { cssRules = 'rules'; }
    className = '.' + className;
    for (i = 0; i < document.styleSheets.length; i++) {
        for (j = 0; j < document.styleSheets[i][cssRules].length; j++) {
            if (document.styleSheets[i][cssRules][j].selectorText == className) {
                //alert(document.styleSheets[i][cssRules][j].selectorText);
                if (document.styleSheets[i][cssRules][j].style[styleName]) {
                    //alert('hier' + styleValue);
                    document.styleSheets[i][cssRules][j].style[styleName] = styleValue;
                    //alert('hier');
                    if (cssRules == 'rules' && styleName == 'background' && styleValue == '') {
                        //alert('hier');
                        document.styleSheets[i][cssRules][j].style.cssText = document.styleSheets[i][cssRules][j - 1].style.cssText;
                    }
                } else {
                    //alert('hier');
                    if (styleName.substring(0, 7) == 'border-') {
                        //alert(document.styleSheets[i][cssRules][j].style.cssText);
                        document.styleSheets[i][cssRules][j].style.cssText = document.styleSheets[i][cssRules][j].style.cssText + ';' + styleName + ':' + styleValue;
                        //alert(document.styleSheets[i][cssRules][j].style.cssText);
                    }
                    if (cssRules == 'cssRules' && styleValue == '') document.styleSheets[i][cssRules][j].style.cssText = styleName + ':' + styleValue;
                    if (cssRules == 'cssRules' && styleName == 'background') document.styleSheets[i][cssRules][j].style.cssText = styleName + ':' + styleValue;
                    if (cssRules == 'rules') document.styleSheets[i][cssRules][j].style[styleName] = styleValue;
                    if (styleName == 'text-align') document.styleSheets[i][cssRules][j].style.cssText = document.styleSheets[i][cssRules][j].style.cssText + ";text-align:" + styleValue;
                }
            }
        }
    }
}*/

function MarkAll(myFormName) {
	if (myFormName == undefined || myFormName == '') return false;
	if (document.getElementById(myFormName).id.length == undefined) {
        if (document.getElementById(myFormName).id.checked == true)
        { document.getElementById(myFormName).id.checked = false; }
        else
        { document.getElementById(myFormName).id.checked = true; }
    }
    else {
        for (var x = 0; x < document.getElementById(myFormName).id.length; x++) {
            if (document.getElementById(myFormName).id[x].checked == true)
            { document.getElementById(myFormName).id[x].checked = false; }
            else
            { document.getElementById(myFormName).id[x].checked = true; }
        }
    }
}

// Suchform auf standard setzen
function ResetAll(myFormName) {
	if (myFormName == undefined||myFormName == '') return false;
    for (var x = 0; x < document.getElementById(myFormName).length; x++) {
        if ((document.getElementById(myFormName).elements[x].type == 'text') || (document.getElementById(myFormName).elements[x].type == 'checkbox')) {
            document.getElementById(myFormName).elements[x].value = '';
        }
        if (document.getElementById(myFormName).elements[x].type == 'select-one') {
            if (document.getElementById(myFormName).elements[x].name == 'secialfield') {
                document.getElementById(myFormName).elements[x].value = 'all';
            }
            else {
                document.getElementById(myFormName).elements[x].value = '';
            }
        }
    }
}

function DisableFormButtons(myFormName) {
	if (myFormName == undefined || myFormName == '') return false;
	for (var x = 0; x < document.getElementById(myFormName).length; x++) {
        if (document.getElementById(myFormName).elements[x].type == 'submit' || document.getElementById(myFormName).elements[x].type == 'button') {
            document.getElementById(myFormName).elements[x].disabled = true;
        }
    }

}

// Get page coordinates of an element
function getPageCoords(elementId) {
    var element;
    if (document.all)
        element = document.all[elementId];
    else if (document.getElementById)
        element = document.getElementById(elementId);
    if (element) {
        var coords = { x: 0, y: 0 };
        do {
            if (element.currentStyle) {
                if (element.currentStyle.position != 'relative') {
                    coords.x += element.offsetLeft;
                    coords.y += element.offsetTop;
                }
            }
            else {
                coords.x += element.offsetLeft;
                coords.y += element.offsetTop;
            }

            element = element.offsetParent;
        }
        while (element)
        return coords;
    }
    else
        return null;
}

// Timer to show or hide dropdown menu
function startDropDownTimer(elementId, area) {
    if (timerOn == false) {
        switch (area) {
            case 'applications':
                if (window.document.getElementById(elementId).style.display == 'block') timerID = setTimeout('ShowHideDropDown("sortList", "sortSelection", 8)', 2000);
                break;
            case 'action':
                if (window.document.getElementById(elementId).style.display == 'block') timerID = setTimeout('ShowHideDropDown("actionDropDownList", "actionDropDown", 4)', 2000);
                break;
        }
        timerOn = true;
    }
}
function stopDropDownTimer() {
    if (timerOn) {
        clearTimeout(timerID);
        timerID = null;
        timerOn = false;
    }
}
function ShowHideDropDown(listElement, posElement, offset) {
    if (window.document.getElementById(listElement).style.display == 'none') {
        window.document.getElementById(listElement).style.display = 'block';
    } else if (window.document.getElementById(listElement).style.display == 'block') {
        window.document.getElementById(listElement).style.display = 'none';
    }
    var coords = getPageCoords(posElement);
    var newPosX = coords.x + window.document.getElementById(posElement).offsetWidth - window.document.getElementById(listElement).offsetWidth;
    var newPosY = coords.y + window.document.getElementById(posElement).offsetHeight + offset;
    window.document.getElementById(listElement).style.left = newPosX + "px";
    window.document.getElementById(listElement).style.top = newPosY + "px";
}



function GetKeyEvent(evt, art, url, neu, machneu, fehler, formName) {
    if (formName == undefined) formName = 'Form_Edit';

    var curKeyCode = 0;
    if (!evt)
        evt = window.event;
    if (evt.which) {
        curKeyCode = evt.which;
    } else if (evt.keyCode) {
        curKeyCode = evt.keyCode;
    }

    //alert(curKeyCode);
    switch(curKeyCode) {
        // F8
        case 119:
            if ((art == neu) || (art == machneu) || (art == "edit") || (art == "update")) {
                document.getElementById(formName).id.value = '0';
                if ((art == machneu) || (fehler == '1') || (fehler == 'True') || (fehler == 'true')) document.getElementById(formName).art.value = neu;
                else document.getElementById(formName).art.value = machneu;
                document.getElementById(formName).submit();
            } else {
                document.location.href = url + '?art=' + machneu;
            }
            break;
        
        // F9 
        case 120:
            if ((art == "edit") || (art == "update")) {
                document.getElementById(formName).submit();
            }
            break;

        // F12
        case 123:
            if ((art == "edit") || (art == "update")) {
                document.getElementById(formName).id.value = '0';
                document.getElementById(formName).art.value = neu;
                document.getElementById(formName).submit();
            }
            break;
    }

}

// Alle Markieren
function PreSelectBox(Box, FormName) {
    if (Box == 1) {
        document.getElementById(FormName).art.selectedIndex = document.getElementById(FormName).art_tmp.selectedIndex;
    }
    else if (Box == 2) {
        document.getElementById(FormName).art_tmp.selectedIndex = document.getElementById(FormName).art.selectedIndex;
    }
}

// ToolTip
var posX = 0;
var posY = 0;
function GetMousePos(e) {
    if (IE) { // grab the x-y pos.s if browser is IE
        posX = event.clientX; //+ document.body.scrollLeft;
        posY = event.clientY; //+ document.body.scrollTop;
    } else {  // grab the x-y pos.s if browser is NS
        posX = e.pageX;
        posY = e.pageY;
    }
}

function ShowToolTipp(MyTxt) {
   
    document.getElementById('ToolTippDivContent').innerHTML = MyTxt;

    theName = "ToolTippDiv";
    obj = document.getElementById(theName);
    obj.style.left = posX + "px";
    obj.style.top = posY + "px";

    obj.style.visibility = 'visible';
    obj.style.display = 'block';
}
function HideToolTipp(MyTxt) {
    theName = "ToolTippDiv";
    obj = document.getElementById(theName);
    obj.style.visibility = 'hidden';
    //bToolTipVisible = false;
}

// EventListener für ToolTips
var bToolTipVisible = false;
function Listener(obj, pFile, pTxt, pWidth, pHeight) {
    if (pWidth == undefined) pWidth = 350;
    if (pHeight == undefined) pHeight = 300;

    if (pFile != "") {
        obj.ondblclick = function() {
            HideToolTipp();
            popupme(pFile, 'Hinweis', 'width=' + pWidth + ',height=' + pHeight);
            //popupme(pFile, 'Hinweis', 'width=' + pWidth + ',height=' + pHeight);
        };
    }

    if (pTxt != "") {
        if (pFile != "") {
            obj.onclick = function () {
                HideToolTipp();
                popupme(pFile, 'Hinweis', 'width=' + pWidth + ',height=' + pHeight);
            };
        } else {
        obj.onclick = function () {
            if (bToolTipVisible == false) {
                ShowNewToolTipp(pTxt, obj.id, pFile, pWidth, pHeight);
                bToolTipVisible = true;
            } else if (bToolTipVisible == true) {                
                HideToolTipp();
                bToolTipVisible = false;
            }
        };
        }
    }

    if (pTxt != "") {
        obj.onmouseout = function () {
            if (bToolTipVisible != true) {
                HideToolTipp();
            }
        };
    }

    if (pTxt != "") {
        obj.onmouseover = function () {
            //if (bToolTipVisible == false) {
            ShowNewToolTipp(pTxt, obj.id, pFile, pWidth, pHeight);
            //}
        };
    }


}


function ShowNewToolTipp(pTxt, id, pFile, pWidth, pHeight) {
    var ausgabe = ""
    //if (pFile != "") ausgabe = ausgabe + '<span style="float:right;" onclick="HideToolTipp();popupme(\'' + pFile + '\', \'Hinweis\', \'width=' + pWidth + ',height=' + pHeight + '\');">(per Doppelklick in einem neuen Fenster &ouml;ffnen)<\/span><br \/><br \/>';
    //if (pFile != "") ausgabe = ausgabe + '<span style="float:right;" onclick="HideToolTipp();popupme(\'' + pFile + '\', \'Hinweis\', \'width=' + pWidth + ',height=' + pHeight + '\');">(per Doppelklick in einem neuen Fenster &ouml;ffnen)<\/span><br \/><br \/>';
    ausgabe = ausgabe + pTxt
    document.getElementById('ToolTippDivContent').innerHTML = ausgabe;

    // Position ermitteln
    var coords = getPageCoords(id);
    var newPosX = coords.x + window.document.getElementById(id).offsetWidth + 10;
    var newPosY = coords.y + window.document.getElementById(id).offsetHeight + 10;

    var newSizeWidth = newPosX + document.getElementById('ToolTippDiv').offsetWidth;
    var newSizeHeight = newPosY + document.getElementById('ToolTippDiv').offsetHeight;
    do {
        newPosX = newPosX - 10;
        newSizeWidth = newPosX + window.document.getElementById('ToolTippDiv').offsetWidth;
    }
    while (newSizeWidth > document.body.clientWidth);
    do {
        newPosY = newPosY - 10;
        newSizeHeight = newPosY + (window.document.getElementById('ToolTippDiv').offsetHeight);
    }
    while (newSizeHeight > document.body.clientHeight);

    if (newPosX < 30) newPosX = 30;
    if (newPosY < 30) newPosY = 30;
    // Position ermitteln

    // Position setzen
    theName = "ToolTippDiv";
    obj = document.getElementById(theName);
    obj.style.left = newPosX + "px";
    obj.style.top = newPosY + "px";
    // Position setzen

    obj.style.visibility = 'visible';
    obj.style.display = 'block';

    //bToolTipVisible = true;
}
// ToolTip