// Klasse for å kunne redigere publikasjoner
var KavliAssociationManager = Class.create();
KavliAssociationManager.prototype = {
    initialize: function(id) {
        this.id = id;
        this.uniq_id = 0;
    },

    _add_field: function(container, id, name, label, size, validation) {
        if (!validation) validation =
            'validate_nbstring:{max_length: 200, min_length: 1}';
        if (!size) size = 50;
        var t = new Template('<input type="hidden" value="id_#{id}_#{name}_field:#{validation}:#{label}: Is required" name="validate[]"/><div id="id_#{id}_#{name}_Container" class="formfieldcontainer"><label id="id_#{id}_#{name}_label" for="id_#{id}_#{name}_field">#{label}:</label><input class="formfield" type="text" size="#{size}" value="" name="#{name}[]" id="id_#{id}_#{name}_field" /></div>');
        container.insert(t.evaluate({'id': id, 'name': name, 'label': label, 'size': size, 'validation': validation}));
    },

    add_publication: function() {
        // Author(s), Title, Journal, Year of publication
        var container = $(this.id);
        this.uniq_id++;
        var elem = new Element('div', {'class': 'publication_container', 'id': 'publication_'+this.uniq_id});
        this._add_field(elem, this.uniq_id, 'publication_author', 'Author(s)', '50');
        this._add_field(elem, this.uniq_id, 'publication_title', 'Title', '50');
        this._add_field(elem, this.uniq_id, 'publication_journal', 'Journal', '50');
        this._add_field(elem, this.uniq_id, 'publication_year_of_publication', 'Year of publication', '4');
        elem.insert('<div class="publication_btn"><input type="button" value="Remove this publication" onclick="kavli_association_mgr.remove_publication(\''+this.uniq_id+'\');" /></div>');
        container.insert({bottom: elem});
    },

    remove_publication: function(id) {
        var elem;
        if ((elem = $('publication_'+id))) {
            elem.remove();
        }
        return false;
    },

    add_referee: function() {
        // Title/Name, Institution, Phone, E-mail
        var container = $(this.id);
        this.uniq_id++;
        var elem = new Element('div', {'class': 'referees_container', 'id': 'referees_'+this.uniq_id});
        this._add_field(elem, this.uniq_id, 'referees_name', 'Name', '50');
        this._add_field(elem, this.uniq_id, 'referees_title', 'Title', '50');
        this._add_field(elem, this.uniq_id, 'referees_institution', 'Institution', '50');
        this._add_field(elem, this.uniq_id, 'referees_phone', 'Phone', '50');
        this._add_field(elem, this.uniq_id, 'referees_email', 'E-mail', '50', 'validate_email');
        elem.insert('<div class="publication_btn"><input type="button" value="Remove this referee" onclick="kavli_association_mgr.remove_referee(\''+this.uniq_id+'\');" /></div>');
        container.insert({bottom: elem});
    },

    remove_referee: function(id) {
        var elem;
        if ((elem = $('referees_'+id))) {
            elem.remove();
        }
        return false;
    }
};

// Utility functions
var update_maxlen = function(field, max_length) {
    if (!max_length) {
        return false;
    }
    var maxlenval = $F(field.id).strip().length;
    $(field.id+'_max_length').value = maxlenval;
    var max_length_msg = $(field.id+'_msg');
    if (maxlenval > max_length) {
        Element.removeClassName(max_length_msg, 'longer-than-maxlen');
        Element.addClassName(max_length_msg, 'shorter-than-maxlen');
    } else {
        Element.removeClassName(max_length_msg, 'shorter-than-maxlen');
        Element.addClassName(max_length_msg, 'longer-than-maxlen');
    }
    return true;
}

var sharedprize_changed = function() {
    var elem = $('sharedprize_field');
    if (!elem) return false;
    if (elem.checked) {
        $('sharedwith_field').enable();
    } else {
        $('sharedwith_field').disable();
        $('sharedwith_field').value = '';
    }
}
