//---------------------------------------------------------------------------
//  Funções Genéricas
//---------------------------------------------------------------------------
function Detalhes(posicao) {
    if (document.getElementById(posicao).style.display == 'none') {
        document.getElementById(posicao).style.display = ''
    } else {
        document.getElementById(posicao).style.display = 'none';
    }
}
//---------------------------------------------------------------------------
//  Faz aparecer um layer mostrando os autores, quando existe mais de 1(ucProdutos.ascx)
//---------------------------------------------------------------------------
function mostraAutores(valor,layerID) {
    document.getElementById(layerID).style.display = '';
    document.getElementById(layerID).innerHTML = "<br><br><br><b>Autores:</b><br>" + valor;
}

// Abre popup

var win=null;

function NewWindow(mypage,myname,w,h,scroll,pos) {
	LeftPosition=(screen.width)?(screen.width-(w*1.2)):100;TopPosition=(screen.height)?(screen.height-(h*1.5)):100;
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
}

//Print Atualize

var JanelaPopup = null;

function Print(valor) {
    var config = "width=800,height=600,top=20,left=20,scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";
    var strUrl = "/previewPrint.aspx?nota=imprime_" + valor;
    JanelaPopup = window.open(strUrl, 'JanelaExterna', config);
}    

//---------------------------------------------------------------------------
//  Controla Layer de Informações sobre o Departamento(faleConosco.aspx)
//---------------------------------------------------------------------------
function faleConosco_carrega_info(dados){
    if(dados == 'at'){
	    document.getElementById('departamento').innerHTML = 'Atendimento ao Cliente';
	    document.getElementById('texto1').innerHTML = 'Informações sobre promoções,  compra e utilização de produtos informatizados - CD-ROM e assinaturas online';
    }
	    if(dados == 'edit'){
		    document.getElementById('departamento').innerHTML = 'Editorial Jurídico';
		    document.getElementById('texto1').innerHTML = 'Fale com o editor: Informações sobre conteúdo de todos os produtos e serviços da Editora Saraiva.';
    }
	    if(dados == 'sup'){
		    document.getElementById('departamento').innerHTML = 'Suporte Técnico';
		    document.getElementById('texto1').innerHTML = 'Informações sobre instalação, acesso  e utilização dos produtos informatizados - CD-ROM e assinaturas online';
    }   	
    document.getElementById('InfDep').style.visibility = 'visible';
}
function faleConosco_fecha_info() {
    document.getElementById('InfDep').style.visibility = 'hidden';
}

function abreDados(valor) {
    var statDiv = ''
    if (document.all[valor + 1].style.display == "")
        statDiv = 'none'
    document.all[valor + 1].style.display = statDiv
}

function flash(flash, largura, altura) {
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="' + largura + '" height="' + altura + '" id=ShockwaveFlash5>');
    document.write('  <param name=movie value="' + flash + '">');
    document.write('  <param name=quality value=high>');
    document.write('  <param name=wmode value=transparent>');
    document.write('  <embed src="' + flash + '" quality=high pluginspage="https://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="' + largura + '" height="' + altura + '">');
    document.write('  </embed> ');
    document.write('</object>');
}

function flashObra(flash, largura, altura) {

    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="' + largura + '" height="' + altura + '" id=ShockwaveFlash5>');
    document.write('  <param name=movie value="' + flash + '">');
    document.write('  <param name=quality value=high>');
    document.write('  <param name=bgcolor value=#ffffff>');
    document.write('  <embed src="' + flash + '" quality=high bgcolor=#933303 pluginspage="https://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="' + largura + '" height="' + altura + '">');
    document.write('  </embed> ');
    document.write('</object>');
}

//---------------------------------------------------------------------------
//  Controla a multipla seleção de obras para envio ao comprar (catalogoLivrosColecoesDetalhe.aspx)
//---------------------------------------------------------------------------

function getComprarSelecao() {    
    var lista = "";
    for (i = 0; i < document.all("chkBoxSelecao").length; i++) {
        if (document.all("chkBoxSelecao")[i].checked == 1) {
            lista += document.all("chkBoxSelecao")[i].value + ",";
        }
    }
    if (lista != "") {
        lista = lista.substr(0, lista.length - 1);
        window.open("catalogoMinhasCompras.aspx?listaCod=" + lista, "_self");
    }
    else
        alert("Atenção: selecione alguma obra.");
}
//---------------------------------------------------------------------------
//  Abre a popup de upesquisainstituicao.aspx (catalogoLivrosColecoesDetalhe.aspx)
//---------------------------------------------------------------------------
function abrePopupInstituicao(theURL) {
    window.open(theURL, '', 'scrollbars=yes,resizable=no,top=50,left=50,width=500,height=300');
}

// Validação de CPF e CNPJ

function valida_CPFCNPJ(oSrc, args) {

    if (args.Value.length == 11) {

        valida_CPF(oSrc, args);

    } else if (args.Value.length == 11) {

        valida_CNPJ(oSrc, args);

    } else {

        return args.IsValid = false;

    }

}

//Validação de CPF

function valida_CPF(oSrc, args) {

    s = args.Value;

    //args.isValid = (s >= 3);

    //document.write(oSrc.Value + ',' + args.Value);

    if (isNaN(s)) {

        return args.IsValid = false;

    }

    var i;

    var c = s.substr(0, 9);

    var dv = s.substr(9, 2);

    var d1 = 0;

    for (i = 0; i < 9; i++) {

        d1 += c.charAt(i) * (10 - i);

    }

    if (d1 == 0) {

        return args.IsValid = false;

    }

    d1 = 11 - (d1 % 11);

    if (d1 > 9) d1 = 0;

    if (dv.charAt(0) != d1) {

        return args.IsValid = 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 args.IsValid = false;

    }

    return args.IsValid = true;

}

