var T=1;
		var F=0;
		 
		var tileDimension=32;
                var marksize = 31;
                var cluemarksize=30;

                var tglHighlight=T;
                var tglFade=T;
		
		var tileimgi; var tileimgih; 
		var tileimgo; var tileimgoh; 
		var tileimgn; var tileimgnh; 

		var digitimg; var digitimg0; 
		var topborder=30 ;var leftborder=10 ;	
		
//		 output.innerHTML="asdf";  

if(gridDimension==5){		

//var grid  =document.getElementById("grid");   
//grid.style.width= "392px";
//grid.style.height= "392px";
		 topborder=7+57; leftborder=144+73;
tileimgi="url(mark-5i.gif)";tileimgo="url(mark-5o.gif)";tileimgn="url(mark-5n.gif)";
tileimgih="url(mark-5ih.gif)";tileimgoh="url(mark-5oh.gif)";tileimgnh="url(mark-5nh.gif)";
digitimg="url(clues-5-odd.gif)";digitimg0="url(clues-5-even.gif)";tileDimension=48;marksize=47;cluemarksize=46}


if(gridDimension==8){		
		 topborder=7+57; leftborder=144+73;			
 tileimgi="url(mark-8i.gif)";tileimgo="url(mark-8o.gif)";tileimgn="url(mark-8n.gif)";
tileimgih="url(mark-8ih.gif)";tileimgoh="url(mark-8oh.gif)";tileimgnh="url(mark-8nh.gif)";
digitimg="url(clues-8-odd.gif)";digitimg0="url(clues-8-even.gif)";tileDimension=32;marksize=31;cluemarksize=30}


if(gridDimension==12){		
		 topborder=10+57; leftborder=147+73;			
		 tileimgi="url(mark-12i.gif)";tileimgo="url(mark-12o.gif)";tileimgn="url(mark-12n.gif)";
tileimgih="url(mark-12ih.gif)";tileimgoh="url(mark-12oh.gif)";tileimgnh="url(mark-12nh.gif)";
digitimg="url(clues-12-odd.gif)";digitimg0="url(clues-12-even.gif)";tileDimension=21;marksize=20;cluemarksize=20;}


if(gridDimension==15){		
		 topborder=3+57; leftborder=140+73;		
tileimgi="url(mark-15i.gif)";tileimgo="url(mark-15o.gif)";tileimgn="url(mark-15n.gif)";
tileimgih="url(mark-15ih.gif)";tileimgoh="url(mark-15oh.gif)";tileimgnh="url(mark-15nh.gif)";
digitimg="url(clues-15-odd.gif)";digitimg0="url(clues-15-even.gif)";tileDimension=17;marksize=16;cluemarksize=16}


if(gridDimension==20){		
		 topborder=4+57; leftborder=141+73;			
tileimgi="url(mark-20i.gif)";tileimgo="url(mark-20o.gif)";tileimgn="url(mark-20n.gif)";
tileimgih="url(mark-20ih.gif)";tileimgoh="url(mark-20oh.gif)";tileimgnh="url(mark-20nh.gif)";
digitimg="url(clues-20-odd.gif)";digitimg0="url(clues-20-even.gif)";tileDimension=13;marksize=12;cluemarksize=12}

		var name="featurepuzzleid";
var days = 1;
var value = featurepuzzle; 
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString(); 
document.cookie = name+"="+value+expires+"; path=/";
 
var name="showviral";
var days = 1;
var value = "0"; 
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString(); 
document.cookie = name+"="+value+expires+"; path=/";

		var clueDepth =gridDimension/2;
                if(clueDepth < 3){clueDepth=3;}
                if(clueDepth ==7.5){clueDepth=8;}
		var renderSurfaceSize = tileDimension*gridDimension+clueDepth*tileDimension;

                var carray=new Array(gridDimension*2)
                for (i=0; i <gridDimension*2; i++){carray[i]=new Array(clueDepth);}
/*Im pretty sure this whole line of though was in error 070927
// if the row or col is done or not (rcdone) we have to fade or unfade it on tile click
                var rcdone = new Array(gridDimension*2)// is this row or col done?  T/F
             for (i=0; i <gridDimension*2; i++){rcdone[i]=F;}
*/   
		var VclueStartIndex=0;
		var HclueStartIndex = clueDepth*gridDimension;
		var GridStartIndex = (clueDepth*gridDimension) *2;
		var cluesOffset = clueDepth * (tileDimension);  
		var totalTiles = (gridDimension * gridDimension) + (clueDepth*gridDimension) *2; 
		var mark= "MARKEDIN";
		var  MARKEDNOT=0;
		var  MARKEDIN=1;
		var MARKEDOUT=2;

 
		var tiles = new Array();
                var highlighted = new Array(gridDimension*gridDimension);
                var currenttile=0;
		
		for(i=0;i<totalTiles;i++)tiles[i]=0; 
                
		

		    var currentSelectedColor;
		    
