
// ajax - dynamic load 


// cross-browser.com DHTML Lib dependencies
xSmartLoadScript("/js/x/lib/xdef.js");
xSmartLoadScript("/js/x/lib/xstr.js");
xSmartLoadScript("/js/x/lib/xgetelementbyid.js");
xSmartLoadScript("/js/x/lib/xdisplay.js");
xSmartLoadScript("/js/x/lib/xvisibility.js");
xSmartLoadScript("/js/x/lib/xinnerhtml.js");
xSmartLoadScript("/js/x/lib/xhttprequest.js");


var XHR;

/*  use xHttpRequest post to load content into a div 
 *  url: post target url
 *  id: id of div element to load into
 *  postData: urlencoded data to post
 */
function nasAjaxLoad(url, id, postData)
{
	//alert("nasAjaxLoad");
	if (xGetElementById(id))
	{
		//alert("send request");
		XHR = new xHttpRequest();
		
		if (!XHR.send('POST', url, postData, 2000, 'rnd', false, id, nasAjaxLoadCallback)) 
		{
			alert('request error');
			xGetElementById(id).innerHTML = '<p>Request Error (' + XHR.err.name + '): ' + XHR.err.message + '<\/p>';
		}
      
	}
	else 
	{
		alert("element "+id+" not found");
	}
}



/*  Callback handles nasAjaxLoad result
 *	req: XMLHttpRequest object
 *  status: status from xHttpRequest call
 *  data: id of target element as passed above.
 */
function nasAjaxLoadCallback(req, status, data)
{
	//alert('callback');
	s = '';
	if (status == XHR.OK) 
	{
    	if (xGetElementById(data))
    	{
    		xInnerHtml(data, req.responseText);
    		xVisibility(data, true);
    	}
  	}
  	else 
  	{
    	if (status & XHR.TIMEOUT) 
    	{
      		s = '<p>Timeout Error<\/p>';
    	}
    	if (status & XHR.NOXMLCT) 
    	{
      		s = '<p>XML content-type expected but received: ' + req.getResponseHeader('Content-Type') + '<\/p>';
    	}
    	if (status & XHR.RSPERR) 
    	{
      		s = '<p>Response Error (' + req.status + '): ' + req.statusText + '<\/p>';
    	}
    	alert (s);
    }
}
