$(document).ready(function(){

	$("table.lista tbody tr:nth-child(odd)").addClass("cor1");
	$("table.lista tbody tr:nth-child(even)").addClass("cor2");

	$('table.resultados tbody tr').hover(function(){
		$(this).toggleClass('hoverRow'); },
		function(){
			$(this).toggleClass('hoverRow');
	  });
});

$(document).ready(function(){

	$("table.filtro tbody tr:nth-child(odd)").addClass("cor1");
	$("table.filtro tbody tr:nth-child(even)").addClass("cor2");

	$('table.filtro tbody tr').hover(function(){
		$(this).toggleClass('hoverRow'); },
		function(){
			$(this).toggleClass('hoverRow');
	  });
});

	Format = function() {};
	
	//formatacao generia para uma ocorrencia
	Format.format = function(strValue, objRegExp, strReplace) {
		if(objRegExp.test(strValue))
			return strValue.replace(objRegExp, strReplace);
		return strValue;
	};
	
	//formatacao generica para varias ocorrencias
	Format.formatAll = function(strValue,objRegExp,strReplace) {
		while(objRegExp.test(strValue))
			strValue = strValue.replace(objRegExp,strReplace);
		return strValue;
	};
	
	//formatacao de Numeros
	Format.number = function (strValue) {
		return Format.format(strValue, new RegExp('[^0-9]*', 'g'), '');
	};

	//formatacao de Numeros sem Zero (1-9)
	Format.numberNoZero = function(strValue) {
		return Format.format(strValue, new RegExp('[^1-9]*', 'g'), '');
	};

	//formatacao de Numeros sem Zero (1-9)
	Format.numberNegative = function(strValue) {
		return Format.format(strValue, new RegExp('[^1-9-]*', 'g'), '');
	};
	
	//formatacao de texto somente texto sem acento
	Format.text = function(strValue) {
		return Format.format(strValue, new RegExp('[^A-Za-z]*', 'g'), '');
	};

	//formatacao de em formato de texto 10.100.100 ou -10.100.100
	Format.textNumber = function(strValue) {
		return Format.format(strValue, new RegExp('[^A-Za-z0-9]*', 'g'), '');
	};


	//Formata Texto(a-z, A-Z), Número e Espaço
	Format.textNumberSpace = function(strValue) {
		return Format.format(strValue, new RegExp('[^\\sA-Za-z0-9]*', 'g'), '');
	};

	//Formata Texto(a-z, A-Z), Número, Espaço, Sinal Agudo e Sinal Crase
	Format.textNumberSpaceAcuteCrase = function(strValue) {
		return Format.format(strValue, new RegExp('[^\\s´`A-Za-z0-9]*', 'g'), '');
	};
	
	//Formata Texto(a-z, A-Z), Número, menos ' " ^ ~ * (FRANKE)
	Format.textNumberNoSpecials = function(strValue) {
		return Format.format(strValue, new RegExp('[^\\s´`A-Za-z0-9!#$%&()+/*,-.//:;<=>?@[\\]_{|}]*', 'g'), '');
	};

	//formatacao de número e vírgula
	Format.numberComma = function(strValue) {
		return Format.format(strValue, new RegExp('[^0-9,]', 'g'), '');
	};

	//formatacao de CEP
	Format.cep = function (strValue) {
		strValue = Format.number(strValue);
		if(strValue.length>8) strValue = strValue.substring(0,8);
		return Format.format(strValue, new RegExp('^([0-9]{5})([0-9])'), '$1-$2');
	};
	
	//005.432.519-69
	Format.cpf = function (strValue) {
		strValue = Format.number(strValue);
		if(strValue.length>11) strValue = strValue.substring(0,11);
		return Format.format(strValue, new RegExp('^([0-9]{3})([0-9]{3})([0-9]{3})([0-9])'), '$1.$2.$3-$4');
	};
	
	//formatacao de DATA 88/88/8888
	Format.date = function (strValue) {
		p = new RegExp('^([0-9]{2})/([0-9]{2})/([0-9]{4})$');
		if(p.test(strValue)) strValue;
		strValue = Format.number(strValue);
		if(strValue.length>8) strValue = strValue.substring(0,8);
		strValue = Format.format(strValue, new RegExp('([0-9]{2})([0-9])'), '$1/$2');
		return Format.format(strValue, new RegExp('([0-9]{2})/([0-9]{2})([0-9])'), '$1/$2/$3');
	};
	
	//formatacao de Hora 88:88
	Format.time = function (strValue) {
		strValue = Format.number(strValue);
		if(strValue.length>4) strValue = strValue.substring(0,4);
		return Format.format(strValue, new RegExp('([0-9]{2})([0-9])'), '$1:$2');
	};

	//formatacao de Telefone 1234-1234
	Format.phone = function (strValue) {
		strValue = Format.number(strValue);
		if(strValue.length>9) strValue = strValue.substring(0,9);
		return Format.format(strValue, new RegExp('([0-9]{4})([0-9])'), '$1-$2');
	};	
	
	//Valida se é uma string com 2 dígitos no ano
	isDate2 = function(pStr) {	
	   var reDate = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{2}$/;
	   return (reDate.test(pStr));
	};
	
	//Valida se é uma string com 4 dígitos no ano
	isDate4 = function(pStr) {
	   var reDate = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
	   return (reDate.test(pStr));
	};

	//Valida se é uma string no formato hora 00:00
	isTime = function(pStr) {
	   var reTime = /^([0-1]\d|2[0-3]):[0-5]\d$/;
	   return (reTime.test(pStr));
	};

	//Valida se é uma string com email válido
	isEmail = function(pStr){
		var reEmail = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
		return reEmail.test(pStr);
	};

	//UTILIZADO PARA VALIDAR VALOR DECIMAL, SENDO POSSÍVEL INFORMAR SINAL +/- E PERMITE VALOR = 0
	isDecimalPt = function(pStr) {
	   var reDecimalPt = /^[+-]?((\d+|\d{1,3}(\.\d{3})+)(\,\d*)?|\,\d+)$/;
	   return (reDecimalPt.test(pStr));
	};

	//UTILIZADO PARA VALIDAR SE A STRING TEM SOMENTE NÚMERO(S) E LETRA(S)
	isNumberAndLetter = function(pStr){
		var reNL = /^[A-Za-z0-9]+$/;
		return (reNL.test(pStr));
	}

	//UTILIZADO PARA VALIDAR SE A STRING TEM SOMENTE NÚMERO(S), LETRA(S) E ESPAÇO(S)
	isNumberAndLetterSpace = function(pStr){
		var reNL = /^[\sfA-Za-z0-9]+$/;
		return (reNL.test(pStr));
	}
	
	//UTILIZADO PARA VALIDAR SE A STRING TEM SOMENTE NÚMERO(S), LETRA(S), ESPAÇO(S), AGUDO E CRASE
	isNumberAndLetterSpaceAcuteCrase = function(pStr){
		var reNL = /^[\sf´`A-Za-z0-9]+$/;
		return (reNL.test(pStr));
	}

	//UTILIZADO PARA VALIDAR SE A STRING TEM CARACTERES ESPECIAIS (FRANKE)
	isNumberLetterNoSpecial = function(pStr){
		var reNL = /^[\sf´`A-Za-z0-9!#$%&()+,-.*/:;<=>?@[\]_{|}]+$/;
		return (reNL.test(pStr));
	}
	
	
	//MÁSCARA PARA MOEDA, OU SEJA, VALOR DECIMAL SEM O SINAL +/-
	Format.money = function(pStr){  
        v=pStr.replace(/\D/g,"");  //permite digitar apenas números
		v=v.replace(/[0-9]{15}/,"");   //limita pra máximo 999.999.999.999,99
		v=v.replace(/(\d{1})(\d{11})$/,"$1.$2");  //coloca ponto antes dos últimos 11 digitos
		v=v.replace(/(\d{1})(\d{8})$/,"$1.$2");   //coloca ponto antes dos últimos 8 digitos
		v=v.replace(/(\d{1})(\d{5})$/,"$1.$2");   //coloca ponto antes dos últimos 5 digitos
		v=v.replace(/(\d{1})(\d{1,2})$/,"$1,$2"); //coloca virgula antes dos últimos 2 digitos
		return v;
    };

	//FORMATA O ELEMENTO APLICANDO A MÁSCARA DE MOEDA - MAXIMO 18 CARACTERES
	formataDinheiro = function(elemento){
		elemento.val ( Format.money(elemento.val()) );
	};

	//FORMATA O ELEMENTO PERMITINDO SOMENTE TEXTO A-Z E a-z
	formataTexto = function(elemento){
		elemento.val ( Format.text(elemento.val()) );
	};
	
	//FORMATA O ELEMENTO PERMITINDO SOMENTE NÚMEROS
	formataNumero = function(elemento){
		elemento.val ( Format.number(elemento.val()) );
	};

	//FORMATA O ELEMENTO PERMITINDO SOMENTE NÚMEROS
	formataNumeroNegativo = function(elemento){
		elemento.val ( Format.numberNegative( elemento.val()) );
	};

	//FORMATA O ELEMENTO PERMITINDO SOMENTE NÚMEROS
	formataNumeroComVirgula = function(elemento){
		elemento.val ( Format.numberComma(elemento.val()) );
	};

	//FORMATA O ELEMENTO PARA NÚMERO DE TELEFONE SEPARADO POR HÍFEN
	formataTelefone = function(elemento){
		elemento.val ( Format.phone(elemento.val()) );
	};
	
	//FORMATA O ELEMENTO PARA NÚMERO DE TELEFONE SEPARADO POR HÍFEN
	formataData = function(elemento){
		elemento.val ( Format.date(elemento.val()) );
	};
	
	//FORMATA O ELEMENTO PARA SOMENTE NÚMEROS, LETRAS E ESPAÇOS
	formataNumerosLetrasEspacos = function(elemento){
		if( elemento.val()!='' && !isNumberAndLetterSpace( elemento.val() ) ){
			novo = Format.textNumberSpace( elemento.val() );
			elemento.val(novo.toUpperCase());
			alert('Este campo deve conter somente Números, Letras (sem caracteres especiais) e Espaços');
			elemento.focus();
		}else{
			elemento.val($(elemento).val().toUpperCase());
		}
	}

	//FORMATA O ELEMENTO PARA SOMENTE NÚMEROS, LETRAS E ESPAÇOS
	formataNumerosLetrasEspacosAgudoCrase = function(elemento){
		if( elemento.val()!='' && !isNumberAndLetterSpaceAcuteCrase( elemento.val() ) ){
			novo = Format.textNumberSpaceAcuteCrase( elemento.val() );
			elemento.val(novo.toUpperCase());
			alert('Este campo deve conter somente Número, Letra (sem caractere especial), Espaço, ´ (agudo) e ` (crase).');
			elemento.focus();
		}else{
			elemento.val($(elemento).val().toUpperCase());
		}
	}
	
	//FORMATA O ELEMENTO PARA SOMENTE NÚMEROS, LETRAS E ESPAÇOS
	formataSemEspeciais = function(elemento){
		if( elemento.val()!='' && !isNumberLetterNoSpecial( elemento.val() ) ){
			novo = Format.textNumberNoSpecials( elemento.val() );
			elemento.val(novo.toUpperCase());
			alert('Este não pode ter caracteres especiais');
			elemento.focus();
		}else{
			elemento.val($(elemento).val().toUpperCase());
		}
	}
	

	formataValorParaCalculo = function(valor){
		valor = valor.replace('.', '')
		return valor.replace(',', '.');
	}
	
	formataValorParaTela = function(valor){
		return valor.replace('.', ',');
	}

	
	//UTILIZADO PARA RETIRAR OS ESPAÇOS À ESQUERDA E À DIREITA 
	//RESOLVE O PROBLEMA DE PREENCHIMENTO DOS CAMPOS COM ESPAÇO
	jQuery.fn.trimFields = function(){
		this.each(function(){
			$(this).val( jQuery.trim($(this).val()) );
		});
	}


	jQuery.fn.counter = function(qtde) {
	  $(this).each(function() {
		var max = qtde;
		var val = $(this).attr('value');
		var cur = 0;
		if(val) //value="", or no value at all will cause an error
		cur = val.length;

		var left = max-cur;
		$(this).after("<div class='counter'>" + left.toString() + " caracteres restantes" + "</div>");
			
			$(this).keyup(function(i) {
			  var max = qtde;
			  var val = $(this).attr('value');
			  var cur = 0;
			  if(val)
			  cur = val.length;
			  var left = max-cur;
				if(left <= 3){
					$('.counter').css('color', '#ff0000');
				} else {
					$('.counter').css('color', '#666666');
				}
				if(left<1){
					$(this).val( ($(this).val()).substring(0,qtde) );	
				}
				if(left>=0){
					$(this).next(".counter").text(left.toString() + " caracteres restantes");
				}
			  return this;
			});
		
	  });
	  return this;
	}
	
	$.fn.clearForm = function() {
		$( this ).
			find( ':text, :password, textarea' ).
				attr( 'value', '' ).end().
			find( ':checkbox, :radio' ).
				attr( 'checked', false ).end().
			find( 'select' ).
				attr( 'selectedIndex', -1 );
	}
