function Form(ids) {
    this.ids = ids;
}

Form.prototype.isAValidForm = function() {
    this.setupForm();
    var isValid = true;

    if (!this.isAValidInput('first_name')) {
        isValid = false;
    }
    if (!this.isAValidInput('last_name')) {
        isValid = false;
    }
    if (!this.isAValidEmail('email')) {
        isValid = false;
    }
    if (!this.isAValidConfirmation('email', 'confirm_email')) {
        isValid = false;
    }

    this.showErrorMessage(isValid);
    return isValid;
}

Form.prototype.setupForm = function() {
    this.removeRequiredFieldHints();
    this.removeErrorMessage();
}

Form.prototype.removeRequiredFieldHints = function() {
    for (var i = 0; i < this.ids.length; i++) {
        document.getElementById(this.ids[i]).removeAttribute('style');
    }
}

Form.prototype.removeErrorMessage = function() {
    var parent = document.getElementById('main_content');
    var nodes = parent.getElementsByTagName('div');

    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].id == 'success' || nodes[i].id == 'error') {
            parent.removeChild(nodes[i]);
            break;
        }
    }
}

Form.prototype.isAValidEmail = function(id) {
    if (this.isAValidInput(id)) {
        var pattern = /^.+@.+\..+$/;

        if (pattern.test(document.getElementById(id).value)) {
            return true;
        }
        else {
            this.showRequiredFieldHint(id);
        }
    }

    return false;
}

Form.prototype.isAValidInput = function(id) {
    if (document.getElementById(id).value.length > 0) {
        return true;
    }
    else {
        this.showRequiredFieldHint(id);
        return false;
    }
}

Form.prototype.isAValidConfirmation = function(input, confirmInput) {
    if (document.getElementById(input).value == document.getElementById(confirmInput).value) {
        return true;
    }
    else {
        this.showRequiredFieldHint(confirmInput);
        return false;
    }
}

Form.prototype.showRequiredFieldHint = function(id) {
    document.getElementById(id).setAttribute('style', 'border-color:red');
}

Form.prototype.showErrorMessage = function(isValid) {
    if (!isValid) {
        var div = document.createElement('div');
        div.setAttribute('id', 'error');
        div.appendChild(document.createTextNode(this.getErrorMessage()));
        var form = document.getElementsByTagName('form').item(0);
        document.getElementById('main_content').insertBefore(div, form);
    }
}

Form.prototype.getErrorMessage = function() {
    var nodes = this.getLanguageNode().getElementsByTagName('feedback');

    for (var i = 0; i < nodes.length; i++) {
        var node = nodes[i].getElementsByTagName('source')[0].firstChild;

        if (node.nodeValue == 'form_error') {
            return nodes[i].getElementsByTagName('text')[0].firstChild.nodeValue;
        }
    }

    return null;
}

Form.prototype.getLanguageNode = function() {
    var xmlDoc = new Document().loadXMLDocument('form_feedback.xml');
    var nodes = xmlDoc.getElementsByTagName('language');
    var language = document.getElementById('language').value;

    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].attributes.getNamedItem('id').value == language) {
            return nodes[i];
        }
    }

    return null;
}

