﻿
function doPlayVideo(valorLimpo, guid) {
    if (masTerPAge == 'video_detalhe.aspx') {
        var slcontrol = document.getElementById("SlrealVideo");
        if (slcontrol != null) {
            try {
                var objAnc = document.getElementById("ancorvideo");
                if (objAnc) {
                    objAnc.focus();
                }
            }
            catch (ex) {

            }
            slcontrol.Content.VideoPlayerPage.PlayVideo(guid, true);
        } else {
            alertScreen();
        }
        try {
            top.changeAdress('/video_detalhe/' + valorLimpo + '/', 'MYWAY VÍDEO - videoclips oficiais, concertos, entrevistas, ensaios, making-ofs');
        }
        catch (e) {
            //alert("erro");
        }
    } else {
        var pagina = tr.concat('http://'+location.hostname + '/video_detalhe/', valorLimpo, ".aspx");
        //alert(pagina);
        top.setLocation(pagina);
    }
}

function isAudioPlayerPlaying() {
    try {
        return top.frmPlayer.isAudioPlayerPlaying();
    }
    catch (ex) {
        return false;
    }
}

//RADIO
function doPlayRadio(guid) {
    try {
        top.frmPlayer.playRadio(guid);
    }
    catch (ex) {
        alertScreen();
    }
}
function doPlayRadioFromProcura(guid, javaName, objTxt, mainDiv, permaid, openPage) {
    doPlayRadio(guid);
    var objTxt = document.getElementById(objTxt);
    if (objTxt) {
        objTxt.value = unescape(javaName);
    }
    hidePreLoad(mainDiv);
    if (openPage == 'true')
        top.setLocation(tr.concat('http://'+location.hostname + '/', permaid, '.aspx'));
}
// ARTISTA 
function doPlayArtist(guid, nomeArtista) {
    try {
        top.frmPlayer.playArtist(guid);

        var w_counter;
        if (wreport_ok == 1) {
            w_counter = new wreport_counter('Play Artist', guid, 421060, WRP_ACC, WRP_CHANNEL);
            w_counter.add_content('Nome: ' + nomeArtista);

            w_counter.count();
        }
    }
    catch (ex) {
        alertScreen();
    }
}
function doPlayArtistSong(guid, nomeArtista) {
    try {
        top.frmPlayer.playArtistSong(guid);

        var w_counter;
        if (wreport_ok == 1) {
            w_counter = new wreport_counter('Play Artist', guid, 421060, WRP_ACC, WRP_CHANNEL);
            w_counter.add_content('Nome: ' + nomeArtista);

            w_counter.count();
        }
    }
    catch (ex) {
        alertScreen();
    }
}
function doPlayArtistFromProcura(guid, javaName, objTxt, mainDiv, permaid, openPage, nomeArtista) {
    if (openPage == 'false' || !isAudioPlayerPlaying())
        doPlayArtist(guid, nomeArtista);
    var objTxt = document.getElementById(objTxt);
    if (objTxt) {
        objTxt.value = unescape(javaName);
    }
    hidePreLoad(mainDiv);
    if (openPage == 'true')
        top.setLocation(tr.concat('http://'+location.hostname + '/artista/', permaid, '.aspx'));
}
// MUSICA 
function doPlayMusic(guid) {
    try {
        top.frmPlayer.playMusic(guid);
    }
    catch (ex) {
        alertScreen();
    }
}
function doPlaySongFromProcura(guid, javaName, objTxt, mainDiv, permaid, openPage) {
    doPlayMusic(guid);
    var objTxt = document.getElementById(objTxt);
    if (objTxt) {
        objTxt.value = unescape(javaName);
    }
    hidePreLoad(mainDiv);
}
//Album
function doPlayAlbum(guid) {
    try {
        top.frmPlayer.playAlbum(guid);
    }
    catch (ex) {
        alertScreen();
    }
}
function doPlayAlbumFromProcura(guid, javaName, objTxt, mainDiv, permaid, openPage) {
    if (openPage == 'false' || !isAudioPlayerPlaying())
        doPlayAlbum(guid);
    var objTxt = document.getElementById(objTxt);
    if (objTxt) {
        objTxt.value = unescape(javaName);
    }
    hidePreLoad(mainDiv);
    if (openPage == 'true')
        top.setLocation(tr.concat('http://'+location.hostname + '/album/', permaid, '.aspx'));
}
//SubStructure
function doPlaySubStructure(guid) {
    try {
        top.frmPlayer.playSubStructure(guid);
    }
    catch (ex) {
        alertScreen();
    }
}
function doPlaySubStructureFromProcura(guid, javaName, objTxt, mainDiv, permaid, openPage) {
    doPlaySubStructure(guid);
    var objTxt = document.getElementById(objTxt);
    if (objTxt) {
        objTxt.value = unescape(javaName);
    }
    hidePreLoad(mainDiv);
}
// User playlists
function doPlayUserPlaylist(guid, query) {
    try {
        top.frmPlayer.playUserPlaylist(guid, query);
    }
    catch (ex) {
        alertScreen();
    }
}
//Video
function doPlayVideoFromProcura(guid, javaName, objTxt, mainDiv, permaid, openPage) {
    doPlayVideo(permaid, guid);
    var objTxt = document.getElementById(objTxt);
    if (objTxt) {
        objTxt.value = unescape(javaName);
    }
    hidePreLoad(mainDiv);
}