//  FADE  FADE  FADE  FADE  FADE  FADE  FADE  FADE  FADE  FADE  FADE  F		    
function toggleFade(){

var btnToggle = document.getElementById("togglefade");

if(tglFade==T){
tglFade=F;
btnToggle.style.backgroundImage = "url(button-play-fadeoff.gif)"
unfadeclues();
}//end if
else{
btnToggle.style.backgroundImage = "url(button-play-fadeon.gif)"
tglFade=T;
var h=0;
for(h=0;h<gridDimension;h++){
   check(h*gridDimension+h);
}//end for h
}//end else
setFadeHighlightCookie( )
}//end togglefade function

// TGLHIGH   TGLHIGH   TGLHIGH   TGLHIGH  TGLHIGH  TGLHIGH  TGLHIGH
function toggleHighlight(){
var vrowHighlight=document.getElementById("rowHighlight");  
var vcolHighlight=document.getElementById("colHighlight");   
var btnToggle = document.getElementById("togglehighlight"); 
if(tglHighlight==T){
btnToggle.style.backgroundImage = "url(button-play-highlightoff.gif)"
vcolHighlight.style.visibility="hidden";
vrowHighlight.style.visibility="hidden";
tglHighlight=F;
}//end if tglhightliht==T
else{
btnToggle.style.backgroundImage = "url(button-play-highlighton.gif)"
tglHighlight=T;

vcolHighlight.style.visibility="visible";
vrowHighlight.style.visibility="visible";
}//end if tglhightlihgt <> T
setFadeHighlightCookie( )
} //end funciotn togglehightlight


function highlight(event){
		    if(!event) event = window.event; 

if (navigator.appName == "Microsoft Internet Explorer")
{var positionyOffset = document.body.scrollTop; var positionxOffset = document.body.scrollLeft;}
else 
{var positionyOffset = window.pageYOffset; var positionxOffset =  window.pageXOffset;} 
		var cx = parseInt(event.clientX-4)-leftborder   -cluesOffset +positionxOffset; while (mod(cx,tileDimension) !=0){   cx-=1;}
		var cy = parseInt(event.clientY-4)-topborder  -cluesOffset +positionyOffset ;	

while (mod(cy,tileDimension) !=0){   cy-=1;}

if (cx>=gridDimension*(tileDimension))cx=(gridDimension-1)*(tileDimension);
if (cy>=gridDimension*(tileDimension))cy=(gridDimension-1)*(tileDimension);
			if (cx<0)cx=0;if (cy<0)cy=0;
			var tilesindex = (cx/tileDimension)+(cy/tileDimension)*gridDimension; 

//set the row Highlight position
var vrowHighlight=document.getElementById("rowHighlight");  
var vcolHighlight=document.getElementById("colHighlight");   



if(gridDimension < 10){ 
vrowHighlight.style.top= 3+topborder+clueDepth*tileDimension+(cy/tileDimension)*tileDimension+"px";
vcolHighlight.style.left= 3+leftborder+clueDepth*tileDimension+(cx/tileDimension)*tileDimension+"px";
 }
 else{ 
vrowHighlight.style.top= 3+topborder+clueDepth*tileDimension+(cy/tileDimension)*tileDimension+"px";
vcolHighlight.style.left= 3+leftborder+clueDepth*tileDimension+(cx/tileDimension)*tileDimension+"px";
 } 

//unhighlight all the squares
for(i=GridStartIndex;i<totalTiles;i++){	
                                   if(highlighted[i-GridStartIndex]==1){// the unhighlight it
                                       highlighted[i-GridStartIndex]=0;//mark it unhighted
					var thispixel = document.getElementById("p"+i);  
					if(tiles[i-GridStartIndex]==MARKEDIN){thispixel.style.backgroundImage = tileimgi; }
					if(tiles[i-GridStartIndex]==MARKEDOUT){thispixel.style.backgroundImage = tileimgo; }
					if(tiles[i-GridStartIndex]==MARKEDNOT){thispixel.style.backgroundImage = tileimgn; }
				}
                               }//end if (if it was prevously highlighted


                         //now highlight theone they're on
			if(tilesindex>=0  ){           
                                highlighted[tilesindex]=1;//mark as being highlighted
				var pixelindex = tilesindex+GridStartIndex;
				var thispixel = document.getElementById("p"+pixelindex);
				//output.innerHTML=tilesindex;
					if(tiles[tilesindex]==MARKEDIN){thispixel.style.backgroundImage = tileimgih; }
					if(tiles[tilesindex]==MARKEDOUT){thispixel.style.backgroundImage = tileimgoh; }
					if(tiles[tilesindex]==MARKEDNOT){thispixel.style.backgroundImage = tileimgnh; }
 
			}//end highlighting the current tile
		}// end highlight function
		    


		    function CheckMouseOverTiles(event){  
				
			if(!event) event = window.event;

			    if (navigator.appName == "Microsoft Internet Explorer")
				{var positionyOffset= document.body.scrollTop; var positionxOffset= document.body.scrollLeft;}
			    else {var positionyOffset= window.pageYOffset; var positionxOffset= window.pageXOffset;}
			    var buffer = document.getElementById("renderSurface");  
			    var cx = parseInt(event.clientX-4)-leftborder -cluesOffset +positionxOffset;
			    var cy = parseInt(event.clientY-4)-topborder  -cluesOffset +positionyOffset;
						
			    if (cx>gridDimension*(tileDimension)|| cx <0 ||    cy>gridDimension*(tileDimension)|| cy <0){StopPlot(event);}

		    
		}// end function CheckMouseOverTiles

  


		function tilestostring(){
			var res = "";
			for(i=GridStartIndex;i<totalTiles;i++){res=res+tiles[i-GridStartIndex];}
			return res;
		}

