var hex;
	
function MakeArray()
{
    this.length = 16;
    return this;
}

// populates an array with the hexidecimal characters
function Populate()
{
    hex = new MakeArray();
    hex[1] = "0";    
    hex[2] = "1";    
    hex[3] = "2";    
    hex[4] = "3";    
    hex[5] = "4";    
    hex[6] = "5";    
    hex[7] = "6";    
    hex[8] = "7";    
    hex[9] = "8";    
    hex[10] = "9";    
    hex[11] = "A";    
    hex[12] = "B";    
    hex[13] = "C";    
    hex[14] = "D";    
    hex[15] = "E";    
    hex[16] = "F";    
}

// function DecimaltoAnother(N, radix)
// 
// return representation of a number N
// in the system based on radix 
//
function DecimaltoAnother(N, radix)
{
    s = "";

    A = N;
    while (A >= radix)
    {
        B = A % radix;
        A = Math.floor(A / radix);
        s += hex[B+1];
    }
    
    s += hex[A+1];
    return Transpose(s);
}

// function Transpose(s)
//
// return a string written from right to left
//
function Transpose(s)
{
    N = s.length;

    t = "";

    for (i = 0; i < N; i++)
        t = t + s.substring(N-i-1, N-i);

    s = t;
    return s;
}


// converts parameter strBinary into a hexidecimal number
// Populate needs to be called before binaryToHex() can be called
function binaryToHex(strBinary) {
	if (strBinary.length != 0) {
	        M = parseInt(strBinary, 2);
	} else {
		M = 0;
	}
	return DecimaltoAnother(M, 16);
}

// converts parameter strHex into a binary number
// Populate() needs to be called before hexToBinary() can be called
function hexToBinary(strHex) {
	if (strHex.length != 0) {
		M = parseInt(strHex, 16);
	} else {
		M = 0;
	}
	return DecimaltoAnother(M, 2);
}

// splits a large binary number (in this case, probably 112 characters) into 8-digit blocks
// returns the 8 digit blocks in an array
function splitBigBinary(strBinary) {
	var arrMultiBinary = new Array(14);
	if (strBinary.length != 0) {
		for(var i = 0; i < strBinary.length/8; i++) {
			arrMultiBinary[i] = strBinary.substr(((i*8)), 8);
		}
	}
	return arrMultiBinary;
}

// splits a large hexidecimal number (in this case, probably 28 characters) into 2-digit blocks
// returns the 2 digit blocks in an array
function splitBigHex(strHex) {
	var arrMultiHex = new Array(14);
	if (strHex.length != 0) {
		for(var i = 0; i < strHex.length/2; i++) {
			arrMultiHex[i] = strHex.substr(((i*2)), 2);
		}
	}
	return arrMultiHex;
}

