﻿function CallHigh(src) {
    location.href = src;
}

/*****************************************************
*** Newsletter
*****************************************************/
function MenuNewsletterTextBoxOnEnter() {
    document.getElementById('txtRegisterMail').className = 'NewsletterTextBoxBlack';
    if (document.getElementById('txtRegisterMail').value == 'e-mail ...') { document.getElementById('txtRegisterMail').value = ''; }
}

function MenuNewsletterTextBoxOnExit() {
    if (document.getElementById('txtRegisterMail').value == '') {
        document.getElementById('txtRegisterMail').value = 'e-mail ...';
        document.getElementById('txtRegisterMail').className = 'NewsletterTextBoxGray';
    }
}

function NewsletterKeyPressEnter() {
    keyPressed = event.keyCode;
    if (keyPressed == 13) {
        NewsletterAdd();
        return false;
    }
}

function NewsletterAdd() {
    var Mail = Trim(document.getElementById('txtRegisterMail').value);

    if ((Mail.length > 5) && (Mail.indexOf('@') > 0) && (Mail.indexOf('.') > 2)) {
        location.href = '/newsletter/' + Mail + '/';
    } else {
        alert('O e-mail digitado não é um endereço válido!');
    }
}

/*****************************************************
*** 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.sincomerciariosorocaba.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) { }
}

/*****************************************************
*** Search
*****************************************************/
function CallSearch(Keyword) {
    if (Keyword == '') {
        parent.location.href = '/busca/';
    } else if (Keyword.length < 3) {
        alert('A palavra-chave tem que ter pelo menos 3 caracteres.');
    } else {
        parent.location.href = '/busca/' + MyUrlEncode(Trim(Keyword));
    }
}

function SearchClick() {
    CallSearch(document.getElementById('ctl00_Header_txtSearchHeader').value);
}

function KeyPressEnterOnSearch() {
    keyPressed = event.keyCode;
    if (keyPressed == 13) {
        SearchClick();
        return false;
    }
}

function Trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

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;
}

function SearchTextBoxOnEnter() {
    document.getElementById('ctl00_Header_txtSearchHeader').className = 'HeaderSearchTextBoxBlack';
    if (document.getElementById('ctl00_Header_txtSearchHeader').value == 'buscar') { document.getElementById('ctl00_Header_txtSearchHeader').value = ''; }
}

function SearchTextBoxOnExit() {
    if (document.getElementById('ctl00_Header_txtSearchHeader').value == '') {
        document.getElementById('ctl00_Header_txtSearchHeader').value = 'buscar';
        document.getElementById('ctl00_Header_txtSearchHeader').className = 'HeaderSearchTextBoxGray';
    }
}

/*****************************************************
*** 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;
    }
}

/*****************************************************
*** 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;
    document.getElementById('ctl00_body_pnlZoomBox').style.left = parseInt((PageWidth - BoxWidth) / 2) + 'px';
    document.getElementById('ctl00_body_pnlZoomBox').style.top = (parseInt((document.body.offsetHeight - BoxHeight) / 2) + document.documentElement.scrollTop) + 'px';
}

function ZoomClose() {
    document.getElementById('ctl00_body_pnlZoomPage').style.display = 'none';
    document.getElementById('ctl00_body_pnlZoomBox').style.display = 'none';
}

/*****************************************************
*** Mask
*****************************************************/
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;
    }
}

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 IsDateValid(value) {
    if(value.length!=10) return false;    
    
    var data        = value;    
    var dia         = data.substr(0,2);    
    var barra1      = data.substr(2,1);    
    var mes         = data.substr(3,2);    
    var barra2      = data.substr(5,1);    
    var ano         = data.substr(6,4);    
    
    if(data.length!=10||barra1!="/"||barra2!="/"||isNaN(dia)||isNaN(mes)||isNaN(ano)||dia>31||mes>12)return false;    
    
    if((mes==4||mes==6||mes==9||mes==11) && dia==31) return false;    
    if(mes==2 && (dia>29||(dia==29 && ano%4!=0))) return false;    
    if(ano < 1900) return false;    
    
    return true;
}