//CLEAR   CLEAR   CLEAR   CLEAR   CLEAR   CLEAR   CLEAR   CLEAR   		    
		function cclear(){		
			var answer = confirm("Clear this grid and start from scratch?");
			if (answer){  
				for(i=GridStartIndex;i<totalTiles;i++){	
					var thispixel = document.getElementById("p"+i);  
					markednot(thispixel,i);
					tiles[i-GridStartIndex]=MARKEDNOT;
				}
                                unfadeclues();

			}
		} 

function unfadeclues(){
       for(u = 0;u< gridDimension;u++){ 
var evenodd="";
var colnum=u;
var rownum=u;
if(mod(colnum,2)==0){evenodd="odd";}else{evenodd="even";}
 
for(c=0;c<clueDepth;c++){//use the clues array to mark the hclue tiles 
             if(carray[colnum+gridDimension ][c]>0){//don't load the 0 or blank clue graphics
		var pixelindex = VclueStartIndex+colnum*clueDepth+c;
		var thispixel = document.getElementById("p"+pixelindex);
		var dimg = "url(clues-"+ gridDimension +"-"+evenodd +carray[colnum+gridDimension][c]+".gif)";
		thispixel.style.backgroundImage=dimg;
             }//end if don't load the 0 images

             if(carray[rownum][c]>0){//don't load the 0 or blank clue graphics
		var pixelindex = HclueStartIndex+rownum*clueDepth+c;
		var thispixel = document.getElementById("p"+pixelindex);
		var dimg = "url(clues-"+ gridDimension +"-"+evenodd+carray[rownum][c]+".gif)";
		thispixel.style.backgroundImage=dimg;
             }//end if don't load the 0 images 

}//end for all clues in this col

  


}//end for u to gridDimension

}//end unfadeclues
		    
			function StartPlotOriginal(event)
			{
				var buffer = document.getElementById("renderSurface");
				 
				    buffer.onmousemove = PixelPlot;
				    buffer.onmouseup = StopPlot;
            }
	    
	    function StartPlot(event){ 
//			output.innerHTML = featurepuzzle;
		
		   if(!event) event = window.event;

if (navigator.appName == "Microsoft Internet Explorer"){var positionyOffset = document.body.scrollTop;var positionxOffset = document.body.scrollLeft;}
else {var positionyOffset = window.pageYOffset;var positionxOffset = window.pageXOffset;}

			var buffer = document.getElementById("renderSurface");  
			var cx = parseInt(event.clientX-4)-leftborder -cluesOffset +positionxOffset; while (mod(cx,tileDimension) !=0){   cx-=1;}
			var cy = parseInt(event.clientY-4)-topborder  -cluesOffset +positionyOffset; while (mod(cy,tileDimension) !=0){   cy-=1;}
			
			if (cx>=gridDimension*(tileDimension))cx=(gridDimension-1)*(tileDimension);	if (cy>=gridDimension*(tileDimension))cy=(gridDimension-1)*(tileDimension);
			
			var tilesindex = (cx/(tileDimension))+(cy/(tileDimension))*gridDimension; 
			if (tiles[tilesindex]==MARKEDIN){mark = "MARKEDOUT";}
			if (tiles[tilesindex]==MARKEDNOT){mark="MARKEDIN";}
			if (tiles[tilesindex]==MARKEDOUT){mark="MARKEDNOT";}
 			
			PixelPlot(event);
			
			buffer.onmousemove = PixelPlot; 
                        buffer.onmouseup = StopPlot; 
		
		}
            
			function StopPlot(event)
			{
				var buffer = document.getElementById("renderSurface");
				buffer.onmousemove = highlight;
				buffer.onmouseup = null;
            }
                        		

	function PixelPlot(event){
		   CheckMouseOverTiles(event);
		    if(!event) event = window.event; 
if (navigator.appName == "Microsoft Internet Explorer"){var positionyOffset = document.body.scrollTop;var positionxOffset = document.body.scrollLeft;}
else {var positionyOffset = window.pageYOffset;var positionxOffset = window.pageXOffset;}

			var cx = parseInt(event.clientX-4)-leftborder -cluesOffset +positionxOffset; 
			var cy = parseInt(event.clientY-4)-topborder -cluesOffset +positionyOffset; 
//			alert("cy" + cy + "cx" + cx + "tiledim" + tileDimension);
			while (mod(cx,tileDimension) !=0  ){ cx-=1;}
			while (mod(cy,tileDimension) !=0  ){ cy-=1;}
			if (cx>=gridDimension*tileDimension)cx=(gridDimension-1)*tileDimension;	if (cy>=gridDimension*tileDimension)cy=(gridDimension-1)*tileDimension;
			if (cx<0)cx=0;if (cy<0)cy=0;
			var tilesindex = (cx/tileDimension)+(cy/tileDimension)*gridDimension; 
			if(tilesindex>=0){
				var pixelindex = tilesindex+GridStartIndex;
				var thispixel = document.getElementById("p"+pixelindex);
				if (mark=="MARKEDIN"){ markedin(thispixel,tilesindex); }
				if (mark =="MARKEDOUT"){ markedout(thispixel,tilesindex);}		
				if (mark=="MARKEDNOT"){ markednot(thispixel,tilesindex); }		                        if (mark=="MARKEDIN" || mark=="MARKEDOUT")      {check(tilesindex); }	
			}
 
			if(CheckSolution()==1  ){
//			alert("solved");
//output.innerHTML = "SOLVED1";
StopPlot(event);
if(puzzletype == 'feature'){// this IS a feature puzzle, go to viral
			   var solutionbutton = document.getElementById("btnplay-OK");   
                           solutionbutton.style.display="inline";
			   var resetbutton = document.getElementById("btnplay-reset"); 
                           resetbutton.style.display="none";
               		   var solutiongif = document.getElementById("animatedsolution");  
                           solutiongif.style.display="block";
                           solutiongif.style.backgroundImage = "url(animated-solution-"+featurepuzzle+".gif)";
 
var name="showviral";
var days = 1;
var value = "1"; 
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString(); 
document.cookie = name+"="+value+expires+"; path=/";
 
}
if(puzzletype == 'user'){// this IS NOT a feature puzzle, its a USER PUZZLE

memberfinished(mid,pid); 
var solutionbutton = document.getElementById("btnplay-Challenge");   
                           solutionbutton.style.display="inline";
			   var resetbutton = document.getElementById("btnplay-reset"); 
                           resetbutton.style.display="none";
var votebutton = document.getElementById("btnplay-VoteRandom");   
                           votebutton .style.display="inline";
if(mid != -1){
	
var favebutton = document.getElementById("btnplay-fave");   
                           favebutton .style.display="inline";
                           newMessage('Click -Vote- to Rank this puzzle');
}
  
}
if(puzzletype == 'potd'){// this IS NOT a feature puzzle, its a POTD
memberfinished(mid,pid); 


var solutionbutton = document.getElementById("btnplay-Challenge");   
                           solutionbutton.style.display="inline";
			   var resetbutton = document.getElementById("btnplay-reset"); 
                           resetbutton.style.display="none";
var votebutton = document.getElementById("btnplay-VoteRandom");   
                           votebutton .style.display="inline";
if(mid != -1){
var favebutton = document.getElementById("btnplay-fave");   

                           favebutton .style.display="inline";

                           newMessage('Click -Vote- to Rank this puzzle');

}
  
}
if(puzzletype == 'original'){// this IS NOT a feature puzzle, its an ORIGNIAL PUZZLE
  
var solutionbutton = document.getElementById("btnplay-Original");   
                           solutionbutton.style.display="inline";
			   var resetbutton = document.getElementById("btnplay-reset"); 
                           resetbutton.style.display="none";
                           newMessage('Click the Check Mark to go back to the Design Screen');


      
}//end elseif
if(puzzletype == 'random'){// this IS NOT a feature puzzle, its a RANDOM PUZZLE
// alert ("random solution");
var solutionbutton = document.getElementById("btnplay-NextRandom");   
                           solutionbutton.style.display="inline";
var favebutton = document.getElementById("btnplay-fave");   
                           favebutton.style.display="inline";
var votebutton = document.getElementById("btnplay-VoteRandom");   
                           votebutton.style.display="inline";
			   var resetbutton = document.getElementById("btnplay-reset"); 
                           resetbutton.style.display="none";
memberfinished(mid,pid); 
      
}//end elseif

getmemberpoints(mid);                        
 }// end if puzzle is completed
		}



 
			
		function markednot(pixel,tile){
//pixel.style.className = "markednot";
pixel.style.backgroundImage = tileimgn;
tiles[tile]=MARKEDNOT;
}


