var changed = false;
var initialFieldValues = new Array();
var descriptionShowing = new Array();
var secondaryWindow;

function isEmpty(field) {
    if (!field) {
        return true;
    }
    var value = field.value;

    return (value.length < 1);
}

function fireActionEvent(actionName, actionParameters, requestPath) {
    var form = document.getElementById('form');

    form._actionName.value = actionName;
    form._actionParameters.value = actionParameters;

    if (requestPath) {
        form.action = document.getElementsByTagName('base')[0].href + requestPath.substr(1);
    }

    clearDescriptions();

    form.submit();
}

function clearDescriptions() {
    var form = document.getElementById('form');

    for (var i = 0; i < form.elements.length; i++) {
        var field = form.elements[i];

        if (descriptionShowing[field.name]) {
            hideDescription(field);
        }
    }
}

function openCenteredPopup(url, windowName, windowWidth, windowHeight) {
    var windowLeft = Math.max(((screen.width - windowWidth) / 2) - 5, 0);
    var windowTop = Math.max((((screen.height - windowHeight) / 2) - 33), 0);

    var windowFeatures = 'width=' + windowWidth + ', height=' + windowHeight + ', left=' + windowLeft + ', top=' + windowTop;

    var newWindow = window.open(url, windowName, windowFeatures);
    newWindow.focus();
}

function openFullScreenPopup(url, windowName) {
    var windowWidth = screen.availWidth;
    var windowHeight = screen.availHeight;
    var windowLeft = screen.availLeft;
    var windowTop = screen.availTop;

    var windowFeatures = 'width=' + windowWidth + ', height=' + windowHeight + ', left=' + windowLeft + ', top=' + windowTop;

    var newWindow = window.open(url, windowName, windowFeatures);
    newWindow.focus();
}

function openSecondaryFullScreen(url, windowName) {
    var windowWidth = 1024 + 5;
    var windowHeight = 768 + 5;
    var windowLeft = 1024 - 5;
    var windowTop = 768 - 35;

    var windowFeatures = 'width=' + windowWidth + ', height=' + windowHeight + ', left=' + windowLeft + ', top=' + windowTop;

    secondaryWindow = window.open(url, windowName, windowFeatures);
    secondaryWindow.focus();
    window.focus();
}

function findField(fieldName) {
    var form = document.getElementById('form');

    return form[fieldName];
}

function showDescription(field, description) {
    field.value = description;
    field.style.color = '#887766';
    descriptionShowing[field.name] = true;
}

function checkShowDescription(field, description) {
    if (isEmpty(field)) {
        showDescription(field, description);
    }
}

function hideDescription(field) {
    field.value = '';
    field.style.color = '#000000';
    descriptionShowing[field.name] = false;
}

function checkHideDescription(field) {
    if (descriptionShowing[field.name]) {
        hideDescription(field);
    }
}

function pageLoaded(message) {
    initializeFieldValues();

    if (window.resizeFixedNote) {
        resizeFixedNote();
    }

    if (message) {
        notify(message);
    }

    if (window.setFocus) {
        setFocus();
    }
}

function pageUnloaded() {
    var top = self.screenTop;

    if (top > 9000) {
        windowClosed();
    }
}

function windowClosed() {
    if (secondaryWindow) {
        secondaryWindow.close();
    }
}

function initializeFieldValues() {
    var form = document.getElementById('form');

    for (var fieldName in initialFieldValues) {
        form[fieldName].value = initialFieldValues[fieldName];
    }
}

function notify(message) {
    var form = document.getElementById('form');

    if (!form._visited.value) {
        window.alert(message);
        form._visited.value = 'true';
    }
}

function valueChanged(element) {
    changed = true;
}

function notImplemented() {
    window.alert('Dit werkt nog niet.');
}

function mouseXRelativeToPage(event) {
    if (event.pageX) return event.pageX;
    else if (event.clientX)
        return event.clientX + (document.documentElement.scrollLeft ?
                              document.documentElement.scrollLeft :
                              document.body.scrollLeft);
    else return null;
}
function mouseYRelativeToPage(event) {
    if (event.pageY) return event.pageY;
    else if (event.clientY)
        return event.clientY + (document.documentElement.scrollTop ?
                              document.documentElement.scrollTop :
                              document.body.scrollTop);
    else return null;
}

function convertXRelativeToPageToXRelativeTo(element, x) {
    var relativeX = x;

    while (element.offsetParent)
    {
        relativeX -= element.offsetLeft ;
        element = element.offsetParent ;
    }

    return relativeX;
}

function convertYRelativeToPageToYRelativeTo(element, y) {
    var relativeY = y;

    while (element.offsetParent)
    {
        relativeY -= element.offsetTop ;
        element = element.offsetParent ;
    }

    return relativeY;
}

function mouseXRelativeTo(element, event) {
    return convertXRelativeToPageToXRelativeTo(element, mouseXRelativeToPage(event));
}

function mouseYRelativeTo(element, event) {
    return convertYRelativeToPageToYRelativeTo(element, mouseYRelativeToPage(event));
}

function debugLayout() {
    var bodyWidth = document.getElementsByTagName('body')[0].offsetWidth;
    var bodyHeight = document.getElementsByTagName('body')[0].offsetHeight;
    var pageWidth = document.getElementById('page').offsetWidth;
    var pageHeight = document.getElementById('page').offsetHeight;
    var marginWidth = document.getElementById('margin').offsetWidth;
    var marginHeight = document.getElementById('margin').offsetHeight;

    if (document.getElementById('content')) {
        var contentWidth = document.getElementById('content').offsetWidth;
        var contentHeight = document.getElementById('content').offsetHeight;
    }

    if (document.getElementById('sidebar')) {
        var sidebarWidth = document.getElementById('sidebar').offsetWidth;
        var sidebarHeight = document.getElementById('sidebar').offsetHeight;
    }

    if (document.getElementById('searcharea')) {
        var searchareaWidth = document.getElementById('searcharea').offsetWidth;
    }

    window.alert('body width = ' + bodyWidth + '; page width = ' + pageWidth + '; margin width = ' + marginWidth + '; content width = ' + contentWidth + '; sidebar width = ' + sidebarWidth + '; searcharea width = ' + searchareaWidth + '\n' +
    'body height = ' + bodyHeight + '; page height = ' + pageHeight + '; margin height = ' + marginHeight +'; content height = ' + contentHeight + '; sidebar height = ' + sidebarHeight);
}

