TestingSelectList.prototype = new SelectList();
TestingSelectList.prototype.constructor=TestingSelectList;
TestingSelectList.prototype.parent = SelectList.prototype;

function TestingSelectList(id, multiple, expandContract, isAnonymous){ 
	return this.parent.constructor.call(this, id, multiple, expandContract);
	this.isAnonymous = isAnonymous == 'anonymous';
} 
TestingSelectList.prototype.loadByCourse = function() {
	if (testsbycourse == undefined) {
		// ajax load by instructors
		var req = new DataRequestor();
		req.onfail = function (status) {
			alert("An error prevented the search from completing: " + status);
		}
		req.onload = function (data) {
			testsbycourse=new Function("return "+data)();
			if (reportErrors(testsbyinstructor) == false)
				tbtt.updateListByCourse(testsbycourse);
		}
		this.showLoading();
		req.getURL("/json/testsByCourse.json.cfm");
	}
	else
		this.updateListByCourse(testsbycourse);
}
TestingSelectList.prototype.loadByInstructor = function() {
	if (testsbyinstructor == undefined) {
		// ajax load by instructors
		var req = new DataRequestor();
		req.onfail = function (status) {
			alert("An error prevented the search from completing: " + status);
		}
		req.onload = function (data) {
			testsbyinstructor=new Function("return "+data)();
			if (reportErrors(testsbyinstructor) == false)
				tbtt.updateListByInstructor(testsbyinstructor);
		}
		this.showLoading();
		req.getURL("/json/testsByInstructor.json.cfm");
	}
	else
		this.updateListByInstructor(testsbyinstructor);
}
TestingSelectList.prototype.loadByName = function() {
	this.updateListByName();
}
TestingSelectList.prototype.testType = function(typeid) {
	if (typeid==1) return 't_online.png" title="online test" alt="online test';
	else if (typeid==2) return 't_scantron.png" title="scantron test" alt="scantron test';
	else if (typeid==3) return 't_paper.png" title="paper test" alt="paper test';
	else if (typeid==4) return 't_survey.png" title="survey" survey="survey';
	else if (typeid==5) return 't_eval.png" title="course evaluation" alt="course evaluation';
	else if (typeid==6) return 't_download.png" title="downloaded test" alt="downloaded test';
	else return 't_unknown.png" title="unknown test type" alt="unknown test type';
}
TestingSelectList.prototype.updateListByCourse = function(list) {
	this.updateList(list);
	var listHTML = '', groups=0;
	with (this.list) {
		for (row=0; row < this.list.id.length; row++) {
			if (type[row] == 0) {
				if (groups >= 1)
					listHTML = listHTML + '</div>';
				groups++;
				courseHasTests = (row < this.list.id.length-1 && this.list.id[row+1] != "");
				if (subjectcode[row] != undefined && coursenumber[row] != undefined) {
					listHTML = listHTML + '<div class="' + (row % 2 == 1? 'groupNameEven' : 'groupNameOdd') + '" row="' + row + '" onClick="'+this.id+'.cg(this)"' + (courseHasTests? '' : ' style="padding-left:19px"') + '><div class="col1">' + (courseHasTests? '<img src="/images/interface/contract.gif" onClick="'+this.id+'.gec(this, event);" alt="Expand or contract">' : '');
					listHTML = listHTML + subjectcode[row] + ' ' + coursenumber[row] + (title[row].length > 0? ' - ' : '') + title[row] + (gradebookid[row].length > 0? '<img src="/images/interface/gradebook.png" alt="gradebook" class="gradebook">' : '') + '</div>';
					listHTML = listHTML + '<div class="col2">' + semester[row] + '</div></div>';
					listHTML = listHTML + '<div class="testGroup" id="'+this.id+'_' + row + 'contents">';
				}
			}
			else if (type[row] == 1) {
				listHTML = listHTML + '<div class="' + (row % 2 == 1? 'testEven' : 'testOdd') + '" row="' + row + '" onClick="'+this.id+'.ct(this);" onDblClick="'+this.id+'.dct(this)"><div class="col1"><div class="name"><img src="/images/interface/'+this.testType(typeid[row])+'">' + testname[row] + '</div></div>';
				listHTML = listHTML + '<div class="col2"><div class="date">' + shortDate(datetaken[row]) + '</div></div></div>';
			}
		}
		if (groups >= 1)
			listHTML = listHTML + '</div>';
	}
	this.clearSelections();
	document.getElementById(this.id).innerHTML = listHTML;
	document.getElementById(this.id + '_col1').innerHTML = 'Courses and Tests';
}
TestingSelectList.prototype.updateListByInstructor = function(list) {
	this.updateList(list);
	var listHTML = '', groups=0;
	with (this.list) {
		for (row=0; row < this.list.id.length; row++) {
			if (type[row] == 0) {
				if (groups >= 1)
					listHTML = listHTML + '</div>';
				groups++;
				instructorHasTests = (row < this.list.id.length-1 && this.list.id[row+1] != "");
				listHTML = listHTML + '<div class="' + (row % 2 == 1? 'groupNameEven' : 'groupNameOdd') + '" row="' + row + '" onClick="'+this.id+'.cg(this)"' + (instructorHasTests? '' : ' style="padding-left:19px"') + '>' + (instructorHasTests? '<img src="/images/interface/contract.gif" onClick="'+this.id+'.gec(this, event);" alt="Expand or contract">' : '');
				listHTML = listHTML + instructorlastname[row] + ', ' + instructorfirstname[row] + (gradebookid[row].length > 0? '<img src="/images/interface/gradebook.png" alt="gradebook" class="gradebook">' : '') + '</div>';
				listHTML = listHTML + '<div class="testGroup" id="'+this.id+'_' + row + 'contents">';
			}
			else if (type[row] == 1) {
				listHTML = listHTML + '<div class="' + (row % 2 == 1? 'testEven' : 'testOdd') + '" row="' + row + '" onClick="'+this.id+'.ct(this);" onDblClick="'+this.id+'.dct(this)"><div class="col1"><div class="name"><img src="/images/interface/'+this.testType(typeid[row])+'">' + testname[row] + '</div></div>';
				listHTML = listHTML + '<div class="col2"><div class="date">' + shortDate(datetaken[row]) + '</div></div></div>';
			}
		}
		if (groups >= 1)
			listHTML = listHTML + '</div>';
	}
	this.clearSelections();
	document.getElementById(this.id).innerHTML = listHTML;
	document.getElementById(this.id + '_col1').innerHTML = 'Instructors and Tests';
}
TestingSelectList.prototype.updateListByName = function(list) {
	var listHTML = '';
	if (list != undefined) {
		this.updateList(list);
		var groups=0;
		if (this.list.id.length == 0) {
			listHTML = listHTML + '<div class="loading">no results found.</div>';
		} else {
			with (this.list) {
				for (row=0; row < this.list.id.length; row++) {
					if (type[row] == 0) {
						if (groups >= 1)
							listHTML = listHTML + '</div>';
						groups++;
						instructorHasTests = (row < this.list.id.length-1 && this.list.id[row+1] != "");
						listHTML = listHTML + '<div class="' + (row % 2 == 1? 'groupNameEven' : 'groupNameOdd') + '" row="' + row + '" onClick="'+this.id+'.cg(this)"' + (instructorHasTests? '' : ' style="padding-left:19px"') + '>' + (instructorHasTests? '<img src="/images/interface/contract.gif" onClick="'+this.id+'.gec(this, event);" alt="Expand or contract">' : '');
						listHTML = listHTML + instructorlastname[row] + ', ' + instructorfirstname[row] + (gradebookid[row].length > 0? '<img src="/images/interface/gradebook.png" alt="gradebook" class="gradebook">' : '') + '</div>';
						listHTML = listHTML + '<div class="testGroup" id="'+this.id+'_' + row + 'contents">';
					}
					else if (type[row] == 1) {
						listHTML = listHTML + '<div class="' + (row % 2 == 1? 'testEven' : 'testOdd') + '" row="' + row + '" onClick="'+this.id+'.ct(this);" onDblClick="'+this.id+'.dct(this)"><div class="col1"><div class="name"><img src="/images/interface/'+this.testType(typeid[row])+'">' + testname[row] + '</div></div>';
						listHTML = listHTML + '<div class="col2"><div class="date">' + shortDate(datetaken[row]) + '</div></div></div>';
					}
				}
				if (groups >= 1)
					listHTML = listHTML + '</div>';
			}
		}
	}
	this.clearSelections();
	document.getElementById(this.id).innerHTML = listHTML;
	document.getElementById(this.id + '_col1').innerHTML = 'Tests';
}
TestingSelectList.prototype.updateList = function(list) {
	if (list != undefined)
		this.list=list;
}
TestingSelectList.prototype.showItemInfo = function(row) {
	var moreInfo = '';
	var availableToTake = (row >= 0);
	var availableForReservation = (row >= 0);
	var availableForProctor = false;
	var availableToReview = false;
	var scoreAvailableForViewing = false;
	if (row >= 0) {
		with (this.list) {
			if (new Date(availableuntil[row]).getTime() <= this.requestTime.getTime()) {
				moreInfo = moreInfo + 'This test is no longer available to be taken, nor can you make a reservation to take the test. It closed on ' + shortDate(this.list.availableuntil[row]) + '.';
				availableToTake = false;
				availableForReservation = false;
			}
			else if (new Date(availablefrom[row]).getTime() >= this.requestTime.getTime()) {
				moreInfo = moreInfo + 'This test is not yet available to be taken. It becomes available on ' + shortDate(availablefrom[row]) + '. ';
				availableToTake = false;
			}
			if (authorizedip[row] == 0 && everywhereelseenabled[row] == 0) {
				moreInfo = moreInfo + 'This test is not available at your site or location.';
				availableToTake = false;
			}
			if (resultid[row] != '') {
				moreInfo = moreInfo + '</p><p>You took this test on ' + shortDate(datetaken[row]) +' at ' + shortTime(datetaken[row]);
				if (resumable[row] == 1) {
					moreInfo = moreInfo + ' but have not yet completed the test. If you take this test, you will resume the test already in progress. ';
					availableToReview = false;
				} else {
					moreInfo = moreInfo + '. ';
					availableToReview = true;
					}
				if (securityattemptsperid[row] == 1) {
					moreInfo = moreInfo + 'You cannot retake this test because it can only be taken once.';
					if (resumable[row] == 0)
						availableToTake = false;
					availableForReservation = false;
				}
				if (reviewenabled[row] == 0) {
					moreInfo = moreInfo + 'Reviewing is not enabled for this test. ';
					availableToReview = false;
				}
				else {
					if (reviewfromthisip[row] == 0) {
						moreInfo = moreInfo + 'This test is not available for review at your site or location.';
						availableToReview = false;
					}
					if (reviewonlyaftertest[row] == 1 && Date.parse(availableuntil[row]) <= this.requestTime.getTime())
						moreInfo = moreInfo + 'You may review this test after ' + shortDate(availableuntil[row]) + '. ';
					if (reviewtimelimit[row].length)
						moreInfo = moreInfo + 'The time limit for reviewing is ' + reviewtimelimit[row] + '. ';
				}
			}
			if (new Date(availableuntil[row]).getTime() >= this.requestTime.getTime() && everywhereelseenabled[row] == 1 && passwordset[row] == 1) {
				availableForProctor = true;
			} else {
				moreInfo = moreInfo + ' Not available for proctoring. ';
			}
			if (instructions[row].length > 0)
				moreInfo = moreInfo + '</p><p id="instLabel">[<a href="javascript:void" onmouseover="document.getElementById(\'instLabel\').style.display=\'none\';document.getElementById(\'instructions\').style.display=\'block\';">Instructions</a>]</p><p id="instructions" style="display:none">' + instructions[row];
			if (canviewscore[row] == 1 && resultid[row] != '' && resumable[row] != 1)
				scoreAvailableForViewing = true;
			if (!availableToTake)
				moreInfo = moreInfo + '</p>';
			if (!availableToTake && resultid[row] == '')
			  	moreInfo = moreInfo + '</p><p><a href="#" onclick="notifyInstructor()">Notify Instructor</a>';
		}
	}
	if (moreInfo.length > 1) {
		document.getElementById('additionalTestingInfoContent').innerHTML = '<p id="testrestrictions">' + moreInfo + '</p>';
		document.getElementById('additionalTestingInfo').style.display = 'block';
	} else {
		document.getElementById('additionalTestingInfo').style.display = 'none';
	}
	/* update the buttons based on availability */
	document.getElementById('takeTest').disabled = !availableToTake;
	if (!this.isAnonymous) {
		document.getElementById('makeReservation').disabled = !availableForReservation;
		document.getElementById('requestProctor').disabled = !availableForProctor;
		document.getElementById('reviewTest').disabled = !availableToReview;
		document.getElementById('viewScores').disabled = !scoreAvailableForViewing;
	}
}
TestingSelectList.prototype.updateButtons = function() {
	document.getElementById('takeTest').disabled = true;
	if (this.selectedItems.length == 0) {
		document.getElementById('makeReservation').disabled = true;
		document.getElementById('reviewTest').disabled = true;
		document.getElementById('requestProctor').disabled = true;
		document.getElementById('viewScores').disabled = true;
		
		
	}
	if (this.selectedGroup != null) {
		if (this.list.gradebookid[this.selectedGroup] != undefined)
			document.getElementById('viewScores').disabled = false;
		else
			document.getElementById('viewScores').disabled = true;
	}
	else {
		document.getElementById('viewScores').disabled = true;
	}
	
}
TestingSelectList.prototype.disableButtons = function() {
	document.getElementById('takeTest').disabled = true;
	if (!this.isAnonymous) {
		document.getElementById('makeReservation').disabled = true;
		document.getElementById('requestProctor').disabled = true;
		document.getElementById('reviewTest').disabled = true;
		document.getElementById('viewScores').disabled = true;
	}
}
TestingSelectList.prototype.outputTestIDs = function() {
	if (this.selectedItems.length > 0) {
		var row = this.selectedItems[0].getAttribute('row');
		document.getElementById('singleTestIDField').value = this.list.id[row];
		document.getElementById('singleDeliveryIDField').value = this.list.deliveryid[row];
		document.getElementById('resultIDField').value = this.list.resultid[row];
		if (this.list.resumable[row] == 1 && this.list.resultid[row] != '')
			document.getElementById('takeTest').value = "Resume this test";
		else if (this.list.resultid[row] != '')
			document.getElementById('takeTest').value = "Retake this test";
		else
			document.getElementById('takeTest').value = "Take this test";
	}
	else {
		document.getElementById('singleTestIDField').value = "";
		document.getElementById('singleDeliveryIDField').value = "";
		document.getElementById('resultIDField').value = "";
	}
}
TestingSelectList.prototype.showLoading = function() {
	document.getElementById(this.id).innerHTML = '<div class="loading">loading . . . <img alt="please wait" src="/images/interface/loading.gif"></div>';
}
TestingSelectList.prototype.setEvent = function(event, section) {
	if (section == undefined) section='';
	document.getElementById('eventField').value=event;
	document.getElementById('sectionField').value=section;
	this.makeSubmittable(true);
	
}
TestingSelectList.prototype.criteriaKeyPressed= function(e) {
	var unicode=e.charCode? e.charCode : e.keyCode;
	if (unicode == 224 || (unicode >= 16 && unicode <= 18) || (unicode >= 37 && unicode <= 40) || unicode == 9)
		return;
	this.keysPressed++;
	if (unicode == 13 || unicode == 10)
		this.confirmSearch();
	else {
		criteria = document.getElementById('tbtt_criteria').value;
		existingSearchListNum = this.findExistingSearch(criteria);
		if (existingSearchListNum != undefined)
			setTimeout('tbtt.criteriaChanged(' + this.keysPressed + ')', 10);
		else
			setTimeout('tbtt.criteriaChanged(' + this.keysPressed + ')', 1000);
	}
	this.makeSubmittable(false);
}
TestingSelectList.prototype.criteriaChanged = function(keyCount) {
	if (keyCount < this.keysPressed) return;
	var criteria = document.getElementById('tbtt_criteria').value;
	if (criteria.length <= 4) return;
	if ((criteria.charCodeAt(0)==87 || criteria.charCodeAt(0)==119) && isNumeric(criteria.substr(1)) && criteria.length<9) return;
	if (isNumeric(criteria) && criteria.length<9) return;
	existingSearchListNum = this.findExistingSearch(criteria);
	
	if (existingSearchListNum != undefined)
		this.updateListByName(this.filterSearchResults(this.searches[existingSearchListNum], criteria));
	else
		this.searchFor(criteria);
}
TestingSelectList.prototype.filterSearchResults = function(list, criteria) {
	var newList = {'id':[], 'availablefrom':[], 'availableuntil':[], 'resultid':[], 'deliveryid':[], 'resumable':[], 'type':[], 'instructorlastname':[], 'instructorfirstname':[], 'testname':[], 'datetaken':[], 'authorizedip':[], 'everywhereelseenabled':[], 'securityattemptsperid':[], 'canviewscore':[], 'reviewenabled':[], 'reviewfromthisip':[], 'reviewonlyaftertest':[], 'reviewtimelimit':[]};
	for (row=0; row < list.id.length; row++) {
		if (list.testname[row].toLowerCase().indexOf(criteria.toLowerCase()) >= 0) {
			newList.id.push(list.id[row]);
			newList.availablefrom.push(list.availablefrom[row]);
			newList.availableuntil.push(list.availableuntil[row]);
			newList.resultid.push(list.resultid[row]);
			newList.deliveryid.push(list.deliveryid[row]);
			newList.resumable.push(list.resumable[row]);
			newList.type.push(list.type[row]);
			newList.instructorlastname.push(list.instructorlastname[row]);
			newList.instructorfirstname.push(list.instructorfirstname[row]);
			newList.testname.push(list.testname[row]);
			newList.datetaken.push(list.datetaken[row]);
			newList.authorizedip.push(list.authorizedip[row]);
			newList.everywhereelseenabled.push(list.everywhereelseenabled[row]);
			newList.securityattemptsperid.push(list.securityattemptsperid[row]);
			newList.canviewscore.push(list.canviewscore[row]);
			newList.reviewenabled.push(list.reviewenabled[row]);
			newList.reviewfromthisip.push(list.reviewfromthisip[row]);
			newList.reviewonlyaftertest.push(list.reviewonlyaftertest[row]);
			newList.reviewtimelimit.push(list.reviewtimelimit[row]);
		}
	}
	return newList;
}
TestingSelectList.prototype.confirmSearch = function() {
	var criteria = document.getElementById(this.id + '_criteria').value;
	if (criteria.length <= 4) {
		if (criteria.length == 0) {
			alert("You must enter some text into the search field before starting the search.");
		} else {
		var doSearch = window.confirm("You have entered only " + criteria.length + " characters. This could return a long list of results. Continue?");
		if (doSearch)
			this.searchFor(criteria);
		}
	} else {
		this.keysPressed++;
		this.searchFor(criteria);
	}
	this.makeSubmittable(false);
}
TestingSelectList.prototype.searchFor = function(criteria) {
	if (criteria != undefined && criteria.length > 0) {
		this.showLoading();
		req.getURL("/json/testSearch.json.cfm?criteria="+escape(criteria)+"&amp;type=1");
	} else alert('oops, no criteria to search for!');
}

