/************************** CALENDARIO **********************************************/
function openCalendar() {
	msAttributes = "dialogWidth: 600px; dialogHeight: 500px; dialogTop: 200px; dialogLeft: 200px; edge: Raised; center: Yes; help: No; resizable: No; status: No; scroll:no;";
//	window.open("../php/calendario.php");
	var retorno = window.showModalDialog("comentario.php","",msAttributes);
	if(retorno != "undefined")
	{
		var strData = retorno.split(";");
		novo_diario.hdnData.value = strData[1]; 				
		novo_diario.txtData.value = strData[0]; 
	}
}

function insereData(id1,id2){
	returnValue = id1 + ';' + id2;
	window.close(); 
}

/********************************* MOEDA ***************************************/

function fMascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
	    var sep = 0;
	    var key = '';
	    var i = j = 0;
	    var len = len2 = 0;
	    var strCheck = '0123456789';
	    var aux = aux2 = '';
	    var whichCode = (window.Event) ? e.which : e.keyCode;

	    if (objTextBox.value.length <= 21){
	    
			if (whichCode == 13) return true;
				key = String.fromCharCode(whichCode); // Valor para o código da Chave
			if (strCheck.indexOf(key) == -1) return false; // Chave inválida
			len = objTextBox.value.length;
				
			for(i = 0; i < len; i++)
			    if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
			aux = '';
			for(; i < len; i++)
			    if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
			aux += key;
			len = aux.length;
			if (len == 0) objTextBox.value = '';
			if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
			if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
			if (len > 2) {
			    aux2 = '';
			    for (j = 0, i = len - 3; i >= 0; i--) {
			        if (j == 3) {
			            aux2 += SeparadorMilesimo;
			            j = 0;
			        }
			        aux2 += aux.charAt(i);
			        j++;
			    }
			    objTextBox.value = '';
			    len2 = aux2.length;
			    for (i = len2 - 1; i >= 0; i--)
			    objTextBox.value += aux2.charAt(i);
			    objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
			}
			return false;
		}
	}	
	
//*********************** FORMATA DATA *********************************	
function formata_data(val){
       	var pass = val.value;
      	var expr = /[0123456789]/;
      		
      	for(i=0; i<pass.length; i++){
      		// charAt -> retorna o caractere posicionado no índice especificado
      		var lchar = val.value.charAt(i);
      		var nchar = val.value.charAt(i+1);
      	
      		if(i==0){
      		   // search -> retorna um valor inteiro, indicando a posição do inicio da primeira
      		   // ocorrência de expReg dentro de instStr. Se nenhuma ocorrencia for encontrada o método retornara -1
      		   // instStr.search(expReg);
      		   if ((lchar.search(expr) != 0) || (lchar>3)){
      			  val.value = "";
      		   }
      		   
      		}else if(i==1){
      			   
      			   if(lchar.search(expr) != 0){
      				  // substring(indice1,indice2)
      				  // indice1, indice2 -> será usado para delimitar a string
      				  var tst1 = val.value.substring(0,(i));
      				  val.value = tst1;				
       				  continue;			
      			   }
      			   
      			   if ((nchar != '/') && (nchar != '')){
      				 	var tst1 = val.value.substring(0, (i)+1);
      				
      					if(nchar.search(expr) != 0) 
      						var tst2 = val.value.substring(i+2, pass.length);
      					else
      						var tst2 = val.value.substring(i+1, pass.length);
      	
      					val.value = tst1 + '/' + tst2;
      			   }
      
      		 }else if(i==4){
      			
      				if(lchar.search(expr) != 0){
      					var tst1 = val.value.substring(0, (i));
      					val.value = tst1;
      					continue;			
      				}
      		
      				if	((nchar != '/') && (nchar != '')){
      					var tst1 = val.value.substring(0, (i)+1);
      
      					if(nchar.search(expr) != 0) 
      						var tst2 = val.value.substring(i+2, pass.length);
      					else
      						var tst2 = val.value.substring(i+1, pass.length);
      	
      					val.value = tst1 + '/' + tst2;
      				}
         		  }
      		
      		  if(i>=6){
      			  if(lchar.search(expr) != 0) {
      					var tst1 = val.value.substring(0, (i));
      					val.value = tst1;			
      			  }
      		  }
      	 }
      	
	if(pass.length>10)
	val.value = val.value.substring(0, 10);
	return true;
}	


//*********************** EXIBE A DATA ATUAL *********************************
function Data(){
		var mydate=new Date()
		var year=mydate.getYear()
		if (year < 1000)
		year+=1900
		var day=mydate.getDay()
		var month=mydate.getMonth()
		var daym=mydate.getDate()
		if (daym<10)
		daym="0"+daym
		var dayarray=new Array("Domingo","Segunda","Terça","Quarta","Quinta","Sexta","Sábado")
		var montharray=new
		Array("Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro")
		document.write("<b>"+dayarray[day]+", "+daym+" de "+montharray[month]+" de "+year+"</b>")
}


//*********************** SÓ PERMEITE NÚMEROS *********************************
function soNumeros(Evento) {

var intKeyCode = Evento.keyCode;
	if (intKeyCode > 47 && intKeyCode < 58)//numeros de 0 a 9
		return true;
	else
	{
		if (intKeyCode != 8 && intKeyCode != 13) // backspace e enter
			event.keyCode = 0;//return false;
		else
		return true;
	}
}

function numerico(Evento) {
 	var intKeyCode = Evento;
	var expr = /[0123456789]/;
	
	for(i = 0; i < Evento.length; i++){
		intKeyCode = Evento.substr(i,1);
		if(intKeyCode.search(expr) != 0){
			alert('Para pesquisar por ID utilize somente numeros.');
			return false
		} 
	}
	return true
}

