﻿(function ($) {

    $(document).ready(function () {
        var fr = document.getElementById("tblYouTube");
        $(".hdn").hide();
        $.datepicker.setDefaults($.datepicker.regional["es"]);


        $("#dialog:ui-dialog").dialog("destroy");
        var name = $("#name"),
			email = $("#email"),
			password = $("#password"),
			allFields = $([]).add(name).add(email).add(password),
			tips = $(".validateTips");


        function updateTips(t) {
            tips
				.text(t)
				.addClass("ui-state-highlight");
            setTimeout(function () {
                tips.removeClass("ui-state-highlight", 1500);
            }, 500);
        }

        function checkLength(o, min, max, msg) {
            if (o.val().length > max || o.val().length < min) {
                o.addClass("ui-state-error");
                updateTips(msg);
                return false;
            } else {

                o.removeClass("ui-state-error");
                return true;
            }
        }

        function checkRegexp(o, regexp, msg) {
            if (!(regexp.test(o.val()))) {
                o.addClass("ui-state-error");
                updateTips(msg);
                return false;
            } else {
                o.removeClass("ui-state-error");
                return true;
            }
        }

        function createDialog() {
            var dlg = $("#dialog-form").dialog({
                autoOpen: false,
                title: '<image src="../imagenes/pinamar_logo.ico" width="11px">&nbsp;Pinamar.Com - Consulta a ' + lblDatoNombreEntidad,
                height: 530,
                width: 550,
                resizable: false,
                position: 'center',
                modal: true,
                draggable: false,
                open: function (type, data) {
                    $(this).parent().appendTo("form");
                },
                buttons: [{
                    text: "Enviar",
                    id: "bntEnviar",
                    click: function () {

                        var bValid = true;
                        bValid = bValid && checkLength($("#txtConsultaTarifaNombre"), 3, 50, "Debe completar su nombre.");
                        bValid = bValid && checkRegexp($("#txtConsultaTarifaEmail"), /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "E-Mail no válido.");
                        bValid = bValid && checkLength($("#txtFechaDesde"), 1, 50, "Debe elegir una fecha de CheckIn.");
                        bValid = bValid && checkRegexp($("#txtFechaDesde"), /^(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d$/, "Fecha de CheckIn no válida.");
                        bValid = bValid && checkLength($("#txtFechaHasta"), 1, 50, "Debe elegir una fecha de CheckOut.");
                        bValid = bValid && checkRegexp($("#txtFechaHasta"), /^(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d$/, "Fecha de CheckOut no válida.");


                        if (bValid) {


                            if (fr != null) {
                                if (fr.style.display != 'none') {
                                    fr.style.display = "none";
                                }
                                else {
                                    fr.style.display = "block";
                                }
                            }

                            $('#frmDetalleAlojamientos').submit();
                            dlg.dialog("destroy");
                        }
                    }
                },
                                   {
                                       text: "Cancelar",
                                       click: function () {
                                           $(this).dialog("destroy");
                                           $(this).dialog("close");
                                           if (fr != null) {
                                           if (fr.style.display != 'none') {
                                               fr.style.display = "none";
                                           }
                                           else {
                                               fr.style.display = "block";
                                           } 
                                       }
                                       }
                                   }],
                close: function (event, ui) {
                     $(this).dialog("destroy");
                    if (fr != null) {
                        if (fr.style.display != 'none') {
                            fr.style.display = "none";
                        }
                        else {
                            fr.style.display = "block";
                        }
                    }
                }
            });
        }

        $(".lnkConsultar").click(function (e) {
            // $("#txtfocus").focus();
            //$("#txtConsultaTarifaNombre").focus();

            createDialog();

            var fechaDesde = $(this).parent().parent().parent().find(".hdn")[0].value;
            var fechaHasta = $(this).parent().parent().parent().find(".hdn")[1].value;

            $("#txtfocus").hide();

            $("#txtFechaDesde").datepicker("destroy");
            $("#txtFechaDesde").datepicker({ showOn: "both",
                buttonImage: "images/calendar.gif",
                buttonImageOnly: true, minDate: fechaDesde, maxDate: fechaHasta
            });

            $("#txtFechaHasta").datepicker("destroy");
            $("#txtFechaHasta").datepicker({ showOn: "both",
                buttonImage: "images/calendar.gif",
                buttonImageOnly: true, minDate: fechaDesde, maxDate: fechaHasta
            });

            $("#dialog-form").dialog("open");
            $(".modalForm").each(function () {
                $(this).children().attr("class", "modalForm");
            });
            //$("#txtFechaDesde")[0].value = $(this).parent().parent().parent().find(".hdn")[0].value;
            //$("#txtFechaHasta")[0].value = $(this).parent().parent().parent().find(".hdn")[1].value;
            $("#lblFechaDesde")[0].innerHTML = fechaDesde;
            $("#lblFechaHasta")[0].innerHTML = fechaHasta;
            if ($(this).parent().parent().parent().find(".txtTarifaPinamar")[0].innerHTML != $(this).parent().parent().parent().find(".txtTarifaMostrador")[0].innerHTML) {
                $("#lblTarifaMostradorModal")[0].innerHTML = "<br/><br/>Tarifa Mostrador &nbsp;&nbsp;   " + $(this).parent().parent().parent().find(".txtTarifaMostrador")[0].innerHTML;
            } else { $("#lblTarifaMostradorModal").hide(); }
            $("#lblTarifaPinamarModal")[0].innerHTML = $(this).parent().parent().parent().find(".txtTarifaPinamar")[0].innerHTML;

            $("#hdnTarifaPinamar")[0].value = $(this).parent().parent().parent().find(".txtTarifaPinamar")[0].innerHTML;
            $("#hdnTarifaPinamar").hide();

            $("#hdnTarifaTipo")[0].value = $(this).parent().parent().parent().find(".txtTipo")[0].innerHTML;
            $("#hdnTarifaTipo").hide();
            $("#hdnTarifaPeriodo")[0].value = $(this).parent().parent().parent().find(".txtDatoEstadia")[0].innerHTML;
            $("#hdnTarifaPeriodo").hide();
            $("#hdnTarifaCapacidad")[0].value = $(this).parent().parent().parent().find(".lblDatoCapacidad")[0].innerHTML;
            $("#hdnTarifaCapacidad").hide();


            $("#lblTarifaTipoModal")[0].innerHTML = $(this).parent().parent().parent().find(".txtTipo")[0].innerHTML;
            $("#lblTarifaCapacidadModal")[0].innerHTML = $(this).parent().parent().parent().find(".lblDatoCapacidad")[0].innerHTML;
            $("#lblTarifaPeriodoModal")[0].innerHTML = $(this).parent().parent().parent().find(".txtDatoEstadia")[0].innerHTML;

            /* if ($(this).parent().parent().parent().find(".txtTipo")[0].innerHTML == "Departamento") {
            $("#chkApartamento")[0].checked = true;
            }

            if ($(this).parent().parent().parent().find(".txtTipo")[0].innerHTML == "Habitación") {
            $("#chkHab")[0].checked = true;
            }*/



            if (fr != null) {
                if (fr.style.display != 'none') {
                    fr.style.display = "none";
                }
                else {
                    fr.style.display = "block";
                }
            }

        });
        var lblDatoNombreEntidad = $("#lblDatoNombreEntidad").html();
       
        $("#dialog-form").parent().appendTo(jQuery("form:first"));
    });





})(jQuery);
