try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

/* função de ajax para subscrever o e-mail na newsletter */
function Subscribe(){
	formulario = document.getElementById('newsletter').innerHTML;
    nome = document.getElementById('nome').value;
    sobrenome = document.getElementById('sobrenome').value;
    email = document.getElementById('email').value;
	document.getElementById('newsletter').innerHTML = "<p>Aguarde...</p>";
    
	xmlhttp.open("GET", "index.php5?m=ajax&o=Newsletter&email="+email+"&nome="+nome+"&sobrenome="+sobrenome,true);
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4){
	        retorno = xmlhttp.responseText
	        //Desfaz o urlencode
            //retorno=retorno.replace(/\+/g," ");
            retorno=unescape(retorno)
            if (retorno=='sucess'){
            	msg = 'Seu e-mail foi cadastrado com sucesso!';
            }else{
            	msg = 'Houve um problema no cadastro de seu e-mail, tente novamente mais tarde!';
            }
            document.getElementById('newsletter').innerHTML = "<p>"+msg+"</p>"
	    }
	}
	xmlhttp.send(null)
}
/* função de ajax para subscrever o e-mail na newsletter */
function enviarContato(){
	formulario = escape(document.getElementById('contato').innerHTML);
    nome = escape(document.getElementById('txtnome').value);
    empresa = escape(document.getElementById('txtempresa').value);
    ddd = escape(document.getElementById('txtddd').value);
	telefone = escape(document.getElementById('txttelefone').value);
    email = escape(document.getElementById('txtemail').value);
    inputs = document.getElementsByTagName('input');
    assunto = '';
    for (i=0;i<inputs.length;i++){
    	if (inputs[i].checked == true){
    		assunto = escape(inputs[i].value);
    	}
    }

    if (assunto == 'outro')
    	assunto = escape(document.getElementById('tpcontato_outro').value);
    	
    mensagem = escape(document.getElementById('txtmensagem').value);
	
    //document.getElementById('contato').innerHTML = "<p>Aguarde...</p>";
    parametros = "?m=ajax&o=Contato&email="+email+"&nome="+nome+"&empresa="+empresa+"&ddd="+ddd+"&telefone="+telefone+"&assunto="+assunto+"&mensagem="+mensagem;
	xmlhttp.open("GET", "index.php5"+parametros,true);
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4){
	        retorno = xmlhttp.responseText
	        //Desfaz o urlencode
            //retorno=retorno.replace(/\+/g," ");
            retorno=unescape(retorno)
			msg = formulario;
            switch (retorno){
            	case'ok':
            	document.getElementById('contato').innerHTML = '<h1 class="internas">Obrigado</h2><h2>Agradecemos o seu contato.</h2>';
            	//<p><input class="botao" type="button" value="Voltar" name="" title="Voltar" onclick="window.location.href=\'index.php5\';"/></p>';
            	break;
	            case 'email':
	            alert('Você precisa fornecer seu e-mail.');
	            break;
	            case 'nome':
	            alert('Você precisa fornecer seu nome');
	            break;
	            case 'ddd':
	            alert('Você precisa fornecer seu ddd.');
	            break;
	            case 'telefone':
	            alert('Você precisa fornecer seu telefone.');
	            break;
	            default:
            	alert('Houve um problema no envio dos dados, tente novamente mais tarde!');
            	break;
            }
	    }
	}
	xmlhttp.send(null)
}

/* função para pegar categorias */
function getSubCategoria(sub,cat){
    document.getElementById(sub).options.length = 0;
	xmlhttp.open("GET", "index.php5?m=ajax&o=Categorias&cat="+cat,true);
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4){
	        retorno = xmlhttp.responseText
	        //Desfaz o urlencode
            retorno=retorno.replace(/\+/g, " ");
            retorno=unescape(retorno)
            linhas = retorno.split(';');
            document.getElementById(sub).options.length = 0;
            for(i=0;i<linhas.length-1;i++){
            	linhas[i]=(linhas[i].split(':'))
            	newOp=document.createElement('OPTION');
				newOp.value=linhas[i][0];
				newOp.text=linhas[i][1];
            	document.getElementById(sub).options.add(newOp,i);
   				newOp = null;
            }
	    }
	}
	xmlhttp.send(null)
}

function SendForFriend(){
	formulario = document.getElementById('FaleComAmigos').style.display = 'none';
	document.getElementById('FaleComAmigosStatus').innerHTML = "<p>Aguarde...</p>";
	url = window.location.href.replace(/\?/g, ";;");
	url = url.replace(/\&/g, "::");
	nomeFrom = document.getElementById('nome').value;
    emailFrom = document.getElementById('email').value;
    nomeTo = document.getElementById('nomeamigo').value;
    emailTo = document.getElementById('emailamigo').value;
    
	xmlhttp.open("GET", "index.php5?m=ajax&o=FaleComAmigos&emailFrom="+emailFrom+"&nomeFrom="+nomeFrom+"&nomeTo="+nomeTo+"&emailTo="+emailTo+"&u="+url,true);
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4){
	        retorno = xmlhttp.responseText
            retorno=unescape(retorno)
            
            if (retorno=='ok'){
            	msg = 'Em breve seu amigo receberá uma indicação.';
            }else{
            	msg = 'Houve um problema no envio do e-mail.';
            }
            document.getElementById('FaleComAmigosStatus').innerHTML = "<h5 class='red'>"+msg+"</h5><input class=\"botao\" type=\"button\" value=\"Enviar outro\" name=\"EnviarMais\" id=\"EnviarMais\" title=\"\" onclick=\"sendForMoreFriend();\" />";
	    }
	}
	xmlhttp.send(null)
}

function sendForMoreFriend(){
	document.getElementById('nome').value = 'Seu nome';
    document.getElementById('email').value = 'Seu e-mail';
    document.getElementById('nomeamigo').value = 'Nome do amigo';
    document.getElementById('emailamigo').value = 'E-mail do amigo';
    
	document.getElementById('FaleComAmigos').style.display='';
	document.getElementById('FaleComAmigosStatus').innerHTML = '';
}

function limpaCampo(id,value){
	if (document.getElementById(id).value == value){
		document.getElementById(id).value='';
	}
	else{
		document.getElementById(id).select();
	}
}

function retornaCampo(id,value){
	if (document.getElementById(id).value==''){
		document.getElementById(id).value=value;
	}
}
