/***** OPACITY *****/
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
	document.getElementById(id).style.display = 'block';
	//
    var speed = Math.round(millisec / 100);
    var timer = 0;
    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
} 
//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	
	if(opacity<=5){
		object.display = 'none';	
	}else{
		object.display = 'block';
	}
    
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
/***** END OPACITY *****/

var clickCount = 0;
var currentSection = 1;
function switch_page(section, set_radio_button) {
    // show next quiz section
	
	//fadeOut = opacity('quiz_content' + section, 1, 1, 1);
	
	for (ndx=1; ndx<=3; ndx++) {
		if(ndx != currentSection && ndx != section){
			document.getElementById('quiz_content' + ndx).style.display = 'none';
		}else{
			document.getElementById('quiz_content' + ndx).style.display = 'block';
		} 
    }
		
	if(section == 2 || section == 3) {
		
		document.getElementById('quiz_content' + section).style.zIndex = 100;
		document.getElementById('quiz_content' + currentSection).style.zIndex = 101;
		fadeIn = opacity('quiz_content' + currentSection, 100, 0, 500);
		//fadeOut = opacity('quiz_content' + section, 1, 1, 1);
   		
		//document.getElementById('quiznext').style.display = "none";
		//document.getElementById('quizfinish').style.display = "block";
	}

    // show appropriate quiz results
    if (section == 3) {
		//document.getElementById('quizfinish').style.display = "none";
		// get the total
        total = 0;
		total += parseInt(get_radio_value(document.quiz_form.selection1));
		total += parseInt(get_radio_value(document.quiz_form.selection2));
		total += parseInt(get_radio_value(document.quiz_form.selection3));
		total += parseInt(get_radio_value(document.quiz_form.selection4));

        if (total >= 4 && total <= 6) {
            // show basic response
            document.getElementById('quiz_results1').style.display = 'block';
            window.setTimeout('window.open("http://www.54g.org/54g_products.php")', 10000);
        }
        else if (total >= 7 && total <= 9) {
            // show basic response
            document.getElementById('quiz_results2').style.display = 'block';
            window.setTimeout('window.open("http://www.54g.org/54g_products.php")', 10000);
        }
        else {
            // show power response
            document.getElementById('quiz_results3').style.display = 'block';
            window.setTimeout('location.href=\'buy/index.html\'', 20000);
        }
    }
	currentSection = section;
}

function switch_page_trigger(){
	var theseRadios = document.getElementsByTagName('input');

	var selection1 = false;
	var selection2 = false;
	var selection3 = false;
	var selection4 = false;
	
	for(i=0;i<theseRadios.length;i++){
		thisRadio = theseRadios[i];
		thisRadioName = thisRadio.getAttribute('name');
		if(!thisRadio.onclick && thisRadioName.match('selection') == 'selection'){
			thisRadio.onclick = function(){
				switch_page_trigger();
			}
		}else{
			if(thisRadio.checked){
				thisSelection = eval(thisRadioName + ' = true;');
			}
		}
	}
	if(selection1 && selection2 && !selection3 && !selection4){
		switch_page(2);
	}
	if(selection1 && selection2 && selection3 && selection4){
		switch_page(3)
	}
}

function set_radio_selection(section, set_radio_button) {
    // activate the radio button if they clicked the text instead
    if (set_radio_button) {
        switch(section) {
			case 1 : set_radio_value(document.quiz_form.selection1, set_radio_button); break;
			case 2 : set_radio_value(document.quiz_form.selection2, set_radio_button); break;
			case 3 : set_radio_value(document.quiz_form.selection3, set_radio_button); break;
			case 4 : set_radio_value(document.quiz_form.selection4, set_radio_button); break;
			default : break;
		}
    }
	switch_page_trigger();
}

function set_radio_value(element, index) {
    for (ndx=0; ndx < element.length; ndx++) {
        if (ndx+1 == index) {
            element[ndx].checked = 1;
        }
        else {
            element[ndx].checked = 0;
        }
    }
}
function get_radio_value(element) {
    for (ndx=0; ndx < element.length; ndx++) {
        if (element[ndx].checked) {
            return element[ndx].value;
        }
    }
    return 0;
}

