function highlightTableRows(tableId) {
    var previousClass = null;
    var table = document.getElementById(tableId);
    if(!table)
    	return;
    	
    var startRow = 0;
    // workaround for Tapestry not using thead
    if (!table.getElementsByTagName("thead")[0]) {
	    startRow = 1;
    }
    
    var tbody = table.getElementsByTagName("tbody")[0];
    var rows = tbody.getElementsByTagName("tr");
	
	if(rows && rows.length>0 && rows[0].onmouseover!=null)
		return;
	
    // add event handlers so rows light up and are clickable
    for (i=startRow; i < rows.length; i++) {
        rows[i].onmouseover = function() { previousClass=this.className;this.className+=' over' };
        rows[i].onmouseout = function() { this.className=previousClass };
        rows[i].onclick = function() {
            var cell = this.getElementsByTagName("td")[0];
            var link = cell.getElementsByTagName("a")[0];
            if (link.onclick) {
                call = link.getAttribute("onclick");
                if (call.indexOf("return ") == 0) {
                    call = call.substring(7);
                }
                // this will not work for links with onclick handlers that return false
                eval(call);
            } else {
                location.href = link.getAttribute("href");
            }
            this.style.cursor="wait";
            return false;
        }
    }
    return;
}

function highlightTableRowsSet(tableId,colunaLink,colInicial,colFinal) {
    var previousClass = null;
    var table = document.getElementById(tableId); 
    var tbody = table.getElementsByTagName("tbody")[0];
    var rows = tbody.getElementsByTagName("tr");
   
	   if(rows && rows.length>0 && rows[0].onmouseover!=null)
		return;
    // add event handlers so rows light up and are clickable
    for (i=0; i < rows.length; i++) {
		
		var corFundo;
		
        rows[i].onmouseover = function() {
			
			previousClass=this.className;this.className+=' over' ;
			
			this.style.cursor = "pointer";
				
				for(j=colInicial; j <= colFinal; j++){
					cells = this.getElementsByTagName("td");
					if(!cells)return;
					celLink = cells[colunaLink];
					alink = celLink.getElementsByTagName("a")[0];
						
					cells[j].onclick = function(){
						//var cell = celLink;
						if (celLink.getElementsByTagName("a").length > 0) {
							
							if (alink.onclick) {
								call = alink.getAttributeValue("onclick");
								// this will not work for links with onclick handlers that return false
								eval(call);
							} else {
							  location.href = alink.getAttribute("href");
							}
							this.style.cursor="wait";
						}
					}
				}
			};

		rows[i].onmouseout = function() { this.className=previousClass; };
        /*
		rows[i].onclick = function() {
            var cell = this.getElementsByTagName("td")[colunaLink];
            if (cell.getElementsByTagName("a").length > 0) {
                var link = cell.getElementsByTagName("a")[0];
                if (link.onclick) {
                    call = link.getAttributeValue("onclick");
                    // this will not work for links with onclick handlers that return false
                    eval(call);
                } else {
                  location.href = link.getAttribute("href");
                }
                this.style.cursor="wait";
            }
        }
		*/
    }
    return;
}

// nameObject virá setado caso o nome do objeto mapeado no action for diferente do 
// nome do proprio arquivo action. Por exemplo: ConteudoNoticiaAction não possui um
// objeto conteudonoticia.
function confirmaExcluir(action, id, nameObject){
	if(confirm('Tem certeza que deseja remover?')){
		var url;
		url = 'delete_'+action+'.html?';
		if(nameObject != null){
			url += nameObject;
		}else{
			url += action.toLowerCase();
		}
		url += '.id='+id
		document.location = url;
	}
}

function confirmaExcluirCategoria(action, id){
	if(confirm('Tem certeza que deseja remover esta categoria?')){
		document.location = 'delete_'+action+'.html?categoria.id='+id;
	}
}

