window.onload = function() {
	
	get_imagem_randomica();
	
}

function get_imagem_randomica() {
	document.getElementById('imagem_randomica').src = URL_IMAGEM_RANDOMICA + '?id=' + Math.random();
}


function mudar_aba_comentario(index,classe) {
    // verificando a quantidade de abas
    var l_e_abas = document.getElementById('abas').getElementsByTagName('a');
    var quant_abas = document.getElementById('abas').getElementsByTagName('a').length;
	var class_inativa = '';
    var class_ativa = '';
    // testando o valor do index
    if(index > quant_abas || index < 0) throw('O index "' + index + '" é inválido');

    var v_class = document.all ? 'className' : 'class';
	if (classe != 'defalt'){ 
       class_inativa = 'menu_aba3_inativa';
       class_ativa = 'menu_aba3_ativa';
	}else{
		class_inativa = 'menu_aba_noticias aba_inativa_coment';
		class_ativa = 'menu_aba_noticias aba_ativa_coment';
	
	}
    // escondendo todos os blocos
    for(var i=1; i<=quant_abas; i++) {
        l_e_abas[i-1].setAttribute(v_class, class_inativa);
        document.getElementById('bloco_aba' + i).style.display = 'none';
    }
    // colocando a classe correta na aba selecionada
    l_e_abas[index-1].setAttribute(v_class, class_ativa);
    // mostrando o bloco especifico
    document.getElementById('bloco_aba' + index).style.display = 'block';
}


 function enviar_comentario(){
		
	var div_imagem_erro = document.getElementById('div_imagem_null');
	var div_imagem_null = document.getElementById('div_imagem_null');
	var div_comentario_null = document.getElementById('div_comentario_null');
	var div_ok = document.getElementById('div_ok');
	var div_erro = document.getElementById('div_erro');
	var div_email_invalido = document.getElementById('div_email_invalido');
	var div_nome_null = document.getElementById('div_nome_null');
	var email = document.getElementById('email');
	var nome = document.getElementById('nome');
	var comentario =  document.getElementById('comentario');
	var cod_imagem =  document.getElementById('letras');
	var blog = document.getElementById('blog').value;

    
	if(nome.value == ''){
		div_nome_null.style.display='block'	;
		div_comentario_null.style.display = 'none';
		div_imagem_null.style.display = 'none';
		div_ok.style.display = 'none';
		div_erro.style.display = 'none' ;
		div_email_invalido.style.display = 'none' ;
	    nome.focus();
	} else if(!isEmail(email.value)){
		div_email_invalido.style.display = 'block';
		div_imagem_null.style.display = 'none';
		div_nome_null.style.display='none'	;
		div_comentario_null.style.display = 'none';
		div_ok.style.display = 'none';
		div_erro.style.display = 'none' ;
		email.focus();
	}else if(comentario.value == ''){
		div_email_invalido.style.display = 'none';
		div_imagem_null.style.display = 'none';
		div_nome_null.style.display='none'	;
		div_comentario_null.style.display = 'block';
		div_ok.style.display = 'none';
		div_erro.style.display = 'none' ;
		comentario.focus();		
			
	}else if(cod_imagem.value == ''){
		div_imagem_null.style.display = 'block';
		div_email_invalido.style.display = 'none';
		div_nome_null.style.display='none'	;
		div_comentario_null.style.display = 'none';
		div_ok.style.display = 'none';
		div_erro.style.display = 'none' ;	
		cod_imagem.focus();
	}else{
		
		div_imagem_null.style.display = 'none';
		div_email_invalido.style.display = 'none';
		div_nome_null.style.display='none'	;
		div_comentario_null.style.display = 'none';
		div_ok.style.display = 'none';
		div_erro.style.display = 'none' ;
	
		var handler = new XMLHandler();
		var xmlreq = new XMLClient(URL_POSTAR, true);
		// setando para ser sincrono
		xmlreq.setAsync(false);
		handler.bdoAsync = false;
		xmlreq.addParam('nome',nome.value);
		xmlreq.addParam('email',email.value);
		xmlreq.addParam('comentario',comentario.value);
		xmlreq.addParam('codigo_imagem',cod_imagem.value);
		xmlreq.addParam('schema',SCHEMA);
		xmlreq.addParam('id_pk',ID_PK);
		xmlreq.addParam('tabela',TABELA);	
		if (blog.length > 7) xmlreq.addParam('site',blog);
		xmlreq.query(handler);                        
		var postado = parseInt(xmlreq.oXMLRequest.responseText);
		if(postado == 2){
			div_imagem_erro.style.display = 'block';
			div_ok.style.display = 'none';
		}else if(postado == 1){
			div_imagem_erro.style.display = 'none';
			div_ok.style.display = 'block';
			
			//limpa todos os campos
			nome.value = '';
			email.value = '';
			comentario.value = '';
			cod_imagem.value = '';
			get_imagem_randomica();
		} else if (postado == 3) {
			div_imagem_erro.style.display = 'none';
			div_ok.style.display = 'none';
			
			//limpa todos os campos
			nome.value = '';
			email.value = '';
			comentario.value = '';
			cod_imagem.value = '';
			alert('Esta página não está disponivel para comentários');
		}
		
			
	}
	
}

function verificar_caracteres(sender, e_contador) {
	var content = sender.value;
	if(document.all) sender.style.width = '225px'
	if(content.length >= 600) sender.value = content.substring(0, Noticia.Comentario._caracteres_max); 
	if(typeof e_contador == 'object') e_contador.innerHTML = sender.value.length;
	else document.getElementById(e_contador).innerHTML = sender.value.length;
}

