function displayMessage(msg, type) {
    msg = '<li>' + msg + '</li>';
    $('#notification').hide().removeClass().addClass(type);
    if ($('#notification a').length === 0) {
        var closeButton = '<a href="/jsscript/javascript_required.shtml" '
            + 'onclick="$(\'#notification\').fadeOut(2500); return false;">'
            + '    <img alt="(close)" src="/admin/images/close.png"/>'
            + '</a>';
        $('#notification').append(closeButton);
    }
    if ($('#notification ul').length > 0) {
        $('#notification ul').append(msg);
    }
    else {
        $('#notification').append('<ul>' + msg + '</ul>');
    } 
    $('#notification').fadeIn(500);
}

function displayInfo(msg) {
    displayMessage(msg, 'info');
}

function displayError(msg) {
    displayMessage(msg, 'error');
}

function displaySuccess(msg) {
    displayMessage(msg, 'success');
}

function displayWarning(msg) {
    displayMessage(msg, 'warning');
}

function promptIfIncompleteProposal(survey_id) {
    $.get('/grants_resume.go?navctrl=none&survey_id=' + survey_id, function(data) {
        if (!data) {
            return true;
        }
        var uri_matches = data.match(/\[\[(.*)\]\]/);
        var uri = RegExp.$1;
        if ( uri.match('survey_id') ) {
            window.location = '/grants.go?survey_id=' + survey_id;
        }
        else {
            var proposal_id_matches = uri.match(/proposal_id=([0-9].*)/);
            var proposal_id = RegExp.$1;
            var q = '<p class="info">You have already started a proposal for this survey.</p>'
            + 'Would you like to continue your previous proposal or start a new one?';
            $.prompt( q, {
                buttons: {
                    'Continue my previous proposal': true,
                    'Start a new proposal': false
                },
                submit: function(v, m, f) {
                    if (v) {
                        window.location = '/grants.go?proposal_id=' + proposal_id;
                    }
                    else {
                        window.location = '/grants.go?survey_id=' + survey_id;
                    }
                }
            });
            return false;
        }
    });
}

// ProjectListModule: (edit mode) Show display options according to the selected view
function displayOptions() {
    var el = document.getElementById('view_select');
    if (el.options[el.selectedIndex].value == 'page') {
        document.getElementById('fullpage_opts').style.display='block';
        document.getElementById('dashboard_opts').style.display='none';
        document.getElementById('listing_opts').style.display='none';
        document.getElementById('selection_criteria').style.display='none';
        document.getElementById('no_selection_criteria').style.display='block';
        document.getElementById('row_limit').style.display='none';
        document.getElementById('no_row_limit').style.display='block';
    }
    else if (el.options[el.selectedIndex].value == 'dashboard') {
        document.getElementById('fullpage_opts').style.display='none';
        document.getElementById('dashboard_opts').style.display='block';
        document.getElementById('listing_opts').style.display='none';
        document.getElementById('selection_criteria').style.display='block';
        document.getElementById('no_selection_criteria').style.display='none';
        document.getElementById('row_limit').style.display='block';
        document.getElementById('no_row_limit').style.display='none';
    }
    else if (el.options[el.selectedIndex].value == 'list') {
        document.getElementById('fullpage_opts').style.display='none';
        document.getElementById('dashboard_opts').style.display='none';
        document.getElementById('listing_opts').style.display='block';
        document.getElementById('selection_criteria').style.display='none';
        document.getElementById('no_selection_criteria').style.display='block';
        document.getElementById('row_limit').style.display='block';
        document.getElementById('no_row_limit').style.display='none';
    }
}

function printable() {
    var uri = window.location.href;
    uri += uri.match(/\?/) ? '&' : '?';
    uri += 'printable=Y';
    window.location = uri;
}

