
/* Used by Page1 of Survey */
function initQuiz()
{
	var f = $("#SurveyForm")[0];
	var ShowResultSignup = f["ShowResultSignup"].value;

	if(ShowResultSignup == "true" || ShowResultSignup == "1")
	{        
		loadQuizAnalysis();
	}
	else
	{
		$("#surveydiv").show();
		if (f["DebugMode"].value == "1" || f["DebugMode"].value == "true")
		{
			fill(f); //test 
		}
	}
}
		
function radioButtonChecker(f, name)
{
	var radio_choice = false;

	for (counter = 0; counter < f[name].length; counter++)
	{
		if (f[name][counter].checked) radio_choice = true;
	}

	if (!radio_choice)
	{
		return (false);
	}

	return (true);
}

function validateQuiz()
{
	var f = $("#SurveyForm")[0];
	var max = parseInt(f["QuestionCount"].value) + 1;

	for (i=1; i < max; i++)
	{    
		if(f["q." + i])
		{
			if(!radioButtonChecker(f,"q." + i))
		    {
			    alert("Please select an answer for question "+i+".");
			    return false;
		    }
		}
	}

	$("#progressdiv").show();
	$("#surveydiv").hide();

	if (f["DebugMode"].value == "1" || f["DebugMode"].value == "true")
	{
		loadQuizAnalysis();
	}
	else
	{
	    setTimeout("progressBar();",50);
	}

	return false;
}

function loadQuizAnalysis()
{
	$("#progressdiv").hide();
	$("#summarydiv").show();
	$("#surveydiv").hide();
	$("#SurveyForm").attr("onsubmit","javascript:return true;");
}

var currentProgress = 1;

function progressBar()
{
	currentProgress+=3;
	if(currentProgress<150)
	{
		$("#progressdone").css("padding","0 "+currentProgress+"px");
		setTimeout("progressBar();",50);
	}
	else
	{
		loadQuizAnalysis();
	}
}

/* Test Function */
function fill(f)
{
var max = parseInt(f["QuestionCount"].value) + 1;

	for (i=1; i < max; i++)
	{    
	    if(f["q." + i])
	    {
    		f["q." + i][0].checked = true;
		}
	}

	f["txtFirstName"].value = "BoRyan";
	f["txtEmail"].value = "boryane@riverseeker.com";
}

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span> of '+x[i].getAttribute('maxlength') + ' characters maximum';
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'counter_toomuch';
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}