// getElement
// -----------------------------------------------------------------------
function getElement(el)  	{ 
	try { return document.all ? document.all[el] : document.getElementById(el); }
	catch (e) { req.running=0; alert('getElement.err('+el+'): '+e.number+'\nmsg: '+e.message); }
}
// Request
// -----------------------------------------------------------------------
function Request()	{ 
	var xmlhttp;
	var warning		= 'Request in process. Please resubmit last request';
	var req			= new Object;
	req.running		= 0;
//	req.pageHistory		= new Array();
//	req.pageHistoryTemp	= new Array();
		
	req.html;
	/* the following attributes may be set by developer */
	req.async 		= true;
	req.concurrent		= false;
	req.showProgress	= true; 
	req.progressImage	= 'progress.gif'; 
	req.element		= ''; 			// HTML element to display results
	req.iPath		= '/images';
	req.url			= 'htdocs/home.html';		// url to call
	req.method		= 'POST'; 
	req.garbage		= function () { };
	// req.xhrerr 		if DEFINED: it will be the ERROR TAG, defaults to results ELEMENT
	
	// req.cookie	= 'siman=says;';	// DOES NOT APPEAR TO WORK FOR COOKIES!
	req.progressImage			= 'progress.squares.gif';
	req.data	= '';			// POSTed data
	req.exception	= function (xhr) {
			req.running	= 0; 
			req.garbage	= function () { };
			var errmsg	= 'Error: '+xhr.status+'\n'+xhr.statusText;
			if (req.xhrerr == undefined) 	{ return errmsg; }
			else 				{ getElement(req.xhrerr).innerHTML = errmsg; return ''; }
	}
	req.Abort	= function () { 
			req.running	= 0;
			if (req.previousContent) { 
				getElement(req.element).innerHTML	= req.previousContent;
			}
			req.garbage	= function() { return; };
			req.exception	= function() { return; };
			try 	 { xmlhttp.abort(); } 
			catch(e) { }
	}
	req.history	= function (pageNumber) {
			// ------------------------------------------------------
			// return page content from history array
			// needs to be a private collector so that developer CANNOT overwrite...
			// ------------------------------------------------------
			if (getElement(req.element) == undefined) { alert('element ('+req.element+') NOT found!'); return; }
			var currentPage		= getElement(req.element);
			

			// TBD: -----------------------------------------------------------------
			// need to store HiSTORY BY RESULTS-ELEMENT
			// applications may use more than one results element for the same component
			// cannot write the history back to the wrong results elements.
			// ---------------------------------------------------------------------

			// check to see if elementHistory exists
			var Hx		= req.element+'Hx';
			var HxTmp	= Hx+'Tmp';
			if (req[Hx] == undefined) { req[Hx] = new Array(); }


			if 	(pageNumber == -1) { 
					if (req[HxTmp].length > 0) { 
						req[Hx].push(currentPage.innerHTML);
						currentPage.innerHTML = req[HxTmp].pop(); 
					}
					else { }			// img.forward=off
						}
			else if (pageNumber == 1)  {			// img.forward=on
					req[HxTmp].push(currentPage.innerHTML);
					currentPage.innerHTML = req[Hx].pop(); 
						}
			else 			{ 			// img.forward=off+img.back=on
					req[Hx].push(currentPage.innerHTML);		// record previous page data
					req[HxTmp]		= new Array();		// clear out temporary pageForward array
			} 
			return;
	} 
	req.send	= 
		function (url,data,el) { 
			// following routines are NOT changeable (defined within fnc)
			// --------------------------------------------------------------
			req.callback	= function () { return xmlhttp.responseText; }
			try { 
				var response		= getElement(req.element);		// determine response element

				// display progress image
				var progressbar	 = '<br><table align=center><tr><td><img src="'+req.iPath+'/'+req.progressImage+'"></td></tr>';
				progressbar	+= '<tr><th>Loading data...</th></tr></table><br>';
				if (req.showProgress) {	response.innerHTML	= progressbar; }

				// are we allowed to run concurrent operations?
				if (! req.concurrent) { 
					if (req.running)	{ return alert(warning); }	// globally assign value to RUNNING...
					req.running = 1;
				}

				// determine which object to instantiate
				if 	(window.XMLHttpRequest)	{ xmlhttp = new XMLHttpRequest(); }
				else if (window.ActiveXObject)	{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
				else				{ return alert('Unable to complete XMLHttp call');  }
	
				//  laod inputs
				if (url != undefined)	{ req.url	= url; }
				if (data != undefined)	{ req.data 	= data; }
				if (el != undefined)	{ req.element	= el; }
				
				req.history();

				// define internal methods
				xmlhttp.open(req.method, req.url, req.async);
				xmlhttp.onreadystatechange	= 
					function() { 
						if (xmlhttp.readyState == 4) {
							// Your callback code goes here
							if (xmlhttp.status == 200) 	{ response.innerHTML = req.callback(); }
							else 				{ response.innerHTML = req.exception(xmlhttp); }
							delete xmlhttp.onreadystatechange;
							xmlhttp		= null;
							req.running 	= 0;
							req.garbage();
						}
					}
				// setHeader(s) stored in obj
				// automatically brings stored-cookie data
				xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				xmlhttp.send(req.data);
			}
			catch (e) { req.running=0; alert('xmlhttp.err: '+e.number+'\nmsg: '+e.message); }
		}
  return req;
}
// end Request

