/**
 * @author David "Rok" Taylor - Q1Com.Com/Savrus.com
 */
var nAnswered;
var nFirstQuestion = 1;
var nQuestions = 0;
var nTotalQuestions = 0;
var nDefWaitTime = 1000;
// Loads the settings from the hidden fields stored on the form
function GetInfo()
{
	var objTest;
	if (objTest = document.getElementById('firstquestnum'))
	{
		nFirstQuestionNum = parseInt(objTest.value);	
	}
	if (objTest = document.getElementById('firstquestid'))
	{
		nFirstQuestion = parseInt(objTest.value);	
	}
	if (objTest = document.getElementById('nummusicquest'))
	{
		nTotalQuestions = parseInt(objTest.value);		
	}
	if (objTest = document.getElementById('numquest'))
	{
		nQuestions = parseInt(objTest.value);		
	}	
	document.getElementById('Q.' + nFirstQuestion + '.1').style.display = '';
//	document.getElementById('Progress_Left').innerHTML = "of " + nTotalQuestions;
	ShowProgress(nFirstQuestionNum, nTotalQuestions);
	setTimeout('ShowQ(' + nFirstQuestion + ')', nDefWaitTime);
	
}
// Callback function used by the timer to display the the questions for the current song
function ShowQ(p_nWhich)
{
	document.getElementById('Q.' + p_nWhich + '.2').style.display = '';
	var objNext = document.getElementById('next.' + p_nWhich);
	objNext.style.display = 'none';
	if (p_nWhich >= nFirstQuestion + nQuestions - 1)
	{
		objNext.src = "images/btn-submit.gif";
	}
	nAnswered = new Array(0,0,0);
}
// Called each time an answer is changed to determine if the person has answered all of the questions before they continue on to the next questions
function GetAnAnswer(p_nWhich, p_What, p_strType, p_nPos, p_qType)
{
	nAnswered[p_nPos] = false;
	var objTest;	
	switch(p_strType)
	{
		case 'radio':
			nOffSet = 0;
			while (objTest = document.getElementById(p_nWhich + "." + p_What + "." + nOffSet))
			{
				if (objTest.checked)
				{
					nAnswered[p_nPos] = true;
                                  if (p_What == 'familiar' && objTest.value == 'unfamiliar')
                                  {
					nAnswered[1] = true;
					nAnswered[2] = true;
                                  } 
                                  if (p_What == 'familiar' && objTest.value == 'familiar')
                                  {
					nAnswered[1] = false;
                                  }
				}
				nOffSet++
			}
			break;

		case 'select':
			if (objTest = document.getElementById(p_nWhich + "." + p_What))
			{
				if (objTest.selectedIndex)
				{
					nAnswered[p_nPos] = true;
				}
			}
			break;
	}
	switch(p_qType)
	{
		case 35:
			if (objTest = document.getElementById(p_nWhich + "." + p_What))
			{
				if (objTest.selectedIndex)
				{
					nAnswered[0] = true;
					nAnswered[1] = true;
					nAnswered[2] = true;
				}
			}
			break;
		
                case 37:
			if (objTest = document.getElementById(p_nWhich + "." + p_What))
			{
                        window.alert(objTest.value);
				if (objTest.selectedIndex)
				{
					nAnswered[0] = true;
					nAnswered[1] = true;
					nAnswered[2] = true;
				}
			}
			break;
	}	
	var nResult = true;
	for (var i = nAnswered.length - 1; i >= 0; i--) 
	{
		nResult &= nAnswered[i];
	}
	if (objTest = document.getElementsByName(p_nWhich + ".next")) 
	{
		if (nResult)
		{
			objTest[0].style.display = '';
		}
		else
		{
			objTest[0].style.display = 'none';
		}
	}
	
}
// Called by the next buttons to hid the current question, display the next song and queue the question to display
function DoNext(p_nWhich)
{
	var nCount =  nFirstQuestion + nQuestions - 3;
	if (p_nWhich >= nCount && (document.getElementById('Q.' + p_nWhich + '.1')))
	{
		document.getElementById('SendResults').click();
	}
	else
	{
		var nNext = p_nWhich + 1;
		document.getElementById('Q.' + p_nWhich + '.1').style.display = document.getElementById('Q.' + p_nWhich + '.2').style.display = 'none';
		document.getElementById('Q.' + nNext + '.1').style.display = '';
		ShowProgress(nNext-1, nQuestions);
		setTimeout('ShowQ(' + nNext + ')', nDefWaitTime);
	}
}

function ShowProgress(p_nWhich, p_nOf)
{
	var a = document.getElementById('Progress').offsetWidth;
	var b = p_nWhich / p_nOf;
	var c = parseInt(a * b);
	
	document.getElementById('Progress_Done').style.width = c + "px";
	if (p_nWhich >= p_nOf)
	{
//		document.getElementById('Progress_Left').innerHTML = "";
//		document.getElementById('Progress_Done').innerHTML = p_nWhich + " of " + p_nOf;
		document.getElementById('Progress_Left').style.width = "0px";
	}
	else
	{
//		document.getElementById('Progress_Done').innerHTML = p_nWhich;
                //var barwidth = (a - c - 2) + "px";
                var barwidth = "15px";
		document.getElementById('Progress_Left').style.width = barwidth;
	}
	document.getElementById('Progress_Label').innerHTML = "Song " + p_nWhich + " of " + p_nOf;;
}
