// JavaScript Document

// This object replaces the inner text of an element called by id.
// Use for hyperlink replacement to direct html to another element.
// Path excludes trailing slash.
// Ext excludes leading period.

	function AjaxToElement(path, ext) {

		// Private Attributes
		var AJAX   = null;
		var myID   = null;
		var myPath = path!=null?path+'/':'';
		var myExt  = ext!=null?'.'+ext:'';
		var myIndication = '<div style="height:500px; width:150px; text-align:center; vertical-align:middle; color:#000066; font-size:20px;">Loading ...</div>'

		// Public Methods
		this.abort = abort;
		this.get   = get;
		this.post  = post;
		
		// Data must be preformatted in item=value format.
		function get(file, id, data) {
			return update(myPath+file+myExt, id, data, 'GET');
		}
		
		function post(file, id, data) {
			return update(myPath+file+myExt, id, data, 'POST');
		}
		
		function abort() {
			if (AJAX != null) {
				AJAX.abort();
				AJAX=null;
			}
		}

		// Private Methods
		function xObj(ready) {
			var x = (navigator.appName == "Microsoft Internet Explorer")? new ActiveXObject("Microsoft.XMLHTTP"): new XMLHttpRequest();
			x.onreadystatechange = ready;
			return x;
		}
		
		function update(url, id, data, method) {
			if ((AJAX==null)&&(url!=null)&&(id!=null)) {
				if ((AJAX = xObj(ready)) != null) {
					myID = id;
					method=(data==null)?"GET":method;
					if (/post/i.test(method)) {
						AJAX.open("POST", noCache(url), true);
						AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
						AJAX.setRequestHeader("Content-Length", data.length);
						AJAX.send(data);
					} else {
						url=(data==null)?url:url+'?'+data;
						AJAX.open("GET", noCache(url), true);
						AJAX.send(null);
					}
					return true;
				}
			}
			return false;
		}
		
		function ready() {
			switch(AJAX.readyState) {
//			case 1:	callback('<div style="text-align:center; padding-top:100px;"><img src="http://www.windycitywebster.com/JS/ajax-loader.gif" /></div>'); break;
				case 1:	callback(myIndication); break;
				case 4:	if (AJAX.status == 200){ callback(AJAX.responseText); AJAX=null; }	break;
				default: break;
			}
		}

		function callback(text) {
			var target = document.getElementById(myID);
			if (target != null) {
				var newdiv = document.createElement(target.type);
				newdiv.id = target.id;
				newdiv.innerHTML = text;
				target.parentNode.replaceChild(newdiv,target);
			} 
			else
				alert("Element "+myID+" not found");
		};

		function noCache(url) { 
			var num = new Date().getTime(); 
			return reqstring=(url.indexOf("?")!=-1)?url+"&"+num:url+"?"+num; 
		}
	}
