function MM_findObj(n, d)
{
	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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers()
{
	var i,p,v,obj,args=MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
	if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
	obj.visibility=v; }
}

function Nume_NumeLink (campo)
{
	/* aplicando a regexp */
	
	link = campo.value;
	regexp = /<link href="([^"]*)" tipo="([^"]*)"( editoria="([^"]*)"|)( popup="([^"]*):([^"]*)"|)( target="([^"]*)"|)>/gi;
	tadelementos = link.match(regexp);


	/* variáveis encontradas */

	link = RegExp.$1;
	tipo = RegExp.$2;
	editoria = RegExp.$4;
	pop_a = RegExp.$7;
	pop_l = RegExp.$6;
	target = RegExp.$9;


	/* montando a query string */
	
	if (link != '')
	{
		query = '&tipo='+tipo+'&target='+target;
		
		if (tipo == 'l')
		{
			query = query + '&link_url='+link;
		}
		else if (tipo == 'c')
		{
			query = query + '&conteudo_pai='+link;
		}
		else if (tipo == 'm')
		{
			query = query + '&elemento_midia='+link;
		}
		else if (tipo == 'p')
		{
			query = query + '&last_pagina_id='+link+'&last_editoria_id='+editoria;
		}
	
		if (target == '_self')
		{
			query = query + '&pop_l='+pop_l+'&pop_a='+pop_a;
		}
	}
	else
	{
		query = '';	
	}

	
	/* chamando a janela com a query string */
	
	Nume_Janela ('link.php?elemento=1' + query,'','width=600,height=424,scrollbars=yes');
}

function Nume_TrocaImagem (nome, img_um, img_dois)
{
	rpcimagem = MM_findObj(nome);

	if (rpcimagem.src.indexOf (img_um) != -1)
	{
		nova_imagem = img_dois;
	}
	else
	{
		nova_imagem = img_um;
	}

	MM_swapImage (nome, '', nova_imagem, 1);
}

function Nume_LayerCombo (campo, nomelayer, valordesejado)
{
        rpccampo = eval ('document.formulario.' + campo);
	rpclayer = MM_findObj (nomelayer);

        if (rpccampo.value == valordesejado)
        {
                MM_showHideLayers (nomelayer,'','show');
                rpclayer.style['height'] = '';
        }
        else
        {
                MM_showHideLayers (nomelayer,'','hide');
		rpclayer.style['height'] = '1px';
	}
}

function Nume_TrocaRadio (campo, valor_proibido, prefixo, img)
{
	campo_objeto = eval (campo);

	if (campo_objeto.value != '' && campo_objeto.value != valor_proibido)
	{
		MM_swapImage (prefixo + campo_objeto.value, '', img, 1);
	}
}

function Nume_Logout()
{
	if (window.confirm ('Deseja mesmo sair do Nume CMS?'))
	{
		top.location.href='/cms/?logout=1';
	}
}

function Nume_Visualizar (url)
{
	/* grava o action antigo para restaurá-lo depois */
	action_antigo = document.formulario.action;

	/* abre pop-up vazio para armazenar o preview */
	Nume_Janela ('about:blank','janelapreview','width=800,height=600,top=10,left=10,scrollbars=yes');
	
	/* trocando os dados do formulário para que o submit seja no pop-up */
	document.formulario.action = "http://"+ url +"/conteudo.php?preview=1";
	document.formulario.target = "janelapreview";
	document.formulario.submit ();

	/* retornando o formulário ao seu estado anterior */
	document.formulario.action = action_antigo;
	document.formulario.target = "_self";
}

function Nume_ContaPalavra (campo, contacampo)
{
	contacampo.value = campo.value.length;
}

function Nume_RetornaValor (v1, v2)
{
	opener.document.formulario.assunto_id.value = v1;
    	opener.document.formulario.assunto_nome.value = v2;
    	self.window.close ();
}

function Nume_Janela (theURL,winName,features)
{ 
	var newWind = window.open(theURL,winName,features);
  	if (newWind.opener == null)
  	{
   		newWind.opener = self;
  	}
}

function Nume_LimpaCampo (t,t2)
{
	t.value = '';
	t2.value = '';
}

function Nume_CheckListCampo (fonte, imagem, prefixo)
{
    lista_alvo = eval (fonte);

    if (lista_alvo.value != '')
    {
	MM_swapImage (prefixo + lista_alvo.value, '', imagem, 1);
    }
}

function Nume_CheckList (fonte, imagem, prefixo)
{
    lista_alvo = eval (fonte + "_select");

    for (x=0; x < lista_alvo.length; x++)
    {
	if (lista_alvo.options[x].value != '0')
        {
		MM_swapImage (prefixo + lista_alvo.options[x].value, '', imagem, 1);
	}
    }
}

function Nume_PovoaLista (alvo)
{
    lista = eval (alvo);
    lista_alvo = eval (alvo + "_select");

    lista.value = '';

    for (x=0; x < lista_alvo.length; x++)
    {
	if (lista_alvo.options[x].value != '0')
        {
		lista.value = lista.value + ',' + lista_alvo.options[x].value;
        }
    }

    /* apaga a vírgula inicial da lista numérica */

    lista.value = lista.value.substr (1, lista.value.length-1);
}

function Nume_TiraDaLista (lista)
{
    list = eval (lista + '_select');
    lista_numerica = eval (lista);

    if (list.selectedIndex == -1)
    {
        alert ("É preciso selecionar um item.");
    }
    else
    {
        for (i=0; i<list.length; i++)
        {
            if (list.options[i].selected == true)
            {
                list.options[i].selected=false;
                list.options[i] = null;
                list.options.sort;
            }
        }
    }

    Nume_PovoaLista (lista);
}

function Nume_AdicionaLista (texto, valor, alvo)
{
    lista = eval (alvo);
    lista_alvo = eval (alvo + "_select");

    ok = false; 

    /* apaga os itens iguais */

    for (x=0; x < lista_alvo.length; x++)
    {
	if (lista_alvo.options[x].value == valor)
	{
		/*
		lista_alvo.options[x].value = 0;
		lista_alvo.options[x].text = '';
		//lista_alvo.options.remove(x);
		*/

		lista_alvo.options[x].selected=false;
		lista_alvo.options[x] = null;
		lista_alvo.options.sort;
		
		ok = true;
	}
    }

    /* adiciona o item, se não tiver sido incluído antes */

    for (x=0; x < lista_alvo.length; x++)
    {
	if (lista_alvo.options[x].value == 0 && !ok)
        {
    		lista_alvo.options[x].value = valor;
        	lista_alvo.options[x].text = texto;

		ok = true;
        }
    }

    Nume_PovoaLista (alvo);
}

function Nume_AdicionaCampo (texto, valor, alvo)
{
    lista = eval (alvo);
    lista_alvo = eval (alvo + "_nome");

    if (lista.value == valor)
    {
    	lista.value = '';
	lista_alvo.value = '';
    }
    else
    {
    	lista_alvo.value = texto;
	lista.value = valor;
    }
}

function Nume_Teclas ()
{
	if (event.ctrlKey != true) return;
	strSelection = document.selection.createRange().text;
	if (event.keyCode == 12) Nume_Janela('link.php','','width=600,height=424,scrollbars=yes');
	if (event.keyCode == 2) Nume_Bold(2);
	if (event.keyCode == 20) Nume_Italico(2);
	if (event.keyCode == 7) Nume_Janela('midia.php','','width=740,height=580,top=20,scrollbars=yes');
}

function Nume_Link ()
{
	strSelection = document.selection.createRange().text
	if (strSelection == "") document.formulario.conteudo_texto.focus()
	strHref = prompt("Escreva o endereço do site:","http://")
	if (strHref == null) return;
	document.selection.createRange().text = "<a href=\"" + strHref + "\" target=\"_blank\" class=\"verdana11laranja\">" + strSelection + "</a>"
	return;
}

function Nume_Direita (from)
{
			form = document.formulario;
                 	texto_inicio = form.conteudo_texto.selectionStart;

			if (!texto_inicio && texto_inicio != '0')
			{
				/* IE */	

				strSelection = document.selection.createRange().text
				if (strSelection == "")
				{
					document.formulario.conteudo_texto.focus()
					if (from == 2) document.formulario.conteudo_texto.select()
					strSelection = document.selection.createRange().text
					document.selection.createRange().text = strSelection + "<p align=\"right\" class=\"texto\"></p>"
				}	
				else 
				{
					document.selection.createRange().text = "<p align=\"right\" class=\"texto\">" + strSelection + "</p>"
				}
			}
			else
			{
				/* Mozilla */

				texto_fim = form.conteudo_texto.selectionEnd;
                        	texto_tamanho = form.conteudo_texto.value.length;
                        	texto_antes = form.conteudo_texto.value.substr (0, texto_inicio);
                        	texto_depois = form.conteudo_texto.value.substr (texto_fim, texto_tamanho - texto_fim);
                        	texto_selecao = form.conteudo_texto.value.substr (texto_inicio, texto_fim - texto_inicio);
				form.conteudo_texto.value = texto_antes + '<p align=\"right\" class=\"texto\">' + texto_selecao + '</p>' + texto_depois;
				
				form.conteudo_texto.focus ();
				
				if (texto_selecao.length > 0)
				{
					form.conteudo_texto.selectionStart = texto_inicio;
					form.conteudo_texto.selectionEnd = texto_fim + 7;
				}
				else
				{
					form.conteudo_texto.selectionStart = texto_inicio + 3;
					form.conteudo_texto.selectionEnd = texto_inicio + 3;
				}
				
			}

			return;
}

function Nume_Centraliza (from)
{
			form = document.formulario;
                 	texto_inicio = form.conteudo_texto.selectionStart;

			if (!texto_inicio && texto_inicio != '0')
			{
				/* IE */	

				strSelection = document.selection.createRange().text
				if (strSelection == "")
				{
					document.formulario.conteudo_texto.focus()
					if (from == 2) document.formulario.conteudo_texto.select()
					strSelection = document.selection.createRange().text
					document.selection.createRange().text = strSelection + "<p align=\"center\" class=\"texto\"></p>"
				}	
				else 
				{
					document.selection.createRange().text = "<p align=\"center\" class=\"texto\">" + strSelection + "</p>"
				}
			}
			else
			{
				/* Mozilla */

				texto_fim = form.conteudo_texto.selectionEnd;
                        	texto_tamanho = form.conteudo_texto.value.length;
                        	texto_antes = form.conteudo_texto.value.substr (0, texto_inicio);
                        	texto_depois = form.conteudo_texto.value.substr (texto_fim, texto_tamanho - texto_fim);
                        	texto_selecao = form.conteudo_texto.value.substr (texto_inicio, texto_fim - texto_inicio);
				form.conteudo_texto.value = texto_antes + '<p align=\"center\" class=\"texto\">' + texto_selecao + '</p>' + texto_depois;
				
				form.conteudo_texto.focus ();
				
				if (texto_selecao.length > 0)
				{
					form.conteudo_texto.selectionStart = texto_inicio;
					form.conteudo_texto.selectionEnd = texto_fim + 7;
				}
				else
				{
					form.conteudo_texto.selectionStart = texto_inicio + 3;
					form.conteudo_texto.selectionEnd = texto_inicio + 3;
				}
				
			}

			return;
}

function Nume_Bold (from)
{
			form = document.formulario;
                 	texto_inicio = form.conteudo_texto.selectionStart;

			if (!texto_inicio && texto_inicio != '0')
			{
				/* IE */	

				strSelection = document.selection.createRange().text
				if (strSelection == "")
				{
					document.formulario.conteudo_texto.focus()
					if (from == 2) document.formulario.conteudo_texto.select()
					strSelection = document.selection.createRange().text
					document.selection.createRange().text = strSelection + "<b></b>"
				}	
				else 
				{
					document.selection.createRange().text = "<b>" + strSelection + "</b>"
				}
			}
			else
			{
				/* Mozilla */

				texto_fim = form.conteudo_texto.selectionEnd;
                        	texto_tamanho = form.conteudo_texto.value.length;
                        	texto_antes = form.conteudo_texto.value.substr (0, texto_inicio);
                        	texto_depois = form.conteudo_texto.value.substr (texto_fim, texto_tamanho - texto_fim);
                        	texto_selecao = form.conteudo_texto.value.substr (texto_inicio, texto_fim - texto_inicio);
				form.conteudo_texto.value = texto_antes + '<b>' + texto_selecao + '</b>' + texto_depois;
				
				form.conteudo_texto.focus ();
				
				if (texto_selecao.length > 0)
				{
					form.conteudo_texto.selectionStart = texto_inicio;
					form.conteudo_texto.selectionEnd = texto_fim + 7;
				}
				else
				{
					form.conteudo_texto.selectionStart = texto_inicio + 3;
					form.conteudo_texto.selectionEnd = texto_inicio + 3;
				}
				
			}

			return;
}

function Nume_Italico (from)
{
			form = document.formulario;
                 	texto_inicio = form.conteudo_texto.selectionStart;

			if (!texto_inicio && texto_inicio != '0')
			{
				/* IE */	

				strSelection = document.selection.createRange().text
				if (strSelection == "")
				{
					document.formulario.conteudo_texto.focus()
					if (from == 2) document.formulario.conteudo_texto.select()
					strSelection = document.selection.createRange().text
					document.selection.createRange().text = strSelection + "<i></i>"
				}	
				else 
				{
					document.selection.createRange().text = "<i>" + strSelection + "</i>"
				}
			}
			else
			{
				/* Mozilla */

				texto_fim = form.conteudo_texto.selectionEnd;
                        	texto_tamanho = form.conteudo_texto.value.length;
                        	texto_antes = form.conteudo_texto.value.substr (0, texto_inicio);
                        	texto_depois = form.conteudo_texto.value.substr (texto_fim, texto_tamanho - texto_fim);
                        	texto_selecao = form.conteudo_texto.value.substr (texto_inicio, texto_fim - texto_inicio);
				form.conteudo_texto.value = texto_antes + '<i>' + texto_selecao + '</i>' + texto_depois;
				
				form.conteudo_texto.focus ();
				
				if (texto_selecao.length > 0)
				{
					form.conteudo_texto.selectionStart = texto_inicio;
					form.conteudo_texto.selectionEnd = texto_fim + 7;
				}
				else
				{
					form.conteudo_texto.selectionStart = texto_inicio + 3;
					form.conteudo_texto.selectionEnd = texto_inicio + 3;
				}
			}

			return;
}

function Midia (v1, v2, elemento, clipping)
{
       form = opener.document.formulario;
	
       /* se for mídia de elemento ou clipping faz um pouco diferente */

if (elemento.length > 0)
{
	conteudo_tad = form.elemento_midia;
	conteudo_tad.value = v1;
}
else if (clipping.length > 0)
{
	conteudo_tad = form.clipping_midia;
	conteudo_tad.value = v1;
}
else
{
  conteudo_tad = form.conteudo_midia;

  if (v2.length == 0)
  {

        /* colocando o tag no texto */

	texto_posicao = form.conteudo_texto.selectionEnd;

	if (!texto_posicao && texto_posicao != '0')
	{
		/* IE */

		strSelection = opener.document.selection.createRange().text;
		if (strSelection == "")
		{
		        form.conteudo_texto.focus();
			strSelection = opener.document.selection.createRange().text;
		}
		opener.document.selection.createRange().text = strSelection + '<midia src="' + v1 + '">';
	}
	else
	{
		/* Mozilla */

		texto_tamanho = form.conteudo_texto.value.length;
		texto_antes = form.conteudo_texto.value.substr (0, texto_posicao);
		texto_depois = form.conteudo_texto.value.substr (texto_posicao, texto_tamanho - texto_posicao);
		form.conteudo_texto.value = texto_antes + '<midia src="' + v1 + '">' + texto_depois;
	}


        /* trocando os valores do input hidden */

        if (conteudo_tad.value != "")
        {
                v1 = ',' + v1;
        }

        conteudo_tad.value = conteudo_tad.value + v1;

  }
  else
  {
        form.conteudo_texto.value = form.conteudo_texto.value.replace ('<midia src="'+v2+'">','<midia src="'+v1+'">');
        conteudo_tad.value = conteudo_tad.value.replace (v2, v1);
  }
}

  /* disparando o outro formulario e fechando esta janela */

  form.submit_valido.value = 0;
  form.submit();
  self.window.close();
}

function Midia_RetiraImagemElemento ()
{
	form = document.formulario;
	imagens = form.elemento_midia;
	imagens.value = '';

	form.submit_valido.value = 0;
        form.submit();      
}

function Midia_RetiraImagemClipping ()
{
	form = document.formulario;
	imagens = form.clipping_midia;
	imagens.value = '';

	form.submit_valido.value = 0;
        form.submit();      
}

function Midia_RetiraImagem (v1)
{
      form = document.formulario;
      imagens = form.conteudo_midia;
      texto = form.conteudo_texto;

      imagens.value = imagens.value.replace(v1,'');
      imagens.value = imagens.value.replace(',,',',');

      ultima = imagens.value.length - 1;

      if (imagens.value.lastIndexOf(',') == ultima)
      {
              imagens.value = imagens.value.substr(imagens.value,ultima)
      }

      if (imagens.value.charAt(0) == ',')
      {
              imagens.value = imagens.value.replace(',','');
      }

      texto.value = texto.value.replace('<midia src=\"'+v1+'\">','');

      form.submit_valido.value = 0;
      form.submit();
}

function Nume_Retranca (str)
{
	var reg = new RegExp ("[a-z]");

	if (!reg.test (str) && str.length > 0)
	{
		if (confirm ('A retranca só tem letras maiúsculas. Isso é correto?'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}		
}
