﻿
// Funktion zum Umleiten von ENTER in einem Textfeld auf den rictigen Button
// verwendet in Login.aspx
function SubmitOnEnter(buttonID, evt) {
    var keyPressed = (evt.which) ? evt.which : evt.keyCode;
    if (keyPressed == 13) {
        evt.returnValue = false;
        document.getElementById(buttonID).click();
    }
}

// Funktion zum Umleiten von ENTER in einem Textfeld zum Form-Submit
// verwendet in Index.aspx
function FormSubmitOnEnter(hiddenFieldID, shopNr, evt) {
    var keyPressed = (evt.which) ? evt.which : evt.keyCode;
    if (keyPressed == 13) {

        evt.returnValue = false;
        document.getElementById(hiddenFieldID).value = shopNr;
        document.getElementById("form1").submit();
    }
}

// ändert Hintergrundfarbe der textbox beim editieren
function ShowTextboxEdited(textBox, evt) {
    var keyPressed = (evt.which) ? evt.which : evt.keyCode;
    textBox.style.backgroundColor = 'yellow';    
}

// ändert Hintergrundfarbe der CheckBox beim editieren
function ShowCheckboxEdited(checkBox, evt) {
    checkBox.style.backgroundColor = 'yellow';
}

// Funktion zum Öffnen eines neuen Fensters (Popup)
function OpenPopupWindow(location) {
    window.open(location, "_blank", "", true);
}

// Funktion Klicken auf einen Button nach einer Zeitspanne
function SubmitOnTimeout(button, seks) {
    // build the call statement
    var cmd = "alert('Timeout for "+button+"'); "+button+".click();";
    var t = setTimeout(cmd, seks*1000);
//        button.click();
}

function disableSelection(target) {
    if (target != null) {
        if (typeof target.onselectstart != "undefined") //IE route
            target.onselectstart = function() { return false }
        else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
            target.style.MozUserSelect = "none";
        else //All other route (ie: Opera)
            target.onmousedown = function() { return false }
        target.style.cursor = "default";
    }
}
//function disableselect(e){
//    return false;
//}
//function reEnable(){
//    return true;
//}

//document.onselectstart = new Function ("return false");
//if (window.sidebar){
//    document.onmousedown=disableselect
//    document.onclick=reEnable
//}

function openPopupImage(imageUrl, width, height) {

    var divPopup = document.getElementById('popUpImage');
    divPopup.style.backgroundImage = 'url(' + imageUrl + ')';
    // set size
    divPopup.style.width = width + 'px';
    divPopup.style.height = height + 'px';
    // calculate position
    var h = (768 - height) / 3;
    if (h < 0)
        h = 10;
    divPopup.style.top = h + "px";
    var w = (1024 - width) / 2;
    divPopup.style.left =  w + "px";
    
    divPopup.style.visibility = 'visible';

}

function closePopupImage() {
    var divPopup = document.getElementById('popUpImage');
    divPopup.style.visibility = 'hidden';
}

// Funktion zum Erkennen von EAN Barcodes
function IsEAN13Code(txtInput) {
    var strInput = "";
    if (txtInput == null)
        return false;
    else
        strInput = txtInput.value;

    var bIsEAN = false;

    if (strInput.length == 13)	// genau 13 Stellen
    {
        // auf nur Zahlen prüfen
        var bAllDigits = true;
        for (i = 0; i < 13; i++) {
            if (strInput[i] < '0' || strInput[i] > '9') {
                bAllDigits = false;
                break;
            }
        }
        if (bAllDigits) {
            // Prüfsumme bilden
            var iSumme = 0;
            var iDigitVal = 0;
            for (i = 0; i < 12; i++) {
                // auf Zahl umrechnen
                iDigitVal = strInput[i] - '0';

                if (i % 2 == 0)	// 1., 3., etc. Stelle
                    iSumme += (iDigitVal * 1);
                else	// 2., 4., etc. Stelle
                    iSumme += (iDigitVal * 3);
            }
            // Prüfsumme dazu, muss genau durch 10 teilbar sein
            iDigitVal = strInput[12] - '0';
            if ((iSumme + iDigitVal) % 10 == 0)
                bIsEAN = true;
        }
    }

    return bIsEAN;
}

function SubmitOnEANCode(txtInput, buttonID) {
    // check EAN
    if (IsEAN13Code(txtInput) == true)
        document.getElementById(buttonID).click();
}