function openPageVideo(permaID) {
    location.href('http://www.myway.pt/video_detalhe/' + permaID + '.aspx');
}

function putAllinOrder() {
    //alert("putAllinOrder")
    try {
        top.frmPlayer.putAllinOrder();
    }
    catch (ex) {
        //alert(ex)
    }
    var obj = document.getElementById("DivNotOK");
    if (obj) {
        obj.style.width = con.concat(document.documentElement.scrollWidth, "px");
        obj.style.height = con.concat(document.documentElement.scrollHeight, "px");
    }
    var obj = document.getElementById("IMGNotOK");
    if (obj) {
        obj.style.width = con.concat(document.documentElement.scrollWidth, "px");
        obj.style.height = con.concat(document.documentElement.scrollHeight, "px");
    }
    var obj = document.getElementById("NotOkSilver");
    if (obj) {
        var valorLeft = parseInt((document.documentElement.offsetWidth - 610) / 2);
        if (valorLeft < 0) {
            valorLeft = 0;
        }
        obj.style.position = "absolute";
        obj.style.left = con.concat(valorLeft, "px");
        obj.style.position = "fixed";

    }
    var obj = document.getElementById("NotOkMyway");
    if (obj) {
        var valorLeft = parseInt((document.documentElement.offsetWidth - 610) / 2);
        if (valorLeft < 0) {
            valorLeft = 0;
        }
        obj.style.position = "absolute";
        obj.style.left = con.concat(valorLeft, "px");
        obj.style.position = "fixed";

    }
}
function onSilverlightError(sender, args) {
    //putAllinOrder();
    //hidePreLoad("PUBPLAYER");
    //showPreLoad("DivNotOK");
    //showPreLoad("NotOkSilver");
}
function alertScreen() {
    putAllinOrder();
    try {
        top.frmPlayer.UnCleanMyScreen();
    }
    catch (ex) {
        //alert(ex)
    }
    hidePreLoad("PUBPLAYER");
    if (Silverlight.isInstalled("3.0.40624.0") == true) {
        showPreLoad("NotOkMyway");
    } else {
        showPreLoad("NotOkSilver");
    }
    showPreLoad("DivNotOK");
}
function fechaAlert() {
    if (Silverlight.isInstalled("3.0.40624.0") == true) {
        // Disabled : banner is no longer applicable to layout 2011
        //showPreLoad("PUBPLAYER");
    }
    hidePreLoad("DivNotOK");
    try {
        top.frmPlayer.CleanMyScreen();
    }
    catch (ex) {
        //alert(ex)
    }
}
function doNotShow(obj) {
    if (obj) {
        var data = new Date;
        var tsSimples = "";
        var DoPost = tr.concat("qual=HideSilverlightPOPUP&valor=", obj.checked);
        tsSimples = tr.concat(data.getFullYear(), "_", data.getMonth(), "_", data.getDate(), "_", data.getHours(), "_", data.getMinutes(), "_", data.getSeconds());
        if (xhrPedido == true) {
            xhr.abort();
        }
        xhrPedido = true;
        xhr = createXHR();
        if (xhr) {
            var xPagina = tr.concat('http://'+location.hostname + '/ground/xSaveCookie.aspx?ts=', tsSimples);
            xhr.open("POST", xPagina);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        //loadSearch(xhr.responseText, objNameText, objNameDestino, objNameNoResults, objNameLoading, objNameMainDiv);
                        xhrPedido = false;
                    }
                }
            };
            xhr.send(DoPost);
        }
    }
}


