﻿function CallSolution(src) {
    switch (src) {
        case 2: Page = '/crescimento-sustentado'; break;
        case 3: Page = '/metas'; break;
        case 4: Page = '/liderancas-sintonizadas'; break;
        case 5: Page = '/politicas-topbottom'; break;
        case 6: Page = '/avaliacao-de-desempenho'; break;
        default: Page = ''; break;
    }

    location.href = '/solucao' + Page;
}

/*****************************************************
*** Restrict Area
*****************************************************/
function Logon() {
    location.href = '/Online/Login.aspx?keyString=' + encryptUrl(document.getElementById('txtLoginMail').value, document.getElementById('txtLoginPassword').value);
}

function KeyPressEnter() {
    keyPressed = event.keyCode;
    if (keyPressed == 13) {
        Logon();
        return false;
    }
}

/*****************************************************
*** On-line
*****************************************************/
function Counter() {
    TypedText = document.getElementById('ctl00_ctl00_body_bodyOnline_txtTextCont').value;
    document.getElementById('divCounter').innerHTML = 500 - TypedText.length;
    VerifyCounter();
}

function VerifyCounter() {
    Remaining = 500 - document.getElementById('ctl00_ctl00_body_bodyOnline_txtTextCont').value.length;
    if (Remaining <= 0) {
        document.getElementById('divCounter').style.color = '#a10000';
        return false;
    } else {
        document.getElementById('divCounter').style.color = '#333333';
        return true;
    }
}

function ValidateDescription(src, args) {
    Remaining = 500 - document.getElementById('ctl00_ctl00_body_bodyOnline_txtTextCont').value.length;
    args.IsValid = (Remaining >= 0);
}

function ScrollEndPage() {
    window.scrollTo(0, 999999999999);
}

/*****************************************************
*** Encrypt
*****************************************************/
function encryptUrl(UserLogin, UserPassword) {
    return encryptedString(UserLogin, 17) + '34797372837A7672728B7C7F4C84' + encryptedString(UserPassword, 17);
}

function encryptedString(typedData, key) {
    var encryptData = '';

    for (i = 0; i < typedData.length; i++) {
        encryptData += Dec2Hex(typedData.charCodeAt(i) + key) + Dec2Hex(Math.floor(Math.random() * 26) + 97 + key);
    }

    return encryptData;
}

function decryptedString(encryptData, key) {
    var decryptData = '';

    for (i = 0; i < encryptData.length; i += 4) {
        decryptData += String.fromCharCode(Hex2Dec(encryptData.substr(i, 2)) - key);
    }

    return decryptData;
}

function Dec2Hex(Decimal) {
    var hexChars = "0123456789ABCDEF";
    var a = Decimal % 16;
    var b = (Decimal - a) / 16;

    hex = hexChars.charAt(b) + "" + hexChars.charAt(a);

    return hex;
}

function Hex2Dec(hex) {
    var hex_chars = "0123456789ABCDEF";

    var n1 = hex.charAt(0);
    var n2 = hex.charAt(1);

    var n1 = hex_chars.indexOf(n1) * 16;
    var n2 = hex_chars.indexOf(n2);

    return n1 + n2;
}

/*****************************************************
*** Search
*****************************************************/
function SearchTextBoxOnEnter() {
    document.getElementById('ctl00_Header_txtSearchHeader').className = 'HeaderSearchTextBoxBlack';
    if (document.getElementById('ctl00_Header_txtSearchHeader').value == 'Digite aqui o que procura ...') { document.getElementById('ctl00_Header_txtSearchHeader').value = ''; }
}

function SearchTextBoxOnExit() {
    if (document.getElementById('ctl00_Header_txtSearchHeader').value == '') {
        document.getElementById('ctl00_Header_txtSearchHeader').value = 'Digite aqui o que procura ...';
        document.getElementById('ctl00_Header_txtSearchHeader').className = 'HeaderSearchTextBoxGray';
    }
}

function Trim(str) { return str.replace(/^\s+|\s+$/g, ""); }
function CallSearch() {
    CallSearchGo(document.getElementById('ctl00_Header_txtSearchHeader').value);
}

function CallSearchGo(Keyword) {
    if (Keyword.length < 3) {
        alert('A palavra-chave tem que ter pelo menos 3 caracteres.');
    } else {
        parent.location.href = '/busca/' + MyUrlEncode(Trim(Keyword));
    }
}
function KeyPressEnterSearch() {
    keyPressed = event.keyCode;
    if (keyPressed == 13) {
        CallSearch();
        return false;
    }
}

/*****************************************************
*** Picture
*****************************************************/
function HiddenPhoto() {
    document.getElementById('ctl00_body_pnlPagePhoto').style.display = 'none';
}

function ShowPicture(src, label) {
    document.getElementById('ctl00_body_imgPictureAmp').src = '/App_Common/Pix.gif';
    document.getElementById('ctl00_body_imgPictureAmp').src = 'http://file.4mconsultoria.com.br/' + src;
    document.getElementById('ctl00_body_lblPictureAmp').innerHTML = label;
}

function PreviousPicture() {
    CurrentPicture = CurrentPicture - 1;
    if (CurrentPicture < 0) { CurrentPicture = PictureCount; }
    ShowPicture(arrPictures[CurrentPicture], arrPicturesLabel[CurrentPicture]);
}

function NextPicture() {
    CurrentPicture = CurrentPicture + 1;
    if (CurrentPicture > PictureCount) { CurrentPicture = 0; }
    ShowPicture(arrPictures[CurrentPicture], arrPicturesLabel[CurrentPicture]);
}

