var pendingUpdates = false;

function SafeAttachEvent(oElement, sEvent, pResult) {
    if (oElement.addEventListener) {
        oElement.addEventListener(sEvent, pResult, false);
    } else if (oElement.attachEvent) {
        oElement.attachEvent('on' + sEvent, pResult);
    }
}

function SafeDetachEvent(oElement, sEvent, pResult) {
    if (oElement.removeEventListener) {
        oElement.removeEventListener(sEvent, pResult, false);
    } else if (oElement.detachEvent) {
        oElement.detachEvent('on' + sEvent, pResult);
    }
}

function ResizePlayer(iWidth, iHeight) {
    var oVideoContainer;
    oVideoContainer = document.getElementById('videoContainer');
    if (oVideoContainer) {
        oVideoContainer.style.width = iWidth + 'px';
        oVideoContainer.style.height = iHeight + 'px';
        oVideoContainer.style.display = 'block';
    }
}

function ExpandSelection(oOldSelection) {
    var oSelection = oOldSelection.duplicate();
    var oFoundElement = null;
    var oElement;
    var iIndex = 0;
    oElement = oSelection.parentElement();
    while (iIndex < 25 && oElement != null) {
        iIndex++;
        switch (oElement.tagName.toLowerCase()) {
            case 'div':
            case 'span':
            case 'p':
                oElement = null;
                break;
            default:
                oFoundElement = oElement;
                oElement = oElement.parentElement;
                oSelection.moveToElementText(oFoundElement);
                break;
        }
    }
    if (oFoundElement != null) {
        return (oSelection);
    }
    return (oOldSelection);
}

function ApplyStyle(tagName, sClassName) {
    var oSelection;
    var oWasFocused;
    oSelection = document.selection.createRange();
    oWasFocused = document.getElementById(document.getElementById('FocusedElement').value);
    if (oSelection) {
        var snewText;
        oSelection = ExpandSelection(oSelection);
        if (0 < sClassName.length) {
            snewText = '<' + tagName + ' class="' + sClassName + '">' + oSelection.text + '</' + tagName + '>';
        }
        else {
            snewText = '<' + tagName + '>' + oSelection.text + '</' + tagName + '>';
        }
        oSelection.select();
        document.execCommand('Delete');
        oSelection.pasteHTML(snewText);
    }
    if (oWasFocused) {
        oWasFocused.fireEvent('onkeyup');
    }
}

function UnloadTrigger(stopPop) {
    if (pendingUpdates) {
        window.event.returnValue = confirm('Your changes will be lost if you navigate away.\\r\\nClick Cancel, then the save button to prevent this.');
    }
}

function ShowSave() {
    var saveActive = document.getElementById('SaveChangesActive');
    var saveInactive = document.getElementById('SaveChanges');
    if (saveActive && saveInactive && saveActive.style.display == 'none') {
        saveActive.style.display = 'inline';
        saveInactive.style.display = 'none';
        pendingUpdates = true;
    }
}

function ToggleStyle(sAction) {
    if (document.execCommand != undefined && document.queryCommandEnabled != undefined && document.queryCommandEnabled(sAction)) {
        document.execCommand(sAction);
        UpdateFocused();
    }
}

function ToggleStyle(sAction, sParameter) {
    if (document.execCommand != undefined && document.queryCommandEnabled != undefined && document.queryCommandEnabled(sAction)) {
        document.execCommand(sAction, false, sParameter);
        UpdateFocused();
    }
}

function UpdateFocused() {
    if (document.getElementById('FocusedElement')) {
        var oWasFocused;
        oWasFocused = document.getElementById(document.getElementById('FocusedElement').value);
        if (oWasFocused) {
            oWasFocused.fireEvent("onkeyup");
        }
    }
}

function FocusedElement(sElementId) {
    document.getElementById('FocusedElement').value = sElementId;
}

