var DecisionTree_Arr = new Array();
var DecisionTree_isAnswered = true;
var DecisionTree_Number = 0;
var DecisionTree_TransitionTime = 1000;

var DecisionTree_DivID = 1;
var DecisionTree_ArrowID = 0;


/*
DecisionTree_Add( "Question ID", "Question text" ,
								"Answer 1 ID", "a) Answer 1 text" ,
								"Answer 2 ID", "a) Answer 2 text" ,
								"Answer 3 ID", "a) Answer 3 text" ,
								..., ... ,
								..., ... ,
								)
*/
function DecisionTree_Add(){
	var j = DecisionTree_Arr.length;
	var i = 0;
	DecisionTree_Arr[j] = new Array();
	for(i=0;i<DecisionTree_Add.arguments.length;i++){
		DecisionTree_Arr[j][i] = DecisionTree_Add.arguments[i]
	}
}


function DecisionTree_Display(){
	if(DecisionTree_Arr[DecisionTree_Number]==null){ return; }
	
	if ((location.search+'').toLowerCase().indexOf('compliance')>=0) { complianceOutput() }

	var sMyText = ''
		if(DecisionTree_ArrowID != 0){
			sMyText+= '<div class="adviceTreeArrow' + DecisionTree_ArrowID + '">&nbsp;</div>';
			DecisionTree_ArrowID = 0;
		}
	sMyText+= '<span class="DecisionTreeQuestionBox">'
	sMyText+= DecisionTree_Arr[DecisionTree_Number][1]
	sMyText+= '</span>'
	sMyText+= '<br>'

	for(var i=2;i<DecisionTree_Arr[DecisionTree_Number].length;i+=2){
		sMyText += '<span class="DecisionTreeAnswerBox answerBox' + Math.round(i/2) + '"><a href="javascript:DecisionTree_Answer(\'' + DecisionTree_Arr[DecisionTree_Number][i] + '\',' + Math.round(i/2) + ',' + (DecisionTree_DivID) + ')">' + DecisionTree_Arr[DecisionTree_Number][i+1] + '</a></span>'
	}

	if(document.layers){document.layers['DecisionTreeDiv'+DecisionTree_DivID].document.write(sMyText); document.layers['DecisionTreeDiv'+DecisionTree_DivID].document.close(); }
	if(document.all){ document.all['DecisionTreeDiv'+DecisionTree_DivID].innerHTML = sMyText; }
	if(!document.all && document.getElementById){ document.getElementById('DecisionTreeDiv'+DecisionTree_DivID).innerHTML = sMyText; }

	DecisionTree_Fade()
}


function DecisionTree_Back(){
	if(DecisionTree_DivID>1){
	if(document.layers){document.layers['DecisionTreeDiv'+DecisionTree_DivID].document.write(''); document.layers['DecisionTreeDiv'+DecisionTree_DivID].document.close(); }
	if(document.all){ document.all['DecisionTreeDiv'+DecisionTree_DivID].innerHTML = ''; }
	if(!document.all && document.getElementById){ document.getElementById('DecisionTreeDiv'+DecisionTree_DivID).innerHTML = ''; }
	DecisionTree_DivID--
	}
}


function DecisionTree_Answer(my_AnswerID, my_ImageID, my_LevelID){
	if(my_LevelID!=DecisionTree_DivID){ return;}
	
	if(DecisionTree_isAnswered){ return; }
	else{ DecisionTree_isAnswered = true; }

	DecisionTree_DivID++
	DecisionTree_ArrowID = my_ImageID
	
	DecisionTree_Number = 0
	for(i=0;i<DecisionTree_Arr.length;i++){
		if(my_AnswerID==DecisionTree_Arr[i][0]){ DecisionTree_Number = i }
	}
	DecisionTree_Fade(true)
}


function DecisionTree_Fade(fadeOut, opac){
	if(opac==null){ opac = 0 }
	if(opac < 100){
		//window.scrollBy(0,10)
		//opac+=5;
		opac=100;
		if(document.all && document.getElementById && !window.opera){
			var obj = document.getElementById('DecisionTreeDiv'+DecisionTree_DivID)
			if (obj.filters.alpha)
				obj.filters.alpha.opacity = (fadeOut?100-opac:opac);
			else
				obj.style.filter = 'alpha(opacity=(fadeOut?100-opac:opac))';
		}
		if(!document.all && document.getElementById){ document.getElementById('DecisionTreeDiv'+DecisionTree_DivID).style.MozOpacity = (fadeOut?100-opac:opac) + "%"; }
		setTimeout("DecisionTree_Fade(" + fadeOut + "," + opac + ")", (DecisionTree_TransitionTime/20) );
	}
	else{
		if(fadeOut){ DecisionTree_Display() }
		else{ DecisionTree_isAnswered = false; }
	}
}

