/**
 *
 * Arquivo de funções comuns aos Sites
 * Autor: BiTS - Business IT Solutions
 *
 */

//Funções que inicializam com o Site
$(document).ready(function(){

    //Url Padrão do Site
    base_url = base_url();

    //$('a[rel="facebox"]').facebox();
    
    //Máscaras Javascript
    //$('input:text').setMask();
    
    //Links para voltar à página anterior
    $('a[rel="voltar"]').click(function(){
        history.back();
        return false;
    });
    
    //Links para abrirem em uma página nova
    $('a[rel="externo"]').each(function(){
        $(this).attr('target','_blank');
    });

    //Validação de Somente Número
    $('.numero').each(function(){
        $(this).keypress(function(e){
            if(e.which!=8 && e.which!=0 && e.which!=46 && (e.which<48 || e.which>57)){
                return false;
            }
        });
    });

    //Validação de Somente Letras
    $('.letra').each(function(){
        $(this).keypress(function(e){
            if((e.which > 64 && e.which < 91) || (e.which > 96 && e.which < 123) || e.which == 8 || e.which == 0 || (e.which > 224 && e.which < 251)) {
                return true;
            } else {
                return false;
            }
        });
    });
    
    //Validação de Somente Letras
    $('.query').each(function(){
        $(this).keypress(function(e){
            if((e.which > 64 && e.which < 91) || (e.which > 96 && e.which < 123) || (e.which > 47 && e.which < 52) || e.which == 8 || e.which == 32 || e.which == 0 || e.which == 13 || (e.which > 224 && e.which < 251)) {
                return true;
            } else {
                return false;
            }
        });
    });

});

/**
 * Função que determina a URL Padrão do Site
 *
 * @return string
 */
function base_url(){

    baseUrl = '';

    if($('base').length > 0){
        baseUrl = $('base').attr('href');
    }

    return baseUrl;
}

/**
 * Função que calcula a altura da página
 *
 * @return int
 */
function getPageHeight(){

	var windowHeight

	if (self.innerHeight) {	// all except Explorer
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowHeight = document.body.clientHeight;
	}

	return windowHeight;

}

/**
 * Função que valida o CPF
 *
 * @return boolean
 *
 */
function checaCPF(CPF){

	if (CPF.charAt(3) == '.') {
		CPF = CPF.substr(0,3) + CPF.substr(4);
	}

	if (CPF.charAt(6) == '.') {
		CPF = CPF.substr(0,6) + CPF.substr(7);
	}

	if (CPF.charAt(9) == '-') {
		CPF = CPF.substr(0,9) + CPF.substr(10);
	}

	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
	CPF == "22222222222" || CPF == "33333333333" || CPF == "44444444444" ||
	CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
	CPF == "88888888888" || CPF == "99999999999")
		return false;

	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10)))
		return false;

	return true;

}

/**
 * Função que valida o CNPJ
 *
 * @return boolean
 *
 */
function checaCNPJ(cnpj){
	
	vr = cnpj;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	cnpj = vr;
	
    var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
      digitos_iguais = 1;
      if (cnpj.length < 14 && cnpj.length < 15)
            return false;
      for (i = 0; i < cnpj.length - 1; i++)
            if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            tamanho = cnpj.length - 2
            numeros = cnpj.substring(0,tamanho);
            digitos = cnpj.substring(tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            tamanho = tamanho + 1;
            numeros = cnpj.substring(0,tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
      

}

