﻿/* JScript File */

var tableFilterCount = new Array();

function FilterTable(objectId, dt, column, value, filterMode, caseType)
{
    if (tableFilterCount[objectId] == null || tableFilterCount[objectId] == "")
    {
        tableFilterCount[objectId] = 0;
    }
    
    dt.filter(column, value, filterMode, caseType);
    
    return ++(tableFilterCount[objectId]);
}

function UnfilterTable(objectId, dt, stackPosition)
{
    if (tableFilterCount[objectId] == null || tableFilterCount[objectId] == "")
    {
        tableFilterCount[objectId] = 0;
    }
    
    for (var i = tableFilterCount[objectId]; i > stackPosition; i--)
    {
        dt.unfilter();
    }
    
    tableFilterCount[objectId] = (tableFilterCount[objectId] == 0 ? 0 : stackPosition);
}

function Trim(texto) 
{ 
    if (texto != null)
    {
	    while (texto.substring(0,1) == " ") 
	    {
		    texto = texto.substring(1, texto.length);
	    }
	    while (texto.substring(texto.length - 1, texto.length) == " ") 
	    {
		    texto = texto.substring(0, texto.length - 1);
	    }
	}
	return texto;
} 

function GanhaFoco(e, texto)
{
    e.select();
    if (Trim(e.value) == texto)
    {
        e.value = "";
    }
}

function PerdeFoco(e, texto)
{
    if (Trim(e.value) == "")
    {
        e.value = texto;
    }
}

function dadosNumericos(e) 
{
    try
    {
        if (!e)
        {
            e = window.event;	
        }
        var key;
        if (e.charCode) 
        {
            key = e.charCode; /* For Gecko */
        } else if (e.keyCode) 
        {
            key = e.keyCode; /* For Presto */
        }
        if (key != 37 && key != 38 && key != 39 && key != 40 && key != 35 && key != 46 && key != 8 && key != 36) 
        {
            if (key < 47 || key > 57) 
            {
                if (e.preventDefault) 
                {
                    e.preventDefault(); /* For Presto and Gecko */
                } else 
                {
                    return false; /* For Trident */
                }
            }
        }
    }
    catch (ex) 
    { 
        return false;
    } 
}

function keyPress()
{
    var e = event;	
    if (e.charCode) /* For Gecko */
    {
        if (e.charCode == 13)
        {
            e.charCode = 9;
        } 
    } 
    else if (e.keyCode) /* For Presto */
    {
        if(e.keyCode==13) e.keyCode=9;
    }
}

function getAbsoluteTop(obj, objContainer) 
{
	/* Get top position from the parent object */
	var oTop = obj.offsetTop;            
	/* Parse the parent hierarchy up to the document element */
	while(obj.offsetParent!=null) 
	{ 
	    /* Get parent object reference */
		var oParent = obj.offsetParent;
		if (objContainer != null && objContainer.id == oParent.id)
		{
		    break;
		}
		/* Add parent top position */
		oTop += oParent.offsetTop;
		obj = oParent;
	}
	/* Return top position */
	return oTop;
}

function getAbsoluteLeft(obj, objContainer) 
{
	/* Get Left position from the parent object */
	var oLeft = obj.offsetLeft;            
	/* Parse the parent hierarchy up to the document element */
	while(obj.offsetParent!=null) 
	{ 
	    /* Get parent object reference */
		var oParent = obj.offsetParent;
		if (objContainer != null && objContainer.id == oParent.id)
		{
		    break;
		}
		/* Add parent Left position */
		oLeft += oParent.offsetLeft;
		obj = oParent;
	}
	/* Return Left position */
	return oLeft;
}

function getScrollXY()
{
  var scrOfX = 0;
  var scrOfY = 0;
  if (typeof(window.pageYOffset) == 'number') 
  {
    /* Netscape compliant */
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } 
  else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) 
  {
    /* DOM compliant */
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } 
  else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) 
  {
    /* IE6 standards compliant mode */
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [scrOfX, scrOfY];
}

function getSizeXY()
{     
    var height = 0;
    var width = 0;
    
    if (window.innerHeight && window.scrollMaxY)
    {// Firefox
        height = window.innerHeight + window.scrollMaxY;
        width = window.innerWidth + window.scrollMaxX;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight)
    { // all but Explorer Mac
        height = document.body.scrollHeight;
        width = document.body.scrollWidth;
    }
    else
    { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        height = document.body.offsetHeight + document.body.offsetTop;
        width = document.body.offsetWidth + document.body.offsetLeft;
    }     
    
    return [width, height]; 
} 

