﻿//Returns true if word2 is an anagram of word1; false otherwise
function AreAnagrams(word1, word2) {
    if (word1.length != word2.length) {
        return false;
    }
    //copy to arrays:
    var w1 = String2Array(word1);
    w1.sort();
    var w2 = String2Array(word2);
    w2.sort();
    for (var i = 0; i < w1.length; i++) {
        if (w2[i] != w1[i]) {
            return false;
        }
    }
    return true;
}
//Copies a string to an array
function String2Array(aWord) {
    var ret = new Array(aWord.length);
    for (var i = 0; i < aWord.length; i++) {
        ret[i] = aWord.substr(i, 1);
    }
    return ret;
}
//Calculates the elapsed time between now and the start date,
//deducting pause length multiplied by answer length, effectively giving
//user credit for the pauses between questions.
function CalcElapsedTime(startTime, pauseLength, answerLength) {
    var finishDate = new Date();
    //Calculate the elapsed time; give user credit for the pauses between questions:
    var elapsed = finishDate - startTime - (pauseLength * answerLength);
    var minutes = Math.floor(elapsed / 60000);
    var seconds = Math.floor((elapsed - (minutes * 60000)) / 1000);
    var elapsedMsg = minutes > 0 ?
                "elapsed time: " + minutes + " minutes, " + seconds + " seconds"
                : "; elapsed time: " + seconds + " seconds";
    return elapsedMsg;
}
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
        ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
function emailIsValid(email) {
    var reEmail = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    return reEmail.test(email);
}
function urlIsValid(url) {
    var reUrl = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?");
    return reUrl.test(url);
}
function logKeys() {
    //user hit a key, increment the counter
    _KeysPressed++;
}
//Fires when use moves the mouse
function timedMousePos(p) {
    try {
        if (!p) {
            p = window.event;
        }
        if (p) {
            var xPos = -1;
            var yPos = -1;
            if (p.pageX || p.pageY) {
                xPos = p.pageX;
                yPos = p.pageY;
            } else if (p.clientX || p.clientY) {
                xPos = p.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
                yPos = p.clientY + document.body.scrollTop + document.documentElement.scrollTop;
            }
            //if user moved the mouse, set the flag and stop checking
            if (xPos >= 0 || yPos >= 0) {
                _MouseMoved = xPos + yPos;
            }
        }
    } catch (e) {
        var msg = e.message;
    }
}
function binarySearch(A, value) {
    var low = 0;
    var high = A.length;
    while (low < high) {
       mid = Math.floor((low + high)/2);
       if (A[mid] < value)
           low = mid + 1; 
       else
            //can't be high = mid-1: here A[mid] >= value,
            //so high can't be < mid if A[mid] == value
            high = mid; 
    }
    // high == low, using high or low depends on taste
    if ((low < A.length) && (A[low] == value))
        return low; // found
    else
        return -1;  // not found
}
//Searches Array A for value using compFunc to compare any two entries
//A can by any kind of array; compFunc should return -1 if element a < element b,
//and return 0 if element a == b and
//return 1 if a > b
function binarySearch(A, value, compFunc) {
    var low = 0;
    var high = A.length;
    while (low < high) {
        var mid = Math.floor((low + high) / 2);
        if (compFunc(A[mid], value) < 0)
            low = mid + 1;
        else
            high = mid;
    }
    if ((low < A.length) && (compFunc(A[low], value) == 0))
        return low;
    else
        return -1;
}
//Compares two words and returns -1 if a < b, returns 0 if a == b, and returns 1 if a > b
function stringComp(a, b) {
    var len = Math.min(a.length, b.length);
    for (var i = 0; i < len; i++) {
        var aCode = a.charAt(i).charCodeAt(0);
        var bCode = b.charAt(i).charCodeAt(0);
        if (aCode < bCode) {
            return -1;
        } else if (aCode > bCode) {
            return 1;
        }
    }
    //At this point, all the letters we have compared are equal, but
    //the words may have different lengths:
    if (a.length < b.length) {
        return -1;
    } else if (a.length == b.length) {
        return 0;
    } else {
        return 1;
    }
}
//Quickly finds the index of the target in the specified array, which must be sorted. Returns -1 if not found
function oldBinarySearch(anArray, target) {
    var start = 0;
    var stop = anArray.length;
    var mid;
    while (start < stop) {
        mid = Math.floor((start + stop) / 2);
        if (anArray[mid] == target) {
            return mid;
        }
        if (anArray[mid] < target) {
            start = mid + 1;
        } else {
            stop = mid - 1;
        }
    }
    return -1;
}
function capitalize(s) {
    return s.substr(0, 1).toUpperCase() + s.substr(1).toLowerCase();
}