// JavaScript Document
function fnEnviar(){
	var nome = document.getElementById("txtNome");
	var email = document.getElementById("txtEmail");
	var cidade = document.getElementById("txtCidade");
	var telefone = document.getElementById("txtTelefone");
	var assunto = document.getElementById("txtAssunto");
	var uploader = document.getElementById("dvUploader");
	
	
	
	if(0==trimAll(nome.value).length){
		window.alert("Por Favor, informe seu Nome!");
		nome.focus();
	} else if(!fnValidaEmail(email.value)){
		window.alert("Este e-mail fornecido n" + String.fromCharCode(227) + "o " + String.fromCharCode(233) + " v" + String.fromCharCode(225) + "lido!");
		email.focus();
	} else if(0==trimAll(cidade.value).length){
		window.alert("Por Favor, informe a Cidade!");
		cidade.focus();
	} else if(0==trimAll(telefone.value).length){
		window.alert("Por Favor, informe o Telefone!");
		telefone.focus();
	} else if(0==trimAll(assunto.value).length){
		window.alert("Por Favor, informe o Assunto da mensagem!");
		assunto.focus();
	} else {
	
	uploader.style.visibility="visible";
	
		var req = null; 
	
		if (window.XMLHttpRequest){
		
			req = new XMLHttpRequest();
			if (req.overrideMimeType){
				req.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject){
		
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e){
			
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
	
		req.onreadystatechange = function() { 
		
			if(req.readyState == 4)	{
				
				if(req.status == 200) {
					
					var doc = req.responseText;
					//window.alert(doc);
					
					if("ok"==doc){
						window.alert("Contato enviado com sucesso!");
					} else {
						window.alert("Desculpe! Não foi possível o envio da Mensagem!\nTente novamente mais tarde! Obrigado!");	
					}
					off();
				}
			} 
		};
		
		req.open("POST", "contato_sender.php", true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.send("&nome=" + nome.value + "&email=" + email.value + "&cidade=" + cidade.value + "&telefone=" + telefone.value + "&assunto=" + assunto.value + "&");	
		nome.value="";
		email.value="";
		cidade.value="";
		telefone.value="";
		assunto.value="";

	}
	
}

//Validando Emails
function fnValidaEmail(emailad){
	var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;
	if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1)){
		return false;
	}
	else {
		return true;
	}
}

function off()
{
	var uploader = document.getElementById("dvUploader");
	uploader.style.visibility="hidden";
}

//elimina espaços em branco no inicio e no final da string
function trimAll(sString){
	var sString = new String(sString);
	while (sString.substring(0,1) == " "){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == " "){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}