// THE SEARCH FUNCTION

// global flag
var isIE = false;

var xmlhttp;

var url = 'livesearch.php?search=' + document.getElementById('search_field').value;

/*try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e){
try {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e){
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
*/
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = processXmlhttpChange;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp) {
            xmlhttp.onreadystatechange = processReqChange;
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }
    }
}

function processXmlhttpChange() {
    // only if xmlhttp shows "loaded"
    if (xmlhttp.readyState == 4) {
        // only if "OK"
        if (xmlhttp.status == 200) {
            document.getElementById('search_results').innerHTML = xmlhttp.responseText + ' ';
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlhttp.statusText);
         }
    }
}

function liveSearch() {
	loadXMLDoc('/livesearch.php?search=' + document.getElementById('search_field').value)
  /*      var url = '/livesearch.php?search=' + document.getElementById('search_field').value;
        xmlhttp.open('GET', url, true);
        xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4) {
                        document.getElementById('search_results').innerHTML = xmlhttp.responseText + ' ';
                }
        }
        xmlhttp.send('');*/
}


// MAKE SEARCH BUTTON LIKE A TIGER SEARCH BUTTON
function searchfield() {
  document.getElementById('search_field').type = 'search';
}

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

function onload() {
	searchfield(); // Changes the search field into a Tiger search field
	addEvent(document.getElementById('search_field'), 'keyup', liveSearch);
	// document.getElementById('search_field').addEventListener('keyup',liveSearch,false); // not supported by MSIE
}

addEvent(window, 'load', onload);