//<!--

var req;
var alvo;
var carregando = '';

function loadXMLDoc(url){
	req = null;

	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange1;
		req.open("GET", url, true);
		req.send('');
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP.4.0");
		} catch(e) {
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP.3.0");
			} catch(e) {
				try {
					req = new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e) {
					try {
						req = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
						req = false;
					}
				}
			}
		}
		if (req) {
			req.onreadystatechange = processReqChange1;
			req.open("GET", url, true);
			req.send('');
		}
	}
}

function processReqChange1(){ 
	if (req.readyState == 4) {
		if (req.status == 200) {
			texto = req.responseText.replace(/\+/g," ");
			
			extraiScript(texto);

			document.getElementById(alvo).innerHTML = req.responseText;
		} else {
			alert("Houve um problema ao atualizar os dados:\n" + req.statusText);
		}
	} else {
		document.getElementById(alvo).innerHTML = carregando;
	}
}

function extraiScript(texto){
	/*
	 * Autor: SkyWalker.TO do imasters/forum 
	 * http://forum.imasters.com.br/index.php?showtopic=165277
	 * 
	 * Permite a esecução de um script/função como retorno do HttpRequest/Ajax
	 */
    // inicializa o inicio ><
    var ini = 0;
    // loop enquanto achar um script
    while (ini!=-1){
        // procura uma tag de script
        ini = texto.indexOf('<script', ini);
        // se encontrar
        if (ini >=0){
            // define o inicio para depois do fechamento dessa tag
            ini = texto.indexOf('>', ini) + 1;
            // procura o final do script
            var fim = texto.indexOf('</script>', ini);
            // extrai apenas o script
            codigo = texto.substring(ini,fim);
            // executa o script
            //eval(codigo);
            /**********************
            * Alterado por Micox - micoxjcg@yahoo.com.br
            * Alterei pois com o eval não executava funções.
            ***********************/
            novo = document.createElement("script")
            novo.text = codigo;
            document.body.appendChild(novo);
        }
    }
}

function atualiza(url, target){
	alvo = target;
	
	loadXMLDoc(url);
}

function validaForm(id){ // Validação dos formulários
	switch (id)	{
		case 1: // CONTATO
			d = document.formContato;
			
			if (d.nome.value=="" || d.telefone.value=="" || d.email.value=="" || d.assunto.value=="" || d.mensagem.value=="") {
				msgErro(1);
				return false;
			}
			
			return true;
			break;
	}
}

function msgErro(tipo){
	switch (tipo) {
		case 1: // CONTATO
			msg = "Para enviar sua mensagem, preencha todos os campos do formulário. Obrigado.";
			break;
	}
	alert (msg);
}

//-->
