// Copyright 2006-2009 (C) Otto de Voogd - http://www.7is7.com/
// Original URL: http://www.7is7.com/software/games/wordsearch.html
//
// If you wish to use (a portion of) this code, whether for non-commercial or
// commercial purposes, you may do so on the following conditions:
// 
// - Proper attribution:
// Any webpage on which you use this code must properly attribute for the
// code with a clearly visible, actionable, indexable and followable link to:
// http://www.7is7.com/software/games/wordsearch.html
// The text of the link must clearly state that you are using my code, by
// either attributing me or my site, for ex: "Code by Otto de Voogd",
// "Software from 7is7.com", etc...
//
// - Create your own puzzle:
// Do not copy the actual puzzles (the way the letters are organized).
// Basically I ask you to be original and make your own puzzle but you can use
// my code to make it work.
//
// - Modification permitted:
// You are permitted to modify the code for your own personal or commercial
// use in any way you see fit, as long as you maintain this copyright notice
// and clearly indicate that you have modified the code.
// 
// - No redistribution:
// You are not permitted to redistribute this code, or any modification
// thereof, unless I have given you explicit prior permission.
// 
// - Abide by our copyright policy:
// http://www.7is7.com/otto/about/copy.html
//
// - No warranty:
// This code is made available with ABSOLUTELY NO WARRANTY.
// Use of this code is entirely at your own risk.
//
// - No unpaid support:
// Please note that there is no obligation for me to provide unpaid support
// to you. Especially when you choose to use modified code.
//
// Feedback is always appreciated.
// 

// WordSearch JavaScript functions

// Winner Message
var winmsg = 'Congratulations you have found all the words!';

var helpmsg = 'Click on the letters until you have located all the words. '
+ 'Click on the words if you want to mark them as found. '
+ 'You will be notified when you have completed the game. '
+ 'If you can not complete it you can check the solution.';


// See wordsearch.css file set colors of letters and words

// DO NOT CHANGE JAVASCRIPT BELOW THIS LINE!
var lt = unescape('%3C');
var gt = unescape('%3E');

var correcttot=0;
var wintot=0;
var gameover=0;
var show_alert=false;
var random_number=getRandomNumber();

// Get the game to play
var game = getQsVal('game');
var invx = getQsVal('invx');
var invy = getQsVal('invy');
var swixy = getQsVal('swixy');

if (-1 == game ) {
	game=random_number%Letters.length;
	random_number=getRandomNumber();
	invx=(Math.floor(random_number/32)%2)?'on':'off';
	invy=(Math.floor(random_number/64)%2)?'on':'off';
	swixy=(Math.floor(random_number/128)%2)?'on':'off';
} else {
	if (-1 == invx ) { invx='off'; }
	if (-1 == invy ) { invy='off'; }
	if (-1 == swixy ) { swixy='off'; }
}

var sizey=Letters[game][1].length, sizex=Letters[game][1][0].length;


// FUNCTIONS

// Total correct letters to complete the game
function CalcWinTot() {
	var x,y;
	for (y=0;y<sizey;y++) {
		for (x=0;x<sizex;x++) {
			wintot=wintot+Letters[game][1][y][x][1];
		}
	}
}
CalcWinTot();

function RestartGame() {
	var x,y;
	var elem;
	for (y=0;y<sizey;y++) {
		for (x=0;x<sizex;x++) {
			elem=document.getElementById('x'+x+'y'+y);
			elem.className='LetterUnSelected';
		}
	}
	var c;
	for (c=0;c<Letters[game][2].length;c++) {
		elem=document.getElementById('w'+c);
		elem.className='WordNotFound';
	}
	gameover=0;
	correcttot=0;
}

function ShowSolution() {
	var x,y;
	var idnum,elem;
	if (0==gameover) {
		for (y=0;y<sizey;y++) {
			for (x=0;x<sizex;x++) {
				idnum='x'+x+'y'+y;
				elem=document.getElementById(idnum);
				if (1!=Letters[game][1][y][x][1]) {
					if (0<=elem.className.indexOf('LetterSelected')) {
						elem.className='LetterError';
					} else {
						elem.className='LetterUnUsed';
					}
				} else {
					if (0<=elem.className.indexOf('LetterSelected')) {
						elem.className='LetterSelected';
					} else {
						elem.className='LetterMissed';
					}
				}
			}
		}
		gameover=1;
	}
}

function ClickLetter(x,y) {
	var idnum='x'+x+'y'+y;
	var elem=document.getElementById(idnum);
	var letonoff;
	if (1!=gameover) {
		if (0<=elem.className.indexOf('LetterSelected')) {
			elem.className='LetterUnSelected';
			letonoff=0;
		} else {
			elem.className='LetterSelected';
			letonoff=1;
		}
		if (Letters[game][1][y][x][1]==letonoff) {
			correcttot++;
		} else {
			correcttot--;
		}
		if (correcttot==wintot) {
			showAlert(winmsg);
			gameover=1;
		}
	}
}

