/**
 * Essa função cria uma mascara(padrão) para o campo fax
 */
function mascaraFax() {
  jQuery('#fax').keypress(function(e) {
  var strValidos = '0123456789';
  var BACKSPACE = 8;
  var key;
  var tecla;

    if(jQuery.browser.msie) {
      tecla = e.keyCode;
    } else {
      tecla = e.which;
    }
    key = String.fromCharCode(tecla);

    if (tecla == 13) return false;
    if (String.fromCharCode(e.keyCode) == '\t') return true;
    if (e.keyCode == 46 || e.keyCode == 35 || e.keyCode == 36 || e.keyCode == 37 || e.keyCode == 39) return true;
    if (tecla == BACKSPACE) return true;
    if (strValidos.indexOf(key) == -1) return false;

    if(jQuery(this).val() == '') {
      jQuery(this).val('('+jQuery(this).val());
    }
    if(jQuery(this).val().length == 3) {
      jQuery(this).val(jQuery(this).val()+') ');
    }
    if(jQuery(this).val().length == 9) {
      jQuery(this).val(jQuery(this).val()+'-');
    }
    });
}
/**
* Essa função cria uma mascara(padrão) para o campo telefone
 */
function maskTel(){
    $("#tel").mask("(99)9999-9999",{placeholder:" "});
};
/**
 * Essa função faz com que mude de campo automaticamente
 */
function keyUp(){
    $('#fax').keyup(function(event) {
        var tamanho = $(this).val().length;
        if (tamanho >= 14) {
            $('#ramal').focus();
        }
    });
};
/**
 * Funação ajax para o form
*/
function ajaxEnviaForm(){
    $forms = $('form');
    $forms.bind('submit', function(){
        var $button = $('button',this).attr('disabled',true);
        var params = $(this.elements).serialize();
        var self = this;

        //erro = validate.errorList.length > 0 ? true : false;


        $.ajax({
            type: 'POST',
            url: this.action,
            data: params,
            beforeSend: function(){
            $('#loading').show();
            
            },
            success: function(txt){
            $button.attr('disabled',false);
            $('#loading').html(txt);
            self.reset();
            },
            error: function(txt){
            $('#loading').html(txt);
            }
        });
        return false;
    });
}
/**
 * Chamo todas as funções
 */
$(document).ready(function(){
    $("#commentForm").validate();
    mascaraFax();
    maskTel();
    keyUp();
    ajaxEnviaForm();
});
  
 
