// JavaScript
function muda_valor_hf(hf_id, valor){
	var elem = document.getElementById(hf_id);
	elem.value = valor;
}

function verifica_checked(n_itens){
	for(var i=0; i<n_itens; i++){
		var elem = document.getElementById('itemid['+ i +']');
		if(elem.checked){
			return true;
		}
	}
	return false;
}

<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
//-->

function abrir(pagina, w, h){
	if(!w){w=400;}if(!h){h=150;}
	var janela=window.open(pagina,"","width="+w+",height="+h+", scrollbars=yes, resizable=yes, location=no");
    //interceptacao de erro na abertura da janela
	var text = "Bloqueador de pop-up ativo! \nDesative-o para escolher a imagem!";
	if(janela == null) { alert(text); return; }
	//fim
}

//funcao para enviar uma imagem para o formulario
function send(imag, dir){
    var el = window.opener.document.getElementById("hidden_img");//hidden field
    var img = window.opener.document.getElementById("Imagem");//img field
	el.value = imag;
    img.setAttribute("src", dir);
    self.close();
}

//funcao para enviar um nome de artista e id para o formulario
function send_nome(namefield, nome, idfield, id){
    var el = window.opener.document.getElementById(idfield);//hidden field
    var fnome = window.opener.document.getElementById(namefield);//text field
	el.value = id;
	fnome.value = nome;
    self.close();
}


function prevent(evt){
	if(!evt){
	   evt = this.event;
	}
	evt.preventDefault();
	evt.returnValue = false;
}

//troca o valor de um campo (elemid) pelo valor de val
function seta_valor(elemid, val){
	var elem = document.getElementById(elemid);
	elem.value = val;
}


//mostra ou esconde um elemento seguindo o status de um checkfield
function check_mostra(id, checkid){
	var div = document.getElementById(id);
	var chk = document.getElementById(checkid);
	
	if(chk.checked){
		div.style.display = "";
	}else{
		div.style.display = "none";
	}
}

//limpa o formulario
function limpaform(form){
	for(var i=1; i < form.length; i++){
		switch (form.elements[i].type){
			case "text":
				form.elements[i].value = "";
				break;
			case "checkbox":
			case "radio":
				form.elements[i].checked = false;
				break;
		}
	}
}


//seleciona todos os checkboxes do formulario copiando o estado do elemento de referencia
function selectall(form, ref_el, start){
	var chk = ref_el.checked;
	for(var i=start; i < form.length; i++){
		if(form.elements[i].type == "checkbox"){
			form.elements[i].checked = chk;
		}
	}
}


//inverte o estado de todos os checkboxes do formulario
function invertall(form, start){
	document.getElementById('selall').checked = false;
	for(var i=start; i < form.length; i++){
		if(form.elements[i].type == "checkbox"){
			var chk = form.elements[i].checked;
			form.elements[i].checked = !(chk);
		}
	}
}

/** MENU ***********************************************
--------------------------------------------------------------------*/

//Inclui alterações no menu para eventos do mouse
function add_menu_events(elink){
	var el = document.getElementById('menu_' + elink);
	
	function ativa(){
		el.className += ' ativo';
	}
	function desativa(){
		el.className = 'menu_link';
	}
	function redirect(){
		window.location=('?p='+elink);
	}
	
	// Adicionando eventos no Netscape e Mozilla
	if (window.addEventListener){
		el.addEventListener("mouseover", ativa, false); //ativa estilo
		el.addEventListener("mouseout", desativa, false); //desativa estilo
		el.addEventListener("click", redirect, false); //ativa estilo
	}
	
	// Adicionando eventos no IE
	if (window.attachEvent){
		el.attachEvent("onmouseover", ativa); //ativa estilo
		el.attachEvent("onmouseout", desativa); //desativa estilo
		el.attachEvent("onclick", redirect); //link		
	}
}


/*** INTERFACE DE FORMULARIOS ***********************************
-----------------------------------------------------------------*/

//altera o estilo de um campo do formulario ao focar o elemento
function ativa(elem){
	var el = document.getElementById(elem);	
	el.className += " destaque";
}
//reverte o estilo de um campo do formulario ao perder o foco
function desativa(elem){
	var el = document.getElementById(elem);	
	var classname 	= "";
	var classe 		= el.className.split(" ");
	classe.pop();
	for(var i=0; i<classe.length; i++){
		classname += classe[i] + " ";
	}
	el.className = classname;
}

//Confirma opção de apagar arquivo e em caso afirmativo, submete o formulario
function apagar_arquivo(formid, filename){
	if(confirm("Você deseja realmente apagar o arquivo '" + filename + "' do servidor ?")){
		myform = document.getElementById(formid);
		myform.submit();
	}	
}

function verifica_chk_confirm_deletion(hf, value1, value2, n_itens){	
	var check = verifica_checked(n_itens);
	
	if(check && confirm("Você deseja apagar os arquivos referentes aos downloads selecionados ?\n Ok = sim\n Cancelar = não")){
		muda_valor_hf(hf, value1);
	}else{
		muda_valor_hf(hf, value2);
	}
	
	return check;
}



