var tms = new Array();
var url_global = "http://www.portalsae.com.br/v1/";

var expreg = new Array();
expreg['texto'] = /\S/i;
expreg['categoria'] = /\S/i;
expreg['descricao'] = /\S/i;
expreg['nome_fantasia'] = /\S/i;
expreg['razao_social'] = /\D{2,} \D{2,}/i;
expreg['contato'] = /\S/i;
expreg['endereco'] = /\S/i;
expreg['bairro'] = /\S/i;
expreg['cidade'] = /\S/i;
expreg['login'] = /\w{3,}/i;
expreg['senha'] = /\w{4,}/i;
expreg['nome'] = /\D{2,} \D{2,}/i;
expreg['dia'] = /^[0-9]{2}/i;
expreg['mes'] = /^[0-9]{2}/i;
expreg['ano'] = /^[0-9]{4}/i;
expreg['inteiro'] = /\d/;
expreg['ddd'] = /^[0-9]{2}/i;
expreg['nr'] = /\d/;
expreg['email'] = /^[\w\.-]+@[\w\.-]+\.\w+$/i;
expreg['cep'] = /^[0-9]{5}[0-9]{3}$|^[0-9]{5}-[0-9]{3}$/;
expreg['data'] = /^[1-9]{2}\/[1-9]{2}\/[1-9]{4}$/;
expreg['data_ini'] = /^[1-9]{2}\/[1-9]{2}\/[1-9]{4}$/;
expreg['data_fim'] = /^[1-9]{2}\/[1-9]{2}\/[1-9]{4}$/;
expreg['estado'] = /\D{2}/i;
expreg['sexo'] = /M|F/;
expreg['cnpj'] = /^[0-9]{2}.[0-9]{3}.[0-9]{3}\/[0-9]{4}-[0-9]{2}$|^[0-9]{14}$/;
expreg['telefone'] = /^[0-9]{3,4}[0-9]{4}$|^[0-9]{3,4}-[0-9]{4}$|^\([0-9]{2,3}\) [0-9]{3,4}-[0-9]{4}$/;	
expreg['sala'] = /\S/i;
expreg['ano_s'] = /\S/i;
expreg['turno'] = /\S/i;
expreg['disciplina'] = /\S/i;
expreg['professor'] = /\S/i;
expreg['periodo'] = /\S/i;
expreg['turma'] = /\S/i;
expreg['media'] = /^[0-9]{2}/i;
expreg['perfil'] = /\S/i;	
expreg['file_csv'] = /^[\w\.-]+\.csv$/i;

var texto_erro = new Array();
texto_erro['texto'] = '';
texto_erro['categoria'] = 'Selecione uma categoria.';
texto_erro['descricao'] = 'Digite uma descric&ccedil;&atilde;o.';
texto_erro['nome_fantasia'] = 'Digite o nome fantasia.';
texto_erro['razao_social'] = 'Digite a raz&atilde;o social.';
texto_erro['contato'] = 'Digite onome de um contato.';
texto_erro['endereco'] = 'Digite um endere&ccedil;o.';
texto_erro['bairro'] = 'Digite o bairro.';
texto_erro['cidade'] = 'Digite uma cidade.';
texto_erro['login'] = 'O usu&aacute;rio deve conter mais de 3 caracteres.';
texto_erro['senha'] = 'A senha deve conter mais de 4 caracteres.';
texto_erro['nome'] = 'Digite o nome e sobrenome.';
texto_erro['dia'] = 'Digite um dia.';
texto_erro['mes'] = 'Digite um m&ecirc;s.';
texto_erro['ano'] = 'Digite um ano.';
texto_erro['ddd'] = 'Digite um DDD.';
texto_erro['nr'] = 'Digite o n&uacute;mero do endere&ccedil;o.';
texto_erro['email'] = 'Digite um email v&aacute;lido.';
texto_erro['cep'] = 'CEP inv&aacute;lido.';
texto_erro['data'] = 'Digite uma data v&aacute;lida.';
texto_erro['data_ini'] = 'Selecione uma data inicial.';
texto_erro['data_fim'] = 'Selecione uma data final.';
texto_erro['estado'] = 'Selecione um estado.';
texto_erro['sexo'] = 'Selecione um sexo.';
texto_erro['cnpj'] = 'CNPJ inv&aacute;lido.';
texto_erro['telefone'] = 'Digite um telefone para contato.';		
texto_erro['sala'] = 'Selecione uma sala.';
texto_erro['ano_s'] = 'Selecione um ano.';
texto_erro['turno'] = 'Selecione um turno.';
texto_erro['disciplina'] = 'Selecione uma disciplina.';
texto_erro['professor'] = 'Selecione um professor.';
texto_erro['periodo'] = 'Selecione um per&iacute;odo.';
texto_erro['turma'] = 'Selecione um turma.';
texto_erro['media'] = 'Digite uma m&eacute;dia v&aacute;lida.';
texto_erro['perfil'] = 'Selecione uma tipo de usu&aacute;rio.';
texto_erro['file_csv'] = 'Selecione um arquivo de texto separado por pon e virgula(;).';

