
	//var gForm;
	var kCourseStrLen=9;
	var gUDUrl;
	var gDeptHttp;
	var gSchoolDeptBase;
	//var gEmailtHttp;
	var gCurCourse;
	var gCurDept;
	var gTargetPage;
	var gCurPage;
	var gCurPageFull;
	var gUpdateList;
	var gCourseTimeout = 5000;
	var gSendEmailTimeout = 10000;
	var gCourseRepeat = 3;
	var gCourseStr;
	var gDeptStr;
//	var gEmailStr;
	var gCourseLoop;
	var gDeptLoop;
	var gTimeoutObj;
	var gDeptTimeoutObj;
	var gDeptListLookup;
	var gStatePage;
	//var gEmailTimeoutObj;
	var gCurSchool;
	var gCurSection;
	var gSchoolLookup;
	var gOnHome;
	var gDoActive;
	var gCurShowEmail = 0;
	var gCurState = 0;
	//
	var gUpdateCnt;
	var gCurUpdate = 0;
	var gCurPriceID;
	var gUpdateList;
	var gPriceHttp;
	function startCourseList()
	{
		gForm = document.forms['courseList'];
		if (gForm.elements['UDUrl'])
			gUDUrl = gForm.elements['UDUrl'].value;
		gTargetPage = gForm.elements['targetPage'].value;
		gCurPage = gForm.elements['curPage'].value;
		if (gForm.elements['schoolDeptBase'])
			gSchoolDeptBase = gForm.elements['schoolDeptBase'].value;
		gCurState = gForm.elements['curState'].value;
		gDoActive = (gForm.elements['doActive']) ? parseInt(gForm.elements['doActive'].value) : false;
		gOnHome = (gForm.elements['onHome']) ? parseInt(gForm.elements['onHome'].value) : false;
		if (gForm.elements['curPageFull'])
			gCurPageFull = gForm.elements['curPageFull'].value
		gCurCourse = parseInt(gForm.elements['curCourse'].value);
		if (!gCurCourse)
			gCurCourse = 0;
		gCurSection = (gForm.elements['curSection']) ? gForm.elements['curSection'].value : 0;
		if (!gCurSection)
			gCurSection = 0;
		gCurDept = gForm.elements['curDept'].value;
		if (!gCurDept)
			gCurDept = 0;
		gCurSchool = parseInt(gForm.elements['curSchool'].value);
		if (parseInt(gCurSchool) != 0)
			setSBSchool(gCurSchool);
		gSchoolLookup = eval(gForm.elements['schoolLookup'].value)[0];
		// Clear 'sid'
		var lSID = readCookie('sid');
		if (lSID && lSID.length)
			eraseCookie('sid');
		//if (document.getElementById('sid'))
		//	createCookie('sid',document.getElementById('sid').value,2);
		// Check if we need to update stuff
		if (gForm.elements['updateList'])
		{
			var lUpdateStr = gForm.elements['updateList'].value;
			if (lUpdateStr.length > 0)
			{
				gUpdateList = lUpdateStr.split('_');
				gUpdateCnt = gUpdateList.length;
				checkUpdates();
			}
		}
		setSchoolList(gCurState, gCurSchool);
		// Preset values
		if (gCurState.length)
			setSelectedValue('state', gCurState);
		if (gCurSchool)
			setSelectedValue('school', gCurSchool);
		checkActive();
		if (gCurSchool )
		{
			setDeptList(gCurDept, true);
			if (gCurDept)
				setCourseList(gCurDept, gCurCourse);
			if (document.getElementById('section'))
				setSectionList(gCurDept, gCurCourse, gCurSection);
		}
		//var lStartList = gForm.elements['startList'].value;
		//getNewList(lStartList);
	}
	addDOMLoadEvent(startCourseList);
	function doCourseList(iCourses)
	{
		var lStr = '<select name="filter[course]" id="f-course" onchange="javascript:selectCourse(this.value)">';
		for (var i=0; i<iCourses.length; i++)
		{
			var lCourseInfo = iCourses[i];
			if (lCourseInfo.length == 2)
				lStr += '<option value="'+iCourses[i][0]+'">'+unescape(iCourses[i][1])+'</option>';
			else
				lStr += '<option value="-'+iCourses[i][0]+'">'+unescape(iCourses[i][1])+'</option>';
		}
    lStr += '</select>';
		//alert(lStr);
		document.getElementById('courseDrop').innerHTML = lStr;
		//document.getElementById('course2').width = 310px;
	}
	function setSchoolList(iCurState, iCurSchool)
	{
		if (document.getElementById('school'))
		{
			var lList = [];
			if (iCurState in gStateSchoolList)
			{
				var lSchoolList = gStateSchoolList[iCurState];
				if (parseInt(iCurSchool) == 0) 
					lList.push([0,'Select a School']);
				for (var i=0; i<lSchoolList.length; i++)
					lList.push(lSchoolList[i]);
				if (!lSchoolList.length)
					lList.push(['', 'No Schools for this State at this Time']);
			}
			else
				lList.push([0,'Select a School']);
			setSelectList('school', lList, iCurSchool);
			// Add in no school option
			/*
			if (lList.length > 1)
			{
				addSelectHRule('school');
				addSelectOptClass('school', 'notListed', 'School Not Listed?', 'listSpecial', iCurSchool);
			}*/
		}
	}
	function setDeptList(iCurDept, iPreserveLower)
	{
		var lList = [];
		if (parseInt(gCurSchool)) //(!gOnHome)
		{
			var lOrderList = gSchoolDeptList['deptOrder'];
			for (var i=0; i<lOrderList.length; i++)
				lList.push([lOrderList[i], gSchoolDeptList[lOrderList[i]][0]]);
			//lList = lList.sort(srchSort2);
			//alert(lList.toString());
		}
		if (!parseInt(iCurDept))
			lList.unshift([0,'Select a Department']);
		setSelectList('department', lList, iCurDept);
		// Do Course list
		if (!iPreserveLower || !iCurDept)
			gCurCourse = 0;
		setCourseList(iCurDept, gCurCourse, iPreserveLower)
	}
	// This function sorts according to the first entry of an array or arrays.
	function srchSort1(a,b)
	{
		return a[0] -b[0];
	}
	// This function sorts according to the 2nd entry of an array or arrays.
	function srchSort2(a,b)
	{
		return a[1] -b[1];
	}
	function setCourseList(iCurDept, iCurCourse)
	{
		var lList = (parseInt(iCurCourse) > 0 && iCurCourse < 99999999) ? [] : [[0,'Select a Course']];
		if (iCurDept > 0 && parseInt(gCurSchool))
		{
			var lCourseList = gSchoolDeptList[iCurDept][1];
			if (lCourseList.length == 0)
				lList = [[0,'No courses for this department at this time.']];
			else
				for (var i=0; i<lCourseList.length; i++)
					lList.push(lCourseList[i]);
		}
		//lList = lList.sort(srchSort2);
		setSelectList('course', lList, iCurCourse);
		// Add in Course Not Listed option
		if (iCurCourse != 99999999)
		{
			addSelectHRule('course');
			addSelectOptClass('course', 99999999, 'Course Not Listed? Search by Book!', 'listSpecial', iCurCourse);
		}
	}
	function setSectionList(iCurDept, iCurCourse, iSection)
	{
		var lList = [];
		if (iCurCourse < 0)
		{
			if (parseInt(iSection) == 0)
				lList = [[0,'Select a Section']];
			var lSelList = [];
			var lCourseList = gSchoolDeptList[iCurDept][1];
			for (var i=0; i<lCourseList.length; i++)
				if (lCourseList[i][0] == iCurCourse)
					lSelList = lCourseList[i][2]
			for (var i=0; i<lSelList.length; i++)
				lList.push(lSelList[i]);
		}
		else
			lList = [[0,'All']];
		setSelectList('section', lList, iSection);
	}
	function doDeptList(iDepts)
	{
		var lStr = '<select name="filter[department]" id="f-department" onchange="javascript:newDept(this.value)">';
		for (var i=0; i<iDepts.length; i++)
			lStr += '<option value="'+iDepts[i][0]+'">'+unescape(iDepts[i][1])+'</option>';
    lStr += '</select>';
		//alert(lStr);
		document.getElementById('deptDrop').innerHTML = lStr;
	}
	function selectCourse(iCourse)
	{
		/*
		if (iCourse == 'notListed')
			document.getElementById('No Book').style.display = '';
		else
		{*/
			//document.getElementById('No Book').style.display = 'none';
			var lSelectionList = document.getElementById('section');
			if (iCourse > 0 || (iCourse < 0 && !lSelectionList))
			{
				var lCourseStr = padStr(iCourse, kCourseStrLen);
		  	document.location = '/'+gSchoolLookup[gCurSchool]+'/'+gSchoolLookup[gCurSchool]+'-Booklist.html?curDept='+gCurDept+'&curCourse='+lCourseStr;
		  }
			else if (iCourse < 0)
			{
				gCurCourse = iCourse;
				setSectionList(gCurDept, gCurCourse, 0);
				checkActive();
			}
		//}
		//alert("Selected course: "+iCourse+"!");
	}
	function padStr(iNum, iLen)
	{
		var lIsNeg = (iNum < 0);
		if (lIsNeg)
			iNum = -iNum;
		var lStr = iNum.toString();
		while (lStr.length < iLen)
			lStr = '0'+lStr;
		if (lIsNeg)
			lStr = "-"+lStr;
		return lStr;
	}
	function selectSection(iSection)
	{
		if (iSection != 0)
		{
			var lSelStr = padStr(iSection, kCourseStrLen);
	  	document.location = '/'+gSchoolLookup[gCurSchool]+'/'+gSchoolLookup[gCurSchool]+'-Booklist.html?curDept='+gCurDept+'&curCourse='+gCurCourse+'&curSection='+lSelStr;
	  }
		//alert("Selected course: "+iCourse+"!");
	}
	function checkActive()
	{
		if (!gDoActive)
		{
			if (document.getElementById('course'))
				document.getElementById('course').focus();
			return;
		}
		var lActiveList = ['schoolSelect', 'deptSelect', 'courseSelect', 'sectionSelect'];
		var lActive = 'schoolSelect';
		// Check school
		var lCheckVal = getSelectedValue('school');
		if (lCheckVal > 0)
		{
			lActive = 'deptSelect';
			// Check Department
			lCheckVal = getSelectedValue('department');
			if (lCheckVal > 0)
				lActive = 'courseSelect';
			// Check Course
			lCheckVal = getSelectedValue('course');
			if (lCheckVal < -1)
				lActive = 'sectionSelect';
		}
		for (var i=0; i<lActiveList.length; i++)
		{
			var lCurElement = document.getElementById(lActiveList[i]);
			if (lCurElement)
				document.getElementById(lActiveList[i]).className = (lActiveList[i] == lActive) ? 'active' : 'inactive';
		}
		// Set drop-down active
		var lCheckState = getSelectedValue('state');
		if (lCheckState == '0')
			lActive = 'stateSelect';
		var lActiveDrop = {'stateSelect': 'state', 'schoolSelect': 'school', 'deptSelect': 'department', 'courseSelect': 'course', 'sectionSelect': 'section'};
		var lCurDrop = document.getElementById(lActiveDrop[lActive]);
		if (lCurDrop)
			lCurDrop.focus();
	}
	function stateSelected(iState)
	{
		if (iState == 'notListed')
			document.location = '/book-search.html';
		else
		{
			
			if (iState && !(iState in gStateSchoolList))
			{
				if (gStatePage)
					document.location = gStatePage[iState];
			}
			gCurState = iState;
			setSchoolList(gCurState, 0);
			// Focuse at school
			document.getElementById('school').focus();
			checkActive();
		}
	}
	
	function newDept(iDept)
	{
		gCurDept = iDept;
		setCourseList(gCurDept, 0);
		checkActive();
	}
	function goSchool(iSchool)
	{
		if (iSchool == 'notListed')
			document.location = '/book-search.html';
		else
			document.location = '/'+gSchoolLookup[iSchool]+'/'+gSchoolLookup[iSchool]+'-Home.html';
	}
	function schoolSelected(iSchool)
	{
		gCurSchool = iSchool;
		// Get request object		
		gSchoolHttp = getxmlhttp();
		var lSchoolInt = parseInt(gCurSchool);
		if (gSchoolHttp && lSchoolInt in gDeptListLookup)
		{
			
	    var lPage = gSchoolDeptBase+gDeptListLookup[lSchoolInt];
			//alert("Search string:"+lPage);
	    gSchoolHttp.onreadystatechange = handleSchoolResponse;
	    gSchoolHttp.open('GET', lPage, true);
	    gSchoolHttp.send(null);
	    startAjax();
	  }
	}
	function startAjax()
	{
		showCenterDiv('waitDiv');
	}
	function endAjax()
	{
		showHide(0,'waitDiv');
	}
	function handleSchoolResponse()
	{
		var lResponse = checkResult(gSchoolHttp);
		if (lResponse)
		{
			endAjax();
			if (lResponse == -1)
				;//alert("There was an error!");
			else if (lResponse.substr(0,10).toLowerCase().indexOf('error') == 0)
				alert(lResponse);
			else
			{
				eval(lResponse);
				gCurDept = 0;
				setDeptList(gCurDept,0);
				checkActive();
			}
		}
	}
	// This function cancels the email
	function cancelEmail()
	{
		showEmail(gCurShowEmail);
	}
	// This function starts off the update request
	function checkUpdates()
	{
		if (gCurUpdate < gUpdateCnt)
			doUpdateRequest(gUpdateList[gCurUpdate++]);
	}
	// This function requests information
	function doUpdateRequest(iBookRef)
	{
		gCurPriceID = iBookRef;
		// Get request object		
		gPriceHttp = getxmlhttp();
		if (gPriceHttp)
		{
	    var lPage = gUDUrl+"?action=getPriceUpdate&bookRef="+iBookRef+"&school="+gCurSchool;
			//alert("Search string:"+lPage);
	    gPriceHttp.onreadystatechange = handlePriceUpdate;
	    gPriceHttp.open('GET', lPage, true);
	    gPriceHttp.send(null);
	  }
	}
	// This function handles response from update request
	function handlePriceUpdate()
	{
		var lResponse = checkResult(gPriceHttp);
		if (lResponse)
		{
			if (lResponse == -1)
				;
			else if (lResponse.indexOf('>>error<<') != -1)
				;
			else if (lResponse.indexOf('>>none<<') != -1)
			{
				checkUpdates();
			}
			else
			{
				var lLines = lResponse.split(">results<<");	// What we want comes after that
				if (lLines.length == 2)
				{
					//alert("lResponse:"+lLines[1]);
					var lParts = lLines[1].split('&');
					for (var i=0; i<lParts.length; i++)
					{
						var lRefVal = lParts[i].split('=');
						var lRef = lRefVal[0];
						if (lRefVal[1] == '0')
						{
							showHide(lRef+'_none_'+gCurPriceID, lRef+'_block_'+gCurPriceID);
						}
						else
						{
							var lPriceName = lRef+'_'+gCurPriceID;
							if (document.getElementById(lPriceName))
							{
								document.getElementById(lPriceName).innerHTML = lRefVal[1];
								showHide(lRef+'_block_'+gCurPriceID, lRef+'_none_'+gCurPriceID);
							}
							//else
							//	alert("No Price obj for '"+lPriceName+"'");
						}
					}
				}
				checkUpdates();
			}
		}
	}
	// This function foces the update
	function forceUpdate()
	{
		gForm['forceUpdate'].value = 1;
		gForm.submit();
	}
	function uploadDepts()
	{
		document.forms.editList.elements['deptMode'].value = 'upload';
		document.forms.editList.submit();
	}
	
