﻿// Funções genéricas de validação do site

// Esconde o texto passado por parâmetro quando se clica em algum link do site
function escondeTexto(resposta)
{
    if (document.getElementById(resposta).style.display == "none")
        document.getElementById(resposta).style.display = "block";
    else
        document.getElementById(resposta).style.display = "none";    
}

// Esconde as noticias e as alterna conforme links selecionados
function alternaNoticias(noticia)
{
    document.getElementById("noticia1").style.display = 'none';
    document.getElementById("noticia2").style.display = 'none';
    document.getElementById("noticia3").style.display = 'none';
    document.getElementById("noticia4").style.display = 'none';
    document.getElementById("noticia5").style.display = 'none';
    document.getElementById("noticia6").style.display = 'none';
    document.getElementById("noticia7").style.display = 'none';
    document.getElementById("noticia8").style.display = 'none';
    document.getElementById("noticia9").style.display = 'none';
    document.getElementById("noticia10").style.display = 'none';
    document.getElementById("noticia11").style.display = 'none';
    document.getElementById("noticia12").style.display = 'none';
    document.getElementById("noticia13").style.display = 'none';
    document.getElementById("noticia14").style.display = 'none';
    document.getElementById("noticia15").style.display = 'none';
    document.getElementById("noticia16").style.display = 'none';
    document.getElementById("noticia17").style.display = 'none';
    document.getElementById("noticia18").style.display = 'none';
    document.getElementById("noticia19").style.display = 'none';
    document.getElementById("noticia20").style.display = 'none';
    document.getElementById("noticia21").style.display = 'none';
    document.getElementById(noticia).style.display = 'block';
}

// Abre janela PopUp centralizada
function abrePopUp(pagina,largura,altura) {
        //pega a resolução do visitante
        w = screen.width;
        h = screen.height;

        //divide a resolução por 2, obtendo o centro do monitor
        meio_w = w/2;
        meio_h = h/2;

        //diminui o valor da metade da resolução pelo tamanho da janela, fazendo com q ela fique centralizada
        altura2 = altura/2;
        largura2 = largura/2;
        meio1 = meio_h-altura2;
        meio2 = meio_w-largura2;

        //abre a nova janela, já com a sua devida posição
        window.open(pagina,'','height=' + altura + ', width=' + largura + ', top='+meio1+', left='+meio2+'');
}

// Abre janela maximizada
function abreMaximizada(url) {
    var w = window.screen.availWidth;
    var h = window.screen.availHeight;
    var win = open(url,'url','resizable=1,status=1,menubar=1,toolbar=1,location=1,scrollbars=1');
    win.moveTo(0, 0);
    win.resizeTo(window.screen.availWidth, window.screen.availHeight)
}

// Validação de CPF e CNPJ

function validaCpfCnpj(oSrc,args){

    if (args.Value.length == 11){

    validaCpf(oSrc,args);

    }else if(args.Value.length == 14){

    validaCnpj(oSrc, args);

    }else{

    return args.IsValid = false;

    }

}

//Validação de CPF

function validaCpf(oSrc,args){

    s = args.Value;

    if (s == '01234567890' || s == '11111111111'  || s == '22222222222'  || s == '33333333333'  || s == '44444444444' ||
        s == '55555555555' || s == '66666666666'  || s == '77777777777'  || s == '88888888888'  || s == '99999999999' || s == '00000000000') {
        
        return args.IsValid = false;
    }

    //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 validaCnpj(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;

} 

// Validação de contato do prestador

function validaContato(oSrc,args){

    if (document.forms[0].ctl00_contentPrincipal_txtTelRes.value == '' && document.forms[0].ctl00_contentPrincipal_txtTelCom.value == '' &&
        document.forms[0].ctl00_contentPrincipal_txtCelular.value == '' && document.forms[0].ctl00_contentPrincipal_txtEmail.value == ''){

    return args.IsValid = false;

    }

    return args.IsValid = true;

}

// Validação de Maxlength para campos do tipo TextBox MultiLine
function ismaxlength(obj){

    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""

    if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)

}