//
// QueryString
//

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="quizRightWrong"></p>');
			ccount++;
		}
		else
		{
			var correctAnswer = this.qList[lastQuestion].GetCorrectAnswer();
			document.write('<p class="quizRightWrong"></p>');
		}
		
		
	}
	
	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>')
	}
	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 + ' questions out of ' +
			this.qList.length + ' Yes.</p>');
		document.write('<p class="quizText">Your score is ' + score + '%. ' + scoreResults + '</p>');
	}
	
	document.write('<input type="hidden" name="ccount" value="' + ccount + '">')
	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
// -->
gQuestionList.ScoreResults(0,"You show very few or no symptoms of social anxiety disorder. However, if you are concerned about your symptoms, you should discuss them with your doctor and/or read more about social anxiety disorder below.");
gQuestionList.ScoreResults(1,"You show very few symptoms of social anxiety disorder. However, if you are concerned about your symptoms, you should discuss them with your doctor and/or read more about social anxiety disorder below.");
gQuestionList.ScoreResults(2,"You indicate that you have some symptoms of social anxiety disorder. Learn more about how to cope with your fears with the information below and be sure to talk to a healthcare provider if these symptoms are intefering with any normal routines.");
gQuestionList.ScoreResults(3,"You indicate that you have some symptoms of social anxiety disorder. Learn more about how to cope with your fears with the information below and be sure to talk to a healthcare provider if these symptoms are intefering with any normal routines.");
gQuestionList.ScoreResults(4,"You indicate that you have some symptoms of social anxiety disorder. Learn more about how to cope with your fears with the information below and be sure to talk to a healthcare provider if these symptoms are intefering with any normal routines.");
gQuestionList.ScoreResults(5,"You have several symptoms of social anxiety disorder and they may be interfering in your normal routines. Please talk to your doctor or another healthcare provider. You may learn more about social anxiety disorder below.");
gQuestionList.ScoreResults(6,"You have several symptoms of social anxiety disorder and they may be interfering in your normal routines. Please talk to your doctor or another healthcare provider. You may learn more about social anxiety disorder below.");
gQuestionList.ScoreResults(7,"The symptoms you are having indicate a possibility of social anxiety disorder. Additionally, it sounds like your symptoms have interfered significantly in your life, whether it's at work or school, with your friends and family, or just in your normal routines. Please talk to your doctor or another healthcare provider. You may learn more about social anxiety disorder below.");
gQuestionList.ScoreResults(8,"The symptoms you are having indicate a possibility of social anxiety disorder. Additionally, it sounds like your symptoms have interfered significantly in your life, whether it's at work or school, with your friends and family, or just in your normal routines. Please talk to your doctor or another healthcare provider. You may learn more about social anxiety disorder below.");
gQuestionList.ScoreResults(9,"The symptoms you are having indicate a possibility of social anxiety disorder. Additionally, it sounds like your symptoms have interfered significantly in your life, whether it's at work or school, with your friends and family, or just in your normal routines. Please talk to your doctor or another healthcare provider. You may learn more about social anxiety disorder below.");
q = gQuestionList.NewQuestion("Do you have an intense fear that you will do or say something that will embarrass you in front of other people?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Are you continually afraid of making a mistake in front of other people?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Do you constantly feel you are being watched and judged by other people?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Does your fear of embarrassment or humiliation keep you from doing things you really want to do?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Does your fear of embarrassment keep you from speaking to people?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("When you have to go to a feared social situation or event, do you worry about it for days or even weeks ahead of time?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Do you have an intense fear of meeting new people or being in situations where you don't know people well?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Before or during any kind of feared social situation, do you experience any physical symptoms such as nausea, trembling, blushing, or sweating?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Have you ever had a panic attack before or during a feared social situation as a result of your anxiety about the situation?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Do you try to avoid social situations that cause you anxiety, if at all possible?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Do you sometimes drink to make the feared social situations easier to handle?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Has your anxiety about certain social situations interfered with your normal routine?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Has your anxiety about certain social situations interfered with your functioning at work or school?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);
q = gQuestionList.NewQuestion("Has your anxiety about certain social situations kept you from having friendships or other relationships or any kind of social life?");
q.NewAnswer("Yes",true);
q.NewAnswer("No",false);


// <-- Quiz Source End 