function confirmaExcluirLote(id){
	if(confirm('Tem certeza que deseja remover todos os itens selecionados?')){
		document.location = $(id).action +'?'+$(id).serialize();
	}
}

function confirmaMudarStatus(methodAction, id){
	if(confirm('Tem certeza que deseja alterar o status?')){
		var idContainer = 'status'+id; 
		var status = 'APROVADO';
		if ($(idContainer).title == status){ // compara status atual com APROVADO
			status = 'PENDENTE';
		}
		$(idContainer).title = status;
		
		$(idContainer+'_img').innerHTML = '<center><img src="images/indicator.gif"></center>';
		new Ajax.Updater(idContainer+'_img', methodAction, {
		  parameters: getParametersUrl(idContainer)+'&id='+id+'&status='+status+'&img=true'
		});
	}
}

function confirmaExcluirArquivo(id, styleId, conteudoId){
	if(confirm('Tem certeza que deseja excluir o arquivo?')){
		$(styleId).innerHTML = '<img src="./images/indicator.gif">';
		new Ajax.Updater(styleId, 'delete_Arquivo.html?arquivo.id='+id+'&idConteudo='+conteudoId, {
			onComplete : function(){
				$(styleId).innerHTML = '<img src="./images/semImagem.jpg" class="noImage">';
			}	 
		});
		return true;
	}
	return false;
}

function confirmaMudarResponsavel(methodAction, id){
	if(confirm('Tem certeza que deseja alterar o respons\u00e1vel?')){
		var idContainer = 'responsavel'+id;
		var atual = ($(idContainer).title == 'Sim'? 'true' : 'false');
		var responsavel = 'true';
		if (atual == responsavel){
			responsavel = 'false';
		}
		$(idContainer).title = (responsavel == 'true'? 'Sim' : 'N\u00e3o');
		
		$(idContainer).innerHTML = '<center><img src="images/indicator.gif"></center>';
		new Ajax.Updater(idContainer, methodAction, {
		  parameters: 'id='+id+'&responsavel='+responsavel+'&img=true'
		});
	}
}

function confirmaExcluirAjax(evt, idContainer, methodAction, index, relation){
	if(confirm('Tem certeza que deseja remover?')){
		$(idContainer+'_dataTable').innerHTML = '<center><img src="images/indicator.gif"></center>';
		new Ajax.Updater(idContainer+'_dataTable', methodAction+'.html', {
		  parameters: 'relation='+relation+'&listIndex='+index+'&'+getParametersUrl(idContainer)
		});
	}
	evt.stop();
}

function executaValor(obj){
	eval(obj.value);
}

function getParametersUrl(idContainer){
	var parameters = '';
	
	var inputs = $$('#'+idContainer+' input');
	for(var i = 0 ;i < inputs.length; i++) {
		if(inputs[i].type == "text" || inputs[i].type == "hidden") {
			parameters += inputs[i].name+'='+inputs[i].value+'&';
		}
	}
	
	var selects = $$('#'+idContainer+' select');
	for(var i = 0 ;i < selects.length; i++) {
		parameters += selects[i].name+'='+selects[i].value+'&';
	}
	
	var textareas = $$('#'+idContainer+' textarea');
	for(var i = 0 ;i < textareas.length; i++) {
		parameters += textareas[i].name+'='+textareas[i].value+'&';
	}
	return parameters;
}

function formSubmit(evt, idContainer, methodAction){
	if(idContainer == null){
		$('tableForm').innerHTML = '<center><img src="images/indicator.gif"></center>';
		new Ajax.Updater('tableForm', $('filterForm').action, {
		  parameters: $('filterForm').serialize(true),
		  evalScripts: true
		});
	} else{
		$(idContainer+'_dataTable').innerHTML = '<center><img src="images/indicator.gif"></center>';
		new Ajax.Updater(idContainer+'_dataTable', methodAction+'.html', {
		  parameters: getParametersUrl(idContainer),
		  evalScripts: true
		});
	}
	if (evt) {
		evt.stop();
	}
}

