// coloca os parametros passado por get, em um array por nomes
var URL = location.href;
var PARAMS = URL.substring(URL.indexOf("?")+1);
var PARAM = new Array();
PARAM = PARAMS.split("&");
var par = new Array();

for(var x=0; x < PARAM.length; x++)
{
	var VALOR = new Array();
	VALOR = PARAM[x].split("=");
	par[VALOR[0]] = VALOR[1];
}

var re = /^\//;
var Path = (document.location.pathname).replace(re,"");
var Paths = new Array;
Paths = Path.split('/');
for(var x=0;x <= 1;x++)
{
	if(Paths[x])
	{
		Path = Paths[x];
	}
}

var re = /result/
if(re.test(Path))
{
	Path = par["cl"]
	var re = /(%2F)/g;
	Path = Path.replace(re,"\/");
	var re = /.*\/(.*)/g;
	Path = Path.replace(re,"$1");
}

function getCookieVal (offset) 
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	   endstr = document.cookie.length;
	   return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name)
{
	 var arg = name + "=";
	 var alen = arg.length;
	 var clen = document.cookie.length;
	 var i = 0;

	 while (i < clen) 
	 {           
	  var j = i + alen;									   
	  if (document.cookie.substring(i, j) == arg)
		  return getCookieVal (j);
		  i = document.cookie.indexOf(" ", i) + 1;
	  if (i == 0) 
		  break; 
	 }
	 return null;
}

function PegaNomeUsuario() // Retorna o nome do usuário logado
{
	username = GetCookie('usuario');
	if (username != null)
	{
		var Paths = new Array;
		Paths = username.split(';');
		for(var x=0;x < Paths.length;x++)
		{
			Paths[x] = Paths[x].replace(/\+/gi, " ")
		}
		return Paths[1];
	}
	else return "";
}

function PegaEmailUsuario() // Retorna o email do usuário logado
{
 username = GetCookie('usuario');
 if (username != null)
 {
  var Paths = new Array;
  Paths = username.split(';');
  for(var x=0;x < Paths.length;x++)
  {
   Paths[x] = Paths[x].replace(/\+/gi, " ")
  }
  return Paths[2];
 }
 else return "";
}

function ValidaTicket()
{
	username = GetCookie('usuario');
	ticket = GetCookie('ticket');
	codigo = '';
	
	if (username != null) {
		valores = username.split(';');
		codigo = valores[0];
	}
	
//	if ((username == null && ticket == null) || (codigo == '2516185'))
	if (((username == null) && (ticket == null)) || (codigo == '2516185'))
	{
		location.replace('http://passaporte.abril.com.br/autenticaUsuario.do?metodo=checarTipoAutenticacao&COD_SITE=133&COD_RECURSO=194&URL_RETORNO=' + escape(window.location));
		return false;
	}
	return true;
}

function envAmigo(codNum) {
	
	username = GetCookie('usuario');
	ticket = GetCookie('ticket');

	if ((username == null) && (ticket == null))
	{
		location.replace('http://passaporte.abril.com.br/autenticaUsuario.do?metodo=checarTipoAutenticacao&COD_SITE=133&COD_RECURSO=194&URL_RETORNO=' + escape(window.location + '?envAmigo=true'));
		return false;
	}

	//var wEnv = window.open('/fwa/envieAmigo/envieAmigo.html', 'PopEnviar', 'width=430,height=500,left=0,top=0');
	var wEnv = window.open('/fwa/envieAmigo/envieAmigo.html?vurl=http://gloss.abril.com.br/blog/#'+codNum, 'PopEnviar', 'width=430,height=500,left=0,top=0');
	wEnv.focus();
}

if (URL.indexOf("envAmigo=true") > 0) {
	envAmigo();
}


