function buscaAjax(base, formulario, url, div) {
	var ajax = new AjaxObject("POST", base + url);
	objRetorno = document.getElementById(div);
	ajax.addAllRequestObjects(document.forms[formulario]);
	objRetorno.innerHTML = "Carregando...";
	ajax.setCallBackFunction( function(resposta, retorno) {
		retorno.innerHTML = resposta;
	}, [ objRetorno ]);
	ajax.load();
}

function strlen (string) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Sakimori
    // +      input by: Kirk Strobeck
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +    revised by: Brett Zamir
    // %        note 1: May look like overkill, but in order to be truly faithful to handling all Unicode
    // %        note 1: characters and to this function in PHP which does not count the number of bytes
    // %        note 1: but counts the number of characters, something like this is really necessary.
    // *     example 1: strlen('Kevin van Zonneveld');
    // *     returns 1: 19
    // *     example 2: strlen('A\ud87e\udc04Z');
    // *     returns 2: 3
 
    var str = string+'';
    var i = 0, chr = '', lgth = 0;
 
    var getWholeChar = function (str, i) {
        var code = str.charCodeAt(i);
        var next = '', prev = '';
        if (0xD800 <= code && code <= 0xDBFF) { // High surrogate(could change last hex to 0xDB7F to treat high private surrogates as single characters)
            if (str.length <= (i+1))  {
                throw 'High surrogate without following low surrogate';
            }
            next = str.charCodeAt(i+1);
            if (0xDC00 > next || next > 0xDFFF) {
                throw 'High surrogate without following low surrogate';
            }
            return str[i]+str[i+1];
        } else if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate
            if (i === 0) {
                throw 'Low surrogate without preceding high surrogate';
            }
            prev = str.charCodeAt(i-1);
            if (0xD800 > prev || prev > 0xDBFF) { //(could change last hex to 0xDB7F to treat high private surrogates as single characters)
                throw 'Low surrogate without preceding high surrogate';
            }
            return false; // We can pass over low surrogates now as the second component in a pair which we have already processed
        }
        return str[i];
    };
 
    for (i=0, lgth=0; i < str.length; i++) {
        if ((chr = getWholeChar(str, i)) === false) {
            continue;
        } // Adapt this line at the top of any loop, passing in the whole string and the current iteration and returning a variable to represent the individual character; purpose is to treat the first part of a surrogate pair as the whole character and then ignore the second part
        lgth++;
    }
    return lgth;
}

function comentariosBlogAlivio(base , idPost){
	var ajax = new AjaxObject("POST", base + "/Clight/comentariosBlogAlivio");
	objRetorno = document.getElementById("BlogAlivioPostsRecentes");
	ajax.addRequestValue("idPost",idPost);
	objRetorno.innerHTML = "<div style='position:absolute; top:100px; left:130px; color:#FFFFFF; font-family: Trebuchet MS;'>Carregando...</div>";
	document.getElementById("BlogAlivioComente").style.display = "block";
	document.getElementById("linkComentar").onclick = function() {
		blogAlivio(base+'/Clight/controladorBlogAlivio/comentar/'+idPost,'BlogAlivioAba3');
	}
	ajax.setCallBackFunction( function(resposta, retorno) {
		retorno.innerHTML = resposta;
		//alert(resposta);
		var fatorDesconto = parseInt((substr_count(resposta,"class=\"BlogAlivioComents\"") * 120) + 155);
		var tamanho = (strlen(resposta) - fatorDesconto);
		var qtdLinhas = parseInt(tamanho/55);
		var tamanhoDiv = parseInt(qtdLinhas/14);
		var h = parseInt(230 * tamanhoDiv);
		h = -h;
		
		scroller2  = new jsScroller(document.getElementById("News2"), 400, h, "Scroller-Container");
		scrollbar2 = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller2, true, scrollbarEvent, "Scrollbar-Up", "Scrollbar-Down", "Scrollbar-Track", "Scrollbar-Handle");
		
	}, [ objRetorno ]);
	
	var container = '<div id="Scrollbar-Container">'+
	'<div class="Top" style="position: absolute; left: -78px;"></div>'+
	'<div class="Scrollbar-Up" style="position: absolute; top: 212px; left: -78px;"></div>'+
	'<div class="Scrollbar-Down" style="position: absolute; top: 228px; left: -78px;"></div>'+
	'<div class="Scrollbar-Track" style="position: absolute; top: 8px; left: -78px; height: 205px;">'+
		'<div class="Scrollbar-Handle"></div>'+
	'</div>'+
	'</div>';
	document.getElementById('betaContainer').innerHTML = container;	
	ajax.load();
}

