var qnum=0;  //current question number
var anum=0;  //number of questions answered

var totallinks=100;
var question=new Array(totallinks);
var answeryes=new Array(totallinks);
var answerno=new Array(totallinks);
var answeredq=new Array(20); // question numbers answered
var answeredyn=new Array(20); // yes or no answered to question

	function setquestions() {
	// question is the question
		// answeryes is next question if answer yes
		// answerno is next question if answer no
		// if answeryes=999 then answerno is the answer number
		question[0]="ERROR script has failed - answer 0";
		question[1]="Is your company currently experiencing problems with cashflow?";
		answeryes[1]=11;
		answerno[1]=12;

		question[11]="Is the company turnover less than expected?";
		answeryes[11]=21;
		answerno[11]=22;

		question[12]="Is the companies balance sheet negative?";
		answeryes[12]=23;
		answerno[12]=24;

		question[21]="Do creditors insist on immediate payment?";
		answeryes[21]=41;
		answerno[21]=42;

		question[22]="Are costs higher than anticipated?";
		answeryes[22]=42;
		answerno[22]=32;

		question[23]="Is the balance sheet negative due to large loans?";
		answeryes[23]=33;
		answerno[23]=34;

		question[24]="Do you think the nature of your business is currently viable?";
		answeryes[24]=35;
		answerno[24]=36;

		question[32]="Is the company curently making loses?";
		answeryes[32]=43;
		answerno[32]=44;

		question[33]="Are creditors willing to support the company?";
		answeryes[33]=45;
		answerno[33]=46;

		question[34]="Is the balance sheet negative due to losses brought forward?";
		answeryes[34]=47;
		answerno[34]=11;

		question[35]="Do you want to continue to run your business?";
		answeryes[35]=49;
		answerno[35]=50;

		question[36]="Are you willing to liquidate the company?";
		answeryes[36]=51;
		answerno[36]=52;

		question[41]="Can you afford the required payments?";
		answeryes[41]=61;
		answerno[41]=62;

		question[42]="Do you still believe in the viability of the business?";
		answeryes[42]=63;
		answerno[42]=64;

		question[43]="Can you afford to pay your creditors?";
		answeryes[43]=65;
		answerno[43]=66;

		question[44]="Are taxes (Corporation Tax, VAT, PAYE) affordable?";
		answeryes[44]=45;
		answerno[44]=66;

		question[45]="It seems that your business is viable";
		answeryes[45]=999;
		answerno[45]=1;

		question[46]="Pre Pack Administration (Phoenixing)";
		answeryes[46]=999;
		answerno[46]=2;

		question[47]="Is the company currently profitable?";
		answeryes[47]=67;
		answerno[47]=11;

		question[49]="It seems that your business is viable";
		answeryes[49]=999;
		answerno[49]=1;

		question[50]="Sale or Voluntary Liquidation";
		answeryes[50]=999;
		answerno[50]=4;

		question[51]="Members Voluntary Liquidation (MVL)";
		answeryes[51]=999;
		answerno[51]=5;

		question[52]="Review your business plan";
		answeryes[52]=999;
		answerno[52]=9;

		question[61]="Administration";
		answeryes[61]=999;
		answerno[61]=6;

		question[62]="Have you received court summons or judgement?";
		answeryes[62]=71;
		answerno[62]=72;

		question[63]="Do you think the company could continue to trade if historic debts were rescheduled?";
		answeryes[63]=73;
		answerno[63]=74;

		question[64]="Creditor Voluntary Liquidation (CVL)";
		answeryes[64]=999;
		answerno[64]=7;

		question[65]="It seems that your business is viable";
		answeryes[65]=999;
		answerno[65]=1;

		question[66]="Administration";
		answeryes[66]=999;
		answerno[66]=6;

		question[67]="Do you feel excessive pressure from creditors?";
		answeryes[67]=75;
		answerno[67]=76;

		question[71]="Do you still believe in the viability of the business?";
		answeryes[71]=81;
		answerno[71]=82;

		question[72]="Do you believe in the companies better future?";
		answeryes[72]=83;
		answerno[72]=84;

		question[73]="Company Voluntary Arrangement (CVA)";
		answeryes[73]=999;
		answerno[73]=8;

		question[74]="Creditors Voluntary Liquidation (CVL)";
		answeryes[74]=999;
		answerno[74]=7;

		question[75]="Pre Pack Administration (Phoenixing)";
		answeryes[75]=999;
		answerno[75]=2;

		question[76]="It seems that your business is viable";
		answeryes[76]=999;
		answerno[76]=1;

		question[81]="Do you think the company could continue to trade if historic debts were rescheduled?";
		answeryes[81]=91;
		answerno[81]=92;

		question[82]="Voluntary Liquidation";
		answeryes[82]=999;
		answerno[82]=10;

		question[83]="Company Voluntary Arrangement (CVA)";
		answeryes[83]=999;
		answerno[83]=8;

		question[84]="Creditor Voluntary Liquidation";
		answeryes[84]=999;
		answerno[84]=7;

		question[91]="Company Voluntary Arrangement (CVA)";
		answeryes[91]=999;
		answerno[91]=8;

		question[92]="Pre Pack Administration (Phoenixing)";
		answeryes[92]=999;
		answerno[92]=2;
	}

	function startagain()
	{
		//start again - refresh page
		qnum=1;
		window.location.search = "";
		window.location.hash = "";
		var sURL = (window.location.pathname);
		window.location.href = sURL;
		//document.write (sURL);
		window.location.replace( sURL );
	}

	function changeLink(yep)
	{
		var i=qnum;
		answeredq[anum]=qnum;//set question number answered
		answeredyn[anum]=yep; // set yes/no answered to question
		anum++;
		if (yep) 
			{
			qnum = answeryes[i];
			}
		else 
			{
			qnum = answerno[i];
			}
		changeQuestion(qnum,i,yep);
	}

	function changeQuestion(num,i,yep) //num=next question, i=current question, yep= yes or no answer 
	{
		//setup new question TextNode
		var newSpan = document.createElement("p");
		var newText = document.createTextNode(question[num]);
		newSpan.appendChild(newText);
		var oldSpan = document.createElement("div");
		oldSpan.setAttribute("class","greycalc")
		oldSpan.className="greycalc";

		// append yes or no text answer to last question
		if (yep)
			{
			var oldText = document.createTextNode(question[i]+" Yes");
			}
		else
			{
			var oldText = document.createTextNode(question[i]+" No");
			}
		oldSpan.appendChild(oldText);
		var spanq = document.getElementById("question");
		if (spanq.childNodes.length!= 0)
			{
			//if not first question remove previous question and append question with answer
			spanq.removeChild(spanq.lastChild);
			}
		if (qnum!=1) {
			var throwaway = spanq.appendChild(oldSpan);
			}
		if (answeryes[num] == 999)
			{
			// if result then show correct result
			//document.getElementById('finish'+answerno[num]).style.display = 'block' ; 
			document.getElementById('after1').style.display = 'block' ; 
			document.getElementById('wrxFormDiv1').style.display = 'block' ; 
			window.location.hash = "#questions";
			writeanswers(); // put answers into form field
			document.forms.theForm1.finishnum.value = answerno[num];
			document.forms.theForm1.finishbox.value = num;
			document.forms.theForm1.whatsite.value = window.location.hostname.replace("www.","");
			document.forms.theForm1.recomend.value = question[num];
			initdefaultevents();
			focusCursor();
			}
		else
			{
			// show next question
			var throwaway = spanq.appendChild(newSpan);
			}
		// hide buttons if end
		document.getElementById('buttons').style.display = (answeryes[num] == 999) ? 'none' : 'block'; 
	}

	function writeanswers() {
		var i=0;
		var newline = "\n";
		var localresponses = '';
		for (i=0;i < anum; i++) {
			localresponses += question[answeredq[i]];
			if (answeredyn[i])
				{ 
				localresponses += ' Yes'; 
				}
			else
				{ 
				localresponses += ' No'; 
				}
			localresponses += newline;
		}
		document.forms.theForm1.qresponses.value=localresponses;
		
	}
	
	function init(firsta) {
	  // start first question - remove click to start button
		if (firsta.length == 0) 
		{	 //not called from solution calc box page
			document.getElementById('start1').style.display = 'block' ; 
			changeQuestion(qnum,1,1);
		}
		else
		{
			if (parseInt(firsta) == 1) 
			{
				qnum = 11; 
			}
			else
			{ 
				qnum = 12; 
			}
			document.getElementById('start1').style.display = 'none' ; 
			document.getElementById('startagain').style.display = 'block' ; 
			answeredyn[anum]=parseInt(firsta); // set yes/no answered to question
			changeQuestion(qnum,1,parseInt(firsta));
			answeredq[anum]=1;//set question number answered
			answeredyn[anum]=parseInt(firsta); // set yes/no answered to question
			anum++;
		}
		//window.location.hash = "#questions"
	}


/* for other browsers */
/*window.onload = init;*/
qnum=1;
setquestions();
document.getElementById('start1').style.display = 'block' ; 

