//
// C Bill Frost 2007
// 

function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();


function QueryString_Parse()
{
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}

}

QueryString_Parse();


//
// Answer
//
function Answer_WriteHTML()
{
	document.write('<INPUT type="radio" value="' + this.id + '" name="answers"> ');
	document.write('<span  class="quizText">' + this.text + '</span><br>');
}

function Answer(aID)
{
	this.text = "New Answer";
	this.id = aID;
	this.correct = false;
	
	this.WriteHTML = Answer_WriteHTML;
}

//
// AnswerList
//

function AnswerList_NewAnswer()
{
	var a = new Answer(this.sequenceID);
	this.sequenceID++;
	this.aList[this.aList.length] = a;

	// Optional Args: text, correct
	if (arguments.length > 0)
		a.text = arguments[0];

	if (arguments.length > 1)
		a.correct = arguments[1];

	if (this.editor)
		this.editor.AnswerSectionUpdate();
		
	return a;
}

function AnswerList_Remove(id)
{
	for (var i=0;i<this.aList.length;i++)
	{
		if (this.aList[i] && this.aList[i].id == id)
		{
			this.aList[i] = null;
			break;
		}
	}
}

function AnswerList_Find(id)
{
	var result = null;
	for (var i=0;i<this.aList.length;i++)
	{
		if (this.aList[i] && this.aList[i].id == id)
		{
			result = this.aList[i];
			break;
		}
	}
	return result;
}

function AnswerList_WriteHTML()
{
	for (var i=0;i<this.aList.length;i++)
		this.aList[i].WriteHTML();
}

function AnswerList(editor)
{
	this.editor = editor;
	this.sequenceID = 0;
	this.aList = new Array();
	
	this.NewAnswer = AnswerList_NewAnswer;
	this.Remove = AnswerList_Remove;
	this.Find = AnswerList_Find;
	this.WriteHTML = AnswerList_WriteHTML;
}

//
// Question
//

function Question_NewAnswer(text,correct)
{
	this.answerList.NewAnswer(text,correct);
}

function Question_WriteHTML()
{
	document.write('<p class="quizQuestion">Q: ' + this.text + '</p>');
	this.answerList.WriteHTML();
}

function Question_GetCorrectAnswer(text,correct)
{
	var result = "";
	for (var i=0;i<this.answerList.aList.length;i++)
	{
		if (this.answerList.aList[i] && this.answerList.aList[i].correct)
		{
			result = this.answerList.aList[i].text;
			break;
		}
	}
	return result;
}

function Question(qID,editor)
{
	this.text = "New Question";
	this.id = qID;
	this.editor = editor;
		
	this.answerList = new AnswerList(editor);
	
	this.NewAnswer = Question_NewAnswer;
	this.WriteHTML = Question_WriteHTML;
	this.GetCorrectAnswer = Question_GetCorrectAnswer;
}

//
// QuestionList
//

function QuestionList_NewQuestion()
{
	var q = new Question(this.sequenceID,this.editor);
	this.sequenceID++;
	this.qList[this.qList.length] = q;
	
	// Optional Args: text
	if (arguments.length > 0)
		q.text = arguments[0];
	
	if (this.editor)
		this.editor.QuestionItemsAdd(q);
		
	return q;
}

function QuestionList_Remove(id)
{
	for (var i=0;i<this.qList.length;i++)
	{
		if (this.qList[i] && this.qList[i].id == id)
		{
			this.qList[i] = null;
			break;
		}
	}
}

function QuestionList_Find(id)
{
	var result = null;
	for (var i=0;i<this.qList.length;i++)
	{
		if (this.qList[i] && (this.qList[i].id == id))
		{
			result = this.qList[i];
			break;
		}
	}
	return result;
}

