// **************************************************************************
//
//    Project: Entrelinhas
//    Create Date: 14/02/2003
//    Layer: HTML;
//    Source: Ricardo David - ricardod@team.inter.net
//
//    Description: JavaScript Functions System
//
//    Update: [autor, dd/mm/aaaa]
//    Description: [update details]
//
// **************************************************************************

function validarCartaoCredito(txtNumCartao, strMsg) {
  st = txtNumCartao.value;
  if (st.length > 19 || st.length == 0){
    if(typeof(strMsg) != "undefined"){
        txtNumCartao.focus();
        alert(strMsg);
    }
    return (false);
  }

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }

  if ((sum % 10) == 0)
    return (true);
  else {
    if(typeof(strMsg) != "undefined"){
        txtNumCartao.focus();
        alert(strMsg);
    }
    return (false);
  }
}

function exibeTituloFuncao (strTitulo, strLink, strParent)    {
    window.parent(1).objTitulo.innerText = strTitulo;
    window.open ( strLink, strParent );
    window.focus();
}

function funcLocationHref( strLink ){
    window.location.href = strLink;
}

function validarTxt(txtInput, strMsg) {

    var strValor = trim(txtInput.value);
    if (strValor.length == 0 ) {
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        txtInput.select();
        txtInput.focus();
        return false;
    }
    return true;
}

function validarTxtMin (txtInput, intMin, strMsg) {

    var strValor = trim(txtInput.value);

    if (strValor.length < intMin ) {
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        txtInput.select();
        txtInput.focus();
        return false;
    }
    return true;
}


function marcarTodos(formulario) {
    f = formulario;
    for (i=0; i<f.length; i++)
        if (f.elements[i].type == "checkbox")
        if (f.todos.checked) f.elements[i].checked = true
        else f.elements[i].checked = false
}

function validarCombo(cboList, strMsg) {
    var intComboValue = cboList[cboList.selectedIndex].value;
    if (intComboValue == -1 ) {
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        cboList.focus();
        return false;
    }
    return true;
}

function validarEmail (txtEmail, strMsg) {

    var strEmail = trim(txtEmail.value);

    if (strEmail.length == 0 ) {
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        txtEmail.select();
        txtEmail.focus();
        return false;
    }

    var strEmailPat=/^(.+)@(.+)$/;
    var matchArray=strEmail.match(strEmailPat);

    if (matchArray==null) {
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        txtEmail.select();
        txtEmail.focus();
        return false;
    }

    return true;
}

function validarIP(txtIp, strMsg){
    var strDomain = trim(txtIp.value);
    var IPArray = strDomain.split(".");

    if (IPArray.length != 4){
        if(typeof(strMsg) != "undefined")
            alert(strMsg);
        txtIp.focus();
        return false;
    }

    for (var loop=0; loop < IPArray.length; loop++){
        if (IPArray[loop]>255) {
            if(typeof(strMsg) != "undefined"){
                alert(strMsg);
            }
            txtIp.select();
            txtIp.focus();
            return false;
        }
    }
    return true;
}

function validarData(cmbDia, cmbMes, txtAno, strMsg){
    //valida as datas no formato dd/mm/aaaa num text ...

    if (txtAno.value.length < 4){
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        return false;
    }

    var intAno = eval(txtAno.value);
    var intDia = cmbDia[cmbDia.selectedIndex].value;
    var intMes = cmbMes[cmbMes.selectedIndex].value;

    var arrMeses = new Array(13);
    arrMeses[1]  = 31;
    if((intAno % 4) == 0)
        arrMeses[2]  = 29;
    else
        arrMeses[2]  = 28;
    arrMeses[3]  = 31;
    arrMeses[4]  = 30;
    arrMeses[5]  = 31;
    arrMeses[6]  = 30;
    arrMeses[7]  = 31;
    arrMeses[8]  = 31;
    arrMeses[9]  = 30;
    arrMeses[10] = 31;
    arrMeses[11] = 30;
    arrMeses[12] = 31;

    if ((intDia < 1) || (eval(intDia) > arrMeses[eval(intMes)]) || (intMes < 1 || intMes > 12)){
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        return false;
    }

    return true;
}


