// JavaScript Document
function ValidaEtapa1()
{
	valid = true;
	
	if (document.getElementById('edtMatriculaAluno').value == "")
	{
		alert("A matrícula do aluno não foi informada!");
		document.getElementById('edtMatriculaAluno').focus();
		valid = false;
	}
	else
	if (document.getElementById("cmbDiaNascimento").value == "null" || document.getElementById("cmbMesNascimento").value == "null"  || document.getElementById("cmbAnoNascimento").value == "null")
	{
		alert("A data de nascimento inválida!");
		document.getElementById("cmbDiaNascimento").focus();
		valid = false;				
	}
	else
	if (document.getElementById("edtCPFResponsavel").value == "")
	{
		alert("O CPF do responsável não foi informado!");
		document.getElementById("edtCPF").focus();
		valid = false;
	}
	
	return valid;
}

function ValidaEtapa2()
{
	valid = true;
	
	if (document.getElementById('cmbParcela').value == "null")
	{
		alert("A parcela não foi informada!");
		valid = false;
	}
	
	else
	if (document.getElementById("cmbDataVencimento").value == "null")
	{
		alert("Data de vencimento inválida!");
		document.getElementById("cmbDataVencimento").focus();
		valid = false;				
	}
	
	return valid;	
}

function GeraBoleto()
{
	if (ValidaEtapa2())
	{
		dataVencimento = document.getElementById("cmbDataVencimento").value;
		idparcela = document.getElementById("cmbParcela").value;
		
		xajax_GeraBoleto(idparcela, dataVencimento);		
	}
}

function Acessa()
{
	if (ValidaEtapa1())
	{
		matricula = document.getElementById("edtMatriculaAluno").value;
		dataNascimento = document.getElementById("cmbAnoNascimento").value + document.getElementById("cmbMesNascimento").value + document.getElementById("cmbDiaNascimento").value;
		cpf = document.getElementById("edtCPFResponsavel").value;
		
		xajax_CheckUser(matricula, dataNascimento, cpf);
	}
}

function ClearCombo(idCombo)
{

	while (document.getElementById(idCombo).options.length > 0)
		document.getElementById(idCombo).options.remove(document.getElementById(idCombo).options.length - 1);	
}

MaskInput = function(f, m){
	function mask(e){
		var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[À-ÿ]/i, "8": /./ },
			rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
		function accept(c, rule){
			for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
				if(r & i && patterns[i].test(c))
					break;
				return i <= r || c == rule;
		}
		var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
		(!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
			r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
			: (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
			r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
	}
	for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
		addEvent(f, i, mask);
};
/*
**************************************
* Event Listener Function v1.4       *
* Autor: Carlos R. L. Rodrigues      *
**************************************
*/
addEvent = function(o, e, f, s){
	var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
	r[r.length] = [f, s || o], o[e] = function(e){
		try{
			(e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
			e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
			e.target || (e.target = e.srcElement || null);
			e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
		}catch(f){}
		for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
		return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
	for(var i = (e = o["_on" + e] || []).length; i;)
		if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
			return delete e[i];
	return false;
};


 function validaCNPJ(CNPJ) {
		 erro = new String;
		 if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! \n\n"; 
		 if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		 if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! \n\n";
		 }
		 //substituir os caracteres que não são números
	   if(document.layers && parseInt(navigator.appVersion) == 4){
			   x = CNPJ.substring(0,2);
			   x += CNPJ. substring (3,6);
			   x += CNPJ. substring (7,10);
			   x += CNPJ. substring (11,15);
			   x += CNPJ. substring (16,18);
			   CNPJ = x; 
	   } else {
			   CNPJ = CNPJ. replace (".","");
			   CNPJ = CNPJ. replace (".","");
			   CNPJ = CNPJ. replace ("-","");
			   CNPJ = CNPJ. replace ("/","");
	   }
	   var nonNumbers = /\D/;
	   if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \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 +="Dígito verificador com problema!";
	   }
	   if (erro.length > 0){
			
			   erro = "CNPJ inválido"
			   return erro;
	   }
	   return true;
}