function QuestionList_WriteHTML()
{
	var index = 0;
	
	var lastQuestion = QueryString("lastQuestion");
	var ccount = QueryString("ccount");

	if (ccount == null)
		ccount = 0;
	else
		ccount = parseInt(ccount);

	document.write('<form name="quiz" method="GET" onsubmit="return QuestionListValidate(this)">');
		
	
	if (lastQuestion!=null)
	{
		lastQuestion = parseInt(lastQuestion);
		index = 1 + lastQuestion;
		var answerID = parseInt(QueryString("answers"));
		
		if (this.qList[lastQuestion].answerList.aList[answerID].correct)
		{
			document.write('<p class="quizRight">Correct!</p>');
			ccount++;
		}
		else
		{
			var correctAnswer = this.qList[lastQuestion].GetCorrectAnswer();
			document.write('<p class="quizRightWrong">That answer was incorrect.</p>');
			document.write('<p class="quizText">The correct answer to:</p>');
			document.write('<p class="quizIndent">' + this.qList[lastQuestion].text + '</p>');
			document.write('<p class="quizText">is:</p>');
			document.write('<p class="quizIndent">' + correctAnswer + '</p>');
document.write('<hr>');
		}
		
		
	}
	
	if (index < this.qList.length)
	{
		document.write('<input type="hidden" name="lastQuestion" value="' + index + '">')
		
		this.qList[index].WriteHTML();
		document.write('<p><input type="submit" name="submit" value="Next Question >>"></p>');
		document.write('<a href="mailto:info@changingstates.co.uk?subject=Hypnotherapy Quiz!&body=Hypno Quiz ' + this.qList[index].text + '">Click here to correct a question or answer</a>');
	}
	else
	{
		var score = Math.round((ccount*100)/this.qList.length);
		var scoreResults = this.ScoreResults(Math.min(Math.floor(score/10),9));
		document.write('<p class="quizText">You answered ' + ccount + ' items out of ' +
			this.qList.length + ' correctly.</p>');
		document.write('<p class="quizText">Your score is ' + score + '%. ' + '</p>');
index = index - 1;
//scoreResults 
	}
	
	document.write('<input type="hidden" name="ccount" value="' + ccount + '">')

document.write('<p>Question ' + (index + 1) + ' out of ' + this.qList.length);

	document.write('</form>');
	
}


function QuestionList_ScoreResults(index,text)
{
	// Optional Args: text
	if (arguments.length > 1)
	{
		this.scoreResults[index] = text;
	}
		
	return this.scoreResults[index];
}

function QuestionList(editor)
{
	this.sequenceID = 0;
	this.qList = new Array();
	this.scoreResults = new Array(10);
	this.editor = editor;
	
	this.NewQuestion = QuestionList_NewQuestion;
	this.Remove = QuestionList_Remove;
	this.Find = QuestionList_Find;
	this.WriteHTML = QuestionList_WriteHTML;
	this.ScoreResults = QuestionList_ScoreResults;
}

function QuestionListValidate(theForm)
{
	var validated = false;

	for (var i=0;i<theForm.answers.length;i++)
	{
		if (theForm.answers[i].checked == true)
		{
			validated = true;
			break;
		}
	}
	
	if (!validated)
		alert("Please select an answer before continuing.");
	
	return validated;
}


var gQuestionList = new QuestionList(null);

// Quiz Source Start (to edit with QuizEditor copy/paste between here and end
// -->

