function fcn_getAJAX() {
    //verifica se o browser tem suporte a ajax, retorna um objeto do tipo AJAX						
    try {
        vobj_ajax = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        try {
            vobj_ajax = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (ex) {
            try {
                vobj_ajax = new XMLHttpRequest();
            } catch (exc) {
                //Quando o browser não suporta ajax, este bloco, exibe uma mensagem do tipo ALERT para o usuário, e seta o Objeto ajax para NULL
                alert("Esse browser não possui recursos para a utilização de AJAX!");
                vobj_ajax = null;
            }
        }
    }
    return vobj_ajax;
}

//conta caracteres
var ns6 = document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){
    if (window.event&&event.srcElement.value.length>=maxlength)
        return false
    else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
        var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
        if (pressedkey.test(String.fromCharCode(e.which)))
            e.stopPropagation()
    }
}

function countlimit(maxlength,e,placeholder){
    var theform=eval(placeholder)
    var lengthleft=maxlength-theform.value.length
    var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
    if (window.event||e.target&&e.target==eval(placeholder)){
        if (lengthleft<0)
            theform.value=theform.value.substring(0,maxlength)
            placeholderobj.innerHTML=lengthleft
    }
}

function displaylimit(thename, theid, thelimit){
    var theform=theid!=""? document.getElementById(theid) : thename
    var limit_text='max.: <b><span id="'+theform.toString()+'" class="text_blue">'+thelimit+'</span></b>'
    if (document.all||ns6)
        document.write(limit_text)
    if (document.all){
        eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
        eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
    }
    else if (ns6){
        document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
        document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
    }
}

//libera flash
// When the page loads:
/*window.onload = function() {
    if (document.getElementsByTagName) {
        // Get all the tags of type object in the page.
        var objs = document.getElementsByTagName("object");

        for (i = 0; i < objs.length; i++) {
            // Get the HTML content of each object tag and replace it with itself.
            objs[i].outerHTML = objs[i].outerHTML;
        }
    }
}

// When the page unloads:
window.onunload = function() {
    if (document.getElementsByTagName) {
        //Get all the tags of type object in the page.
        var objs = document.getElementsByTagName("object");
        for (i = 0; i < objs.length; i++) {
            // Clear out the HTML content of each object tag to prevent an IE memory leak issue.
            objs[i].outerHTML = "";
        }
    }
}*/

// Máscara de telefone

function fcn_mascaraTelefoneDigitada(objeto) {
    if (objeto.value.length == 0)
        objeto.value = '(' + objeto.value;

    if (objeto.value.length == 3)
        objeto.value = objeto.value + ')';

    if (objeto.value.length == 8)
        objeto.value = objeto.value + '-';
}

function fcn_mascaraTelefone(objeto) {

    objeto.value = objeto.value.replace('-', '');
    objeto.value = objeto.value.replace('-', '');
    objeto.value = objeto.value.replace('-', '');


    if (objeto.value.length == 8 || objeto.value.length == 9) {

        objeto.value = objeto.value.replace('-', '');
        objeto.value = objeto.value.replace('-', '');
        objeto.value = objeto.value.replace('-', '');
        objeto.value = objeto.value.replace('(', '');
        objeto.value = objeto.value.replace(')', '');

        for (i = 0; i < objeto.value.length; i++) {
            if (i == 4)
                objeto.value = objeto.value.substring(0, 4) + '-' + objeto.value.substring(4, objeto.value.length)
        }
    }

    if (objeto.value.length > 9 && objeto.value.length <= 10) {

        objeto.value = objeto.value.replace('-', '');
        objeto.value = objeto.value.replace('-', '');
        objeto.value = objeto.value.replace('-', '');
        
        for (i=0; i<objeto.value.length; i++){
            if (i == 0)
                objeto.value = '(' + objeto.value;

            if (i == 3)
                objeto.value = objeto.value.substring(0, 3) + ')' + objeto.value.substring(3, objeto.value.length)
            
            if (i == 8)
                objeto.value = objeto.value.substring(0, 8) + '-' + objeto.value.substring(8, objeto.value.length)
        }
    }

    if (objeto.value.length == 12) {

        objeto.value = objeto.value.replace('-', '');
        objeto.value = objeto.value.replace('-', '');
        objeto.value = objeto.value.replace('-', '');
        objeto.value = objeto.value.replace('(', '');
        objeto.value = objeto.value.replace(')', '');

        for (i = 0; i < objeto.value.length; i++) {
            if (i == 0)
                objeto.value = '(' + objeto.value;

            if (i == 3)
                objeto.value = objeto.value.substring(0, 3) + ')' + objeto.value.substring(3, objeto.value.length)

            if (i == 8)
                objeto.value = objeto.value.substring(0, 8) + '-' + objeto.value.substring(8, objeto.value.length)
        }
    }
}


