/**
 * Filename    : index.js
 * Author      : Robert Cerny
 * Created     : 04.08.2004
 * Last Change : 2007-03-17
 *
 * Functionality for the Rosegardens webpage.
 *
 * Depends on  : element.js, misc.js
 *
 * Copyright 2004 Robert Cerny
 */

CERNY.require("CO.rosegardens", "CO.navigation");

CERNY.namespace("rosegardens", CO);

function init() {
    CO.navigation.markLinksAsSelected();
    addEmail();
    _focus("username");

    var status = getURLParameterValue("status");
    if (status != null && status == "forgot") {
        showForgotForm();
    }

    _focus(getURLParameterValue("field"));
}

/**
 * Checks input and submits registration.
 */
function submitRegistration() {
    if (! checkMandatoryField("r-email",
                              "You must enter an email address!",
                              "registration")) return;
    if (! checkLength("r-email", 50, "Email address", "registration")) return;

    if (! checkMandatoryField("r-username",
                              "You must choose a username!",
                              "registration")) return;
    if (! checkLength("r-username", 30, "Username", "registration")) return;

    if (! checkMandatoryField("r-password",
                              "You must choose a password!",
                              "registration")) return;
    if (! checkLength("r-password", 15, "Password", "registration")) return;


    if (! checkMandatoryField("r-password-repeat",
                              "You must repeat your password!",
                              "registration")) return;

    var passwordField = document.getElementById("r-password");
    var password      = passwordField.value;

    var passwordRepeatField = document.getElementById("r-password-repeat");
    var passwordRepeat      = passwordRepeatField.value;

    if (password != passwordRepeat) {
        passwordField.value = "";
        passwordRepeatField.value = "";
        displayFormErrorMessage("Passwords do not match. Enter your password again!",
                                passwordField,
                                "registration");
        return;
    }

    // hide error message (maybe there were some)
    hide(document.getElementById("message"));

    document.getElementById("registration-form").submit();

}


function submitAdministration() {
    if (! checkMandatoryField("a-password-old",
                              "You must enter your current password!",
                              "administration")) return;

    if (! checkMandatoryField("a-password-new",
                              "You must enter a new password!",
                              "administration")) return;

    if (! checkLength("a-password-new", 15, "Password", "administration")) return;


    if (! checkMandatoryField("a-password-new-repeat",
                              "You must repeat your new password!",
                              "administration")) return;

    var passwordNewField = document.getElementById("a-password-new");
    var passwordNew      = passwordNewField.value;

    var passwordNewRepeatField = document.getElementById("a-password-new-repeat");
    var passwordNewRepeat      = passwordNewRepeatField.value;


    if (passwordNew != passwordNewRepeat) {
        passwordNewField.value = "";
        passwordNewRepeatField.value = "";
        displayFormErrorMessage("Passwords do not match. Enter your new password again!",
                                passwordNewField,
                                "administration");
        return;
    }

    // hide error message (maybe there were some)
    hide(document.getElementById("message"));

    document.getElementById("administration-form").submit();

}

/**
 * Submits the forgot form.
 */
function submitForgot() {
    if (! checkMandatoryField("f-username", "You must enter your username!")) return;

    if (! checkMandatoryField("f-email", "You must enter your email address!")) return;

    // hide error message (maybe there were some)
    hide(document.getElementById("message"));

    var forgotForm = document.getElementById("forgot-form");
    forgotForm.submit();
}

/**
 * Submits the login and sets the hidden parameter destination.
 *
 * destination - either "rosegarden" or "administration"; if null, then "rosegarden"
 */
function submitLogin(destination) {
    if (destination == null)
        destination = "rosegarden";

    if (! checkMandatoryField("username", "You must enter your username!")) return;
    if (! checkMandatoryField("password", "You must enter your password!")) return;

    var loginForm = document.getElementById("login-form");
    loginForm.submit();
}

/**
 * Starts the import by submitting the import form.
 */
function startImport() {

    if (! checkMandatoryField("i-file", "You must choose a file!")) return;

    var importForm = document.getElementById("import-form");
    importForm.submit();
}


function showForgotForm() {
    show(document.getElementById("forgot-form"));
    document.getElementById("f-username").focus();
}

/**
 * Displays an error message.
 *
 * errorMessage - the errorMessage to display
 */
function displayErrorMessage(errorMessage) {

    var message = document.getElementById("message");
    if (message != null) {
        message.parentNode.removeChild(message);
    }

    var errorDiv = _createElement("div", null, "id=message", "class=margin-section error");
    errorDiv.appendChild(_createElement("h2", "Error"));
    errorDiv.appendChild(_createElement("p", errorMessage));
    var margin = document.getElementById("margin");
    margin.insertBefore(errorDiv, margin.firstChild);
}

function displayFormErrorMessage(errorMessage, inputField, _displayOnly) {
    displayErrorMessage(errorMessage);
    inputField.focus();
}

/**
 * Returns true if o is an error message.
 *
 * o - the object to check
 */
function isErrorMessage(o) {
    if (isSpan(o)
        && (o.getAttribute(trans("class")) != null && o.getAttribute(trans("class")).indexOf("error-message") >= 0)) {
        return true;

    }
    return false;
}

function checkMandatoryField(fieldId, errorMessage, _displayOnly) {
    var field = document.getElementById(fieldId);
    var value = field.value;

    if (value == null || value.length == 0) {
        displayFormErrorMessage(errorMessage, field, _displayOnly);

        return false;
    }

    return true;
}

function checkLength(fieldId, max, fieldName, _displayOnly) {
    var field = document.getElementById(fieldId);
    var value = field.value;

    if (value.length > max) {
        displayFormErrorMessage(fieldName + " can be no longer than " + max + " characters.", field, _displayOnly);
        return false;
    }

    return true;
}

function checkRegExp(fieldId, regex, errorMessage, _displayOnly) {
    var field = document.getElementById(fieldId);
    var value = field.value;

    var match = new RegExp(eval(regex)).exec(value);
    if (match == null) {
        displayFormErrorMessage(errorMessage, field, _displayOnly);
        return false;
    }

    return true;
}

function checkForEnter(_event, submitFunction) {
    if (_event.keyCode == 13 && !is_opera) {
        submitFunction();
    }
}

function addEmail() {
    setEmail("support");
    setEmail("support-in-text");
}

function setEmail(_email) {
    var link = document.getElementById(_email);
    if (!isNode(link)) {
        return;
    }
    var email = "rosegardensoxscernyuvgonlinepungtcom";
    email = email.replace(/oxs/, '@');
    email = email.replace(/uvg/, '-');
    email = email.replace(/pungt/, '.');
    link.setAttribute("href", "mailto:" + email);
}

function initSupport() {
    addEmail();
    _focus("username");

    var _displayOnly = getURLParameterValue("display");
    displayOnly(_displayOnly);
}

function initAdmin() {
    addEmail();
    _focus("username");

    var _displayOnly = getURLParameterValue("display");
    displayOnly(_displayOnly);

    var data = document.getElementById("data");
    if (data == null) {
        displayErrorMessage("You must be logged in to access the Administration section.");
    }
}