//*********************************************************
// here "tile" is the 0-> gridDimension^2 index (it doesnt accomdate clues indices
		function markedin(pixel,tile){
pixel.style.backgroundImage = tileimgi;
tiles[tile]=MARKEDIN;
} 	


//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
		function markedout(pixel,tile){
pixel.style.backgroundImage = tileimgo;
tiles[tile]=MARKEDOUT;
}

           
			window.onload = function()
			{
				var buffer = document.getElementById("renderSurface");
				    buffer.onmousedown = StartPlot;  
				
				var rowHighlight= document.getElementById("rowHighlight");
				rowHighlight.style.borderColor = "rgb(222,222,222)";  
                                rowHighlight.style.borderStyle="solid";
                                rowHighlight.style.borderWidth=1+"px";
                                rowHighlight.style.height = tileDimension+"px";
                                rowHighlight.style.zIndex="80";
                                rowHighlight.style.padding="0";
                                rowHighlight.style.margin="0";
                                rowHighlight.style.position="absolute";
                                rowHighlight.style.display="block";
                                rowHighlight.style.overflow="hidden";
                                rowHighlight.style.left=4+leftborder+"px";
                                rowHighlight.style.backgroundRepeat="no-repeat";
rowHighlight.style.top=4+topborder+clueDepth*tileDimension+"px";

                                var colHighlight= document.getElementById("colHighlight");
				colHighlight.style.borderColor = "rgb(222,222,222)";  
                                colHighlight.style.borderStyle="solid";
                                colHighlight.style.borderWidth=1+"px";
                                colHighlight.style.height =tileDimension*clueDepth-2+"px";
                                colHighlight.style.padding="0";
                                colHighlight.style.margin="0";
                                colHighlight.style.zIndex="80";
                                colHighlight.style.position="absolute";
                                colHighlight.style.display="block";
                                colHighlight.style.overflow="hidden";
                                colHighlight.style.top=4+topborder+"px";
                                colHighlight.style.backgroundRepeat="no-repeat";
colHighlight.style.left=4+leftborder+clueDepth*tileDimension+"px";
                                if(gridDimension < 10){
                                colHighlight.style.width =  tileDimension-1+"px";
                                rowHighlight.style.width = tileDimension*clueDepth-2+"px";
                                }
                                else{
                                colHighlight.style.width =  tileDimension -2+"px";
                                rowHighlight.style.width = tileDimension*clueDepth -2+"px";
                                }

				
				//-------------------------------------------------------
				
					    /* var bg= document.getElementById("background");  
					     bg.style.width="100px";
						 */
             //bg.onmouseover = CheckMouseOverTiles; 
	     var grid= document.getElementById("grid");  
             grid.style.top = topborder;
             grid.style.left = leftborder; 
             grid.style.backgroundImage= "url("+gridDimension+"-grid.gif)"; 
             //output.innerHTML = "url("+gridDimension+"-grid.gif)";

			var buffer = document.getElementById("renderSurface"); 	 
	
			renderSurface.ondrag=function(){return false;};
			renderSurface.onselectstart=function(){return false;};
			buffer.style.width = renderSurfaceSize;
			buffer.style.height = renderSurfaceSize;
//                 The below are done at the end of this sub to speed things up
//			buffer.onmousedown = StartPlot;  
//			buffer.onmousemove =highlight;
			buffer.style.position="absolute";
			buffer.style.top = topborder+"px";
			buffer.style.left=leftborder+"px"; 
					
			var spx=0;
			var vcc=0;
			var hcc=0;
                        var dimg;
			
			for(i=0;i<totalTiles;i++){   
			
				var newPixel = document.createElement("div");
				newPixel.className = "pixel"; 
				newPixel.id="p"+i; 
                                newPixel.style.zIndex=2;
                                newPixel.style.padding=0;
                                newPixel.style.margin=0;
                                newPixel.style.backgroundRepeat="no-repeat";
                                

/**/				if(i>=0 && i <HclueStartIndex ){// the verticle clues 
					newPixel.style.top = mod(vcc,clueDepth)*(tileDimension)+4;//+4 to accomodate grid border
					newPixel.style.left = cluesOffset+Math.floor((vcc/clueDepth))*(tileDimension)+4;//+4 to accomodate grid border
					newPixel.style.width = marksize+ "px";
					newPixel.style.height = marksize+ "px";
                                        //if(mod(Math.floor(vcc/clueDepth),2)==0){dimg=digitimg;}
                                     // else{dimg=digitimg0;}
					//newPixel.style.backgroundImage = dimg;
//					newPixel.style.zIndex = 0;
					vcc++;
				} 
				if(i>=HclueStartIndex && i <GridStartIndex ){// the horizontal clues
					newPixel.style.top = cluesOffset+Math.floor((hcc/clueDepth))*(tileDimension)+4;//+4 to accomodate grid border
					newPixel.style.left = mod(hcc,clueDepth)*(tileDimension)+4;//+4 to accomodate grid border
					newPixel.style.width = marksize+ "px";
					newPixel.style.height = marksize+ "px";
	//				newPixel.style.zIndex = 0; 
                                        //if(mod(Math.floor(hcc/clueDepth),2)==0){dimg=digitimg;}
                                      //  else{dimg=digitimg0;}
					//newPixel.style.backgroundImage = dimg;
					hcc++;
				}
				if(i>=GridStartIndex){ 
					newPixel.style.top = cluesOffset+(Math.floor((spx/gridDimension))*(tileDimension) )+4;//+4 to accomodate grid border
					 newPixel.style.left = cluesOffset+(mod(spx,gridDimension)*(tileDimension))+4;//+4 to accomodate grid border
					newPixel.style.width = marksize+ "px";
					newPixel.style.height = marksize+ "px";
					newPixel.style.zIndex = 3;
					//newPixel.style.backgroundImage = tileimgi; 	
					//markednot(newPixel,i);	 


newPixel.style.className = "markednot";
tiles[i]=MARKEDNOT;

					spx++;   
				
				}
				buffer.appendChild(newPixel);	
                             disableSelection(document.getElementById(newPixel.id));
			}// end for totalTiles

                        var thispixel = document.getElementById("p"+GridStartIndex);
                          //an attempt to preload these mark graphics
                         markedin(thispixel ,0);
                         markedout(thispixel ,0);
                         markednot(thispixel ,0);

                        thispixel = document.getElementById("p0");
                         for(ci = clueDepth;ci>=0;ci--){//effort to preload clue graphics
                                   var dimg = "url(clues-"+ gridDimension+"-even"+ci+".gif)";
    				   thispixel.style.backgroundImage=dimg;
                                   dimg = "url(clues-"+ gridDimension +"-odd"+ci+".gif)";
 				   thispixel.style.backgroundImage=dimg;
                          }//end effort to preload clue graphics


			 makeHclues();
			makeVclues();
			
				//*********************************************
				
			buffer.onmousedown = StartPlot; 
			buffer.onmousemove =highlight;
			setFadeHightlight();
			}
			
    		function makeHclues(){
			var clues = new Array();
			//for each row
			//	go thru the row looking for ones
			//	for each one you find, inc cluenum
			//	when find a zero, write clue num in right most space
			//	continue running thru zeros
			
			for (r=0;r<gridDimension;r++){                                
                                var evenodd="";
                                if(mod(r,2)==0){evenodd="odd";}else{evenodd="even";} 

				for(i=0;i<clueDepth;i++){clues[i]=0;}//init clue array to size of clueDepth, elements all = 0
				//document.write("new row");
				var cluecount=0;
				var charindex=0;
				for(charindex=0;charindex<gridDimension;charindex++){
					if (puzzle[1].charAt(r*gridDimension+charindex) =="1"){
						 cluecount++;
					}
					else{ //we've found a zero
						if (cluecount>0){ 
							for(c=0;c<clueDepth-1;c++){clues[c]=clues[c+1];}
							clues[clueDepth-1]=cluecount; 
							cluecount=0;
						}
					}
				}//after the row is over, make sure we push the last cluecount
				if (cluecount>0){ 
					for(c=0;c<clueDepth-1;c++){clues[c]=clues[c+1];}
					clues[clueDepth-1]=cluecount; 
					cluecount=0;
				}			 
				//document.write(clues);	
				for(c=0;c<clueDepth;c++){//use the clues array to mark the hclue tiles 
                                       carray[r][c] =clues[c];
                                       if(clues[c]>0){//don't load the 0 or blank clue graphics
					var pixelindex = HclueStartIndex+r*clueDepth+c;
					var thispixel = document.getElementById("p"+pixelindex);

					var dimg = "url(clues-"+ gridDimension +"-"+evenodd+clues[c]+".gif)";
					thispixel.style.backgroundImage=dimg;
                                       }//end if don't load the 0 images
				}
			}// for each row_or_column
		}   


 function makeVclues(){ 
			var clues = new Array();
			for (c=0;c<gridDimension;c++){
                                var evenodd="";
                                if(mod(c,2)==0){evenodd="odd";}else{evenodd="even";}
				//document.write("new column");
				var cluecount=0;
				var charindex=0;
				for(i=0;i<clueDepth;i++){clues[i]=0;}//init clue array to size of clueDepth, elements all = 0
				for(charindex=0;charindex<gridDimension;charindex++){
					if (puzzle[1].charAt(c+charindex*gridDimension) =="1"){
						 cluecount++;
					}
					else{ //we've found a zero
						if (cluecount>0){ 
							for(e=0;e<clueDepth-1;e++){clues[e]=clues[e+1];}
							clues[clueDepth-1]=cluecount; 
							cluecount=0; 
						}
					}
				}//after the row is over, make sure we push the last cluecount
				if (cluecount>0){ 
					for(e=0;e<clueDepth-1;e++){clues[e]=clues[e+1];}
					clues[clueDepth-1]=cluecount; 
					cluecount=0; 
				}
				//document.write(clues);	
				for(e=0;e<clueDepth;e++){//use the clues array to mark the hclue tiles   
                                       carray[c+gridDimension][e]=clues[e];
                                   if(clues[e]>0){//don't load the 0 or blank clue graphics
					var pixelindex = VclueStartIndex+c*clueDepth+e;
					var thispixel = document.getElementById("p"+pixelindex);

					var dimg = "url(clues-"+ gridDimension +"-"+evenodd+clues[e]+".gif)";
					thispixel.style.backgroundImage=dimg;
                                       }//end if don't load the 0 images
				}				
			}
		}
  

            
                 
	    
		function CheckSolution(){

			var done = T; //assume the puzzle is correct, so we can Stop checking as soon as we find an bad mark
			for( i=0;i<puzzle[1].length;i++){
				if((tiles[i] ==MARKEDIN && puzzle[1].charAt(i)!="1") ||(tiles[i] !=MARKEDIN && puzzle[1].charAt(i)=="1")  ){
				done = F;
				}
			} 
			return(done);
		}
	    function mod(divisee,base) { return Math.round(divisee - (Math.floor(divisee/base)*base));	}
		