//------------------------------------------------------
// Função para validação de CPF
//------------------------------------------------------
function validacpf() {
    var i;
    s = document.frm_newi.txt_cpf.value;

    if (document.frm_newi.txt_cpf.value == '')
    { return false; }

    var c = s.substr(0, 9);
    var dv = s.substr(9, 2);
    var d1 = 0;
    for (i = 0; i < 9; i++) {
        d1 += c.charAt(i) * (10 - i);
    }
    if (d1 == 0) {
        alert("CPF Inválido!")
        document.frm_newi.txt_cpf.focus()
        return false;
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1) {
        alert("CPF Inválido!")
        document.frm_newi.txt_cpf.focus()
        return false;
    }
    d1 *= 2;

    for (i = 0; i < 9; i++) {
        d1 += c.charAt(i) * (11 - i);
    }

    d1 = 11 - (d1 % 11);

    if (d1 > 9) d1 = 0;
    if (dv.charAt(1) != d1) {
        alert("CPF Inválido!")
        document.frm_newi.txt_cpf.focus()
        return false;
    }

    return true;
} 

//------------------------------------------------------
// Campo somente numerico
//------------------------------------------------------
function fcn_validaNumero() {
    var vstr_caracter = document.layers ? evt.which
						: document.all ? event.keyCode
						: evt.keyCode;


    if (vstr_caracter != 46 && vstr_caracter != 8 && vstr_caracter != 9 && vstr_caracter != 188 && vstr_caracter != 194) {
        if ((vstr_caracter < 48 || vstr_caracter > 58) && (vstr_caracter < 96 || vstr_caracter > 105)) { return false; }
    }
}

//------------------------------------------------------
// Para tabulação no campo
//------------------------------------------------------
function f_parar_tab(quem) { v_tab = false; }

//------------------------------------------------------
// Checa tabulação do campo
//------------------------------------------------------
function f_checar_tab() { v_tab = true; }

//------------------------------------------------------
// Formata data
//------------------------------------------------------
function f_data(obj) {
    if (obj.value.length == 9 || obj.value.length == 0 || obj.value.length == 10) { f_checar_tab() } else { v_tab = false; }
    var c = document.layers ? evt.which
            : document.all ? event.keyCode
            : evt.keyCode;
    if ((c < 48 || c > 58)) { return false; }
    else {
        if (obj.value.length == 2 || obj.value.length == 5) { obj.value = obj.value + "/" }
    }
}

//------------------------------------------------------
// Consiste Data
//------------------------------------------------------
function f_consiste_data(obj) {
    if (obj.value.length > 0) {
        var v_data = obj.value
        var v_dia = v_data.substring(0, 2)
        var v_mes = v_data.substring(3, 5)
        var v_ano = v_data.substring(6, 10)
        if (v_dia < 0 || v_dia > 31 || v_dia.substring(0, 1) == "/" || v_dia.substring(1, 1) == "/") { alert("Data com Dia inválido"); obj.focus(); obj.select(); return }
        if (v_mes < 0 || v_mes > 12 || v_mes.substring(0, 1) == "/" || v_mes.substring(1, 1) == "/") { alert("Data com Mês inválido"); obj.focus(); obj.select(); return }
        if (v_ano < 1900 || v_ano > 2100 || v_ano.substring(0, 1) == "/" || v_ano.substring(1, 1) == "/" || v_ano.substring(2, 1) == "/" || v_ano.substring(3, 1) == "/") { alert("Data com Ano inválido"); obj.focus(); obj.select(); return }
    }
}


//------------------------------------------------------
// verifica se algum radio foi selecionado
//------------------------------------------------------
function fcn_verificaCheckExcluir_checked(pObjCheckBox) {

    if (pObjCheckBox != undefined) {

        var vobj_checkBox = pObjCheckBox
        var vint_TT_checkBox = vobj_checkBox.length;
        var vstr_FL_selecionou = false;

        //verificando o total de check box
        if (isNaN(vint_TT_checkBox) == true) {
            vint_TT_checkBox = 1;
        }

        if (vint_TT_checkBox == 1) {

            if (vobj_checkBox.checked == true) {
                vstr_FL_selecionou = true;
            }

        } else {

            for (x = 0; x < vint_TT_checkBox; x++) {

                if (vobj_checkBox[x].checked == true) {
                    vstr_FL_selecionou = true;
                    break;
                }
            }
        }

        return vstr_FL_selecionou;
    
    }else{
        return false;
    }
}

