﻿
/******************************/

function OpenWindow(theUrl) {
    var parameters = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=500,left=10,top=10";
    var theWindow = window.open(theUrl, "_blank", parameters);
    theWindow.focus();
}

function RemoveBanner() {

    if (typeof (trialNode) != "undefined") {
        trialNode.style.visibility = 'hidden';
    }
}

/*********** StrRedir *********/

function myUnescape(theString) {
    //  bugs bugs and more bugs bugs
    theString = unescape(theString);
    theString = theString.replace("Ã§", "ç");
    theString = theString.replace("Ã£", "ã");
    return theString;
}

function VoltarOuFechar() {

    var numBrackets = 0;
    var strRedir = GetUriParameter("StrRedir");
    if (strRedir != null) { // pode ser que o cara queira o botao voltar
        numBrackets = RetiraRedir(GetMyOrdexUrl(), strRedir).split("{").length;
    }

    if (numBrackets > 1) { // botao voltar
        document.write("<center><div align='left' style='width:90%;'><a href='javascript:BotaoVoltar();'><img alt='Retornar a tela anterior' src='../Imagem/Voltar.gif' border=0 /></a></div></center>");
    } else { // botao fechar ou nada 
        if (opener != undefined && opener != null) { // botao fechar
            document.write("<div align='center' ><input type='button' value='Fechar' class='Button' onclick='window.close()' /></div>");
        }
    }

}

function OpenOrdexUrl(targetUrl, parametros) {
    location.href = GotoOrdexUrl(targetUrl, parametros);
}

function GotoOrdexUrl(targetUrl, parametros) {

    var strRedir;
    var myUrl = GetMyOrdexUrl();


    strRedir = GetUriParameter("StrRedir");

    strRedir = myUnescape(strRedir);
    var thisUrlParameters = "{" + targetUrl + "|" + parametros + "}";

    if (strRedir.indexOf(myUrl) < 0) {
        strRedir = "?StrRedir=" + escape(thisUrlParameters + "{" + myUrl + "|}" + strRedir);
    } else {
        strRedir = "?StrRedir=" + escape(thisUrlParameters + strRedir);
    }

    return targetUrl + strRedir;
}



function BotaoVoltar(myUrl) {

    if (myUrl == undefined || myUrl == null) {
        myUrl = GetMyOrdexUrl();
    }

    var strRedir;

    strRedir = (GetUriParameter("StrRedir"));

    strRedir = RetiraRedir(myUrl, strRedir);

    strRedir = ProgRedit(strRedir) + "?StrRedir=" + strRedir;

    location.href = strRedir;
}

function GetMyOrdexUrl() {
    var baseUrl = location.pathname;
    var penultimaBarra = baseUrl.substring(baseUrl, baseUrl.lastIndexOf('/')).lastIndexOf('/');
    var saida = ".." + baseUrl.substring(penultimaBarra, baseUrl.length);
    return saida;

}

//   'Retorna o programa para redirecionamento
function ProgRedit(strRedir) {

    var posFim = strRedir.indexOf("aspx");
    if (posFim < 1) {
        return "";
    }
    var saida = strRedir.substring(1, posFim + 4);
    return saida;

}

function GetUriParameter(name) {
    var x = location.search;

    var pos = x.indexOf(name);
    if (pos < 0) return null
    x = x.substring(pos + name.length + 1);

    if (x.indexOf('&') >= 0) {
        x = x.substring(0, x.indexOf('&'));
    }
    x = myUnescape(x);
    return x;
}

function RetiraRedir(needle, haystack) {
    needle = needle.toLowerCase();
    var haystack2 = haystack.toLowerCase();

    if (haystack2.indexOf(needle) >= 0) {
        var posFim = haystack2.indexOf("}");
        var haystackSize = haystack2.length;
        return haystack.substring(posFim + 1, haystackSize);
    }
    return haystack;
}

function GetStrRedirParameter(strRedir, parameter) {

    var reRaw = "[{|]" + parameter + "=([\\d\\s]+)";
    var re = new RegExp(reRaw);
    //        alert(re);
    var m = re.exec(strRedir);
    // alert(m);
    if (m == null) {
        return false;
    }
    if (m.length >= 2) {
        return m[1];
    }
    return m[0]; // should not happend 
}

function GetThisStrRedirParameter(parameter) {
    return GetStrRedirParameter(GetUriParameter("StrRedir"), parameter);
}


function ReloadComParametro(parametro, valor) {
    // recarrega a pagina atual, adicionando 'parametro=valor' na URL ( location.search )
    var theSearch = location.search;

    if (GetUriParameter(parametro) != null) {
        // tem que mudar o parametro atual
        var pos = theSearch.indexOf(parametro + "=");
        var theChar = theSearch.charAt(pos - 1);
        var pos2 = pos + parametro.length + 1;
        var subStr = theSearch.substring(pos2, theSearch.length);
        var p2 = subStr.indexOf("&");
        if (p2 < 0) p2 = subStr.length;


        var theUrl = theSearch.substring(0, pos2) +
        valor + subStr.substring(p2, subStr.length);

        location.href = theUrl;

    } else {
        // nao tem. Basta adicionar 'parametro=valor'

        theSearch = "?" + parametro + "=" + valor + "&" + theSearch.substring(1, theSearch.length);
        //alert(theSearch);
        location.href = theSearch;
    }
}



/*********** StrRedir *********/

function min(n1, n2) {
    if (n1 == null && n2 == null) return null;
    if (n1 == null) return n2;
    if (n2 == null) return n1;
    if (n1 > n2) return n2;
    return n1;
}


/************************* INSPECT ***************/
function PrettyPlot(text) {
    var w = window.open("about:blank", "prettyPlot");
    w.document.open();
    w.document.write("<form><textarea rows=37 cols=100>" + text + "</textarea></form>");
    w.document.close();
    w.focus();

}

function Inspect(xxx) {
    var saida = "";
    for (var x in xxx) {
        saida += x + "\t\t" + xxx[x] + "\n"; ;
    }
    return saida;
}


function InspectGui(theObject) {
    PrettyPlot(Inspect(theObject));
}