function checkBoxPageSubmit(intQuestionsOnPage, intCharactersBeforeThisPage, pageName) {
	
	// call the populate function to populate the HEX array		
	Populate();
	
	var strHexQS = new String();
	var strBigBinary = new String();
	var strNewHex = new String();
	var arrBigBinaries = new Array();
	var arrBigHexes = new Array();
	var strNewBigBinary = new String();
	var tempHex = new String();
	var tempBinary = new String();
	var strNewBinary = new String();
	
	// initialize some variables
	strNewHex = '';
	
	// get the hex number from the querystring
	strHexQS = document.location.search.substr(3);
	
	// if for some reason, we can't get the right querystring, use a blank one
	if (strHexQS.length < 28) {
		strHexQS = '0000000000000000000000000000';
	}
	// split the Hex from the querystring into an arrary containing 2-character hexadecimal numbers
	arrBigHexes = splitBigHex(strHexQS);
	
	// using 2-digit hexes, convert them to binary 
	for (var i=0; i < arrBigHexes.length; i++) {
		tempBinary = hexToBinary(arrBigHexes[i]);
		if (tempBinary.length < 8) {	
			// add additional leading zeros to make sure this is 8 characters
			for (var j=tempBinary.length; j < 8 ; j++) {
				tempBinary = '0' + tempBinary;
			}
		}
		strNewBinary = strNewBinary + tempBinary;
	}
	
	
	// copy the value for the big binary into the BigBinary variable
	strBigBinary = strNewBinary;
	
	// use the first intCharactersBeforeThisPage characters 
	// then add the responses from this page's form
	// in the BigBinary with the corresponding answers from this form
	strNewBigBinary = strBigBinary.substr(0,intCharactersBeforeThisPage);		
	
	for (var i = 1; i <= intQuestionsOnPage; i++) {
		// ok, this form element needs to be added to the new Big Binary
		// if this element is checked, then add a 1 in spot i
		// if not checked, add a 0 in spot i
		if (document.forms['theForm']['check_'+i].checked) {
			strNewBigBinary = strNewBigBinary + '1';
		} else {
			strNewBigBinary = strNewBigBinary + '0';
		}
	}
	
	// so now we should have strNewBigBinary with 12 0's and 1's in it. 
	// let's tack on anything coming from the old BigBinary
	strNewBigBinary = strNewBigBinary + strBigBinary.substr(intCharactersBeforeThisPage + intQuestionsOnPage);
	
	// split big binary into array of 8-digit binary numbers
	arrBigBinaries = splitBigBinary(strNewBigBinary);
	
	// using 8-digit binaries, convert them to hex, 
	for (var i=0; i < arrBigBinaries.length; i++) {
		tempHex = binaryToHex(arrBigBinaries[i]);
		if (tempHex.length == 1) {
			tempHex = '0' + tempHex;
		}
		strNewHex = strNewHex + tempHex;
	}
	if (window.opener) {
		window.opener.strHexQS = strNewHex;
	}
	// tack on Hex to QS before submitting page
	document.location.href = pageName + '?i=' + strNewHex;
}

function checkBoxPageGetHex(intQuestionsOnPage, intCharactersBeforeThisPage) {
	
	// call the populate function to populate the HEX array		
	Populate();
	
	var strHexQS = new String();
	var strBigBinary = new String();
	var strNewHex = new String();
	var arrBigBinaries = new Array();
	var arrBigHexes = new Array();
	var strNewBigBinary = new String();
	var tempHex = new String();
	var tempBinary = new String();
	var strNewBinary = new String();
	
	// initialize some variables
	strNewHex = '';
	
	// get the hex number from the querystring
	strHexQS = document.location.search.substr(3);
	
	// if for some reason, we can't get the right querystring, use a blank one
	if (strHexQS.length < 28) {
		strHexQS = '0000000000000000000000000000';
	}
	// split the Hex from the querystring into an arrary containing 2-character hexadecimal numbers
	arrBigHexes = splitBigHex(strHexQS);
	
	// using 2-digit hexes, convert them to binary 
	for (var i=0; i < arrBigHexes.length; i++) {
		tempBinary = hexToBinary(arrBigHexes[i]);
		if (tempBinary.length < 8) {	
			// add additional leading zeros to make sure this is 8 characters
			for (var j=tempBinary.length; j < 8 ; j++) {
				tempBinary = '0' + tempBinary;
			}
		}
		strNewBinary = strNewBinary + tempBinary;
	}
	
	
	// copy the value for the big binary into the BigBinary variable
	strBigBinary = strNewBinary;
	
	// use the first intCharactersBeforeThisPage characters 
	// then add the responses from this page's form
	// in the BigBinary with the corresponding answers from this form
	strNewBigBinary = strBigBinary.substr(0,intCharactersBeforeThisPage);		
	
	for (var i = 1; i <= intQuestionsOnPage; i++) {
		// ok, this form element needs to be added to the new Big Binary
		// if this element is checked, then add a 1 in spot i
		// if not checked, add a 0 in spot i
		if (document.forms['theForm']['check_'+i].checked) {
			strNewBigBinary = strNewBigBinary + '1';
		} else {
			strNewBigBinary = strNewBigBinary + '0';
		}
	}
	
	// so now we should have strNewBigBinary with 12 0's and 1's in it. 
	// let's tack on anything coming from the old BigBinary
	strNewBigBinary = strNewBigBinary + strBigBinary.substr(intCharactersBeforeThisPage + intQuestionsOnPage);
	
	// split big binary into array of 8-digit binary numbers
	arrBigBinaries = splitBigBinary(strNewBigBinary);
	
	// using 8-digit binaries, convert them to hex, 
	for (var i=0; i < arrBigBinaries.length; i++) {
		tempHex = binaryToHex(arrBigBinaries[i]);
		if (tempHex.length == 1) {
			tempHex = '0' + tempHex;
		}
		strNewHex = strNewHex + tempHex;
	}
	if (window.opener) {
		window.opener.strHexQS = '?i=' + strNewHex;
	}
}


