﻿
function searching(objNameText, objNameMainDiv, objNameLoading, objNameDestino, objNameNoResults, origem, origemclik, text) {
    var objTxt = document.getElementById(objNameText);

    if (objTxt == null) {
        return;
    }

    if (objTxt.value == null || objTxt.value == '') {
        return;
    }

    var objMain = document.getElementById(objNameMainDiv);
    mopen(objNameMainDiv, objNameText);
    if (objTxt != null) {
        if (objTxt.value != text) {
            if (((objTxt.value.length >= 1) && (origemclik == 'txtbox')) || ((origemclik == 'img') && (objTxt.value.length >= 1))) {
                showPreLoad(objNameLoading);
                var xSearch = objTxt.value;
                while (xSearch.indexOf("'") != -1) {
                    xSearch = xSearch.replace("'", "%27");
                }
                while (xSearch.indexOf('"') != -1) {
                    xSearch = xSearch.replace('"', "%22");
                }
                while (xSearch.indexOf('&') != -1) {
                    xSearch = xSearch.replace('&', "%26");
                }
                var data = new Date;
                var tsSimples = "";
                var DoPost = tr.concat("xSearch=", xSearch);
                DoPost += tr.concat("&xOrigem=", origem);
                DoPost += tr.concat("&xTxtDestino=", objNameText);
                DoPost += tr.concat("&xPanelDestino=", objNameDestino);
                DoPost += tr.concat("&xPanelNoResultso=", objNameNoResults);
                DoPost += tr.concat("&xLoading=", objNameLoading);
                DoPost += tr.concat("&xPanelMain=", objNameMainDiv);
                tsSimples = tr.concat(data.getFullYear(), "_", data.getMonth(), "_", data.getDate(), "_", data.getHours(), "_", data.getMinutes(), "_", data.getSeconds());
                if (xhrPedido == true) {
                    xhr.abort();
                }
                xhrPedido = true;
                xhr = createXHR();
                if (xhr) {
                    var xPagina = tr.concat('http://'+location.host + ((location.pathname.toLowerCase().indexOf("frontoffice") !=-1)?'/Frontoffice':'') + '/ground/xSearch.aspx?ts=', tsSimples);
                    xhr.open("POST", xPagina);
                    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState == 4) {
                            if (xhr.status == 200) {
                                loadSearch(xhr.responseText, objNameText, objNameDestino, objNameNoResults, objNameLoading, objNameMainDiv);
                                xhrPedido = false;
                            }
                        }
                    };
                    xhr.send(DoPost);
                }
                //showPreLoad(objNameDestino);
            }
            else {
                //hidePreLoad(objNameDestino);
                hidePreLoad(objNameLoading);
            }
        }
    }
}

// time in milliseconds to wait after last character typed by user
var _WAIT_TIME = 500;
var _timeoutFunction;

function onTxtSearchKeyDown() {
    if (_timeoutFunction != null) clearTimeout(_timeoutFunction);
}
function onTxtSearchKeyUp(objNameText, objNameMainDiv, objNameLoading, objNameDestino, objNameNoResults, origem, origemclik, text) {
    if (_timeoutFunction != null) clearTimeout(_timeoutFunction);
    _timeoutFunction = setTimeout(
                                function() {
                                    searching(objNameText, objNameMainDiv, objNameLoading, objNameDestino, objNameNoResults, origem, origemclik, text);
                                    objNameText = null;
                                    objNameMainDiv = null;
                                    objNameLoading = null;
                                    objNameDestino = null;
                                    objNameNoResults = null;
                                    origem = null;
                                    origemclik = null;
                                    text = null;
                                },
                                _WAIT_TIME
                            );
}
function loadSearch(retorno, objNameText, objNameDestino, objNameNoResults, objNameLoading, objNameMainDiv) {
    if (retorno != null) {
        var obj = document.getElementById(objNameDestino);
        if (obj != null) {

            var textoR = retorno.split("§");

            if (textoR) {
                var Al = tr.concat(" xOrigem=", textoR[0]);
                Al += tr.concat(" xTxtDestino=", textoR[1]);
                Al += tr.concat(" xPanelDestino=", textoR[2]);
                Al += tr.concat(" xPanelNoResultso=", textoR[3]);
                Al += tr.concat(" xLoading=", textoR[4]);
                Al += tr.concat(" xPanel=", textoR[5]);
                //alert(Al);

                var preCom = textoR[6];

                //
                preCom = preCom.replace('<form name="form1" method="post" action=', "<!--")
                preCom = preCom.replace('id="form1">', "-->")
                preCom = preCom.replace("</form>", "")
                preCom = preCom.replace(' name="__VIEWSTATE" id="__VIEWSTATE" ', "")

                obj.innerHTML = preCom;
            }

            mopen(objNameMainDiv, objNameText);
            showPreLoad(objNameDestino);
        }
        hidePreLoad(objNameLoading);
    }
}
