// **************************************************************************
//
//    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 compararSenha(txtSenha1, txtSenha2, strMsg)
{
	var strSenha1 = trim(txtSenha1.value);
	var strSenha2 = trim(txtSenha2.value);
	
	if(strSenha1 != strSenha2)
	{
        if(typeof(strMsg) != "undefined"){
            alert(strMsg);
        }
        txtSenha1.select();
        txtSenha1.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 textareaTamMax(f,intLength,e){
    if(f.value.length>=intLength){
        return false;
    }
    return true;
}

function textareaCounter(f, intLength, intCounter){
        eval('f.form.' + intCounter).value = intLength - f.value.length;
}

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;
}

function formatarIP(event, txtIp, intMax, txtDestination){
    var intTecla = event.keyCode;
    strIp = txtIp.value;
    intLen = strIp.length;
    intPos = strIp.lastIndexOf(".");
    var IPArray = strIp.split(".");

    if (((intLen == 3 && intPos < 0) || (eval(intPos + 4) == intLen )) && (IPArray.length < 4) && (intTecla != 8)){

        txtIp.value = txtIp.value + '.';
    }

    if(typeof(intMax) != "undefined" && typeof(txtDestination) != "undefined"){
        autoTab(txtIp, txtDestination, intMax, event);
    }
}


function autoTab(txtOriginal,txtDestination, intMax, event){
   var intTecla = event.keyCode;
   if ( (txtOriginal.value.length >= intMax) && (intTecla != 37 && intTecla != 36)  ){
           txtDestination.focus();
   }
}

function trim(inputString)
{
        if (typeof inputString != "string")
        {
                return inputString;
        }

        var retValue = inputString;
        var ch = retValue.substring(0, 1);

        while (ch == " ")
        {
                retValue = retValue.substring(1, retValue.length);
                ch = retValue.substring(0, 1);
        }

        ch = retValue.substring(retValue.length-1, retValue.length);

        while (ch == " ")
        {
                retValue = retValue.substring(0, retValue.length-1);
                ch = retValue.substring(retValue.length-1, retValue.length);
        }

        while (retValue.indexOf("  ") != -1)
        {
                retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
        }

        return retValue;
}


function mudaCorBg(varTr, varCor){
  if(document.getElementById||(document.all && !(document.getElementById))){
         varTr.style.backgroundColor=varCor;
  }
}


function enviarBuscaSimples(objForm){

   var bolValida = true;

   bolValida = validarTxt(objForm.txtPalavraChave, "Por favor informe uma palavra chave para a busca");
   if (!bolValida)
     return false;

   return bolValida;

}

function enviarBusca(objForm){

   if ( (bolPreencheuDia) && ((!bolPreencheuMes) || (!bolPreencheuAno)) ){
     alert('Por favor preencha a data de publicação corretamente');
     return false;
   }else{
     return true;
   }



}

function paginacao( objForm )
{

      objForm.submit();

}
function status(id,op,msg,pg)
{
    varTr = eval('tr_'+id);
    varBgOld = varTr.style.backgroundColor;
    //mudaCorBg(varTr, '#FFFF99');
    varTr.style.backgroundColor = '#FE9900';

	if (confirm("Deseja "+msg+" a opção para este cadastro?"))

	{

		window.location.href = pg+"?stat="+id+"&opcao="+op;

	}else{
          varTr.style.backgroundColor = varBgOld;
          //mudaCorBg(varTr, varBgOld);
    }

	pg = "";
}


function txtBoxFormat(objForm, strField, sMask, evtKeyPress) { 
     var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla; 

     var nTecla = evtKeyPress.keyCode ? evtKeyPress.keyCode : evtKeyPress.which ? evtKeyPress.which : evtKeyPress.charCode;

     sValue = objForm[strField].value; 

     // Limpa todos os caracteres de formatação que 
     // já estiverem no campo. 
     sValue = sValue.toString().replace( "-", "" ); 
     sValue = sValue.toString().replace( "-", "" ); 
     sValue = sValue.toString().replace( ".", "" ); 
     sValue = sValue.toString().replace( ".", "" ); 
     sValue = sValue.toString().replace( "/", "" ); 
     sValue = sValue.toString().replace( "/", "" ); 
     sValue = sValue.toString().replace( "(", "" ); 
     sValue = sValue.toString().replace( "(", "" ); 
     sValue = sValue.toString().replace( ")", "" ); 
     sValue = sValue.toString().replace( ")", "" ); 
     sValue = sValue.toString().replace( " ", "" ); 
     sValue = sValue.toString().replace( " ", "" ); 
     fldLen = sValue.length; 
     mskLen = sMask.length; 

     i = 0; 
     nCount = 0; 
     sCod = ""; 
     mskLen = fldLen; 

     while (i <= mskLen) { 
       bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/")) 
       bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " ")) 

       if (bolMask) { 
         sCod += sMask.charAt(i); 
         mskLen++; } 
       else { 
         sCod += sValue.charAt(nCount); 
         nCount++; 
       } 

       i++; 
     } 

     objForm[strField].value = sCod; 

     if (nTecla != 8) { // backspace 
       if (sMask.charAt(i-1) == "9") { // apenas números... 
         return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9 
       else { // qualquer caracter... 
         return false; 
       } } 
     else { 
       return true; 
     } 
   } 
