function mascarasPeso(obj, tipo, teclapres, qtd){
	var tecla;
	var tamanho = obj.value.length;
	var aux = obj.value;

	if(tipo == 'peso' && tecla != 9 && tecla != 37 && tecla != 39){
		var aux = obj.value.replace(/,/gi,"");
		
		if((tamanho>=qtd)&&(tamanho<=4)){
			obj.value = aux.substring(0,tamanho)+','; 
			if(obj.value == ','){
				obj.value = '';
			}
		}
	}
}

function mascaras(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	} else {
		nTecla = evtKeyPress.which;
		if (nTecla == 8) {
			return true;
		}
	}
    sValue = objeto.value;
    // Limpa todos os caracteres de formata��o que j� estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ",", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == ",") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++; }
      else {
        sCod += sValue.charAt(nCount);
        nCount++;
      }

      i++;
    }

    objeto.value = sCod;

    if (nTecla != 8) { // backspace
      if (sMask.charAt(i-1) == "9") { // apenas n�meros...
        return ((nTecla > 47) && (nTecla < 58)); }
      else { // qualquer caracter...
        return true;
      }
    }
    else {
      return true;
    }
  }

function isNum( caractere ){ 
	var strValidos = "0123456789" 
	if (strValidos.indexOf(caractere)==-1) 
		return false; 	
		return true; 
}

function validaNumero(campo, event){ 

	var BACKSPACE= 8; 
	var key; 
	var tecla; 
	CheckTAB=true; 
	if(navigator.appName.indexOf("Netscape")!= -1) 
		tecla= event.which;
	else 
		tecla= event.keyCode;
	key = String.fromCharCode( tecla); 
	
	if ( tecla == 13 ) return false; 
	if ( tecla == BACKSPACE ) return true; 
	if ( tecla == 0) return true; 
	return (isNum(key)); 
}
function validaNumero2(campo, event){ 

	var BACKSPACE=8; 
	var key; 
	var tecla; 
	CheckTAB=true; 
	if(navigator.appName.indexOf("Netscape")!= -1) 
		tecla= event.which;
	else 
		tecla= event.keyCode;
	key = String.fromCharCode( tecla); 
	
	if ( tecla == 13 ) return false; 
	if ( tecla == BACKSPACE ) return true; 
	if ( tecla == 0) return true; 
	return (isNum(key)); 
}

function validaEmail(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 {
			return false;
		}
	} else if(typeof(mail)=='object'){
		if(er.test(mail.value)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

$(document).ready(
         function() {
            $(".listagem tr:even").addClass("alt");
         }
      );

$(function(){
		   
	$('.isNum').live('keypress',function (e){
		if(validaNumero(this, e)){
			return true;
		} else {
			return false;
		}
	});

	$('.isAlpha').keypress(function (e){
		if(validaLetras(e,'inteiro')){
			return true;
		}else{
			return false;
		}
	});

	$('.mascaraTelefone').keypress(function (e){
		mascaras(this, '(99)9999-9999', e)
	});
	
	$('.mascaraEmail').keypress(function(e){
		validaEmail(this, e)
			});
										

	$('.mascaraData').live('keypress',function (e){
		mascaras(this, '99/99/9999', e)
		if(validaNumero(this, e)){
			return true;
		} else {
			return false;
		}
	});
	
		$('.mascaraTime').live('keypress',function (e){
		mascaras(this, '24:60:60', e)
		if(validaNumero2(this, e)){
			return true;
		} else {
			return false;
		}
	});

	$('.mascaraPeso').keypress(function (e){
		if(validaNumero(this, e)){
			mascarasPeso(this,'peso',e,2); 
		}
	});

	$('.mascaraAltura').keypress(function (e){
		mascaras(this, '9,99', e)
	});

	$('#frm_cadastro').submit(function(event){

		$('.obrig',this).not(':hidden').not(':disabled').each(function(){

			$('.input-obrig').removeClass("input-obrig");
			$("#msg").html('');
			$("#msg").hide();

			if($(this).attr('type')=='radio' && ! $('input[@name='+$(this).attr('name')+'][@checked]').val() ){
				$(this).addClass('input-obrig');
				campo_msg = "#msg_" + $(this).attr('id');
				$("#msg").html($(campo_msg).html());
				$("#msg").show();
				event.preventDefault();
				return false;
			}
			if($(this).attr('type')=='checkbox' && ! $('input[@name='+$(this).attr('name')+'][@checked]').val() ){
				$(this).addClass("input-obrig");
				campo_msg = "#msg_" + $(this).attr('id');
				$("#msg").html($(campo_msg).html());
				$("#msg").show();
				event.preventDefault();
				return false;
			} 
			if( ($(this).attr('type')=='select-one')&&($(this).val()==0)){
				$(this).addClass("input-obrig");
				campo_msg = "#msg_" + $(this).attr('id');
				$("#msg").html($(campo_msg).html());
				$("#msg").show();
				event.preventDefault();
				return false;
			} 
			
			if($.trim($(this).val())==''){
				$(this).addClass("input-obrig");
				campo_msg = "#msg_" + $(this).attr('id');
				$("#msg").html($(campo_msg).html());
				$("#msg").show();
				event.preventDefault();
				return false;
			}else{
				if($(this).hasClass('isEmail') && !validaEmail( $.trim( $(this).val() ) ) ){
					$(this).addClass("input-obrig");
					$("#msg").html('O formato do E-Mail n&atilde;o &eacute; v&aacute;lido');
					$("#msg").show();
					event.preventDefault();
					return false;
				}
				if ($(this).hasClass('isCpf')&& (existeCPF(this)==0 || existeCPF(this)==2)){
					$(this).addClass("input-obrig");
					event.preventDefault();
					return false;
				}
				if ($(this).hasClass('isCnpj') && !isCnpj($(this).val())){
					$(this).addClass("input-obrig");
					event.preventDefault();
					return false;
				}
			}
		});
	});
});