function fecharAba(div){
	document.getElementById("BlogAlivioAba1").style.display = "none";
	document.getElementById("BlogAlivioAba2").style.display = "none";
	document.getElementById("BlogAlivioAba3").style.display = "none";
}

function comentarBlogAlivio(url,formulario,div){
	if(document.getElementById("nome").value == ""){
		alert("Campo nome Obrigatório."); 
		document.getElementById("nome").focus();
		return;
	}else if(document.getElementById("cidade").value == ""){
		alert("Campo Cidade Obrigatório."); 
		document.getElementById("cidade").focus();
		return;
	}else if(document.getElementById("uf").value == ""){
		alert("Campo UF Obrigatório."); 
		document.getElementById("uf").focus();
		return;
	}else if(document.getElementById("email").value == ""){
		alert("Campo E-mail Obrigatório."); 
		document.getElementById("email").focus();
		return;
	}else if(document.getElementById("comentario2").value == ""){
		alert("Campo Comentário Obrigatório."); 
		document.getElementById("comentario2").focus();
		return;
	}
	
	var ajax = new AjaxObject("POST", url);
	ajax.addAllRequestObjects(document.forms[formulario]);
	document.getElementById("comentarioAuxiliar").value = document.getElementById("comentario2").value;
	objRetorno = document.getElementById(div);
	objRetorno.innerHTML = "<div class=\"BlogAlivioCarregando\" style='position:absolute; top:100px; left:100px;'>Carregando...</div>";
	ajax.setCallBackFunction( function(resposta, retorno) {
		retorno.innerHTML = resposta;
	}, [ objRetorno ]);
	ajax.load();
	
}
function postarBlogAlivio(url,formulario,div){
	if(document.getElementById("cidade").value == ""){
		alert("Campo Cidade Obrigatório."); 
		document.getElementById("cidade").focus();
		return;
	}else if(document.getElementById("uf").value == ""){
		alert("Campo Uf Obrigatório."); 
		document.getElementById("uf").focus();
		return;
	}else if(document.getElementById("titulo").value == ""){
		alert("Campo Título Obrigatório."); 
		document.getElementById("titulo").focus();
		return;
	}else if(document.getElementById("conteudo").value == ""){
		alert("Campo conteúdo Obrigatório."); 
		document.getElementById("conteudo").focus();
		return;
	}
	
	var ajax = new AjaxObject("POST", url);
	document.getElementById("hiddenConteudo").value = document.getElementById("conteudo").value;
	ajax.addAllRequestObjects(document.forms[formulario]);
	/* isto só funcionará p/ IE */
	//ajax.addRequestValue('conteudo2', document.getElementById("conteudo").value); 
	objRetorno = document.getElementById(div);
	objRetorno.innerHTML = "<div class=\"BlogAlivioCarregando\" style='position:absolute; top:100px; left:100px;'>Carregando...</div>";
	
	ajax.setCallBackFunction( function(resposta, retorno) {
		retorno.innerHTML = resposta;
	}, [ objRetorno ]);
	ajax.load();
}