function UpdateButton(iFeaturesAvailable, sButtonId, iFeaturesSupported) {
    var buttonActive = document.getElementById(sButtonId + 'Active');
    var buttonInactive = document.getElementById(sButtonId);
    if (buttonActive) {
        if (iFeaturesSupported == 0 || 0 < (iFeaturesAvailable & iFeaturesSupported)) {
            buttonActive.style.display = 'inline';
            if (buttonInactive) {
                buttonInactive.style.display = 'none';
            }
        }
        else {
            buttonActive.style.display = 'none';
            if (buttonInactive) {
                buttonInactive.style.display = 'inline';
            }
        }
    }
}

function ValidateDayMonth(prefix) {
    var curDays;
    curDays = Number(document.getElementById(prefix + 'day').value);
    if (28 < curDays) {
        var maxDays;
        switch (document.getElementById(prefix + 'month').value) {
            case '4':
            case '6':
            case '9':
            case '11':
                maxDays = 30;
                break;
            case '2':
                var year;
                year = Number(document.getElementById(prefix + 'year').value);
                maxDays = year % 4 == 0 ? 29 : 28;
                break;
            default:
                maxDays = 31;
                break;
        }
        if (maxDays < curDays) {
            var maxDays;
            document.getElementById(prefix + 'day').value = maxDays;
        }
    }
    return true;
}

function ClearDefault(elementId, defaultValue) {
    var element = document.getElementById(elementId);
    if (element.value == defaultValue) {
        element.value = ''
    }
}

function StandardPopup(sWindowName, sUrl) {
    CustomPopup(sWindowName, sUrl, 520, 500);
}

function CustomPopup(sWindowName, sUrl, iWidth, iHeight) {
    oPopup = window.open(EscapeURL(sUrl), sWindowName, 'width=' + iWidth + ',height=' + iHeight + ',scrollbars');
    if (oPopup) {
        oPopup.opener = window;
        oPopup.window.focus();
    }
}

function NewWindow(sWindowName, sUrl) {
    oPopup = window.open(EscapeURL(sUrl), sWindowName, 'location=yes,directories=yes,menubar=yes,resizable=yes,status=yes,toolbar=yes,scrollbars=yes');
    if (oPopup) {
        oPopup.opener = window;
        oPopup.window.focus();
    }
}

function EscapeURL(sUrl) {
    var iQuestPos = sUrl.indexOf('?');
    if (0 <= iQuestPos) {
        var sAnswer;
        var sRemainder;
        var sSeparator;
        var iAmpPos;
        sAnswer = sUrl.substr(0, iQuestPos + 1);
        sRemainder = sUrl.substr(iQuestPos + 1);
        sSeparator = '';
        iAmpPos = sRemainder.indexOf('&');
        while (0 <= iAmpPos) {
            sAnswer = sAnswer + sSeparator + EscapeParameter(sRemainder.substr(0, iAmpPos));
            sRemainder = sRemainder.substr(iAmpPos + 1);
            iAmpPos = sRemainder.indexOf('&');
            sSeparator = '&';
        }
        sAnswer = sAnswer + sSeparator + EscapeParameter(sRemainder);
        return sAnswer;
    }
    return sUrl;
}

function EscapeParameter(sParameter) {
    var iEqPos = sParameter.indexOf('=');
    if (0 <= iEqPos) {
        return sParameter.substr(0, iEqPos + 1) + encodeURIComponent(sParameter.substr(iEqPos + 1));
    }
    return sParameter;
}

function UpdateImage(imageId) {
    var oImage;
    oImage = document.getElementById(imageId);
    if (oImage) {
        oImage.src = document.getElementById('txt' + imageId + 'path').value;
        oImage.width = document.getElementById('lng' + imageId + 'width').value;
        oImage.height = document.getElementById('lng' + imageId + 'height').value;
    }
}

function SelectImage(url, controlId, category, maxWidth, maxHeight) {
    StandardPopup('SelectImage', url + '?controlId=' + controlId + '&cat=' + category + '&width=' + maxWidth + '&height=' + maxHeight);
}
