var ajax = new function(){
	var getRO = function(){return (navigator.appName == "Microsoft Internet Explorer") ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();}
	var get = function(url,handler) {
		var http = getRO();
		var anchor;
		if(match = url.match(/#(.*)$/)){
			anchor = match[1];
			url = url.replace(match[0],'');
		}		
		http.open('get', url, true);
		if(typeof(handler) == "function"){
			http.onreadystatechange = function(){if(http.readyState == 4 && http.status == 200){ handler(http.responseText); }};
		} else{
			el = typeof(handler) == "string" ? document.getElementById(handler) : handler;
			if(el.clientHeight>100)el.style.height=el.clientHeight+'px';
			el.innerHTML = '';
			var loading = document.createElement('DIV');
			loading.className = "loading";
			el.appendChild(loading);
			
			// IF THE TOP OF THE HANDLER IS OUT OF VIEW THEN SCROLL TO IT SO CLIENT CAN SEE MESSAGE
			var pos = absPos(el);
			if(pos.y < document.body.parentNode.scrollTop || pos.y > (document.body.parentNode.scrollTop+document.body.parentNode.clientHeight)){
				document.body.parentNode.scrollTop = pos.y-40;
			}
			http.onreadystatechange = function(){ if(http.readyState == 4 && http.status == 200){
				el.innerHTML = http.responseText;
				el.style.height='';
				// SCROLL TO ANCHOR IF IT IS SPECIFIED
				if(anchor) pos = absPos(document.anchors[anchor]);
				loadScripts(http.responseText);
				if(typeof activatePlaceholders == "function") activatePlaceholders(el);
			} }
		}
		http.send(null);
		return false;
	}
	var post = function(url,handler,data,after_function){
		var form = typeof(data) == "string" ? document.getElementById(data) : data;
		if(form && form.nodeName == "FORM"){
			var data = '';
			var item;
			var option;
			for(var i=0;item = form.elements[i];i++){
				if(item.type == "select-multiple"){
					for(var p = 0;option = item.options[p]; p++) if(option.selected == true) data += item.name+"[]="+option.value.replace("&","%26")+"&";
				} else if(item.type){
					data += ((item.type != "checkbox" && item.type != "radio") || item.checked == true) ? item.name+"="+item.value.replace("&","%26")+"&" : "";
				}
			}
			data = data.substr(0,(data.length - 1));
		} else if(typeof(data) == "object" && data.length && data.join){
			data = data.join('&');
		}
		//if(event.target.type == "button" || event.target.type == "submit") event.target.disabled = true;
		
		var http = getRO();
		var anchor;
		if(match = url.match(/#(.*)$/)){
			anchor = match[1];
			url = url.replace(match[0],'');
		}	
		http.open('post', url);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
		//http.setRequestHeader("Content-length", data.length);
		//http.setRequestHeader("Connection", "close");
		if(typeof(handler) == "function"){
			http.onreadystatechange = function(){if(http.readyState == 4 && http.status == 200){ handler(http.responseText); }};
		} else if(handler){
			el = typeof(handler) == "string" ? document.getElementById(handler) : handler;
			if(el.clientHeight>100)el.style.height=el.clientHeight+'px';
			el.innerHTML = '';
			var loading = document.createElement('DIV');
			loading.className = "loading";
			el.appendChild(loading);
			
			// IF THE TOP OF THE HANDLER IS OUT OF VIEW THEN SCROLL TO IT SO CLIENT CAN SEE MESSAGE
			var pos = absPos(el);
			if(pos.y < document.body.parentNode.scrollTop || pos.y > (document.body.parentNode.scrollTop+document.body.parentNode.clientHeight)){
				document.body.parentNode.scrollTop = pos.y-40;
			}
			
			http.onreadystatechange = function(){ if(http.readyState == 4 && http.status == 200){
				el.innerHTML = http.responseText;
				el.style.height='';
				pos = (anchor) ? absPos(document.anchors[anchor]) : false;
				if(form && form.id){ // IF THE FORM IS PRESENT AGAIN ON THE NEW PAGE THEN SCROLL TO IT..
					//for(var i=0; i<document.forms.length; i++)
						//if(document.forms[i].id == form.id)  pos = absPos(document.forms[i]);
				}
				if(pos) document.body.parentNode.scrollTop = pos.y-40;
				loadScripts(http.responseText);
				if(typeof activatePlaceholders == "function") activatePlaceholders(el);
			} }
		}
		http.send(data);
		//return false;
	}
	this.get = get;
	this.post = post;
}
function loadScripts(string,start){
	if(!start) start = 0;
	var src;
	while(true){
		start = string.indexOf('<script',start);
		if(start != -1){
			tag_end = string.indexOf('>',start);	
			if(end = string.indexOf('</script>',tag_end)){
				var script_string = string.substr(start,tag_end-start);
				if(end-start > 1){
					var e = newEl("script",{'type':'text/javascript'});
					if(src = /src=['"](.+s)['"]/.exec(script_string)){
						e.src = src = src[1];
						var scripts = document.getElementsByTagName("head")[0].getElementsByTagName("script");
						for(var i=0,script; script=scripts[i]; i++){
							if(script.src == e.src){ // THAT SCRIPT IS ALREADY LOADED..
								return loadScripts(string,end);
							}
						}
						document.getElementsByTagName("head")[0].appendChild(e);
						e.onreadystatechange = function(){loadScripts(string,end);}
						e.onload = function(){loadScripts(string,end);}
						return;
					} else{
						if(document.all){
							e.text = string.substr(tag_end+1,end-tag_end-1);
						}else{
							e.innerHTML = string.substr(tag_end+1,end-tag_end-1);
						}
						document.getElementsByTagName("head")[0].appendChild(e);
					}
				}
			}
			start = end;
		} else{
			break;
		}
	}
	if(document.createEvent){
		var evt = document.createEvent('HTMLEvents');		
		evt.initEvent('DOMContentLoaded',false,false,document,1);
		document.dispatchEvent(evt);
	} else{
		//window.fireEvent('domready');
	};
}