//*********************** CNPJ *********************************
function validaCNPJ(cnpj,filial,controle) {
	CNPJ = cnpj + filial + controle;

	erro = new String;
	if (CNPJ.length < 14){
		erro += "É necessarios preencher todos os numeros do CNPJ! \n\n";
	}

	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++){
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2){
		a[12] = 0;
	}else{
		a[12] = 11-x;
	}

	b = 0;
	for (y=0; y<13; y++){
		b += (a[y] * c[y]);
	}

	if ((x = b % 11) < 2){ 
		a[13] = 0;
	}else{
		a[13] = 11-x;
	}

	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		erro +="CNPJ Inválido";
	}

	if (erro.length > 0){
		alert(erro);
		return false;
	}
}

//*********************** CPF *********************************
function validaCPF(cpf, controle){
 
var i;
 
s = cpf + controle;
 
var c  = s.substr(0,9);
var dv = s.substr(9,2);
var d1 = 0;
 
for (i = 0; i < 9; i++){
d1 += c.charAt(i)*(10-i);
}
 
if (d1 == 0){ 
alert("CPF Invalido")
return false;
}
 
d1 = 11 - (d1 % 11);
 
if (d1 > 9) d1 = 0;
 
if (dv.charAt(0) != d1){
	alert("CPF Invalido")
 	return false;
}
 
d1 *= 2;
 
for (i = 0; i < 9; i++){
	d1 += c.charAt(i)*(11-i);
}
 
d1 = 11 - (d1 % 11);
 
if (d1 > 9){
	d1 = 0;
}
 
if (dv.charAt(1) != d1){ 
	alert("CPF Invalido")
	return false;
}
 
return true;
}

//*********************** TECLADO VIRTUAL *********************************
function senha(valor) {
	//Objeto de acesso ao campo visor
	var senha = document.getElementById("senha");
	valor = valor.substr(1,1);
	//Acrescenta o numero digitado ao visor e a senha
	senha.value += valor;
 if(document.getElementById('mensagem') != null){
			Security(senha.value);
	}
}

function limpa() {
	var senha = document.getElementById("senha");
	//apaga os valores já digitados
	senha.value = "";
}

function valida() {
	var senha = document.getElementById("senha");
	//valida algum valor foi digitado
	if(!senha.value){alert("Utilize o teclado virtual para preencher a senha.");return false;}
	
	//valida se ainda é a senha padrão
	if(senha.value == "K9T6G8"){alert("Você ainda está utilizando a senha padrão, aumente sua segurança trocando por uma senha de sua escolha.");}
	
	//valida o tamanho da senha, pode ser alterada ou excluida
	if(senha.value.length < 6){alert("A senha deve possuir no mínimo 6 caractéres.");return false;}

	document.login.submit();

}

//*********************** SEGURANÇA DE SENHA *********************************
function Security(val) {
 	
	document.getElementById('mensagem').innerHTML = "";
	document.getElementById('d_baixa').style.background = 'white';
	document.getElementById('d_media').style.background = 'white';
	document.getElementById('d_alta').style.background = 'white';
 
	if(val.length >= 8 && val.search(/[A-Z]/) != -1 && val.search(/[0-9]/) != -1 
		||val.length >= 8 && val.search(/[A-Z]/) != -1 
		||val.length >= 8 && val.search(/[0-9]/) 
		||val.length >= 8  && val.search(/[A-Z]/) != -1 && val.search(/[0-9]/))
		{
			document.getElementById('mensagem').innerHTML = "Forte";
			document.getElementById('d_baixa').style.background = 'green';
			document.getElementById('d_media').style.background = 'green';
			document.getElementById('d_alta').style.background = 'green';
	
  	} 
  	else{
  		if(val.length >= 5 && val.search(/[A-Z]/) != -1 
  			||val.length >= 5 && val.search(/[0-9]/) != -1 
					||val.length >= 5 && val.search(/[A-Z]/) != -1 && val.search(/[0-9]/) != -1
					||val.length >= 5 && val.search(/[A-Z]/) != -1
					||val.length >= 5 && val.search(/[0-9]/) != -1){

					document.getElementById('mensagem').innerHTML = "Média";
 				document.getElementById('d_baixa').style.background = 'yellow';
 				document.getElementById('d_media').style.background = 'yellow';
 				document.getElementById('d_alta').style.background = 'white';
  				
  			} 
  			else {
  				if(val.length >= 3){
	  				document.getElementById('mensagem').innerHTML = "Fraca";
  					document.getElementById('d_baixa').style.background = 'red';
							document.getElementById('d_media').style.background = 'white';
							document.getElementById('d_alta').style.background = 'white';
  				}
  		}
  	}
  }

//*********************** ABRE UM MODAL *********************************
function abreModal(strURL,wight,height){
		window.open(strURL,"Mapa","width="+wight+",height="+height+",resizable,scrollbars=yes,left=120,top=0"); 
}

//************************** MÁSCARA CEP ******************************
function mascaraCEP (objeto,e){
	campo = eval (objeto);
	var whichCode = (window.Event) ? e.which : e.keyCode;
	caracteres = '0123456789';
	separacoes = 1;
	separacao1 = '-';
	conjuntos = 2;
	conjunto1 = 5;
	conjunto2 = 3;
	if ((caracteres.search(String.fromCharCode (whichCode))!=-1) && campo.value.length < (conjunto1 + conjunto2 + 1)){
		if (campo.value.length == conjunto1){campo.value = campo.value + separacao1;}
	}
	else{
		event.returnValue = false;
	}
}
