﻿function wordcount(field, submitId, divId, maxWords) {
    var words = field.value.split(/\s+/);
    var cnt = words.length;

    // correct for empty words (i.e. blank) in the split
    if (0 < cnt && !words[cnt - 1]) {
        cnt--;
    }

    var el = document.getElementById(divId);

    // if maxwords is defined, then use it
    if (0 < maxWords) {
        if (cnt > maxWords) {
            el.className = "field-validation-error";
            el.innerHTML = "You have entered " + cnt + " words. A maximum of " + maxWords + " words is allowed!";
            if (field.form.elements[submitId]) {
                field.form.elements[submitId].disabled = true;
            }
        }
        else {
            el.className = "note";
            el.innerHTML = (maxWords - cnt) + " words remaining";
            if (field.form.elements[submitId]) {
                field.form.elements[submitId].disabled = false;
            }
        }
    }
    // else if no max words, just report the running count
    else {
        el.className = "note";
        el.innerHTML = (cnt) + " words entered.";
    }
}

function popitup(url) {
    newwindow = window.open(url, 'popup', 'height=450,width=965,resizable=1,scrollbars=1');
    if (window.focus) { newwindow.focus() }
    return false;
}
