function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
}
window.onload = externalLinks;


function tryAjax(){
	try{
		xmlhttp = new XMLHttpRequest();
	}
	catch(e){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(exc){
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

function callAjax(divId, url){
	xmlhttp = tryAjax();
	url = "includes/inc_"+url+".php";
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState < 4){
			document.getElementById(divId).innerHTML = '<span style="font-weight: bold; background-color: #000; color: #FFF;">Carregando...</span>';
		}
		if(xmlhttp.readyState == 4){
			document.getElementById(divId).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

function linkAjax(divId, url){
	xmlhttp = tryAjax();

	form = document.getElementById('formCalculo');
	form_get = get(form);

	url = "includes/"+url+".php";
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState < 4){
			document.getElementById(divId).innerHTML = '<span style="font-weight: bold; background-color: #000; color: #FFF;">Submetendo dados...</span>';
		}
		if(xmlhttp.readyState == 4){
			document.getElementById(divId).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(form_get);
}

function get(obj){
	// come from: http://www.captain.at/howto-ajax-form-post-get.php
	var getstr = "";
	var strdecode = "";
	for (i=0; i<obj.childNodes.length; i++) {
		if (obj.childNodes[i].tagName == "INPUT") {
			if (obj.childNodes[i].type == "text") {
				strdecode = obj.childNodes[i].value.replace(/:/g , "%3A");
				strdecode = strdecode.replace(/\//g, "%2F");
				strdecode = strdecode.replace(/\?/g , "%3F");
				strdecode = strdecode.replace(/=/g , "%3D");
				strdecode = strdecode.replace(/&/g , "%26");
				strdecode = strdecode.replace(/\+/g , "%2B");
				getstr += obj.childNodes[i].name + "=" + strdecode + "&";
			}
			if (obj.childNodes[i].type == "hidden") {
				getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
			}
		}
	}
	return getstr;
}