function newMessage(txt){  

   var bigcross = '+ + + + + + + + + + + + + + + + + + + + + + + + + + ';

	document.getElementById('lowerinfotext1').innerHTML =txt;
	document.getElementById('lowerinfotext2').innerHTML = txt;

//t=setTimeout("document.getElementById('lowerinfotext1').innerHTML =txt",1500);
//t1=setTimeout("document.getElementById('lowerinfotext2').innerHTML =txt",1500);
	
}

//_/   _/   _/   _/   _/   _/   _/   _/   _/   _/   _/   _/   _/   _/   _/   _/   _/   _/   
//tile is 0-gridDimension^2, a location on the grid.
//we'll look at that row, and that col, and check for correctness,
// and dim clues if neccessary
function check(tile){if(tglFade==T){check_h(tile);check_v(tile);}}
function check_h(tile){
//find the beginning of this row
var remainder = tile%gridDimension;
var rowstart = tile-remainder;
var rownum = rowstart/gridDimension
var done = T;
for( i=rowstart;i<rowstart+gridDimension;i++){//for this row
   if((tiles[i] ==MARKEDIN && puzzle[1].charAt(i)!="1") || (tiles[i] !=MARKEDIN && puzzle[1].charAt(i)=="1")  ){
	done = F;
   }//end if
} //end for this row
var redrawclues = F;
var fade="";
/*
if(rcdone[rownum]!= done){
   rcdone[rownum]= done;
   redrawclues = T;
   if(done==T  ){fade="f";}else{fade="";}
}
*/
redrawclues = T;
   if(done==T  ){fade="f";}else{fade="";}
if(redrawclues == T){

var evenodd="";
if(mod(rownum,2)==0){evenodd="odd";}else{evenodd="even";} 
for(c=0;c<clueDepth;c++){//use the clues array to mark the hclue tiles 
             if(carray[rownum][c]>0){//don't load the 0 or blank clue graphics
		var pixelindex = HclueStartIndex+rownum*clueDepth+c;
		var thispixel = document.getElementById("p"+pixelindex);
		var dimg = "url(clues-"+ gridDimension +"-"+evenodd+fade+carray[rownum][c]+".gif)";
		thispixel.style.backgroundImage=dimg;
             }//end if don't load the 0 images
}//end for all clues in this row 
}//end if done = T
}//end function check_h


