///////////////////////////////////////////////////////////////////////////////
// ajax routines

function rwajx_docallback(urlaction)
{
	if(typeof init == 'function')
	{
		init();
	}
	if(typeof formvalidate == 'function')
	{
		if ( !formvalidate())
			return false;
	}
	var ajaxworking = document.getElementById("ajaxworking");
	if (ajaxworking)
	{
		ajaxworking.style.visibility = "visible";
	}
	//alert('done valid');
	var params;
	if(typeof getparams == 'function')
		params = getparams();
	//alert('done getparam');
	rwajx_httprequest(urlaction, params);
	return true;
}

function rwajx_httprequest(action, params)
{
	var req;
	if ( !params || params == null) params = '';

	if (window.ActiveXObject) // IE
	{
		//req = new ActiveXObject('Microsoft.XMLHTTP');
		req = rwajx_MSHttpRequest();
		if (req)
		{
			req.onreadystatechange = function() { rwajx_processrequest(req); };
			req.open('POST', fullhostname + '/' + action, true);
			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			req.setRequestHeader("Content-length", params.length);
			req.setRequestHeader("Connection", "close");
			jswait(500);
			req.send(params);
		}
	}
	else if (window.XMLHttpRequest)// Mozilla, Safari, Firefox...
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = function() { rwajx_processrequest(req); };
		req.open('POST', fullhostname + '/' + action, true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", params.length);
		req.setRequestHeader("Connection", "close");
		jswait(500);
		req.send(params);

	}
}

function rwajx_MSHttpRequest()
{
	var progIDs = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP' ];

	for (var i = 0; i < progIDs.length; i++) {
		try {
			var xmlHttp = new ActiveXObject(progIDs[i]);
			//alert(progIDs[i]);
			return xmlHttp;
		}
		catch (ex) {  }
	}
	return false;
}

function jswait(millis)
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while(curDate-date < millis);
}

function rwajx_processrequest(req)
{
	if (req)
	{
		if (req.readyState == 4)
		{
			var ajaxworking = document.getElementById("ajaxworking");
			if (ajaxworking)
			{
				ajaxworking.style.visibility = "hidden";
			}
			if (req.status == 200)
			{
				// good to go
				if(typeof ajaxhandler == 'function')
					ajaxhandler(req.responseText);
			}
			else if (req.status >= 12029 && req.status <= 12159)
			{
				alert('Error processing your request, please re-try');
			}
			else
			{
				alert('HTTP ' + req.status + ' error');
			}
		}
	}
	else alert('Sorry, this browser does not support callbacks');
}

///////////////////////////////////////////////////////////////////////////////
function makeHttpRequest(url)
{
	var d = new Date();
	var t = "" + d.getHours() + d.getMinutes() + d.getSeconds();
	var req;
	if(typeof loadingMessage == 'function')
	{
		loadingMessage();
	}

	if (window.ActiveXObject)
	{
		req = rwajx_MSHttpRequest();
		if (req)
		{
			req.onreadystatechange = function() { processStateChange(req); };
			req.open('GET', fullhostname + url + '&t=' + t, true);
			req.send();
		}
	}
	else if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = function() { processStateChange(req); };
		req.open('GET', fullhostname + url + '&t=' + t, true);
		req.send(null);
	}
}

function processStateChange(req)
{
	if (req)
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				var resp = req.responseText;
				if(typeof completeMessage == 'function')
				{
					completeMessage();
				}
				// check for any redirects
				if ( typeof redirectUrl != 'undefined' && redirectUrl)
				{
					location.href= redirectUrl;
				}
			}
			else
			{
				alert('AJAX request error ' + req.status);
			}
		}
	}
	else alert('Sorry, this browser does not support AJAX callbacks');
} // end function



/////////////////////////////////////////////////////////////////////////////////////////////
function rwajx_item(itemid, propid)
{
	var d = new Date();
	var t = "" + d.getHours() + d.getMinutes() + d.getSeconds();
	var req;

	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = function() { processReqChange(req); };
		req.open('GET', fullhostname + '/propitem.rwx?itemid=' + itemid
			+ '&propid=' + propid + '&t=' + t, true);
		/*
  		if (req.overrideMimeType)
  		{
			req.overrideMimeType('text/xml');
        }
        */
		req.send(null);
	}
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject('Microsoft.XMLHTTP');
		if (req)
		{
			req.onreadystatechange = function() { processReqChange(req); };
			req.open('GET', fullhostname + '/propitem.rwx?itemid=' + itemid
				+ '&propid=' + propid + '&t=' + t, true);
			req.send();
		}
	}

}

function processReqChange(req)
{
	if (req)
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				var resp = req.responseText;
				var respArray = resp.split('%%%');
				if ( respArray.length > 0 )
				{
					var id = respArray[0];
					//alert(respArray[1]);
					var e = document.getElementById(id);
					e.innerHTML=respArray[1];
					var _id = "_" + id;
					e = document.getElementById(_id);
					e.innerHTML = "&nbsp;";
				}
				else
				{
					alert('Error retrieving property information');
				}
			}
			else
			{
				alert('Problem retrieving property information');
			}
		}
	}
	else alert('Sorry, this browser does not support callbacks');
} // end function


function rwajx_close(itemid)
{
	var e = document.getElementById(itemid);
	e.innerHTML="";
	var _id = "_" + itemid;
	e = document.getElementById(_id);
	e.innerHTML = "";
}


function rwajx_county(city, propid)
{
	var d = new Date();
	var t = "" + d.getHours() + d.getMinutes() + d.getSeconds();
	var req;

	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = function() { processReqChange(req); };
		req.open('GET', fullhostname + '/countylookup.rwx?itemid=' + itemid
			+ '&propid=' + propid + '&t=' + t, true);
		req.send(null);
	}
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject('Microsoft.XMLHTTP');
		if (req)
		{
			req.onreadystatechange = function() { processReqChange(req); };
			req.open('GET', fullhostname + '/propitem.rwx?itemid=' + itemid
				+ '&propid=' + propid + '&t=' + t, true);
			req.send();
		}
	}

}


function countyLookup(cityfld)
{
	if (!isblank(cityfld.value))
	{
	}
	else
	{
		cityfld.form.county.value = "";
	}
}

	function isblank(s)
	{
	   if ( s == null || s=="") return true;
	   for (var i=0; i < s.length; i++) {
	     var c = s.charAt(i);
	     if (( c != ' ') && (c != '\n') && (c != '\t')) return false;
	   }
	   return true;
	}

