/*************************************************************************
	All functions except buildInitialElements() = contentElementsBuild.js
*************************************************************************/
/* Version date: 10/16/2006 4:21 PM */
// -----------------------------------------------------------------------

//All arrays 0 based except content objects in adDataLoaderCG.js, adDataObjs.js

var subelementid;
var activeListItem;
var selectedQuests = new Array();
var lastQuestion = 0;
var lastQuestionStat = false;
var questionSpace = "";
var spaceNum = 0;
var specificChar = 0;

//Object that keeps track of movie state
function FlashManager() {
 this.playIntro = true;
 this.hasPlayedTopicForSection = new Array(false,false,false);
 
 this.playVideoFor = function(sect) { 
	if (this.hasPlayedTopicForSection[sect] == false) {
		window.document.aadflash.LoadMovie(0, 'media/flash/ask_' + sect + '.swf');
		this.hasPlayedTopicForSection[sect] = true;
	}
	else
		this.playStillVideo();
 }  
 this.playIntroVideo = function() { 
	window.document.aadflash.LoadMovie(0, 'media/flash/ask_intro.swf');
 }  
 this.playStillVideo = function() { 
	window.document.aadflash.LoadMovie(0, 'media/flash/ask_still.swf');
 }  
}

var flashManager = new FlashManager();

//Object that builds the elements (elements = steps)
function elementEntry(elementImage,elementType,elementText,elementClose,elementHeight)
{
	this.elementImage = elementImage;
	this.elementType = elementType;
	this.elementText = elementText;
	this.elementClose = elementClose;
	this.elementHeight = elementHeight;
}

//controls opening and closing of main container elements
function showNextElement(elementId,closeoption)
{	
	if(closeoption == 1)
	{
		for(m=0;m<elementArray.length;m++)
		{
			document.getElementById(['subElement' + m]).style.visibility = "hidden";
			document.getElementById(['subElement' + m]).style.position = "absolute";
			document.getElementById(['subElement' + m]).style.top = "0px";
			document.getElementById(['subElement' + m]).style.left = "-800px";
			
			document.images['arrow' + m].src = "media/images/white_arrow_right.gif";
		}
	}
	
	document.getElementById(['subElement' + elementId]).style.position = "relative";
	document.getElementById(['subElement' + elementId]).style.left = "0px";
	document.getElementById(['subElement' + elementId]).style.visibility = "visible";
	
	document.images['arrow' + elementId].src = "media/images/white_arrow_down.gif";
	
	if(elementId == 1)
	{
		selectTopic(0,1);
	}
	else
	{
		if(closeoption == 1)
		{
			selectTopic(-1,-1);
		}
	}
	if(elementId == elementArray.length - 1)
	{
		document.getElementById('eBottomPict').style.visibility = "hidden";
		document.getElementById('eBottomPict').style.position = "absolute";
		document.getElementById('eBottomPict').style.top = "0px";
	}
	else
	{
		document.getElementById('eBottomPict').style.visibility = "visible";
		document.getElementById('eBottomPict').style.position = "relative";
	}
}

//resizes content area of main elements for expanding content list
function resizeElement(resizeid,subelementid,optionid,amount)
{
	document.getElementById('question_list_9').style.height = amount + 'px';
}

//shows sub topics based on clicked main topic
function selectTopic(id,formid)
{
	for(m=0;m<topicAreas.Topics.length;m++)
	{
		document.getElementById(['mark' + m]).style.visibility = 'hidden';
	}
	
	if(id != -1)
	{
		document.edietician_phone.formTopicId.value = "me";
		document.getElementById(['mark' + id]).style.visibility = 'visible';
	}
	
	for(t=0;t<topicAreas.Topics.length;t++)
	{
		for(s=0;s<topicAreas.Topics[t].Subtopics.length;s++)
		{
			if(id == t)
			{
				document.getElementById(['subtopic' + t + '_' + s]).style.visibility = 'visible';
			}
			else
			{
				document.getElementById(['subtopic' + t + '_' + s]).style.visibility = 'hidden';
			}
		}
	}
	showText(id,id)	
}