function resizeIFrame(id) 
{
    var iframe = document.getElementById(id);
//    iframe.height = iframe.document.body.scrollHeight;
    iframe.height = "88px";
}

/*************************************************************************************************************************************************/
/*                                                             ABRE UMA URL                                                                      */
/*************************************************************************************************************************************************/
function openNewWindow(url)
{
    var validated = false;
    
    try
    {
        var newWindow = window.open(url);
        if (newWindow && !newWindow.closed)
        {
            newWindow.focus();
            
            validated = true;
        }
    }
    catch (e) { }
    if (!validated)
    {
        window.location = url;
    }
}

/*************************************************************************************************************************************************/
/*                                                             RENDER DE AVISO                                                                   */
/*************************************************************************************************************************************************/
function ExibeAviso(texto, desabilitaConteudo)
{   
    /* posiciona o aviso no centro da tela */
    var sizeXY = getSizeXY();
    var scrollXY = getScrollXY();
    $("popupWindow").style.top = ((sizeXY[1] * 0.10) + scrollXY[1]) + "px";

    if (desabilitaConteudo == true)
    {
        $("divDesabilitaConteudo").style.display = "block";
    }
    
    $("popup").document.write(texto);
    $("popupWindow").style.display = 'block';
    $("popup").document.close();             
}

/*************************************************************************************************************************************************/
/*                                                             RENDER DE AGUARDE                                                                 */
/*************************************************************************************************************************************************/
 
/*

Licence: GPL-V3

Autor: Leonardo Vit¢rio de Souza Stuginski

Data: 2007/07/05

Classes: ThreadSettings, Thread

*/

var ThreadSettings = new Object();
ThreadSettings.queue = new Array();
ThreadSettings.delay = 1000;
ThreadSettings.launch = 10;

ThreadSettings.addThread = function(thread)
{
    var index = ThreadSettings.queue.length;
    ThreadSettings.queue[index] = thread;
    return index;
};

ThreadSettings.run = function()
{
    if(ThreadSettings.queue.length < 500)
    {
        /* executa os processos */
        for(var i=0; i < ThreadSettings.queue.length; i++)
        {
            /* verifica se a thread não foi removida ou esta dormindo */
            /* alert(i+" >> "+ThreadSettings.queue[i].sleepTime); */
            if(ThreadSettings.queue[i] != null)
            {
                if(ThreadSettings.queue[i].sleepTime <= 0)
                {
                    setTimeout("ThreadSettings.queue["+i+"].background()",ThreadSettings.launch);
                }
                else
                {
                    /* alert(ThreadSettings.queue[i].sleepTime); */
                    ThreadSettings.queue[i].sleepTime -= ThreadSettings.delay;
                }
            }
        }
    }
    else
    {
        /* executa garbade collector */
        var clearQueue = new Array();
        for(var i=0; i < ThreadSettings.queue.length; i++)
        {
            if(ThreadSettings.queue[i] != null)
            {
                clearQueue[i] = ThreadSettings.queue[i];
            }
        }
        ThreadSettings.queue[i] = clearQueue();
    }
    setTimeout("ThreadSettings.run()",ThreadSettings.delay);
};

ThreadSettings.remove = function(index)
{
    /* remove a Thread */
    ThreadSettings.queue[index] = null;
};

ThreadSettings.sleep = function(index,time)
{
    /* sleep */
    ThreadSettings.queue[index].sleepTime = time;
};

ThreadSettings.run();

Thread = function()
{
    this.runnning = false;
    this.retValue = false;
    
    this.index = 0;
    this.sleepTime = 0;
    this.start = function() 
    {
        this.index = ThreadSettings.addThread(this);
    };
    this.background = function()
    {
        if (this.runnning == false)
        {
            this.runnning = true;
            
            retValue = this.run();
        }
        
        if (retValue == true)
        {
            this.end();
        }
        else
        {
            this.start();
        }
    } ;
    this.run = function() {};
    this.end = function() 
    {
        this.destructor();ThreadSettings.remove(this.index);
    };
    this.destructor = function() {};
    this.sleep = function(time) 
    {
        ThreadSettings.sleep(this.index,time);
    };
};

/* Thread Example */
/*
var teste = new Thread();

teste.run = function(){alert(this.index+" : "+ThreadSettings.queue[this.index].sleepTime);this.end();}

teste.start();

var teste2 = new Thread();

teste2.run = function(){alert(this.index+" : "+ThreadSettings.queue[this.index].sleepTime);this.sleep(1000);}

teste2.start();
*/