function populateCheckBoxes(intQuestionsOnPage, intCharactersBeforeThisPage, strGlobalBigBinary) {
	// for each checkbox, if (questionAlreadyChecked(i, strGlobalBigBinary)) , then set checked to true
	for (var i = 1; i <= intQuestionsOnPage; i++) {
		// ok, this form element needs to be added to the new Big Binary
		// if this element is checked, set the form element.checked = true
		if (questionAlreadyChecked(i, strGlobalBigBinary)) {
			document.forms['theForm']['check_'+i].checked = true;
		}
	}
}

function populateRadioButtons(intQuestionsOnPage, intUniqueRadioButtons, intCharactersBeforeThisPage, strGlobalBigBinary) {
	
	var intThisRadioButton = 1;
	// for each radio button, if (questionAlreadyChecked(i, strGlobalBigBinary)) , then set selected to true
	for (var i = 1; i <= intUniqueRadioButtons; i++) {
		// ok, this form element needs to be added to the new Big Binary
		// if this element is checked, then add a 1 in spot i
		// if not checked, add a 0 in spot i
		for (var j = 0; j < document.forms['theForm']['radio_'+i].length; j++) {
			if (questionAlreadyChecked(intThisRadioButton+intCharactersBeforeThisPage, strGlobalBigBinary)) {
				document.forms['theForm']['radio_'+i][j].checked = true;
			}
			intThisRadioButton++;
		}
	}
}

