//Word Search Generator by Shawn Olson http://www.shawnolson.net
//Copyright 2004
//All Rights Reserved
//**************************************************************

	function wordPage(){
	 codeWindow = window.open("", "wordSearch", "toolbar=no, scrollbars=yes, resizable=yes, statusbar=false");
	 codeWindow.document.write("<html><head><title>Artistic Network Word Search Generator</title><link rel=\"stylesheet\" href=\"http://www.shawnolson.net/style/art.css\"/><link rel=\"stylesheet\" href=\"/style/words.css\"/><script type=\"text/javascript\" src=\"/scripts/wordSearch.js\"></script></head><body style=\"padding: 5px; background-color: white\"><h3 class=\"head\" style=\"margin-bottom: 0px\">Word Search Generator Form 2</h3><p style=\"padding-top: 0px; padding-bottom: 0px; margin-top:0px; margin-bottom: 1px\"><b>Generating Form. This may take a few moments...</b></p>");
    }
	
	function previewPage(){
	 codeWindow2 = window.open("", "wordPreview", "toolbar=no, scrollbars=yes, resizable=yes, statusbar=false");
	 codeWindow2.document.write("<html><head><title>Artistic Network Word Search Generator Preview</title><link rel=\"stylesheet\" href=\"http://www.shawnolson.net/style/art.css\"/><link rel=\"stylesheet\" href=\"/style/words.css\"/><script type=\"text/javascript\" src=\"/scripts/public_smo_scripts.js\"></script><script type=\"text/javascript\" src=\"/scripts/wordSearch.js\"></script></head><body style=\"padding: 5px; background-color: white\"><h3 class=\"head\">Word Search Generator Preview</h3><a href=\"javascript:window.print()\">Print Page</a>");
    }
	
    var copyright = '<hr/><p>This Word Search Form and all the scripts utilized in it were designed by <a href="http://www.shawnolson.net">Shawn Olson</a> and are copyrighted. You may use this script free of charge, but must include a link to <a href="http://www.shawnolson.net">www.shawnolson.net</a> if using any results from this page on another web site.</p>';

	var optlist='<option value="?" class="random">?</option><option value="A">A</option><option value="B">B</option><option value="C">C</option><option value="D">D</option><option value="E">E</option><option value="F">F</option><option value="G">G</option><option value="H">H</option><option value="I">I</option><option value="J">J</option><option value="K">K</option><option value="L">L</option><option value="M">M</option><option value="N">N</option><option value="O">O</option><option value="P">P</option><option value="Q">Q</option><option value="R">R</option><option value="S">S</option><option value="T">T</option><option value="U">U</option><option value="V">V</option><option value="W">W</option><option value="X">X</option><option value="Y">Y</option><option value="Z">Z</option>';
	
	function Menu(row,column,opt){
	 this.row = row;
	 this.col = column;
	 this.opt = opt;
	 this.makeCell = makeCell;
	 this.makeCellOut = makeCellOut;
	}
	
	function makeCell(){
	 codeWindow.document.writeln("<td><select name=\"row" + this.row +"col"+this.col+"\" style=\"font-weight: bold\">"+optlist+"</select></td>");
	}
	
	function makeCellOut(cell){
	 var findCell = codeWindow.cellForm.cell.value;
	 codeWindow2.document.writeln("<td>"+findCell+"</td>");
	}	
	
	function makeGrid(){
	 var b = false;
	 var rows = sizeForm.rows.value;
	 var cols = sizeForm.cols.value;
	 var words = sizeForm.words.value;
	 if((words == '') ||  (words.length < 10)){alert("You must create a word list for your puzzle. You must also have at least 10 letters.");}
	 if(words != '' && words.length >= 10){
	 
	 if (rows <10){ rows = 10; alert("You must have more than 10 rows. Rows set to 10.");}
	 if (cols <10){ cols = 10; alert("You must have more than 10 columns. Columns set to 10.");} 
	 if (rows >30){ rows = 30; alert("You cannot have more than 30 rows. Rows set to 30.");}
	 if (cols >30){ cols = 30; alert("You cannot have more than 30 columns. Columns set to 30.");}  
	 
	 var tot = cols * rows;
	 wordPage();
	 //codeWindow.document.writeln("<p>Rows: " + rows +"; Cols: " +cols+"; Total Cells: "+tot+"</p><h4>Processing Grid...</h4>");
	 codeWindow.document.writeln("<form name=\"cellForm\" style=\"padding:0px\"><table cellspacing=\"3\" cellpadding=\"0\" style=\"border-style: solid; border-width: 2px; background-color: navy; margin-top: 0px\">");
	 var rowA = 1;
	 var colA = 1;
	 for(var i=1; i<=tot; ++i){
	  b = false;
	  if(colA==1){codeWindow.document.writeln("<tr>");}
      
	  var Addcell = new Menu(rowA,colA,"?");
	  Addcell.makeCell();
	  
	  if (cols==colA){colA=1; ++rowA; codeWindow.document.writeln("</tr>"); b=true;}
	  if ((cols!=colA) && (b==false)){++colA;}
	 }
	 
	 codeWindow.document.writeln("<p style=\"padding: 5px; margin: 0px; padding-bottom: 2px; padding-top: 0px\">To generate a random letter in any cell, select \"<span class=\"warn\">?</span>\". To help organize your process, you can click a word below that you have entered into the chart to gray it out of the list.</p>");
	 words = wordList();
	 codeWindow.document.writeln("<h4>Use to Add/Delete your Word List</h4><textarea name=\"words\" rows=\"5\" onblur=\"this.value = stripCharacter(this.value, ' ');\">"+words+"</textarea><h4>Title (optional)</h4><input type=\"text\" name=\"title\" maxlength=\"36\"/><h4>Full HTML Document? <input type=\"checkbox\" name=\"fullHTML\" style=\"width: 15px\"/></h4>");
	 codeWindow.document.writeln("<input type=\"hidden\" name=\"rows\" value=\""+rows+"\"/><input type=\"hidden\" name=\"cols\" value=\""+cols+"\"/><input type=\"hidden\" name=\"tot\" value=\""+tot+"\"/><input class=\"btn\" type=\"button\" value=\"Preview\" onclick=\"makePreview('preview')\"/><input class=\"btn\" type=\"button\" value=\"Get Code\" onclick=\"makePreview('code')\"/></form>");
     codeWindow.document.writeln(copyright+"</body></html>");
	 }
	 
	 
	}
	
	function makePreview(type){
	 if(type==null){type='preview';}
	 var b = false;
	 var title=cellForm.title.value;
	   if(title==''){title='Word Search';}
	   var head1 = '';
	   var head2="&lt;link rel=\"stylesheet\" href=\"http://www.shawnolson.net/style/words.css\"/&gt;&lt;script type=\"text/javascript\" src=\"http://www.shawnolson.net/scripts/public_smo_scripts.js\"&gt;&lt;/script&gt;";
       var head3='';
	   var end='';
	   if(cellForm.fullHTML.checked){
	    var head1='&lt;html&gt;&lt;head&gt;&lt;title&gt;'+title+'&lt;/title&gt;';
	    var head3='&lt;/head&gt;&lt;body&gt;';
		var end='&lt;/body&gt;&lt;/html&gt;';
	   }
	   
 	 var rows = cellForm.rows.value;
	 var cols = cellForm.cols.value;
	 var tot = cols * rows;
	 previewPage();
	 if (type=='preview'){
	 codeWindow2.document.writeln("<h1>"+title+"</h1><table align=\"center\" style=\"border-style: solid; border-width: 2px\">");}
	 if (type=='code'){
	 codeWindow2.document.writeln("<h4 class=\"time\">Copy the code below and paste it into your web page.</h4><code>"+head1+head2+head3+"&lt;table align=\"center\" style=\"border-style:solid;border-width:2px; font-size: 14px\"&gt;");}
	 var rowA = 1;
	 var colA = 1;
	 var g = 0;
	 var change = '';
	 for(var i=1; i<=tot; ++i){
	  b = false;
	  if(colA==1){
	   if (type=='preview'){codeWindow2.document.writeln("<tr>");}
	   if (type=='code'){codeWindow2.document.writeln("&lt;tr&gt;");}
	  }
	  change = '';
      var theCell = "row"+rowA+"col"+colA;
	  var findCell = cellForm[g].value;
	  if (findCell == "?"){findCell=randomLetter(cellForm.words.value); change = 'Cell';}
       if (type=='preview'){codeWindow2.document.writeln("<td class=\"puzzle"+change+"\" id=\""+theCell+"\" onclick=\"changeColor('"+theCell+"','red')\">"+findCell+"</td>");}
       if (type=='code'){codeWindow2.document.writeln("&lt;td class=\"puzzle"+change+"\" id=\""+theCell+"\" onclick=\"changeColor('"+theCell+"','red')\"&gt;"+findCell+"&lt;/td&gt;");}
	  if (cols==colA){
	   colA=1;
	   ++rowA;
	   b=true;
	   if (type=='preview'){codeWindow2.document.writeln("</tr>");} 
	   if (type=='code'){codeWindow2.document.writeln("&lt;/tr&gt;");} 
	  }
	  if ((cols!=colA) && (b==false)){++colA;}
	  ++g;
	 }
	 if (type=='preview'){
	  codeWindow2.document.writeln("</table>"); wordList('preview');
	  codeWindow2.document.writeln("<hr/><p style=\"font-size: 10px; text-align: center\">This Word Search generated with the <a href=\"http://www.shawnolson.net/word_search_maker.php\" target=\"smo\">Artistic Network Word Search Generator</a>.</p>");
	 }
     if (type=='code'){
	 wordList('code');
	 
	 codeWindow2.document.writeln("&lt;hr/&gt;&lt;p style=\"font-size: 10px; text-align: center\"&gt;This Word Search generated with the &lt;a href=\"http://www.shawnolson.net/word_search_maker.php\" target=\"smo\"&gt;Artistic Network Word Search Generator&lt;/a&gt;.&lt;/p&gt;"+end+"</code>");}
     codeWindow2.document.writeln(copyright+"</body></html>");
	}

	
    function randomLetter(list){
	 list = stripCharacter(stripCharacter(list, ','), ' ');
	 var randLetter = list.charAt(Math.round(Math.random() * list.length));
	 randLetter = randLetter.toUpperCase();
	 if(randLetter == ''){randLetter = String.fromCharCode(65 + Math.round(Math.random() * 25));}
     return randLetter;
    }
	
	function showSlash(word){
	
	 if(word.className == 'isUsed'){
	  word.className = 'notUsed';
	 }
	 
	 if(word.className == 'notUsed'){
	  word.className = 'isUsed';
	 }
	
	}
	
	function wordList(type){
	var words1;
	if (type==null){
		 codeWindow.document.writeln("</table><h4>Your Words:</h4><table cellpadding=\"2\" cellspacing=\"5\"><tr><td class=\"top\">");
	 
     words1 = sizeForm.words.value;
	 }
	 	if (type=='preview'){
		 codeWindow2.document.writeln("</table><h4 style=\"text-align: center\">Words to Find:</h4><table cellpadding=\"2\" cellspacing=\"5\" align=\"center\"><tr><td class=\"top\">");
	 
     words1 = cellForm.words.value;
	 }
	 if (type=='code'){
	 words1 = cellForm.words.value;
		 codeWindow2.document.writeln("&lt;/table&gt;&lt;h4 style=\"text-align: center\"&gt;Words to Find:&lt;/h4&gt;&lt;table cellpadding=\"2\" cellspacing=\"5\" align=\"center\"&gt;&lt;tr&gt;&lt;td style=\"vertical-align: top\"&gt;");
	 }
	 words1 = words1.replace(" , ", ",");
	 words1 = words1.replace(" ", "");
	 var words = new Array();
	 words = words1.split(',');
	 words.sort();
	 for (var w = 0; w < words.length; w++){

	 if (type==null){ codeWindow.document.writeln('<span class="notUsed" id="'+words[w]+'" onclick="showSlash('+words[w]+')">'+words[w]+'</span><br/>');}
	 if (type=='preview'){ codeWindow2.document.writeln(words[w]+'<br/>');}
     if (type=='code'){ codeWindow2.document.writeln(words[w]+'&lt;br/&gt;');}
	  if ((w-9)%10 == 0){
       if (type==null){codeWindow.document.writeln("</td><td class=\"top\">");}
	   if (type=='preview'){codeWindow2.document.writeln("</td><td class=\"top\">");}
	   if (type=='code'){codeWindow2.document.writeln("&lt;/td&gt;&lt;td style=\"vertical-align: top\"&gt;");}
      }
	 }

	 if (type==null){codeWindow.document.writeln("</td></tr></table>");}
	 if (type=='preview'){codeWindow2.document.writeln("</td></tr></table>");}
	 if (type=='code'){codeWindow2.document.writeln("&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;");}
	 
	 if (type!=null){
	 var buttonPreview="<div style=\"text-align: center\"><button onclick=\"changecss('.puzzleCell','backgroundColor','black');\">Show Answers</button><button onclick=\"changecss('.puzzleCell','backgroundColor','white');\">Hide Answers</button></div>";
	 if (type=='preview'){codeWindow2.document.writeln(buttonPreview);}
	 if (type=='code'){codeWindow2.document.writeln("&lt;div style=\"text-align: center\"&gt;&lt;button onclick=\"changecss('.puzzleCell','backgroundColor','black');\"&gt;Show Answers&lt;/button&gt;&lt;button onclick=\"changecss('.puzzleCell','backgroundColor','white');\"&gt;Hide Answers&lt;/button&gt;&lt;/div&gt;");}
	 }
	 return words;
	}
	


	function stripCharacter(words,character) {
	  var spaces = words.length;
	  for(var x = 1; x<spaces; ++x){
	   words = words.replace(character, "");   
	 }
	 return words;
    }