// JavaScript Document

function fnEnviarCI(){
	var ci_codigo = document.getElementById("ci_codigo");
	var nome = document.getElementById("ci_nome");
	var email = document.getElementById("ci_email");
	var telefone = document.getElementById("ci_telefone");
	var assunto = document.getElementById("ci_assunto");
	var mensagem = document.getElementById("ci_mensagem");
	var uploader = document.getElementById("uploader");
	var fundo = document.getElementById("overlay_ci");
	
	
	
	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(telefone.value).length){
		window.alert("Por Favor, informe o Telefone da mensagem!");
		telefone.focus();
	} else if(0==trimAll(assunto.value).length){
		window.alert("Por Favor, informe o Assunto da mensagem!");
		assunto.focus();
	} else if(0==trimAll(mensagem.value).length){
		window.alert("Por Favor, informe a mensagem!");
		mensagem.focus();
	} else {
	
	uploader.style.visibility="visible";
	fundo.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_imediato_sender.php", true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.send("&ci_codigo=" + ci_codigo.value + "&nome=" + nome.value + "&email=" + email.value + "&telefone=" + telefone.value + "&assunto=" + assunto.value + "&mensagem=" + mensagem.value + "&");	
		nome.value="";
		email.value="";
		telefone.value="";
		assunto.value="";
		mensagem.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("uploader");
	var fundo = document.getElementById("overlay_ci");
	
	uploader.style.visibility="hidden";
	fundo.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;
}
