destino = "";

function getXmlHttpRequest () {
	var xmlHttpRequest = null;
	try {
		xmlHttpRequest = new XMLHttpRequest();
	}catch (e) {
		try {
			xmlHttpRequest = new ActiveXObject ("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttpRequest = new ActiveXObject ("Microsoft.XMLHTTP");
		}
	}
	return xmlHttpRequest;
}

function ajaxFunction()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  //alert("firefox");
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  return xmlHttp;
  }



function stateChanged () {
	if(xmlHttp.readyState == 1) {
		document.getElementById(destino).innerHTML = "<center><br /><br /><br /><img src='img/galeria/loading.gif' /></center>";
	}if(xmlHttp.readyState == 2) {
		document.getElementById(destino).innerHTML = "<center><br /><br /><br /><img src='img/galeria/loading.gif' /></center>";
	}if(xmlHttp.readyState == 3) {
		document.getElementById(destino).innerHTML = "<center><br /><br /><br /><img src='img/galeria/loading.gif' /></center>";
	}if(xmlHttp.readyState == 4) {
		document.getElementById(destino).innerHTML = xmlHttp.responseText;
	}
}
/*******************************
parmetros: 
source = de onde ele recebe os dados 
ex:base.php
param  = o filtro das buscas         
ex:?query=param  
dest   = onde a funo vai jogar o retorno o detalhe  que esse campo tem que ser "string"
ex:div tal
*********************************/
function sAjax(source,param,dest) {	
	destino = dest;
	
	xmlHttp = ajaxFunction();
	if(xmlHttp == null) {
		alert("seu browser no suporta AJAX");
		return;
	}	

	source = source + param;
	xmlHttp.onreadystatechange = stateChanged;
	
	//xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.open("GET",source,true);
	xmlHttp.send(null);
}


function ajaxTeste()
{
var xmlHttp;
try
  {
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  return xmlHttp;
  }


function ajaxEmail(div, idNome, idEmail, idMsg)
{
	nome  = document.getElementById(idNome);
	email = document.getElementById(idEmail);
	msg   = document.getElementById(idMsg);
	
	ajax = ajaxTeste();
	var url = "mail.php?";
		url += "nome=" + nome.value;
		url += "&email=" + email.value;
		url += "&mensagem=" + msg.value;
	
	ajax.onreadystatechange = function(){
		if(ajax.readyState == 4){
			nome.value = "";
			email.value = "";
			msg.value = "";
			var retorno = "";
			
			if(ajax.responseText == "1"){
				retorno = "<p><img src='img/certo.gif' align='absmiddle' style='margin:0 5px;' />Recado enviado com sucesso!</p>";
			}else{
				retorno = "<p><img src='img/errotorre.gif' align='absmiddle' style='margin:0 5px;' /> Falha ao enviar sua mensagem!</p>";				
			}
			document.getElementById("resposta").innerHTML = "";
			document.getElementById("resposta").innerHTML = retorno;
		}else{
			document.getElementById("resposta").innerHTML = "";
			document.getElementById("resposta").innerHTML = "<p style='padding:0;margin:0;'><b>Enviando<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='15' height='14' align='absbottom'><param name='movie' value='flash/pontinhos.swf'/><param name='quality' value='high'/><param name='wmode' value='transparent'/><embed src='flash/pontinhos.swf' width='15' height='14' align='absbottom' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' wmode='transparent'></embed></object></b><\p>";
		
			}
			
	}
	
	ajax.open("POST", url, true);
	ajax.send(null);		  

}

function checkMail(mail){
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(er.test(mail)){
		return true;
    }else{        
		return false;
    }
}

function validaFormulario(div , idNome, idEmail, idMsg){
	nome  = document.getElementById(idNome);
	email = document.getElementById(idEmail);
	msg   = document.getElementById(idMsg);
	var erro = "";
	
	if(nome.value.length <= 0){
		erro = "Nome inválido!";
		nome.focus();
	}else if(!checkMail(email.value)){
		erro = "E-mail inválido!";
		email.focus();
	}else if(msg.value.length <= 0){
		erro = "Mensagem inválida!";
		msg.focus();
	}
	if(erro.length > 0){
		document.getElementById("resposta").innerHTML = "<p style='padding:0;margin:0;color:#fff;'> <img src='img/errotorre.gif' align='absmiddle' style='margin:0 5px;' />" + erro + "<\p>";
		
		return;
	}else{
		ajaxEmail(div, idNome, idEmail, idMsg);
	}
}

