//javascript JsGameQuiz class
// created by Filippo di Pisa
// 23 Jul 2007
//How to use
//include this library into the html file
//	request.stContent.stConfig.lstJavaScriptFiles = listAppend(request.stContent.stConfig.lstJavaScriptFiles,'/_hbi/_lib/JsGameQuiz.js');
//Example:
//g = new JsGameQuiz();
//g.setDb("q1&1~flamethrower communications&flame thrower communications&flamethrower&flame thrower|q2&2~mindshare");
//db map: questionId&questionName~anser1&answer2&answer3&anser4|questionId&questionName~answer1
//required html id: 
//<span id="L#i#"></span><span id="e#i#"></span><span id="wq#ii#" style="display:none">x</span><span id="rq#ii#" style="display:none">v</span>
//see also the file businesssearch/admin/games/dsp_GameQuiz.cfm
//NOTE: USE THIS LIBRARY WITH JsHash.jS LIBRARY 


function JsGameQuiz()//Constructor
{
	this.setDb             = setDb;						//public method
	this.getAnswer	       = getAnswer;					//public method
	this.__isAnswerCorrect = __isAnswerCorrect;			//private method
	this.__setLabel        = __setLabel;				//private method
	this.__setElement      = __setElement;				//private method
	this.__setScore		   = __setScore;				//private method
	this.__encode		   = __encode;
	this.__db			   = new Array();				//private var
	this.__score		   = 0;							//private var
	
}

//create database with ansers and quetions
function setDb(sData)//:void
{
	var obj               = new Object();
	var aData 		      = sData.split("|");
	var aQuestion 		  = false;
	var aQuestionAnswers  = false;
	for(var i=0; i<aData.length; i++)
	{
		obj = new Object();
		aQuestionAnswers  = aData[i].split("~"); 
		aQuestion         = aQuestionAnswers[0].split("&");
		obj.questionId	  = aQuestion[0];
		obj.question	  = aQuestion[1];
		this.__setLabel(i,obj.question,'TXT');
	    this.__setElement(i,"text");
		obj.answers       = aQuestionAnswers[1].split("&");
		this.__db.push(obj);
	}
	document.getElementById('score').innerHTML = this.__score + "/" + this.__db.length;
}

//check if the answer is correct
function getAnswer(obj)//:void
{
	if(obj.value != "")
	{
		//alert(hex_md5(obj.value.toLowerCase()));
		this.__isAnswerCorrect(obj.id,hex_md5(obj.value.toLowerCase()));
	}
}

//set question label
function __setLabel(index,label,type)//:void
{
  if(type == 'IMG')
  {
  	document.getElementById('L'+index).innerHTML = '<img src=\"'+label+'\">';
  }
  else
  {
  	document.getElementById('L'+index).innerHTML = label;
  }
}

//set question input text
function __setElement(index,type)//:void
{
   var name     = "e"+index;
   var ii		= index+1;
   var id       = "q"+ii;
   var element = "<input type="+type+" name="+name+"  id="+id+" onChange=\"g.getAnswer(this);\" />";
   document.getElementById('e'+index).innerHTML = element;
}

//check into the database if the answer is correct, and if it is set the socre
function __isAnswerCorrect(questionId,answer)//:void
{
	var result = false;
	var index  = 0;
	for(var i=0; i<this.__db.length; i++)
	{
		if(this.__db[i].questionId == questionId)
		{
			for(var j=0; j<this.__db[i].answers.length; j++)
			{
				if(this.__db[i].answers[j] == answer)
				{
					result = true;
					break;
				}
				else
				{
					result = false;
				}
			}
		}
	}
	this.__setScore(result,questionId);
}

//set score
function __setScore(b,questionId)//:Void
{
	if(b)
	{
		this.__score += 1;
		document.getElementById('score').innerHTML = this.__score + "/" + this.__db.length;
		document.getElementById('w'+questionId).style.display = 'none';
		document.getElementById('r'+questionId).style.display = 'inline';
		document.getElementById(questionId).onblur = null;
		document.getElementById(questionId).readOnly = true;
	}
	else
	{
		document.getElementById('r'+questionId).style.display = 'none';
		document.getElementById('w'+questionId).style.display = 'inline';
	}
}

function __encode(key, message)//:String
{
    var alphabet, coded, i, ch, index;

    alphabet = "qwertyuioplkjhgfdsazxcvbnm";

    coded = "";                                      
    for (i = 0; i < message.length; i++) 
    {        
        ch = message.charAt(i);                   
        index = alphabet.indexOf(ch);            
        if (index == -1) {                        
            coded = coded + ch;                 
        }                                        
        else {                                   
            coded = coded + key.charAt(index);    
        }
    }
    return coded;
}
