// JavaScript Document

function Confirmacao(){

	var name=confirm("Deseja Mesmo Excluir?")
	if (name==true)
	{
		return true;
	}
	else
	{
		return false
	}
}

function limite(campo, limite) {
var max=limite;
if (campo.value.length > max) {
campo.value=campo.value.substring(max, 2);
return false;

}
else return true;
}

   
function habilita_buttons() {
 var d = document.forms[0];
 d.button1.disabled = false;
 d.button2.disabled = false;
 d.button3.disabled = false;
 d.button4.disabled = false;
}

function habilita_buttons_meta() {
 var d = document.forms[0];
 d.meta1.disabled = false;
 d.meta2.disabled = false;
}

function doDecimal(pStr)
{
	var reDecimalPt = /^[+-]?((\d+|\d{1,3}(\.\d{3})+)(\,\d*)?|\,\d+)$/;
	var reDecimalEn = /^[+-]?((\d+|\d{1,3}(\,\d{3})+)(\.\d*)?|\.\d+)$/;
	var reDecimal = reDecimalPt;
	pLang = "En";
	charDec = ( pLang != "En"? ",": "." );
	eval("reDecimal = reDecimal" + pLang);
	if (reDecimal.test(pStr)) {
		pos = pStr.indexOf(charDec);
		decs = pos == -1? 0: pStr.length - pos - 1;
		return true;
	} else if (pStr != null && pStr != "") {
		alert(pStr + " NÃO é um valor válido.");
		return false;
	}
} 

function checkbox(campo)
{

	if(campo.checked == true)
		campo.value = true; 
	else
	{
		campo.checked = false;
		campo.value = false; 
	}
}


function validaCPF(cpf) {	
			if(cpf == "" || cpf == null)
			{
				return true;
			}
			valor = true;
			erro = new String;
			if (cpf.length != 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n"; 
			var nonNumbers = /\D/;
			if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";	
			if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
				  erro += "Numero de CPF invalido!"
			}	
			var a = [];
			var b = new Number;
			var c = 11;
			for (i=0; i<11; i++){
				a[i] = cpf.charAt(i);
				if (i < 9) b += (a[i] *  --c);
			}	
			if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
			b = 0;
			c = 11;
			for (y=0; y<10; y++) b += (a[y] *  c--); 
			if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
			if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
				erro +="Digito verificador com problema!";
			}	
			if (erro.length > 0){
				alert(erro);
				return false;
			}
   		    return true;
}	


function doDigits(pStr)
{
	var reDigits = /^\d+$/;
	if (reDigits.test(pStr)) {
		return true;
	} else if (pStr != null && pStr != "") {
		alert(pStr + " NÃO contém apenas dígitos.");
		return false;
	}
}

function notnull(pStr)
{
	if (pStr.value == null || pStr.value == "") {
		alert(pStr.name + " NÃO pode ser nulo.");
		return false;
	}
	return true;
}

function confere_valores(val1,val2)
{
		if(val1!=val2)
		{
				alert("Senhas não conferem!");
				return false;
		}
		return true;
}


function validaData(str) { 

	if(str.value == "" || str.value == null)
	{
		return true;
	}

	dia = (str.value.substring(0,2)); 
    mes = (str.value.substring(3,5)); 
	ano = (str.value.substring(6,10)); 

	cons = true; 
	
	// verifica se foram digitados números
	if (isNaN(dia) || isNaN(mes) || isNaN(ano)){
		alert("Preencha a data somente com números."); 
		str.value = "";
		str.focus(); 
		return false;
	}
		
    // verifica o dia valido para cada mes 
    if ((dia < 01)||(dia < 01 || dia > 30) && 
		(mes == 04 || mes == 06 || 
		 mes == 09 || mes == 11 ) || 
		 dia > 31) { 
    	cons = false; 
	} 

	// verifica se o mes e valido 
	if (mes < 01 || mes > 12 ) { 
		cons = false; 
	} 

	// verifica se e ano bissexto 
	if (mes == 2 && ( dia < 01 || dia > 29 || 
	   ( dia > 28 && 
	   (parseInt(ano / 4) != ano / 4)))) { 
		cons = false; 
	} 
    
	if (cons == false) { 
		alert("A data inserida não é válida: " + str.value); 
		str.value = "";
		str.focus(); 
		return false;
	} 
}

// Diferença da anterior é que nao valida data nula
function validaData2(str) { 

	dia = (str.value.substring(0,2)); 
    mes = (str.value.substring(3,5)); 
	ano = (str.value.substring(6,10)); 

	cons = true; 
	
	// verifica se foram digitados números
	if (isNaN(dia) || isNaN(mes) || isNaN(ano)){
		alert("Preencha a data somente com números."); 
		str.value = "";
		str.focus(); 
		return false;
	}
		
    // verifica o dia valido para cada mes 
    if ((dia < 01)||(dia < 01 || dia > 30) && 
		(mes == 04 || mes == 06 || 
		 mes == 09 || mes == 11 ) || 
		 dia > 31) { 
    	cons = false; 
	} 

	// verifica se o mes e valido 
	if (mes < 01 || mes > 12 ) { 
		cons = false; 
	} 

	// verifica se e ano bissexto 
	if (mes == 2 && ( dia < 01 || dia > 29 || 
	   ( dia > 28 && 
	   (parseInt(ano / 4) != ano / 4)))) { 
		cons = false; 
	} 
    
	if (cons == false) { 
		alert("A data inserida não é válida: " + str.value); 
		str.value = "";
		str.focus(); 
		return false;
	} 
}


// colocar no evento onKeyUp passando o objeto como parametro
function formata(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;
}



function usuario_add()
{
	if(doDigits(document.forms[0].ano_titulacao.value) == false)
		return false;
    if(validaCPF(document.forms[0].cpf.value) == false)
		return false;
	if(notnull(document.forms[0].email) == false)
		return false;
	if(notnull(document.forms[0].senha) == false)
		return false;
	if(confere_valores(document.forms[0].senha.value,document.forms[0].confirme_senha.value) == false)
		return false;
	if(validaData(document.forms[0].data_rg) == false)
		return false;
}

function go_to(local, id)
{
		document.location.href=local+id.value;
}

function go_to_excluir(local, valor, conf)
{
	if(conf == true)
	   	document.location.href=local+valor;
}


function go_to_radio(local, id, conf)
{
	if(conf)
	{
		var i;
		var flag = false;
		for (i=0;i<id.length;i++)
		{
		    if (id[i].checked)
			{
			  flag=true;
    		  break;
			}
						
	   	}
		if(flag)
		{
		   	document.location.href=local+id[i].value;
		}
		else
		{
			if(id.checked)
			{
				document.location.href=local+id.value;
			}
			else
			{
				alert("Selecione um objeto!");
			}
		}
	}

}


