/* funcao de redirecionamento da pagina ------------------------------------ */
function goURL(url) {
    
    document.location = url;
    
}

/* funcao que chama uma acao do struts ------------------------------------- */
function callAction(method, params, frm) {
    if ((!Util.isEmpty(method)) && (Util.isEmpty(params))) {

        goURL(method + ".do");
            
    } else if (Util.isEmpty(frm)) {
      
        goURL(method + ".do?metodo=" + params);
            
    } else {
   
        document.forms[frm].action = method + ".do";
        document.forms[frm].metodo.value = params;
        document.forms[frm].submit();
            
    }
    
}

/* chamada da ajuda -------------------------------------------------------- */
function callHelp() {

    window.open("jsp/help/PMN_Ajuda_index.htm","","width=710,height=410");

}

/* regras aplicadas nos inputs --------------------------------------------- */
var defaultRules = {
            
    ".text" : function(el) {
            
        el.onfocus = function() { this.className = "text_focus"; this.value = this.value.toUpperCase(); },
        el.onblur  = function() { this.className = "text"; this.value = this.value.toUpperCase(); }
            
    },
    
    ".select" : function(el) {
        
        el.onfocus = function() { this.className = "select_focus"; },
        el.onblur  = function() { this.className = "select"; }
            
    },
    
    "* a" : function(el) {
    
        el.onmouseover = function() { window.status=''; return true; }
        el.onmouseout  = function() { window.status=''; return true; }
    
    }
    
};

Behaviour.register(defaultRules);

/* funcao para sair do sistema ---------------------------------------------- */
function logout() {
        
    if (confirm("Deseja realmente sair do sistema ?")) {
    
        callAction("SairSistema","sairSistema");
    
    }

}

function logoutUrl(pUrl) {
    
    callAction("SairSistema", "sairSistema&url=" + pUrl);

}

/* chamada de um popup ----------------------------------------------------- */

function callPop(popURL, frName, pWidth, pHeight) {
    
    var frm     = parent.document.forms["frPOP"];
    var popName = (!Util.isEmpty(frName))?(frName+"P"):"wopen";
      
    var vWidth  = (!Util.isEmpty(pWidth))?pWidth:600;
    var vHeight = (!Util.isEmpty(pHeight))?pHeight:300;
    
    frm.target        = popName;
    frm.vPagina.value = popURL;
    
    win = window.open("about:blank", popName, "dialog=yes, modal=yes, scrollbars=yes, width=" + vWidth + "px, height=" + vHeight + "px");
    
    lt = (screen.width/2) - (vWidth/2);
    tp = (screen.height/2) - (vHeight/2);
    
    win.moveTo(lt, tp);
    win.focus();
    
    frm.submit();
    
    return false;
    
}

function callPopClose(frName) {
    
    if (!Util.isEmpty(frName)) {
        
        if (frName!="wopen") {
        
            vName = frName.substr(0,frName.length-1);
            
            if (vName=="noFrame") {
        
                parent.document.location.reload();
            
            }
            
            if (vName!="noFrame") {
            
                vLocation = eval("parent." + frName);
                eval("parent.frames['" + vName  + "'].document.location.replace('" + vLocation + "')");
                
            }
                
        }
        
    }
    
    return false;
    
}

function definePopUpLocations() {

    for(var i=0; i<window.frames.length; i++) {
    
        var fName     = window.frames[i].name + "P";
        var fLocation = window.frames[i].document.location;
    
        var def = eval("parent." + fName);
    
        if (typeof def=="undefined") eval("parent." + fName + "='" + fLocation + "'");
    
    }

}

Behaviour.addLoadEvent(definePopUpLocations);

/* ------------------------------------------------------------------------- */

/* funcao para setar parametros na url ------------------------------------- */

function setParameters() {

    var args = arguments;
    var pars = "";
    
    if (args.length%2==0) {
    
        for (var i=0; i<args.length; i+=2) {
        
            pars += args[i] + "=" + args[i+1] + "&";
        
        }
    
    }
    
    return pars.substring(0,pars.length-1);

}

/* ------------------------------------------------------------------------- */
/* funcao de acao dos links da barra de navegacao -------------------------- */

function navbar() {

    var args = arguments;                    // argumentos da funcao
    var frm  = document.forms["navbarForm"]; // navbarForm (formulario de acao da barra de navegacao)
    var pars = frm.postValues.value;         // valores postados
    
    if (args.length > 0) {
    
        var href = args[0].split(",");
        var act  = "";
        
        act += href[0] + ".do?";
        
        for(var i=1; i<href.length; i++) {
            
            var h = href[i];
            var p = href[i]; // parametro que sera adicionado a ACTION do navbarForm
            
            if (h.indexOf("?")>=0) {
             
                var pName  = ""; // nome do parametro a ser adicionado a URL da ACTION (navbarForm.action)
                var pValue = ""; // valor do parametro
             
                pName = h.split("=")[0];
                
                var from = pars.indexOf(pName) + pName.length + 1;
                var to   = pars.indexOf(";", from);
                
                pValue = pars.substring(from, to);
                
                p = pName + "=" + pValue;
            
            }
            
            act += p + "&";
        
        }
        
        goURL(act);
    
    }

}

/* ------------------------------------------------------------------------- */
/* TODO: inserir essa funcao no JSC */

function mascaraValorDecimal(objTextBox, SeparadorMilesimo, SeparadorDecimal, casasDecimais, e, maxLength){

        var sep = 0;
        var key = '';
        var i = j = z = 0;
        var len = len2 = 0;
        var strCheck = '0123456789';
        var aux = aux2 = '';
        var whichCode;
        var strZeros = '';
       
        if(window.event) { // Internet Explorer
            whichCode = e.keyCode; 
        }else if(e.which) { // Nestcape / firefox
            whichCode = e.which;
        }else whichCode = e.keyCode;

        if ((e.which!=0 && whichCode != 8 && objTextBox.value.length < maxLength) || (whichCode==null) ) {
                      
            if (whichCode!=null) {
                key = String.fromCharCode(whichCode); // Valor para o c??digo da Chave
                if (strCheck.indexOf(key) == -1) {
                    return false; // Chave inv??lida
                }
            }
            
            
            //formando string zerada com o numero de casas decimais 
            strZeros = '0,';
            for(;z < casasDecimais; z++)
                strZeros += '0';
            
            if(objTextBox.value==strZeros)
                return true;
                
            len = objTextBox.value.length;
            
            for(i = 0; i < len; i++)
                if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
                
            aux = '';
            
            for(; i < len; i++)
                if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
            
            aux += key;
            len = aux.length;
            
            if(len <= casasDecimais){
            
                if (len == 0){
                
                    objTextBox.value = '';
                }else{
                
                    strZeros = '';
                    
                    for(z = 0; z < casasDecimais - len; z++)
                        strZeros += '0';
                    
                    objTextBox.value = '0'+ SeparadorDecimal + strZeros + aux;
                }
                
            }else{
                
                aux2 = '';
                for (j = 0, i = len - (casasDecimais + 1); i >= 0; i--) {
                    
                    if (j == 3) {
                        aux2 += SeparadorMilesimo;
                        j = 0;
                    }
                    
                    aux2 += aux.charAt(i);
                    j++;
                }
                
                objTextBox.value = '';
                len2 = aux2.length;
                for (i = len2 - 1; i >= 0; i--)
                    objTextBox.value += aux2.charAt(i);
                
                objTextBox.value += SeparadorDecimal + aux.substr(len - casasDecimais, len);
            }
            
            return false;
            
        }else return true;
    }    
