// example of useage:   onclick="displayResults('fillme','username','testTxt','alias','testHidden');"
//                      results in ajaxcodebehind.asp?username=mew3&alias=SpaceGhost
//                          assuming that mew3 is the value in the element with the id "testTxt"
//                          and SpaceGhost is the value in the element with the id "testHidden"
//                      the div with id 'fillme' will populate with the results from the url.
// this code uses the url 'ajaxcodebehind.asp' to do the server-side functions
// mew 09/15/2008

var xhr=false;
var vdivID;

function displayResults(divLocation,mlsnum){
    var url;
    //alert($(divLocation).innerHTML.length);
    vdivID = divLocation;
    var mlsSend;
    if ($(divLocation).innerHTML.length > 1000) {
        mlsSend = '00001';
    }
    else {
        mlsSend = mlsnum;
    }
    url = "ajaxcodebehind.asp?mls=" + mlsSend;
    makeRequest(url);
    return false;
}

function makeRequest(url) {
    if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}

	if (xhr) {
	    xhr.onreadystatechange = showContents;
		xhr.open('GET', url+'&ms='+ new Date().getTime(), true);    //+'?ms='+ new Date().getTime() keeps from caching
		xhr.send(null);
	}
	else {
		document.getElementById(divID).innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
	}
}

function showContents() {
// spinner
if (xhr.readyState != 4 ){
    // loading graphic here
    //$(vdivID).innerHTML="<div style='text-align:center;'><img src='../../graphics/spinner20.gif' /></div>";
    }
	
	if (xhr.readyState == 4) {
	    if (xhr.status == 200) {
	        //may use xml or text (html)
			//var outMsg = (xhr.responseXML && xhr.responseXML.contentType=="text/xml") ? xhr.responseXML.getElementsByTagName("b2bCSItems")[0].textContent : xhr.responseText;
			var outMsg = xhr.responseText;
		}
		
		else {
			var outMsg = "There was a problem with the request " + xhr.status;
		}
		$(vdivID).innerHTML = outMsg;
	}
}