function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}
function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}
function mouseLeavesShare(element, evt) {
    var $teste = false;
    if (typeof evt.toElement != 'undefined' && evt.toElement.nodeName == 'DIV' && typeof element.contains != 'undefined') {
        $teste = !element.contains(evt.toElement);
    }
    else if (typeof evt.relatedTarget != 'undefined' && evt.relatedTarget && evt.relatedTarget.nodeName == 'DIV') {
        $teste = !elementContains(element, evt.relatedTarget);
    }
    if ($teste == true) {
        $teste = true;
    }
    return $teste;
}
function PopUp(objectName) {
    ShowBlanketFadeIn();
    CenterFixedDiv(objectName);
    $('#' + objectName).fadeIn("slow");
}

function PopOut(objectName) {
    HideBlanketFadeOut();
    $('#' + objectName).fadeOut("slow");
}

function refreshIframe() {
    var refresh = document.getElementById('mrecIFrame');
    refresh.contentDocument.location.reload(true);
}


function ganhaFocusTexto(obj, text) {
    if (obj != null) {
        if (obj.value == text) {
            obj.value = "";
        }
    }
}
function perdeFocusTexto(obj, text) {
    if (obj != null) {
        if (obj.value == '') {
            obj.value = text;
        }
    }
}

function escondenumero(numero, bnt) {
    hidePreLoad(numero);
    showPreLoad(bnt);
}

function motranumero(numero, bnt) {
    hidePreLoad(bnt);
    showPreLoad(numero);
}
function contaCaracteres(objTxtName, objNameDestino, maxCaracteres, objAlertName, defaultText) {
    var objDestino = document.getElementById(objNameDestino);
    var objAlert = document.getElementById(objAlertName);
    var objTxt = document.getElementById(objTxtName);
    if ((objDestino) && (objAlert) && (objTxt)) {
        var nCaracteres = objTxt.value.length;
        objDestino.innerHTML = nCaracteres;
        if (nCaracteres < maxCaracteres) {
            objDestino.style.color = "";
            objAlert.style.color = "";
            if ((objTxt.value != defaultText) && (objTxt.value != "")) {
                return true;
            } else {
                return false;
            }
        } else {
            objDestino.style.color = "red";
            objAlert.style.color = "red";
            return false;
        }
    }
}

function ChangeHeigth(objHolder, objBig, plus) {
    var objHolderCorrido = document.getElementById(objHolder);
    plus = parseInt(plus);
    var objCorrido = document.getElementById(objBig);
    if ((objHolderCorrido) && (objCorrido)) {
        var tr = "";
        var objCorridoValue = objCorrido.offsetHeight;
        objCorridoValue = objCorridoValue.toString().replace("px", "");
        objCorridoValue = parseInt(objCorridoValue);
        var valorTotal = objCorridoValue + plus;
        objHolderCorrido.style.height = tr.concat(valorTotal, "px");
    }
}

