// JS ajax microlibrary
// Programmed by Ben in 2007

function InitAjax(){
	var ajax=false;
	if(document.all){
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				ajax = false;
			}
		}
	}
	else if (!ajax && typeof XMLHttpRequest!='undefined') {
		ajax = new XMLHttpRequest();
	}
	return ajax;
}

function html_entities_decode(v){
	var o = document.createElement("textarea");
	o.innerHTML = v.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	return o.value;
}

function ajax_call(url, div_name, div_loading, binded_action){
	if(div_loading!=null){ document.getElementById(div_loading).style.display = 'inline'; }	
	if(typeof(url) == 'undefined'){ return false; }
	var ajax = InitAjax();
	ajax.open("GET", url, true);
 	ajax.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(null);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			var d = document.getElementById(div_name);
			var c = html_entities_decode(ajax.responseText);
			if(div_loading!=null){ document.getElementById(div_loading).style.display = 'none'; }
			if(document.all) { // IE QUICKFIX ONLY
				if(ajax.responseText.indexOf('<option') === 0 || ajax.responseText.indexOf('<OPTION') === 0){
					d.innerHTML = '<option>1</option>' + c;
				}
   				else{
					d.innerHTML = c;
				}
				d.outerHTML = document.getElementById(div_name).outerHTML;
			}
			else{
				d.innerHTML = c;
			}
			
			// Clean up content
			var re = null;
			var matches = null;
			var matches = new Array();
			var script = null;
			var style = null;
			var repl = null;
			
			// For each match that is found...
			matches = c.match(/<script.*?>([\s\S]*?)<\/script>/gim);
			if(matches != null){
				for(i = 0; i < matches.length; i++){
					// Remove begin/end tag
					repl = new RegExp('<script(.*?)>', 'igm');
					script = matches[i].replace(repl, '');
					repl = new RegExp('<\/script>', 'igm');
					script = script.replace(repl, '');
					
					// Insert script tags in the HEAD section (needed for IE)
					var head = document.getElementsByTagName('head').item(0);
					var s = document.createElement('script');
					s.type = 'text/javascript';
					s.text = script;
					head.appendChild(s);
				}
			}
			
			// Fix CSS style for other browsers than Firefox
			if(navigator.userAgent.indexOf("Firefox")==-1){
				// Match anything inside <style> tags
				re = new RegExp('<style.*?</style>', 'ig');
				matches = c.match(re);
				
				// For each match that is found...
				if(matches != null){
					for(i = 0; i < matches.length; i++){
						// Remove begin/end tag
						repl = new RegExp('<style.*?>', 'igm');
						style = matches[i].replace(repl, '');
						repl = new RegExp('</style>', 'igm');
						style = style.replace(repl, '');
						
						// Insert style tags in the HEAD section (needed for IE)
						var head = document.getElementsByTagName('head').item(0);
						var s = document.createElement('style');
						s.type = 'text/css';
						if(!!(window.attachEvent && !window.opera)) s.styleSheet.cssText = style; // IE
						else s.appendChild(document.createTextNode(style)); // Sarafi/Opera
						head.appendChild(s);
					}
				}
			}
			
			if(typeof(binded_action) != 'undefined'){ eval(binded_action); }
		}
	}
}

