var xmlHttpRequest;

function loadXmlData(url, object) {
    if (window.XMLHttpRequest) {
	xmlHttpRequest = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
	xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlHttpRequest.open("GET", url, true);
    xmlHttpRequest.onreadystatechange = function () { processRequestChange(object) };
    xmlHttpRequest.send(null);
}

function processRequestChange(object) {
    if (xmlHttpRequest.readyState == 4) {
	if (xmlHttpRequest.status == 200) {
	    if (object.name == "course") {
		putCourses();
	    }
	}
    }
}

function getCourses() {
    document.byCourse.course.disabled = false;
    if (document.byCourse.subject.options[0].value == "") { document.byCourse.subject.remove(0); }
    clearList(document.byCourse.course);
    loadXmlData("/ajax/?subject=" + document.byCourse.subject.value, document.byCourse.course);
}

function putCourses() {
    var select = document.byCourse.course;
    clearList(select);
    document.byCourse.submit.disabled = true;
    var result = xmlHttpRequest.responseXML.getElementsByTagName("course");
    if (result.length > 0) {
	var prev = -1;
        addElementToList(select, "", "");
	for (var i = 0; i < result.length; i++) {
	    var node = result[i];
	    var key = node.getElementsByTagName("key")[0].firstChild.nodeValue;
	    var value = node.getElementsByTagName("value")[0].firstChild.nodeValue;
            if (key.substring(0,1) != prev) {
                if (prev > 0) { select.appendChild(optgroup); }
                prev = key.substring(0,1);
                var optgroup = document.createElement("optgroup");
                optgroup.label = prev + "00s";
            }
            addElementToList(optgroup, key, key + " - " + value);
	}
	select.appendChild(optgroup);
    }
}

function clearList(list) {
    var next = list.firstChild;
    while (next) {
        var prev = next;
	next = prev.nextSibling;
	if (prev.tagName == "OPTGROUP") { list.removeChild(prev); }
    }
    while (list.length > 0) {
        list.remove(0);
    }
}

function addElementToList(list, value, label) {
    var option = document.createElement("option");
    option.value = value;
    var labelNode = document.createTextNode(label);
    option.appendChild(labelNode);
    list.appendChild(option);
}

function simulateSearch(myObject, placeholder) {
    if (myObject.type != 'search') {
        if (myObject.value == "") {
	    myObject.style.color = '#999999';
            myObject.value = placeholder;
        }
	else if (myObject.value == placeholder) {
            myObject.style.color = '#999999';
        }
    }
}

function clearSearch(myObject, placeholder) {
    if (myObject.type != 'search') {
        if (myObject.value == placeholder) {
	    myObject.style.color = '';
            myObject.value = "";
        }
    }
}

function checkButtons(myForm) {
    if (myForm == document.bearscat) {
        if (myForm.username.value == "" || myForm.password.value == "") { myForm.submit.disabled = true; }
        else { myForm.submit.disabled = false; }
    }
    else if (myForm == document.byTerm) {
        if (myForm.term.value == "") { myForm.submit.disabled = true; }
        else { myForm.submit.disabled = false; }
	if (myForm.term.options[0].value == "") { myForm.term.remove(0); }
    }
    else if (myForm == document.byCourse) {
        if (myForm.subject.value == "" || myForm.course.value == "") { myForm.submit.disabled = true; }
        else { myForm.submit.disabled = false; }
	if (myForm.course.options[0].value == "") { myForm.course.remove(0); }
    }
    else if (myForm == document.byBook) {
        if (myForm.search.value == "") { myForm.submit.disabled = true; }
        else { myForm.submit.disabled = false; }
    }
}

function searchByTerm(term) {
    location = "/" + term + "/";
    return false;
}

function searchByCourse(subject, course) {
    if (subject == '') { return false; }
    if (course == '') { location = "/" + subject.replace(/ /g, "") + "/"; }
    else { location = "/" + subject.replace(/ /g, "") + "/" + course + "/"; }
    return false;
}

function searchByBook(search) {
    if (search == '') { return false; }
    return true;
}