function envAmigoCopa() {
	
	username = GetCookie('usuario');
	ticket = GetCookie('ticket');

	if ((username == null) && (ticket == null))
	{
		location.replace('http://passaporte.abril.com.br/autenticaUsuario.do?metodo=checarTipoAutenticacao&COD_SITE=133&COD_RECURSO=194&URL_RETORNO=' + escape(window.location + '?envAmigoCopa=true'));
		return false;
	}

	var wEnv = window.open('/copa2006/envieamigo/envie_amigo.shtml', 'PopEnviar', 'width=430,height=435,left=0,top=0');
	wEnv.focus();

}

if (URL.indexOf("envAmigoCopa=true") > 0) {
	envAmigoCopa();
}


function indAmiga() {
	
	username = GetCookie('usuario');
	ticket = GetCookie('ticket');

	if ((username == null) && (ticket == null))
	{
		location.replace('http://passaporte.abril.com.br/autenticaUsuario.do?metodo=checarTipoAutenticacao&COD_SITE=133&COD_RECURSO=194&URL_RETORNO=' + escape(window.location + '?indAmigo=true'));
		return false;
	}

	var wEnv = window.open('/envieamigo/indique_amigo.shtml', 'PopEnviar', 'width=430,height=400,left=0,top=0');
	wEnv.focus();
}

if (URL.indexOf("indAmigo=true") > 0) {
	indAmiga();
}



// Função para validar um email
function emailValido (email)
{
   invalidChars = " /:,;"

   if (email == "") return false;
   
   for (i=0; i<invalidChars.length; i++)
   {
      badChar = invalidChars.charAt(i);
      if (email.indexOf(badChar,0) > -1) return false;
   }

   atPos = email.indexOf("@",1);
   if ((atPos == -1) || (email.indexOf("@",atPos+1) != -1)) return false;

   periodPos = email.indexOf(".",atPos)
   if ((periodPos == -1) || (periodPos+3 > email.length)) return false;

   return true;
}

