var JSONAjax=new Object();
JSONAjax.READY_STATE_UNINITIALIZE=0;
JSONAjax.READY_STATE_LOADING=1;
JSONAjax.READY_STATE_LOADED=2;
JSONAjax.READY_STATE_INTERACTIVE=3;
JSONAjax.READY_STATE_COMPLETE=4;
JSONAjax.SERIALNO = 0;
JSONAjax.ContentLoader=function(url,onload,isForcedIFrame, onerror, isHTML){
	this.url = url;
	this.req=null;
	this.onload= onload;
	this.onerror=(onerror)? onerror : this.defaultError;
	this.IFrameObj = null;
	this.isForcedIFrame=(isForcedIFrame)? true : false;
	this.isHTML= (isHTML)? true : false;
}

JSONAjax.ContentLoader.prototype={
	loadContent:function(bAfterComplete){
		var targetHost = this.getHost(this.url);
		var thisHost = this.getHost(document.location.href);
		if(!this.isForcedIFrame && (targetHost == null || targetHost == thisHost)) {
			this.loadAjaxContent(); //same server
		}
		else { //different server
			if(document.readyState && document.readyState != 'complete' && bAfterComplete != 'undefined' && bAfterComplete == true ) { //ie and delayed
				var loader = this;
				attachEvent("onload", function(){loader.loadIFrameContent();});
			}
			else {
				this.loadIFrameContent();
			}
		}
	},
	loadContentAfterComplete:function(){
		this.loadContent(true);
	},
	loadAjaxContent:function(){
		if(window.XMLHttpRequest) {
			this.req = new XMLHttpRequest();
		}
		else if(window.ActiveXObject) {
			this.req=new ActiveXObject("Microsoft.XMLHTTP");
		}

		if(this.req){
			try{
				var loader = this;
				this.req.onreadystatechange=function(){
					loader.onReadyState();
				};

				this.req.open('GET', this.url, true);
				this.req.send(null);
			}
			catch ( err ) {
				this.onerror();
			}
		}
	},
	loadIFrameContent:function(){
		//alert('ready:' +  document.readyState);
		var iframeURL = this.url + ((!this.url.match("\\?"))? "?" : "&") +  "callback=" + this.onload;

		if (!this.IFrameObj) {
			try {
				var iframeID = 'JSONIFrame' + JSONAjax.SERIALNO ++;
				if(document.readyState) { // IE
					var iframeHTML='<iframe id="' + iframeID + '" style="';
					iframeHTML+='border:0px;';
					iframeHTML+='width:0px;';
					iframeHTML+='height:0px;';
					iframeHTML+='"></iframe>';

					if(document.readyState == 'complete') {
						document.body.insertAdjacentHTML("afterEnd", iframeHTML);
					}
					else {
						document.write(iframeHTML);
					}

					this.IFrameObj = document.getElementById(iframeID);
				}
				else { // for firefox
						var tempIFrame=document.createElement('iframe');
						tempIFrame.setAttribute('id', iframeID);
						tempIFrame.style.border='0px';
						tempIFrame.style.width='0px';
						tempIFrame.style.height='0px';
						this.IFrameObj = document.body.appendChild(tempIFrame);
				}
			} catch(exception) {
				alert('json_ajax.js: debugging' + exception);
			}
		}
		if( this.IFrameObj ) this.IFrameObj.src = iframeURL;
	},
	onReadyState:function() {
		var req = this.req;
		var ready = req.readyState;
		if( ready == JSONAjax.READY_STATE_COMPLETE) {
			var httpStatus = req.status;
			if( httpStatus == 200 || httpStatus == 0) {
				var onload = eval( this.onload );
				if (this.isHTML == false) {
					onload(eval('(' + req.responseText + ')'));
				} else {
					onload(req.responseText);
				}
			}
			else {
				this.onerror();
			}
		}
	},
	defaultError:function(){
	/* for debugging
		alert("error fetching data!"
			+"\n\nreadyState:" + this.req.readyState
			+"\nStatus:" + this.req.status
			+"\nheaders: " + this.req.getAllResponseHeaders());
	*/
	},
	getHost:function(url) {
		var reg = /^http:\/\/([^\/]*)\/.*/i;
		var matched = reg.exec(url);
		var host = null;
		if(matched != null && matched.length == 2) {
			host = matched[1];
		}
		return host;
	}
};

function ajaxHttpRequest(url, callback, isForcedIFrame, isHTML) {
  var contentLoader = new JSONAjax.ContentLoader(url, callback, isForcedIFrame, null, isHTML);
  contentLoader.loadContentAfterComplete();
}