function  createRequestObject() //Em time que ganha não se mexe.

{

    var ret = null;

    try {

        ret = new ActiveXObject('Msxml2.XMLHTTP');

    }

    catch (e) {

        try {

            ret = new ActiveXObject('Microsoft.XMLHTTP');

        }

        catch (ee) {

            ret = null;

        }

    }

    if (!ret && typeof XMLHttpRequest != 'undefined')

        ret = new XMLHttpRequest();



    return ret;

}

   // Make the XMLHttpRequest object
   var http = createRequestObject();
   
function NelAjax(idForm, idTarget, phpPage){
	this.idForm = idForm;
	this.idTarget = idTarget;
	this.phpPage = phpPage;
	this.dados = new Array();
	//alert(this.idForm + " " + this.targetId + " " + this.phpPage);

}
NelAjax.prototype.enviarEmail = function() {
	
	var form = document.getElementById(this.idForm);
	allInputs = form.getElementsByTagName('input');
	allSelects = form.getElementsByTagName('select');
	allTextareas = form.getElementsByTagName('textarea');
	function collectionToArray(col) {
		a = new Array();
		for (i = 0; i < col.length; i++)
		{
			a[a.length] = col[i];
		}
		return a;
	}
	
	(allInputs.length > 0) ? this.dados = this.dados.concat(collectionToArray(allInputs)):this.dados = this.dados;
	(allSelects.length > 0) ? this.dados = this.dados.concat(collectionToArray(allSelects)):this.dados = this.dados;
	(allTextareas.length > 0) ? this.dados = this.dados.concat(collectionToArray(allTextareas)):this.dados = this.dados;

	http.open('post', this.phpPage);
	var id = this.idTarget;
	//alert(id);
	http.onreadystatechange = function() {
			//alert(this.idTarget);
			if(http.readyState == 4 && http.status == 200){
			//alert(id);
			handleResponse(id);
			}
		}
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	var dado = '';
	for (var i = 0; i < this.dados.length; i++) {
		if (i > 0) {
			//alert (dado);
			dado = dado + '&' + this.dados[i].name + '=' + this.dados[i].value;
		} else {
			dado = this.dados[i].name + '=' + this.dados[i].value;
		}
	}
	//alert(this.idTarget);
	http.send(dado);
	
	function handleResponse(id) {
		//alert(id);
		document.getElementById(id).innerHTML = "<p style=\"text-align: center;\"><img style=\"margin: 100px auto;\" src=\"/novo/estilos/img\"ajax-" + id + ".gif\" alt=\"Carregando...\" /></p>";
	   //alert(http.readyState);
	   if(http.readyState == 4 && http.status == 200){
		  //Text returned from PHP script
		  //alert(http.readyState);
		  var response = http.responseText;
		  if(response) {
			  //alert(response);
		  //Update ajaxTest content
			 document.getElementById(id).innerHTML = response;
		  }
		  else {  document.getElementById(id).innerHTML = "Bad bad server. No donut for you." ;
		  }
	   }
	
	}
}	


NelAjax.prototype.preload  = function(imageSrc) {
	var Aberto = new Image();
	Aberto.src = imageSrc;	
}