/*
Name: 		   scripts.js
Copyright:   Fundação Abrinq
Author: 	   Marcelo Martins
Data: 		   02/08/2006
Atualizado:  22/03/2008
Description: Funções em JavaScript para rodar no cliente.
*/

//------------------------------------------------------------------------------

function abre_janela(url, config) {
    var urls = '';
    var conf = '';
    urls = url;
    conf = config;
    window.open(urls, 'Jal', conf);
}

function confirma() {
    var frase = 'Deseja realmente sair do sistema ?';
    if (!confirm(frase)) return false;
}

function confirmaEnc() {
    var frase = 'Deseja realmente encerrar o preenchimento ?';
    if (!confirm(frase)) return false;
}

function confirmaacao() {
    var frase = 'Todas as acoes cadastradas serao apagadas!';
    if (!confirm(frase)) return false;
}

function confirmaExcluir() {
    var fraseE = 'Deseja realmente excluir o registro ?';
    if (!confirm(fraseE)) return false;
}

function confirmaRecursoHumano() {
    var fraseE = 'Nao ha nenhum recurso humano a ser contratado. Tem certeza que deseja continuar?';
    if (!confirm(fraseE)) return false;
}


function alerta(msg) {
    if (!alert(msg)) return false;
}

function key() {
    theKey = event.keyCode;
    if ((theKey < 48) || (theKey > 57))
        event.returnValue = false;
}

/*
Exemplo (key): aplicar no atributo ( onKeyPress="key();" )
*/

function isMail(mailField) {
    strMail = mailField.value;
    var re = new RegExp;
    re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var arr = re.exec(strMail);
    if (arr == null)
        return (false);
    else
        return (true);
}

function FormataReais(fld, milSep, decSep, e) {
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    //var whichCode = (window.Event) ? e.which : e.keyCode;
    var whichCode = (window.addEventListener) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode);  // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false;  // Chave inválida
    len = fld.value.length;
    for (i = 0; i < len; i++)
        if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
    aux = '';
    for (; i < len; i++)
        if (strCheck.indexOf(fld.value.charAt(i)) != -1) aux += fld.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) fld.value = '';
    if (len == 1) fld.value = '0' + decSep + '0' + aux;
    if (len == 2) fld.value = '0' + decSep + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += milSep;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        fld.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            fld.value += aux2.charAt(i);
        fld.value += decSep + aux.substr(len - 2, len);
    }
    return false;
}


function FormataTel(fld, e) {
    var key = '';
    var len = 0;
    var strCheck = '0123456789';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode);
    len = fld.value.length;
    if (len > 8) return false;
    if (len == 4) {
        if (key == '-') {
            return;
        } else {
            if (strCheck.indexOf(key) >= 0) {
                fld.value += '-' + key;
            }
            return false;
        }
    }
    if (strCheck.indexOf(key) == -1)
        return false;
}

/*
Exempllo (FormataTel): aplicar no atributo ( onKeyPress="return(FormataTel(this,event));" )
*/

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e) {
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for (i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for (; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i)) != -1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0' + SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0' + SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

/*
Exemplo (MascaraMoeda): aplicar no atributo ( onKeyPress="return(MascaraMoeda(this,'.',',',event));" )
*/

function mascara(e, src, mask) {
    if (window.event) {
        _TXT = e.keyCode;
    } else
        if (e.which) {
        _TXT = e.which;
    }
    if (_TXT > 47 && _TXT < 58) {
        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);
        }
        return true;
    } else {
        if (_TXT != 8) {
            return false;
        } else {
            return true;
        }
    }
}



function FormataCpf(campo, teclapres) {
    var tecla = teclapres.keyCode;
    var vr = new String(campo.value);
    vr = vr.replace(".", "");
    vr = vr.replace("/", "");
    vr = vr.replace("-", "");
    tam = vr.length + 1;
    if (tecla != 14) {
        if (tam == 4)
            campo.value = vr.substr(0, 3) + '.';
        if (tam == 7)
            campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 6) + '.';
        if (tam == 11)
            campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(7, 3) + '-' + vr.substr(11, 2);
    }
}



function FormataCPF(fld, e) {
    var sep = key = '';
    var len = 0;
    var intCheck = '0123456789';
    var strCheck = '.-';
    var aux = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 0 || whichCode == 8) return true;
    key = String.fromCharCode(whichCode);
    len = fld.value.length;
    if (len > 13) return false;
    if (len == 1 || len == 2 || len == 3 || len == 7 || len == 11) {
        if (strCheck.indexOf(key) == 0) {
            if (len == 3 || len == 7) { return; }
            else if (len < 3) {
                aux = '00' + fld.value;
                fld.value = aux.substr(aux.length - 3, 3) + key;
                return false;
            }
        } else if (strCheck.indexOf(key) == 1) {
            if (len == 11) { return; }
            else { return false; }
        }
        else {
            if (intCheck.indexOf(key) >= 0) {
                if (len < 3) { return; }
                if (len < 11) { sep = '.'; }
                else { sep = '-'; }
                fld.value += sep + key;
            }
            return false;
        }
    }
    if (intCheck.indexOf(key) == -1) return false;
}

/*
Exemplo (FormataCPF): aplicar no atributo ( onKeyPress="return(FormataCPF(this,event))" )
*/

