
var PossibleAnswer=Class.create({
	initialize: function(answerText, nextChooserQuestion) {
		this.answerText=answerText;
		this.nextChooserQuestion=nextChooserQuestion;
	}
});

var ChooserQuestion=Class.create({
	initialize: function(questionText, answers, resultText, imageId, promoCode) {
		this.questionText=questionText;
		this.answers=answers;
		this.resultText=resultText;
		this.imageId=imageId;
		this.promoCode=promoCode;
	},
	isLeaf: function() {
		return (this.imageId=="") ? false : true;
	}
});

// Change this data structure to change the decision tree used.

var chooserQuestion=[
	// ChooserQuestion 0
	new ChooserQuestion("Is your broadband provider Cable or ADSL?",
				[new PossibleAnswer("cable_btn.gif", 1), new PossibleAnswer("adsl_btn.gif", 11)],
				"",
				"",
				""
	),
	// ChooserQuestion 1
	new ChooserQuestion("Do you have more than 1 computer connected to your cable connection?",
				[new PossibleAnswer("yes_btn.gif", 2), new PossibleAnswer("no_btn.gif", 3)],
				"",
				"",
				""
	),
	// ChooserQuestion 2
	new ChooserQuestion("Do you have a spare <a class=link href=javascript:popUpSmall('/uk-hardware-chooser/ethernet_popup.html')>Ethernet port</a> on your router? ",
				[new PossibleAnswer("yes_btn.gif", 8), new PossibleAnswer("no_btn.gif", 3)],
				"",
				"",
				""
	),
	// ChooserQuestion 3
	new ChooserQuestion("Do you want a new DECT cordless phones?",
				[new PossibleAnswer("yes_btn.gif", 4), new PossibleAnswer("no_btn.gif", 6)],		// No PossibleAnswers
				"",
				"",
				""
	),
	// ChooserQuestion 4 *** NEED TO CHANGE TO V-PORTAL & DECT BUNDLE
	new ChooserQuestion("",
				"",		// No PossibleAnswers
				"Vonage V-Portal &amp; Twin iDECT X1 Bundle",
				"large/vportal_twin_idect_x1",	
				"Vportal-DECT-Bundle"	
	),
	// ChooserQuestion 5
	new ChooserQuestion("",
				"",		// No PossibleAnswers
				"D-Link VWR",
				"VWR",
				"DLink-VWR"
	), 
	// ChooserQuestion 6
	new ChooserQuestion("",
				"",		// No PossibleAnswers
				"Vonage V-Portal",
				"VPORTAL",
				"VPortal"
	),
	// ChooserQuestion 7
	new ChooserQuestion("",
				"",		// No PossibleAnswers
				"D-Link VWR",
				"VWR",
				"DLink-VWR"
	), 
	// ChooserQuestion 8
	new ChooserQuestion("Do you want new DECT cordless phones?",
				[new PossibleAnswer("yes_btn.gif", 4), new PossibleAnswer("no_btn.gif", 13)],		// No PossibleAnswers
				"",
				"",
				""
	),
	// ChooserQuestion 9
	new ChooserQuestion("Do you connect to your Broadband modem via a USB connection?",
				[new PossibleAnswer("yes_btn.gif", 10), new PossibleAnswer("no_btn.gif", 11)],
				"",
				"",
				""
	),
	// ChooserQuestion 10
	new ChooserQuestion("",
				"",		// No PossibleAnswers
				"VTA with Guru Ethernet modem bundle",
				"GURU-VTA-Bundle",
				"DLink-VTA-Bundle"
	),
	// ChooserQuestion 11
	new ChooserQuestion("Do you have more than 1 computer connected to your Broadband connection?",
				[new PossibleAnswer("yes_btn.gif", 2), new PossibleAnswer("no_btn.gif", 12)],
				"",
				"",
				""
	),
	// ChooserQuestion 12
	new ChooserQuestion("Do you have a router? If you don't know, click No",
				[new PossibleAnswer("yes_btn.gif", 8), new PossibleAnswer("no_btn.gif", 3)],
				"",
				"",
				""
	),
	// ChooserQuestion 13
	new ChooserQuestion("",
				"",		// No PossibleAnswers
				"D-Link VTA Certified Reconditioned",
				"VTA",
				"DLink-VTARB"
	),
	// ChooserQuestion 14
	new ChooserQuestion("",
				"",		// No PossibleAnswers
				"D-Link VTA + DECT Bundle",
				"VTA_DECT",
				"DLink-VTA-DECT-Bundle"
	)
];

// You shouldn't need to change anything below this comment.