function ChangeMaxHeight(obj, valorMin, valorVerdadeiro) {
    valorMin = parseInt(valorMin);
    valorVerdadeiro = parseInt(valorVerdadeiro);
    if (typeof (obj) == "string") {
        obj = document.getElementById(obj);
    }
    if (obj) {
        if (valorVerdadeiro > valorMin) {
            obj.style.height = valorVerdadeiro + 'px';
        } else {
            obj.style.height = valorMin + 'px';
        }
    }
}
var DesT;
var DesTimerIsOn = false;
var DesTimerOut = 50;
var DesTimerPixeis = 20;
function DestMove(objHidden, Item) {
    var objimg = document.getElementById(Item);
    if (DesTimerIsOn == true) {
        DestMoveStop();
    }
    if (objimg) {
        var valorAnterior = objimg.style.left;
        valorAnterior = parseInt(valorAnterior.toString().replace("px", ""));
        if (valorAnterior < 0) {
            DesTimerIsOn = true;
            destMovePositive(objHidden, Item);
        } else if (valorAnterior > 0) {
            DesTimerIsOn = true;
            destMoveNegative(objHidden, Item);
        } else if (valorAnterior == 0) {
            DesTimerIsOn = false;
        }
    }
}
function DestMoveStop() {
    clearTimeout(DesT);
    DesTimerIsOn = false;
}
function destMovePositive(objHidden, Item) {
    if (DesTimerIsOn == true) {
        var NewvalorAnterior = 0;
        var StopTimer = false;
        var objimg = document.getElementById(Item);
        if (objimg) {
            NewvalorAnterior = objimg.style.left;
            NewvalorAnterior = parseInt(NewvalorAnterior.replace("px", ""));
        }
        var ValorFinal = NewvalorAnterior + DesTimerPixeis;
        var valorReal = Math.abs(NewvalorAnterior - ValorFinal);
        if (ValorFinal >= 0) {
            valorReal = Math.abs(NewvalorAnterior);
            ValorFinal = 0;
            StopTimer = true;
        }
        var objOrigem = document.getElementById(objHidden);
        if (objOrigem) {
            var arrayString = objOrigem.value;
            if (arrayString.indexOf("§") != -1) {
                var arrayArray = arrayString.split("§");
                for (var i = 0; i < arrayArray.length; i++) {
                    var objMovimentar = document.getElementById(arrayArray[i]);
                    if (objMovimentar) {
                        var valorAnterior = objMovimentar.style.left;
                        valorAnterior = valorAnterior.replace("px", "");
                        var valorNovo = 0;
                        valorNovo = parseInt(valorAnterior) + valorReal;
                        objMovimentar.style.left = valorNovo + "px";
                    }
                }
            }
        }
        if (StopTimer == true) {
            DestMoveStop()
        } else {
            DesT = setTimeout("destMovePositive('" + objHidden + "','" + Item + "')", DesTimerOut);
        }
    }
}
function destMoveNegative(objHidden, Item) {
    if (DesTimerIsOn == true) {
        var NewvalorAnterior = 0;
        var StopTimer = false;
        var objimg = document.getElementById(Item);
        if (objimg) {
            NewvalorAnterior = objimg.style.left;
            NewvalorAnterior = parseInt(NewvalorAnterior.replace("px", ""));
        }
        var ValorFinal = NewvalorAnterior - DesTimerPixeis;
        var valorReal = Math.abs(NewvalorAnterior - ValorFinal);
        if (ValorFinal <= 0) {
            valorReal = Math.abs(NewvalorAnterior);
            ValorFinal = 0;
            StopTimer = true;
        }
        var objOrigem = document.getElementById(objHidden);
        if (objOrigem) {
            var arrayString = objOrigem.value;
            if (arrayString.indexOf("§") != -1) {
                var arrayArray = arrayString.split("§");
                for (var i = 0; i < arrayArray.length; i++) {
                    var objMovimentar = document.getElementById(arrayArray[i]);
                    if (objMovimentar) {
                        var valorAnterior = objMovimentar.style.left;
                        valorAnterior = valorAnterior.replace("px", "");
                        var valorNovo = 0;
                        valorNovo = parseInt(valorAnterior) - valorReal;
                        objMovimentar.style.left = valorNovo + "px";
                    }
                }
            }
        }
        if (StopTimer == true) {
            DestMoveStop()
        } else {
            DesT = setTimeout("destMoveNegative('" + objHidden + "','" + Item + "')", DesTimerOut);
        }
    }
}

function changebntsColor(obj, hidenName, cor1, cor2) {
    var objHiden = document.getElementById(hidenName);
    if (objHiden) {
        var arrayString = objHiden.value;
        if (arrayString.indexOf("§") != -1) {
            var arrayArray = arrayString.split("§");
            for (var i = 0; i < arrayArray.length; i++) {
                var objMovimentar = document.getElementById(arrayArray[i]);
                if (objMovimentar) {
                    objMovimentar.style.color = cor2;
                }
            }
        }
    }
    if (obj) {
        obj.style.color = cor1;
    }
}
function moveNextItem(nextId) {
    var obj = document.getElementById(nextId);
    if (obj != null) {
        document.getElementById(nextId).focus();
    }
}

