//faz o http request
var objAjax;
var funcToCallName, funcToCallArgs
var respostaErro = "Problemas no HTTP Request:\n";
function doHTTPRequest(url, f, a) {
	objAjax = false;
	funcToCallName = "";
	funcToCallArgs = "";
	funcToCallName = f; //function de resposta
	funcToCallArgs = a; //argumentos para a function
	if (window.XMLHttpRequest) { //Mozilla, Safari
		objAjax = new XMLHttpRequest();
	} else if (window.ActiveXObject) { //IE
		try {
			objAjax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				objAjax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
	if (!objAjax) {
		window.alert("Não foi possível criar uma instância do HTTP Request");
		return false;
	}
	objAjax.onreadystatechange = callFunction;
	objAjax.open("GET", url, true);
	objAjax.setRequestHeader("content-type", "text/xml");
	objAjax.send(null);
}
//executa uma function qualquer
function callFunction() {
	//argumentos: parâmetros para a função que será chamada, separados por ; se houver mais que 1
	if (funcToCallArgs!="") {
		if (funcToCallArgs.indexOf(";")==-1) { //somente 1 argumento
			if(funcToCallArgs.substr(0, 1)=="#") { //parametro é numérico; é precedido por #
				strArgs = funcToCallArgs.substr(1, (funcToCallArgs.length-1));
			} else { //parametro é string
				strArgs = "'"+ funcToCallArgs +"'";
			}
		} else { //mais de 1 argumento
			aArgsTemp = funcToCallArgs.split(";");
			strArgs = "";
			for (i=0; i<aArgsTemp.length; i++) {
				if(aArgsTemp[i].substr(0, 1)=="#") { //parametro é numérico; é precedido por #
					strArgs += aArgsTemp[i].substr(1, (aArgsTemp[i].length-1)) +", ";
				} else { //parametro é string
					strArgs += "'"+ aArgsTemp[i] +"', ";
				}
			}
			strArgs = strArgs.substr(0,(strArgs.length-2)); //tira a última vírgula
		}
	}
	strToCall = funcToCallName+".call(this, "+ strArgs +");"
	eval(strToCall)
}
//function padrão para respostas; recebe o nome do div e escreve nele a resposta do ajax
function responseReturn(strDiv) {
	if (objAjax.readyState == 4) {
		if (objAjax.status == 200) {
			writeInDiv(strDiv, objAjax.responseText);
		} else {
			alert(respostaErro+ objAjax.statusText);
		}
	}
}
//functions para escrever em divs
function writeInDiv(d, s) {
	var divName = document.getElementById(d);
	divName.innerHTML = s;
}

//function xml
function getAddress() {
	varUrl = "src/ajax.asp?action=1&cep="+ document.form_cadastro.f_cep.value;
	doHTTPRequest(varUrl, "XMLToForm", "form_cadastro;cepinfo");
	document.form_cadastro.f_endereco.value = "";
	document.form_cadastro.f_numero.value = "";
	document.form_cadastro.f_compl.value = "";
	document.form_cadastro.f_bairro.value = "";
	document.form_cadastro.f_cidade.value = "";
	document.form_cadastro.f_estado.value = "0";
	document.form_cadastro.f_numero.focus();
}
//function retorno xml
function XMLToForm(frmName, strDiv) {
	if (objAjax.readyState == 4) {
		if (objAjax.status == 200) {
			if (objAjax.getResponseHeader("Content-Type") == "text/xml") {
				var objForm = "document."+ frmName +".";
				var objXML = objAjax.responseXML;
				var objRoot = objXML.getElementsByTagName("formdata")[0];
				var op = "";
				for(i=0; i<objRoot.childNodes.length;i++) {
					objNode = objXML.getElementsByTagName(objRoot.childNodes[i].nodeName)[0];
					if(objNode.nodeName!=strDiv) {
						eval(objForm+objNode.nodeName).value = objNode.firstChild.nodeValue;
					} else {
						writeInDiv(strDiv, objNode.firstChild.nodeValue);
					}
				}
			} else if (objAjax.getResponseHeader("Content-Type") == "text/html") {
				writeInDiv(strDiv, objAjax.responseText);
			}
		} else {
			alert(respostaErro+ objAjax.statusText);
		}
	}
}