function blogAlivio(url,div){
	if(div == "BlogAlivioAba1"){
		document.getElementById(div).style.display = "block";
		document.getElementById("BlogAlivioAba2").style.display = "none";
		document.getElementById("BlogAlivioAba3").style.display = "none";
	}else if(div == "BlogAlivioAba2"){
		document.getElementById(div).style.display = "block";
		document.getElementById("BlogAlivioAba1").style.display = "none";
		document.getElementById("BlogAlivioAba3").style.display = "none";
	}else if(div == "BlogAlivioAba3"){
		document.getElementById(div).style.display = "block";
		document.getElementById("BlogAlivioAba1").style.display = "none";
		document.getElementById("BlogAlivioAba2").style.display = "none";
	}

	var ajax = new AjaxObject("POST", url);
	objRetorno = document.getElementById(div);
	objRetorno.innerHTML = "<div class=\"BlogAlivioCarregando\" style='position:absolute; top:100px; left:100px;'>Carregando...</div>";
	ajax.setCallBackFunction( function(resposta, retorno) {
		retorno.innerHTML = resposta;
	}, [ objRetorno ]);
	ajax.load();
}

function logarBlogAlivio(url,formulario,div){
	var ajax = new AjaxObject("POST", url);
	objRetorno = document.getElementById(div);
	ajax.addAllRequestObjects(document.forms[formulario]);
	objRetorno.innerHTML = "<div class=\"BlogAlivioCarregando\" style='position:absolute; top:100px; left:100px;'>Carregando...</div>";
	ajax.setCallBackFunction( function(resposta, retorno) {
		vetRetorno  =  resposta.split("!#!");
		if( vetRetorno[0] != undefined &&  vetRetorno.length > 1)
			exibeDivLogin(vetRetorno[0]);

		if(vetRetorno.length == 1 )
			retorno.innerHTML = resposta;
		else
			retorno.innerHTML = vetRetorno[1];
	}, [ objRetorno ]);
	ajax.load();
}

function postsBlogAlivio(base, tag){
	document.getElementById("BlogAlivioComente").style.display = "none";
	var ajax = new AjaxObject("POST", base + "/Clight/postsBlogAlivio"+"/"+tag);
	objRetorno = document.getElementById("BlogAlivioPostsRecentes");
	
	objRetorno.innerHTML = "<div class=\"BlogAlivioCarregando\" style='position:absolute; top:100px; left:150px;'>Carregando...</div>";
	ajax.setCallBackFunction( function(resposta, retorno) {
		retorno.innerHTML = resposta;
	}, [ objRetorno ]);
	
	document.getElementById('listagemBlogueiras').style.top = 0;
	document.getElementById('betaContainer').innerHTML = '';
	ajax.load();
}
function substr_count( haystack, needle, offset, length ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // *     example 1: substr_count('Kevin van Zonneveld', 'e');
    // *     returns 1: 3
    // *     example 2: substr_count('Kevin van Zonneveld', 'K', 1);
    // *     returns 2: 0
    // *     example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10);
    // *     returns 3: false
 
    var pos = 0, cnt = 0;
 
    haystack += '';
    needle += '';
    if(isNaN(offset)) offset = 0;
    if(isNaN(length)) length = 0;
    offset--;
 
    while( (offset = haystack.indexOf(needle, offset+1)) != -1 ){
        if(length > 0 && (offset+needle.length) > length){
            return false;
        } else{
            cnt++;
        }
    }
 
    return cnt;
}

