var xmlhttp;
if (typeof XMLHttpRequest != 'undefined') {
	xmlhttp = new XMLHttpRequest();
}
else {
	xmlhtttp = new ActiveXObject("Microsoft.XMLHttp");
	if (!xmlhttp) {
		xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
	}
}

/* tratamento do editor */

var editor_comp = "";
var editor_objdiv = "";


function abrirPopup(url, larg, alt, x, y) {
  window.open(url, "_blank", "width=" + larg + ",height=" + alt + ",left=" + x + ",top=" + y + ",toolbar=1,menubar=0,scrollbars=1,resizable=1");
}

function abrirEditor(comp, objdiv) {
	if (!comp) comp = "conteudo";
	if (!objdiv) objdiv = "textoconteudo";
	editor_comp = comp;
	editor_objdiv = objdiv;
	
	janela = window.open("editor_conteudo.php", "editor", "width=800,height=550,scrollbars=1,resizable=0");
	janela.janelaPrincipal = this;
	janela.comp = comp;
	janela.objdiv = objdiv;
}

function respostaAjax(url) {
	xmlhttp.open("GET", url, false);
	xmlhttp.send(null);
	return xmlhttp.responseText;
}


function enviarDadosAjaxPost(url, parametros) {
    xmlhttp.open("POST", url, false);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(parametros);
	return xmlhttp.responseText;
}




function carregarImagem(imagem, objeto, largura, altura) {
	if (imagem == "") {
		document.getElementById(objeto).innerHTML = "";
	}
	else {
		var url = "imagem_obter.php?id=" + imagem;
		var resposta = respostaAjax(url);
		eval(resposta);
		if (ds_caminho_thumbnail) {
			document.getElementById(objeto).innerHTML = '<img src="' + ds_caminho_thumbnail + '" border="0">';
		}
		else {
			document.getElementById(objeto).innerHTML = '<img src="' + ds_caminho_imagem + '" border="0" width="' + largura + '" height="' + altura + '">';
		}
	}
}


function abrirPopupImagem(imagem, descricao) {
  var janelaImagem = window.open('','imagem','width=20,height=20,resizable=1');
  var fatoraltura = 130;
  var desc = "";
  if (descricao) {
	  desc = "<tr><td style=\"font: 11px arial;\" colspan=\"2\">" + descricao + "</td></tr>";
	  fatoraltura = 160;
  }

  html =   "<html>\n"
  		 + "<head>\n"
         + "</head>\n"
		 + "<body style=\"margin: 0;\" onload='window.resizeTo(document.getElementById(\"imagem\").width + 10, document.getElementById(\"imagem\").height + " + fatoraltura + ")'>\n"
		 + "<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n"
		 + "<tr><td colspan=\"2\">\n"
         + "<img src=\"" + imagem + "\" border=\"0\" id=\"imagem\" alt=\"\"/>\n"
		 + "</td></tr>"
		 + "<tr><td colspan=\"2\" height=\"10\"></td></tr>\n"
		 + "<tr><td align=\"left\" valign=\"middle\" width=\"50%\">\n"
		 + "<a class=\"textopadrao\" href=\"javascript: print();\"><img src=\"imgs/cliqueparaimprimir.gif\" alt=\"\" border=\"0\">\n"
		 + "</td><td align=\"right\" valign=\"middle\" width=\"50%\"><img src=\"imgs/logo_transparente.gif\" alt=\"\" border=\"0\"></td>\n"
		 + "</tr></table>\n"
         + "</body></html>\n";

  janelaImagem.document.open();
  janelaImagem.document.write(html);
  janelaImagem.document.close();

  janelaImagem.focus();
}

function abrirPopupFlash(flash, largura, altura) {

	var X = (screen.width - largura) / 2;
	var Y = (screen.height - altura) / 2;
	var janelaImagem = window.open('popup.php?tipo=flash&arquivo=' + flash + '&l=' + largura + '&a=' + altura,'imagem','width=' + largura + ',height=' + (altura - 70) + ',left=' + X + ',top=' + Y);
	janelaImagem.focus();  
}