// Função que checa se os emails são válidos e se a quantidade de nomes é igual a quantidade de emails
// Os valores das variáveis "nomesValue" e "emailsValue" devem estar sepadaros por ";"
function checkEmail(nomesValue, emailsValue)
{
   var NOMES = new Array();
   NOMES = nomesValue.split(";");

   if (emailsValue == "") return false;

   var emails;
   var re=/[ +]/g;
   emails = emailsValue.replace(re,"");

   var EMAILS = new Array();
   EMAILS = emails.split(";");

   if(NOMES.length > 1) {
      if(NOMES.length != EMAILS.length) {
         alert("Os campos de nome e endereço do destinatário devem ter a mesma quantidade.");
         return false;
      }
   }

   for (var i=0;i<EMAILS.length;i++) {
      if (!emailValido(EMAILS[i])) {
         alert("Por favor, preencha corretamente o campo do endereço do destinatário.");
         return false;
      }
   }

   return true;
}
//----------------------------------------------------------------------------------
var wComente = false;
var tituloTratado = document.title.toLowerCase().replace(" - gloss","");
function imprimir(id){
	var vUrl = location.href;
	if (vUrl.indexOf("?")!=-1){
		vUrl = 	vUrl.split("?")[0];
	}
	vUrl = vUrl +"?imprimir=true"
	window.open(vUrl, 'Imprimir','toolbar=no,location=no,status=no,menubar=yes,scrollbars=yes,resizable=no,width=780,height=500');
}
function elemento(e){
	return document.getElementById(e);
}
function abreComentario(id){
	if (wComente==false){
		var div = document.createElement("div");
		var div2 = document.createElement("div");
		var root = document.createElement("div");
		var script = document.createElement("script");

		div.className = "widget_comente";
		
		div.innerHTML = '&nbsp;<object width="600" height="300"><param name="movie" value="/common/swf/comentarMateria.swf"></param><param name="allowFullScreen" value="true">	</param><param name="wmode" value="transparent"><param name="flashvars" value="materiaId='+id+'"></param><embed src="/common/swf/comentarMateria.swf" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true"  flashvars="materiaId='+id+'" width="600" height="300"></embed></object>'

		div.style.position = "absolute";
		div.style.top = "0px";
		div.style.left = "0px";
		div.style.zIndex = 999;
		/*div.style.clip = "rect(0px, 600px, 300px, 0px)";*/
			
		div2.style.background = "url(/common/img/clear.gif)";
		div2.style.position = "absolute";
		div2.style.width = "250px"
		div2.style.height = "50px"
		div2.style.left = "15px"
		div2.style.top = "5px"
		div2.style.zIndex = 9999;
		div2.style.cursor = "move"
		div2.innerHTML = "&nbsp;"
		root.style.position = "absolute";
		root.style.top = "300px";
		root.style.left = "300px";
		root.style.zIndex = 9999999;
		root.setAttribute("id","widgetComente");
		
		//root.style.clip = "rect(0px, 600px, 300px, 0px)";
		root.appendChild(div);
		root.appendChild(div2);
		document.body.appendChild (root);
	
		Drag.init(div2, root);
		wComente = true;
	}else{
		document.body.removeChild(elemento("widgetComente"));
		wComente = false;
	}
}
function imprimeTestePaginacao(xml,pagina){
	var parametros = "";
	if(pagina){
		parametros = "pg="+pagina;
	}
	parametros += "&arquivo="+xml;
	flash("/common/swf/testes_v2.swf",552,700,parametros);
}
function imprimeTeste(xml){
	var parametros = "";
	if(par['pg']){
		if(par['pg']==0) cookies.criar('pontosQuiz',0,0);
		parametros = "pg="+par['pg']+"&pts="+cookies.busca("pontosQuiz");
	}else{
		parametros = "pg=0&pts=0";
		cookies.criar('pontosQuiz',0,0);
	}

	parametros += "&arquivo="+xml;
	flash("/common/swf/testes.swf",552,700,parametros);
}
function proTest(pg,pts){
	if(cookies.busca("pontosQuiz")==null){
		cookies.criar('pontosQuiz',pts,0);
	}else{
		cookies.criar('pontosQuiz',pts+"|"+cookies.busca("pontosQuiz"),0);
	}
	var tUrl = URL;
	if (tUrl.indexOf("?")!=-1){
		tUrl = URL.split("?")[0];
	}
	window.location.href = tUrl+"?pg="+pg;
}
function indAmigo(){
	var url=location.href;
	var titulo=document.title;
	if (url.indexOf("?")!=-1){
		url = url.split("?")[0]
	}	
	if (titulo.indexOf("- Gloss")!=-1){
		titulo = titulo.split("- Gloss")[0];	
	}
	if (titulo.indexOf("-Gloss")!=-1){
		titulo = titulo.split("-Gloss")[0];	
	}
	var parametros = "?url="+url+"&title="+titulo;
	window.open("/ferramentas/indique/"+parametros, "_blank", "width=300,height=450");

}
function addScript(url) {
	var s = document.createElement('script');
	s.type = 'text/javascript';
	s.src = url;
	document.getElementsByTagName('head')[0].appendChild(s);
}
String.prototype.removeAcentoEspacos = function(){
	var palavra = this;
	var comAcento = "àáâãäèéêëìíîïòóôõöùúûüÀÁÂÃÄÈÉÊËÌÍÎÒÓÔÕÖÙÚÛÜçÇñÑ";
	var semAcento = "aaaaaeeeeiiiiooooouuuuAAAAAEEEEIIIOOOOOUUUUcCnN";
	for (var i=0;i<comAcento.length;i++){
		var temp = comAcento.substring(i,i+1);
		var temp2 = semAcento.substring(i,i+1);
		palavra = palavra.replace(temp,temp2);
	}
	palavra = palavra.replace(/\s+/g,"-");
	return palavra;
}
tituloTratado= tituloTratado.removeAcentoEspacos();

    function mostraEsconde(id,valor){
        document.getElementById(id).style.display=valor;
    }
