/*

Handles the page by page question bank random picking.

*/


function _expandRanges(whatIDArray)
{
	this.parentObj.debug("expandRanges(whatIDArray)", 1, whatIDArray, false)
	var count = 0;
	var newIDsArray = new Array()
	while (count < whatIDArray.length)
	{
		var rangeMatchRegExp = /\d{3}-\d{3}/gi
		var rangeMatch = whatIDArray[count].match(rangeMatchRegExp)
		if (rangeMatch)
		{
			var IDStem = whatIDArray[count].slice(0,whatIDArray[count].lastIndexOf(rangeMatch))
			var startRangeNum = Number(rangeMatch.toString().split("-")[0]);
			var endRangeNum = Number(rangeMatch.toString().split("-")[1]);
			//alert("startRangeNum:" + startRangeNum)
			//alert("startRangeNum:" + parseInt(startRangeNum))
			var iCount = 0;
			var rangeToGenerate = (endRangeNum - startRangeNum)
			while (iCount <= rangeToGenerate)
			{
				newIDsArray[newIDsArray.length] = IDStem + this.padDigits(iCount + parseInt(startRangeNum), 3)
				iCount++
			}
		}
		else
		{
			newIDsArray[newIDsArray.length] = whatIDArray[count]
		}
		count++;
	}
	this.parentObj.debug("expandRanges()", 1, newIDsArray, false)
	return newIDsArray
}



function _padDigits(whatNum, howManyDigits)
{
	this.parentObj.debug("padDigits(whatNum: " + whatNum + ", howManyDigits: " + howManyDigits + ")", 1, false, true)
	var numString = whatNum.toString()
	while (numString.length < howManyDigits)
		numString = "0" + numString;
	return numString
}

function _chooseBankQuestions(howMany)
{
	this.parentObj.debug("_chooseBankQuestions(howMany: " + howMany + ")", 1, this, true);
	var compositeIDArray = new Array();
	var breakCount = 0;
	while ((compositeIDArray.length < howMany) && (breakCount < 100))
	{
		// Check it isn't a 1 element array where the element is ""
		if ((this.primaryIDs.length > 0) && (this.primaryIDs[0] != ""))
		{
			// If there are less than the total needed, copy them all into the new array.
			if (this.primaryIDs.length < howMany)
			{
				//alert(1)
				compositeIDArray = this.joinArrays(this.primaryIDs, compositeIDArray);
				this.primaryIDs = new Array();
			}
			else	// Otherwise remove them until the array is full.
			{
				this.parentObj.debug("WARNING: Any optional questions specified will be ignored as the mandatory question ID's have provided all (or more) of the required questions (howMany:" + howMany + ").", 2, this, false)
				var newCompositeIDIndex = this.getRandom(0, (this.primaryIDs.length - 1));
				compositeIDArray[compositeIDArray.length] = this.primaryIDs[newCompositeIDIndex];
				this.primaryIDs = this.removeElement(this.primaryIDs, newCompositeIDIndex);
			}
		}
		// Check it isn't a 1 element array where the element is ""
		else if ((this.optionalIDs.length > 0) && (this.optionalIDs[0] != ""))
		{
			// If there are less than the total needed, copy them all into the new array.
			if (this.optionalIDs.length < (howMany - compositeIDArray.length))
			{
				this.parentObj.debug("Adding ALL, this.optionalIDs.length: " + this.optionalIDs.length + ", howMany: " + howMany, 1)
				compositeIDArray = this.joinArrays(this.optionalIDs, compositeIDArray);
				this.optionalIDs = new Array();
			}
			else	// Otherwise remove them until the array is full.
			{
				this.parentObj.debug("Adding SOME, this.optionalIDs.length: " + this.optionalIDs.length + ", howMany: " + howMany, 1)
				var newCompositeIDIndex = this.getRandom(0, (this.optionalIDs.length - 1));
				compositeIDArray[compositeIDArray.length] = this.optionalIDs[newCompositeIDIndex];
				this.optionalIDs = this.removeElement(this.optionalIDs, newCompositeIDIndex);
			}
		}
		if ((this.primaryIDs.length == 0) && (this.optionalIDs.length == 0))
		{
			this.parentObj.debug("PLEASE PROVIDE MORE 'Mandatory questions' OR 'Optional questions' ID's OR REDUCE THE 'Number of questions to display'", 3, this, false)
			break;
		}
		breakCount++;
	}
	if (breakCount == 100)
	{
		var debugArray = new Array()
		debugArray['manditory IDs'] = this.primaryIDs
		debugArray['optional IDs'] = this.optionalIDs
		debugArray['compositeIDArray IDs'] = compositeIDArray
		this.parentObj.debug("ERROR: The current screen was asked to pick " + howMany + " bank questions. However, the code was only able to find " + compositeIDArray.length + ", please provide more ID's",3,debugArray,true)
	}
	this.parentObj.debug("_chooseBankQuestions(), compositeIDArray: ", 1, compositeIDArray, true)
	return compositeIDArray
}