//Monta lista
function lista(fbox, tbox) {

    var arrFbox = new Array();
    var arrTbox = new Array();
    var arrLookup = new Array();
    var i;

    for (i = 0; i < tbox.options.length; i++) {
        arrLookup[tbox.options[i].text] = tbox.options[i].value;
        arrTbox[i] = tbox.options[i].text;
    }

    var fLength = 0;
    var tLength = arrTbox.length;

    for (i = 0; i < fbox.options.length; i++) {
        arrLookup[fbox.options[i].text] = fbox.options[i].value;
        if (fbox.options[i].selected && fbox.options[i].value != "") {
            arrTbox[tLength] = fbox.options[i].text;
            tLength++;
        }
        else {
            arrFbox[fLength] = fbox.options[i].text;
            fLength++;
        }
    }

    arrFbox.sort();
    arrTbox.sort();
    fbox.length = 0;
    tbox.length = 0;

    var c;

    for (c = 0; c < arrFbox.length; c++) {
        var no = new Option();
        no.value = arrLookup[arrFbox[c]];
        no.text = arrFbox[c];
        fbox[c] = no;
    }

    for (c = 0; c < arrTbox.length; c++) {
        var no = new Option();
        no.value = arrLookup[arrTbox[c]];
        no.text = arrTbox[c];
        tbox[c] = no;
    }

    avancar();
}

function avancar() {

    var strValues = "";
    var boxLength = document.frm_newi.lst_usuarioEnviar.length;
    var count = 0;

    if (boxLength != 0) {
        for (i = 0; i < boxLength; i++) {
            if (count == 0) {
                strValues = document.frm_newi.lst_usuarioEnviar.options[i].value;
            }
            else {
                strValues = strValues + ", " + document.frm_newi.lst_usuarioEnviar.options[i].value;
            }
            count++;
        }
    }

    //Carrega hidden selecionados com os valores selecionados
    document.frm_newi.selecionados.value = strValues

    //Valida Campos obrigatorios e da submit		
}

function compareOptionValues(a, b) {

    // Radix 10: for numeric values
    // Radix 36: for alphanumeric values

    var sA = parseInt(a.value, 36);
    var sB = parseInt(b.value, 36);
    return 0
}

// Dual list move function
function moveDualList(srcList, destList, moveAll) {

    var selecionados = "";

    // Do nothing if nothing is selected
    if ((srcList.selectedIndex == -1) && (moveAll == false)) {
        return;
    }

    newDestList = new Array(destList.options.length);

    var len = 0;

    for (len = 0; len < destList.options.length; len++) {
        if (destList.options[len] != null) {
            newDestList[len] = new Option(destList.options[len].text, destList.options[len].value, destList.options[len].defaultSelected, destList.options[len].selected);
        }
    }

    for (var i = 0; i < srcList.options.length; i++) {
        if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
            // Statements to perform if option is selected
            // Incorporate into new list
            newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected);
            len++;
        }
    }

    // Sort out the new destination list
    newDestList.sort(compareOptionValues);   // BY VALUES
    //newDestList.sort( compareOptionText );   // BY TEXT

    // Populate the destination with the items from the new array
    for (var j = 0; j < newDestList.length; j++) {
        if (newDestList[j] != null) {
            destList.options[j] = newDestList[j];
        }
    }

    // Erase source list selected elements
    for (var i = srcList.options.length - 1; i >= 0; i--) {
        if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
            // Erase Source
            //srcList.options[i].value = "";
            //srcList.options[i].text  = "";
            srcList.options[i] = null;
        }
    }

    for (var i = 0; i < destList.options.length; i++) {
        selecionados = selecionados + '' + destList.options[i].value + ', ';
    }

    document.frm_newi.selecionados.value = selecionados.substring(0, selecionados.length - 2);
}

