﻿function fnLimpar(palavra)
{
    //Não busca quando texto default estiver preenchido
    if (palavra == 'O que você procura?') {
        palavra = '';
    }

    //Remover caracteres especiais
    if(palavra != '' && palavra != null) {
        palavra = palavra.replace(/\,|\//ig, " ")
        palavra = palavra.replace(/[^\wçàáéíóúüãõâêô\-\. ]/ig, "")
        palavra = palavra.replace(/\s{2,}/ig, " ")
    }
    
    return palavra;
}

/*
function createNewFormElement(inputForm, elementName, elementValue) {
    var newElement = document.createElement("<input name='" + elementName + "' type='hidden'>");
    inputForm.appendChild(newElement);
    newElement.value = elementValue;
    return newElement;
}*/
 

function fnbuscar(e, palavra)
{
    palavra = fnLimpar(palavra);
    if(e != null)
    {
    
        if(e.which || e.keyCode)
        {
            if ((e.which == 13) || (e.keyCode == 13)) {
                if (palavra.length == 0) {
                    return false;
                }
                if (palavra == 'O que você procura?') {
                    return false;
                }
                Modal_Abrir('goBusca');

                var formulario = document.createElement("Form");
                document.body.appendChild(formulario);
                formulario.method = "POST";

                //var txtPalavra = document.createElement("<input name='palavra' type='hidden'>");
                var txtPalavra = document.createElement("input");
                txtPalavra.name = "txtPalavra";
                txtPalavra.type = "hidden";
                txtPalavra.value = palavra;
                formulario.appendChild(txtPalavra);

                formulario.action = (palavra).replace(".", "") + '_LIS.aspx';
                formulario.submit();
                
                return false;
            }
            else
            {
                return e;
            }
        }
    }
    else
    {  
        var palavra = document.getElementById('ctl00_topo1_txtbuscar').value;
        if (palavra == '' || palavra == null) {
            return false;
        }

        if (palavra != '')
        {
            palavra = fnLimpar(palavra);
            if (palavra.length == 0) {
                return false;
            }
            else {
                Modal_Abrir('goBusca');

                var formulario = document.createElement("Form");
                document.body.appendChild(formulario);
                formulario.method = "POST";

                //var txtPalavra = document.createElement("<input name='palavra' type='hidden'>");
                var txtPalavra = document.createElement("input");
                txtPalavra.name = "txtPalavra";
                txtPalavra.type = "hidden";
                txtPalavra.value = palavra;
                formulario.appendChild(txtPalavra);
                
                formulario.action = (palavra).replace(".", "") + '_LIS.aspx';
                formulario.submit();

                //window.parent.location = (palavra).replace(".","") + '_LIS.aspx';
            }
            return false;
        }
    }
}

function fnEntrar(e, botao) {
    if (e != null) {
        if (e.which || e.keyCode) {
            if ((e.which == 13) || (e.keyCode == 13)) {
                Modal_Abrir('goBusca');
                document.getElementById(botao).click();
                return false;
            }
            else {
                return e;
            }
        }
    }
}
