﻿function HjSelectText(id) {
    document.getElementById(id).select();
}
   
var timeoutID = null;
function HjTextInputAb() {
    document.getElementById(HjTextInputUpdateClientID).click();
    document.getElementById('JsUpdating').style.visibility = 'visible';
}
function HjTextInputAd() {
    if (timeoutID) {
        clearTimeout(timeoutID);
    }
    
    timeoutID  = setTimeout("HjTextInputAb();", advertPreviewDelay * 1000);
}

function BindWordCounter(elementID, minWords, maxWords) {
    if (1 == 1) {
            var textbox = document.getElementById(elementID);
            var myNewString = textbox.value
            myNewString = myNewString.replace(/\n/g, " ");
            var sentenceSplit = jQuery.trim(myNewString).split('.');
            var numWords = 0
            var wordList
            for (i = 0; i < sentenceSplit.length; i++) {
                numWords = numWords + parseInt(jQuery.trim(sentenceSplit[i]).split(' ').length);
                if (i == 0) {
                    wordList = sentenceSplit[i].split(' ');
                }
                else {
                    wordList = $.merge(wordList, sentenceSplit[i].split(' '));
                }
            }
            if (textbox.value === '') {
                numWords = 0;
            }
            if (numWords < minWords || (numWords > maxWords && maxWords != 0)) {
                if (maxWords == 0) {
                    document.getElementById(elementID + 'Count').innerHTML = 'Word Count: ' + numWords;
                }
                else {
                    if (enforceMaxWordCount == 'True') {
                        if (numWords >= maxWords) {
                            document.getElementById(elementID + 'Count').innerHTML = 'Word Count: ' + maxWords + '/' + maxWords;
                        }
                        else {
                            document.getElementById(elementID + 'Count').innerHTML = 'Word Count: ' + numWords + '/' + maxWords;
                        }
                    }
                    else {
                        document.getElementById(elementID + 'Count').innerHTML = 'Word Count: ' + numWords + '/' + maxWords;
                    }
                }
                if (numWords < minWords) {
                    document.getElementById(elementID + 'Message').innerHTML = 'You have not entered enough words (Min ' + minWords + ').';
                    document.getElementById(elementID + 'Message').className = JsErrorCssClass;
                }
                else {
                    if (enforceMaxWordCount == 'True') {
                        var thisSentence = '';
                        for (j = 0; j < maxWords; j++) {
                            if (thisSentence.length > 0) {
                                thisSentence = thisSentence + " "
                            }
                            thisSentence = thisSentence + wordList[j]
                        }
                        document.getElementById(elementID).value = thisSentence + ' ';
                    }
                    else {
                        document.getElementById(elementID + 'Message').innerHTML = 'You have entered too many words (Min ' + minWords + ').';
                        document.getElementById(elementID + 'Message').className = JsErrorCssClass;
                    }
                }
            }
            else {
                if (maxWords == 0) {
                    document.getElementById(elementID + 'Count').innerHTML = 'Word Count: ' + numWords;
                }
                else {
                    document.getElementById(elementID + 'Count').innerHTML = 'Word Count: ' + numWords + '/' + maxWords;
                }
                document.getElementById(elementID + 'Count').className = '';
                document.getElementById(elementID + 'Message').innerHTML = '';
            }
    }
};