function carregarComboCursos(cpf) {
	if(cpf == ""){
		alert("Informe o CPF.");
		return false;
	}
    //limpa o select
    var objSelect = document.getElementById('wcurso');
    while(objSelect.options.length > 0) objSelect.options[0]=null;
    objSelect.options[0]=new Option("Aguarde ... ","Aguarde ... ");

	var url = "ead_obter.php?cpf=" + cpf;
	var resposta = respostaAjax(url);
	eval(resposta);

	if(codigo != ""){
    	objSelect.options[0]=new Option("Selecione o curso","")
		for(i = 0; i < codigo.length; i++){
			objOption = document.createElement('OPTION');
			objOptionText = document.createTextNode(descricao[i]);
			objOption.setAttribute('value', codigo[i]);
			objOption.appendChild(objOptionText);
			objSelect.appendChild(objOption);
		}
		objSelect.disabled = false;
	}
	else{
		objSelect.options[0]=new Option("Cursos","")
		objSelect.disabled = true;
		alert("Este CPF não tem nenhum curso a distância cadastrado");
	}
}

function buscarCliente(cpf, semsenha) {
	if(cpf == ""){
		alert("Informe o CPF.");
		return false;
	}
	var url = "/meus_dados_obter_cliente.php?cpf=" + cpf;
	var resposta = respostaAjax(url);
	eval(resposta);
	
	if(a1_cpf != ""){
		
		if (semsenha == "1") {
			if (self.submit) {
				document.form_contato.a1_cod.value = a1_cod;
				document.form_contato.a1_loja.value = a1_loja;
				document.form_contato.a1_cgc.value = a1_cpf;
				self.submit("buscarClientePorCPFSenha");
			}
			else {
				return resposta;
			}
			
		}
		else {		
		
			document.form_contato.btnProcurarCPF.disabled = true;
			document.form_contato.senha.disabled = false;
			document.form_contato.senha.className = "caixatexto";
			alert("Você já é cadastrado(a). Por favor entre com sua senha");
			//document.getElementById('btnSubmit').innerHTML = "<input name=\"btnSubmit\" type=\"button\" onClick=\"self.submit('buscarCPF');\" class=\"caixatexto\" value=\"Prosseguir\">";
			document.getElementById('divsenha1').style.visibility = "visible";
			document.getElementById('divsenha2').style.visibility = "visible";
			document.getElementById('divsenha3').style.visibility = "visible";
			document.getElementById('CPFTrocaSenha').innerHTML = "<input type=\"hidden\" value=\"a1_cpf\" class=\"caixatexto\" name=\"CPFTrocaSenha\" maxlength=\"14\" size=\"20\" />";
	
			document.form_contato.senha.focus();
		}
	}
	else{
		if (document.form_contato) {
			alert("Você ainda não é cadastrado(a). Você vai ser redirecionado(a) para a página de cadastro agora.");
			document.form_contato.senha.className = "textodesabilitado";
			document.form_contato.senha.disabled = true;
			document.form_contato.CPF.focus();
			if (document.form_contato.retorno.value.indexOf("cpf%3D") == -1) {
				if (unescape(document.form_contato.retorno.value).indexOf("?") > -1) {
					document.form_contato.retorno.value += "%26";
				}
				else {
					document.form_contato.retorno.value += "%3F";
				}				
				document.form_contato.retorno.value += "cpf%3D" + cpf;
			}
			location.replace("meus_dados_form_incluir.php?cpf=" + cpf + "&retorno=" + document.form_contato.retorno.value);
		}
	}
	return true;
}

