﻿function CallMenu(src) {
    var Page;

    switch (src) {
        case 1: Page = '/expositor'; break;
        case 2: Page = '/visitante'; break;
        case 3: Page = '/seminario'; break;
        case 4: Page = '/faleconosco'; break;

        case 5: Page = '/busca'; break;

        default: Page = '/';
    }
    location.href = Page;
}

function CallLanguage(src) {
    var Culture;

    switch (src) {
        case 1: Culture = 'en'; break;
        case 2: Culture = 'es'; break;
        default: Culture = 'pt';
    }
    location.href = '/idioma/' + Culture;
}

function CallMovie(idItem, titleItem) {
    location.href = '/videos/' + titleItem + '/' + idItem;
}

function GoToSubscribe() {
    location.href = '/seminario/inscricao';
}

function GoToCourse() {
    location.href = '/cursos';
}

function Trim(str) { return str.replace(/^\s+|\s+$/g, ""); }
function CallSearch(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 MyUrlEncode(Keyword) {
    Keyword = Keyword.toLowerCase();
    Keyword = escape(Keyword);

    while (Keyword.indexOf('%C') >= 0) { Keyword = Keyword.replace('%C', '%c3%8'); }
    while (Keyword.indexOf('%D') >= 0) { Keyword = Keyword.replace('%D', '%c3%9'); }
    while (Keyword.indexOf('%E') >= 0) { Keyword = Keyword.replace('%E', '%c3%a'); }
    while (Keyword.indexOf('%F') >= 0) { Keyword = Keyword.replace('%F', '%c3%b'); }

    return Keyword;
}

/*****************************************************
*** Login
*****************************************************/
function Logon() {
    // location.href = '/AquaFair/WebSite/Login/Default.aspx?keyString=' + encryptUrl(document.getElementById('Header_txtLoginUser').value, document.getElementById('Header_txtLoginPassword').value);
    alert('Usuário e/ou senha inválidos!');

}

function KeyPressEnter() {
    keyPressed = event.keyCode;
    if (keyPressed == 13) {
        Logon();
        return false;
    }
}

/*****************************************************
*** Font Size
*****************************************************/
var PageFontSize = 3;
function FontSizeMore() {
    if (PageFontSize < 6) {
        PageFontSize += 1;
        document.getElementById('body_lblPageText').className = 'PageText' + PageFontSize;
    }
}

function FontSizeLess() {
    if (PageFontSize > 1) {
        PageFontSize -= 1;
        document.getElementById('body_lblPageText').className = 'PageText' + PageFontSize;
    }
}

/*****************************************************
*** Send To
*****************************************************/
var BoxWidth;
var BoxHeight;
function SendToFriend(TitleSend, LinkSend) {
    var PageWidth = document.body.offsetWidth;
    var PageHeight = document.getElementById('PageEnd').offsetTop + 20;

    document.getElementById('body_pnlSendToPage').style.display = 'block';
    document.getElementById('body_pnlSendToPage').style.width = PageWidth + 'px';
    document.getElementById('body_pnlSendToPage').style.height = PageHeight + 'px';

    document.getElementById('body_pnlSendToBox').style.display = 'block';
    BoxWidth = document.getElementById('body_pnlSendToBox').offsetWidth;
    BoxHeight = document.getElementById('body_pnlSendToBox').offsetHeight;
    document.getElementById('body_pnlSendToBox').style.left = parseInt((PageWidth - BoxWidth) / 2) + 'px';
    document.getElementById('body_pnlSendToBox').style.top = (parseInt((document.body.offsetHeight - BoxHeight) / 2) + document.documentElement.scrollTop) + 'px';

    while (LinkSend.indexOf("/") >= 0) {
        LinkSend = LinkSend.replace("/", "\\");
    }

    document.getElementById('body_lblSendToBox').innerHTML = '<iframe id="frmSend" name="frmSend" src="/SendTo/Default.aspx?title=' + escape(TitleSend) + '&link=' + escape(LinkSend) + '" height="258" width="500" frameborder="0" scrolling="no"></iframe>';
}

function SendToClose() {
    document.getElementById('body_pnlSendToPage').style.display = 'none';
    document.getElementById('body_pnlSendToBox').style.display = 'none';
}

/*****************************************************
*** Zoom
*****************************************************/
function ZoomIn() {
    var PageWidth = document.body.offsetWidth;
    var PageHeight = document.getElementById('PageEnd').offsetTop + 20;

    document.getElementById('body_pnlZoomPage').style.display = 'block';
    document.getElementById('body_pnlZoomPage').style.width = PageWidth + 'px';
    document.getElementById('body_pnlZoomPage').style.height = PageHeight + 'px';

    document.getElementById('body_pnlZoomBox').style.display = 'block';
    BoxWidth = document.getElementById('body_pnlZoomBox').offsetWidth;
    BoxHeight = document.getElementById('body_pnlZoomBox').offsetHeight;
    document.getElementById('body_pnlZoomBox').style.left = parseInt((PageWidth - BoxWidth) / 2) + 'px';
    document.getElementById('body_pnlZoomBox').style.top = (parseInt((document.body.offsetHeight - BoxHeight) / 2) + document.documentElement.scrollTop) + 'px';
}

function ZoomClose() {
    document.getElementById('body_pnlZoomPage').style.display = 'none';
    document.getElementById('body_pnlZoomBox').style.display = 'none';
}

/*****************************************************
*** 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 MaskCEP(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;
    }
}

/*****************************************************
*** 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 tn1 = hex.charAt(0);
    var tn2 = hex.charAt(1);

    var n1 = hex_chars.indexOf(tn1) * 16;
    var n2 = hex_chars.indexOf(tn2);

    return n1 + n2;
}