﻿addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};

//Checar Cookies Ativados
var cookies = checarCookiesAtivados();
if (!cookies) {
	//top.location = "/avisos/cookies-desativados/";
}

function getElementsByClassName(strClass) {
	var ret   = new Array();
	var tag   = arguments[1] || "*"; 
	var node  = arguments[2] || document; 
	var base  = node.getElementsByTagName(tag);	
	var tBase = base.length;
	for(var i = 0; i < tBase; i++) {
		var aClass = base[i].className.split(" ");
		var taClass = aClass.length;
		for(var j = 0; j < taClass; j++) {
			if(aClass[j] == strClass) {
				ret[ret.length] = base[i];
				break;
			}
		}
	}
	return ret;
}

function global() {
	if(navigator.appVersion.indexOf("MSIE") != -1) {
		arrVersao = navigator.appVersion.split("MSIE"); 
		if (parseFloat(arrVersao[1]) < 7) {
			menuCss("menu-mais");
			menuCss("menu-inicio");
		}
	}

	if (document.getElementById("carregando")) {
		ocultarLayers();
	}
	
	externalLinks();
	conformancePos();
}

function popup(url,janela,larg,alt,scroll,pos1,pos2){
	var janela = window.open(url, janela, "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars="+scroll+",resizable=yes,copyhistory=no,top="+pos1+",left="+pos2+",screenY="+pos1+",screenX="+pos2+",width="+larg+",height="+alt);
}

function esqueceuSenha() { 
	popup('/site-gratis/relembrar-senha/','esqueceu',385,190,'no',((screen.availHeight-190)/2)-90,(screen.availWidth-385)/2);
}

function externalUrl(url, larg, alt) {
	if (!larg) larg = 778; 
	if (!alt) alt = 472;
	pos1 = 0; pos2 = 0;
	window.open(url, '_blank', "toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,top="+pos1+",left="+pos2+",screenY="+pos1+",screenX="+pos2+",width="+larg+",height="+alt);
}

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if ( anchor.getAttribute("href") && anchor.className.indexOf("external") != -1 ) anchor.target = "_blank";
	}
}

function cliqueUMB() {
	var codigo = document.getElementById("afl-codigo").innerHTML;
	alert("Antes de visitar o Site Oficial Unibanco MegaBônus anote o Código de Indicação MegaBônus para solicitar o seu cartão.\n\nO Código de Indicação MegaBônus é necessário, pois como trata-se de um Sistema de Indicações, só é possível solicitar, indicado por alguém que já possua o cartão.\n\nAssim, anote o Código para solicitar o seu Cartão Pré-pago MegaBônus, que é: " + codigo + "\n\nAo clicar em OK, o Site Oficial Unibanco MegaBônus será aberto em uma janela ou aba a parte.");
}

function ocultarLayers() {
	if(document.getElementById('carregando') && document.getElementById('avisos')) {
		document.getElementById('carregando').style.display = "none";
		document.getElementById('avisos').style.display = "none";
	}
}

function menuCss(elemento) {
	if (document.all&&document.getElementById) {
		if(document.getElementById(elemento)) {
			nav = document.getElementById(elemento);
			for (i=0; i<nav.childNodes.length; i++) {
				node = nav.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
					this.className+=" over";
					}
					node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
					}
				}
			}
		}
 	}
}

function avisoPos(lt) {
	var saida;
	if (document.getElementById("avisos")) {
		if(lt == "left") { saida = ( 0 - avisos.offsetWidth - 2 + ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth ) + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px'; }
		if(lt == "top")  { saida = ( 0 - avisos.offsetHeight - 2 + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) ) + 'px'; }
		return saida;
	}
}

function conformancePos() {
	if(document.getElementById("footer")) {
		hWrapper = document.getElementById("footer").offsetTop + 75;
		hBrowser = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		if (hWrapper > hBrowser) {
			var objBody = document.getElementsByTagName("body").item(0);
			objBody.style.height = hWrapper + "px";
		}
		var cnf = document.getElementById("conformance");
		cnf.style.display = "block";
		cnf.style.paddingBottom = "3px";
	}
}

function mouseX(evt) {
	if (!evt) evt = window.event; 
	if (evt.pageX) return evt.pageX; 
	else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
	else return 0;
}

function mouseY(evt) {
	if (!evt) evt = window.event; 
	if (evt.pageY) return evt.pageY; 
	else if (evt.clientY) return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); 
	else return 0;
}

function seguir(divName, evt) {
	if(document.getElementById(divName)) {
		var obj = document.getElementById(divName);
		var mX = parseInt(mouseX(evt));
		var mY = parseInt(mouseY(evt));
		var recuo = 86;
		obj.style.display = "block";

		if (screen.width > (mX + 300 - recuo + 30)) {
			obj.style.left = mX - recuo + "px";
		}
		else {
			obj.style.left = screen.width - 300 - 30 + "px";
		}
		obj.style.top = mY - (parseInt(externalCSS(obj, "height", "height"))) - 5 + "px";
		
		//Browsers ajustes
		if(navigator.userAgent.indexOf("Opera")!=-1) { obj.style.top = parseInt(obj.style.top) - 10 + "px"; }
		if((navigator.appVersion.indexOf("MSIE")!=-1) && (!navigator.userAgent.indexOf("Opera")!=-1)) { obj.style.top = parseInt(obj.style.top) + 8 + "px"; }
	}
}

function ajudaContextual(acao, elem, event, cb) { //cb -> 1 para cima / 0 para baixo
	if(document.getElementById("ajuda-" + elem)) {
		if (acao == 1) {
			seguir("ajuda-" + elem, event);
		}
		else {
			document.getElementById("ajuda-" + elem).style.display = "none";
		}
	}
}

function externalCSS(el, cssproperty, csspropertyNS){
	if (el.currentStyle)
		return el.currentStyle[cssproperty]
	else if (window.getComputedStyle){
		var elstyle=window.getComputedStyle(el, "")
		return elstyle.getPropertyValue(csspropertyNS)
	}
}

function checarCookiesAtivados() {
	if (readCookie("CMS")) {
		return true;
	}
	else {
		return false;
	}
}

function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  {
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours*60*60*1000);
	expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}

function loadUrl(selObj,janela){
	var url = selObj.options[selObj.selectedIndex].value; 

	if (url) { 
		remote = window.open(url, janela); 
		if (remote != null) { 
			if (remote.opener == null) remote.opener = self; 
		} 
	} 
} 

addEvent(window, 'load', global);