function CallPicture(numItem) {
    try {
        CurrentPicture = numItem;
        ShowPicture(arrPictures[CurrentPicture], arrPicturesLabel[CurrentPicture]);
    } catch (err) { }
}

/*****************************************************
*** Font Size
*****************************************************/
var PageFontSize = 3;
function FontSizeMore() {
    if (PageFontSize < 6) {
        PageFontSize += 1;
        document.getElementById('ctl00_body_lblPageText').className = 'PageText' + PageFontSize;
    }
}

function FontSizeLess() {
    if (PageFontSize > 1) {
        PageFontSize -= 1;
        document.getElementById('ctl00_body_lblPageText').className = 'PageText' + PageFontSize;
    }
}

/*****************************************************
*** Mask
*****************************************************/
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) num = "0";

    cents = Math.floor((num * 100 + 0.5) % 100);
    num = Math.floor((num * 100 + 0.5) / 100).toString();
    if (cents < 10) cents = "0" + cents;

    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
    return ("R$ " + num + "," + cents);
}

function formatNumber(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) num = "0";

    cents = Math.floor((num * 100 + 0.5) % 100);
    num = Math.floor((num * 100 + 0.5) / 100).toString();
    if (cents < 10) cents = "0" + cents;

    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
    return (num + "," + cents);
}

function MasktZipCode(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '00.000-000', event);
}

function MaskPhone(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '(00) 0000-0000', event);
}

function MaskDate(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '00/00/0000', event);
}

function MaskHour(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '00.00', event);
}

function MaskCNPJ(cnpj) {
    if (MaskInteger(cnpj) == false) {
        event.returnValue = false;
    }
    return FormatField(cnpj, '00.000.000/0000-00', event);
}

function MaskCPF(cpf) {
    if (MaskInteger(cpf) == false) {
        event.returnValue = false;
    }
    return FormatField(cpf, '000.000.000-00', event);
}

function MaskInteger() {
    if (event.keyCode < 48 || event.keyCode > 57) {
        event.returnValue = false;
        return false;
    }
    return true;
}

function FormatField(campo, Mascara, evento) {
    var boleanoMascara;

    var Digitato = evento.keyCode;
    exp = /\-|\.|\/|\(|\)| /g
    campoSoNumeros = campo.value.toString().replace(exp, "");

    var posicaoCampo = 0;
    var NovoValorCampo = "";
    var TamanhoMascara = campoSoNumeros.length; ;

    if (Digitato != 8) { // backspace 
        for (i = 0; i <= TamanhoMascara; i++) {
            boleanoMascara = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                || (Mascara.charAt(i) == "/"))
            boleanoMascara = boleanoMascara || ((Mascara.charAt(i) == "(")
                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
            if (boleanoMascara) {
                NovoValorCampo += Mascara.charAt(i);
                TamanhoMascara++;
            } else {
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
                posicaoCampo++;
            }
        }
        campo.value = NovoValorCampo;
        return true;
    } else {
        return true;
    }
}

function ValidateCPF(src, args) {
    args.IsValid = IsCPFValid(document.getElementById(src.controltovalidate).value);
}

function IsCPFValid(cpf) {
    var i;

    while (cpf.indexOf('.') >= 0) { cpf = cpf.replace('.', ''); }
    while (cpf.indexOf('-') >= 0) { cpf = cpf.replace('-', ''); }
    while (cpf.indexOf(' ') >= 0) { cpf = cpf.replace(' ', ''); }

    if ((cpf == '11111111111') || (cpf == '22222222222') || (cpf == '33333333333') || (cpf == '44444444444') || (cpf == '55555555555') || (cpf == '66666666666') || (cpf == '77777777777') || (cpf == '88888888888') || (cpf == '99999999999')) {
        return false;
    }

    var c = cpf.substr(0, 9);
    var dv = cpf.substr(9, 2);
    var d1 = 0;

    for (i = 0; i < 9; i++) {
        d1 += c.charAt(i) * (10 - i);
    }

    if (d1 == 0) {
        return false;
    }

    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1) {
        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) {
        return false;
    }

    return true;
}

function ValidateCNPJ(src, args) {
    args.IsValid = IsCNPJValid(document.getElementById(src.controltovalidate).value);
}

function IsCNPJValid(CNPJ) {
    IsValid = false;

    if ((CNPJ.length > 17) && (CNPJ.charAt(2) == ".") && (CNPJ.charAt(6) == ".") && (CNPJ.charAt(10) == "/") && (CNPJ.charAt(15) == "-")) {
        if (document.layers && parseInt(navigator.appVersion) == 4) {
            x = CNPJ.substring(0, 2);
            x += CNPJ.substring(3, 6);
            x += CNPJ.substring(7, 10);
            x += CNPJ.substring(11, 15);
            x += CNPJ.substring(16, 18);
            CNPJ = x;
        } else {
            CNPJ = CNPJ.replace(".", "");
            CNPJ = CNPJ.replace(".", "");
            CNPJ = CNPJ.replace("-", "");
            CNPJ = CNPJ.replace("/", "");
        }

        var nonNumbers = /\D/;
        if (!nonNumbers.test(CNPJ)) {
            var a = [];
            var b = new Number;
            var c = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
            for (i = 0; i < 12; i++) {
                a[i] = CNPJ.charAt(i);
                b += a[i] * c[i + 1];
            }
            if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11 - x }
            b = 0;
            for (y = 0; y < 13; y++) {
                b += (a[y] * c[y]);
            }
            if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11 - x; }
            if ((CNPJ.charAt(12) == a[12]) && (CNPJ.charAt(13) == a[13])) {
                IsValid = true;
            }
        }
    }

    return IsValid;
}
