
var req = null;

function InitXMLHttpRequest() {
	// Make a new XMLHttp object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function RunJS(dest, response){
	if (response.search(/<script/) == -1){
		dest.innerHTML=response;
		return false;
	}
	//Check user browser
	var agent=(window.navigator.appName=='Netscape')?'Firefox':'IE';
	if(agent=='IE'){
		///place &nbsp; before <scrript> tag
		dest.innerHTML=response;
		var arr_scripts = dest.getElementsByTagName("script");
		for(i in arr_scripts){
			var src = arr_scripts[i].src;
			if(null!=src){
				var has_question = (src.indexOf('?', 1)>0 ? true : false);
				if(0==src.length)
					arr_scripts[i].setAttribute('src', '');
				else
					arr_scripts[i].setAttribute('src', arr_scripts[i].src + (has_question==true ? '&' : '?') + '1=1');
			}
		}
	}else{
		//Mozilla
		var d=dest.parentNode;
		var d_new=document.createElement('div');

		//Set same attributes as original
		var attributes=dest.attributes;
		var len=attributes.length;

		d_new.innerHTML=response;
		for(var i=0; i<len; i++){
			d_new.setAttribute(attributes[i].name, attributes[i].value);
		}

		d.replaceChild(d_new, dest);
	}
}

function ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous){
	if (str != '') {
		str = str+'&ajax=1';
		InitXMLHttpRequest();
		// Load the result from the response page
		if (req) {
			if (anisochronous){
				req.onreadystatechange = function() {
					if (req.readyState == 4) {
						//destination_odj.innerHTML = req.responseText;
						RunJS(destination_odj,req.responseText);
					} else {
						destination_odj.innerHTML = tmp_text;
					}
				}
			}
			req.open("POST", file_name+'?rnd='+Math.random(), anisochronous);
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8 ');
			req.setRequestHeader("Content-length", str.length);
      		req.setRequestHeader("Connection", "close");
			req.send(str);
			if (!anisochronous) RunJS(destination_odj,req.responseText);
		} else {
			destination_odj.innerHTML = 'Browser unable to create XMLHttp Object';
		}        
	} else {
		destination_odj.innerHTML = "no string";
	}
}