function ClickWord(c) {
	var idnum='w'+c;
	var elem=document.getElementById(idnum);
	if (0<=elem.className.indexOf('WordFound')) {
		elem.className='WordNotFound';
	} else {
		elem.className='WordFound';
	}
}

function GameGrid() {
	var x,y,t;
	var a,b;
	var sizea=sizex;
	var sizeb=sizey;
	if ('on'==swixy) {sizea=sizey;sizeb=sizex;}
	document.write(lt+'table id="puzzle">');
	for (b=0;b<sizeb;b++) {
		document.write(lt+'tr>');
		for (a=0;a<sizea;a++) {
			if ('on'==invy) {y=sizeb-b-1;} else {y=b;}
			if ('on'==invx) {x=sizea-a-1;} else {x=a;}
			if ('on'==swixy) {t=x;x=y;y=t;}
			document.write(lt+'td id="x'+x+'y'+y+'" class="LetterUnSelected"><a href="javascript:ClickLetter('+x+','+y+');">'+lt+'span>'+Letters[game][1][y][x][0]+lt+'/span>'+lt+'/a>'+lt+'/td>')
		}
		document.write(lt+'/tr>\n');
	}
	document.write(lt+'/table>');
}

function GameWords() {
	var c;
	document.write(lt+'div id="wordlist"'+gt);
	for (c=0;c<Letters[game][2].length;c++) {
		document.write(lt+'a href="javascript:ClickWord('+c+');"'+gt+lt+'span class="WordNotFound" id="w'+c+'"'+gt+Letters[game][2][c]+lt+'/span'+gt+lt+'/a>\n');
	}
	document.write(lt+'/div>\n');
}

function GameSelector() {
	var c;
	document.write(lt+'form method="get" id="selgame" name="selgame" action="'+location.href+'">\n');
	document.write('Select Game: ');
	document.write(lt+'select name="game" onChange="javascript:selgame.submit();"'+gt);
	for (c=0;c<Letters.length;c++) {
		document.write(lt+'option value="'+c+'"');
		//if (game==c) {document.write(' selected');}
		document.write(gt+Letters[c][0]+lt+'/option'+gt);
	}
	document.write(lt+'/select>\n');
	setSelected(document.selgame.game,game);
	document.write('Invert x: <input type="checkbox" name="invx"');
	if ('on'==invx) {document.write(' checked');}
	document.write(' onClick="javascript:selgame.submit();">\n');
	document.write('y: <input type="checkbox" name="invy"');
	if ('on'==invy) {document.write(' checked');}
	document.write(' onClick="javascript:selgame.submit();">');
	document.write('Switch x/y: <input type="checkbox" name="swixy"');
	if ('on'==swixy) {document.write(' checked');}
	document.write(' onClick="javascript:selgame.submit();">');
	document.write(lt+'/form>\n');
}

// Get value from object
function getObjVal (obj,pre,sep,name) {
    name = name + "=";
    var start = obj.indexOf (name,0);
    if (start == -1) return start;
    if (start != 0) {
        name = pre + name;
        var start = obj.indexOf (name,0)
        if (start == -1) return start;
    }
    start += name.length;
    var end = obj.indexOf (sep,start);
    if (end == -1) end = obj.length;
    return obj.substring (start,end);
}

// Get value from query string
function getQsVal (name) {
    return getObjVal (location.search.substring(1),"&","&",name);
}

// Set selected item in select option
function setSelected(obj,val) {
 for(var i=0; i<obj.options.length; i++) {
  if(obj.options[i].value==val) {
   obj.options[i].selected=true;
   return true;
  }
 }
 return false;
}

// Change Display of given element
function setDisplay(id,setting) {
    if (
        document.getElementById(id) &&
        document.getElementById(id).style
    ) document.getElementById(id).style.display=setting;
}

// Replace text
function replaceTextById(id,str) {
	if (
		document.getElementById(id) &&
		document.getElementById(id).firstChild
	) {
		document.getElementById(id).firstChild.nodeValue=unescape(str);
	}
}

// Show popupOverlay Alert
function showAlert(str) {
	if (!show_alert) {
		show_alert=true;
		replaceTextById('popup_text',str);
		// Leave a fraction of a second for player to comtemplate the board.
		setTimeout("document.getElementById('popup_msg').style.visibility='visible';",100);
	}
}

// Hide popupOverlay Alert
function hideAlert() {
	document.getElementById('popup_msg').style.visibility='hidden';
	show_alert=false;
}

// Set global random number (between 100 and 999)
// I prefer this to Math.random
function getRandomNumber() {
	var now = new Date();
	random_number = (Math.floor(now.getTime()/100) % 900) + 100;
	return random_number;
}

var version="0.2.1_a3";


// That's All Folks!
