/*************************************************************************
	Javascript file that contains the javascript data objects for building
	the Dietitian Support Center topic area navigation menu and question
	sets.
*************************************************************************/

/* Prototype version date: July 25, 2006 */

/*
 * This object represents the Topic which corresponds to the first row
 * on the Navigation menu.
 */
function Topic(topicID,description,subtopics) {
 this.TopicID = topicID;
 this.Description = description;
 this.Subtopics = subtopics;
}

/*
 * This object represents the Subtopic which corresponds to the second row
 * on the Navigation menu.
 */
function Subtopic(subtopicID,description,topicCategorys) {
 this.SubtopicID = subtopicID;
 this.Description = description;
 this.TopicCategories = topicCategorys;
}

/*
 * This object represents the Topic Category which corresponds to the third
 * row on the Navigation menu.
 */
function TopicCategory(topicCategoryID,description,questions) {
 this.TopicCategoryID = topicCategoryID;
 this.Description = description;
 this.Questions = questions;
}

/*
 * This object represents the Questions which are displayed for a Topic
 * Category
 */
function Question(questionID,text) {
 this.QuestionID = questionID;
 this.Text = text;
}

/*
 * This object represents a Keyword.
 */
function Keyword(word,questionIDs) {
 this.Word = word;
 this.QuestionIDs = questionIDs;
}

/*
 * This object contains the collection of Topics and provides
 * an indexer to the topic categories
 */
function TopicAreas(topics,keywords) {
 this.Topics = topics;
 this.TopicCategories = new Array();
 this.Questions = new Array();
 this.Keywords = new Array();
 this.init(keywords);

 this.GetTopicCategory = function(topicCategoryID) {
   return this.TopicCategories[topicCategoryID];
 }
 this.GetQuestionIdsForKeyword = function(keyword) {
   return this.Keywords[keyword] == null ? null : this.Keywords[keyword].QuestionIDs
 }
 this.GetQuestion = function(questionId) {
   return this.Questions[questionId];
 }
 this.GetQuestionIdsForSearchPhrase = function(phrase) {
	questions = this.GetQuestionsForSearchPhrase(phrase);
	questionIds = "";
	i = 0;
	for (questionID in questions) {
		question = questions[questionID];
		if (i > 0)
			questionIds += ",";
		questionIds += question.QuestionID;
		i++;
	}
	
    return questionIds;
 }
 this.GetQuestionsForSearchPhrase = function(phrase) {
	//prepare phrase for parsing
	phrase = phrase.toLowerCase();
	phrase = phrase.replace(/^\s*|\s*$/g,''); //Trim whitespace
	phrase = phrase.replace(/-/g,' '); // replace hyphens with spaces
	phrase = phrase.replace(/[_=~`!@#%&.*+?^$,;:"'<>{}()|[\]\/\\]/g, ''); // remove special characters
	keywords = phrase.split(' ');

	var questions = new Object();

	for(i=0; i<keywords.length; i++) {
		keyword = keywords[i];
		questionIds = topicAreas.GetQuestionIdsForKeyword(keyword);
		if (questionIds != null) {
			for(j=0; j<questionIds.length; j++) {
				questionId = questionIds[j];
				question = this.GetQuestion(questionId);
				questions[question.QuestionID] = question;
			}
		}
	}
	return questions;
 }
}

TopicAreas.prototype.init = function(keywords) {

	//initialize the topic category collection
	 for(i=0; i<this.Topics.length; i++)
	 {
		topic = this.Topics[i];
		subtopics = topic.Subtopics;
		for(j=0; j<subtopics.length; j++)
		{
			subtopic = subtopics[j];
			topicCategories = subtopic.TopicCategories;
			for(k=0; k<topicCategories.length; k++)
			{
				topicCategory = topicCategories[k];
				this.TopicCategories[topicCategory.TopicCategoryID] = topicCategory;

				questions = topicCategory.Questions;
				for(l=0; l<questions.length; l++) {
					question = questions[l];
					this.Questions[question.QuestionID] = question;
				}
			}
		}
	 }
	//initialize the keywords
	for(i=0; i<keywords.length; i++)
	{
		keyword = keywords[i];
		this.Keywords[keyword.Word] = keyword;
	}
 }


