var to_normal = 30 * (1000); // Variavel timeout das funcoes ajax (padrao)
var to_retorno = 5 * (1000); // Variavel tempo exibindo mensagem de retorno

// Retorna se valor = vazio / nulo
function IsEmpty(s) {
  return ((s == null) || (s.length == 0) || (s == '') || typeof(s) == 'undefined')
}
// Verifica formato de E-mail
function vEmail(email) {
	var BadChars = 'áàâãäêéèëíìïîóòôõöúùûüçÿý*´`$#!\'"¨&(){}?/\^~:;,= ';
	var GoodChars = '@.', i;
	var posarroba = email.indexOf ('@', 0);
	if (email.length < 6) { return true; }
	for (i = 0; i < email.length; i++) { if (BadChars.indexOf(email.charAt(i)) != -1) { return true; } }
	for (i = 0; i < GoodChars.length; i++) {
		if (email.indexOf(GoodChars.charAt(i)) == -1) return true;
		if (email.indexOf(GoodChars.charAt(i), 0) == 0) return true;
		if (email.lastIndexOf(GoodChars.charAt(i)) > email.length - 3) return true;
	}
	if (email.lastIndexOf('@') > email.lastIndexOf('.')) return true;
	if (email.indexOf ('@.', 0) != -1 || email.indexOf ('.@', 0) != -1) return true;
	if (email.indexOf ('@', posarroba + 1) != -1) return true;
	return false;
}
// Funcao para controlar loading
function fLoading(tp) {
  if (tp == 'show') {
    $('#status_loading:ui-dialog').dialog('destroy');
    $('#status_loading').dialog({
			height: 120,
      resizable: false,
      title: 'Enviando mensagem',
			modal: true
    });
    $('#status_loading').html('<p><img src="images/loading.gif" alt="" align="absmiddle" /> Enviando contato...</p><br /><p align="center"><b>Por favor, aguarde a conclus\u00e7\u00e3o do processo</b>.</p>');
  } else if (tp == 'hide') {
    setTimeout(function() {
      $('#status_loading').dialog('close');
    }, to_retorno);
  }
}
// Definicoes ajax
$.ajaxSetup({
  type: 'POST',
  contentType: 'application/x-www-form-urlencoded; charset=utf-8',
  cache: false,
  dataType: 'html',
  timeout: to_normal,
  error: function(data) {
    if ($('#status_loading')) {
      $('#status_loading').html(data);
      if ($('#status_loading').is(':hidden')) {
        $('#status_loading').slideDown();
      }
    } else {
      alert('Erro identificado: ' + data);
    }
  }
});
$(document).ajaxError(function(event, xhr, settings) {
  var str = 'Erro encontrado!\n\nDescri\u00e7\u00e3o: ';
  if (xhr.status == 404) {
    str += 'Arquivo ' + settings.url + ' n\u00e3o localizado!';
  } else {
    str += xhr.responseText;
  }
  alert(str);
});
// Funcao enviar mensagem
function SendMensage() {
  $.ajax({
    url: 'funcoes/contato_enviar.php',
    data: {
      Nome: $('#Nome').val(),
      Email: $('#Email').val(),
      Assunto: $('#Assunto').val(),
      Mensagem: $('#Mensagem').val()
    },
    beforeSend: function() {
      if (IsEmpty($('#Nome').val())) {
        alert('Por favor, entre com o seu nome');
        $('#Nome').focus();
        return false;
      } else if (vEmail($('#Email').val())) {
        alert('Endere\u00e7o de e-mail n\u00e3o preenchido ou inv\u00e1lido.');
        $('#Email').focus();
        return false;
      } else if (IsEmpty($('#Assunto').val())) {
        alert('Por favor, entre com o assunto da mensagem');
        $('#Assunto').focus();
        return false;
      } else if (IsEmpty($('#Mensagem').val())) {
        alert('Por favor, entre com a Mensagem');
        $('#Mensagem').focus();
        return false;
      } else {
        fLoading('show')
        return true;
      }
    },
    success: function(data) {
      $('#status_loading').html(data);
      $('#Nome').val('');
      $('#Email').val('');
      $('#Assunto').val('');
      $('#Mensagem').val('');
    },
    complete: function() {
      fLoading('hide');
    }
  });
  return true;
}
