//================================== AJAX =========================================
// global flag
var isIE = false;

// global request and XML document objects
var req;

var tipo;

// controla o tipo de envio da requisição
var tipo_envio = "GET";

// contém o formulário para envio (usado para o método post)
var form_envio;


function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
	   req = new XMLHttpRequest();
	   req.onreadystatechange = processReqChange;
	   req.open(tipo_envio, url, true);
	   if (tipo_envio == "POST") {
	   	   req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-1');
	   	   req.send(get(form_envio));
	   } else
	   		req.send(null);
	   // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
			 isIE = true;
			 req = new ActiveXObject("Microsoft.XMLHTTP");
			 if (req) {
				req.onreadystatechange = processReqChange;
				var form_string = get(form_envio);
				req.open(tipo_envio, url, true);
				if (tipo_envio == "POST") {
	   	   			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-1');
	   	   			req.send(get(form_envio));
	   	   		} else 
	   	   			req.send();
			 }
		  }
}

function get(obj) {
  if (obj) {
	  // come from: http://www.captain.at/howto-ajax-form-post-get.php
	  var getstr = "";
	  for (i=0; i<obj.elements.length; i++) {
		   campo = obj.elements[i];
		   switch (campo.type) {
		   		case "textarea":
				case "password":			
				case "text":  getstr += campo.name + "=" + url_encode(campo.value); break;
	
				case "radio": if (campo.checked)
					              getstr += campo.name + "=" + url_encode(campo.value);
							  break;
	
				case "checkbox": if (campo.checked)
									 getstr += campo.name + "=" + url_encode(campo.value);
									 else
										getstr += campo.name + "=";
								 break;
	
				case "select-one": getstr += campo.name + "=" + url_encode(campo.value);
								   break;
		   }
		   getstr += "&";
	  }
	
	  getstr =  getstr.substr(0, getstr.length-1);
	  return getstr;
  }
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {

	   // only if "OK"
	   if (req.status == 200) {
	   	
	   	   // chama a função de retorno	
		   eval(tipo);
		 
	   } else {
		  alert("There was a problem retrieving the XML data:\n" +		req.statusText);
		}
    }
}

function url_encode(str) { 
	var hex_chars = "0123456789ABCDEF"; 
	var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
	var n, strCode, hex1, hex2, strEncode = ""; 

	for(n = 0; n < str.length; n++) { 
		if (noEncode.test(str.charAt(n))) { 
			strEncode += str.charAt(n); 
		} else { 
			strCode = str.charCodeAt(n); 
			hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
			hex2 = hex_chars.charAt(strCode % 16); 
			strEncode += "%" + (hex1 + hex2); 
		} 
	} 
	return strEncode; 
	//return str; 
}  
//===========================================================================