function radioButtonPageSubmit(intQuestionsOnPage, intUniqueRadioButtons, intCharactersBeforeThisPage, pageName) {
		
	// call the populate function to populate the HEX array		
	Populate();
	
	var strHexQS = new String();
	var strBigBinary = new String();
	var strNewHex = new String();
	var arrBigBinaries = new Array();
	var arrBigHexes = new Array();
	var strNewBigBinary = new String();
	var tempHex = new String();
	var tempBinary = new String();
	var strNewBinary = new String();
	
	
	// initialize some variables
	strNewHex = '';
	
	// get the hex number from the querystring
	strHexQS = document.location.search.substr(3);
	
	// if for some reason, we can't get the right querystring, use a blank one
	if (strHexQS.length < 28) {
		strHexQS = '0000000000000000000000000000';
	}
	
	// split the Hex from the querystring into an arrary containing 2-character hexadecimal numbers
	arrBigHexes = splitBigHex(strHexQS);
	
	// using 2-digit hexes, convert them to binary 
	for (var i=0; i < arrBigHexes.length; i++) {
		tempBinary = hexToBinary(arrBigHexes[i]);
		if (tempBinary.length < 8) {	
			// add additional leading zeros to make sure this is 8 characters
			for (var j=tempBinary.length; j < 8 ; j++) {
				tempBinary = '0' + tempBinary;
			}
		}
		strNewBinary = strNewBinary + tempBinary;
	}
	
	
	// copy the value for the big binary into the BigBinary variable
	strBigBinary = strNewBinary;
	
			
	// use the first intCharactersBeforeThisPage characters 
	// then add the responses from this page's form
	// in the BigBinary with the corresponding answers from this form
	strNewBigBinary = strBigBinary.substr(0,intCharactersBeforeThisPage);		
	
	for (var i = 1; i <= intUniqueRadioButtons; i++) {
		// ok, this form element needs to be added to the new Big Binary
		// if this element is checked, then add a 1 in spot i
		// if not checked, add a 0 in spot i
		for (var j = 0; j < document.forms['theForm']['radio_'+i].length; j++) {
			if (document.forms['theForm']['radio_'+i][j].checked) {
				strNewBigBinary = strNewBigBinary + '1';
			} else {
				strNewBigBinary = strNewBigBinary + '0';
			}
		}
	}
	
	// so now we should have strNewBigBinary with 12 0's and 1's in it from page 1
	// and 16 0's and 1's in it from this page, now we just  
	// tack on anything coming from the old BigBinary
	strNewBigBinary = strNewBigBinary + strBigBinary.substr(intCharactersBeforeThisPage + intQuestionsOnPage);
	
	// split big binary into array of 8-digit binary numbers
	arrBigBinaries = splitBigBinary(strNewBigBinary);
	
	// using 8-digit binaries, convert them to hex, 
	for (var i=0; i < arrBigBinaries.length; i++) {
		tempHex = binaryToHex(arrBigBinaries[i]);
		if (tempHex.length == 1) {
			tempHex = '0' + tempHex;
		}
		strNewHex = strNewHex + tempHex;
	}
	
	if (window.opener) {
		window.opener.strHexQS = strNewHex;
	}
	
	// tack on Hex to QS before submitting page
	document.location.href = pageName + '?i=' + strNewHex;

}


function radioButtonPageGetHex(intQuestionsOnPage, intUniqueRadioButtons, intCharactersBeforeThisPage) {
		
	// call the populate function to populate the HEX array		
	Populate();
	
	var strHexQS = new String();
	var strBigBinary = new String();
	var strNewHex = new String();
	var arrBigBinaries = new Array();
	var arrBigHexes = new Array();
	var strNewBigBinary = new String();
	var tempHex = new String();
	var tempBinary = new String();
	var strNewBinary = new String();
	
	
	// initialize some variables
	strNewHex = '';
	
	// get the hex number from the querystring
	strHexQS = document.location.search.substr(3);
	
	// if for some reason, we can't get the right querystring, use a blank one
	if (strHexQS.length < 28) {
		strHexQS = '0000000000000000000000000000';
	}
	
	// split the Hex from the querystring into an arrary containing 2-character hexadecimal numbers
	arrBigHexes = splitBigHex(strHexQS);
	
	// using 2-digit hexes, convert them to binary 
	for (var i=0; i < arrBigHexes.length; i++) {
		tempBinary = hexToBinary(arrBigHexes[i]);
		if (tempBinary.length < 8) {	
			// add additional leading zeros to make sure this is 8 characters
			for (var j=tempBinary.length; j < 8 ; j++) {
				tempBinary = '0' + tempBinary;
			}
		}
		strNewBinary = strNewBinary + tempBinary;
	}
	
	
	// copy the value for the big binary into the BigBinary variable
	strBigBinary = strNewBinary;
	
			
	// use the first intCharactersBeforeThisPage characters 
	// then add the responses from this page's form
	// in the BigBinary with the corresponding answers from this form
	strNewBigBinary = strBigBinary.substr(0,intCharactersBeforeThisPage);		
	
	for (var i = 1; i <= intUniqueRadioButtons; i++) {
		// ok, this form element needs to be added to the new Big Binary
		// if this element is checked, then add a 1 in spot i
		// if not checked, add a 0 in spot i
		for (var j = 0; j < document.forms['theForm']['radio_'+i].length; j++) {
			if (document.forms['theForm']['radio_'+i][j].checked) {
				strNewBigBinary = strNewBigBinary + '1';
			} else {
				strNewBigBinary = strNewBigBinary + '0';
			}
		}
	}
	
	// so now we should have strNewBigBinary with 12 0's and 1's in it from page 1
	// and 16 0's and 1's in it from this page, now we just  
	// tack on anything coming from the old BigBinary
	strNewBigBinary = strNewBigBinary + strBigBinary.substr(intCharactersBeforeThisPage + intQuestionsOnPage);
	
	// split big binary into array of 8-digit binary numbers
	arrBigBinaries = splitBigBinary(strNewBigBinary);
	
	// using 8-digit binaries, convert them to hex, 
	for (var i=0; i < arrBigBinaries.length; i++) {
		tempHex = binaryToHex(arrBigBinaries[i]);
		if (tempHex.length == 1) {
			tempHex = '0' + tempHex;
		}
		strNewHex = strNewHex + tempHex;
	}
	
	if (window.opener) {
		window.opener.strHexQS = '?i=' + strNewHex;
	}
}