function check_v(tile){
//find the beginning of this col
var remainder = tile%gridDimension;
var colstart = remainder;
var colnum = colstart;
var done = T;
for( i=colstart;i<gridDimension*gridDimension;i+=gridDimension){//for this row
   if((tiles[i] ==MARKEDIN && puzzle[1].charAt(i)!="1") || (tiles[i] !=MARKEDIN && puzzle[1].charAt(i)=="1")  ){
	done = F;
   }//end if
} //end for this col
var redrawclues = F;
var fade="";

/*if(rcdone[gridDimension+colnum]!= done){
   rcdone[gridDimension+colnum]= done;
   redrawclues = T;
   if(done==T ){fade="f";}else{fade="";}
}
*/
//after some use, lets remove this redrawclues var and make it redraw always
   redrawclues = T;
   if(done==T ){fade="f";}else{fade="";}


if(redrawclues == T){
var evenodd="";
if(mod(colnum,2)==0){evenodd="odd";}else{evenodd="even";} 
for(c=0;c<clueDepth;c++){//use the clues array to mark the hclue tiles 
             if(carray[colnum+gridDimension ][c]>0){//don't load the 0 or blank clue graphics
		var pixelindex = VclueStartIndex+colnum*clueDepth+c;
		var thispixel = document.getElementById("p"+pixelindex);
		var dimg = "url(clues-"+ gridDimension +"-"+evenodd+ fade +carray[colnum+gridDimension][c]+".gif)";
		thispixel.style.backgroundImage=dimg;
             }//end if don't load the 0 images
}//end for all clues in this row 
}//end if redrawclues == T
}//end function check_v




function setFadeHighlightCookie( ) {
   var name="fadehighlight";
   var days = 20;
   tglFade+="";
   tglHighlight+=""; 
   var value = tglFade+ ":" + tglHighlight;
	if (days) {var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();	}
	else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/";
}
function setPuzzleCompletedCookie(pid ) {
	var name="puzzlecompleted"; 
	var days = 0;
	pid +=""; 
	var value = pid;
	var date = new Date();
	date.setTime(date.getTime()+(3*60*1000));
	var expires = "; expires="+date.toGMTString();	

   document.cookie = name+"="+value+expires+"; path=/";
}

//useing the cookied settings, set the fade and hightlight appropriately
function setFadeHightlight(){
if(cookiefade == F){toggleFade();}
if(cookiehighlight == F){toggleHighlight();}

}

function memberfinished(mid,pid){
if(mid != -1){
	setPuzzleCompletedCookie(pid );
	ajaxmemberfinished(mid,pid);
	
}//endif
}
function getmemberpoints(mid){ajaxgetmemberpoints(mid);}

function disableSelection(element) {
element.onselectstart = function() {
return false;
};
element.unselectable = "on";
element.style.MozUserSelect = "none";
element.style.cursor = "default";
}