function GetQSParameter(querypar) {
    var querystr = new Array();

    if (arguments.length > 1)
        loc = arguments[1].substr(1).split('&');
    else
        loc = window.location.search.substr(1).split('&');
    
    if ((loc != '') && (loc != null)) {
        for (var icnt = 0; icnt < loc.length; icnt++) {
            var q = loc[icnt].split('=');
            if(q[1] != null && q[1] != "") querystr[q[0].toLowerCase()] = decodeURIComponent(q[1]);
        }
        return querystr[querypar];
    } else {
        return "";
    }
}

function GetStrippedQS(qstostrip) {
    var newLoc = new Array();
    var filt;
    loc = window.location.search.substr(1).split('&');
    if ((loc != '') && (loc != null)) {
        for (var icnt = 0; icnt < loc.length; icnt++) {
            var q = loc[icnt].split('=');
            filt = false;

            for (var qsscnt = 0; qsscnt < qstostrip.length; qsscnt++) {
                if (qstostrip[qsscnt].toLowerCase() == q[0].toLowerCase()) { filt = true; break; }
            }

            if(!filt && (q[1] != null && q[1] != "")) newLoc.push(q[0] + "=" + q[1]);
        }

        if (newLoc.length > 0) return "?" + newLoc.join("&");

        return "";
    } else {
        return "";
    }
}

function GetHrefParts(link) {
    var hParts = {
        href: "",
        qs: new Array(),
        hash: ""
    }

    aHParts = link.split("?");

    if (aHParts.length > 1) {
        hParts.href = aHParts[0];
        aQSParts = aHParts[1].split("#");
        if (aQSParts.length > 1) hParts.hash = aQSParts[1];

        aQSFields = aQSParts[0].split("&");
        aQSFieldParts = new Array();
        for(var ix = 0; ix < aQSFields.length; ix++) {
            aQSFieldParts = aQSFields[ix].split("=");
            hParts.qs.push({ qf: aQSFieldParts[0] , val: decodeURIComponent(aQSFieldParts[1])});
        }
    } else {
        aHParts = link.split("#");
        hParts.href = aHParts[0];
        if (aHParts.length > 1) hParts.hash = aHParts[1];
    }
    
    return hParts;
}

function BuildLinkFromParts(hParts) {
    var hrefStr = hParts.href;

    if (hParts.qs.length > 0) {
        hrefStr += "?";
        qsFields = new Array();

        for (var ix = 0; ix < hParts.qs.length; ix++) qsFields.push(hParts.qs[ix].qf + "=" + encodeURIComponent(hParts.qs[ix].val));

        hrefStr += qsFields.join("&");
    }

    if (hParts.hash != "") hrefStr += "#" + hParts.hash;

    return hrefStr;
}

function getWindowSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Não-IE (padrão W3C)
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ em modo 'standards compliant'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    return new Array(myWidth, myHeight);
}

function resizeByOnCenter(x, y) {
    nx = x - parseInt(x / 2);
    ny = y - parseInt(y / 2);

    window.moveBy(-nx, -ny);
    window.resizeBy(x, y);
}

function isDef(variavel) {
	return (typeof(variavel) != "undefined");
}

function IsLogado() {
    var isLogado = false;

    $.ajax({
        type: "GET",
        url: "/Conta/IsLogado",
        timeout: 3000,
        dataType: "json",
        async: false,
        success: function(auth) {
            isLogado = (auth.isLogado == "1" ? true : false);
        },
        error: function(xhr, textStatus, errorThrown) {
            alert("Erro ao obter status de autenticação:\n" + (textStatus ? textStatus : errorThrown));
        }
    });

    return isLogado;
}
