String.prototype.trim=function(){
return this.replace(/^\s*|\s*$/g,'');
}
WEBNAME_HIDDEN_ID = "schoolWebName"
SUBJECTID_HIDDEN_ID = "subjectID"
COURSE_HIDDEN_ID = "courseNumber"
TYPE_HIDDEN_ID = "type"
LEFT_SHOW = "auto"
LEFT_HIDE = "-999em"
USC_FOOTBALL_SUBJECT_ID = 611
USC_FOOTBALL_HTML = "
"
LOADING_HTML = ""
var loadingSchool = false
//loadingSubject = false
var waitingToSubmit = false // If we are waiting to submit the form, this will be
// set to the form object.
subjectsHTML = new Array()
coursesHTML = new Array()
// Get the schools array from PHP. The format for the schools array is:
//
// schools[schoolID] = new Array()
// schools[schoolID] = [synonym1, synonym2,...]
//
// For example, for ucla, it might be:
//
// schools[1] = ["ucla","uclosangeles","universityofcalifornialosangeles"]
schoolSynonyms = new Array()
schoolSynonyms['ucla'] = ['ucla', 'uc los angeles', 'uc at los angeles', 'university of california la', 'university of california los angeles', 'UCLA','ucla']
schoolSynonyms['stanford'] = ['stanford', 'Stanford','stanford']
schoolSynonyms['berkeley'] = ['berkeley', 'uc berkeley', 'ucb', 'university of california, berkeley', 'university of california berkeley', 'university of california at berkeley', 'uc at berkeley', 'cal', 'UC Berkeley','berkeley']
schoolSynonyms['ucsd'] = ['ucsd', 'uc san diego', 'university of california at san diego', 'UC San Diego','ucsd']
schoolSynonyms['uci'] = ['uci', 'uc irvine', 'university of california irvine', 'university of california at irvine', 'uc at irvine', 'UC Irvine','uci']
schoolSynonyms['harvard'] = ['Harvard','harvard']
schoolSynonyms['usc'] = ['usc', 'university of southern california', 'university of sucky california', 'u of southern california', 'USC','usc']
schoolSynonyms['mit'] = ['MIT','mit']
schoolSynonyms['caltech'] = ['caltech', 'california institute of technology', 'california institute technology', 'california institute tech', 'california institute of tech', 'Caltech','caltech']
schoolSynonyms['dartmouth'] = ['Dartmouth','dartmouth']
schoolSynonyms['rice'] = ['Rice','rice']
schoolSynonyms['duke'] = ['Duke','duke']
schoolSynonyms['wisc'] = ['UW Madison','wisc']
schoolSynonyms['uiuc'] = ['UI Urbana\-Champaign', 'UI Urbana Champaign','uiuc']
schoolSynonyms['colorado'] = ['UC Boulder','colorado']
schoolSynonyms['ucdavis'] = ['UC Davis','ucdavis']
schoolSynonyms['ucsb'] = ['UC Santa Barbara','ucsb']
schoolSynonyms['ucr'] = ['UC Riverside','ucr']
schoolSynonyms['ucsc'] = ['UC Santa Cruz','ucsc']
schoolSynonyms['upenn'] = ['U Penn','upenn']
schoolSynonyms['princeton'] = ['Princeton','princeton']
schoolSynonyms['jhu'] = ['Johns Hopkins','jhu']
schoolSynonyms['brown'] = ['Brown','brown']
schoolSynonyms['vanderbilt'] = ['Vanderbilt','vanderbilt']
schoolSynonyms['asu'] = ['Arizona State','asu']
schoolSynonyms['columbia'] = ['Columbia','columbia']
schoolSynonyms['yale'] = ['Yale','yale']
schoolSynonyms['wfu'] = ['Wake Forest','wfu']
schoolSynonyms['bu'] = ['Boston University','bu']
subjectSynonyms = new Array()
courseList = new Array()
highlight_backgroundImage = "../images/topbarbg.gif"
previousSchool = null
previousSubject = null
previousCourse = null
previousCourseLink = null
previousType = null
previousCourseNumber = null
// http://earthcode.com/blog/2006/01/optional_args.html
function augment (oSelf, oOther) {
if (oSelf == null) {
oSelf = {};
}
for (var i = 1; i < arguments.length; i++) {
var o = arguments[i];
if (typeof(o) != 'undefined' && o != null) {
for (var j in o) {
oSelf[j] = o[j];
}
}
}
return oSelf;
}
function byId(id)
{
try {
return document.getElementById(id)
}
catch(e) {
return null
}
}
function toggle(obj)
{
if(obj.style.display == "none" || obj.style.display == "")
{
show(obj)
}
else
{
hide(obj)
}
}
function show(obj)
{
if(obj) showBlock(obj)
}
function showBlock(obj)
{
if(obj) obj.style.display = "block"
}
function showInline(obj)
{
if(obj) obj.style.display = "inline"
}
function hide(obj)
{
if(obj) obj.style.display = "none"
}
function wipeHTML(obj) {
obj.innerHTML = " "
}
function checkAndSubmit(form) {
if(loadingSchool) {
waitingToSubmit = form
return false
}
else {
reparseQuery()
return true
}
}
function recordClick(destURL)
{
// alert("recordClick: " + destURL)
hiddenIframe = document.getElementById("hiddenIframe")
// alert(hiddenIframe)
if(hiddenIframe)
{
// alert("scripts/recordClick.php?sourceURL=" + location + "&destURL=" + destURL)
hiddenIframe.src = "scripts/recordClick.php?sourceURL=" + location + "&destURL=" + destURL
}
}
function loadSchool(schoolWebName, optional) {
var optional = augment({
loadHTML : true
}, optional)
if (!byId(WEBNAME_HIDDEN_ID)) return;
byId(WEBNAME_HIDDEN_ID).value = schoolWebName
schoolObj = byId("school_" + schoolWebName)
if(!schoolObj) {
byId("courses").innerHTML = " "
byId("subjects").innerHTML = " "
if(previousSchool) {
unHighlight(previousSchool)
}
previousSchool = null
return
}
// Set school cookie
document.cookie = "schoolWebName=" + schoolWebName
if(schoolObj == previousSchool) {
return
}
if(previousSchool) {
unHighlight(previousSchool)
}
highlight(schoolObj)
previousSchool = schoolObj
// clear courses any subject synonyms
if(optional.loadHTML) {
showLoading("subjects")
byId("courses").innerHTML = " "
subjectSynonyms = new Array()
courseList = new Array()
if(subjectsHTML["school_" + schoolWebName]) {
byId("subjects").innerHTML = subjectsHTML["school_" + schoolWebName]
xmlDoc = getXMLDoc(subjectsHTML["school_" + schoolWebName])
updateSynonyms(xmlDoc.getElementsByTagName("synonymsList").item(0))
highlight(schoolObj) // To override loading animation on mini search bar
}
else {
loadingSchool = true
xobj = getXMLHttp()
xobj.onreadystatechange = function() {
if(xobj.readyState == 4) {
byId("subjects").innerHTML = xobj.responseText
subjectsHTML["school_" + schoolWebName] = xobj.responseText
xmlDoc = getXMLDoc(xobj.responseText)
updateSynonyms(xmlDoc.getElementsByTagName("synonymsList").item(0))
reparseQuery()
loadingSchool = false
highlight(schoolObj) // To override loading animation on mini search bar
if(waitingToSubmit) {
reparseQuery()
waitingToSubmit.submit()
}
}
}
xobj.open("GET", "/subjects.php?schoolWebName=" + schoolWebName, true)
xobj.send(null)
}
}
}
function updateSynonyms(synonymsListObj) {
synonymsTags = synonymsListObj.getElementsByTagName("synonyms")
for(var i = 0; i < synonymsTags.length; i++) {
synonyms = synonymsTags.item(i)
subjectSynonyms[synonyms.getAttribute("subjectID")] = synonyms.getAttribute("synonyms").split(",")
}
}
function loadSubject(subjectID, optional) {
var optional = augment({
loadHTML : true
}, optional)
if (!byId(SUBJECTID_HIDDEN_ID)) return;
byId(SUBJECTID_HIDDEN_ID).value = subjectID
subjectObj = byId("subject_" + subjectID)
if(!subjectObj) {
byId("courses").innerHTML = " "
if(previousSubject) {
unHighlight(previousSubject)
}
previousSubject = null
return
}
if(subjectObj == previousSubject) {
return
}
if(previousSubject) {
unHighlight(previousSubject)
}
highlight(subjectObj)
previousSubject = subjectObj
if(optional.loadHTML) {
if(subjectID == USC_FOOTBALL_SUBJECT_ID) {
byId("courses").innerHTML = USC_FOOTBALL_HTML
return
}
showLoading("courses")
// Get rid of old course list
courseList = new Array()
if(coursesHTML["subject_" + subjectID]) {
byId("courses").innerHTML = coursesHTML["subject_" + subjectID]
xmlDoc = getXMLDoc(coursesHTML["subject_" + subjectID])
updateCourseList(subjectID, xmlDoc.getElementsByTagName("courseList").item(0))
highlight(subjectObj) // To override loading animation on mini search bar
}
else {
xobj = getXMLHttp()
xobj.onreadystatechange = function() {
if(xobj.readyState == 4) {
coursesHTML["subject_" + subjectID] = xobj.responseText
byId("courses").innerHTML = xobj.responseText
xmlDoc = getXMLDoc(xobj.responseText)
updateCourseList(subjectID, xmlDoc.getElementsByTagName("courseList").item(0))
highlight(subjectObj) // To override loading animation on mini search bar
reparseQuery()
}
}
xobj.open("GET", "/courses.php?subjectID=" + subjectID, true)
xobj.send(null)
}
}
}
function updateCourseList(subjectID, coursesObj) {
courseList[subjectID] = new Array()
courseList[subjectID] = coursesObj.getAttribute("numbers").split(",")
}
function loadCourse(number) {
if (!byId(COURSE_HIDDEN_ID)) return;
byId(COURSE_HIDDEN_ID).value = number
courseObj = byId("course_" + number.toUpperCase())
courseNumberObj = byId("course_" + number.toUpperCase() + "_number")
if(!courseObj) {
if(previousCourse) {
unHighlight(previousCourse)
}
if(previousCourseNumber) {
unHighlight(previousCourseNumber)
}
previousCourse = null
previousCourseNumber = null
return
}
if(courseObj == previousCourse) {
return
}
if(previousCourse) {
unHighlight(previousCourse)
}
if(previousCourseNumber) {
unHighlight(previousCourseNumber)
}
highlight(courseObj)
highlight(courseNumberObj)
byId(COURSE_HIDDEN_ID).value = number // In case unHighlight erased the value
previousCourse = courseObj
previousCourseNumber = courseNumberObj
}
function selectType(type) {
byId(TYPE_HIDDEN_ID).value = type
typeObj = byId(type)
if(previousType == typeObj) {
return
}
radio = byId(type + "Radio")
if (radio) {
radio.checked = true
highlight(typeObj)
if(previousType) {
unHighlight(previousType)
previousType.style.color = "black"
}
previousType = typeObj
}
}
function showLoading(id) {
byId(id).innerHTML = LOADING_HTML
}
function highlight(obj) {
obj.style.color = "white"
obj.style.fontWeight = "bold"
obj.style.backgroundColor = "#696969"
// obj.style.backgroundImage = "url('images/topbarbg.gif')"
obj.style.backgroundPosition = "center center"
}
function unHighlight(obj) {
obj.style.color = ""
obj.style.fontWeight = "normal"
obj.style.backgroundColor = "white"
obj.style.backgroundImage = ""
}
function getXMLDoc(strXML) {
//load xml file
// code for IE
if (window.ActiveXObject)
{
var doc=new ActiveXObject("Microsoft.XMLDOM");
doc.async="false";
doc.loadXML(strXML);
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var parser=new DOMParser();
var doc=parser.parseFromString(strXML,"text/xml");
}
else
{
return false;
}
return doc.documentElement
}
function getXMLHttp()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
return new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
return new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
return false;
}
}
}
}
function addOptionalSpaces(regexStr) {
regexStr = regexStr.replace(/(.)/g, "$1 ")
regexStr = regexStr.trim()
regexStr = regexStr.replace(/ /g, " ?")
return regexStr
}
function Query(query, inputObj)
{
this.school = ""
this.schoolWebName = ""
this.subject = ""
this.subjectID = 0
this.number = ""
this.type = ""
this.courseNumber = ""
this.query = query
this.inputObj = inputObj
this.prevSubject = null
this.prevType = null
this.prevNumber = null
this.parseQuery = function(newQuery)
{
// Reset everything
this.school = ""
this.schoolWebName = ""
this.subject = ""
this.subjectID = 0
this.type = ""
this.courseNumber = ""
// Store the query and then remove spaces from it
this.query = newQuery
newQuery = newQuery.replace(/[^A-Za-z0-9& ]/g,"")
// First, extract the type
var regex = new RegExp("(?: |^)(finals?|mid ?terms?|home ?works?|hws?|notes?)(?: |$)", ["i"])
match = regex.exec(newQuery)
if(match != null)
{
if(match[1][0] == "f")
this.type = "final";
else if(match[1][0] == "m")
this.type = "midterm";
else if(match[1][0] == "h")
this.type = "hw";
else if(match[1][0] == "n")
this.type = "notes";
}
else
{
this.type = "all"
}
// Remove the type string from the query
regex = new RegExp("(final|finals|mid ?term|mid ?terms|home ?work|hw|note|notes)", ["i"])
newQuery = newQuery.replace(regex, "")
// Next, look for a school name
for(key in schoolSynonyms) {
joined = schoolSynonyms[key].join("|").replace(/[^A-Za-z0-9 \t\n|]/g, "")
regexStr = ("(?: |^)(" + addOptionalSpaces(joined) + ")(?: |$)")
regex = eval("/" + regexStr + "/i")
match = regex.exec(newQuery)
if(match != null) {
this.schoolWebName = key
loadSchool(key)
newQuery = newQuery.replace(regex, "")
break
}
}
// If we found a school, look for a subject
if(this.schoolWebName != "") {
longest = 0
matchText = ""
for(key in subjectSynonyms) {
joined = subjectSynonyms[key].join("|").replace(/[^A-Za-z0-9 \t\n|]/g, "")
regexStr = ("(?: |^)(" + addOptionalSpaces(joined) + ")(?: |$)")
regex = eval("/" + regexStr + "/i")
match = regex.exec(newQuery)
if(match != null && match[1].length > longest) {
this.subjectID = key
longest = match[1].length
matchText = match[1]
}
match = null
}
if(matchText != "") {
newQuery = newQuery.replace(matchText, "")
}
}
// Look for a course number
if(this.schoolWebName != "" && this.subjectID != 0) {
regex = /[ \t^]([A-Za-z0-9.]+)/
match = regex.exec(newQuery)
if(match != null) {
this.courseNumber = match[1].toUpperCase()
}
}
// If we have a school and subject, then look for a course number in
// what's left of the query string
try {
if(this.schoolWebName != "" && this.subjectID != 0 && courseList[subjectID]) {
longest = 0
for(var i = 0; i < courseList[subjectID].length; i++) {
// Some schools have 1, 2 or 3 zeros to pad the course numbers. Make those optional.
couresNumRegex = courseList[subjectID][i]
couresNumRegex = couresNumRegex.replace(/^000/, "0*")
couresNumRegex = couresNumRegex.replace(/^00/, "0*")
couresNumRegex = couresNumRegex.replace(/^0/, "0*")
regex = eval("/[ \t^](" + couresNumRegex + ")/i")
match = regex.exec(this.query)
if(match != null && match[1].length > longest) {
this.courseNumber = courseList[subjectID][i]
longest = courseList[subjectID][i].length
}
match = null
}
}
}
catch(e) { }
loadSchool(this.schoolWebName)
loadSubject(this.subjectID)
loadCourse(this.courseNumber)
selectType(this.type)
}
this.updateQuery = function()
{
query = this.schoolWebName + " " + this.subjectID + " " + this.number + " " + this.type
query = ""
if(this.schoolWebName) {
schoolObj = byId("school_" + schoolWebName)
if(schoolObj.innerText)
query += schoolObj.innerText
else if(schoolObj.textContent)
query += schoolObj.textContent
}
if(this.subjectID) {
if(this.schoolWebName) query += " "
subjectObj = byId("subject_" + subjectID)
if(subjectObj.innerText)
query += subjectObj.innerText
else if(subjectObj.textContent)
query += subjectObj.textContent
}
if(this.courseNumber) {
if(this.subjectID) query += " "
query += this.courseNumber
}
if(this.type && this.type != "all") {
if(query.trim().length > 0) query += " "
query += this.type
}
query = query.replace(/[\n\r]/g, "")
this.inputObj.value = query
}
this.setSchool = function(newSchoolWebName) {
this.schoolWebName = newSchoolWebName
}
this.setType = function(newType)
{
this.type = newType
}
this.setSubjectID = function(newSubjectID)
{
this.subjectID = newSubjectID
}
this.setCourse = function(newNumber)
{
this.courseNumber = newNumber
}
//this.parseQuery(query)
return this
};
var q = null
function reparseQuery() {
if(q)
{
q.parseQuery(q.inputObj.value)
}
}
function queryTypingHandler(e)
{
reparseQuery()
}
function updateType(e)
{
if(q)
{
inputs = document.getElementById("typeSelector").getElementsByTagName("input")
for(i = 0; i < inputs.length; i++)
{
if(inputs.item(i).checked)
{
q.setType(inputs.item(i).value)
q.updateQuery()
break
}
}
}
}
function updateSchool(schoolWebName)
{
if(q)
{
q.setSchool(schoolWebName)
q.setSubjectID(0)
q.setCourse("")
q.updateQuery()
}
}
function updateSubjectID(subjectID)
{
if(q)
{
q.setSubjectID(subjectID)
q.setCourse("")
q.updateQuery()
}
}
function updateCourse(number)
{
if(q)
{
q.setCourse(number)
q.updateQuery()
}
}
function init()
{
/*
q = Query("", document.getElementById("query"))
alert(q);
alert(q.parseQuery);
alert(document.getElementById("query").value);
q.parseQuery("ucla")
document.getElementById("query").onkeyup = queryTypingHandler
document.getElementById("query").onkeydown = queryTypingHandler
document.getElementById("query").onchange = queryTypingHandler
document.getElementById("query").onclick = queryTypingHandler
*/
document.getElementById("typeSelector").onclick = updateType
}