var GetProspectorIDList = new Array();
var GetProspectorISBNList = new Array();

function getProspectorStatuses(id, isbn)
{
    GetProspectorIDList[GetProspectorIDList.length] = id;
    GetProspectorISBNList[GetProspectorISBNList.length] = isbn;
}

function doGetProspectorStatuses()
{
	if (GetProspectorIDList.length == 0) return;

    var now = new Date();
    var ts = Date.UTC(now.getFullYear(),now.getMonth(),now.getDay(),now.getHours(),now.getMinutes(),now.getSeconds(),now.getMilliseconds());
    var http = createRequestObject();

    var url = path + "/Search/AJAX?method=getProspectorItemStatus";
    var elemId;
	var isbn;
	var doAjax = false;
    for (var i=0; i<GetProspectorIDList.length; i++) {
		elemId = GetProspectorIDList[i];
        statusDiv = getElem('status' + elemId);
		if (statusDiv && statusDiv.firstChild) {
			statusClass = statusDiv.firstChild.className;
			//alert(statusClass);
			if (statusClass == 'checkedout') {	
				isbn = GetProspectorISBNList[i];
				//alert('doGetProspectorStatuses: isbn=' + isbn);
				if (isbn == "") {
					// Other Sources link
					if (document.URL.match('Search/Home'))
					statusDiv.innerHTML += ' &nbsp; <a href="' + path + '/Record/' + elemId + '/OtherSources" title="Check other sources for this item" class="holdicon" style="white-space: nowrap">Other Sources</a>';
				} else {
                	statusDiv.innerHTML += ' &nbsp; Checking Regional Catalog (Prospector)...';
					url += "&id[]=" + elemId;
					url += "&isbn[]=" + isbn;
					doAjax = true;
				}
			}
		}
    }
	if (doAjax == false) return;
    url += "&time="+ts;

    http.open("GET", url, true);
    http.onreadystatechange = function()
    {
        if ((http.readyState == 4) && (http.status == 200)) {
            var response = http.responseXML.documentElement;
            var items = response.getElementsByTagName('item');
            var statusDiv;

            for (i=0; i<items.length; i++) {
                elemId = items[i].getAttribute('id');
				elemId8 = elemId.substr(1,8);
				isbn = items[i].getAttribute('isbn');
                statusDiv = getElem('status' + elemId);
				statusDiv.innerHTML = statusDiv.innerHTML.replace(' &nbsp; Checking Regional Catalog (Prospector)...', '');
				if (items[i].getElementsByTagName('prospector')
				 && items[i].getElementsByTagName('prospector').item(0).firstChild
				 && items[i].getElementsByTagName('prospector').item(0).firstChild.data > 0) {
					isSearch = document.URL.match('Search/Home');
					isSources = document.URL.match('OtherSources');
					availText = isSources ? 'Available' : 'Available from Regional Catalog (Prospector)';
					statusDiv.innerHTML += ' &nbsp; ' 
					// Prospector link
					+ '<span class="available">'
					+ '<a style="white-space: nowrap" ' 
					+ 'title="View and request this item in Prospector" '
					+ 'href="http://prospector.coalliance.org/search/'
					+ '?searchtype=z&searcharg=9csup+.' + elemId8 
					+ '&backlink=' + document.URL + '">' 
					+ availText
					+ '</a>'
					+ '</span> ';
					if (isSearch) {
						// help icon
						statusDiv.innerHTML += '' 
						+ '<a href="http://lib.colostate.edu/access/prospector.html" '
						+ 'title="About the Regional Catalog (Prospector): 23 libraries in Colorado and Wyoming">'
						+ '<img src="/images/silk/help.png" border="0" style="vertical-align:middle;" ' 
						+ 'alt="About the Regional Catalog (Prospector): ' + "\n" + '23 libraries in Colorado and Wyoming">'
						+ '</a>';
					} else {
						// request link
						statusDiv.innerHTML += ' &nbsp; '
						+ '<a class="holdicon" title="Request this item from Prospector '
						+ '(Log in with your CSU ID and select a pickup location)" '
						+ 'href="http://prospector.coalliance.org/search~S0?/' 
						+ 'z9csup+' + elemId8 + '/z9csup+' + elemId8 + '/1%2C1%2C1%2CB/request&FF=z9csup+' + elemId8 + '&1%2C1%2C'
						+ '&backlink=' + document.URL + '">Request This</a>';
					}
				} else if (document.URL.match('Search/Home')) {
					// Other Sources link
					statusDiv.innerHTML += ' &nbsp; <a href="' + path + '/Record/' + elemId + '/OtherSources" title="Check other sources for this item" class="holdicon" style="white-space: nowrap">Other Sources</a>';
				}
            }
        }
    }
    http.send(null);
}

