var hash;
var last_q = 15;
var q_num;
var cur_location;

function checkAnchor()
{
	if(hash != window.location.hash && hash != undefined)
	{
		old_q_num = hash.substring(2);
		hash = window.location.hash;
		q_num = hash.substring(2);
		
		if(!q_num)
		{
			q_num = 1;
		}
			
		if(old_q_num)
		{
			if(q_num == 1)
			{
				$('q'+old_q_num+'_cont').hide();
				$('q'+q_num+'_cont').show();
			}
			else
			{
				Effect.Fade('wiz_img', {duration: 1.1, queue: {position:'front', scope:'wiz'}});
				Effect.Fade('q'+old_q_num+'_cont', {duration: 1.0, queue: {position: 'front', scope:'q'}});
				Effect.Appear('q'+q_num+'_cont', {duration: 0.7, queue: {position: 'end', scope:'q'}});
				Effect.Appear('wiz_img', {duration: 0.7, queue: {position: 'end', scope: 'wiz'}});
			}
		}
		
		$('q_num').innerHTML = q_num;
	}
}

function doClick(e)
{
	if(q_num != last_q)
	{
		if( $RF('questions_form', 'q'+q_num) != undefined ) 
		{
			q_num++;
			window.location.href = cur_location + '#q' + q_num;	
		}
	}
	else
	{
		$('questions_form').submit();
	}
}

document.observe("dom:loaded", function() {

	checkAnchor();
	$$('.q_container').invoke('hide');
	
	cur_location = window.location.href.split('#')[0];
	hash = window.location.hash;
	q_num = hash.substring(2);
	
	if(q_num != 1)
	{
		window.location.href = cur_location + '#q1';	
	}
	
	q_num = 1;
	
	$('q1_cont').show();
	
	$$('label').each( function (e) {
		e.observe('click', doClick);
	});

	setInterval("checkAnchor()", 300);
});

function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}
