maxWords = 9;
wordCount = 0;
wordNormal = new Array(maxWords);
wordScramble = new Array(maxWords);
wordUsed = new Array(maxWords);
letterUsed = new Array(maxWords);
phraseKey = new Array();
letterList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
line = "hr";
tgraph = "";
pgraph = "";
pid = "";
clue = "";
phrase = "";
puzzMade = false;

function makePuzzle() {
     puzzMade = false;

        wordCount = 0;

window.status = "Loading word list...";
	    for (var i=0; i<maxWords; i++) {
                var wd = eval("document.getWords.word" + i + ".value");
                if (wd.length>0) {
                    wordCount = wordCount+1;
                    wordNormal[i] = wd.toUpperCase(wd);
                    wordScramble[i] = "";
                    wordUsed[i] = -1;
                }
            }

window.status = "Loading puzzle clue...";
            clue = document.getWords.clue.value;
window.status = "Loading puzzle solution...";
            phrase = document.getWords.phrase.value;

            if (phrase == "")
                clue = "";

window.status = "Generating puzzle...";
            GeneratePuzzle();
}


function makePuzzPage(solve) {
    for (var c=0;c<document.getOpts.elements.length;c++)
        if (document.getOpts.elements[c].checked)
        {
            if (document.getOpts.elements[c].name == "line")
            {
                if (document.getOpts.elements[c].value == 0)
                    line = "";
                if (document.getOpts.elements[c].value == "hr")
                    line = "hr";
                if ((document.getOpts.elements[c].value != 0) && (document.getOpts.elements[c].value != "hr"))
                    line = eval("document." + document.getOpts.elements[c].value + ".src");
            }
            if (document.getOpts.elements[c].name == "title")
            {
                if (document.getOpts.elements[c].value == 0)
                    tgraph = "";
                if (document.getOpts.elements[c].value != 0)
                    tgraph = eval("document." + document.getOpts.elements[c].value + ".src");
            }
            if (document.getOpts.elements[c].name == "puzzgraph")
            {
                if (document.getOpts.elements[c].value == 0)
                    pgraph = "";
                if (document.getOpts.elements[c].value != 0)
                    pgraph = eval("document." + document.getOpts.elements[c].value + ".src");
            }
        }


window.status = "Generating page...";
        if (puzzMade)
        {
            puzzWin = window.open("", "wordsearch",'toolbar=yes,location=yes,directories=no,status=yes,scrollbars=yes,menubar=yes,resizable=yes,width=600,height=440');
            puzzWin.document.writeln("<head><title>" + document.getWords.title.value + "</title></head>");
            puzzWin.document.write("<body");
            puzzWin.document.writeln(">");

            puzzWin.document.writeln("<font face=\"Arial\"><center><table cellspacing=0 cellpadding=3><tr bgcolor=\"#FFFFFF\"><td>");
            if (tgraph != "")
                puzzWin.document.write("<td><img align=\"middle\" src=\"" + tgraph + "\"></td><td>");
            puzzWin.document.write("<font size=\"3\"><b>" + document.getWords.title.value + "</b></font>");
            if (tgraph != "")
                puzzWin.document.write("</td><td><img align=\"middle\" src=\"" + tgraph + "\">");
            puzzWin.document.writeln("</td></tr></table></center>");
            if (line == "hr")
                puzzWin.document.writeln("<hr>");
            if (line != "hr")
            {
                if (line != "")
                    puzzWin.document.writeln("<p><center><img src=\"" + line + "\"></center><p>");
                if (line == "")
                    puzzWin.document.writeln("<br><p>");
            }

window.status = "Adding instructions...";
            puzzWin.document.writeln("<font size=\"2\">Unscramble the words by placing one letter on each </font>");
            if (Browser != "IE")
                puzzWin.document.writeln("<font size=\"2\">line.</font>");
            if (Browser == "IE")
                puzzWin.document.writeln("<font size=\"2\">line.</font>");
            if (phrase != "") {
                puzzWin.document.write("<font size=\"2\">Then use the numbered </font>");
                if (Browser != "IE")
                    puzzWin.document.write("<font size=\"2\">lines </font>");
                if (Browser == "IE")
                    puzzWin.document.write("<font size=\"2\">lines </font>");
                puzzWin.document.writeln("<font size=\"2\">to complete the answer to the riddle at the end of this sheet.</font>");
            }
            puzzWin.document.writeln("<p>");

window.status = "Adding puzzle...";

        puzzWin.document.writeln("<table align=\"center\" border=0><tr><td width=\"75%\">");
            for (var x=0;x<wordCount;x++)
            {
                puzzWin.document.writeln("<font size=\"2\">" + wordScramble[x] + "</font>");
                ShowWordScramble(puzzWin, wordScramble[x], wordNormal[x], letterUsed[x], solve);
            }
        if (pgraph != "")
            puzzWin.document.writeln("</td><td><center><img src=\"" + pgraph + "\"></center>");
        puzzWin.document.writeln("</td></tr>");

        puzzWin.document.writeln("<tr><td colspan=2>");
        if (clue != "")
            puzzWin.document.write("<br><center><b><font size=\"+1\">RIDDLE:</font></b><br><center>" + clue + "</center>");
        puzzWin.document.write("<center><br><b><font size=\"+1\">");
        if (clue == "")
            if (phrase != "")
                puzzWin.document.write("SECRET PHRASE:");
        if (clue != "")
            puzzWin.document.write("ANSWER:");
        puzzWin.document.writeln("</font></b></center>");
        ShowWordScramble(puzzWin, phrase, phrase, phraseKey, solve);
        puzzWin.document.writeln("</td></tr></table>");


            if (line == "hr")
                puzzWin.document.writeln("<hr>");
            if (line != "hr")
            {
                if (line != "")
                    puzzWin.document.writeln("<p><center><img src=\"" + line + "\"></center><p>");
                if (line == "")
                    puzzWin.document.writeln("<br><p>");
            }

            AddFooter();

            puzzWin.document.close();
            puzzWin.focus();
        }
        if (! puzzMade)
            alert("You must click on the 'Create Puzzle' button first.");
window.status="";
}