q = gQuestionList.NewQuestion("Hypnosis is the same as going to sleep");
q.NewAnswer("You are deep asleep throughout",false);
q.NewAnswer("Nothing like sleep at all",false);
q.NewAnswer("Like sleep but not the same",true);
q = gQuestionList.NewQuestion("When in trance you hear nothing at all");
q.NewAnswer("Yes - you have to focus intently on every word",false);
q.NewAnswer("Most people hear everything that is said, its OK for your mind to wander",true);
q.NewAnswer("Only your unconscious mind hears what is said",false);
q = gQuestionList.NewQuestion("You remember nothing after a trance");
q.NewAnswer("Amnesia is always present - you remember nothing at all",false);
q.NewAnswer("You will consciously recall everything that is appropriate for you to recall",true);
q.NewAnswer("You have no conscious memory of what was said at all",false);
q = gQuestionList.NewQuestion("A hypnotherapist can make you do anything");
q.NewAnswer("Yes - just a good therapist can make you do anything at all",false);
q.NewAnswer("No - Only suggestions that match with your values and beliefs will be accepted by the unconscious mind",true);
q.NewAnswer("Yes - just like you see on the TV",false);
q = gQuestionList.NewQuestion("Hypnotherapy is the same as stage hypnosis");
q.NewAnswer("Yes - very similar indeed",false);
q.NewAnswer("No - they are totally different - stage hypnosis arguably does not involve trance at all",true);
q.NewAnswer("No - but hypnotherapy evolved from stage hypnosis",false);
q = gQuestionList.NewQuestion("Hypnotherapy always involves regression");
q.NewAnswer("Yes - all problems are rooted in the past",false);
q.NewAnswer("No - regression is used only when other approaches have not generated the desired results",true);
q.NewAnswer("Yes - all problems are rooted in past lives",false);
q = gQuestionList.NewQuestion("Hypnotherapy only requires one session typically");
q.NewAnswer("Yes - just like waving a magic wand the problem will go away with one session only",false);
q.NewAnswer("No - one to five sessions is typical and the number of sessions varies greatly between individuals",true);
q.NewAnswer("Yes - if there's no change after one session then hypnotherapy will not work",false);
q = gQuestionList.NewQuestion("Change is linear - there will be steady progress with each session");
q.NewAnswer("No - most change takes place right at the beginning then tapers off",false);
q.NewAnswer("No - most change takes place right at the end of the process",false);
q.NewAnswer("Not necessarily - change can occur in bursts throughout the therapeutic process",true);
q = gQuestionList.NewQuestion("Hypnotherapy is dangerous");
q.NewAnswer("Yes - you could develop a major mental illness",false);
q.NewAnswer("No - hypnotherapy via a trained hypnotherapist is very safe indeed",true);
q.NewAnswer("Yes - you may never wake up again",false);
q = gQuestionList.NewQuestion("Hypnotherapy is a new psychotherapy");
q.NewAnswer("Yes - it was invented by the NLP movement",false);
q.NewAnswer("No - hypnosis has been used for hundreds if not thousands of years",true);
q.NewAnswer("Yes - came over from the USA in the 1960s",false);
q = gQuestionList.NewQuestion("Hypnotherapy is expensive");
q.NewAnswer("Hypnotherapy is generally as costly as any other form of therapy",false);
q.NewAnswer("Hypnotherapy is very expensive generally",false);
q.NewAnswer("Hypnotherapy is a highly cost effective form of therapy and is often cheaper than may other forms of therapy",true);
q = gQuestionList.NewQuestion("Hypnotherapy is not psychotherapy");
q.NewAnswer("Psychotherapy only refers to therapies that use psycho analysis",false);
q.NewAnswer("Hypnotherapy is a form of psychotherapy",true);
q.NewAnswer("Psychotherapy only means therapies that use CBT",false);
q = gQuestionList.NewQuestion("The positive effects of hypnotherapy are short acting");
q.NewAnswer("Positive results are generally long lasting if not permanent",true);
q.NewAnswer("Effects only last for a few months",false);
q.NewAnswer("Effects only last for a few days",false);
q = gQuestionList.NewQuestion("You have to go back into the past to resolve issues");
q.NewAnswer("Yes - everything has its roots in the past",false);
q.NewAnswer("Hypnotherapy means never going back to past events",false);
q.NewAnswer("No - many issues can be resolved in the current moment without needing to go back to past events. If required however hypnotherapy can enable past events to be resolved with minimal disturbance.",true);

// <-- Quiz Source End 

