/** 
 * @fileoverview This file instantiates most of the other classes in the app.
 * All calls to class methods from the html (index.html) are made via this class.
 * @author nick stewart @nicholas.stewart@bbc.co.uk
 * @version 1.3 
 */
function Main()
	{
		/**
		 * Total number of questions in questionnaire  - note this is not as straight forward
		 * as it might seem, since it is possible for a single question from the user's point of view to be
		 * several questions from this program's point of view, eg. q8 of the Debt Test.
		 */
		this._numQuestions = 18;
		/**
		 * Number of questions on each 'page'. If only one question on a page, you must still
		 * provide upper and lower range bounds - for one question, same number ie. [11,11].
		 */
		this._profile = new Array("buffer",[1,5],[6,8],[9,10],[11,11],[12,13],[14,16],[17,18]);
		/**
		 * Identifies those questions which expect text for an answer - since there is currently
		 * only one such question on any given page this works fine, although the buffers are required to force
		 * the appropriate array index to a page number.
		 */
		this._questionType = new Array("buffer","moreBuffer");
		/**
		 * This array contains those questions whose answers control the display
		 * (activating/deactivating) of later questions.
		 */
		this._greyedOut = new Array("buffer",2,3,4,7,8,12);
		
		//alert('main');
		
		/**
		 * @constructor These are the constructors for all other classes (sub-classes).
		 */
		this._data = new Data(this._numQuestions,this._greyedOut); 
		this._filter = new Filter(this);
		this._validate = new Validate(this._data,this); 
		this._error = new Error(this._type,this._qNum);
		this._navigation = new Navigation(this._data);
		this._process = new Process(this._data,this);
		this._results = new Results(this._data);
	}
	
/**
 * Whenever a form element in the html recieves data, an onClick action calls this method to record that data.
 */
Main.prototype.update = function(id,q)
	{
		this._data.update(id,q);
	}
	
/**
 * When the 'Next' button on any 'page' in the html is clicked, this function is called.
 * It decides what to do based on the current 'page' number.
 */
Main.prototype.doFilter = function(page)
	{
		this._filter.doFilter(page);
	}

/**
 * Validates the input data on a 'page'.
 */
Main.prototype.doValidate = function(page)
	{
		var range = this._profile[page];
		var qType = this._questionType[page];
		this._validate.doValidate(page,range,qType);
	}

/**
 * Generates error message alert boxes depending on the error type.
 * @ param (string)
 * @ param (integer)
 */
Main.prototype.generateMessage = function(errorType,questionNumber)
	{
		this._error.generateMessage(errorType,questionNumber);
	}

/**
 * Decides which is the next page (pages do not always run in numerical order) and reveals/hides the divs
 * that form each page.
 */	
Main.prototype.doNavigate = function(page)
	{
		this._navigation.doNavigate(page);
	}

/**
 * When the Questionnaire is completed, the Debt Test requires that certain calculations are done on the data
 * before the results are output. The Financial Healthcheck does not do any calculations.
 */	
Main.prototype.doCalculate = function()
	{
		this._process.doCalculate();
	}

/**
 * When all the questions have been answered (and validated), doCalculate is called, which then calls doResults.
 * If the app requires no calculation on the input data, doResults is called. (Note this decision is hard-wired,
 * not dynamic.)
 */	
Main.prototype.doResults = function()
	{
		this._process.doResults();
	}

/**
 *  Called via HTML Body onLoad action, when browser refresh button is clicked.
 */
Main.prototype.clearAll = function()
	{
		this._navigation.clearAll();
	}

/**
 * Called when the application Restart button is clicked.
 */
Main.prototype.startAgain = function()
	{
		this._navigation.startAgain();
	}