var quiz = {

	theDivs : null,
	previousButton : null,
	nextButton : null,
	theCount : null,
	theInputs : null,
	theSubmitButton : null,
	productDetailAnchor : null,
	productName : null,
	tellAFriend : null,

	setUp : function(){
		var quizContainer = document.getElementById('quiz');
		quiz.productDetailAnchor = document.getElementById('find_out_more');
		quiz.theDivs = quizContainer.getElementsByTagName('div');
		quiz.previousButton = document.getElementById('previous');
		quiz.nextButton = document.getElementById('next');
		quiz.theCount = 0;
		quiz.theSubmitButton = document.getElementById('submit');
		quiz.theSubmitButton.className = 'display_none';
		var theFirstDiv = quiz.theDivs[0];
		quiz.theInputs = theFirstDiv.getElementsByTagName('input');
		
		// hide all the divs
		for(var i=0; i< quiz.theDivs.length - 2; i++){
			quiz.theDivs[i].className = "display_none";
		}
		
		// display the first div
		theFirstDiv.className = "display";
		// trigger selected function to all the inputs on click
		for(var i=0; i<quiz.theInputs.length; i++){
			quiz.addEvent(quiz.theInputs[i], 'click', quiz.selected);
		}
		
		quiz.addEvent(quiz.nextButton, 'click', quiz.next);
		quiz.addEvent(quiz.previousButton, 'click', quiz.previous);
		quiz.addEvent(quiz.theSubmitButton, 'click', quiz.submit);
		quiz.tell_a_friend = document.getElementById('tell_a_friend');
		quiz.tell_a_friend.useService("/webservices/sendEmail.asmx?WSDL","tellAFriend");
		
	},
	
	submit : function(e){
		quiz.productDetailAnchor = document.getElementById('find_out_more');
		var groupOne = document.theForm.RadioGroup1;
		var groupTwo = document.theForm.RadioGroup2;
		var groupThree = document.theForm.RadioGroup3;
		var groupFour = document.theForm.RadioGroup4;
		var groupFive = document.theForm.RadioGroup5;
		var tellFriendButton = document.getElementById('tell_friend_button');

		var theResultsHeader = document.createElement('h1');
		var theHResult = '';
		var theReResult = '';
		var theSResult = '';
		var theRfResult = '';
		
		for(var i=0; i< quiz.theDivs.length - 2; i++){
			quiz.theDivs[i].className = "display_none";
		}
		
		quiz.theDivs[5].className = 'display';
		quiz.previousButton.className = "display_none";
		
		// check which radio buttons have been clicked and assign them their category word
		if(groupOne[0].checked){theHResult += 'hydrate ';}
		if(groupTwo[0].checked){theHResult += 'hydrate ';}
		if(groupThree[0].checked){theHResult += 'hydrate ';}
		if(groupFour[0].checked){theHResult += 'hydrate ';}
		if(groupFive[0].checked){theHResult += 'hydrate ';}
		
		if(groupOne[3].checked){theReResult += 'renew ';}
		if(groupTwo[1].checked){theReResult += 'renew ';}
		if(groupThree[1].checked){theReResult += 'renew ';}
		if(groupFour[1].checked){theReResult += 'renew ';}
		if(groupFive[1].checked){theReResult += 'renew ';}
		
		if(groupOne[2].checked){theSResult += 'soothe ';}
		if(groupTwo[3].checked){theSResult += 'soothe ';}
		if(groupThree[3].checked){theSResult += 'soothe ';}
		if(groupFour[3].checked){theSResult += 'soothe ';}
		if(groupFive[2].checked){theSResult += 'soothe ';}
		
		if(groupOne[1].checked){theRfResult += 'refresh ';}
		if(groupTwo[2].checked){theRfResult += 'refresh ';}
		if(groupThree[2].checked){theRfResult += 'refresh ';}
		if(groupFour[2].checked){theRfResult += 'refresh ';}
		if(groupFive[3].checked){theRfResult += 'refresh ';}
		
		// split the string and set the variable to a number
		theHResult = theHResult.split(" ");
		theHResult = theHResult.length;
		
		theReResult = theReResult.split(" ");
		theReResult = theReResult.length;
		
		theSResult = theSResult.split(" ");
		theSResult = theSResult.length;
		
		theRfResult = theRfResult.split(" ");
		theRfResult = theRfResult.length;
		
		// determine which category has the highest number and display the correct info accordingly
		if((theHResult >= theReResult) && (theHResult >= theSResult) && (theHResult >= theRfResult)){
			theResultsHeader.appendChild(document.createTextNode('You sound like a Hydrate person'));
			quiz.theDivs[5].parentNode.className = 'hydrate';
			quiz.productName = 'Hydrate';
			quiz.productDetailAnchor.appendChild(document.createTextNode(quiz.productName));
			quiz.productDetailAnchor.href = '/Our_Collection/Product_Detail.asp?ProductID=29';
			quiz.theDivs[5].appendChild(theResultsHeader);
		}
		
		else if((theReResult >= theHResult) && (theReResult >= theSResult) && (theReResult >= theRfResult)){
			theResultsHeader.appendChild(document.createTextNode('You sound like a Renew person'));
			quiz.theDivs[5].parentNode.className = 'renew';
			quiz.productDetailAnchor.href = '/Our_Collection/Product_Detail.asp?ProductID=30';
			quiz.productName = 'Renew';
			quiz.productDetailAnchor.appendChild(document.createTextNode(quiz.productName));
			quiz.theDivs[5].appendChild(theResultsHeader)
		}
		
		else if((theSResult >= theHResult) && (theSResult >= theReResult) && (theSResult >= theRfResult)){
			theResultsHeader.appendChild(document.createTextNode('You sound like a Soothe person'));
			quiz.theDivs[5].parentNode.className = 'soothe';
			quiz.productDetailAnchor.href = '/Our_Collection/Product_Detail.asp?ProductID=32';
			quiz.productName = 'Soothe';
			quiz.productDetailAnchor.appendChild(document.createTextNode(quiz.productName));
			quiz.theDivs[5].appendChild(theResultsHeader)
		}
		
		else if((theRfResult > theHResult) && (theRfResult > theReResult) && (theRfResult > theSResult)){
			theResultsHeader.appendChild(document.createTextNode('You sound like a Refresh person'));
			quiz.theDivs[5].parentNode.className = 'refresh';
			quiz.productDetailAnchor.href = '/Our_Collection/Product_Detail.asp?ProductID=31';
			quiz.productName = 'Refresh'
			quiz.productDetailAnchor.appendChild(document.createTextNode(quiz.productName));
			quiz.theDivs[5].appendChild(theResultsHeader)
		}
		
		quiz.stopDefault(e);
		quiz.addEvent(tellFriendButton, 'click', quiz.tellFriend);
		
	},
	
	
	tellFriend : function(e){

		var theSubmitButton = document.getElementById('tell_friend_submit');
		for(var i=0; i< quiz.theDivs.length - 2; i++){
			quiz.theDivs[i].className = "display_none";
		}
		
		quiz.theDivs[6].parentNode.parentNode.className = "tell_friend";
		quiz.theDivs[6].className = 'display';
		
		quiz.addEvent(theSubmitButton, 'click', function(e){
			if(document.tellFriendForm.to_email.value == ""){
				alert("Please enter a friend's email address");			
				quiz.stopDefault(e);
				document.tellFriendForm.to_email.focus();
				return;
			}
			
			else {
				var regExpSpace = /\s+/g;
				// clean up the recipient email address field first
				// get rid of any spaces in string
				sSendToAddresses = document.tellFriendForm.to_email.value.replace(regExpSpace,'');
				// then replace any multiple instance of semicolons
				sSendToAddresses = sSendToAddresses.replace(/[;]+/g,';');
				// then remove any leading and trailing semicolons
				sSendToAddresses = sSendToAddresses.replace(/^\;+|\;+$/g,'');
				document.tellFriendForm.to_email.value = sSendToAddresses;
				// now that the email recipient field is cleaned up, split on the semicolon and check each one for validity
				aSendToAddresses = document.tellFriendForm.to_email.value.split(";");
				for (var i=0; i < aSendToAddresses.length; i++) {
					sEmailAddress = quiz.trimField(aSendToAddresses[i]);
					if (!isValidEmail(sEmailAddress)) {
						alert("Please make sure all email addresses are valid and separated by semicolons.");
						quiz.stopDefault(e);
						document.tellFriendForm.to_email.focus();
						return;
					}
				}
			}
			
			if(document.tellFriendForm.from_name.value == ""){
				alert('Please enter your name');			
				quiz.stopDefault(e);
				return;
			}
			
			if(document.tellFriendForm.from_email.value == ""){
				alert('Please enter your email address');			
				document.tellFriendForm.from_email.focus();
				quiz.stopDefault(e);
				return;
			}
			
			if(isValidEmail(document.tellFriendForm.from_email.value)){
			    document.tellFriendForm.submit();
			    quiz.stopDefault(e);
				quiz.theDivs[6].className = 'display_none';
				quiz.theDivs[7].className = 'display';
				var prodDetail = document.getElementById('find_out_more2');
				prodDetail.href = quiz.productDetailAnchor.href;
				prodDetail.appendChild(document.createTextNode(quiz.productName));
				
				
			}
		});
		
		quiz.stopDefault(e);
		
	},
	
	myResults : function(result){
	
	
	},
	
	selected : function(){
		for(var i=0; i<quiz.theInputs.length; i++){
			if(quiz.theInputs[i].checked){
				quiz.nextButton.className = "display";
				quiz.previousButton.className = "display";
			}
		}
		
		quiz.nextButton.className = "display";
		quiz.previousButton.className = "display";
		
		if(quiz.theDivs[4].className == "display"){quiz.nextButton.className = "display_none";}
		if(quiz.theDivs[0].className == "display"){quiz.previousButton.className = "display_none";}
		if(quiz.theDivs[4].className == "display"){quiz.theSubmitButton.className = 'display';}
	},
	
	next : function(e){
		quiz.theCount++;
		quiz.theInputs = quiz.theDivs[quiz.theCount].getElementsByTagName('input');
		
		// hide previous and next buttons until one of the options are selected
		quiz.nextButton.className = "display_none";
		quiz.previousButton.className = "display";
		
		// loop through the divs and hide them all except the one we're on
		for(var i=0; i < quiz.theDivs.length - 2; i++){
			quiz.theDivs[i].className = "display_none";
			quiz.theDivs[quiz.theCount].className = "display";
		}
		
		// if we're on the last div, don't display the next button
		if(quiz.theDivs[4].className == "display"){quiz.nextButton.className = "display_none";}
		
		// go to the selected function if one of the radio buttons is selected
		for(var i=0; i<quiz.theInputs.length; i++){
			if(quiz.theInputs[i].checked){
				quiz.nextButton.className = "display";
			}
			quiz.addEvent(quiz.theInputs[i], 'click', quiz.selected);
		}
		
		quiz.stopDefault(e);
	},
	
	previous : function(e){
		quiz.theCount--;
		
		quiz.theInputs = quiz.theDivs[quiz.theCount].getElementsByTagName('input');

		// loop through the divs and hide them all except the one we're on		
		for(var i=0; i< quiz.theDivs.length - 2; i++){
			quiz.theDivs[i].className = "display_none";
			quiz.theDivs[quiz.theCount].className = "display";
		}
		
		if(quiz.theDivs[0].className == "display"){
			quiz.previousButton.className = "display_none";
		}
		
		for(var i=0; i<quiz.theInputs.length; i++){
			if(quiz.theInputs[i].checked){
				quiz.nextButton.className = "display";
			}
		}
		
		quiz.stopDefault(e);
	},
	
	// utility functions 
	trimField : function(fld) {
		// removes leading and trailing blanks from fld
		var res = "";
		var c = 0;
		for (i=0; i < fld.length; i++) {
		if (fld.charAt(i) != " " || c > 0) {
			res += fld.charAt(i);
			if (fld.charAt(i) != " ") c = res.length;
		}
		}
		return res.substr(0,c);
	},
	
	addEvent : function(obj, type, func) {
    if (obj.addEventListener) {obj.addEventListener(type, func, false);}
    else if (obj.attachEvent) {
      obj["e" + type + func] = func;
      obj[type + func] = function() {obj["e" + type + func] (window.event);}
      obj.attachEvent("on" + type, obj[type + func]);
    }
    else {obj["on" + type] = func;}
	},
	
	stopDefault : function(e) {
     if (!e) {e = window.event;}
     if (!e.preventDefault) {
         e.preventDefault = function() { this.returnValue = false; }
     }
     e.preventDefault();
     return false;
  }
}