function ActualAnswer(questionAskedIndex, possibleAnswerIndex) {
	this.questionAskedIndex=questionAskedIndex;
	this.possibleAnswerIndex=possibleAnswerIndex;

	this.answerAsHTML=answerAsHTML;
}

function answerAsHTML() {
	return   'questionAskedIndex  = '+this.questionAskedIndex+'<br/>'
			+'possibleAnswerIndex = '+this.possibleAnswerIndex+'<br/>';
}

var answer=[new ActualAnswer(0, -1)];
var currentAnswer=0;

// The arguments to this function are the answerIndex, which is the 0-based number of the "row"
// of chooserQuestion on-screen, and the possibleAnswerIndex, which is the 0-based column referring to the button
// actually pressed.
function userResponse(answerIndex, possibleAnswerIndex) {
	// Record the answer we got in the "current" ActualAnswers slot.
	answer[answerIndex].possibleAnswerIndex=possibleAnswerIndex;
	// Adjust the length of the array to accomodate (only) the new ActualAnswer we're about to create ...
	answer.length=answerIndex+2;
	// Find out which "question" (see array above) we asked ...
	var questionAskedIndex=answer[answerIndex].questionAskedIndex;
	// and from that and the possibleAnswerIndex, determine what the next chooserQuestion should be.
	var nextChooserQuestionIndex=chooserQuestion[questionAskedIndex].answers[possibleAnswerIndex].nextChooserQuestion;
	answer[answerIndex+1]=new ActualAnswer(nextChooserQuestionIndex, -1);

	// now that we've (re)built the list of answers, we need to process them ...
	refreshDisplay();
}

function refreshDisplay() {
	var row;
	var html="";
	var debugHTML="";
	for(row=0; row < answer.length; row++) {
//		debugHTML+=answer[row].answerAsHTML();

		var questionIndex=answer[row].questionAskedIndex;
		var answerIndex=answer[row].possibleAnswerIndex;
		if(chooserQuestion[questionIndex].isLeaf()) {
			html+=lastRowHTML(chooserQuestion[questionIndex]);
		} else {
			html+=choiceRowHTML(chooserQuestion[questionIndex], row, answerIndex);
		}
	}
	document.getElementById("hardware-chooser-body").innerHTML=html;
//	document.getElementById("answers-debugging").innerHTML=debugHTML;
}

function lastRowHTML(currentChooserQuestion) {
	
	var html="";
	 html+='<div id="hardware-chooser-answer">';
     html+='   <h3><img src="/identity/vonage_uk/images/equipment/titles/You_need_the_following_device.gif" alt="You need the following device" /></h3>';
     html+='   	<p><a href="/equipment/';
	 html+='	'+currentChooserQuestion.promoCode+'.php"><span class="answer-link">'+currentChooserQuestion.resultText+'</span></a></p>';
     html+='	<p><a href="/equipment/';
	 html+='	'+currentChooserQuestion.promoCode+'.php"><img src="/identity/vonage_uk/images/equipment/hardware/'+currentChooserQuestion.imageId+'.gif" alt="'+currentChooserQuestion.imageId+'" /></a><br /><br />';
    // html+='	<a href="javascript:load(\'https://secure.vonage.co.uk/subscribe/index.htm?promo_id=UKVONPROD';
	 html+='<a href="/equipment/'+currentChooserQuestion.promoCode+'.php"><img src="/identity/vonage_uk/images/equipment/btns/more_about_device.gif" alt="More about this device"/></a>\n';
     html+=     '</p>';
     html+='</div>';
	 return html;
}

function choiceRowHTML(currentChooserQuestion, questionRow, answerIndex) {
	
	var questionnumber=eval(questionRow)+1;
	var html='';
	html+='<div class="hardware-chooser-questions">';
	html+='	<h3><img src="/identity/vonage_uk/images/equipment/q'+questionnumber+'.gif" alt="'+questionnumber+'" /></h3>';
    html+='	<div class="question floatleft">'+currentChooserQuestion.questionText+'</div>';    
	html+=' <div class="buttons floatright">';
			var possibleAnswerIndex;
			for(possibleAnswerIndex=0; possibleAnswerIndex<currentChooserQuestion.answers.length; possibleAnswerIndex++) {
				html+='<a href="javascript:userResponse('+questionRow+','+possibleAnswerIndex+')">';
				html+='<img src="'+((answerIndex == possibleAnswerIndex)?'/identity/vonage_uk/images/equipment/btns/chosen/':'/identity/vonage_uk/images/equipment/btns/');
				html+=currentChooserQuestion.answers[possibleAnswerIndex].answerText;
				html+='" alt="';
				html+=currentChooserQuestion.answers[possibleAnswerIndex].answerText;
				html+='" /></a>\n';
			}
	html+=' </div>';
    html+='</div>';
	return html;
}
