// JavaScript Document

var _ajax = { handler: {}, idCounter: 0 }; // a prop handler do obj permite várias conexões  simultaneas

function makeHttpRequest(url, send_n_load, params, meth){ 
	
	this.id = ++_ajax.idCounter;
	_ajax.handler[this.id] = this; //estancio o proprio objeto para handler com um id unico
	this.meth = meth ? meth : 'GET';	
	this.url = url;	
	this.params = this.meth == 'POST' ? params : null;
	this.paramsforGet = this.meth == 'GET' ? '?'+params : '';
	this.http_request = false;
	this.oReturn = send_n_load;

	this.init = function(){ // inicializa o objeto XMLHttpRequest
		if (window.XMLHttpRequest) { // Mozilla, Safari,... 
		this.http_request = new XMLHttpRequest(); 
				   //if (this.http_request.overrideMimeType) { 
					  // this.http_request.overrideMimeType('text/xml'); 
				   //} 
		} else if (window.ActiveXObject) { // IE 
			try { 
			this.http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
			} catch (e) { 
				try { 
				this.http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
				} catch (e) {} 
			} 
		} 
	}

this.init();

   if (!this.http_request) { 
       alert('No browser support'); 
       return false; 
   } 
   
   this.http_request.onreadystatechange = new Function("_ajax.handler["+this.id+"].onChange();");   
   
   this.onChange = function() {
	// alert("here");
       if (this.http_request.readyState == 4) { 
           if (this.http_request.status == 200) {              
			  
			   new Function("_ajax.handler["+this.id+"].oReturn(_ajax.handler["+this.id+"].http_request.responseText);")();
			   //alert(this.http_request.responseText);                   
             
           } else { 
            alert('There was a problem with the request.(MCode: ' + this.http_request.status + ')'); 
			//if($GEBId("load"))$GEBId("load").style.display="none"; 
           } 
       } 
   } 
   
   
   this.http_request.open(this.meth, this.url+this.paramsforGet, true);
    if(this.meth == "POST"){
	   this.http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	this.http_request.send(this.params);
   }
} // fim classe makeHttpRequest


//intancia o objeto acima
function ajax(doc,retfunc,params,meth){
	//alert(doc+" - "+retfunc+" - "+params+" - "+meth)
myAjax = new makeHttpRequest(doc, retfunc, params, meth);
}

function validaEmail(mail){
	
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
//var filter2=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var ok = filter.test(mail);
	if(!ok) return false;
	else return true;
	
}

function trimString (str) { // NAO UTILIZADA AQUI
  return str.replace(/^s+/g, '').replace(/s+$/g, ''); 
} 

function $GEBId(id,doc) { return (doc || document).getElementById(id); }

function $GEBTn(nome,doc) { return (doc || document).getElementsByTagName(nome); }