// file for general purpose javascript javascript support
//////////////////////////////////////////////////////////////////////////////////////////
function CountWords (this_field, show_word_count, show_char_count, max_value, which) {
    if (show_word_count == null) {
        show_word_count = true;
    }
    if (show_char_count == null) {
        show_char_count = false;
    }
    var char_count = this_field.value.length;
    var fullStr = this_field.value + " ";
    var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
    var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
    var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9-/]+/gi;
    var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
    var splitString = cleanedStr.split(" ");
    var word_count = splitString.length -1;
    if (fullStr.length <2) {
        word_count = 0;
    }
    if (word_count == 1) {
        wordOrWords = " word";
    }
    else {
        wordOrWords = " words";
    }
    if (char_count == 1) {
        charOrChars = " character";
    } else {
        charOrChars = " characters";
    }
    if (show_word_count & show_char_count) {
        alert ("Word Count:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
    }
    else {
        if (show_word_count && word_count > 0) {
             //alert ("Word Count:  " + word_count + wordOrWords);
             var the_span = document.getElementById(which);
             the_span.innerHTML = "(Current count: " + word_count + wordOrWords + ")";
             if (word_count > max_value) {
                the_span.style.color = '#F00';
             }
             else {
                the_span.style.color = '#000';
             }
        }
        else {
            if (show_char_count) {
                alert ("Character Count:  " + char_count + charOrChars);
            }
        }
    }
    return word_count;
}

//////////////////////////////////////////////
function CountWordsMultiple (show_word_count, show_char_count, max_value, which, field_name) {
    if (show_word_count == null) {
        show_word_count = true;
    }
    if (show_char_count == null) {
        show_char_count = false;
    }

    var fields = document.getElementsByTagName("textarea");
    var total_word_count = null;
    var total_char_count = null;

    for (i=0;i<fields.length;i++) {
        var text_name = fields[i].id;

        if (text_name.match(field_name) != null) {
            var char_count = fields[i].value.length;
            var fullStr = fields[i].value + " ";
            var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
            var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
            var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9-/]+/gi;
            var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
            var splitString = cleanedStr.split(" ");
            var word_count = splitString.length -1;

            if (fullStr.length <2) {
                word_count = 0;
            }

            total_word_count = total_word_count + word_count;
            total_char_count = total_char_count + char_count;

        }

    }

    if (total_word_count == 1) {
        wordOrWords = " word";
    } else {
        wordOrWords = " words";
    }
    if (total_char_count == 1) {
        charOrChars = " character";
    } else {
        charOrChars = " characters";
    }
    if (show_word_count & show_char_count) {
        alert ("Word Count:\n" + "    " + total_word_count + wordOrWords + "\n" + "    " + total_char_count + charOrChars);
    }
    else {
        if (show_word_count && total_word_count > 0) {
             //alert ("Word Count:  " + word_count + wordOrWords);
             var the_span = document.getElementById(which);
             the_span.innerHTML = "(Current count: " + total_word_count + wordOrWords + ")";
             if (total_word_count > max_value) {
                the_span.style.color = '#F00';
             }
             else {
                the_span.style.color = '#000';
             }
        }
        else {
            if (show_char_count) {
                alert ("Character Count:  " + total_char_count + charOrChars);
            }
        }
    }
    return total_word_count;
}