//Monta lista
function lista2(fbox, tbox) {

    var arrFbox = new Array();
    var arrTbox = new Array();
    var arrLookup = new Array();
    var i;

    for (i = 0; i < tbox.options.length; i++) {
        arrLookup[tbox.options[i].text] = tbox.options[i].value;
        arrTbox[i] = tbox.options[i].text;
    }

    var fLength = 0;
    var tLength = arrTbox.length;

    for (i = 0; i < fbox.options.length; i++) {
        arrLookup[fbox.options[i].text] = fbox.options[i].value;
        if (fbox.options[i].selected && fbox.options[i].value != "") {
            arrTbox[tLength] = fbox.options[i].text;
            tLength++;
        }
        else {
            arrFbox[fLength] = fbox.options[i].text;
            fLength++;
        }
    }

    arrFbox.sort();
    arrTbox.sort();
    fbox.length = 0;
    tbox.length = 0;

    var c;

    for (c = 0; c < arrFbox.length; c++) {
        var no = new Option();
        no.value = arrLookup[arrFbox[c]];
        no.text = arrFbox[c];
        fbox[c] = no;
    }

    for (c = 0; c < arrTbox.length; c++) {
        var no = new Option();
        no.value = arrLookup[arrTbox[c]];
        no.text = arrTbox[c];
        tbox[c] = no;
    }

    avancar2();
}



function avancar2() {

    var strValues = "";
    var boxLength = document.frm_newi.lst_usuarioEnviar2.length;
    var count = 0;

    if (boxLength != 0) {
        for (i = 0; i < boxLength; i++) {
            if (count == 0) {
                strValues = document.frm_newi.lst_usuarioEnviar2.options[i].value;
            }
            else {
                strValues = strValues + ", " + document.frm_newi.lst_usuarioEnviar2.options[i].value;
            }
            count++;
        }
    }

    //Carrega hidden selecionados com os valores selecionados
    document.frm_newi.selecionados2.value = strValues

    //Valida Campos obrigatorios e da submit		
}

function compareOptionValues2(a, b) {

    // Radix 10: for numeric values
    // Radix 36: for alphanumeric values

    var sA = parseInt(a.value, 36);
    var sB = parseInt(b.value, 36);
    return 0
}

// Dual list move function
function moveDualList2(srcList, destList, moveAll) {

    var selecionados = "";

    // Do nothing if nothing is selected
    if ((srcList.selectedIndex == -1) && (moveAll == false)) {
        return;
    }

    newDestList = new Array(destList.options.length);

    var len = 0;

    for (len = 0; len < destList.options.length; len++) {
        if (destList.options[len] != null) {
            newDestList[len] = new Option(destList.options[len].text, destList.options[len].value, destList.options[len].defaultSelected, destList.options[len].selected);
        }
    }

    for (var i = 0; i < srcList.options.length; i++) {
        if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
            // Statements to perform if option is selected
            // Incorporate into new list
            newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected);
            len++;
        }
    }

    // Sort out the new destination list
    newDestList.sort(compareOptionValues2);   // BY VALUES
    //newDestList.sort( compareOptionText );   // BY TEXT

    // Populate the destination with the items from the new array
    for (var j = 0; j < newDestList.length; j++) {
        if (newDestList[j] != null) {
            destList.options[j] = newDestList[j];
        }
    }

    // Erase source list selected elements
    for (var i = srcList.options.length - 1; i >= 0; i--) {
        if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
            // Erase Source
            //srcList.options[i].value = "";
            //srcList.options[i].text  = "";
            srcList.options[i] = null;
        }
    }

    for (var i = 0; i < destList.options.length; i++) {
        selecionados = selecionados + '' + destList.options[i].value + ', ';
    }

    document.frm_newi.selecionados2.value = selecionados.substring(0, selecionados.length - 2);
}


function lista3(fbox, tbox) {

    var arrFbox = new Array();
    var arrTbox = new Array();
    var arrLookup = new Array();
    var i;

    for (i = 0; i < tbox.options.length; i++) {
        arrLookup[tbox.options[i].text] = tbox.options[i].value;
        arrTbox[i] = tbox.options[i].text;
    }

    var fLength = 0;
    var tLength = arrTbox.length;

    for (i = 0; i < fbox.options.length; i++) {
        arrLookup[fbox.options[i].text] = fbox.options[i].value;
        if (fbox.options[i].selected && fbox.options[i].value != "") {
            arrTbox[tLength] = fbox.options[i].text;
            tLength++;
        }
        else {
            arrFbox[fLength] = fbox.options[i].text;
            fLength++;
        }
    }

    arrFbox.sort();
    arrTbox.sort();
    fbox.length = 0;
    tbox.length = 0;

    var c;

    for (c = 0; c < arrFbox.length; c++) {
        var no = new Option();
        no.value = arrLookup[arrFbox[c]];
        no.text = arrFbox[c];
        fbox[c] = no;
    }

    for (c = 0; c < arrTbox.length; c++) {
        var no = new Option();
        no.value = arrLookup[arrTbox[c]];
        no.text = arrTbox[c];
        tbox[c] = no;
    }

    avancar3();
}

