var ___footNoteRefIndex = 0;
var ___footNoteIndex = 0;

function noteRef() {
    document.write(noteRefHTML(++___footNoteRefIndex, true));
}

function noteRepeatLastRef() {
    document.write(noteRefHTML(___footNoteRefIndex, false));
}

function noteAnchor() {
    document.write('<a name="notes_' + (++___footNoteIndex) + '"></a>');
}

function noteEndAnchor() {
    var message = document.documentElement.lang == "ru" ?
        "&#x041D;&#x0430;&#x0437;&#x0430;&#x0434; &#x043A; "
        + "&#x0442;&#x0435;&#x043A;&#x0441;&#x0442;&#x0443;" :
        "Back to the text";
    document.write('<div class="footnote_return_to_text"><a href="#notes_' + ___footNoteIndex + '_in_text">'
        + message + '</a></div>'
        + '<div class="clearer"></div>');
}

// For example:
//      indexDifference=-1 (previous footnote)
//      textPattern="See note # above" (# will be replaced by note index)
function noteRefInFootnotesHTML(indexDifference, textPattern, htmlForOutsideReference) {
    var index = ___footNoteIndex + indexDifference;
    if (index <= 0) {
        if (htmlForOutsideReference == null) {
            alert("Error in noteRefInFootnotesHTML! No HTML for outside reference");
        } else {
            document.write(htmlForOutsideReference);
        }
    } else {
        var text = textPattern.replace("#", index);
        document.write('<a href="#notes_' + index + '">' + text + '</a>');
    }
}

function noteRefHTML(index, addAnchor) {
    return '<a class="footnote"'
        + (addAnchor ? ' name="notes_' + index + '_in_text"' : '')
        + ' href="#notes_' + index + '">' + index + '</a>';
}