//Validação de CNPJ

function valida_CNPJ(oSrc, args) {

    s = args.Value;

    if (isNaN(s)) {

        return args.IsValid = false;

    }

    var i;

    var c = s.substr(0, 12);

    var dv = s.substr(12, 2);

    var d1 = 0;

    for (i = 0; i < 12; i++) {

        d1 += c.charAt(11 - i) * (2 + (i % 8));

    }

    if (d1 == 0)

        return args.IsValid = false;

    d1 = 11 - (d1 % 11);

    if (d1 > 9) d1 = 0;

    if (dv.charAt(0) != d1) {

        return args.IsValid = false;

    }

    d1 *= 2;

    for (i = 0; i < 12; i++) {

        d1 += c.charAt(11 - i) * (2 + ((i + 1) % 8));

    }

    d1 = 11 - (d1 % 11);

    if (d1 > 9)

        d1 = 0;

    if (dv.charAt(1) != d1) {

        return args.IsValid = false;

    }

    return args.IsValid = true;

} 



//function enviaCompra(formulario) {
//    document.getElementById(formulario).submit();
//    document.location.href = "../menuEsquerdo/catalogoMinhasComprasConfirmacao.aspx";
//}

// Validação de CPF e CNPJ
//function valida_CPFCNPJ(oSrc, args) {
//    argsTotal = args.Value.replace(/\./g, "").replace(/-/g, "");
//    if (argsTotal.length == 11) {
//        valida_CPF(oSrc, args);
//    } else if (argsTotal.length == 11) {
//        valida_CNPJ(oSrc, args);
//    } else {
//        return args.IsValid = false;
//    }

//}

////Validação de CPF

//function valida_CPF(oSrc, args) {
//    s = args.Value;
//    s = replaceAll(s, ".", "");
//    s = replaceAll(s, "-", "");
//    //args.isValid = (s >= 3);
//    //document.write(oSrc.Value + ',' + args.Value);
//    if (isNaN(s)) {
//        return args.IsValid = false;
//    }

//    var i;
//    var c = s.substr(0, 9);
//    var dv = s.substr(9, 2);
//    var d1 = 0;
//    for (i = 0; i < 9; i++) {
//        d1 += c.charAt(i) * (10 - i);
//    }
//    if (d1 == 0) {
//        return args.IsValid = false;
//    }

//    d1 = 11 - (d1 % 11);
//    if (d1 > 9) d1 = 0;
//    if (dv.charAt(0) != d1) {
//        return args.IsValid = 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 args.IsValid = false;
//    }

//    return args.IsValid = true;

//}

////Validação de CNPJ

//function valida_CNPJ(oSrc, args) {
//    s = args.Value;
//    s = replaceAll(s, ".", "");
//    s = replaceAll(s, "-", "");
//    if (isNaN(s)) {
//        return args.IsValid = false;
//    }
//    var i;
//    var c = s.substr(0, 12);
//    var dv = s.substr(12, 2);
//    var d1 = 0;
//    for (i = 0; i < 12; i++) {
//        d1 += c.charAt(11 - i) * (2 + (i % 8));
//    }

//    if (d1 == 0)
//        return args.IsValid = false;

//    d1 = 11 - (d1 % 11);
//    if (d1 > 9) d1 = 0;
//    if (dv.charAt(0) != d1) {
//        return args.IsValid = false;
//    }
//    d1 *= 2;
//    for (i = 0; i < 12; i++) {
//        d1 += c.charAt(11 - i) * (2 + ((i + 1) % 8));
//    }
//    d1 = 11 - (d1 % 11);
//    if (d1 > 9)
//        d1 = 0;
//    if (dv.charAt(1) != d1) {
//        return args.IsValid = false;
//    }
//    return args.IsValid = true;

//}

////Função que faz um replace em objeto enviado
//function replaceAll(str, de, para) {
//    var pos = str.indexOf(de);
//    while (pos > -1) {
//        str = str.replace(de, para);
//        pos = str.indexOf(de);
//    }
//    return (str);
//}

//function mask(format, obj, e) {
//    var keynum, keychar, numcheck;
//    var iChar = obj.value.length;

//    keynum = (window.event) ? e.keyCode : e.which;
//    keychar = String.fromCharCode(keynum);

//    if (keynum == 8 || keynum == 0) { return true; }
//    if (iChar >= format.length) { return false; }

//    var out = format.substring(iChar, iChar + 1);

//    switch (out) {
//        case "0":
//            numcheck = /\d/;
//            if (numcheck.test(keychar)) { return keychar; }
//            break;
//        case "A":
//            numcheck = /[A-Za-zÀ-ÿ]/;
//            if (numcheck.test(keychar)) { return keychar; }
//            break;
//        case "#":
//            return keychar;
//            break;
//        default:
//            obj.value += out;
//            return mask(format, obj, e);
//    }
//    return false;
//}

////********************************************************************************************
////Só não funcionou no Chrome
//function formatarCPF(src, mask) {
//    var i = src.value.length;
//    var saida = mask.substring(0, 1);
//    var texto = mask.substring(i)
//    if (texto.substring(0, 1) != saida) {
//        src.value += texto.substring(0, 1);
//    }
//}


