﻿if(document.all && !document.getElementById) {
    document.getElementById = function(id) {
         return document.all[id];
    }
}

String.prototype.trimEnd = function() {
    return this.replace(/\s*$/, '');
}

function confirmDelete() {
    return confirm("Är du säker på att du vill radera objektet?");
}

function activeBoxChangedState(strCheckboxId, strButtonId) {
    var objCheckbox = document.getElementById(strCheckboxId);
    var objButton = document.getElementById(strButtonId);
    if (objButton != null && objCheckbox != null) {
        if (objCheckbox.checked) {
            objButton.value = "Godkänn länk";
        }
        else {
            objButton.value = "Uppdatera länk";
        }
    }
}

/*** Sets the cursor focus to the first text, password, or 
 * textarea field on the first form of the document (if it exists).
 *
 * @return True/False Whether or not the classes were set
 */
function focusFirstField() {
    var boolReturn = false ;
    if (!document || !document.forms || (document.forms.length < 1) || (document.forms[0].elements < 1)) {
        return boolReturn;
    }
    var form = document.forms[0];
    var elements = form.elements;
    for (var i = 0; i < elements.length && !boolReturn; i++) {
        var element = elements[i];
        switch (element.type) {
            case "text":
            case "textarea":
                if (!element.disabled) {
                    element.focus();
                    //element.select();
                    boolReturn = true;
                }
                break;
            default:
                // keep looping
                break ;
        }
    }
    return boolReturn ;
}

/*** Call this function inside a script tag
 * instead of waiting until the onload event is fired to
 * call a function.
 *
 * @param statement The statement to try to run.
 * Statement can return true to 
 * break iteration.
 * @param iInterval Number milliseconds between tries
 * @param iNumTries Keep trying for iNumTries times
 * @return nothing
 */
function tryStatement(statement, iInterval, iNumTries) {
    var stopTrying = eval(statement);
    if (!stopTrying && (iNumTries > 0)) {
        iNumTries--;
        functionCall = "tryStatement(" + "'" + statement + "'" + "," + iInterval + "," + iNumTries + ")";
        setTimeout(functionCall, iInterval);
    }
}

function initFavoritListan() {
    tryStatement("focusFirstField()", 1, 300);
}
