//variaveis para a formatação individual de cada formulario
var color_ok = "#A4D1FF";
var color_error = "#FFBBBB";
var color_neutral = "#ffffff";
var img_ok = "http://localhost/emporio/imagens/form_ok.gif";
var img_error = "http://localhost/emporio/imagens/form_error.gif";

function submeterForm(){
	nome = document.f_contato.nome;
	email = document.f_contato.email;
	assunto = document.f_contato.assunto;
	telefone = document.f_contato.telefone;
	mensagem = document.f_contato.mensagem;

	flag = 0;
	if(nome.value == '' && flag == 0){
		alert('Por favor, preencha seu nome!!!');
		flag = 1;
		nome.focus();
	}
	if((!checkMail(email.value) && flag == 0) || (email.value == '' && flag == 0)){
		alert('Por favor, preencha seu email corretamente!!!');
		flag = 1;
		email.focus();
	}
	if(telefone.value == '' && flag == 0){
		alert('Por favor, preencha seu telefone!!!');
		flag = 1;
		telefone.focus();
	}else{
		if(!checkPhone(telefone.value) && flag == 0){
			alert('O campo do telefone deve conter apenas numeros');
			telefone.value = '';
			telefone.focus();
			flag = 1;
		}
	}
	if(assunto.value == '' && flag == 0){
		alert('Por favor, preencha o assunto!!!');
		flag = 1;
		assunto.focus();
	}
	texto = 'Por favor, preencha a mensagem!!!';
	if(mensagem.type == 'file'){
		texto = 'Por favor, anexe o seu currículo!!!';
	}
	if(mensagem.value == '' && flag == 0){
		alert(texto);
		flag = 1;
		mensagem.focus();
	}
	if(flag == 0){
		content = 'nome=' + nome.value + '&email=' + email.value + '&assunto=' + assunto.value + '&telefone=' + telefone.value + '&mensagem=' + mensagem.value;
		postar('conteudo/envia_form.php',content,'resultado');
		nome.value = email.value = assunto.value = telefone.value = mensagem.value = '';
	}
	return false;
}

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(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
                    return true;
                }
    }else{
        return false;
        }
}

function mascara(formulario){
	tel = eval("document." + formulario + ".telefone");
	tam = eval("document." + formulario + ".telefone.value.length");
	assunto = eval("document." + formulario + ".assunto");
	tecla = event.keyCode;
	if(tecla < 48 || tecla > 57){
		event.keyCode = 0;
	}
	if(tam == 1){
		tel.value = "(" + tel.value;
	}
	if(tam == 3){
		tel.value = tel.value + ") ";
	}
	if(tam == 9){
		tel.value = tel.value + "-";
	}
	tel.focus();
	maximo = 13;
	if(document.all){
		maximo = 13;
	}
	if(tam > maximo){
		restaura();
		if(assunto != null){
			assunto.focus();
		}else{
			tel.value = tel.value.substring(0, maximo);
		}
	}
}

function mascara2(formulario){
	tel = eval("document." + formulario + ".c_telefone");
	tam = eval("document." + formulario + ".c_telefone.value.length");
	assunto = eval("document." + formulario + ".assunto");
	tecla = event.keyCode;
	if(tecla < 48 || tecla > 57){
		event.keyCode = 0;
	}
	if(tam == 1){
		tel.value = "(" + tel.value;
	}
	if(tam == 3){
		tel.value = tel.value + ") ";
	}
	if(tam == 9){
		tel.value = tel.value + "-";
	}
	tel.focus();
	maximo = 13;
	if(document.all){
		maximo = 13;
	}
	if(tam > maximo){
		restaura();
		if(assunto != null){
			assunto.focus();
		}else{
			tel.value = tel.value.substring(0, maximo);
		}
	}
}

function limpaCampos(){
	document.getElementById('c_email').style.background = color_neutral;
	document.getElementById('c_nome').style.background = color_neutral;
	document.getElementById('c_telefone').style.background = color_neutral;
	document.getElementById('c_assunto').style.background = color_neutral;
	document.getElementById('f_email').innerHTML = '';
	document.getElementById('f_nome').innerHTML = '';
	document.getElementById('f_telefone').innerHTML = '';
	document.getElementById('f_assunto').innerHTML = '';
}
function validaEmail(obj, alvo){
	if(!checkMail(obj.value)){
		//document.getElementById(alvo).innerHTML = '<img src=\'' + img_error + '\' title=\'ok\' />';
		obj.style.background = color_error;
	}else{
		//document.getElementById(alvo).innerHTML = '<img src=\'' + img_ok + '\' title=\'ok\' />';
		obj.style.background = color_ok;
	}
}
function restaura(){
	if(document.f_contato.telefone.value.length >= 14){
		document.f_contato.telefone.value = document.f_contato.telefone.value.substring(0,14);
	}
}
function troca(obj, verificar, aplicar, novoTipo){
	if(obj.value == verificar){
		obj.value = aplicar;
	}
	if(novoTipo != false){
		document.f_sac.senha.type = 'password';
	}
}
function checkNull(obj, alvo){
	if(obj.value == '' || obj == null){
		//document.getElementById(alvo).innerHTML = '<img src=\'' + img_error + '\' title=\'ok\' />';
		obj.style.background = color_error;
	}else{
		//document.getElementById(alvo).innerHTML = '<img src=\'' + img_ok + '\' title=\'ok\' />';
		obj.style.background = color_ok;
	}
}
function validaTelefone(obj, alvo){
	if(!checkPhone(obj)){
		//document.getElementById(alvo).innerHTML = '<img src=\'' + img_error + '\' title=\'ok\' />';
		obj.style.background = color_error;
	}else{
		//document.getElementById(alvo).innerHTML = '<img src=\'' + img_ok + '\' title=\'ok\' />';
		obj.style.background = color_ok;
	}
}

function checkPhone( tel ){

	var er = new RegExp(/^\([0-9]{2}\)( ){1}[0-9]{4}\-[0-9]{4}$/);

	if(typeof(tel) == "string"){

		if(er.test(tel)){

			return true;

		}

	}else if(typeof(tel) == "object"){

		if(er.test(tel.value)){

			return true;

		}

	}else{

		return false;

	}

}