/* VERSÃO ANTERIOR DE THREADS */
/*
var thread;

Thread = function()
{
    this.runnning = false;
    this.kill = false;
    this.time;
    this.retValue = false;
    
    this.start = function() 
    {
        if (this.kill == false)
        {
            this.time = setTimeout("thread.background()", 1000);
        }
        else
        {
            clearTimeout(this.time);
        }
    }
    this.background = function()
    {
        if (this.runnning == false)
        {
            this.runnning = true;
            
            retValue = this.run();
        }
        
        if (retValue == true)
        {
            this.end();
        }
        else
        {
            this.start();
        }
    } 
    this.run = function()
    {
    }
    this.end = function()
    {
        this.kill = true;
        this.destructor();
    }
    this.destructor = function()
    {
    }
}
*/

function ExibeAguardeComThread(textoAguarde, runFunction, desabilitaConteudo)
{
    /* posiciona o aviso no centro da tela */
    var sizeXY = getSizeXY();
    var scrollXY = getScrollXY();
//    $("divAguarde").style.top = ((document.documentElement.clientHeight * 0.35) + scrollXY[1]) + "px";
    $("divAguarde").style.top = ((sizeXY[1] * 0.10) + scrollXY[1]) + "px";

    $("spanTextoAguarde").innerHTML = textoAguarde;
    
    if (desabilitaConteudo == true)
    {
        $("divDesabilitaConteudo").style.display = "block";
    }
    $("divAguarde").style.display = "block";

    var thread = new Thread();
    thread.run = runFunction;
    thread.destructor = function(){$("divAguarde").style.display = "none";$("divDesabilitaConteudo").style.display = "none";};
    thread.start();
}

function ExibeAguarde(textoAguarde, desabilitaConteudo)
{
    /* posiciona o aviso no centro da tela */
    var sizeXY = getSizeXY();
    var scrollXY = getScrollXY();
//    $("divAguarde").style.top = ((document.documentElement.clientHeight * 0.35) + scrollXY[1]) + "px";
    $("divAguarde").style.top = ((sizeXY[1] * 0.10) + scrollXY[1]) + "px";

    $("spanTextoAguarde").innerHTML = textoAguarde;
    
    if (desabilitaConteudo == true)
    {
        $("divDesabilitaConteudo").style.display = "block";
    }
    $("divAguarde").style.display = "block";
}

function EscondeAguarde()
{
    $("divAguarde").style.display = "none";
    $("divDesabilitaConteudo").style.display = "none";
    
    try
    {
        $("bg_overlay").style.display = "none";
        $("divStayTopLeft").style.display = "none";
    }
    catch(ex){}
}

/*************************************************************************************************************************************************/
/*                                                             DESABILITA ELEMENTOS                                                              */
/*************************************************************************************************************************************************/
function disable(element) 
{
    try 
    {
        element.disabled = element.disabled ? false : true;
    }
    catch(ex) { }
    
    if (element.childNodes && element.childNodes.length > 0) 
    {
        for (var x = 0; x < element.childNodes.length; x++) 
        {
            disable(element.childNodes[x]);
        }
    }
}

/*************************************************************************************************************************************************/
/*                                                                  QUERY STRING                                                                 */
/*************************************************************************************************************************************************/
function QueryString(key) 
{
    var qs = window.location.search.substring(1);
    return QueryString_SetQS(qs, key);
}

function QueryString_SetQS(qs, key) 
{
    var value = null;
    
    var elements = qs.split("&");
    for (var i = 0;i < elements.length; i++) 
    {
        var element = elements[i].split("=");
        if (element[0].toLowerCase() == key.toLowerCase()) 
        {
            value = element[1];
        }
    }
    
    return value;
}
/**************************************** Selecionar item em um combo ********************************/
/*
/*****************************************************************************************************/
function selecionarItemCombo(obj, str, val)
{
    if(str != null)
    {
        // Varrer todas as opcoes do combo para encontrar o item à selecionar
        for (var i=1;i < obj.length;i++)
        { 
            if(val.toLowerCase() == "val")
            {
                // Pesquisar no atributo value do objeto
                if(obj[i].value.toLowerCase().indexOf(str.toLowerCase()) > -1)
                { 
                    obj.selectedIndex=i; 
                    break;
                } 
            }
            else if(val.toLowerCase() == "txt")
            {
                // Pesquisar no atributo text do objeto
                if(obj[i].text.toLowerCase().indexOf(str.toLowerCase()) > -1)
                { 
                    obj.selectedIndex=i; 
                    break;
                } 
            }
        }
    }
}
