
function EnforceMaximumLength(fld,len) {
			if(fld.value.length > len) { fld.value = fld.value.substr(0,len); }
		}
		

function setRadioValue(theObj,theObjValue) {
	for (var i=0; i<theObj.length; i++) {
		if (theObj[i].value==theObjValue) {
			theObj[i].checked = true;
		}
	}
}
		
function appendOptionLast(listId, text) {
	var elOptNew = document.createElement('option');
	elOptNew.text = text;
	elOptNew.value = text;
	var elSel = document.getElementById(listId);

	try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		elSel.add(elOptNew); // IE only
	}
}

function removeOptionLast()
{
	var elSel = document.getElementById('selectX');
	if (elSel.length > 0)
	{
		elSel.remove(elSel.length - 1);
	}
}


//This function uses AJAX to load the answer types into
//the answer types combo box in the addQuestion.html
function loadAnswerTypes() {
	var xmlhttp;
	if (window.XMLHttpRequest)
  	{
  
  		// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}
	else if (window.ActiveXObject)
  	{
  		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	else
  	{
  		alert("Your browser does not support XMLHTTP!");
  	}
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{
			while(document.getElementById('answerCategory').length > 0) {
				document.getElementById('answerCategory').remove(0);
			}
			
			
  			var str = xmlhttp.responseText;
  			str = str.substring(1, str.length - 1);
  			var array = str.split('","');
  			for(i = 0; i < array.length; i++) {
  				appendOptionLast('answerCategory', array[i]);
  			}
			
			loadCorrectAnswers(document.forms.questionForm.answerCategory.value);
  		}
	}
	xmlhttp.open("GET","getAnswerTypes.php",true);
	xmlhttp.send(null);
	
	
}
//This function uses AJAX to load the answer types into
//the answer types combo box in the addQuestion.html
function loadCorrectAnswers(answertype) {
	var xmlhttp;
	if (window.XMLHttpRequest)
  	{
  
  		// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}
	else if (window.ActiveXObject)
  	{
  		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	else
  	{
  		alert("Your browser does not support XMLHTTP!");
  	}
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{
  			var str = xmlhttp.responseText;
  			
  			//Clear the existing list
			var sel = document.getElementById('correctAnswer');  			
  			while(sel.length > 0) {
  				sel.remove(0);
  			}  			
  			
  			//If nothing returned, we're done
  			if(str.length == 0)
  				return; 				

  			//Process the returned string
  			str = str.substring(1, str.length - 1);
  			var array = str.split('","');
  			for(i = 0; i < array.length; i++) {
  				appendOptionLast('correctAnswer', array[i]);
  			}
  		}
	}
	xmlhttp.open("GET","getCorrectAnswers.php?answertype=" + answertype,true);
	xmlhttp.send(null);
}

function removeNewWrongAnswer() {
	var sel = document.forms.questionForm.newWrongAnswers;
	if(sel.selectedIndex == -1)
		return;
		
	sel.remove(sel.selectedIndex);
}

function startupCode() {
	document.forms.questionForm.questionTextArea.focus();
	loadAnswerTypes();
	document.getElementById('newWrongAnswers').remove(0);
}

function submitMyForm() {
	
	var sel = document.forms.questionForm.newWrongAnswers;
	sel.multiple = "multiple";
	for(var i = 0; i < sel.length; i++) {
		
		sel[i].selected = true;
	}
	document.forms.questionForm.submit();
}

function parseQueryString() {
	var qString = window.location.search.substring(1);
	var listName = "random";	//list
	var minDifficulty = 1;
	var maxDifficulty = 10;		
	var currentQuestion = 0;	//currQ
	
	if(qString.length > 0) {
		var stringParts = qString.split("&");
	
		//Wend through the queryString variables and add them in
		for(var i = 0; i < stringParts.length; i++) {
			var parts = stringParts[i].split("=");
			switch(parts[0].toUpperCase()) {
				case "LIST": {
					listName = parts[1];
					break;
				}
				case "DIFFICULTY": {
					if(parts[1].indexOf("-") != -1) {
						var minmax = parts[1].split("-");
						minDifficulty = minmax[0];
						maxDifficulty = minmax[1];
					} else {
						minDifficulty = parts[1];
						maxDifficulty = parts[1];
					}
				}
				case "CURRQ": {
					currentQuestion = parts[1];
				}
			}
		}
	}
			
	loadQuestion(listName, currentQuestion,minDifficulty, maxDifficulty);
			
}
function loadQuestion(list, currentQuestion, minDiff, maxDiff) {
	var xmlhttp;
	if (window.XMLHttpRequest)
  	{
  
  		// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}
	else if (window.ActiveXObject)
  	{
  		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	else
  	{
  		alert("Your browser does not support XMLHTTP!");
  	}
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{
  			var str = xmlhttp.responseText;
  			
			//Split the string into questions and answers
			str = str.substring(1, str.length - 1);
			var parts = str.split("\",\"");
			
			//Set the question text
			document.getElementById('questionBox').innerHTML = parts[0];
			
			//Get currentQuestion Number
			var currQ = parts[1];
			currentQuestionHolder = currQ;
			
			//Get the correct answer
			var correctAnswer = parts[2];
			correctAnswerHolder = correctAnswer;
			
			//Get Number of answers
			var answerCount = parts.length - 3;
			
			//Display Answers
			for(var i = 0; i < 4; i++) {
				switch(i) {
					case 0: {
						if(answerCount < 1) {
							//hide the answer box
							document.getElementById('answerBox1').style.visibility = "hidden";
						} else {
							document.getElementById('answerText1').innerHTML = parts[i + 3];
						}						
						break;
					}
					case 1: {
						if(answerCount < 2) {
							//hide the answer box
							document.getElementById('answerBox2').style.visibility = "hidden";
						} else {
							document.getElementById('answerText2').innerHTML = parts[i + 3];
						}						
						break;
					}
					case 2: {
						if(answerCount < 3) {
							//hide the answer box
							document.getElementById('answerBox3').style.visibility = "hidden";
						} else {
							document.getElementById('answerText3').innerHTML = parts[i + 3];
						}						
						break;
					}
					case 3: {
						if(answerCount < 4) {
							//hide the answer box
							document.getElementById('answerBox4').style.visibility = "hidden";
						} else {
							document.getElementById('answerText4').innerHTML = parts[i + 3];
						}						
						break;
					}
				}
			}
			
  			//Hide the loading Gif
			document.getElementById('loadingDiv').style.visibility="hidden";
			
  		}
	}
	xmlhttp.open("GET","getQuestion.php?list=" + list + "&currQ=" + currentQuestion + "&minDiff=" +
		minDiff + "&maxDiff=" + maxDiff,true);
	xmlhttp.send(null);
}
function selectRepeatStyle() {
	if(document.startForm.qOrder.value=='RANDOM') {
		document.getElementById('startFormRepeatSpan').innerHTML = 'Allow repeats?';
	} else {
		document.getElementById('startFormRepeatSpan').innerHTML = 'Loop through questions?';
	}
}
function keywordSelectChange() {
	if(document.startForm.keyword.selectedIndex == 0) {
		document.startForm.keyword.selectedIndex = 0;
	}
}

function processAnswer(answerText) {
	answerText = answerText.replace("'", "&#39");
	if(answerText == correctAnswerHolder) {
		document.getElementById('answerReportText').innerHTML = "Correct!";
		document.getElementById('answerDiv').style.visibility = "visible";
	} else {
		document.getElementById('answerReportText').innerHTML = "Incorrect!";
		document.getElementById('answerDiv').style.visibility = "visible";
	}
}
function nextQuestion() {
	var qs = new Querystring();
	var listType = qs.get("list", "random")

	document.getElementById('answerDiv').style.visibility = "hidden";
	var nextQ = Number(currentQuestionHolder) + 1;
	var targetString = "?list=" + listType + "&difficulty=0-10&currQ=" + nextQ;
	document.location.href=targetString;
}