function buscarSenha(cpf, senha){
	if(cpf == ""){
		alert("Por favor informe o CPF.");
		return false;
	}
	if(senha == ""){
		alert("Por favor informe a senha.");
		return false;
	}
	var url = "meus_dados_validar_senha.php";
	var parametros = "cpf=" + cpf + "&senha=" + senha;
	
	var resposta = enviarDadosAjaxPost(url, parametros);
	eval(resposta);
	if(a1_cpf != "" && a1_senha != ""){
		if (typeof document.getElementById("a1_cod") != "undefined") {
			document.getElementById("a1_cod").value = a1_cod;
			document.getElementById("a1_loja").value = a1_loja;
			document.getElementById("a1_cgc").value = cpf;
		}
		self.submit("buscarClientePorCPFSenha");
		
		return true;
	}
	else{
		alert("Senha inválida.");
		return false;
	}
}


function buscaVerbeteFocus(sender) {
	if (sender.value.toLowerCase() == "dicionário de seguros" || sender.value.toLowerCase() == "dicion&aacute;rio de seguros" || sender.value.toLowerCase() == "verbete") {
		sender.value = "";
	}
}


function downloadArquivo(url) {
	if (url != "") {
		location.replace("download.php?url=" + url);
	}
}

function IsCPF(st) {

if (	st == "00000000000" || 
	st == "11111111111" || 
	st == "22222222222" || 
	st == "33333333333" ||
	st == "44444444444" ||
	st == "55555555555" ||
	st == "66666666666" ||
	st == "77777777777" ||
	st == "88888888888" ||
	st == "99999999999" ||
	st == "01234567890"  ) 
	return false;

if (st == "")
  return (false);
l = st.length;
st2 = "";
for (i = 0; i < l; i++) {
  caracter = st.substring(i,i+1);
  if ((caracter >= '0') && (caracter <= '9'));
     st2 = st2 + caracter;
}
if ((st2.length > 11) || (st2.length < 10))
   return (false);
if (st2.length==10)
   st2 = '0' + st2;
digito1 = st2.substring(9,10);
digito2 = st2.substring(10,11);
digito1 = parseInt(digito1,10);
digito2 = parseInt(digito2,10);
sum = 0; mul = 10;
for (i = 0; i < 9 ; i++) {
    digit = st2.substring(i,i+1);
    tproduct = parseInt(digit ,10) * mul;
    sum += tproduct;
    mul--;
}
dig1 = ( sum % 11 );
if ( dig1==0 || dig1==1 )
   dig1=0;
else
  dig1 = 11 - dig1;
if (dig1!=digito1)
  return (false);
sum = 0;
mul = 11;
for (i = 0; i < 10 ; i++) {
    digit = st2.substring(i,i+1);
    tproduct = parseInt(digit ,10)*mul;
    sum += tproduct;
    mul--;
}
dig2 = (sum % 11);
if ( dig2==0 || dig2==1 )
  dig2=0;
else
  dig2 = 11 - dig2;
if (dig2 != digito2)
  return (false);
return (true);
}
function trim(str) {
	return str.replace(/^\s+/,'').replace(/\s+$/,'');
}


function montarComboArray(objSelect, array, primeiroItem) {
	
    while(objSelect.options.length > 0) objSelect.options[0]=null;

	objSelect.options[0]=new Option(primeiroItem, "");
	for(i = 0; i < array.length; i++){
		objOption = document.createElement('OPTION');
		objOptionText = document.createTextNode(array[i][1]);
		objOption.setAttribute('value', array[i][0]);
		objOption.appendChild(objOptionText);
		objSelect.appendChild(objOption);
	}
	
}




function dataValida(dia, mes, ano) {

	var diaInt = parseInt(dia);
	var mesInt = parseInt(mes) - 1;
	var anoInt = parseInt(ano);
	
	var objData = new Date(anoInt, mesInt, diaInt);
	
	if ((objData.getMonth() == mesInt) && (objData.getDate() == diaInt) && (objData.getFullYear() == anoInt))
		return true;
	else
		return false;

}