function validarTxtData(txtInput, strMsg){
    if (txtInput.value.length < 10){
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        txtInput.focus();
        return false;
    }

    var arrCookies = document.cookie.split(/;/);
    var intIdIdioma;
    for(var i = 0; i < arrCookies.length; i++){
        if (arrCookies[i].indexOf("cokIdIdiomaFuncionario") == 1 ){
            intIdIdioma = arrCookies[i].substring(arrCookies[i].indexOf("=") + 1);
            break;
        }
    }

    var intAno = eval(txtInput.value.substring(6));

    if (intIdIdioma == 1 || intIdIdioma == 3){
        var intDia = txtInput.value.substring(0,2);
        var intMes = txtInput.value.substring(3,5);
    }else {
        var intDia = txtInput.value.substring(0,2);
        var intMes = txtInput.value.substring(3,5);
    }

    var arrMeses = new Array(13);
    arrMeses[1]  = 31;
    if((intAno % 4) == 0)
        arrMeses[2]  = 29;
    else
        arrMeses[2]  = 28;
    arrMeses[3]  = 31;
    arrMeses[4]  = 30;
    arrMeses[5]  = 31;
    arrMeses[6]  = 30;
    arrMeses[7]  = 31;
    arrMeses[8]  = 31;
    arrMeses[9]  = 30;
    arrMeses[10] = 31;
    arrMeses[11] = 30;
    arrMeses[12] = 31;

    if ((intDia < 1) || (eval(intDia) > arrMeses[eval(intMes)]) || (intMes < 1 || intMes > 12)){
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        txtInput.focus();
        return false;
    }

    return true;
}

function teclasNum(event, txtInput, intPos){
    var tecla = event.keyCode;
    if ((tecla > 47 && tecla < 58)) // numeros de 0 a 9
        return true;
    else {
        if(typeof(txtInput) != "undefined" && typeof(intPos) != "undefined" && txtInput.value.length == intPos){
            if (tecla != 8 && tecla != 45) // backspace
                event.keyCode = 0;
            else
                return true;
        }else {
            if (tecla != 8) // backspace
                event.keyCode = 0;
                //return false;
            else
                return true;
        }
    }
}

function teclasAlfa(event){
    var tecla = event.keyCode;
    if ((tecla > 64 && tecla < 91) || (tecla > 96 && tecla < 123))
        return true;
    else {
        if (tecla != 8) // backspace
            event.keyCode = 0;
            //return false;
        else
            return true;
    }
}

function teclasMoeda(event, txtInput){
    var tecla = event.keyCode;
    var strValor = txtInput.value;
    if (tecla > 47 && tecla < 58)
        return true;
    else {
        if ((tecla == 8) || ( (tecla == 44) && (strValor != '') && (strValor.indexOf(",") < 0 )))
            return true;
        else {
            if ((tecla == 8) || ( (tecla == 46) && (strValor != '') && (strValor.indexOf(".") < 0 )))
                return true;
            else
                event.keyCode = 0;
        }
    }
}

function teclasAlfaNum(E){
    var tecla = event.keyCode;
    if ((tecla > 64 && tecla < 91) || (tecla > 96 && tecla < 123) || (tecla > 47 && tecla < 58))
        return true;
    else {
        if (tecla != 8) // backspace
            event.keyCode = 0;
            //return false;
        else
            return true;
    }
}

function formatarData(event, txtInput, intMax, txtDestino){
    var tecla = event.keyCode;
    var intTam = txtInput.value.length;
    if ((intTam == 2 || intTam == 5) && tecla != 8)
        txtInput.value = txtInput.value + '/';

    autoTab(txtInput, txtDestino, intMax, event);

}

function caixaAlta(txtInput){
    var strValor = txtInput.value;
    txtInput.value = strValor.toUpperCase();
}

function caixaBaixa(txtInput){
    var strValor = txtInput.value;
    txtInput.value = strValor.toLowerCase();
}

function validacpf(txtCpf, strMsg){
    var i;
    strValor = txtCpf.value;
    var c = strValor.substr(0,9);
    var dv = strValor.substr(9,2);

    var d1 = 0;
    for (i = 0; i < 9; i++) {
        d1 += c.charAt(i)*(10-i);
    }

    if (d1 == 0){
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        return false;
    }

    d1 = 11 - (d1 % 11);
    if (d1 > 9)
        d1 = 0;

    if (dv.charAt(0) != d1) {
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        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){
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        return false;
    }

    return true;

}

function validarRadio(optRadio, strMsg) {
        var intLen = optRadio.length;
        if(typeof(intLen) == "undefined"){
            if (optRadio.checked)
                return true;
        }

        for (i=0; i<intLen; i++) {
            if (optRadio[i].checked) {
                return true;
            }
        }
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        return false;
}