function _chooseAllQuestions()
{
	this.parentObj.debug("this.chooseAllQuestions()",1)
	var returnArray = new Array()
	if ((this.primaryIDs) && (this.primaryIDs[0] != ""))
	{
		returnArray = this.joinArrays(this.primaryIDs, returnArray)
	}
	if ((this.optionalIDs) && (this.optionalIDs[0] != ""))
	{
		returnArray = this.joinArrays(this.optionalIDs, returnArray)
	}
	return returnArray
}

function _removeElement(whatArray, whatIndex)
{
	var returnArray = new Array()
	for (var currIndex in whatArray)
	{
		if (currIndex != whatIndex)
			returnArray[returnArray.length] = whatArray[currIndex]
	}
	return returnArray;
}

function _joinArrays(array1, array2)
{
	var returnArray = new Array()

	for (var currIndex in array1)
		returnArray[returnArray.length] = array1[currIndex]

	for (var currIndex in array2)
		returnArray[returnArray.length] = array2[currIndex]

	return returnArray;
}

function _getRandom(minNum, maxNum)
{
	return minNum + Math.round((Math.random() * maxNum))
}

function _mixEmUp(a, b)
{
	return Math.round(Math.random());
}


function _buildReferences()
{
	var scriptBlockString = "<script>var idsToExpect = new Array('" + this.currIDs.join("','") + "')</script>\n"
	var count = 0
	while (count < this.currIDs.length)
	{
		scriptBlockString += "<script src='" + this.parentObj.rootPath + "data/data1/" + this.currIDs[count] + "_qnData1.js'></script>\n";
		scriptBlockString += "<script src='" + this.parentObj.rootPath + "data/data2/" + this.currIDs[count] + "_qnData2.js'></script>\n";
		scriptBlockString += "<script src='" + this.parentObj.rootPath + "data/data3/" + this.currIDs[count] + "_qnData3.js'></script>\n";
		count++
	}
	scriptBlockString += "<script>startChecking()</script>\n"
	return scriptBlockString
}



function _checkIDUsed()
{
	var usedIDString = "";
	var idsToAdd = new Array();
	var count = 0;
	// Check the ID's against the currently used ID's
	while (count < this.usedIDsArray.length)
	{
		var iCount = 0;
		while (iCount < this.currIDs.length)
		{
			if (this.currIDs[iCount] == this.usedIDsArray[count])
			{
				usedIDString += this.currIDs[iCount] + "\n"
			}
			iCount++;
		}
		count++;
	}
	if (usedIDString != "")
	{
		this.parentObj.debug("WARNING: The following Id's have been used before, possibly indicating different screen questions are using the same pool of bank questions:\n\n" + usedIDString, 3, this, false)
	}
	// Add the current ID's to the used ID's array
	this.usedIDsArray = this.joinArrays(this.currIDs, this.usedIDsArray)
}

function _insertScripts(whatLocation, howMany, whatPrimaryIDs, whatOptionalIDs)
{
	if (!this.generatedScriptBlocks[whatLocation])
	{
		this.parentObj.debug("padDigits(whatPrimaryIDs: " + whatPrimaryIDs + ", whatOptionalIDs: " + whatOptionalIDs + ")", 1, new Array(whatPrimaryIDs, whatOptionalIDs), false)

		this.primaryIDs = this.expandRanges(whatPrimaryIDs)
		this.optionalIDs = this.expandRanges(whatOptionalIDs)

		if (!showAllOptions)
		{
			this.parentObj.debug("Not showing all options, picking " + howMany, 1)
			this.currIDs = this.chooseBankQuestions(howMany)
			this.currIDs = this.currIDs.sort(_mixEmUp)
		}
		else
		{
			this.parentObj.debug("Showing all options",1)
			this.currIDs = this.chooseAllQuestions()
		}
		this.parentObj.debug("this.currIDs: " + this.currIDs, 1, this.currIDs)
		var scriptBlock = this.buildReferences();

		this.generatedScriptBlocks[whatLocation] = scriptBlock;

		this.checkIDUsed();

		return scriptBlock;
	}
	else
	{
		return this.generatedScriptBlocks[whatLocation]
	}
}



function questionBankHandlerObject(whatParent)
{
	whatParent.debug("NEW questionBankHandlerObject()", 0)
	whatParent.debug("questionBankHandlerObject(whatParent: " + whatParent + ")", 1, false, true)

	this.parentObj = whatParent;
	this.usedIDsArray = new Array();
	this.currIDs;
	this.primaryIDs;
	this.optionalIDs;

	this.generatedScriptBlocks = new Array()

	this.parentObj.debug("Created questionBankHandlerObject()", 1, this, false)
}

questionBankHandlerObject.prototype.buildReferences = _buildReferences;
questionBankHandlerObject.prototype.chooseBankQuestions = _chooseBankQuestions;
questionBankHandlerObject.prototype.chooseAllQuestions = _chooseAllQuestions;


questionBankHandlerObject.prototype.checkIDUsed = _checkIDUsed;
questionBankHandlerObject.prototype.expandRanges = _expandRanges;
questionBankHandlerObject.prototype.padDigits = _padDigits;
questionBankHandlerObject.prototype.getRandom = _getRandom;
questionBankHandlerObject.prototype.removeElement = _removeElement;
questionBankHandlerObject.prototype.joinArrays = _joinArrays;

questionBankHandlerObject.prototype.insertScripts = _insertScripts;
