function FP_preloadImgs() {//v1.0
 var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();
 for(var i=0; i<a.length; i++) { d.FP_imgs[i]=new Image; d.FP_imgs[i].src=a[i]; }
}

function FP_swapImg() {//v1.0
 var doc=document,args=arguments,elm,n; doc.$imgSwaps=new Array(); for(n=2; n<args.length;
 n+=2) { elm=FP_getObjectByID(args[n]); if(elm) { doc.$imgSwaps[doc.$imgSwaps.length]=elm;
 elm.$src=elm.src; elm.src=args[n+1]; } }
}

function FP_getObjectByID(id,o) {//v1.0
 var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);
 else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;
 if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)
 for(n=0; n<c.length; n++) { el=FP_getObjectByID(id,c[n]); if(el) return el; }
 f=o.forms; if(f) for(n=0; n<f.length; n++) { els=f[n].elements;
 for(m=0; m<els.length; m++){ el=FP_getObjectByID(id,els[n]); if(el) return el; } }
 return null;
}

var messagecount=1;

function checkme()
{	

	// --------------------------
	// initializing variables
	// --------------------------
	var rows = document.getElementById('rows').value;
	var columns= document.getElementById('columns').value;
	var glevel=document.getElementById('level').value;
	var puzzlenum=document.getElementById('puzzlenumstr').value; 
	var curr_block_defected = false;
	var game_full = true;
	var game_error = false;
	var curr_block_array=new Array(9);


	bglevel=document.getElementById('levelcolor').value;

	// ---------------------------------------------
	//	re-painting all number cells with white bg
	// ---------------------------------------------
	for (currrow1=0; currrow1<rows; currrow1++) 
	{
		for (currcolumn1=0; currcolumn1<columns;currcolumn1++)
		{
			currobj=document.getElementById('tdNumber'+currrow1+'_'+currcolumn1);
			if (currobj!=null)
			{
				currobj.bgColor="#FFFFFF";
			}
		}
	}

	// ---------------------------------------------
	// checking rows for problems...
	// ---------------------------------------------
	for (currrow1=0;currrow1<rows ;currrow1++ )
	{

		currcolumn1=0;
		while (currcolumn1<columns)
		{
			// ---------------------------------------------
			// if cell diagonal , check the block 
			// associated with it 
			// ---------------------------------------------
			currobj=document.getElementById('DRow'+currrow1+'_'+currcolumn1);
			if (currobj!=null)
			{
				// ---------------------------------------------
				// clearning the curr_block_array (will be used to check 
				// in a digit appears twice in the block
				// ---------------------------------------------
				for (q=0;q<9 ;q++ )
				{
					curr_block_array[q]=0;
				}
				// ---------------------------------------------
				// getting current block desired sum
				// ---------------------------------------------
				curr_block_sum = parseInt(currobj.innerHTML);
				curr_block_defected = false;
				curr_block_full=true;
				currcolumn1++;
				curr_block_first_index=currcolumn1;

				// ---------------------------------------------
				// building the block actual sum
				// ---------------------------------------------				
				currobj=document.getElementById('N'+currrow1+'_'+currcolumn1);
				while (currobj!=null && currcolumn1<columns)
				{
					// ----------------------------------------------
					// if current cell is empty - update the game_full 
					// and curr_block_full indicators
					// ----------------------------------------------
					if (currobj.value=='')
					{
						curr_block_full = false;
						game_full = false;
					}
					// ----------------------------------------------
					// current cell not empty, handle it
					// ----------------------------------------------					
					else
					{
						curr_block_array[(currobj.value-1)]++;
						if (curr_block_array[(currobj.value-1)]>1)
						{
							// -------------------------------------------------
							// the same digit apperred twice, block is defected
							// -------------------------------------------------
							curr_block_defected=true;
						}
					}
					currcolumn1++;
					currobj=document.getElementById('N'+currrow1+'_'+currcolumn1);
				}
				curr_block_last_index=currcolumn1;
				currcolumn1--;
				
				// ----------------------------------------------
				// if the block is not defected due to digits check
				// check for sum problems
				// ----------------------------------------------
				if (!curr_block_defected)
				{
					curr_sum=0;
					for (q=0;q<9 ;q++ )
					{
						curr_sum=curr_sum+(q+1)*curr_block_array[q];
					}
					if (curr_block_full)
					{
						if (curr_sum!=curr_block_sum)
						{
							curr_block_defected=true;
						}
					}
					else
					{
						if (curr_sum>=curr_block_sum)
						{
							curr_block_defected=true;
						}
					}
				}
				// ----------------------------------------------
				//  if the block is defected init the game_error
				//  indicator, and mark the block as defected
				//  on board with it's level color
				// ----------------------------------------------
				if (curr_block_defected)
				{
					game_error = true;
					for (tty=curr_block_first_index;tty<curr_block_last_index;tty++)
					{
									currobj=document.getElementById('tdNumber'+currrow1+'_'+tty);
									if (currobj!=null)
									{
										currobj.bgColor=bglevel;
									}
					}
				}

				
			}
			currcolumn1++;
		}
	}

	// ---------------------------------------------
	// checking columns for problems...
	// ---------------------------------------------
	for (currcolumn1=0;currcolumn1<columns ;currcolumn1++ )
	{
		currrow1=0;
		while (currrow1<rows)
		{

			// ---------------------------------------------
			// if cell is diagonal, check the block 
			// associated with it 
			// ---------------------------------------------
			currobj=document.getElementById('DColumn'+currrow1+'_'+currcolumn1);
			if (currobj!=null)
			{
				// ---------------------------------------------
				// clearning the curr_block_array (will be used to check 
				// in a digit appears twice in the block
				// ---------------------------------------------
				for (q=0;q<9 ;q++ )
				{
					curr_block_array[q]=0;
				}

				// ---------------------------------------------
				// getting current block desired sum
				// ---------------------------------------------
				curr_block_sum = parseInt(currobj.innerHTML);
				curr_block_defected = false;
				curr_block_full=true;
				currrow1++;
				curr_block_first_index=currrow1;

				// ---------------------------------------------
				// building the block actual sum
				// ---------------------------------------------				
				currobj=document.getElementById('N'+currrow1+'_'+currcolumn1);
				while (currobj!=null && currrow1<rows)
				{
					// ----------------------------------------------
					// if current cell is empty - update the game_full 
					// and curr_block_full indicators
					// ----------------------------------------------
					if (currobj.value=='')
					{
						curr_block_full = false;
						game_full = false;
					}
					// ----------------------------------------------
					// current cell not empty, handle it
					// ----------------------------------------------					
					else
					{
						curr_block_array[(currobj.value-1)]++;
						if (curr_block_array[(currobj.value-1)]>1)
						{
							// -------------------------------------------------
							// the same digit apperred twice, block is defected
							// -------------------------------------------------
							curr_block_defected=true;
						}
					}
					currrow1++;
					currobj=document.getElementById('N'+currrow1+'_'+currcolumn1);
				}
				curr_block_last_index=currrow1;
				currrow1--;
				
				// ----------------------------------------------
				// if the block is not defected due to digits check
				// check for sum problems
				// ----------------------------------------------
				if (!curr_block_defected)
				{
					curr_sum=0;
					for (q=0;q<9 ;q++ )
					{
						curr_sum=curr_sum+(q+1)*curr_block_array[q];
					}

					if (curr_block_full)
					{
						if (curr_sum!=curr_block_sum)
						{
							curr_block_defected=true;
						}
					}
					else
					{
						if (curr_sum>=curr_block_sum)
						{
							curr_block_defected=true;
						}
					}
				}
				// ----------------------------------------------
				//  if the block is defected init the game_error
				//  indicator, and mark the block as defected
				//  on board with it's level color
				// ----------------------------------------------
				if (curr_block_defected)
				{
					game_error = true;
					for (ttx=curr_block_first_index;ttx<curr_block_last_index;ttx++)
					{
									currobj=document.getElementById('tdNumber'+ttx+'_'+currcolumn1);
									if (currobj!=null)
									{
										currobj.bgColor=bglevel;
									}									
					}
				}

				
			}
			currrow1++;
		}
	}

	if (!game_error && game_full)
	{
			document.getElementById('msgbox').innerHTML='<FONT COLOR=#800000><B><U>You WIN! (puzzle no. '+puzzlenum+')</U></B></FONT>';
	}
	else if (!game_error)
	{
		if (messagecount==1)
		{
			document.getElementById('msgbox').innerHTML='<FONT COLOR=#000080><B>So far So good ... (puzzle no. '+puzzlenum+')</B></FONT>';	
			messagecount=2;
		}
		else if (messagecount==2)
		{
			document.getElementById('msgbox').innerHTML='<FONT COLOR=#000080><B>keep on working ... (puzzle no. '+puzzlenum+')</B></FONT>';	
			messagecount=3;
		}
		else if (messagecount==3)
		{
			document.getElementById('msgbox').innerHTML='<FONT COLOR=#000080><B>you are doing just fine ... (puzzle no. '+puzzlenum+')</B></FONT>';	
			messagecount=4;
		}
		else if (messagecount==4)
		{
			document.getElementById('msgbox').innerHTML='<FONT COLOR=#000080><B>you have a chance to win this one ... (puzzle no. '+puzzlenum+')</B></FONT>';	
			messagecount=1;
		}

	}
	else
	{
			document.getElementById('msgbox').innerHTML='<FONT COLOR=#800000><B>Your mistakes are marked on board  (puzzle no. '+puzzlenum+')</B></FONT>';
	}
}

