// Content: js RPCFrame
var autoID = 1;
function OldRPCFrame(targetWindow) {
	this.ID = autoID++;
	this.parent = targetWindow;
	if ((window.navigator.appVersion.search("MSIE 5.0") != -1) && (navigator.platform.indexOf('Mac')==-1)) {
		this.IE50 = true;
		var newFrameHTML = "<iframe id='iframe"+this.ID+"' style='position:absolute;top:" + this.ID + "px;display:block;border:0px;width:1px;height:1px;'></iframe>";
		var d = document.createElement("div");
		d.innerHTML = newFrameHTML;
		document.body.appendChild(d);
		this.HTMLObject = targetWindow.document.getElementById('iframe'+this.ID);
	} else {
		this.IE50 = false;
		var newFrame = document.createElement("iframe");
		newFrame.setAttribute("id", "iframe"+this.ID);
		newFrame.style.position = "absolute";
		newFrame.style.border = "0px";
		newFrame.style.width = "0px";
		newFrame.style.height = "0px";
		this.HTMLObject = document.body.appendChild(newFrame);
	}
	return this;
}
OldRPCFrame.prototype.setLocation = function (newURL, callBack) {
	this.HTMLObject.src = newURL;
	addEventHandler(this.HTMLObject, "load", callBack);
	if (this.IE50) {
		if (callBack) top.callBack = callBack; else top.callBack = null;
		this.getDocument().onreadystatechange = this.returnToCaller;
	}
}
OldRPCFrame.prototype.returnToCaller = function () {
	if (this.readyState == "complete") {
		if (top.callBack) top.callBack();
	}
}

OldRPCFrame.prototype.getDocument = function () {
	if (this.HTMLObject.contentDocument) {
		return this.HTMLObject.contentDocument;
	} else if (this.HTMLObject.contentWindow) {
		return this.HTMLObject.contentWindow.document;
	} else if (this.HTMLObject.document) {
		if (navigator.platform.indexOf('Mac') != -1) {
			return this.parent.document.frames["iframe"+this.ID].document;
		} else {
			return this.parent.frames["iframe"+this.ID].document;
		}
	}
}
OldRPCFrame.prototype.destroy = function () {
	this.ID = null;
	this.HTMLObject.removeNode(true);
}
OldRPCFrame.prototype.getHTMLById = function (id) {
	return this.getDocument().getElementById(id);
}
OldRPCFrame.prototype.getHTMLByTag = function (tag) {
	return this.getDocument().getElementsByTagName(tag);
}

function getHTMLById(element, id) {
	var children = element.childNodes
	for (var i=0;i<children.length;i++) {
		if (children[i].id) {
			if (children[i].id == id) {
				return children[i];
			}
		}
		var child = getHTMLById(children[i], id);
		if (child != null) {
			return child;
		}
	}
	return null;	
}

var oldResponseText="";
function RPCFrame(targetWindow) {
	this.setLocation = function (newURL, callBack) {
		mycallback = callBack;
		xmlhttp.open("GET", newURL ,true);
		xmlhttp.onreadystatechange = this.myonreadystatchange;
		xmlhttp.send(null);
	}

	this.myonreadystatchange = function() {
		if (xmlhttp.readyState == 4 && oldResponseText!=xmlhttp.responseText) {
			var container = document.createElement("DIV");
			container.innerHTML = xmlhttp.responseText; 
                                                oldResponseText=xmlhttp.responseText;
			mycontent = container;
			mycallback();
		}
	}
	
	this.getDocument = function () {
		return mycontent;
	}

	this.destroy = function () {
		mycontent = null;;
	}

	this.getHTMLById = function (id) {
		return getHTMLById(mycontent, id);
	}

	this.getHTMLByTag = function (tag) {
		return mycontent.getElementsByTagName(tag);
	}


	var xmlhttp;
	var mycallback;
	var mycontext;
	var mycontent;
	try {
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	} catch (e) {
		try {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		} catch (E) {
			xmlhttp=false
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	// In case xmlrpc is not supported we will fallback to the old method.
	if(!xmlhttp) {
		return new OldRPCFrame(targetWindow);
	}
	return this;
}