/*
ultima modificação: 21 de Janeiro de 2009
*/

this.$ = function(obj){
	try{
		return document.getElementById(obj);
	}catch(e){
		alert(e.message);
		return null;
	}
}


function Ajax(){
	this.assincr = false;
	this.method = "POST";
	this.val = "";
	this.xmlhttp = null;
	
	try{
		this.xmlhttp =  new ActiveXObject("Microsoft.XMLHTTP");
	}catch(e){
		try{
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(ex){
			try{
				this.xmlhttp = new XMLHttpRequest();
			}catch(exc){
				alert("Esse browser não tem recursos para uso do Ajax");
				this.xmlhttp = null;
			}
		}
	}
	
	this.urlRand = function(uri){
		var dt = new Date();
		if(uri.indexOf("?")>=0){
			return uri+"&"+encodeURI(Math.random()+"_"+dt.getTime());
		}else{
			return uri+"?"+encodeURI(Math.random()+"_"+dt.getTime());
		}
	}
	
	
	//executa os script que estiverem dentro do text
	this.loadScripts = function(content){
		var start, src_pos, end, source;  
		var objScript = null;  
		start = content.indexOf('<script', 0)  
		while (start!=-1){  
			var objScript = document.createElement("script");  
			src_pos = content.indexOf(' src', start)  
			start = content.indexOf('>', start) + 1;  
			if (src_pos < start && src_pos >=0){
				start = src_pos + 4;  
				end = content.indexOf('.', start)+4;  
				source = content.substring(start,end);  
				source = source.replace("=","").replace(" ","").replace("\"","").replace("\"","").replace("\'","").replace("\'","").replace(">","");  
				objScript.src = source;  
			}else{
				end = content.indexOf('</script>', start);  
				source = content.substring(start,end);  
				objScript.text = source;  
			}  
			document.body.appendChild(objScript);  
			start = content.indexOf('<script', end);  
			objScript = null;  
		}  
	}


	//retorna o conteudo de uma ajax
	this.loadResult = function(url){
		if(this.xmlhttp) {
			this.xmlhttp.open(this.method, this.urlRand(url) , this.assincr);
			//headers, vulnerável
			this.xmlhttp.setRequestHeader("Cache-Control", "no-cache");
        	this.xmlhttp.setRequestHeader("Pragma", "no-cache");
			//
			if(this.method == 'GET'){
				this.xmlhttp.send(null);
			}else if(this.method == 'POST'){
				this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
				try{
					this.xmlhttp.send(url.split("?")[1]);
				}catch(e){}
			}
			//
			if(this.assincr){
				this.xmlhttp.onreadystatechange = function(){
					if(ajax.xmlhttp.readyState == 4){
						if(ajax.xmlhttp.status == 200){
							//
							if(ajax.val == 'desconectado'){
								alert('Você foi desconectado e será redirecionado em instantes...!');
								parent.location = '';
							}
							//
							ajax.val = ajax.xmlhttp.responseText;
						}else{
							alert(ajax.xmlhttp.statusText);
						}
					}
				}
			}else{
				ajax.val = ajax.xmlhttp.responseText;
			}
			//
			ajax.loadScripts(ajax.val);
			//jsInit();
		}
		return this.val;
	}
	
	//carrega uma requisição dentro de um div
	this.loadContent = function(url , div_name){
		try{
			document.getElementById(div_name).innerHTML = ajax.loadResult(url);
		}catch(e){
			alert(e.message);
		}
		//jsInit();
	}
	
	//carrega uma página sem ajax e sem reload
	this.simpleLoad = function(url){
		try{
			var js = document.createElement("script");
			js.setAttribute('type' , 'text/javascript');
			js.setAttribute('src' , url);
			document.body.appendChild(js);
		}catch(e){
			alert('Impossível carregar o script solicitado!')
		}
	}
	
}

var ajax = new Ajax();
ajax.assincr = false;
ajax.method = "POST";