function restartgame()
{
	// --------------------------
	// initializing variables
	// --------------------------
	var rows = document.getElementById('rows').value;
	var columns= document.getElementById('columns').value;

	for (currrow1=0; currrow1<rows; currrow1++) {
		for (currcolumn1=0; currcolumn1<columns; currcolumn1++) {

			currobj=document.getElementById('N'+currrow1+'_'+currcolumn1);
			if (currobj!=null)
			{
				currobj.value='';
			}
		}
	}
	checkme();
	
}



function tell_friend()
{
  window.open("tellafriend2.php","_blank","STATUS=NO,TOOLBAR=NO,LOCATION=NO,DIRECTORIES=NO,COPYHISTORY=NO,MENU=NO,RESISABLE=NO,SCROLLBARS=NO,TOP=40,LEFT=20,WIDTH=391,HEIGHT=500"); 
}

function contact_us()
{
  window.open("contactus2.php","_blank","STATUS=NO,TOOLBAR=NO,LOCATION=NO,DIRECTORIES=NO,COPYHISTORY=NO,MENU=NO,RESISABLE=NO,SCROLLBARS=NO,TOP=40,LEFT=20,WIDTH=391,HEIGHT=450"); 
}
function block_list()
{
  window.open("blocklist.php","_blank","STATUS=NO,resizable=1,scrollbars=1,TOOLBAR=NO,LOCATION=NO,DIRECTORIES=NO,COPYHISTORY=NO,MENU=NO,RESISABLE=NO,TOP=40,LEFT=20,WIDTH=410,HEIGHT=500"); 
}
function learn()
{
  window.open("learn_new.php","_blank","STATUS=NO,resizable=1,scrollbars=1,TOOLBAR=NO,LOCATION=NO,DIRECTORIES=NO,COPYHISTORY=NO,MENU=NO,RESISABLE=NO,TOP=40,LEFT=20,WIDTH=700,HEIGHT=600"); 
}
function combinations()
{
  window.open("http://www.kevinpluck.net/kakuro/KakuroCombinations.html","_blank","STATUS=NO,resizable=1,scrollbars=1,TOOLBAR=NO,LOCATION=NO,DIRECTORIES=NO,COPYHISTORY=NO,MENU=NO,TOP=40,LEFT=20,WIDTH=900,HEIGHT=500"); 
}
function guestbook()
{
  window.open("guestbook.php","_blank","STATUS=NO,resizable=1,scrollbars=1,TOOLBAR=NO,LOCATION=NO,DIRECTORIES=NO,COPYHISTORY=NO,MENU=NO,TOP=40,LEFT=20,WIDTH=900,HEIGHT=500"); 
}
function buy()
{
  window.open("buy.php","_blank","STATUS=NO,TOOLBAR=NO,LOCATION=NO,DIRECTORIES=NO,COPYHISTORY=NO,MENU=NO,RESISABLE=NO,SCROLLBARS=NO,TOP=40,LEFT=20,WIDTH=391,HEIGHT=450"); 
}

function ebook2()
{
window.open("canceleBook.php", "_blank","STATUS=NO,TOOLBAR=NO,LOCATION=NO,DIRECTORIES=NO,SCROLLBARS=YES,COPYHISTORY=NO,MENU=NO,RESISABLE=NO,TOP=40,LEFT=20,WIDTH=791,HEIGHT=560"); 
}


function fixspace(currrow1)
{
	var currcolumn1=document.getElementById(currrow1).value;
	if(currcolumn1==" ")
	{
		document.getElementById(currrow1).value="";
	}
	else if (currcolumn1=="0")
	{
		alert('only number between 1 to 9 can be used');
		document.getElementById(currrow1).value="";
	}

}