function validatudo(form){
	var tipo;
	var my_erro='';
	var class_erro='campo_invalido';
	
	for(i=0;i<form.length;i++){
		tipo = form[i].getAttribute('tipo');
		if(tipo&&expreg[tipo]){
			form[i].getAttribute('class')==class_erro?form[i].setAttribute('class',''):false;
			if(!expreg[tipo].test(form[i].value)){
				my_erro+='<li>'+texto_erro[tipo]+'</li>';
				form[i].style.background = "#FFDDDD";
			}
		}
	}
	
	if(my_erro)
		return false;
	else
		return true;
	
}

function ver_msg(texto,tipo){
	if(texto){
		var divAviso=document.getElementById('divAviso');
		var conteudo=document.getElementById('divAviso_texto');
		
		divAviso.style.display='block';
		//divAviso.style.visibility='visible';
		if(!conteudo){
			var conteudo = document.createElement("div");
			conteudo.setAttribute('id','divAviso_texto');
			divAviso.appendChild(conteudo);
		}			
		conteudo.className=tipo?tipo:'aviso';
		conteudo.innerHTML=texto;
		
	}
}

function Verifica(form){
	var msg = "";
	
	for(i=0;i<form.length;i++){
		if(form[i].value=='' && form[i].getAttribute('name')!='redirect'){
			msg = "Preencha seu login e senha!";
		}
	}
	
	if(msg!=''){
		alert(msg);
		return false;
	}else{
		return true;
	}
}

function popUp(url,largura,altura){
	window.open (url,'PortalSAE','status=no,width='+largura+',height='+altura+',scrollbars=yes');
}

function AbreVideo(div,video){
	var dir_url = url_global+'swf/player.swf';
	var dir_skin = url_global+'swf/player_iesde.swf';
	var flash = new SWFObject(dir_url,div,'300','240','9','#ffffff');
	flash.addParam('allowfullscreen','true');
	flash.addParam('allowscriptaccess','always');
	flash.addParam('wmode','opaque');
	flash.addVariable('file',video);
	flash.addVariable('skin',dir_skin);
	flash.write(div);
}

function Evento(ano){
	window.location = url_global+"conteudos/encontros-pedagogicos/"+ano+".php";
}

function addFav(){
    var url      = "http://www.portalsae.com.br";
    var title    = "Portal SAE - Sistema de Apoio ao Ensino";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}

function setFocus(){
	var login = document.getElementById('login');
	var senha = document.getElementById('senha');
	if(login.value==''){
		login.focus();
	}else{
		senha.focus();
	}
}

function progresso_tecla(obj,div,max) {
	var progresso = document.getElementById(div);  
	/*if(obj.value.length+1>max) {
		obj.value = obj.value.substring(0, max);
	}*/
	progresso.innerHTML = "(Restam "+(max-obj.value.length)+" caracteres)";
	if(obj.value.length<=1){
		progresso.innerHTML = "";
	}
	retiraCaracteres(obj);
}

function retiraCaracteres(campo){
	var valor = campo.value;
	valor = valor.replace('%','');
	valor = valor.replace('{','');
	valor = valor.replace('}','');
	valor = valor.replace('<','');
	valor = valor.replace('>','');
	valor = valor.replace('£','');
	valor = valor.replace('¢','');
	valor = valor.replace('¬','');
	valor = valor.replace('§','');
	campo.value = valor;
}

function Termo(termo){
	if(termo.value=='Concordo'){
		window.location = url_global+"conteudos/cadastrese/confirma.php";
	}else{
		window.location = url_global+"index.php";
	}
}

function TermoEscola(termo){
	if(termo.value=='Concordo'){
		window.location = url_global+"conteudos/cadastrese/confirmaEscola.php";
	}else{
		window.location = url_global+"index.php";
	}
}

function TermoChave(termo){
	if(termo.value=='Concordo'){
		window.location = url_global+"conteudos/chave-acesso/confirma.php";
	}else{
		window.location = url_global+"index.php";
	}
}

function boletimPopUp(usuario_id, turma_id, matricula_id ){
	if(matricula_id){
		var url="http://www.portalsae.com.br/portalescola/boletim/printBol.php?TurID="+turma_id+"&MatID="+matricula_id+"&UsuID="+usuario_id;
		alert(url);
		//window.open(url, '_blank', 'scrollbars=no, toolbar=no');
	}else{
		alert("O aluno não esta matricula em uma turma!");
	}
}

function urlencode( str ) {
 
    var histogram = {}, tmp_arr = [];
    var ret = (str+'').toString();
 
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
 
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';
 
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
 
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
 
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
 
    return ret;
}

function urldecode( str ) {
 
    var histogram = {};
    var ret = str.toString();
 
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
 
    // The histogram is identical to the one in urlencode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';
 
    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing   
    }
 
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
 
    return ret;
}