function funcLocationHref( strLink ){
    window.location.href = strLink;
}

function numberOnly(evt) {

    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
       var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
       if (elem) {
           var charCode = (evt.charCode) ? evt.charCode :
               ((evt.which) ? evt.which : evt.keyCode);

           if ((charCode < 32 ) ||
               (charCode > 44 && charCode < 44) ||
               (charCode > 47 && charCode < 58)) {
               return true;
           } else {
               return false;
           }
       }
    }
}

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 comparacaoSenha(txtSenha1, txtSenha2) {
	
	var strSenha1 = trim(txtSenha1.value);
	var strSenha2 = trim(txtSenha2.value);
	
	if (strSenha1 != strSenha2) {
		alert('As senhas nao conferem, por favor redigite-as.');
		txtSenha1.value = '';
		txtSenha2.value = '';
		txtSenha1.select();
		txtSenha1.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 == '' ) {
        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, intTeclaEspaco, txtInput, intPos){
    var tecla = event.keyCode;
    if ((tecla > 47 && tecla < 58) || tecla == intTeclaEspaco) // 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 ((strValor == '00000000000') || (strValor == '11111111111') || (strValor == '22222222222') || (strValor == '33333333333')
		|| (strValor == '44444444444') || (strValor == '55555555555') || (strValor == '66666666666') || (strValor == '77777777777')
		|| (strValor == '88888888888') || (strValor == '99999999999')) {
		alert(strMsg);
		return false;
	}
	
    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;
   //alert (intTecla);
   if ( (txtOriginal.value.length >= intMax) && ((intTecla != 8) && (intTecla != 46) && (intTecla != 9) && (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 isCGC(txtCpf, strMsg)
{
 strNum=""
 x = txtCpf.value;

 if (x=="") {
  alert(strMsg);
  return (false);
 }
 l = x.length;
 for (i = 0; i < l; i++) {
  caracter = x.substring(i,i+1)
  if ((caracter >= '0') && (caracter <= '9'))
   strNum = strNum + caracter;
        }

 strMul = "6543298765432"
        iValido = 1
        if(strNum.length != 14)
          {
		  alert(strMsg);	
          return(false)
          }
        iSoma = 0
        strNum_base = strNum.substring(0,12)
        iLenNum_base = strNum_base.length - 1
        iLenMul = strMul.length - 1
        for(i=0;i<12;i++)
           {
           iSoma = iSoma + parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) * parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10)
           }

        iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11)
        if(iSoma == 11 || iSoma == 10)
          {
          iSoma = 0
          }

        strNum_base = strNum_base + iSoma
        iSoma = 0
        iLenNum_base = strNum_base.length - 1
        for(i=0;i<13;i++)
           {
           iSoma = iSoma + parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) * parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10)
           }

        iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11)
        if(iSoma == 11 || iSoma == 10)
          {
          iSoma = 0
          }

        strNum_base = strNum_base + iSoma
        if(strNum != strNum_base)
          {
		  alert(strMsg);
          return(false);
          }

        return(true)
}

function somenteNumero(obj,e)
{

 var valor, val;

 liberado = new Array(',','.');
 liberadoE = new Array(188,190,8);

 valor = obj.value;
 if(document.all)
 {
  if(!((e.keyCode > 47 && e.keyCode < 58) || Array.find(liberadoE,e.keyCode) != '-1' ))   {
    obj.value = valor. substr(0,valor.length - 1);
  }
 }
 else
 {
  val = '';

  for (x = 0; x < valor.length; x++)
  {
   if(!isNaN(valor[x]) || Array.find(liberado,valor[x]) != '-1')
   {
    val += valor[x];
   }
  }
  obj.value = val;
 }
}

Array.find = function(ary, element)
{
  for(var i=0; i<ary.length; i++)
  {
    if(ary == element)
    {
      return i;
    }
  }
  return -1;
}