
function isNumberKey(evt) {
    try {
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57) && !(charCode == 37 || charCode == 39)) {
            return false;
        }
        return true;
    }
    catch (e) {
        alert("Error in isNumberKey(evt): \nPlease notify support@counttonine.com \n" + e);
    }
}
function getFilledRatingStar() {
    try {
        if (!document.filledRatingStar) {
            document.filledRatingStar = new Image();
            document.filledRatingStar.src = "/images/rating-star-filled.gif";
        }
        return document.filledRatingStar;
    }
    catch (e) {
        alert("Error in getFilledRatingStar(): \nPlease notify support@counttonine.com \n" + e);
    }
}
function getPreratedStar() {
    try {
        if (!document.preratedStar) {
            document.preratedStar = new Image();
            document.preratedStar.src = "/images/rating-star-prerated.gif";
        }
        return document.preratedStar;
    }
    catch (e) {
        alert("Error in getPreratedStar(): \nPlease notify support@counttonine.com \n" + e);
    }
}
function getEmptyRatingStar() {
    try {
        if (!document.emptyRatingStar) {
            document.emptyRatingStar = new Image();
            document.emptyRatingStar.src = "/images/rating-star-empty.gif";
        }
        return document.emptyRatingStar;
    }
    catch (e) {
        alert("Error in getPreratedStar(): \nPlease notify support@counttonine.com \n" + e);
    }
}
function mouseOverRating(rating) {
    try {
        document.getElementById("rating-hover").innerHTML = rating;
        var intRating = 0;
        if (rating == "easiest") {
            intRating = 1;
        } else {
            if (rating == "easy") {
                intRating = 2;
            } else {
                if (rating == "medium") {
                    intRating = 3;
                } else {
                    if (rating == "hard") {
                        intRating = 4;
                    } else {
                        if (rating == "hardest") {
                            intRating = 5;
                        }
                    }
                }
            }
        }
        if (intRating >= 1) {
            document.getElementById("rating-easiest").src = getFilledRatingStar().src;
        }
        if (intRating >= 2) {
            document.getElementById("rating-easy").src = getFilledRatingStar().src;
        }
        if (intRating >= 3) {
            document.getElementById("rating-medium").src = getFilledRatingStar().src;
        }
        if (intRating >= 4) {
            document.getElementById("rating-hard").src = getFilledRatingStar().src;
        }
        if (intRating >= 5) {
            document.getElementById("rating-hardest").src = getFilledRatingStar().src;
        }
    }
    catch (e) {
        alert("Error in mouseOverRating(rating): \nPlease notify support@counttonine.com \n" + e);
    }
}
function mouseOutRating(rating) {
    try {
        document.getElementById("rating-hover").innerHTML = "";
        var intRating = 0;
        var averageRating = document.gameBoard.averageRating.value;
        if (rating == "easiest") {
            intRating = 1;
        } else {
            if (rating == "easy") {
                intRating = 2;
            } else {
                if (rating == "medium") {
                    intRating = 3;
                } else {
                    if (rating == "hard") {
                        intRating = 4;
                    } else {
                        if (rating == "hardest") {
                            intRating = 5;
                        }
                    }
                }
            }
        }
        if (intRating >= 1) {
            document.getElementById("rating-easiest").src = averageRating > 0 ? getPreratedStar().src : getEmptyRatingStar().src;
        }
        if (intRating >= 2) {
            document.getElementById("rating-easy").src = averageRating > 1.5 ? getPreratedStar().src : getEmptyRatingStar().src;
        }
        if (intRating >= 3) {
            document.getElementById("rating-medium").src = averageRating > 2.5 ? getPreratedStar().src : getEmptyRatingStar().src;
        }
        if (intRating >= 4) {
            document.getElementById("rating-hard").src = averageRating > 3.5 ? getPreratedStar().src : getEmptyRatingStar().src;
        }
        if (intRating >= 5) {
            document.getElementById("rating-hardest").src = averageRating > 4.5 ? getPreratedStar().src : getEmptyRatingStar().src;
        }
    }
    catch (e) {
        alert("Error in mouseOutRating(rating): \nPlease notify support@counttonine.com \n" + e);
    }
}
function navigate(evt, inputElement) {
    try {
        if (document.getElementById) {
            var square = inputElement.id.substring("board-squares-".length, inputElement.id.length);
            var startingRow = Math.floor(square / 9);
            var startingColumn = square % 9;
            var curInput;
            var charCode = evt.which ? evt.which : evt.keyCode;
            if (charCode == 37 && inputElement.value.length < 2) { // left arrow
                var found = false;
                while (!found) {
//            alert('startingColumn: '+startingColumn+' startingRow:'+startingRow);
                    startingColumn = (startingColumn == 0 ? 8 : startingColumn - 1);
                    square = startingRow * 9 + startingColumn;
//                alert('curSquare: '+square);
                    curInput = document.getElementById("board-squares-" + square);
                    if (curInput && curInput.type == "text") {
                        try {
                            curInput.focus();
                        }
                        catch (e) {
                        }
                        found = true;
                    }
                }
            }
            if (charCode == 38) { // up arrow
                var found = false;
                while (!found) {
                    startingRow = (startingRow == 0 ? 8 : startingRow - 1);
                    square = startingRow * 9 + startingColumn;
                    curInput = document.getElementById("board-squares-" + square);
                    if (curInput && curInput.type == "text") {
                        try {
                            curInput.focus();
                        }
                        catch (e) {
                        }
                        found = true;
                    }
                }
            }
            if (charCode == 39 && inputElement.value.length < 2) { // right arrow
                var found = false;
                while (!found) {
                    square = startingRow * 9 + (++startingColumn % 9);
                    curInput = document.getElementById("board-squares-" + square);
                    if (curInput && curInput.type == "text") {
                        try {
                            curInput.focus();
                        }
                        catch (e) {
                        }
                        found = true;
                    }
                }
            }
            if (charCode == 40) { // down arrow
                var found = false;
                while (!found) {
                    startingRow = (startingRow == 8 ? 0 : startingRow + 1);
                    square = startingRow * 9 + startingColumn;
                    curInput = document.getElementById("board-squares-" + square);
                    if (curInput && curInput.type == "text") {
                        try {
                            curInput.focus();
                        }
                        catch (e) {
                        }
                        found = true;
                    }
                }
            }
        }
    }
    catch (e) {
        alert("Error in navigate(evt, inputElement): \nPlease notify support@counttonine.com \n" + e);
    }
}
var moreErrors = true;
function configureSquare(square, originalClass) {
    try {
        square.className = originalClass + "-" + square.value.length;
        square.title = square.value;
        var cell = document.getElementById("cell-" + square.id);
        if (cell) {
            cell.className = originalClass + "-" + square.value.length;
            cell.title = square.value;
        }
        if (moreErrors) {
            checkForErrors();
            if (!moreErrors) {
                var errorMessage = document.getElementById("incorrect-message");
                if (errorMessage) {
                    pause();
                    document.gameBoard.submit();
                }
            }
        }
    }
    catch (e) {
        alert("Error in configureSquare(square, originalClass): \nPlease notify support@counttonine.com \n" + e);
    }
}
function checkForErrors() {
    try {
        var foundError = false;
        var inputs = document.gameBoard.elements;
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].className != "" && inputs[i].className.indexOf("invalid-") == 0) {
                foundError = true;
                break;
            }
        }
        moreErrors = foundError;
    }
    catch (e) {
        alert("Error in checkForErrors(): \nPlease notify support@counttonine.com \n" + e);
    }
}
var timerId = 0;
var startTime = null;
function updateTimer() {
    try {
        if (timerId) {
            clearTimeout(timerId);
            clockId = 0;
        }
        displayTime();
        timerId = setTimeout("updateTimer()", 1000);
    }
    catch (e) {
        alert("Error in updateTimer(): \nPlease notify support@counttonine.com \n" + e);
    }
}
function displayTime() {
    try {
        var curTime = new Date();
        var diffTime = curTime.getTime() - startTime.getTime();
        curTime.setTime(diffTime);
        document.gameBoard.time.value = curTime.getTime();
        var timerElement = document.timerForm.timer;
        if (document.gameBoard.showTime.value != "true") {
            document.timerForm.timer.value = "--:--";
            timerElement.title = "Show Timer";
        } else {
            timerElement.value = formatTime(curTime.getMinutes()) + ":" + formatTime(curTime.getSeconds());
            timerElement.title = "Hide Timer";
        }
    }
    catch (e) {
        alert("Error in displayTime(): \nPlease notify support@counttonine.com \n" + e);
    }
}
function toggleTimer() {
    try {
        var showTimeElement = document.gameBoard.showTime;
        if (showTimeElement.value != "true") {
            showTimeElement.value = "true";
        } else {
            showTimeElement.value = "false";
        }
    }
    catch (e) {
        alert("Error in toggleTimer(): \nPlease notify support@counttonine.com \n" + e);
    }
}
function formatTime(time) {
    try {
        if (time < 10) {
            time = "0" + time;
        }
        return time;
    }
    catch (e) {
        alert("Error in formatTime(time): \nPlease notify support@counttonine.com \n" + e);
    }
}
function start(time, paused) {
    try {
        startTime = new Date();
        if (time) {
            startTime.setTime(startTime.getTime() - time);
        }
        displayTime();
        if (!paused) {
            timerId = setTimeout("updateTimer()", 1000);
        }
    }
    catch (e) {
        alert("Error in start(time, paused): \nPlease notify support@counttonine.com \n" + e);
    }
}
function pause() {
    try {
        if (timerId) {
            clearTimeout(timerId);
            timerId = 0;
        }
        startTime = null;
    }
    catch (e) {
        alert("Error in pause(): \nPlease notify support@counttonine.com \n" + e);
    }
}
function reset() {
    try {
        startTime = null;
        document.timerForm.timer.value = "00:00";
    }
    catch (e) {
        alert("Error in reset(): \nPlease notify support@counttonine.com \n" + e);
    }
}
function renderImage(gameId, owner, imageId) {
    try {
        var imageElement = document.getElementById(imageId);
        if (imageElement.src) {
            var formElement = document.getElementById(imageId + "-form");
            imageElement.src = "displaySudokuImage.htm?gameId=" + gameId + "&owner=" + owner + "&squareSize=" + formElement.squareSize.value + "&fontSize=" + formElement.fontSize.value + "&regionBorder=" + escape(formElement.regionBorder.value) + "&squareBorder=" + escape(formElement.squareBorder.value) + "&squareBackground=" + escape(formElement.squareBackground.value) + "&fontColor=" + escape(formElement.fontColor.value) + "&fontName=" + escape(formElement.fontName.value) + "&solution=" + escape(formElement.solution.value);
        }
    }
    catch (e) {
        alert("Error in renderImage(gameId, owner, imageId): \nPlease notify support@counttonine.com \n" + e);
    }
}
function updateTotal() {
    try {
        var total = 0;
        var inputs = document.purchaseSponsor.elements;
        for (var i = 0; i < inputs.length; i++) {
            try {
                if (inputs[i].type == "text") {
                    var curValue = parseInt(inputs[i].value);
                    if (curValue + "" != "NaN") {
                        total += curValue;
                    }
                }
            }
            catch (e) {
            }
        }
        document.getElementById("total").innerHTML = "$" + total + ".00";
    }
    catch (e) {
        alert("Error in updateTotal(): \nPlease notify support@counttonine.com \n" + e);
    }
}

