
function MenuMain() {

    this.menuItem = [];

};

MenuMain.prototype.addMenuItem = function(sId, bSelected, sTargetUrl, iType) {

    this.menuItem.push(new MenuItem(sId, bSelected, sTargetUrl, iType));

};


MenuMain.prototype.mouseOver = function(sId) {

    this.getMenuItem(sId).mouseOver();

};


MenuMain.prototype.mouseOut = function(sId) {

    this.getMenuItem(sId).mouseOut();

};


MenuMain.prototype.mouseClick = function(sId) {

    this.getMenuItem(sId).mouseClick();

};


MenuMain.prototype.getMenuItem = function(sId) {

    for (var i = 0; i < this.menuItem.length; i++) {

        if (this.menuItem[i].id == sId)
            return this.menuItem[i];

    };

};


function MenuItem(sId, bSelected, sTargetUrl, iType) {

    this.id = sId;
    this.selected = bSelected;
    this.type = iType;

    this.targetUrl = sTargetUrl;

    switch (this.type) {
        case 0:
            this.imageFileSel = 'images/layout/menu_main_sel.gif';
            this.imageFileOver = 'images/layout/menu_main_over.gif';
            this.defaultTextColor = '#C5C5C5';
            break;
        case 1:
            this.imageFileSel = 'images/layout/menu_sub_sel.gif';
            this.imageFileOver = 'images/layout/menu_sub_over.gif';
            this.defaultTextColor = '#5D79BE';
            break;
    };

    this.element = document.getElementById(this.id);
    this.linkElement = document.getElementById('a_' + this.id);

    this.drawIcon();

};


MenuItem.prototype.drawIcon = function() {

    this.iconImage = document.createElement('img');
    this.iconImage.style.width = '9px';
    this.iconImage.style.height = '7px';
    this.iconImage.style.position = 'absolute';
    this.iconImage.style.top = '17px';
    this.iconImage.style.left = (Math.floor(this.element.offsetWidth / 2) - 4) + 'px';

    if (this.selected) {
        this.iconImage.src = this.imageFileSel;
        this.element.style.cursor = 'default';
    } else {
        this.iconImage.src = this.imageFileOver;
        this.iconImage.style.display = 'none';
    };

    this.element.appendChild(this.iconImage);

};


MenuItem.prototype.mouseOver = function() {

    if (!this.selected) {
        this.iconImage.style.display = 'block';
        this.linkElement.style.color = '#0b2b79';
    };

};


MenuItem.prototype.mouseOut = function() {

    if (!this.selected) {
        this.iconImage.style.display = 'none';
        this.linkElement.style.color = this.defaultTextColor;
    };

};


MenuItem.prototype.mouseClick = function() {

    if (!this.selected)
        document.location.href = this.targetUrl;

};


function CustomButton(sTargetUrl) {

    this.clicked = false;
    this.targetUrl = sTargetUrl;

};


CustomButton.prototype.click = function() {

};


CustomButton.prototype.clickWithGo = function() {

    if (!this.clicked) {

        location.href = this.targetUrl;

    };

};


// =================================
// ClosureSwitches
// =================================

function ClosureSwitches() {

    this.closureSwitch = [];
    this.textShow = '';
    this.textHide = '';

};


ClosureSwitches.prototype.addClosureSwitch = function(sRowId) {

    this.closureSwitch.push(new ClosureSwitch(sRowId, this));

};


function ClosureSwitch(sId, parent) {

    this.id = sId;
    this.parent = parent;

    this.rowPrefix = 'closureRow'
    this.buttonIdPrefix = 'closureSwitchButton'

    this.button = document.getElementById(this.buttonIdPrefix + this.id);
    this.button.onclick = createMethodReference(this, 'buttonClicked');
    this.closureRow = document.getElementById(this.rowPrefix + this.id);

    this.show = false;
    this.changeText(this.button, this.parent.textShow);

};

ClosureSwitch.prototype.buttonClicked = function() {

    if (this.show) {

        this.changeText(this.button, this.parent.textShow);
        this.closureRow.style.display = 'none';
        this.show = false;

    } else {

        this.changeText(this.button, this.parent.textHide);

        try {
            // Only for IE8 or FF.
            this.closureRow.style.display = 'table-row';

        } catch (err) {

            this.closureRow.style.display = 'block';

        };

        this.show = true;

    };

};


ClosureSwitch.prototype.changeText = function(oElem, sText) {

    // Remove all childnodes.
    while (oElem.hasChildNodes()) {

        oElem.removeChild(oElem.firstChild);

    };

    oElem.appendChild(document.createTextNode(sText));

};


// =================================
// Formulier
// =================================

function WebForm(sId) {

    this.id = sId;
    this.field = [];
    this.form = document.getElementById(this.id);

};


WebForm.prototype.addField = function(sId, sName, sErrorMessage, sMandatory) {

    this.field.push(new FormField(sId, sName, sErrorMessage, sMandatory));

};


WebForm.prototype.submitButtonClicked = function() {

    var anyErrors = false;

    for (var i = 0; i < this.field.length; i++) {

        this.field[i].clearError();

        if (!this.field[i].valid()) {

            anyErrors = true;

        };

    };

    if (!anyErrors)
        this.form.submit();

};


function FormField(sId, sName, sErrorMessage, sMandatory) {

    this.id = sId;
    this.name = sName;
    this.errorMessage = sErrorMessage;
    this.mandatory = sMandatory;
    this.errorPlacholderIdPrefix = 'errorPlaceholder';

    this.fieldElem = document.getElementById(this.id);

    this.errorPlaceholder = document.getElementById(this.errorPlacholderIdPrefix + this.id);
    this.errorPlaceholder.style.width = '194px';
    this.errorPlaceholder.style.padding = '2px';

};


FormField.prototype.showError = function() {

    var errorText = document.createElement('p');
    errorText.style.margin = '0px';
    errorText.style.color = '#d75e03';
    errorText.appendChild(document.createTextNode(this.errorMessage));

    this.errorPlaceholder.appendChild(errorText);

};


FormField.prototype.clearError = function() {

    // Remove all childnodes.
    while (this.errorPlaceholder.hasChildNodes()) {

        this.errorPlaceholder.removeChild(this.errorPlaceholder.firstChild);

    };

};


FormField.prototype.valid = function() {

    if (this.fieldElem.value == '' && this.mandatory) {
        this.showError();
        return false;
    } else {
        return true;
    };

};


// ==========================
// Write Email
// ==========================

function writeEmail(sDomain, sAlias, sDisplayText) {

    if (!sDomain)
        sDomain = 'alphapackaging.eu';

    if (!sDisplayText)
        sDisplayText = sAlias + '\u0040' + sDomain;

    document.write('<a href="mailto:' + sAlias + '\u0040' + sDomain + '">' + sDisplayText + '</a>');

};


// =================================
// Overig
// =================================

function createMethodReference(object, method) {

    if (!(method instanceof Function))
        method = object[method];

    return function() {
        method.apply(object, arguments);
    };

};

function Debug(sLine) {

    java.lang.System.out.println(sLine);

};


