var selecao_array = new Array();
	var sel_sequencial=0;
	var openerUrl='';
	var selecao_lendo=false;
	var selecao_lida=false;
	var contador_selecao=0;
	var cod_selecao_ativa=0;
	var showMenuAdmin=false;
	var contador_selecao_total=0;
	

	function mostrarOperacoesAdmin(e)
	{
		if(showMenuAdmin)
		{
			showMenuAdmin=false;
			document.getElementById('divMenuAdmin').style.display='none';
		}
			else
		{
			showMenuAdmin=true;
			document.getElementById('divMenuAdmin').style.display='';
		}
	}

	function inserirOutroCliente(origem)
	{
		w = window.open('/admin/insere_outro_cliente.php?from='+origem, "selecaoOutrosClientes","status=no, menubar=no, location=no, scrollbars=no, width=462, height=350");
		w.focus();
	}

		
	function ler_dados_selecao()
	{
		selecao_lendo=true;

		var right_now=new Date();
		var script = document.createElement('script');
		url='/dyndata/usuariors.php?tm=' + rand(9999) + right_now.getHours() + right_now.getMinutes() + right_now.getSeconds();
		if(CacheControl) url+='&cache=true';
		if(cod_noticia)
		{
			url+='&cod_noticia='+cod_noticia;
		}
			else
		{
			if(rs=/action=(\d+)/g.exec(document.location))
			{
				url+='&cod_noticia='+rs[1];
			}
		}

		script.type = 'text/javascript';
		script.src = url;
		
		document.getElementsByTagName('head')[0].appendChild(script);

		//alert('script pedido');
	}

	function _onload_selecao()
	{

		ler_selecao();
	}

	function limparSelecao()
	{
		if(confirm('Limpar a seleção atual?'))
		{
			document.location=openerUrl+'&limparSelecao=1';
		}
	}

	function criarSelecao(perguntar)
	{
		if(perguntar)
		{
			if(confirm("Suas alterações na seleção atual serão perdidas e uma nova seleção vazia será criada.\n\nDeseja continuar?"))
			{
				document.location=openerUrl+'&criarSelecao=1';
			}
		}
			else
		{
			document.location=openerUrl+'&criarSelecao=1';
		}
	}


	function ler_selecao()
	{
		if(!selecao_lida)
		{
			if(!selecao_lendo) ler_dados_selecao();

			if(typeof(window.selecao_string)!=="undefined")
			{
				selecao_lida=true;

				//alert('Marcando...'+selecao_string);
				contador_selecao=0;
				selecao_array = new Array();

				if(selecao_string.length)
				{
					selecao_array = selecao_string.split(',');
					if(selecao_array.length > 0)
					{
						for(n=0;n<(selecao_array.length);n++)
						{
							if(selecao_array[n].length>0)
							{
								contador_selecao++;
								//eval('if(document.getElementById("check[' + selecao_array[n] + ']")) document.getElementById("check[' + selecao_array[n] + ']").checked=true;');
								if(obj=document.getElementById("sNt["+selecao_array[n]+"]"))
								{
									obj.title="Notícia selecionada";
									obj.src='/imagens/radio_on.gif';
								}
							}
						}
					}
				}

				updateContadorSelecao();

				if(document.getElementById('marcar_desmarcar')) document.getElementById('marcar_desmarcar').style.visibility='visible';
			}
				else
			{
				//alert('esperando...');
				setTimeout('ler_selecao()', 200);
			}
		}
	}



	function updateContadorSelecao()
	{
		if(document.getElementById('contaSelecao'))
		{
			//alert('document:'+contador_selecao);
			alterarDisplaySelecao('Minha Seleção: ' + contador_selecao_total);
		}
			else
		{
			if(parent.left)
			{
				if((typeof parent.left.alterarDisplaySelecao)!='function')
				{
					setTimeout('updateContadorSelecao()', 200);
				}
					else
				{
					//alert('left:'+contador_selecao);
					parent.left.alterarDisplaySelecao(contador_selecao_total);
				}
			}
				else
			{
				if(parent)
				{
					//alert('parent: '+parent.location);
					parent.alterarDisplaySelecao('Minha Seleção: ' + contador_selecao_total);
				}
			}
		}
	}

	function alterarDisplaySelecao(txt)
	{		if(document.getElementById('contaSelecao'))
		{
			document.getElementById('contaSelecao').innerHTML=txt;
		}
	}


	function get_selecao()
	{
		return document.selecao_form.noticia_string.value;
	}

	function selecao_submit()
	{
		document.selecao_form.submit();
	}

	function selNot(obj)
	{
		selurl="/selecao/selecao_noticia.php?seq="+selecao_check_timestamp + sel_sequencial + "&cod="+cod_selecao_ativa+"&"+obj.id;
		sel_sequencial++;

		if(obj.title=="")
		{
			obj.title="Notícia selecionada";
			obj.src=selurl+'=1';
			contador_selecao++;
			contador_selecao_total++;
		}
			else
		{
			obj.title="";
			obj.src=selurl+'=0';
			contador_selecao--;
			contador_selecao_total--;
		}

		updateContadorSelecao();
	}

	function marcarNoticia(obj)
	{
	  	selurl="/selecao/selecao_noticia.php?seq="+selecao_check_timestamp + sel_sequencial + "&cod="+cod_selecao_ativa+"&"+obj.id;
		if(obj.title=="")
		{
			obj.title="Notícia selecionada";
			obj.src=selurl+'=1';
			contador_selecao++;
			contador_selecao_total++;
		}
	}

	function desmarcarNoticia(obj)
	{
		selurl="/selecao/selecao_noticia.php?seq="+selecao_check_timestamp + sel_sequencial + "&cod="+cod_selecao_ativa+"&"+obj.id;
		if(obj.title!="")
		{
			obj.title="";
			obj.src=selurl+'=0';
			contador_selecao--;
			contador_selecao_total--;
		}
	}


	function selNotJanelaNoticia(obj)
	{
		selNot(obj);
		opener.atualizaOpenerSelecao(obj.id)
	}


	function atualizaOpenerSelecao(notId)
	{
		if(obj=document.getElementById(notId))
		{
			//alert('opener: '+notId);
			if(obj.title=="")
			{
				obj.title="Notícia selecionada";
				obj.src='/imagens/radio_on.gif';
				contador_selecao++;
				contador_selecao_total++;
			}
				else
			{
				obj.title="";
				obj.src='/imagens/radio_off.gif';
				contador_selecao--;
				contador_selecao_total--;
			}
			updateContadorSelecao();
		}
	}


	function limpar_selecao()
	{
		document.selecao_form.noticia_string.value='';
		document.getElementById('contador_selecao').value=0;
	}


	function selecionar(cod_noticia)
	{

		document.links(cod_noticia).style.color='#33cc66';

	}

	function selecao(cod_noticia)
	{

	}

	function setOpenerUrl(val)
	{
		openerUrl=val;
	}

	function ShowFormSelecao(op)
	{
		w = window.open("/intranet/admin_selecao.php?op="+op+"&openerUrl="+openerUrl,"adminSelecao","status=no, menubar=no, location=no, scrollbars=no, width=462, height=350");
		w.focus();
	}

	function apagarselecao()
	{
		if (window.confirm("Atenção, todas as notícias da seleção serão apagadas do clipping.\n\nVocê deseja mesmo apagá-las?"))
		{
			document.location='/selecao/apagar_noticias.php';
		}
	}

	confirmWindow = function(texto, link) {

		var confirmation = window.confirm(texto);

		if(confirmation) {
			window.location = link;
		}
	}



	function getCheckedValues(frm){
	   var str = '';
	   var delimeter = '|||';
	   var counter = 0;
	   for(var i=0;i<frm.elements.length;i++){
	     if((frm.elements[i].type=="checkbox") && (frm.elements[i].checked)){
	       if(counter==0){str += frm.elements[i].value;}
	       else{str += delimeter + frm.elements[i].value;}
	       counter++;
	     }
	   }
	   frm.txt.value = str;
	 }

	 function showValues(frm){
	   getCheckedValues(frm);
	   alert(frm.txt.value);
	   return false;
	 }

	 function checkCheckBoxes(frm_txt, flag)
	 {
		frm=document.getElementById(frm_txt);
		for(var i=0;i<frm.elements.length;i++)
		{
	    	if(frm.elements[i].type=="checkbox")
			{
				if(flag!=frm.elements[i].checked)
				{
//					selecao(frm.elements[i].name.substr(4));
//			        frm.elements[i].checked = flag;
			        frm.elements[i].click();
				}
			}
		}
	}

	function marcar_selecao()
	{
		auto_alterar_selecao('marcar');
	}

	function desmarcar_selecao()
	{
		auto_alterar_selecao('desmarcar');
	}

	function inverter_selecao()
	{
		auto_alterar_selecao('inverter');
	}


	function auto_alterar_selecao(acao)
	{
		if(selecao_lida)
		{
			if(typeof(window.selecao_string)!=="undefined")
			{
				if(document.getElementById('marcar_desmarcar')) document.getElementById('marcar_desmarcar').style.visibility='hidden';

			  	var right_now=new Date();
				var imgs=document.getElementsByTagName('IMG');
				var delNot='';
				var selurl="/selecao/selecao_noticia.php?seq=" + right_now.getHours() + right_now.getMinutes() + right_now.getSeconds();
				var tt=0;

				if(imgs.length)
				{
				  for(k in imgs)
				  {
					  if(imgs[k].id)
					  {
						  if(imgs[k].id.substr(0,4)=='sNt[')
						  {
								obj=imgs[k];
								tt++;

								if(obj.title=='')
								{
									if(acao=='marcar' || acao=='inverter')
									{
										obj.title="Notícia selecionada";
										//obj.src=selurl+'=1';
										obj.src='/imagens/radio_on.gif';
										contador_selecao++;
										contador_selecao_total++;
										delNot+='&'+imgs[k].id+'=1';
									}
								}
									else
								{
									if(acao=='desmarcar' || acao=='inverter')
									{
										obj.title="";
										//obj.src=selurl+'=0';
										obj.src='/imagens/radio_off.gif';
										contador_selecao--;
										contador_selecao_total--;
										if(acao=='inverter') delNot+='&'+imgs[k].id+'=0';
									}
								}

								if(delNot.length > 1000)
								{
									dynJs(selurl+tt+delNot);
									delNot='';
								}
							}
						}
				  }
				  
				  if(delNot.length>0)
				  {
					  dynJs(selurl+tt+delNot);
				  }

				  if(acao=='desmarcar')
				  {
					  dynJs(selurl+tt+"&limpar_tudo=1");
				  }

				  updateContadorSelecao();
				  setTimeout('updateContadorSelecao()', 1000);

				  if(document.getElementById('marcar_desmarcar')) document.getElementById('marcar_desmarcar').style.visibility='visible';

			   }
			}
		}
	}