﻿$(document).ready(function() {

	jQuery.fn.ForceNumericOnly2 = function(){
		return this.each(function(){
			$(this).bind('change '+($.browser.msie ? 'paste' : 'input'), function(e){
				this.value = this.value.replace(/[^0-9^//]/g,'');
			})
			$(this).bind('keydown keypress', function(e){
				var key = e.charCode || e.keyCode || 0;
				// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
				
				if (key  <  32 || 
						key ==  46 || key ==  47 ||
						key == 127 ||	key == 116 ||
						key == 111 || key == 193 ||
						(key >= 37 && key <= 40) ||
						(key >= 46 && key <= 57) ||
						(key >= 96 && key <= 105)
				){
					return true;
				} else {
					return false;
				}
			})
		})
	};
	jQuery.fn.ForceNumericOnly = function(){
		return this.each(function(){
			$(this).bind('change '+($.browser.msie ? 'paste' : 'input'), function(e){
				this.value = this.value.replace(/[^0-9]/g,'');
			})
			$(this).bind('keydown keypress', function(e){
				var key = e.charCode || e.keyCode || 0;
				// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
				if (key  <  32 ||	key ==  46 ||
						key == 127 ||	key == 116 ||
						(key >= 37 && key <= 40) ||
						(key >= 46 && key <= 57) ||
						(key >= 96 && key <= 105)
				){
					return true;
				} else {
					return false;
				}
			})
		})
	};
	
	jQuery.fn.ForceLettersOnly = function(){
		return this.each(function(){
			$(this).bind('change '+($.browser.msie ? 'paste' : 'input'), function(e){
				this.value = this.value.replace(/[0-9]/g,'');
			})
			$(this).bind('keydown keypress', function(e){
				var key = e.charCode || e.keyCode || 0;
				// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
				if((key >= 48 && key <= 57)){
					return false;
				} else {
					return true;
				}
			})
		})
	};

	labelInInputPassword('senhaLoginText','senhaLogin');
	$('.labelInInput').bind('blur', function() {
		if(this.value==''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	$('.labelInInput').bind('focus', function() {
		if(this.value==(this.defaultValue ? this.defaultValue : '')){
			this.value = '';
		}
	});
	$('.onlyLettersInInput').ForceLettersOnly();
	$('.onlyNumbersInInput').ForceNumericOnly();
	$('.onlyNumbersInInput2').ForceNumericOnly2();
	
	$('#p-esqueceuSenha').toggle(
		function () {
			$("#frmLoginAcao").val("esqueciSenha");
			$("#senhalogin").hide();
			$("#btnSubmitLembrarSenha").css("margin-top", "16px");
			$("#a-esqueceuSenha").html("voltar para login");
		},
		function () {
			$("#frmLoginAcao").val("login");
			$("#senhalogin").show();
			$("#btnSubmitLembrarSenha").css("margin-top", "");
			$("#a-esqueceuSenha").html("esqueci a senha");
		}
	);
	$('#nav-o-que-e,#sub-nav-o-que-e').mouseover(function() {
		$('#sub-nav-o-que-e').show();
	});
	$('#nav-o-que-e,#sub-nav-o-que-e').mouseout(function() {
		$('#sub-nav-o-que-e').hide();
	});
});

function labelInInputPassword(idInputText,idInputPassword){
	$('input#'+idInputText).bind('focus', function() {
		$(this).hide();
		$('input#'+idInputPassword).show();
		$('input#'+idInputPassword).focus();
	});
	$('input#'+idInputPassword).bind('blur', function() {
		if(this.value==''){
			$(this).hide();
			$('input#'+idInputText).show();
		}
	});
}
function validaCNPJ(StrCGC){
	StrCGC = StrCGC.replace(/\./g,"");
	StrCGC = StrCGC.replace(/\//g,"");
	StrCGC = StrCGC.replace(/-/g,"");
	var varFirstChr = StrCGC.charAt(0);
	var vlMult,vlControle,s1, s2 = "";
	var i,j,vlDgito,vlSoma = 0;
	var vaCharCGC;
	for ( var i=0; i<=13; i++ ){
		var c = StrCGC.charAt(i);
		if(!(c>="0") && (c<="9")){
			return false;
		}
		if(c!=varFirstChr){
			vaCharCGC = true;
		}
	}
	if(!vaCharCGC){
		return false;
	}
	s1 = StrCGC.substring(0,12);
	s2 = StrCGC.substring(12,15);
	vlMult = "543298765432";
	vlControle = "";
	for (j=1; j<3; j++){
		vlSoma = 0;
		for (i=0; i<12; i++){
			vlSoma += eval( s1.charAt(i) )* eval( vlMult.charAt(i) );
		}
		if(j == 2){
			vlSoma += (2 * vlDgito);
		}
		vlDgito = ((vlSoma*10) % 11);
		if(vlDgito == 10){
			vlDgito = 0;
		}
		vlControle = vlControle + vlDgito;
		vlMult = "654329876543";
	}
	if(vlControle != s2){
		return false;
	} else {
		return true;
	}
}
function validaEmail(src) {
  emailReg = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
  var regex = new RegExp(emailReg);
  return regex.test(src);
}
function validaLogin(formulario){
	formulario.btnSubmit.focus();
	if(formulario.login.value==formulario.login.defaultValue){
		alert('Digite seu usuário.');
		formulario.login.focus();
		return false;
	} else if(formulario.senha.value==''){
		formulario.senhaText.style.display = 'none';
		formulario.senha.style.display = '';
		alert('Digite sua senha.');
		formulario.senha.focus();
		return false;
	} else {
		formulario.action = 'login.asp';
		return true;
	}
}
