function $$(id) { return document.getElementById(id); }

function isset(variable) {
    return typeof(variable) != "undefined" && variable !== null;
}

function getName() {
	var new_name = Math.random();
	while (document.getElementsByName("new_name").length || $$("new_name") ) {
		new_name = Math.random();
	}
	return new_name;
}

//run script in between script tags
function runJavascript(html) {
	var scripts = html.match(/<script[^>]*>([\s\S]+?)<\/script>/ig);
	if (scripts != null) {
		for (var i=0; i < scripts.length; i++) {
			eval(scripts[i].replace(/([\s\S]*<script[^>]*>|<\/script>[\s\S]*)/ig, ""));
		}
	}
}

var xmlhttp_arr = [];
var xmlhttp_ready = [];
function getNewXmlHttp () {
	for(var i = 0; i < xmlhttp_arr.length; i++) {
		if (xmlhttp_ready[i]) {
			xmlhttp_ready[i] = false;
			return i;
		}
	}
	xmlhttp_arr[i] = false;
	xmlhttp_ready[i] = false;
	if(window.ActiveXObject) xmlhttp_arr[i] = new ActiveXObject("Microsoft.XMLHTTP");
	if(window.XMLHttpRequest) xmlhttp_arr[i] = new XMLHttpRequest();
	return i;
}

function replaceNode(node,file) {
	var index = getNewXmlHttp(); 
	if (! xmlhttp_arr[index]) return;
	xmlhttp_arr[index].open("GET", file, true);
	xmlhttp_arr[index].onreadystatechange = function() {
		if(xmlhttp_arr[index].readyState == 4) {
			node.innerHTML = xmlhttp_arr[index].responseText;
				var j = 0;
			for (var i = 0; i < node.childNodes.length; i++) {
				if (node.childNodes[i].tagName) {
					node.parentNode.insertBefore(node.childNodes[i].cloneNode(true), node);
				}
			}
			node.parentNode.removeChild(node);
			xmlhttp_ready[index] = true;
			runJavascript(xmlhttp_arr[index].responseText);
		}
	}
	xmlhttp_arr[index].send(null);
}