function avancar3() {

    var strValues = "";
    var boxLength = document.frm_newi.lst_usuarioEnviar3.length;
    var count = 0;

    if (boxLength != 0) {
        for (i = 0; i < boxLength; i++) {
            if (count == 0) {
                strValues = document.frm_newi.lst_usuarioEnviar3.options[i].value;
            }
            else {
                strValues = strValues + ", " + document.frm_newi.lst_usuarioEnviar3.options[i].value;
            }
            count++;
        }
    }

    //Carrega hidden selecionados com os valores selecionados
    document.frm_newi.selecionados3.value = strValues

    //Valida Campos obrigatorios e da submit		
}


function compareOptionValues3(a, b) {

    // Radix 10: for numeric values
    // Radix 36: for alphanumeric values

    var sA = parseInt(a.value, 36);
    var sB = parseInt(b.value, 36);
    return 0
}

function moveDualList3(srcList, destList, moveAll) {

    var selecionados = "";

    // Do nothing if nothing is selected
    if ((srcList.selectedIndex == -1) && (moveAll == false)) {
        return;
    }

    newDestList = new Array(destList.options.length);

    var len = 0;

    for (len = 0; len < destList.options.length; len++) {
        if (destList.options[len] != null) {
            newDestList[len] = new Option(destList.options[len].text, destList.options[len].value, destList.options[len].defaultSelected, destList.options[len].selected);
        }
    }

    for (var i = 0; i < srcList.options.length; i++) {
        if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
            // Statements to perform if option is selected
            // Incorporate into new list
            newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected);
            len++;
        }
    }

    // Sort out the new destination list
    newDestList.sort(compareOptionValues3);   // BY VALUES
    //newDestList.sort( compareOptionText );   // BY TEXT

    // Populate the destination with the items from the new array
    for (var j = 0; j < newDestList.length; j++) {
        if (newDestList[j] != null) {
            destList.options[j] = newDestList[j];
        }
    }

    // Erase source list selected elements
    for (var i = srcList.options.length - 1; i >= 0; i--) {
        if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
            // Erase Source
            //srcList.options[i].value = "";
            //srcList.options[i].text  = "";
            srcList.options[i] = null;
        }
    }

    for (var i = 0; i < destList.options.length; i++) {
        selecionados = selecionados + '' + destList.options[i].value + ', ';
    }

    document.frm_newi.selecionados3.value = selecionados.substring(0, selecionados.length - 2);
}

function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen, bolMask, sCod, nTecla;


    if (document.all) { // Internet Explorer
        nTecla = evtKeyPress.keyCode;
    } else if (document.layers) { // Nestcape
        nTecla = evtKeyPress.which;
    } else {
        nTecla = evtKeyPress.which;
        if (nTecla == 8) {
            return true;
        }
    }

    sValue = objeto.value;

    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace("-", "");
    sValue = sValue.toString().replace("-", "");
    sValue = sValue.toString().replace(".", "");
    sValue = sValue.toString().replace(".", "");
    sValue = sValue.toString().replace("/", "");
    sValue = sValue.toString().replace("/", "");
    sValue = sValue.toString().replace(":", "");
    sValue = sValue.toString().replace(":", "");
    sValue = sValue.toString().replace("(", "");
    sValue = sValue.toString().replace("(", "");
    sValue = sValue.toString().replace(")", "");
    sValue = sValue.toString().replace(")", "");
    sValue = sValue.toString().replace(" ", "");
    sValue = sValue.toString().replace(" ", "");
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

        if (bolMask) {
            sCod += sMask.charAt(i);
            mskLen++;
        } else {
            sCod += sValue.charAt(nCount);
            nCount++;
        }

        i++;
    }

    objeto.value = sCod;

    if (nTecla != 8) { // backspace
        if (sMask.charAt(i - 1) == "9") { // apenas números...
            return ((nTecla > 47) && (nTecla < 58));
        } else { // qualquer caracter...
            return true;
        }
    } else {
        return true;
    }
}
