/**
 * Quiz functions 
 */
function $(id){
	return document.getElementById(id);	
}
function addEvent(obj, evType, fn, useCapture){
 
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    
  } else if (obj.attachEvent){
    obj.attachEvent("on"+evType, fn);
   
  } else {
   obj['on'+evType]=fn;
  }
}

function nextQuestion()
{
	if(validate("questionHair")){
		if (validate("questionScalp")){
			var elOne = $('questionOne');
			var elTwo = $('questionTwo');
			elOne.style.display = "none";
			elTwo.style.display = "block";
		}
	}
}

function validate(id)
{
	var inputList = document.getElementById(id);
	var arrayOfCheckBoxes = inputList.getElementsByTagName("input");

	for(var i=0;i<arrayOfCheckBoxes.length;i++){
		if((arrayOfCheckBoxes[i]).checked) {
			if (id == "questionType" && (arrayOfCheckBoxes[5]).checked){
				var inputSubList = $("cleansubmenu");
				var arrayOfCheckBoxesSub = inputSubList.getElementsByTagName("input");
				for(var i=0;i<arrayOfCheckBoxesSub.length;i++){
					if((arrayOfCheckBoxesSub[i]).checked) {
						return verifyDestination();
					}
				}
				error("questionTwoSub");
				return false;
			}
			return verifyDestination();
		}
	}
	error(id);
	return false;
}

function error(id) {
	if (id == "questionHair") {
		document.getElementById('errorBox').innerHTML = 'Por favor, selecciona el tipo de cabello!';
	}
	if (id == "questionScalp") {
		document.getElementById('errorBox').innerHTML = 'Por favor, selecciona el tipo de cuero cabelludo!';
	}
	if (id == "questionType") {
		document.getElementById('errorBox2').innerHTML = 'Por favor, selecciona alguna opción!';
	}
	if (id == "questionTwoSub") {
		document.getElementById('errorBox2').innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Por favor, selecciona una sub categoría!';
	}
}

function showCleanSubMenu() {
	cleanErrorMsg('errorBox2');
	
	var el = document.getElementById('cleansubmenu');
	if (document.getElementById('response3f').checked) {
		el.style.display = "block";
	}else{
		el.style.display = "none";
	}
}

function cleanErrorMsg(id){
		if(id =='errorBox') {
			if(validate("questionHair")){
				document.getElementById(id).innerHTML = '';
			}
		}else if(id =='errorBox') {
			if(validate("questionScalp")){
				document.getElementById(id).innerHTML = '';
			}
		}else{
				document.getElementById(id).innerHTML = '';
		}		
}
window.onload=function(){
addEvent($('questionHair'), 'click', function(){cleanErrorMsg('errorBox')}, false)
addEvent($('questionScalp'), 'click', function(){cleanErrorMsg('errorBox')}, false)
addEvent($('questionType'), 'click', function(){showCleanSubMenu()}, false)
}
function verifyDestination(){
var f=$('product_selector');
	if($('response2f').checked){
		f.action='http://www.headandshoulders.com/es_US/collections/severe_dandruff/';
		return true;
	}
	var rad=[];
	var aIn=f.getElementsByTagName('input');
	for(i=0;i<aIn.length;i++){
		if(aIn[i].type!='radio')continue;
		rad.push(aIn[i]);
	}
	for(i=0;i<rad.length;i++){
		if(rad[i].checked){
			switch(i){
			case 0:
			f.action='http://www.headandshoulders.com/es_US/collections/extra_volume/';
			return true;
			break;
			case 1:
			f.action='http://www.headandshoulders.com/es_US/collections/smooth_&_silky/';
			return true;
			break;
			case 2:
			f.action='http://www.headandshoulders.com/es_US/collections/dry_scalp_care/';
			return true;
			break;
			case 3:
			f.action='http://www.headandshoulders.com/es_US/collections/sensitive_care/';
			return true;
			break;
			case 4:
			f.action='http://www.headandshoulders.com/es_US/collections/restoring_shine/';
			return true;
			break;
			case 6:
			f.action='http://www.headandshoulders.com/es_US/collections/citrus_breeze/';
			return true;
			break;
			case 7:
			f.action='http://www.headandshoulders.com/es_US/collections/refresh/';
			return true;
			break;
			case 8:
			f.action='http://www.headandshoulders.com/es_US/collections/ocean_lift/';
			return true;
			break;
			case 9:
			f.action='http://www.headandshoulders.com/es_US/collections/classic_clean/';
			return true;
			break;
			}
		}	
	}
	return true;
}