function MM_formt(chr,src,mask, e) {
    var tecla = (window.event)?event.keyCode:e.which;
    if( ( tecla == 8 ) || ( tecla == 9 ) || ( tecla == 11 ) || ( tecla == 0 ) ) 
    	return true;
    	
	var i = src.value.length;
	var texto = mask.substring(i);
	
 	if (texto.substring(0,1) != chr) { 
 		src.value += texto.substring(0,1);
 	}
 	
 	return true; 
}	
	
function validaCampoFloat(e){
	var tecla=(window.event)?event.keyCode:e.which;
	var input;
	if(!e.srcElement)
		input = e.target; // firefox aceita, ie não
	else
		input = e.srcElement; // ie aceita, firefox não
	if((tecla == 44) && (input.value.indexOf(',') < 0)) // permite digitar virgula
		return true;
	else
		return SomenteNumero(e);
}
	
// verifica se é um numero, backspace ou tab e retorna true
function SomenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;
    if((tecla > 47 && tecla < 58))
    	return true;
    else{
	    if( ( tecla == 8 ) || ( tecla == 9 ) || ( tecla == 11 ) || ( tecla == 0 ) ) 
	    	return true;
	    else 
	    	return false;
	}
}

// Show the document's title on the status bar
window.defaultStatus=document.title;

// The following doesn't work in IE 6 or 7
// window.onload = StyleSheetSwitcher.initialize();
// window.onload = function() { StyleSheetSwitcher.initialize(); }

function enviar(id, wait, target, errorTarget, bgcolor){
	 var form = document.getElementById(id);
	
	 var indicador = document.getElementById(target);
	 if(indicador != null)
		indicador.innerHTML = "<img src='images/indicator.gif' style='vertical-align:middle;' border='0'/>";
	
	 var action = form.action;
     var tela = new sack(action);
	    
	 var fields = form.getElementsByTagName('input');
	 for (i =0; i< fields.length; i++){
		tela.setVar(fields[i].name,fields[i].value);
	 }
	 fields = form.getElementsByTagName('select');
	 for (i =0; i< fields.length; i++){
		tela.setVar(fields[i].name,fields[i].value);
	 }
	 fields = form.getElementsByTagName('textarea');
	 for (i =0; i< fields.length; i++){
		tela.setVar(fields[i].name,fields[i].value);
	 }
	 
	 
	 tela.onCompletion = function(){
		var resposta = tela.response;
		var obj = document.getElementById(target);
		obj.innerHTML = resposta;
		execJS(obj);
		}; 
	 tela.runAJAX();
	 
	 return false;
}

function loadFilter(idMarcador){
	if(idMarcador != null)
		document.getElementById('idMarcador').value = idMarcador;
		
	enviar('selectMarcador', 'docFilter', 'docFilter', 'docFilter', '#FFFFFF');
	
	return false;
}

function bloqBotoesSave(){
	var botoes = document.getElementsByName('method:save');
	for (var k in botoes){
		botoes[k].disabled = true;
	}
}

function desbloqBotoesSave(){
	var botoes = document.getElementsByName('method:save');
	for (var k in botoes){
		botoes[k].disabled = false;
	}
}

function toggleBuscaAvancada(botao){
	botao.blur();
	Element.toggleClassName('searchOption', 'opened');
	$('searchOptions').toggle();
	if($('searchOptions').style.display == ''){
		$('searchOption').update('Ocultar Busca Avan\u00e7ada');
	} else{
		$('searchOption').update('Exibir Busca Avan\u00e7ada');
	}
}

function publicar(pub, id, idCategoria){
		new Ajax.Request('publish_Conteudo.html', {
			method:'post',
			parameters: { publicado : pub, id: id},
		 
		  onSuccess: function(){
			refresh(idCategoria);
  		}
	});
}

function gerarPDF(idConteudo, idCategoria){
	window.location = 'gerarPDF_ConteudoAtividade.html?conteudo.id='+idConteudo;
}