// JavaScript Document
var id = "";
//menu
function extraiScript(texto){
    var ini, pos_src, fim, codigo;
    var objScript = null;
    ini = texto.indexOf('<script', 0)
    while (ini!=-1){
        var objScript = document.createElement("script");
        //Busca se tem algum src a partir do inicio do script
        pos_src = texto.indexOf(' src', ini)
        ini = texto.indexOf('>', ini) + 1;

        //Verifica se este e um bloco de script ou include para um arquivo de scripts
        if (pos_src < ini && pos_src >=0){//Se encontrou um "src" dentro da tag script, esta e um include de um arquivo script
            //Marca como sendo o inicio do nome do arquivo para depois do src
            ini = pos_src + 4;
            //Procura pelo ponto do nome da extencao do arquivo e marca para depois dele
            fim = texto.indexOf('.', ini)+4;
            //Pega o nome do arquivo
            codigo = texto.substring(ini,fim);
            //Elimina do nome do arquivo os caracteres que possam ter sido pegos por engano
            codigo = codigo.replace("=","").replace(" ","").replace("\"","").replace("\"","").replace("\'","").replace("\'","").replace(">","");
            // Adiciona o arquivo de script ao objeto que sera adicionado ao documento
            objScript.src = codigo;
        }else{//Se nao encontrou um "src" dentro da tag script, esta e um bloco de codigo script
            // Procura o final do script
            fim = texto.indexOf('</script>', ini);
            // Extrai apenas o script
            codigo = texto.substring(ini,fim);
            // Adiciona o bloco de script ao objeto que sera adicionado ao documento
            objScript.text = codigo;
        }

        //Adiciona o script ao documento
        document.body.appendChild(objScript);
        // Procura a proxima tag de <script
        ini = texto.indexOf('<script', fim);

        //Limpa o objeto de script
        objScript = null;
    }
}

///////////////////////////////////////////////////////////////////////////

function XMLHttpRequestChange() {
	if (HttpReq.readyState == 1) {
		var texto1 = document.getElementById(id);
		texto1.innerHTML = '<div class="arial_12_cinza">carregando<img src="img/loading.gif" border="0"  align="absmiddle"></div>';
	}

	if (HttpReq.readyState == 4 && HttpReq.status == 200){
		// coloca o valor no objeto requisitado
		texto=unescape(HttpReq.responseText.replace(/\+/g," "));
		document.getElementById(id).innerHTML=texto;
		// executa scripts
		extraiScript(texto);
   }
}


/*
function XMLHttpRequestChange() {
	if (HttpReq.readyState == 1) {
		var texto1 = document.getElementById(id);
		texto1.innerHTML = '<div class="arial_12_cinza">carregando<img src="img/loading.gif" border="0"  align="absmiddle"></div>';
	}

	if (HttpReq.readyState == 4 && HttpReq.status == 200){
      var result = HttpReq.responseText;
      var texto = document.getElementById(id);
		texto.innerHTML = result;	
   }
}*/
//////////////////////////////////////////////////////////////////

function detalhes(url, campo){
	id = campo;
	
	if (document.getElementById) { //Verifica se o Browser suporta DHTML.
		if(window.XMLHttpRequest) {
			try {
				HttpReq = new XMLHttpRequest();
			} catch(e) {
				HttpReq = false;
			}
		} else if(window.ActiveXObject) {
			try {
				HttpReq = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					HttpReq = false;
				}
			}
		}

	if(HttpReq) {
			HttpReq.onreadystatechange = XMLHttpRequestChange;
			HttpReq.open("GET", url, true);
			HttpReq.send(null);
		}		  
	}
}
//////////////////////////////////////////////////////////////////


//Enviar Email
function ajaxEmail(url, form, campo){
	id = campo;

	var nome = form.nome.value;
	if (nome == "")	{
		alert('Digite um NOME para prosseguir');
		form.nome.focus();
		return;
	}

	var email = form.email.value;
	if (email == "")	{
		alert('Digite um EMAIL para prosseguir');
		form.email.focus();
		return;
	}else{
		if(!checarEmail(email))	{
			alert('Email invalido.');
			form.email.focus();
			return;
		}
	}

	var texto = form.texto.value;
	if (texto == "")	{
		alert('Digite um COMENTÁRIO para prosseguir');
		form.texto.focus();
		return;
	}
	
	if (document.getElementById) { //Verifica se o Browser suporta DHTML.
		if(window.XMLHttpRequest) {
			try {
				HttpReq = new XMLHttpRequest();
			} catch(e) {
				HttpReq = false;
			}
		} else if(window.ActiveXObject) {
			try {
				HttpReq = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					HttpReq = false;
				}
			}
		}
		url = url+'?email='+email+'&nome='+nome+'&texto='+texto;
		if(HttpReq) {
			HttpReq.onreadystatechange = XMLHttpRequestChange;
			HttpReq.open("GET", url, true);
			HttpReq.send(null);
		}		  
	}
}

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

//Newsletter 
function ajaxNewsletter(url, form, campo){
	id = campo;
	var email = form.email.value;
	
	if (email == "") {
		alert('Digite um email para prosseguir');
		form.email.focus();
		return;
	}else{
		if(!checarEmail(email))	{
			alert('Email invalido.');
			form.email.focus();
			return;
		}
	}
	
	form.email.value = '';
	
	if (document.getElementById) { //Verifica se o Browser suporta DHTML.
		if(window.XMLHttpRequest) {
			try {
				HttpReq = new XMLHttpRequest();
			} catch(e) {
				HttpReq = false;
			}
		} else if(window.ActiveXObject) {
			try {
				HttpReq = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					HttpReq = false;
				}
			}
		}
		url = url+'?email='+email;
		if(HttpReq) {
			HttpReq.onreadystatechange = XMLHttpRequestChange;
			HttpReq.open("GET", url, true);
			HttpReq.send(null);
		}		  
	}
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

//BUSCA
function ajaxBusca(url, form, campo){
	id = campo;

	var texto = form.texto.value;
	if (texto == "")	{
		alert('Digite algo para prosseguir');
		form.texto.focus();
		return;
	}
	
	if (document.getElementById) { //Verifica se o Browser suporta DHTML.
		if(window.XMLHttpRequest) {
			try {
				HttpReq = new XMLHttpRequest();
			} catch(e) {
				HttpReq = false;
			}
		} else if(window.ActiveXObject) {
			try {
				HttpReq = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					HttpReq = false;
				}
			}
		}
		url = url+'?texto='+texto;
		if(HttpReq) {
			HttpReq.onreadystatechange = XMLHttpRequestChange;
			HttpReq.open("GET", url, true);
			HttpReq.send(null);
		}		  
	}
}

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////
//Valida EMAIL
///////////////////////////////////////////////////
function checarEmail(campo){
	email=campo;
  	carac_invalido = " /:,;";
  	if (email == ""){
    	return false;
  	}
  	for (i=0; i<carac_invalido.length; i++){
    	carac_errado = carac_invalido.charAt(i);
    	if (email.indexOf(carac_errado,0) > -1){
      	return false;

    	}
  	}

  	atPos = email.indexOf("@",1);
  	if (atPos == -1){
 		return false;
  	}

  	if (email.indexOf("@",atPos+1) != -1){
    	return false;
  	}
  	periodPos = email.indexOf(".",atPos)

  	if (periodPos == -1){
    	return false;
  	}

  	if (periodPos+3 > email.length){
	   return false;
  	}
  return true
}