function getBigBinary() {
	var strHexQS = new String();
	var strBigBinary = new String();
	var arrBigHexes = new Array();
	var strNewBigBinary = new String();
	var tempBinary = new String();
	var strNewBinary = new String();
	var intQuestionAnswer;
		
	// get the hex number from the querystring
	strHexQS = document.location.search.substr(3);
	
	// if for some reason, we can't get the right querystring, use a blank one
	if (strHexQS.length < 28) {
		strHexQS = '0000000000000000000000000000';
	}
	
	// split the Hex from the querystring into an arrary containing 2-character hexadecimal numbers
	arrBigHexes = splitBigHex(strHexQS);
	
	// using 2-digit hexes, convert them to binary 
	for (var i=0; i < arrBigHexes.length; i++) {
		tempBinary = hexToBinary(arrBigHexes[i]);
		if (tempBinary.length < 8) {	
			// add additional leading zeros to make sure this is 8 characters
			for (var j=tempBinary.length; j < 8 ; j++) {
				tempBinary = '0' + tempBinary;
			}
		}
		strNewBinary = strNewBinary + tempBinary;
	}
	
	
	// return the value for the big binary
	return strNewBinary;
}

function questionAlreadyChecked(intQuestionNumber, strBigBinary) {
	
	var intQuestionAnswer;
	
	// now find the character at the specified question number in big binary
	intQuestionAnswer = strBigBinary.charAt(intQuestionNumber-1);
	
	if ((intQuestionAnswer == '1') || (intQuestionAnswer == 1)) {
		return true;
	} else {
		return false;
	}
}

function calculateResults(strBigBinary) {
	
	var arrFinalSums = new Array(14);
			
	for (var i = 0; i < arrFinalSums.length; i++) {
		// initialize array variables to 0
		arrFinalSums[i] = 0;
	}
	
	for (var i = 0; i < strBigBinary.length; i++) {
		if (strBigBinary.charAt(i) == '1') {
			// sum all variables into appropriate variables
			for (var j = 0; j < arrFinalSums.length; j++) {
				arrFinalSums[j] = arrFinalSums[j] + arrQuestionScores[i][j];
			}
		}
	}
	return arrFinalSums;
}


function printResults(arrFinalSums) {
	for (var i=0; i < arrFinalSums.length; i++) {
		document.write((i+1) + '. ' + arrFinalSums[i][0] + ' (received ' + arrFinalSums[i][1] + ' points)<br>');
	}
	document.write('</p>');
}

function printTop5Results(arrFinalSums) {
	for (var i=0; i < 5; i++) {
		document.write((i+1) + '. ' + arrFinalSums[i][0] + ' (received ' + arrFinalSums[i][1] + ' points)<br>');
	}
}