//
//  To Set a session cookie which expires after the browser is closed
//  setCookie ('cookieName', 'cookieValue');
//  
//  To set a cookie which expires after 24 hours
//  var now = new Date();
//  var tomorrow = new Date(now.getTime() + 1000 * 60 * 60 * 24)
//  setCookie ('cookieName', 'cookieValue', tomorrow);
//
//  To set a cookie with a path
//  setCookie ('cookieName', 'cookieValue', null, '/')
//
//  To delete a cookie you just set it with an expires date in the past:
//  var now = new Date();
//  var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
//  setCookie('cookieName', 'cookieValue', yesterday);
//  
function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
    try {
        document.cookie = escape(cookieName) + "=" + escape(cookieValue) + (expires ? "; EXPIRES=" + expires.toGMTString() : "") + (path ? "; PATH=" + path : "") + (domain ? "; DOMAIN=" + domain : "") + (secure ? "; SECURE" : "");
    }
    catch (e) {
//        alert("Error in setCookie(cookieName, cookieValue, expires, path, domain, secure): nPlease make sure cookies are enabled.  If they are please notify support@counttonine.com n" + e);
    }
}
function getCookie(cookieName) {
    try {
        document.cookie = escape(cookieName) + "=" + escape(cookieValue) + (expires ? "; EXPIRES=" + expires.toGMTString() : "") + (path ? "; PATH=" + path : "") + (domain ? "; DOMAIN=" + domain : "") + (secure ? "; SECURE" : "");
        var cookieValue = null;
        var posName = document.cookie.indexOf(escape(cookieName) + "=");
        if (posName != -1) {
            var posValue = posName + (escape(cookieName) + "=").length;
            var endPos = document.cookie.indexOf(";", posValue);
            if (endPos != -1) {
                cookieValue = unescape(document.cookie.substring(posValue, endPos));
            } else {
                cookieValue = unescape(document.cookie.substring(posValue));
            }
        }
        return cookieValue;
    }
    catch (e) {
//        alert("Error in getCookie(cookieName): nPlease make sure cookies are enabled.  If they are please notify support@counttonine.com n" + e);
    }
}
function deleteCookie(cookieName, path, domain) {
    try {
        document.cookie = escape(cookieName) + "=;" + "EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT" + (path ? "; PATH=" + path : "") + (domain ? "; DOMAIN=" + domain : "");
    }
    catch (e) {
//        alert("Error in deleteCookie(cookieName, path, domain): nPlease make sure cookies are enabled.  If they are please notify support@counttonine.com n" + e);
    }
}
function deleteGameCookies() {
    try {
        deleteCookie("gameId");
        for (var i = 0; i < 81; i++) {
            deleteCookie("boardSquares[" + i + "]");
        }
    }
    catch (e) {
        alert("Error in deleteGameCookies(): \nPlease notify support@counttonine.com \n" + e);
    }
}
/*var queryParameters = new Array();
function populateQueryString() {
    try {
        if (location.search) {
            var queryString = unescape(location.search);
	//remove ?
            if (queryString && queryString.length > 1) {
                var queryString = queryString.substring(1, queryString.length);
                if (queryString && queryString.indexOf("&") >= 0) {
                    var params = queryString.split("&");

	// creates the querystring array
                    var queryParameters = new Array();
                    var keyValuePair;
                    for (var i = 0; i < params.length; i++) {
                        keyValuePair = params[i].split("=");
                        queryParameters[keyValuePair[0]] = keyValuePair[1];
                    }
                }
            }
        }
    }
    catch (e) {
        alert("Error in populateQueryString(): nPlease notify support@counttonine.com n" + e);
    }
}
populateQueryString();*/
function populateAJAXGame(domainName) {
    try {
        if (document.gameBoard.gameId.value == "-1") {
            var url = "http://" + domainName + "/getNextAJAXGame.htm" + location.search;
            xmlLoader(url, parseGameResponse);
        }
    }
    catch (e) {
        location = "displayGoogleGame.htm" + location.search;
    }
}
function populateGame(board) {
    try {
        var cells;
        var curCell;
        cells = board.split(",");
        for (var j = 0; j < cells.length; j++) {
            var squareNumber = j;
            curCell = cells[j];
            if (curCell.length > 0) {
                var noInputCell = document.getElementById("cell-board-squares-" + squareNumber);
                noInputCell.innerHTML = "<input type=\"hidden\" value=\"" + curCell + "\" id=\"board-squares-" + squareNumber + "\" name=\"boardSquares[" + squareNumber + "]\" />" + curCell;
                noInputCell.className = "google-sudoku-number";
            } else {
                  //  var squareClass = "input-google-sudoku-number-0";
                  //  document.getElementById("cell-board-squares-" + squareNumber).innerHTML = "<input type="text" size="1" maxlength="9" align="center" valign="middle" class="input-google-sudoku-number-0" id="board-squares-" + squareNumber + "" name="boardSquares[" + squareNumber + "]" onKeyPress="return isNumberKey(event);" onKeyUp="navigate(event, this); configureSquare(this,'input-google-sudoku-number'); setCookie('boardSquares[" + squareNumber + "]',this.value)">";
            }
        }
    }
    catch (e) {
        alert("Error in populateGame(board): \nPlease notify support@counttonine.com \n" + e);
    }
}
function parseGameResponse(xmlHttp) {
    try {
        var responseText;
        try {
            responseText = xmlHttp.responseText;
        }
        catch (e) {
            responseText = "game=4,,,,9,3,,7,6,6,8,9,2,,,3,1,,,3,7,,,6,8,,,,4,5,3,,1,7,8,2,1,2,,,,,,4,3,,,8,,,9,,6,5,8,6,,,3,,,,7,7,5,4,9,,,,3,8,2,,3,6,,8,,,1,|^|gameId=66654|^|squareSize=null|^|sponsorUrl=http://www.counttonine.com/sponsor|^|sponsorName=Count to Nine|^|sponsorContent=<iframe marginwidth=\"0\" marginheight=\"0\" src=\"http://www.managebanner.com/ads/ads.asp?local=2&amp;x=1504\" frameborder=\"0\" width=\"234\" scrolling=\"no\" height=\"60\"></iframe>";
        }
        var variables = responseText.split("|^|");
        var keyValuePairs = new Array();
        for (var i = 0; i < variables.length; i++) {
            var keyValue = variables[i].split("=");
            keyValuePairs[keyValue[0]] = keyValue.length > 2 ? variables[i].substring(keyValue[0].length + 1) : keyValue[1];
        }
        if (keyValuePairs["game"]) {
            populateGame(keyValuePairs["game"]);
        }
        if (keyValuePairs["gameId"]) {
            document.getElementById("game-title").innerHTML = "game " + keyValuePairs["gameId"];
            document.getElementById("check-board-icon").href = "checkBoard.htm?gameId=" + keyValuePairs["gameId"];
            document.getElementById("print-board-icon").href = "printGame.htm?gameId=" + keyValuePairs["gameId"];
            document.getElementById("counttonine-link").href = "sudokuGame.htm?gameId=" + keyValuePairs["gameId"];
            document.gameBoard.gameId.value = keyValuePairs["gameId"];
        }
        if (keyValuePairs["sponsorUrl"]) {
            document.getElementById("sponsor-name").innerHTML = "<a href=\"" + keyValuePairs["sponsorUrl"] + "\" style=\"font-size: 10px\" target=\"_top\">" + keyValuePairs["sponsorName"] + "</a>";
        }
        if (keyValuePairs["sponsorContent"]) {
            document.getElementById("sponsor-button-content").innerHTML = keyValuePairs["sponsorContent"];
        }
        document.getElementById("loading-image").style.display = "none";
    }
    catch (e) {
        location = "displayGoogleGame.htm" + location.search;
    }
}

