﻿$(function () {
    function log(message) {
        $("<div/>").text(message).prependTo("#log");
        $("#log").attr("scrollTop", 0);
    }

    $("#txtSearch").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: $("#hidLocalApplicationName").val() + "/GetEstablishments",
                data: "{ 'p_search_text': '" + request.term + "' }",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataFilter: function (data) { return data; },
                success: function (data) {
                    var v_data = $.parseJSON(data.d);
                    response(v_data);
                }
            });
        },
        minLength: 2,
        select: function (event, ui) {
            if (ui.item.type != "establishment") {
                var sText = ui.item.label;
                var iChar = sText.indexOf("(");
                sText = sText.substr(0, iChar - 1);
                //$("#hidSearch").val(sText);
                $("#hidSearch").val(ui.item.id);
                location.href = "javascript:fControlSearch('" + ui.item.type + "','search', '" + $("#ddlEstablishmentTypes :selected").val() + "','" + $("#ddlEstablishmentServices :selected").val() + "')";
            } else {
                location.href = g_establishment_detail_path + "?id_establishment=" + ui.item.id + "&id_visit_type=2";
            }
        }
    });

    $("#txtSearchQuick").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: $("#hidLocalApplicationName").val() + "/GetEstablishments",
                data: "{ 'p_search_text': '" + request.term + "' }",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataFilter: function (data) { return data; },
                success: function (data) {
                    var v_data = $.parseJSON(data.d);
                    response(v_data);
                }
            });
        },
        minLength: 2,
        select: function (event, ui) {
            if (ui.item.type != "establishment") {
                var sText = ui.item.label;
                var iChar = sText.indexOf("(");
                sText = sText.substr(0, iChar - 1);
                //$("#hidSearch").val(sText);
                $("#hidSearch").val(ui.item.id);
                location.href = "javascript:fControlSearch('" + ui.item.type + "','search', '" + $("#ddlEstablishmentTypesQuick :selected").val() + "','" + $("#ddlEstablishmentServicesQuick :selected").val() + "')";
            } else {
                location.href = g_establishment_detail_path + "?id_establishment=" + ui.item.id + "&id_visit_type=2";
            }
        }
    });

    $("#search").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: $("#hidLocalApplicationName").val() + "/GetEstablishments",
                data: "{ 'p_search_text': '" + request.term + "' }",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataFilter: function (data) { return data; },
                success: function (data) {
                    var v_data = $.parseJSON(data.d);
                    response(v_data);
                }
            });
        },
        minLength: 2,
        select: function (event, ui) {
            if (ui.item.type != "establishment") {
                var sText = ui.item.label;
                var iChar = sText.indexOf("(");
                sText = sText.substr(0, iChar - 1);
                //$("#hidSearch").val(sText);
                $("#hidSearch").val(ui.item.id);
                location.href = "javascript:fControlSearch('" + ui.item.type + "','search')";
            } else {
                location.href = g_establishment_detail_path + "?id_establishment=" + ui.item.id + "&id_visit_type=2";
            }
        }
    });
});

function fSearchQuick() {
    if ($("#txtSearchQuick").val() == "Ingrese su búsqueda") {
        $("#txtSearchQuick").val() == ""
    }
    $("#hidSearch").val($("#txtSearchQuick").val());
    
    $.ajax({
        url: $("#hidLocalApplicationName").val() + "/ClearSession",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataFilter: function (data) { return data; },
        success: function (data) {
        },
        error: function (data) {
        }
    });
    
    $("#hidSearchEstType").val($("#ddlEstablishmentTypesQuick :selected").val());
    $("#hidSearchEstService").val($("#ddlEstablishmentServicesQuick :selected").val());
    
    if ($("#hidSearchEstType").val() != "-1" && $("#hidSearchEstType").val() != "") {
        addToTypeCart(true, $("#hidSearchEstType").val());
        $("#hidSearchEstType").val("1&est_type0=" + $("#hidSearchEstType").val());
    }

    if ($("#hidSearchEstService").val() != "-1" && $("#hidSearchEstService").val() != "") {
        addToServiceCart(true, $("#hidSearchEstService").val());
        $("#hidSearchEstService").val("1&est_service0=" + $("#hidSearchEstService").val());
    }
}

function fSearch() {
    if ($("#txtSearch").val() == "Ingrese su búsqueda") {
        $("#txtSearch").val() == ""
    }
    $("#hidSearch").val($("#txtSearch").val());

    $.ajax({
        url: $("#hidLocalApplicationName").val() + "/ClearSession",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataFilter: function (data) { return data; },
        success: function (data) {
        },
        error: function (data) {
        }
    });

    $("#hidSearchEstType").val($("#ddlEstablishmentTypes :selected").val());
    $("#hidSearchEstService").val($("#ddlEstablishmentServices :selected").val());

    if ($("#hidSearchEstType").val() != "-1" && $("#hidSearchEstType").val() != "") {
        addToTypeCart(true, $("#hidSearchEstType").val());
        $("#hidSearchEstType").val("1&est_type0=" + $("#hidSearchEstType").val());
    }

    if ($("#hidSearchEstService").val() != "-1" && $("#hidSearchEstService").val() != "") {
        addToServiceCart(true, $("#hidSearchEstService").val());
        $("#hidSearchEstService").val("1&est_service0=" + $("#hidSearchEstService").val());
    }
}

function fFillHidden(cart, data) {
    $("#hidSearchEst" + cart).val(data);
}

function fSearchAdjustment(cart) {

    $.ajax({
        url: $("#hidLocalApplicationName").val() + "/Get" + cart +"Cart",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataFilter: function (data) { return data; },
        success: function (data) {
            fFillHidden(cart, data.d);
            fSearchEstablishments(0, $("#hidSearch").val(), $("#hidSearchFor").val(), $("#hidSearchType").val(), $("#hidSearchEstType").val(), $("#hidSearchEstService").val());
        },
        error: function (data) {
            alert("Hubo un error.<br />Intente Nuevamente mas tarde<br />Disculpe las molestias ocasionadas");
        }
    });

}

function fProvinceSearch(p_id_province) {
    $("#hidSearch").val(p_id_province);
    fControlSearch('province', 'map');
}
