﻿function CallHigh(src) {
    parent.location.href = '/RevistaHorse/WebSite' + src;
}

function CallNews(item) {
    parent.location.href = '/RevistaHorse/WebSite/Noticias/Default.aspx?c=H&item=' + item;
}

function CallMovie(item) {
    parent.location.href = '/RevistaHorse/WebSite/Movie/Default.aspx?item=' + item;
}

function CallSearch(Keyword) {
    if (Keyword.length < 3) {
        alert('A palavra-chave tem que ter pelo menos 3 caracteres.');
    } else {
        parent.location.href = '/RevistaHorse/WebSite/Search/Default.aspx?key=' + MyUrlEncode(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;
}

/*****************************************************
*** Publicity
*****************************************************/
function PublicityClick(IdPub) {
    wsPublicity.CountClick(IdPub, OnCompleteCountPublicityClick, OnTimeOutCountPublicityClick, OnErrorCountPublicityClick);
}

function OnCompleteCountPublicityClick(value) { }

function OnTimeOutCountPublicityClick(value) { }

function OnErrorCountPublicityClick(value) { }

/*****************************************************
*** Masks
*****************************************************/
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 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 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;
}

function MaskPhone(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '(00) 0000-0000', event);
}

function MaskCEP(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '00.000-000', event);
}

function MaskDate(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '00/00/0000', event);
}

function MaskCPF(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '000.000.000-00', event);
}

function MaskCNPJ(field) {
    if (MaskInteger(field) == false) {
        event.returnValue = false;
    }
    return FormatField(field, '00.000.000/0000-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 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;
}

/*****************************************************
*** 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;
    }
}

/*****************************************************
*** Send To
*****************************************************/
var BoxWidth;
var BoxHeight;
function SendToFriend(TitleSend, LinkSend) {
    var PageWidth = document.body.offsetWidth;
    var PageHeight = document.getElementById('PageEnd').offsetTop + 20;
    
    document.getElementById('ctl00_body_pnlSendToPage').style.display = 'block';
    document.getElementById('ctl00_body_pnlSendToPage').style.width = PageWidth + 'px';
    document.getElementById('ctl00_body_pnlSendToPage').style.height = PageHeight + 'px';

    document.getElementById('ctl00_body_pnlSendToBox').style.display = 'block';
    BoxWidth = document.getElementById('ctl00_body_pnlSendToBox').offsetWidth;
    BoxHeight = document.getElementById('ctl00_body_pnlSendToBox').offsetHeight;
    document.getElementById('ctl00_body_pnlSendToBox').style.left = parseInt((PageWidth - BoxWidth) / 2) + 'px';
    document.getElementById('ctl00_body_pnlSendToBox').style.top = (parseInt((document.body.offsetHeight - BoxHeight) / 2) + document.documentElement.scrollTop) + 'px';

    document.getElementById('ctl00_body_lblSendToBox').innerHTML = '<iframe id="frmSend" name="frmSend" src="/RevistaHorse/WebSite/SendTo/Default.aspx?title=' + escape(TitleSend) + '&link=' + escape(LinkSend) + '" height="258" width="500" frameborder="0" scrolling="no"></iframe>';
}

function SendToClose() {
    document.getElementById('ctl00_body_pnlSendToPage').style.display = 'none';
    document.getElementById('ctl00_body_pnlSendToBox').style.display = 'none';
}

/*****************************************************
*** Zoom
*****************************************************/
function ZoomIn() {
    var PageWidth = document.body.offsetWidth;
    var PageHeight = document.getElementById('PageEnd').offsetTop + 20;
    
    document.getElementById('ctl00_body_pnlZoomPage').style.display = 'block';
    document.getElementById('ctl00_body_pnlZoomPage').style.width = PageWidth + 'px';
    document.getElementById('ctl00_body_pnlZoomPage').style.height = PageHeight + 'px';

    document.getElementById('ctl00_body_pnlZoomBox').style.display = 'block';
    BoxWidth = document.getElementById('ctl00_body_pnlZoomBox').offsetWidth;
    BoxHeight = document.getElementById('ctl00_body_pnlZoomBox').offsetHeight;
    BoxTop = parseInt((document.body.offsetHeight - BoxHeight) / 2) + document.documentElement.scrollTop;
    if (BoxTop < 0) { BoxTop = 0; }
    document.getElementById('ctl00_body_pnlZoomBox').style.left = parseInt((PageWidth - BoxWidth) / 2) + 'px';
    document.getElementById('ctl00_body_pnlZoomBox').style.top = BoxTop + 'px';
}

function ZoomClose() {
    document.getElementById('ctl00_body_pnlZoomPage').style.display = 'none';
    document.getElementById('ctl00_body_pnlZoomBox').style.display = 'none';
}

function ZoomInBlog(src) {
    var PageWidth = document.body.offsetWidth;
    var PageHeight = document.getElementById('PageEnd').offsetTop + 20;
    
    document.getElementById('ctl00_body_pnlZoomPage_' + src).style.display = 'block';
    document.getElementById('ctl00_body_pnlZoomPage_' + src).style.width = PageWidth + 'px';
    document.getElementById('ctl00_body_pnlZoomPage_' + src).style.height = PageHeight + 'px';

    document.getElementById('ctl00_body_pnlZoomBox_' + src).style.display = 'block';
    BoxWidth = document.getElementById('ctl00_body_pnlZoomBox_' + src).offsetWidth;
    BoxHeight = document.getElementById('ctl00_body_pnlZoomBox_' + src).offsetHeight;
    BoxTop = parseInt((document.body.offsetHeight - BoxHeight) / 2) + document.documentElement.scrollTop;
    if (BoxTop < 0) { BoxTop = 0; }
    document.getElementById('ctl00_body_pnlZoomBox_' + src).style.left = parseInt((PageWidth - BoxWidth) / 2) + 'px';
    document.getElementById('ctl00_body_pnlZoomBox_' + src).style.top = BoxTop + 'px';
}

function ZoomCloseBlog(src) {
    document.getElementById('ctl00_body_pnlZoomPage_' + src).style.display = 'none';
    document.getElementById('ctl00_body_pnlZoomBox_' + src).style.display = 'none';
}

/*****************************************************
*** Developer
*****************************************************/
function ShowDeveloper() {
    alert('OK');
}