// JavaScript Document
function WM_toggle (id){
    if (document.all){
     if(document.all[id].style.display == 'none'){
       document.all[id].style.display = '';
     } else {
       document.all[id].style.display = 'none';
     }
  return false;

   } else if (document.getElementById){
     if(document.getElementById(id).style.display == 'none'){
       document.getElementById(id).style.display = 'block';

     } else {
       document.getElementById(id).style.display = 'none';
     }
  return false;

   }
  }
  
function confirmChanges () {
  
  if ((tinyMCE.getInstanceById('texto').isDirty() == true) || (tinyMCE.getInstanceById('intro').isDirty() == true)) document.form.isChanged.value = '1';
  //alert('changed: '+document.form.isChanged.value);
  // Temporario
  //document.form.isChanged.value = '1';
}
function confirmPublish (ver) {
 selObj = document.form.activo;
 
 if ((selObj.options[selObj.selectedIndex].value == '1')&&(document.form.isChanged.value == '1')) {
 var answer = (document.form.idcontent)? confirm('Foram detectadas alterações neste conteúdo.\nConfirma publicação imediata com estas alterações?') : confirm('Confirma publicação imediata deste conteúdo?\n(Ficará disponível imediatamente online)');
	if (answer) { 
	    return true; 
	    // document.getElementById('submit_publicar').value = "A processar...";
        // document.getElementById('submit_publicar').disabled = "disabled";
	} else { return false }
 }
}
function disableIt(obj) {
  obj.disabled = !(obj.disabled);
  var z = (obj.disabled) ? 'disabled' : 'enabled';
  //alert(obj.type + ' now ' + z);
}
function wpreview() { 
  //window.open('','previewcontent','menubar=yes,status=yes,scrollbars=yes,resizable=yes,width=800,height=580');
  this.form.target='previewcontent';
  this.form.action='content_preview.php';
}
function FillAlias () {
  s = document.form.titulo.value.toLowerCase();
  
// Retirar acentos e apenas caracteres alfanuméricos
  s = s.replace(/[áàã]+/g, 'a').replace(/[óòõô]+/g, 'o').replace(/[éèê]+/g, 'e').replace(/[íì]+/g, 'i').replace(/[úù]+/g, 'u').replace(/[ç]+/g, 'c').replace(/[^a-z A-Z]+/g, '');
  
// Apenas pega em palavras superiores a 3 caracteres
  var aliasString = "";
  var aa = s.split(/\s+/g); // split the sentence into an array of words
  for (i=0; i < aa.length ; i++) {
   if (aa[i].length >= 3) { aliasString += aa[i]+" "; };
  }
  
// Substitui espaços por hifens
 aliasString = aliasString.replace(/^\s*|\s*$/g, "");
 document.form.alias.value = aliasString.replace(/\s/g, "-");
}
function submitHome () {
	document.form.target = '_self';
    document.form.action = location.href;
}
function ajax_refreshContents (searchString) {
    agent.call('','listRelatedContents','ajax_refreshContents_callback', searchString);
	return false;
}

function ajax_refreshContents_callback (result) {
 // Actualiza DIV com resultados
 text = '<b>Conteúdos disponiveis para relacionar</b>:<br />';
 if (result == '') text += 'Não foram encontrados resultados. Tente de novo!';
 document.getElementById('relatedbox').innerHTML = text+result;
 WM_toggle ('waitstatus');
}  