function listagemBlogueiras(select,url,div){
	var ajax = new AjaxObject("POST", url);
	ajax.addRequestValue("idBlogueira",select.value);
	objRetorno = document.getElementById(div);
	ajax.setCallBackFunction( function(resposta,objRetorno){
		//alert(resposta);
		var qtd = substr_count(resposta,"class=\"foto\"");
		var divisao = parseInt(qtd/3);
		var h = parseInt((180*divisao)-280);
		h = -h;
		scroller1  = new jsScroller(document.getElementById("News1"), 245, h, "Scroller-Container1");
		scrollbar1 = new jsScrollbar (document.getElementById("Scrollbar-Container1"), scroller1, true, scrollbarEvent1, "Scrollbar-Up1", "Scrollbar-Down1", "Scrollbar-Track1", "Scrollbar-Handle1");
		objRetorno.innerHTML = resposta;
	},[objRetorno]);
	var container = '<div id="Scrollbar-Container1">'+
	  '<div class="Top1" style="position: absolute; top: -45px; left: -203px;"></div>'+
	  '<div class="Scrollbar-Up1" style="position: absolute; top: 85px; left: -203px;"></div>'+
	  '<div class="Scrollbar-Down1" style="position: absolute; top: 102px; left: -203px;"></div>'+
	  '<div class="Scrollbar-Track1" style="position: absolute; top: -35px; left: -203px; height: 123px;">'+
	  		'<div class="Scrollbar-Handle1"></div>'+
	  '</div>'+
	  '</div>';
	document.getElementById('testeContainer').innerHTML = container;
	ajax.load();
}

function paginacaoAjax(base, url, div) {
	var ajax = new AjaxObject("POST", base + url);
	objRetorno = document.getElementById(div);
	//objRetorno.innerHTML = "Carregando...";
	ajax.setCallBackFunction( function(resposta, retorno) {
		retorno.innerHTML = resposta;
		/*h = document.getElementById("test").clientHeight;*/
		scroller  = new jsScroller(document.getElementById("News"), 400, 150, "Scroller-Container");
		scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, true, scrollbarEvent, "Scrollbar-Up", "Scrollbar-Down", "Scrollbar-Track", "Scrollbar-Handle");
	}, [ objRetorno ]);
	ajax.load();
}

function mascaraDataNascimento(tipo) {

	Dia = document.getElementById('nascimentoDia');
	Mes = document.getElementById('nascimentoMes');
	Ano = document.getElementById('nascimentoAno');

	if (tipo == "d")
		if (Dia.value.length == 2)
			Mes.focus();

	if (tipo == "m")
		if (Mes.value.length == 2)
			Ano.focus();
}

function isArray(x) {
	return ((y = typeof (x)) == 'object') ? ((x.length > -1) ? true : false) : y;
}


/**
 * Esta função valida campos de um formulário HTML.
 * Pode ser passado qualquer quantidade de parâmetros, ou seja, independe da quantidade de campos a serem validados.
 * Para validar um campo de senha (com campo confirmar senha) deve ser utilizada a seguinte sintaxe:
 * validaCampos(arg1, arg2, new Array(senha, confirmaSenha), formulario);
 * O objeto formulario SEMPRE deve ser passado como ultimo parâmetro da função para que possa ser submetido.
 * 
 */
function validaCampos(){
	var senha = "";
	var campoChecked = false;
	for ( var i = 0; i < validaCampos.arguments.length - 1; i++) {
		var campo = validaCampos.arguments[i];
		if (isArray(campo)){
			if(campo.type == 'select-one'){
				if((campo.value == "")){
					alert("Os campos com (*) devem ser preenchidos!");
					return false;
				}					
			}else{
				for ( var properties in campo) {
					if (campo[properties].type === "password") {						
						if (campo[properties].value == ""){
							alert('O campo senha deve ser preenchido corretamente!');
							return false;
						}
						else if ((senha != "") && (senha != campo[properties].value)){
							alert('O campo senha deve ser preenchido corretamente!');
							return false;						
						}else
							senha = campo[properties].value;
					} else {
						campoChecked = true;
						var checked = false;
						if ((campo[properties].checked))
							checked = true;
					}
				}
			}
			if ( (!checked) && (campoChecked)){
				alert("Os campos com (*) devem ser preenchidos!");
				return false;
			}
		}
		if ( ((campo.type === "text") || (campo.type === "password")) && (campo.value == "")) {
			alert("Os campos com (*) devem ser preenchidos!");
			return false;
		}
	}
	var formulario = validaCampos.arguments[validaCampos.arguments.length-1];
	document.forms[formulario].submit();
}