//shows text lists in sub sub topics
function showText(subelementid,optionid)
{
	var topicCategoriesLength = 0;
	var oldCategoriesLength = 0;
	var longestCatLength = 0;
	if(optionid == -1)
	{
		for(o=0;o<topicAreas.Topics.length;o++)
		{
			for(r=0;r<topicAreas.Topics[o].Subtopics.length;r++)
			{
				document.getElementById(['texttopic'+ o + '_' + r]).style.visibility = 'hidden';
			}
		}
	}
	else
	{
		for(o=0;o<topicAreas.Topics.length;o++)
		{		
			for(s=0;s<topicAreas.Topics[o].Subtopics.length;s++)
			{
				document.getElementById(['texttopic' + o + '_' + s]).style.visibility = 'hidden';
				document.getElementById(['texttopic' + subelementid + '_' + s]).style.visibility = 'visible';
				if(subelementid == o)
				{
					topicCategoriesLength = topicAreas.Topics[subelementid].Subtopics[s].TopicCategories.length;
					if(topicCategoriesLength > oldCategoriesLength)
					{
						longestCatLength = topicCategoriesLength;
						oldCategoriesLength = longestCatLength;
					}
				}
			}
		}
	}
	resizeElement(1,subelementid,optionid,(longestCatLength * 32) + 160);
}

function selectText(subid,catid,closeoption)
{
	document.edietician_phone.formSubTopicId.value = "me";//subid;
	document.edietician_phone.formCategoryId.value = "me"; //catid;
	showNextElement(2,closeoption);
}

function showQuestions(idlist,optionid)
{
	spaceNum = 0;
	document.getElementById('bm_submit').style.visibility = 'visible';
	for(i=0;i<elementArray.length;i++)
	{
		document.getElementById('question_list_' + i).style.position='absolute';
		document.getElementById('question_list_' + i).style.visibility='hidden';
	}
	
	document.getElementById('question_list_9').style.visibility='visible';
	document.getElementById('question_list_9').style.position='relative';
	// SSK 20070223 - Fix Safari defect //
	document.getElementById('question_list_9').style.display = 'block';
	
	var questArr = new Array();

	questArr = idlist.split(",");

	for(e=0;e<norepeatArray.length;e++)
	{
		document.getElementById(['question' + norepeatArray[e]]).style.visibility = 'hidden';
		document.getElementById(['question' + norepeatArray[e]]).style.top = 0;
	}
	
	if(optionid != -1)
	{
	
		for(q=0;q<questArr.length;q++)
		{			
			document.getElementById(['question' + questArr[q]]).style.visibility = 'visible';
			// SSK 20070223 - Fix Safari defect //
			document.getElementById(['question' + questArr[q]]).style.display = 'block';
			if(lastQuestion > 65)
			{
				spaceNum = spaceNum + 45;
				questionSpace = spaceNum + 'px';
			}
			else
			{
				spaceNum = spaceNum + 30;
				questionSpace = spaceNum + 'px';
			}
			document.getElementById(['question' + questArr[q]]).style.top = questionSpace;
			
			lastQuestion = 	textArray[questArr[q]].length;	
		}
		
		amount = spaceNum + 60;
		resizeElement(0,0,0,amount);
	}
}

function hideQuestions()
{
	document.getElementById('bm_submit').style.visibility = 'hidden';

	for(i=0;i<elementArray.length;i++)
	{
		//document.getElementById('question_list_' + i).style.position='absolute';
		document.getElementById('question_list_' + i).style.position='relative';
		document.getElementById('question_list_' + i).style.visibility='hidden';
	}
	
	document.getElementById('question_list_9').style.visibility='hidden';
	//document.getElementById('question_list_9').style.position='absolute';
	document.getElementById('question_list_9').style.position='relative';
	
	for(e=0;e<norepeatArray.length;e++)
	{
		document.getElementById(['question' + norepeatArray[e]]).style.visibility = 'hidden';
		document.getElementById(['question' + norepeatArray[e]]).style.top = 0;
	}	
}