function FormataCNPJ(fld, e) {
    var sep = key = '';
    var len = 0;
    var intCheck = '0123456789';
    var strCheck = './-';
    var aux = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 0 || whichCode == 8) return true;
    key = String.fromCharCode(whichCode);
    len = fld.value.length;
    if (len > 17) return false;
    if (len == 1 || len == 2 || len == 6 || len == 10 || len == 15) {
        if (strCheck.indexOf(key) == 0) {
            if (len == 2 || len == 6) { return; }
            else if (len == 1) {
                aux = '0' + fld.value;
                fld.value = aux + key;
                return false;
            }
        }
        else if (strCheck.indexOf(key) == 1) {
            if (len == 10) { return; }
            else { return false; }
        }
        else if (strCheck.indexOf(key) == 2) {
            if (len == 15) { return; }
            else { return false; }
        } else {
            if (intCheck.indexOf(key) >= 0) {
                if (len == 1) { return; }
                if (len < 10) { sep = '.'; }
                else if (len < 15) { sep = '/'; }
                else { sep = '-'; }
                fld.value += sep + key;
            }
            return false;
        }
    }
    if (intCheck.indexOf(key) == -1) return false;
}

/*
Exemplo (FormataCNPJ): aplicar no atributo ( onKeyPress="return(FormataCNPJ(this,event))" )
*/

function cpfcnpj_ok(field, length) {
    numero = getNumber(field, length);
    primeiro = numero.substr(1, 1);
    falso = true;
    size = numero.length;
    if (size != length) { return false; }
    size--;
    for (i = 2; i < size - 1; ++i) {
        proximo = (numero.substr(i, 1));
        if (primeiro != proximo) { falso = false; }
    }
    if (falso) { return false; }
    if (modulo(numero.substring(0, numero.length - 2)) + ''
  													   		  + modulo(numero.substring(0, numero.length - 1))
  														 			!= numero.substring(numero.length - 2, numero.length)) {
        return false;
    }
    return true;
}

function modulo(str) {
    soma = 0;
    ind = 2;
    for (pos = str.length - 1; pos > -1; pos = pos - 1) {
        soma = soma + (parseInt(str.charAt(pos)) * ind);
        ind++;
        if (str.length > 11) { if (ind > 9) ind = 2; }
    }
    resto = soma - (Math.floor(soma / 11) * 11);
    if (resto < 2) { return 0; }
    else {
        return 11 - resto;
    }
}

function getNumber(number, len) {
    var result = '';
    var num, i;
    for (i = 0; i < number.length; i++) {
        try {
            num = parseInt(number.substring(i, i + 1));
            if (!isNaN(num)) { result += num; }
        }
        catch (exception)
      													 { }
    }
    if (result.length != len) {
        result = '000000000000000' + result;
        var newLen = result.length;
        result = result.substring(newLen - len, newLen);
    }
    return result;
}

function FormataData(fld, sep, e) {
    var key = '';
    var len = 0;
    var strCheck = '0123456789';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 0 || whichCode == 8) return true;
    key = String.fromCharCode(whichCode);
    len = fld.value.length;
    if (len > 9) return false;
    if (len == 2 || len == 5) {
        if (key == sep) {
            return;
        } else {
            if (strCheck.indexOf(key) >= 0) {
                fld.value += sep + key;
            }
            return false;
        }
    }
    if (strCheck.indexOf(key) == -1) return false;
}




//--- Exemplo: onKeyPress="return(FormataData(this,'/',event));"

function date_ok(field) {
    var DateStr = field.value;
    var reg = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
    if (!reg.test(DateStr)) {
        return false;
    }
    var arr_date = DateStr.split('/');
    var iso = arr_date[2] + arr_date[1] + arr_date[0];
    var mnum = parseInt(arr_date[1], 10);
    if (mnum > 12 || mnum < 1) {
        return false;
    }
    var month = new Array();
    month[1] = 'Jan';
    month[2] = 'Feb';
    month[3] = 'Mar';
    month[4] = 'Apr';
    month[5] = 'May';
    month[6] = 'Jun';
    month[7] = 'Jul';
    month[8] = 'Aug';
    month[9] = 'Sep';
    month[10] = 'Oct';
    month[11] = 'Nov';
    month[12] = 'Dec';
    var checkDate = new Date(arr_date[0] + ' ' + month[mnum] + ' ' + arr_date[2]);
    var checkDateStr = checkDate.toGMTString();
    var arr_checkdate = checkDateStr.split(' ');
    if (month[mnum] != arr_checkdate[2]) {
        return false;
    }
    return true;
}

VerifiqueTAB = true;
function Mostra(quem, tammax) {
    if ((quem.value.length == tammax) && (VerifiqueTAB)) {
        var i = 0, j = 0, indice = -1;
        for (i = 0; i < document.forms.length; i++) {
            for (j = 0; j < document.forms[i].elements.length; j++) {
                if (document.forms[i].elements[j].name == quem.name) {
                    indice = i;
                    break;
                }
            }
            if (indice != -1) break;
        }
        for (i = 0; i <= document.forms[indice].elements.length; i++) {
            if (document.forms[indice].elements[i].name == quem.name) {
                while ((document.forms[indice].elements[(i + 1)].type == "hidden") &&
                  (i < document.forms[indice].elements.length)) {
                    i++;
                }
                document.forms[indice].elements[(i + 1)].focus();
                VerifiqueTAB = false;
                break;
            }
        }
    }
}

function PararTAB(quem) { VerifiqueTAB = false; }
function ChecarTAB() { VerifiqueTAB = true; }
													 