function validaUsuario(formulario) {
	document.formulario.submit();
}

function transferefoco(tamatual,tamanhomax,proximo) {
	if(tamanhomax == tamatual){
		document.getElementById(proximo).focus();
	}
}
function ativasubmenu() {
	document.getElementById('submenuid1').style.display='block';
	document.getElementById('submenuid2').style.display='block';
	document.getElementById('submenuEsp').style.display='block';
}
function desativasubmenu() {
	document.getElementById('submenuid1').style.display='none';
	document.getElementById('submenuid2').style.display='none';
	document.getElementById('submenuEsp').style.display='none';
}
function ativalogar() {
	document.getElementById('logar').style.display='block';
	document.login.email1.focus();
}
function desativalogar() {
	/*document.getElementById('login').style.display='block';
	document.getElementById('esquecisenha').style.display='none';*/
	document.getElementById('logar').style.display='none';
}
function esquecisenhaAtivaDiv() {
	document.getElementById('login').style.display='none';
	document.getElementById('esquecisenha').style.display='block';
}
function esquecisenhaDesativaDiv() {
	document.getElementById('login').style.display='block';
	document.getElementById('esquecisenha').style.display='none';
}

function exibeCampoApelido(check){
	document.getElementById('apelidoText').value ="";
	if(check) { 
		document.getElementById('_apelido').style.display = "block";
	}else{
		document.getElementById('_apelido').style.display = "none";
	}
}

function Trim(str){
	return str.replace(/^\s+|\s+$/g,"");
}

function ge(nome){
	return document.getElementById(nome);
}
function validaCampoComentario(){
	var erro = false;
	if(ge("apelido").checked == true ){
		if( Trim(ge("apelidoText").value) == ""){
			alert("Por favor, preencha o campo Apelido");
			ge("apelidoText").focus();
			erro = true;
		}
	}
	
	if(Trim(ge("comentario").value) == "" && erro == false){
		alert("Por favor, preencha o campo comentário");
		ge("comentario").focus();
		erro = true;
	}
	
	if(erro== false){
		document.form_comentario.submit();
	}
}

function esqueciMinhaSenha(base,idPost){
	/*
	var html = "<div class='descricaoLogin'>Informe seu e-mail no campo abaixo.</div>";
	html += "<div class='BlogAlivioFormLogin' id='formulario_login'>";
	html += "<form name='formulario' method='post' action='"+base+"/Clight/esqueciSenha'>";
	html += "<div style='float: left; padding-bottom: 3px;'>";
	html += "<div class='login'>E-mail:</div>";
	html += "<div class='bg_input' style='float: left;'><input type='text' name='email_senha' id='login'></div></div>";
	html += "</form></div>";
	html += "<div class='bt_ok_cc'><img src='"+base+"/visao/imagens/culpa_cronica/btn_ok_peq.gif' title='OK' alt='OK' onClick='validaCampos(\"formulario\");'></div>";
	*/
	var html = "";
	html = "<div class='descricaoLogin'>";
	html += "Informe seu e-mail no campo abaixo:";
	html += "</div>";
	html += "<div class='formulario_login' id='formulario_login'>";
	html += "<form name='formulario' method='post' action='"+base+"/Clight/esqueciSenha'>";
	html += "<div style='float: left; padding-bottom: 3px;'>";
	html += "<div class='login'>E-mail:</div>";
	html += "<div class='bg_input' style='float: left;'><input type='text' name='email_senha' id='login'></div>";
	html += "</div>";
	html += "<div style='clear: left;'>";
	html += "</div>";
	html += "</form>";
	html += "</div>";
	html += "<div class='bt_ok_cc'>";
	html += "<img src='"+base+"/visao/imagens/culpa_cronica/btn_ok_peq.gif' title='OK' alt='OK' onClick='validaCampos(\"formulario\");'>";
	html += "</div>";
	
	document.getElementById('login_cc').innerHTML = html;	
}