//|
window.shardIfSmallImg = function (url) {
    var s = url.indexOf('-s') !== -1,
        m = url.indexOf('-m') !== -1;
    if (url.indexOf('http://') !== -1 && ( s || m )) {
        var ixOfGuid = url.lastIndexOf('/') + 1,
            firstChar = url.charAt(ixOfGuid),
            guid = url.slice(url.ixOfGuid).slice(0, url.length - 6);
        return 'http://' + firstChar + '.services.myway.pt/Image/' + guid + '?size=' + ( s ? '50' : '300' );
    } else
        return url;
};

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = shardIfSmallImg(x.oSrc);
}
function MM_swapimgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = shardIfSmallImg(x.oSrc);
}
function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = shardIfSmallImg(a[i]); }
    }
}

function MM_showHideLayers() { //v6.0
    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 MM_displayLayers() { //v6.0
    var i, p, v, obj, args = MM_displayLayers.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') ? 'block' : (v == 'hide') ? 'none' : v; }
        obj.display = v;
    }
}
function MM_findObj(n, d) { //v4.01
    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 && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function MM_changeImage(imageName, newImageUrl) {
    var image = document.getElementById(imageName);

    image.src = newImageUrl;
    FitImageWindow(image);
}

function FitImageWindow(image) {
    var viewportwidth;
    var viewportheight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0) {
        viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
    }

    // older versions of IE

    else {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    var newImg = new Image();
    newImg.src = shardIfSmallImg(image.src);
    var ratio = 0.0;
    if (newImg.height != 0) {
        ratio = newImg.width / newImg.height;
        image.height = viewportheight - 150;
        image.width = (viewportheight - 150) * ratio;
    }
}

function getDaysInMonth(mymonthG, myyearG) {
    var days;
    mymonth = mymonthG + 1;
    if (mymonth == 1 || mymonth == 3 || mymonth == 5 || mymonth == 7 || mymonth == 8 || mymonth == 10 || mymonth == 12) {
        days = 31;
    }
    else if (mymonth == 4 || mymonth == 6 || mymonth == 9 || mymonth == 11) {
        days = 30;
    }
    else if (mymonth == 2) {
        if (isLeapYear(myyearG)) {
            days = 29;
        } else {
            days = 28;
        }
    }
    return (days);
}
function isLeapYear(YearLeap) {
    if (((YearLeap % 4) == 0) && ((YearLeap % 100) != 0) || ((YearLeap % 400) == 0)) {
        return (true);
    } else {
        return (false);
    }
}
function createXHR() {
    var request = false;
    try {
        request = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (err2) {
        try {
            request = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (err3) {
            try {
                request = new XMLHttpRequest();
            }
            catch (err1) {
                request = false;
            }
        }
    }
    return request;
}


function IsValidTime(timeStr) {
    var timePat = /^(\d{1,2}):(\d{1,2}):(\d{1,2})/;
    var matchArray = timeStr.match(timePat);
    if (matchArray == null) {
        return false;
    }
    hour = matchArray[1];
    //alert(hour);
    minute = matchArray[2];
    //alert(minute);
    second = matchArray[3];
    //alert(second);
    if (second == "") { second = null; }
    if (hour < 0 || hour > 23) {
        return false;
    }
    if (minute < 0 || minute > 59) {
        return false;
    }
    if (second != null && (second < 0 || second > 59)) {
        return false;
    }
    return true;
}
function isEmail(campoEmail) {
    var padraoEmail2 = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
    return padraoEmail2.test(campoEmail);
}
function justNumbers(obj) {
    reg = /[^0-9]/g;
    obj.value = obj.value.replace(reg, "");
}
function money(obj) {
    reg = /[^0-9.,]/g;
    obj.value = obj.value.replace(reg, "");
}
function removePercent(obj) {
    reg = /[%]/g;
    obj = obj.replace(reg, " ");
    reg = /[*]/g;
    obj = obj.replace(reg, " ");
    reg = /[']/g;
    return obj.replace(reg, " ");
}
function readyForFilter(obj) {
    if (obj != null) {
        obj.value = removePercentWithSpaces(obj.value);
        obj.value = obj.value.split('  ').join(' ');
        obj.value = "%" + obj.value + "%";
        obj.value = obj.value.split('%%').join('%');
    }
}
function readyForFilterSql(obj) {
    if (obj != null) {
        obj.value = removePercentWithSpaces(obj.value);
        obj.value = removeSpacesWithPercent(obj.value);
    }
}
function limpaFiltro(obj) {
    if (obj != null) {
        obj.value = removePercentWithSpaces(obj.value);
    }
}
function numbersAndAlpha(obj) {
    reg = /[^A-Za-z0-9]/g;
    obj.value = obj.value.replace(reg, "");
}
function numbersAndAlphaAndPoint(obj) {
    reg = /[^A-Za-z0-9_.]/g;
    obj.value = obj.value.replace(reg, "");
}
function showPreLoad(a) {
    var objPreload = document.getElementById(a);
    if (objPreload != null) {
        objPreload.style.visibility = "visible";
        objPreload.style.display = "block";
    }
}
function getScrollPosY() {
    if (document.documentElement.scrollTop != null) {
        return document.documentElement.scrollTop;
    }
    if (window.pageYOffset != null) {
        return window.pageYOffset;
    }
    return 0;
}

function hidePreLoad(a) {
    var objPreload = document.getElementById(a);
    if (objPreload != null) {
        objPreload.style.visibility = "hidden";
        objPreload.style.display = "none";
    }
}
function removeSpaces(string) {
    return string.split(' ').join('');
}
function removeSpacesWithPercent(stringmy) {
    var rwor = "%" + stringmy.split(' ').join('%') + "%";
    rwor = rwor.split('%%').join('%');
    return rwor;
}
function removePercentWithSpaces(stringnew) {
    var working = stringnew.split('%').join(' ');
    working = working.split('  ').join(' ');
    if (working.substring(0, 1) == " ") {
        working = working.substring(1);
    }
    if (working.substring(working.length - 1) == " ") {
        working = working.substring(0, working.length - 1)
    }
    return working;
}


////////////////////////////////////////////
function Position(x, y) {
    this.X = x;
    this.Y = y;

    this.Add = function(val) {
        var newPos = new Position(this.X, this.Y);
        if (val != null) {
            if (!isNaN(val.X))
                newPos.X += val.X;
            if (!isNaN(val.Y))
                newPos.Y += val.Y
        }
        return newPos;
    }

    this.Subtract = function(val) {
        var newPos = new Position(this.X, this.Y);
        if (val != null) {
            if (!isNaN(val.X))
                newPos.X -= val.X;
            if (!isNaN(val.Y))
                newPos.Y -= val.Y
        }
        return newPos;
    }

    this.Min = function(val) {
        var newPos = new Position(this.X, this.Y)
        if (val == null)
            return newPos;

        if (!isNaN(val.X) && this.X > val.X)
            newPos.X = val.X;
        if (!isNaN(val.Y) && this.Y > val.Y)
            newPos.Y = val.Y;

        return newPos;
    }

    this.Max = function(val) {
        var newPos = new Position(this.X, this.Y)
        if (val == null)
            return newPos;

        if (!isNaN(val.X) && this.X < val.X)
            newPos.X = val.X;
        if (!isNaN(val.Y) && this.Y < val.Y)
            newPos.Y = val.Y;

        return newPos;
    }

    this.Bound = function(lower, upper) {
        var newPos = this.Max(lower);
        return newPos.Min(upper);
    }

    this.Check = function() {
        var newPos = new Position(this.X, this.Y);
        if (isNaN(newPos.X))
            newPos.X = 0;
        if (isNaN(newPos.Y))
            newPos.Y = 0;
        return newPos;
    }

    this.Apply = function(element) {
        if (typeof (element) == "string")
            element = document.getElementById(element);
        if (element == null)
            return;
        if (!isNaN(this.X))
            element.style.left = this.X + 'px';
        if (!isNaN(this.Y))
            element.style.top = this.Y + 'px';
    }
}

function dragObject(element, attachElement, lowerBound, upperBound, startCallback, moveCallback, endCallback, attachLater) {
    if (typeof (element) == "string")
        element = document.getElementById(element);
    if (element == null)
        return;

    if (lowerBound != null && upperBound != null) {
        var temp = lowerBound.Min(upperBound);
        upperBound = lowerBound.Max(upperBound);
        lowerBound = temp;
    }

    var cursorStartPos = null;
    var elementStartPos = null;
    var dragging = false;
    var listening = false;
    var disposed = false;

    function dragStart(eventObj) {
        if (dragging || !listening || disposed) return;
        dragging = true;

        if (startCallback != null)
            startCallback(eventObj, element);

        cursorStartPos = absoluteCursorPostion(eventObj);

        elementStartPos = new Position(parseInt(element.style.left), parseInt(element.style.top));

        elementStartPos = elementStartPos.Check();

        hookEvent(document, "mousemove", dragGo);
        hookEvent(document, "mouseup", dragStopHook);

        return cancelEvent(eventObj);
    }

    function dragGo(eventObj) {
        if (!dragging || disposed) return;

        var newPos = absoluteCursorPostion(eventObj);
        newPos = newPos.Add(elementStartPos).Subtract(cursorStartPos);
        newPos = newPos.Bound(lowerBound, upperBound)
        newPos.Apply(element);
        if (moveCallback != null)
            moveCallback(newPos, element);

        return cancelEvent(eventObj);
    }

    function dragStopHook(eventObj) {
        dragStop();
        return cancelEvent(eventObj);
    }

    function dragStop() {
        if (!dragging || disposed) return;
        unhookEvent(document, "mousemove", dragGo);
        unhookEvent(document, "mouseup", dragStopHook);
        cursorStartPos = null;
        elementStartPos = null;
        if (endCallback != null)
            endCallback(element);
        dragging = false;
    }

    this.Dispose = function() {
        if (disposed) return;
        this.StopListening(true);
        element = null;
        attachElement = null
        lowerBound = null;
        upperBound = null;
        startCallback = null;
        moveCallback = null
        endCallback = null;
        disposed = true;
    }

    this.StartListening = function() {
        if (listening || disposed) return;
        listening = true;
        hookEvent(attachElement, "mousedown", dragStart);
    }

    this.StopListening = function(stopCurrentDragging) {
        if (!listening || disposed) return;
        unhookEvent(attachElement, "mousedown", dragStart);
        listening = false;

        if (stopCurrentDragging && dragging)
            dragStop();
    }

    this.IsDragging = function() { return dragging; }
    this.IsListening = function() { return listening; }
    this.IsDisposed = function() { return disposed; }

    if (typeof (attachElement) == "string")
        attachElement = document.getElementById(attachElement);
    if (attachElement == null)
        attachElement = element;

    if (!attachLater)
        this.StartListening();
}
function correctOffset(pos, offset, neg) {
    if (neg)
        return pos.Subtract(offset);
    return pos.Add(offset);
}

function hookEvent(element, eventName, callback) {
    if (typeof (element) == "string")
        element = document.getElementById(element);
    if (element == null)
        return;
    if (element.addEventListener) {
        element.addEventListener(eventName, callback, false);
    }
    else if (element.attachEvent)
        element.attachEvent("on" + eventName, callback);
}

function unhookEvent(element, eventName, callback) {
    if (typeof (element) == "string")
        element = document.getElementById(element);
    if (element == null)
        return;
    if (element.removeEventListener)
        element.removeEventListener(eventName, callback, false);
    else if (element.detachEvent)
        element.detachEvent("on" + eventName, callback);
}

function cancelEvent(e) {
    e = e ? e : window.event;
    if (e.stopPropagation)
        e.stopPropagation();
    if (e.preventDefault)
        e.preventDefault();
    e.cancelBubble = true;
    e.cancel = true;
    e.returnValue = false;
    return false;
}

function getMousePos(eventObj) {
    eventObj = eventObj ? eventObj : window.event;
    var pos;
    if (isNaN(eventObj.layerX))
        pos = new Position(eventObj.offsetX, eventObj.offsetY);
    else
        pos = new Position(eventObj.layerX, eventObj.layerY);
    return correctOffset(pos, pointerOffset, true);
}

function getEventTarget(e) {
    e = e ? e : window.event;
    return e.target ? e.target : e.srcElement;
}

function absoluteCursorPostion(eventObj) {
    eventObj = eventObj ? eventObj : window.event;

    if (isNaN(window.scrollX))
        return new Position(eventObj.clientX + document.documentElement.scrollLeft + document.body.scrollLeft,
                  eventObj.clientY + document.documentElement.scrollTop + document.body.scrollTop);
    else
        return new Position(eventObj.clientX + window.scrollX, eventObj.clientY + window.scrollY);
}
var pointerOffset = new Position(0, navigator.userAgent.indexOf("Firefox") >= 0 ? 1 : 0);

function mouseLeavesElement(element, evt) {
    if (typeof evt.toElement != 'undefined' && typeof element.contains != 'undefined') {
        return !element.contains(evt.toElement);
    }
    else if (typeof evt.relatedTarget != 'undefined' && evt.relatedTarget) {
        return !elementContains(element, evt.relatedTarget);
    }
}

function elementContains(container, containee) {
    while (containee) {
        if (container == containee) {
            return true;
        }
        containee = containee.parentNode;
    }
    return false;
}
function gotoanchor(sAnchor) {
    var objAnc = document.getElementById(sAnchor);
    if (objAnc) {
        objAnc.focus();
    }
    return true;
}

function changelinkColor(objID, cor) {
    var obj = document.getElementById(objID);

    if (obj) {
        obj.style.color = cor;
    }
}

////////////////////////////////////////////


function toggle(div_id) {
    var el = document.getElementById(div_id);
    if (el.style.display == 'none') { el.style.display = 'block'; }
    else { el.style.display = 'none'; }
}
function blanket_size(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById('blanket');
    blanket.style.height = blanket_height + 'px';
    var popUpDiv = document.getElementById(popUpDivVar);
    popUpDiv_height = blanket_height / 2 - 150; //150 is half popup's height
    popUpDiv.style.top = '100px';
}
function window_pos(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerHeight;
    } else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
        window_width = viewportwidth;
    } else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
            window_width = document.body.parentNode.clientWidth;
        } else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
    var popUpDiv = document.getElementById(popUpDivVar);
    window_width = window_width / 2 - 300; //150 is half popup's width
    popUpDiv.style.marginleft = 'auto';
    popUpDiv.style.marginright = 'auto';
    popUpDiv.style.position = 'fixed';
    popUpDiv.style.top = window.document.body.scrollTop + 100;
}
function popuppassatempos(windowname) {
    blanket_size(windowname);
    window_pos(windowname);
    //CenterFixedDiv(windowname);
    toggle('blanket');
    toggle(windowname);
}

function setTitle(text, divName) {
    div = document.getElementById(divName);
    div.innerHTML = text;
}

function RecentPostsScrollerv2(json) {
    var sHeadLines;
    var sPostURL;
    var objPost;
    var sMoqueeHTMLStart;
    var sMoqueeHTMLEnd;
    var sPoweredBy;
    var sHeadlineTerminator;
    var sPostLinkLocation;


    try {

        sMoqueeHTMLStart = "\<MARQUEE onmouseover=\"this.stop();\" onmouseout=\"this.start();\" ";


        if (nWidth) {
            sMoqueeHTMLStart = sMoqueeHTMLStart + " width = \"" + nWidth + "%\"";
        }
        else {
            sMoqueeHTMLStart = sMoqueeHTMLStart + " width = \"100%\"";
        }

        if (nScrollDelay) {
            sMoqueeHTMLStart = sMoqueeHTMLStart + " scrolldelay = \"" + nScrollDelay + "\"";
        }
        if (sDirection) {
            sMoqueeHTMLStart = sMoqueeHTMLStart + " direction = \"" + sDirection + "\"";

            if (sDirection == "left" || sDirection == "right") {
                //For left and right directions seperate the headilnes by two spaces. 
                sHeadlineTerminator = "&nbsp;&nbsp;";
            }
            else {
                //For down and up directions seperate headlines by new line
                sHeadlineTerminator = "\<br/\>";
            }
        }
        if (sOpenLinkLocation == "N") {
            sPostLinkLocation = " target= \"_blank\" ";
        }
        else {
            sPostLinkLocation = " ";
        }


        sMoqueeHTMLEnd = "\</MARQUEE\>"

        sHeadLines = "";

        for (var nFeedCounter = 0; nFeedCounter < nMaxPosts; nFeedCounter++) {
            objPost = json.feed.entry[nFeedCounter];


            for (var nCounter = 0; nCounter < objPost.link.length; nCounter++) {
                if (objPost.link[nCounter].rel == 'alternate') {
                    sPostURL = objPost.link[nCounter].href;
                    break;
                }
            }
            sHeadLines = sHeadLines + "\<b\>" + sBulletChar + "\</b\> \<a " + sPostLinkLocation + " href=\"" + sPostURL + "\">" + objPost.title.$t + "\</a\>" + sHeadlineTerminator;
        }


        document.write(sMoqueeHTMLStart + sHeadLines + sMoqueeHTMLEnd)

    }
    catch (exception) {
        alert(exception);
    }
}

function playRadioAndNavigate($radio, $pagina) {
    doPlayRadio($radio);
    top.setLocation($pagina);
}