function ShowWordScramble(puzzWin, ws, wn, used, solve) {
    puzzWin.document.writeln("<table border=0 align=\"center\" cellspacing=0 cellpadding=1><tr align=\"center\"><td width=5></td>");
    for (var y=0;y<wn.length;y++)
    {
        puzzWin.document.write("<td width=40 height=8");
        if (wn.substring(y,y+1) != " ") {
            if (letterList.indexOf(wn.substring(y,y+1))<0)
                puzzWin.document.write("><center>" + wn.substring(y,y+1) + "</center");
            if (letterList.indexOf(wn.substring(y,y+1))>=0) {
                if (Browser != "IE")
                    puzzWin.document.write(" bgcolor=\"#E0E0E0\"");
                puzzWin.document.write("><center>");
                if (solve)
                    puzzWin.document.write(wn.substring(y,y+1));
                if ((! solve) && (Browser != "IE"))
                    puzzWin.document.write("<font color=\"#E0E0E0\">_</font>");
                puzzWin.document.write("</center");
            }
        }
        puzzWin.document.write("></td><td width=5></td>");
    }
    puzzWin.document.writeln("</tr>");

    if (Browser == "IE") {
        puzzWin.document.writeln("<tr><td></td>");
        for (var y=0;y<wn.length;y++)
        {
            puzzWin.document.write("<td ");
            if (letterList.indexOf(wn.substring(y,y+1))>=0)
                puzzWin.document.write(" bgcolor=\"#000000\"");
            puzzWin.document.write("></td><td></td>");
        }
    }

    puzzWin.document.write("<tr align=\"center\"><td width=5></td>");
    for (var y=0;y<ws.length;y++) {
        puzzWin.document.write("<td>");
        if (used[y] > 0)
            puzzWin.document.write("<small>" + used[y] + "</small>");
        puzzWin.document.write("</td><td width=5></td>");
    }
    puzzWin.document.writeln("</tr></table><p>");
}

function GeneratePuzzle() {
    for (var x=0;x<wordCount;x++) {
window.status = "Scrambling word #" + x + ": " + wordNormal[x];
        var ts = wordNormal[x];
        letterUsed[x] = new Array(ts.length);
        for (var ch=0;ch<ts.length;ch++)
            if (letterList.indexOf(ts.substring(ch,ch+1))<0) {
                ts = ts.substring(0,ch) + ts.substring(ch+1,ts.length);
                ch = ch - 1;
            }
        do {
            var r = Math.round(Math.random() * ts.length);
            if (ts.substring(r,r+1) != " ")
                wordScramble[x] += ts.substring(r,r+1);
            ts = ts.substring(0,r) + ts.substring(r+1,ts.length);
            letterUsed[x][ts.length] = 0;
        } while (ts.length > 0);
    }

window.status = "Marking letters for solution...";
    puzzMade = true;
    var missingList = "";
    for (var x=0;x<phrase.length;x++)
        phraseKey[x] = 0;
    phrase = phrase.toUpperCase(phrase);
    var clueCount = 0;
    for (var x=0;x<phrase.length;x++)
        if (phraseKey[x] == 0) {
            if (phrase.substring(x,x+1) != " ")
                if (letterList.indexOf(phrase.substring(x,x+1))>=0) {
                    ct = 0;
                    for (var w=0;w<wordCount;w++)
                        for (var c=0;c<wordNormal[w].length;c++)
                            if (wordNormal[w].substring(c,c+1) == phrase.substring(x,x+1))
                                ct = ct + 1;
                    if (ct == 0) {
                        if (missingList.indexOf(phrase.substring(x,x+1)) < 0)
                            missingList += phrase.substring(x,x+1)
                        puzzMade = false;
                    }
                    if (ct > 0) {
                        var pick = Math.round(Math.random() * (ct-1)) + 1;
                        for (var w=0;w<wordCount;w++)
                            for (var c=0;c<wordNormal[w].length;c++)
                                if (wordNormal[w].substring(c,c+1) == phrase.substring(x,x+1)) {
                                    pick = pick - 1;
                                    if (pick < 1) {
                                        clueCount = clueCount + 1;
                                        letterUsed[w][c] = clueCount;
                                        for (var pc=0;pc<phrase.length;pc++)
                                            if (phrase.substring(pc,pc+1) == phrase.substring(x,x+1))
                                                phraseKey[pc] = clueCount;
                                        c = wordNormal[w].length + 1;
                                        pick = 999;
                                    }
                                }
                    }
                }
        }    
    if (! puzzMade) {
        alert("Your riddle solution can't be made from your list of words. Please add words with the following letters. Letter(s) missing: '" + missingList + "'");
        window.status = "Need letters: " + missingList;
    }
    if (puzzMade) {
        window.status = "Puzzle generated. Click on 'View Completed Puzzle' or 'View Answer key'.";
        getWorksheetID();
    }
}