function esqueciMinhaSenha2(base){
	
	var html = "<h2 class='BlogAlivioCriarPostTitulo'>Esqueci minha senha</h2>";
	html += "<div class='BlogAlivioCriarPostExplica'>Informe seu e-mail no campo abaixo.</div>";
	html += "<div class='BlogAlivioFormEsqueci' id='formulario_login'>";
	html += "<form name='formulario' method='post' action='"+base+"/Clight/esqueciSenha'>";
	html += "<div style='clear: left;'>";
	html += "<div class='BlogAlivioText'>E-mail:</div>";
	html += "<div class='bg_input'><input type='text' name='email_senha' id='login'></div>";
	html += "</form></div>";
	html += "<div class='BlogAlivioFechar'><a onclick='fecharAba();'></a></div>";
	html += "<div class='BlogAlivioBotaoOk'><a onClick='validaCampos(\"formulario\");'></div>";
	
	document.getElementById('login_cc').innerHTML = html;
}

function BlogBlogAlivioPosts() {
	document.getElementById('BlogAlivioPostsRecentes').className="BlogAlivioPostsRecentes_v";
	document.getElementById('BlogAlivioPostCarregado').className="BlogAlivioPostCarregado_i";
	document.getElementById('BlogAlivioComentariosPost').className="BlogAlivioComentariosPost_i";
}
function BlogBlogAlivioPost() {
	document.getElementById('BlogAlivioPostsRecentes').className="BlogAlivioPostsRecentes_i";
	document.getElementById('BlogAlivioPostCarregado').className="BlogAlivioPostCarregado_v";
	document.getElementById('BlogAlivioComentariosPost').className="BlogAlivioComentariosPost_v";
}


function exibeDivLogin(nomeUsuario){
	conteudo  = document.getElementById("logado").innerHTML + nomeUsuario;
	document.getElementById("logado").innerHTML = conteudo;
	document.getElementById("logado").style.display="block";
	document.getElementById("logarSair").style.display="block";
	document.getElementById("mudarsenha").style.display="block";
	document.getElementById("mnli").style.display="none";
	desativalogar() ;	
}

function preloadImages(){
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function siteexterno(urlexterna){
	document.getElementById('disclaimer').style.display="none";
	if(urlexterna){
		window.open(urlexterna,'_blank');
	}
}
function disclaimer(urlexterna){
	document.getElementById("disclaimer").style.display="block";
	html = "<div class=\"DisclaimerTitulo\">Disclaimer</div><div class=\"TextoDisclaimer\">Lorem ipsum dolor sit amet, donec praesent lorem semper sit tortor, erat sit arcu torquent neque phasellus malesuada, vehicula praesent consectetuer eu nullam viverra massa, in sociis feugiat. Vivamus duis aenean tincidunt fermentum suspendisse, leo magna tellus sit venenatis sed sapien, tellus parturient blandit blandit est ac proin, est elit nulla commodo, elit nulla felis justo aliquam. Nunc erat incidunt leo, quam lorem, tincidunt est tellus ligula tincidunt vel, adipiscing hymenaeos duis diam eu, quisque pede nunc vel commodo unde ut. Non at, ut vivamus condimentum, placerat et eget, in wisi. Sagittis nunc eu eget in, praesent ipsum sem ac adipiscing dolor porta, amet sem, at vestibulum mi eros nulla sem, sit nec lectus venenatis a cum. Urna sapien vestibulum, a arcu adipiscing arcu, eros mattis integer vestibulum mattis neque sed, tincidunt leo in ac proin risus risus.</div><div class=\"linhadisclaimer\"></div><div class=\"disclaimerCalcelar\"><a href=\"#\" onclick=\"siteexterno();\">Voltar</a></div><div class=\"disclaimerOk\"><a href=\"#\" onclick=\"siteexterno('" + urlexterna + "');\">Continuar</a></div>";
	document.getElementById('disclaimer').innerHTML = html;
}