function activeLink(cat,sect)
{
	document.getElementById('sect_' + cat + '_' + sect).style.background='#315FA2';
	document.getElementById('asect_' + cat + '_' + sect).style.color = '#ffffff';
}
function deActiveLink(cat,sect)
{
	for(i=0;i<9;i++)
	{
		if(i < 3)
		{
			ii = 0;
		}
		
		if(i >= 3 && i < 6)
		{
			ii = 1;
		}
		
		if(i > 5)
		{
			ii = 2;
		}
		compareActive = 'sect_' + i + '_' + ii;
		
		if(activeListItem != compareActive)
		{
			if (document.getElementById('sect_' + i + '_' + ii)) document.getElementById('sect_' + i + '_' + ii).style.background='none';
			if (document.getElementById('asect_' + i + '_' + ii)) document.getElementById('asect_' + i + '_' + ii).style.color = '#848589';
		}	
	}
}
function showQuestionList(cat,sect)
{
	flashManager.playVideoFor(sect);
	clearSelected();
	for(e=0;e<norepeatArray.length;e++)
	{
		document.getElementById(['question' + norepeatArray[e]]).style.visibility = 'hidden';
		document.getElementById(['question' + norepeatArray[e]]).style.top = 0;
	}
	document.getElementById('bm_submit').style.visibility = 'visible';
	document.getElementById('sect_image').style.position = 'relative';
	//document.getElementById('sect_image').style.visibility = 'visible';
	document.getElementById('sect_image').style.display = 'block';
	document.images['sect_theimage'].src = "media/images/sectimage_" + sect + ".jpg";
	activeListItem = "sect_" +  cat + "_" + sect
	for(i=0;i<9;i++)
	{
		if(i < 3)
		{
			ii = 0;
		}
		
		if(i >= 3 && i < 6)
		{
			ii = 1;
		}
		
		if(i > 5)
		{
			ii = 2;
		}
		
		document.getElementById('sect_' + i + '_' + ii).style.background='none';
		document.getElementById('asect_' + i + '_' + ii).style.color = '#848589';	
	}
	document.getElementById(activeListItem).style.background='#315FA2';
	document.getElementById('asect_' + cat + '_' + sect).style.color = '#ffffff';
	
	for(i=0;i<elementArray.length;i++)
	{
		document.getElementById('question_list_' + i).style.position='relative';
		document.getElementById('question_list_' + i).style.display='none';
	}
	document.getElementById('question_list_' + cat).style.position='relative';
	document.getElementById('question_list_' + cat).style.visibility='visible';
	// SSK 20070222 - fix Safari Defect //
	document.getElementById('question_list_' + cat).style.display = 'block';
}


function checksearch(caller) {
	clearSelected();
	qIds = topicAreas.GetQuestionIdsForSearchPhrase(document.aad_email.search.value);
	if (qIds == '') {
		hideQuestions();
		document.getElementById('noresults').style.visibility='visible';
	}
	else {
		document.getElementById('noresults').style.visibility='hidden';
		showQuestions(qIds);
	}
	return;
}
function trackQuests(id)
{	
	selectednum = 0;
	for(i=0;i<norepeatArray.length;i++)
	{
		if(norepeatArray[i] == id)
		{
			if(selectedQuests[i] == true)
			{
				selectedQuests[i] = false;
			}
			else
			{
				selectedQuests[i] = true;
			}
			selectednum = i;
		}
	}

	document.aad_email.questionId.value = "";
	for(ii=0;ii<norepeatArray.length;ii++)
	{
		if(selectedQuests[ii] == true)
		{
			document.aad_email.questionId.value += norepeatArray[ii] + ",";
		}
	}
}

function buildText(thetext)
{
	if(thetext.length > 65)
	{
		specificChar = thetext.charCodeAt(65);
		if(specificChar != 32)
		{
			for(i=1;i<65;i++)
			{
				if(thetext.charCodeAt(65 - i) == 32)
				{
					specificChar = 65 - i;
					break;
				}
			}
		}
		else
		{
			specificChar = 65;
		}
		
		buildQuestion1 = thetext.substr(0,specificChar + 1);
		buildQuestion2 = thetext.substring(specificChar);
		buildQuestion = buildQuestion1 + '<br>' + buildQuestion2;
	}
	else
	{
		buildQuestion = thetext;
	}
	return buildQuestion;
}

function clearSelected()
{
	for(i=0;i<norepeatArray.length;i++)
	{
		selectedQuests[i] = false;
		if (document.getElementById('quest_' + (i + 1)))
			document.getElementById('quest_' + (i + 1)).checked = false;
		if (document.getElementById('quest_s_' + (i + 1)))
			document.getElementById('quest_s_' + (i + 1)).checked = false;
	}
	document.aad_email.questionId.value = "0";
}

