// 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_prepare_post_fields(arr_obj_field_ids){
	var uri_encoded_post_params = '';
	if(arr_obj_field_ids.length > 0){
		for(var i in arr_obj_field_ids){
			var o = document.getElementById(arr_obj_field_ids[i]);
			if(typeof(o) != 'undefined' && o != null){
				uri_encoded_post_params += (uri_encoded_post_params != '' ? '&' : '')+arr_obj_field_ids[i]+'='+encodeURI(o.value);
			}
			else{
				alert('Error: Field not found ['+arr_obj_field_ids[i]+']');
			}
		}
	}
	return uri_encoded_post_params;
}

function ajax_call(url, div_name, div_loading, binded_action, str_uri_encoded_post_params){
	if(url == null){ return false; }
	if(div_loading != null){ document.getElementById(div_loading).style.display = 'inline'; }	
	str_post_params = (typeof(str_uri_encoded_post_params) == 'undefined' ? '' : str_uri_encoded_post_params);
	var ajax = InitAjax();
	ajax.open('POST', 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.setRequestHeader("Content-length", str_post_params.length);
	ajax.setRequestHeader("Connection", "close");
	ajax.send(str_post_params);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4) {
			if(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);
						}
					}
				}
			}
			else if(ajax.status != 0){
				alert('Error: Communication status ['+ajax.status+':'+url+']');
			}
			
			if(typeof(binded_action) != 'undefined'){ eval(binded_action); }
		}
	}
}