function complianceOutput(){
	var html = '<br /><hr /><br />';
	for(i=0;i<DecisionTree_Arr.length;i++){
		if (DecisionTree_Arr[i][0] != null) {
			html += '<div style="border:1px solid #193191; margin:5px; padding:2px 0 0 5px;">';
			html += '<b>' + DecisionTree_Arr[i][0] + '</b><br />';
			html += '<div style="margin:10px 0 10px 25px;">' + DecisionTree_Arr[i][1] + '</div>';
			if(DecisionTree_Arr[i].length>2){
				html += '<b>Options</b><ul>';
				for(j=2;j<DecisionTree_Arr[i].length;j=j+2){
					html += '<li>' + DecisionTree_Arr[i][j+1].replace(/(<([^>]+)>)/ig,"") + ' - <i>(goto ' + DecisionTree_Arr[i][j] + ')</i><br />&nbsp;</li>';
				}
				html += '</ul>';
			}
			html += '</div>';
		}
	}
	document.getElementById('complianceoutput').innerHTML = html + '<p>&nbsp;</p>';
}

DecisionTree_TransitionTime = 1500;

//Question 1
DecisionTree_Add( "Q1", "Q) Confident you can identify your financial needs and choose the most suitable combination of products to meet them, without expert advice?" ,
						"Q2", "<p>Yes, I'm confident I will make the right choices</p>" ,
						"Q3", "<p>No, I think I need advice</p>" )

//Question 2
DecisionTree_Add( "Q2", "Q) Happy to spend your time researching your financial needs and identifying the most suitable financial products to meet them?" ,
						"Summary2", "<p>Yes, I'm happy to do it all myself</p>" ,
						"Q3", "<p>No, I'd prefer expert help</p>" )

//Question 3
DecisionTree_Add( "Q3", "Q) Want your adviser to choose you the most suitable product(s) from just one financial company?" ,
						"Summary3", "<p>Yes, I'm happy with advice on just one company's products</p>" ,
						"Q4", "<p>No, I'd want recommendations based on more than one company's products</p>" )

//Question 4
DecisionTree_Add( "Q4", "Q) Want your adviser to choose you the most suitable product(s) from a limited number of financial companies (eg four or five)?" ,
						"Summary4", "<p>Yes, I'm happy with limited choice</p>" ,
						"Summary6", "<p>No, I want recommendations based on a full range of financial products</p>" )

//Summary2
DecisionTree_Add( "Summary2", "<p>Sounds like the DIY route suits you, for now at least, although remember that financial circumstances tend to get more complex as you get older.  Try to plan your finances to be as tax efficient as possible overall.  Finally you should be aware that, by opting for the self-select approach, you have no access to financial redress should the products you choose turn out to be unsuitable.</p>" )

//Summary3
DecisionTree_Add( "Summary3", "<p>A tied adviser, found in most banks and building societies, will offer you advice based on the products of just one financial company, with which the adviser has a direct commercial relationship.</p><p>Q) Happy that the products your adviser can recommend will be restricted by commercial relationships with certain financial companies?</p>" ,
						"Summary5", "<p>Yes, I'm comfortable that commercial relationships will restrict the products my adviser can offer</p>" ,
						"Summary6", "<p>No, I'd rather my adviser had no commercial relationships restricting the products they can advise on</p>" )

//Summary4
DecisionTree_Add( "Summary4", "<p>A multi-tied adviser will offer you advice based on the products of a limited range of financial companies, with which the adviser has direct commercial relationships.</p><p>Q) Happy that the products your adviser can recommend will be restricted by commercial relationships with certain financial companies?</p>" ,
						"Summary5", "<p>Yes, I'm comfortable that commercial relationships will restrict the products my adviser can offer</p>" ,
						"Summary6", "<p>No, I'd rather my adviser had no commercial relationships restricting the products they can advise on</p>" )

//Summary5
DecisionTree_Add( "Summary5", "<p>Sounds like a tied/ multi-tied adviser suits you, for now at least.  Remember, all types of advisers are now required to disclose a menu of costs for their advice.  This means that not only can you compare the up-front cost of different [tied/ multi-tied] advisers, but you can also contrast these costs with what an independent financial adviser would charge for unbiased advice, based on access to the whole market of financial companies and products.</p>" )

//Summary6
DecisionTree_Add( "Summary6", "<p>Only an independent financial adviser (IFA) can recommend the most suitable combination of products for you from all the providers on the market, free of commercial ties to financial companies.  They must offer you the option of paying by fee, although many will offer the choice of paying for the advice through commission on the products you buy, or a combination of fee and product commission.  Click <a href='/find-an-ifa/' target='_blank'>here</a> to find a local IFA to meet your financial needs.</p><p>For further information regarding your financial needs visit <a href='/' target='_blank'>www.unbiased.co.uk</a>.</p>" )

$(document).ready(function() {
	DecisionTree_Display();
}); 
