﻿function SelectAllCheckboxes(spanChk) {
    var oItem = spanChk.children;
    var theBox = (spanChk.type == "checkbox") ? spanChk : spanChk.children.item[0];
    xState = theBox.checked;
    elm = theBox.form.elements;

    for (i = 0; i < elm.length; i++)
        if (elm[i].type == "checkbox" && elm[i].id != theBox.id) {
            if (elm[i].checked != xState)
                elm[i].click();
        }
    }

function updAba(divId, tipo) {
    var div = document.getElementById(divId);
    if (tipo == 1) {
        div.className = 'aba2';
    } else {
        if (tipo == 11) {
            div.className = 'aba2';
            showDiv('menuCursos');
        } else {
            div.className = 'aba1';
        }
    }
}

function showDiv(name) {
    document.getElementById(name).style.display = '';
}

function hideDiv(name) {
    document.getElementById(name).style.display = 'none';
}

function alternateDiv(name) {    
    if (document.getElementById(name).style.display == 'none')
        showDiv(name)
    else
        hideDiv(name);
}

function SelectAllCheckboxes(spanChk) {
    var oItem = spanChk.children;
    var theBox = (spanChk.type == "checkbox") ? spanChk : spanChk.children.item[0];
    xState = theBox.checked;
    elm = theBox.form.elements;
    for (i = 0; i < elm.length; i++)
        if (elm[i].type == "checkbox" && elm[i].id != theBox.id) {
            if (elm[i].checked != xState)
                elm[i].click();
        }
}

function MascararCPF(campo) {
    if ((campo.value.length == 3) || (campo.value.length == 7)) {
        campo.value = campo.value + '.';
    } else {
        if (campo.value.length == 11) {
            campo.value = campo.value + '-';
        }
    }
    changeFocus(campo);
}

function MascararCNPJ(campo) {
    if ((campo.value.length == 2) || (campo.value.length == 6)) {
        campo.value = campo.value + '.';
    } else {
        if (campo.value.length == 10) {
            campo.value = campo.value + '/';
        } else {
            if (campo.value.length == 15) {
                campo.value = campo.value + '-';
            }
        }
    }
    changeFocus(campo);
}

function MascararRG(campo) {
    if ((campo.value.length == 2) || (campo.value.length == 6)) {
        campo.value = campo.value + '.';
    } else {
        if (campo.value.length == 10) {
            campo.value = campo.value + '-';
        }
    }
    changeFocus(campo);
}

function MascararCEP(campo) {
    if (campo.value.length == 5) {
        campo.value = campo.value + '-';
    }
    changeFocus(campo);
}

function MascararTelefone(campo) {
    if (campo.value.length == 4) {
        campo.value = campo.value + '-';
    }
    changeFocus(campo);
}

function MascararData(campo) {
    if ((campo.value.length == 2) || (campo.value.length == 5)) {
        campo.value = campo.value + '/';
    }
    changeFocus(campo);
}

function MascararMoeda(campo) {
    var vr = campo.value;
    vr = vr.replace(/[\/\.,]/g, '');
    var tam = vr.length;

    if (tam <= 2) {
        campo.value = vr;
    }
    if ((tam > 2) && (tam <= 5)) {
        campo.value = vr.substr(0, tam - 2) + ',' + vr.substr(tam - 2, tam);
    }
    if ((tam >= 6) && (tam <= 8)) {
        campo.value = vr.substr(0, tam - 5) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
    }
    if ((tam >= 9) && (tam <= 11)) {
        campo.value = vr.substr(0, tam - 8) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
    }
    if ((tam >= 12) && (tam <= 14)) {
        campo.value = vr.substr(0, tam - 11) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
    }
    if ((tam >= 15) && (tam <= 17)) {
        campo.value = vr.substr(0, tam - 14) + '.' + vr.substr(tam - 14, 3) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
    }

    changeFocus(campo);
}

/******** Altera o foco para o próximo objeto ********/
function changeFocus(input) {
    if (input.value.length == input.maxLength) {
        var nextObject = -1;

        for (var i = 0; i < input.form.length; i++) {
            if ((input.form.elements[i].name == input.name) && (i < input.form.length)) {
                nextObject = i + 1;
                break;
            }
        }
        if (nextObject != -1) {
            input.form.elements[nextObject].focus();
        }
    }
}

function formatCase(txtInput) {
    var strResult = '';
    var arrTitle = txtInput.value.split(" ");
    for (var i = 0; i < arrTitle.length; i++) {
        var strPart = trim(arrTitle[i].toLowerCase());

        if ((strPart == 'mba') || (strPart == 'sp'))
            strResult = strResult + strPart.toUpperCase() + ' '
        else
            if ((strPart == 'c/') || (strPart == 'c.'))
                strResult = strResult + 'com '
            else
                if ((strPart == 'p/') || (strPart == 'p.')) 
                    strResult = strResult + 'para '
                    else 
                        if ((strPart == 'até') || (strPart == 'e') || (strPart == 'ou') || (strPart == 'de') || (strPart == 'do') || (strPart == 'que') || (strPart == 'com') || (strPart == 'para') || (strPart == 'em') || (strPart == 'no') || (strPart == 'mg') || (strPart == 'g') || (strPart == 'kg') || (strPart == 'mm') || (strPart == 'cm') || (strPart == 'm') || (strPart == 'km'))
                        strResult = strResult + strPart + ' '
                        else 
                        strResult = strResult + strPart.substring(0, 1).toUpperCase() + strPart.substring(1) + ' ';
    }

    txtInput.value = trim(strResult);
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}

// ========================================
//Envia formulário de busca pela tecla ENTER
// ========================================
function submitOnEnter(myfield, e) {
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else
        return true;
    if (keycode == 13) {
        buscarSite();
        return false;
    }
    else
        return true;
}

function buscarSite() {
    document.location = '/busca.aspx?cx=006084484043640516766:lmgvljk87fu&cof=FORID%3A11&ie=UTF-8&q=' + URLEncode(document.getElementById('q').value);    
}

// ========================================
//Formata URL para querystring da busca
// ========================================
function URLEncode(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
            x += match[1].length;
        } else {
            if (clearString[x] == ' ')
                output += '+';
            else {
                var charCode = clearString.charCodeAt(x);
                var hexVal = charCode.toString(16);
                output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return output;
}
