var IE = document.all?true:false
var isDev = (document.location.href.indexOf("pxccc/") >= 0);

function showLoading(v) {
	if (!IE)
		theElement = document.getElementById('searchResults');
	else
		theElement = document.all('searchResults');
	theElement.innerHTML = ((v == 1) ? 'Loading...' : '');
}

function makeRequest(searchTerm) {
	var url;
	var http_request = false;
	if (isDev)
		url = document.location.protocol + '//' + document.location.host + '/pxccc/search.php?item=' + searchTerm + '&r=';
	else
		url = document.location.protocol + '//' + document.location.host + '/search.php?item=' + searchTerm + '&r=';
	// IE doesn't seem to like the no-cache, so requires this cachebuster:
	for (var i=0; i<10; i++)
		url = url + String.fromCharCode((Math.floor(Math.random() * 1000) % 26) + 65);
	
	showLoading(1);	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    http_request = new XMLHttpRequest();
	    if (http_request.overrideMimeType) {
	    	//Breaks Firefox:
	        //http_request.overrideMimeType('text/xml');
	    }
	} else if (window.ActiveXObject) { // IE
	    try {
	        http_request = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	        try {
	            http_request = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch (e) {}
	    }
	}
	
	if (!http_request) {
	    alert('Cannot create an XMLHTTP instance');
	    return false;
	}
	
	http_request.onreadystatechange = function() { alertContents(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function doSearch() {
	var term = document.forms['searchForm'].elements['q'].value;
	if (term.length == 0) {
		alert("No search term given");
		return false;
	}
	makeRequest(term);
	return false;
}

function processLine(line) {
	var season, number, name;
	var pos1 = line.indexOf('|');
	var pos2 = line.indexOf('|', pos1+1);
	if ((pos1 == -1) || (pos2 == -1))
		return '';
	season = line.substring(0, pos1);
	number = line.substring(pos1+1, pos2);
	name = line.substr(pos2+1);
	while (number.length < 3)
		number = '0' + number;
	if (isDev)
		return '<a href="/pxccc/' + season + '/' + number + '/" class="searchResult">' + name + '</a><br />';
	return '<a href="/' + season + '/' + number + '/" class="searchResult">' + name + '</a><br />';
}

function alertContents(http_request) {
	var theElement;
	var resultHtml = '';
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
        	var line = '';
        	var mode = 0;
        	for (var i=0; i < http_request.responseText.length; i++) {
        		var ch = http_request.responseText.charAt(i);
        		switch(ch) {
        		case '\n':
        		case '\r': 
					resultHtml = resultHtml + processLine(line);
        			line = '';
        			break;
        		default:
					line = line + ch;
        		}
        	}
			showLoading(0);
			if (!IE)
				theElement = document.getElementById('searchResults');
			else
				theElement = document.all('searchResults');
			if (resultHtml.length == 0)
				resultHtml = "nothing found";
			theElement.innerHTML = resultHtml;
        } else {
            alert('There was a problem with the request.');
        }
    }
}
