var comparator = function(a, b){ return (a < b) ? -1 : (a > b); }; 
Array.prototype.binarySearch = function binarySearch(find, comparator) {
    var low = 0, high = this.length - 1, i, comparison;
    while (low <= high) {
        i = parseInt((low + high) / 2, 10);
        comparison = comparator(this[i], find);
        if (comparison < 0) { low = i + 1; continue; };
        if (comparison > 0) { high = i - 1; continue; };
        return i;
    }
    return null;
}

Array.prototype.shuffle = function() {
    // n is the number of items remaining to be shuffled.
    for(n = this.length; n > 1; n--) {
        // Pick a random element to swap with the nth element.
        k = Math.floor(Math.random()*n);  // 0 <= k <= n-1 (0-based array)
        // Swap array elements.
        tmp = this[k];
        this[k] = this[n-1];
        this[n-1] = tmp;
    }
}

function genSuggest(usedletters) {
    var maxwords = document.getElementById('maxwords').value;
    var shuffle = document.getElementById('doshuffle').checked;
    
    if(shuffle) { wordPool.shuffle(); }
    
    count = 0;
    sugWords = '';
    for(curword=0;curword<wordList.length;curword++) {
        if(count > maxwords) { break; }
        /* start checking if a word should be in the list of available words */
        validity = checkValid(wordList[curword],usedletters,true);
        if(!validity['fail']) {
            count++;
            sugWords = sugWords + wordList[curword] + ' ';
        }
    }

    return sugWords;
}

function updateHints(sent) {
    var regex = /\b[A-Za-z]+\b/g;
    hintlist['11letter'].count = 0;
    hintlist['8letter'].count = 0;
    //Check for length
    while(match = regex.exec(sent)) {
        if(match[0].length == 11) {
            hintlist['11letter'].count++;
            if (lastwordlength == 8) { hintlist['sidebyside'].pass = true; }
        } else if(match[0].length == 8) {
            hintlist['8letter'].count++;
            if (lastwordlength == 11) { hintlist['sidebyside'].pass = true; }
        }
        lastwordlength = match[0].length;
    }

    hintlist['startswithi'].pass = (sent.substring(0,2) == "I ");
    hintlist['has!!'].pass = (sent.indexOf("!!") != -1);
    hintlist['punctuation'].pass = (/^[^:,!]*(:[^:,!]*(,[^:,!]*(![^:,!]*(![^:,!]*)?)?)?)?[^:,!]*$/.test(sent));
}

function checkList(sent, usedletters) {
    var regex = /(\b[A-Za-z]+\b)./g;  //The dot ignores the last word, cause they're still typing it
    var badwords = '';
    while(match = regex.exec(sent)) {
        if(match[1].length < 9 || match[1].length == 11) {
            if(checkValid(usedletters,match[1],true)) { 
                //It's a valid length and word, check The List
                if(checkListVar.binarySearch(match[1].toLowerCase(), comparator) == null) {
                    badwords = badwords + match[1] + ' ';
                    errorlist['notinlist'].iserr = true;
                }
            } else {
                //If it's a valid length, but not word, say so
                badwords = badwords + match[1] + ' ';
                errorlist['doesntmatch'].iserr = true;
            }
        } else {
            badwords = badwords + match[1] + ' ';
            errorlist['badlength'].iserr = true;
        }
    }
    return badwords;
}
