﻿function makereturn(f) {
    return f;
}
function isDate(dateStr) {
    //DD/MM/YYYY
    var datePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2}|\d{4})$/;
    var matchArray = dateStr.match(datePattern);
    //Check valid format
    if (matchArray == null) { return false; }
    month = matchArray[3];
    day = matchArray[1];
    year = matchArray[5];
    // check month range
    if (month < 1 || month > 12) { return false; }
    //Check day range
    if (day < 1 || day > 31) { return false; }
    //Check months with 30 days
    if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) { return false; }
    //Check Feb days
    if (month == 2) {
        var leapYr = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day > 28 && !leapYr)) { return false; }
    }
    return true;
}
/* this is custom js - jQ calling file */
$(document).ready(function () {
    window.onload = function () {

        //--- add js here
        $('#menuhozirontal .sf-menu li').hover(function () {
            $(this).addClass('hover');
        }, function () {
            $(this).removeClass('hover');
        });
        $('#menuhozirontal .sf-menu li').click(function () {
            window.location = $(this).find('a').attr('href');
        });
        //Seephoto click
        $('.seeP').colorbox({
            transition: 'fade',
            speed: 500,
            iframe: true,
            width: 790,
            height: 550,
            onComplete: function () {
                if ($('.cboxIE') != null) {
                    $('#cboxContent').height(530);
                    $('#cboxLoadedContent').height(530);
                    $('#colorbox').height(530);
                    $('#cboxWrapper').height(565);
                }
            }
        });
        // --- Optional Menu Bottom
        function optMenu() {
            var $loc = $('#optionalMenu .htmltext');
            var $sou = $('#menuhozirontal .sf-menu');
            $loc.hide();
            $loc.append('<ul class="listTree">' + $sou.html() + '</ul>').find("li:eq(0)").remove();
            $loc.find('ul[style]').css({ "display": "block", "visibility": "visible" });
            var $li = $("li#1103");
            var $li2 = $("li#1203");
            $("li#1102", $loc).add("li#1103", $loc).remove(); //.add("li#1109", $loc)
            $("li#1212", $loc).add("li#1203", $loc).remove(); //.add("li#1213", $loc)
            $("li#1104", $loc).after("<div class='nl'></div><li class='lv1' id='1103'>" + $li.html() + "</li>");
            $("li#1204", $loc).after("<div class='nl'></div><li class='lv1' id='1203'>" + $li2.html() + "</li>");
            //Add class
            $("li#1099", $loc).add("li#1105", $loc).add("li#1184", $loc).add("li#1208", $loc).addClass("ws");
            $("li#1100", $loc).add("li#1106", $loc).addClass("wl");
            $loc.show();
        };
        optMenu();
        // Article
        if ($('.brl').length > 0) {
            $('.listArticlesitem').each(function () {
                $(this).find('.brl').height($(this).find('.describe').height() - 10);
            });
        }
        //Get price product
        function setPrice() {
            $('.price').each(function () {
                var p = parseInt($(this).find('.price2').text(), 10);
                if (p == 0) {
                    $(this).remove();
                }
            });
            var dt = new Date();
            var url2 = "/WebPart/Booking/getdataBookingDeals.ashx?GetAllPrice=''&t=" + dt.getTime() + "";
            $.getJSON(url2, function (data) {
                $.each(data, function (i, item) {
                    $('.duration').each(function () {
                        if ($(this).attr('rel') == item.Id_Product) {
                            if (parseInt(item.Duration, 10) > 0 && parseInt(item.Duration, 10) < 10) {
                                $(this).prepend("0" + item.Duration);
                            } else if (parseInt(item.Duration, 10) > 10) {
                                $(this).prepend(item.Duration);
                            } else {
                                $(this).prepend("0");
                            }
                        }
                    });
                    $('.currentprice').each(function () {
                        if ($(this).attr('rel') == item.Id_Product) {
                            if (parseInt(item.Cost, 10) > 0) {
                                $(this).find('.price2').text(item.Cost);
                            } else {
                                $(this).remove();
                            }
                        }
                    });
                });
                function setPadDetail() {
                    $('.wrapunit').each(function () {
                        var h = $(this).height();
                        var temp = 73;
                        var c = $(this).find('.currentprice').height();
                        if (c == null) {
                            temp = 53;
                            c = 0;
                        }
                        var p = $(this).find('.price').height();
                        //alert(h - c - p - temp);
                        $(this).find('.detail').css('margin-top', h - c - p - temp);
                    });
                }
                setPadDetail();
            });
        }
        if ($('.price').length > 0) {
            setPrice();
        }
        function checkBookingContact() {
            var title = getQuerystring('webparttitle');
            title = decodeURI(title);
            if (title != '') {
                $('.contactus h3').text(title).css('padding-bottom', 0);
                $('.contactus .webpartbodycontrol').hide();
                $('#menuhozirontal').find('.active').removeClass('active');
                $('#nav_bottom').find('.active').removeClass('active');
                $('.colright').each(function () {
                    if ($(this).find('input').val() == "Contact Us") {
                        $(this).find('input').val(title);
                    }
                });
            }
            //});
        }
        //Lien he
        var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
        function sendMailClick() {
            $(".btnsendmail").live('click', function () {
                var check = true;
                var name = $('.requiredName').val();
                var mail = $('.requiredEmail').val();
                var slrequired = $('select.required').last().get(0).selectedIndex;
                if (name == '') {
                    alert(fillyourname);
                    return false;
                }
                if (mail == '') {
                    alert(fillemail);
                    return false;
                }
                if (!filter.test(mail)) {
                    alert(fillvalidemail);
                    //Please re-enter your e-mail address
                    mail.focus
                    return false;
                }
                if (slrequired == 0) {
                    alert(firsttimeknow);
                    return false;
                }
                var Code = $('#codetext').val();

                if (Code == '') {
                    alert(codeshow);
                    return false;
                }
                $.ajax({
                    url: "/CaptChaImage.ashx?chkcaptcha=true&code=" + Code + "",
                    async: false,
                    dataType: "text",
                    success: function (data) {
                        if (data == 'true') {
                            makereturn(data);
                        } else { check = false; }
                    }
                });
                if (check == true) return true;
                else {
                    alert(codeshow);
                    return false;
                }
            });
        }
        if ($('#contactus').length > 0) {
            $('.btnloadformbuilder').find('input').eq(1).remove();
            sendMailClick();
            checkBookingContact();
            $('.loadformbuilder li:eq(0) select').clone().appendTo($(".loadformbuilder li:eq(1)"));
            $(".loadformbuilder li:eq(0)").remove();
            $(".loadformbuilder select:eq(0)").css({ "margin-top": 10, "margin-left": 10 });
        }
        //* booking *//
        function clickBooking() {
            var wid = $('#relatetour2').width() + 40;
            var hei = $('#relatetour2').height() + 130;
            $('.icondate').live('click', function () {
                $('.hasDatepicker').focus();
            });
            $('.btnCheckRate input:eq(0)').live('click', function () {
                $('.divUpdatePanel').show();
                var $temp = 0;
                var A = setInterval(function () {
                    if ($('#container_In_Ex').length > 0) {
                        if ($('#container_In_Ex').find('.inclu').height() > $('#container_In_Ex').find('.exclu').height()) {
                            $temp = $('#container_In_Ex').find('.inclu').height();
                        } else {
                            $temp = $('#container_In_Ex').find('.exclu').height();
                        }
                        $('#container_In_Ex').children('div').each(function () {
                            $(this).find('li:eq(0)').css('padding-top', 0);
                        });
                        if ($temp > 0) {
                            $('#container_In_Ex').find('.inclu').height($temp);
                            $('#container_In_Ex').find('.exclu').height($temp);
                            clearInterval(A);
                        }
                    }
                }, 500);
            });
            var pos = $('#wrapContent #ininerary').index();
            var pos2 = $('#wrapContent #rateandavailability').index();
            function hideBooking(id) {
                $('#wrapContent').children().hide();
                $('#' + id).show();
            };
            //Display Tab and Content
            function displayBooking(id, pos) {
                $('#tabbooking li').removeClass('active');
                $('#tabbooking li').eq(pos).addClass('active');
                hideBooking(id);
            };
            //Create click handle
            function tabBooking(id, pos) {
                $('#tabbooking li').eq(pos).click(function () {
                    $('#tabbooking li').removeClass('active');
                    $(this).addClass('active');
                    if (pos == 0) {
                        $('#bookingRight').show();
                    } else {
                        $('#bookingRight').hide();
                    }
                    hideBooking(id);
                });
                $('#tabbooking li').eq(pos).find('div').hover(function () {
                    $(this).addClass("hover");
                }, function () {
                    $(this).removeClass("hover");
                });
            };
            //ininerary
            tabBooking('ininerary', pos);
            tabBooking('rateandavailability', pos2);
            $('#bookingControl div a:eq(2)').click(function () {
                pos = $('#wrapContent #rateandavailability').index();
                displayBooking('rateandavailability', pos);
                tabBooking('rateandavailability', pos);
                $('#bookingRight').hide();
            });
            //rateandavailability
            $('.btbook div input:eq(0)').live('click', function () {
                pos = $('#wrapContent #ininerary').index();
                hideBooking('ininerary', pos);
                displayBooking('ininerary', pos);
                $('#bookingRight').show();
                return false;
            });
            $(".btbook div input:eq(2)").live('click', function () {
                //Yes or no
                var blnYN = $('.rdlb input:eq(0)').is(':checked');
                //allow transfer by
                var blnATran = false;
                if ($('.servicecar') != null)
                    blnATran = $('.servicecar').is(':checked');
                //Choose bus
                var blnBus = true;
                if ($('.servicebus') != null)
                    blnBus = $('.servicebus').is(':checked');
                //Agree online booking
                var blnCheck = $('#rblSelect_Agree').is(':checked');
                if (blnATran == true && blnBus == false && $('.picknote:eq(0) input:eq(0)').val() == '' && blnYN) {
                    alert("Please fill in your address in Hanoi for pick-up/drop-off.");
                    return false;
                }
                if (blnCheck == false) {
                    alert('Please confirm that you have read and agreed with our Online Booking Conditions.');
                    return false;
                } else {
                    pos = $('#wrapContent #guestinfo').index();
                    displayBooking('guestinfo', pos);
                    tabBooking('guestinfo', pos);
                }
                $('#bookingRight').hide();
            });
            //Yes no Booking
            //Yes
            $('#checkrate2 .rdlb input:eq(0)').live('click', function () {
                yesCheck();
            });

            $('#checkrate2 .rdlb input:eq(1)').live('click', function () {
                noCheck();
            });
            $('.servicebus').live('click', function () {
                $('.picknote:eq(0)').hide();
                $('.picknote:eq(1)').show();
            });
            $('.servicecar').live('click', function () {
                $('.picknote:eq(0)').show();
                $('.picknote:eq(1)').hide();
            });
            //guestinfo
            $('#guestinfo .book input:eq(0)').live('click', function () {
                pos = $('#wrapContent #rateandavailability').index();
                hideBooking('rateandavailability', pos);
                displayBooking('rateandavailability', pos);
                return false;
            });
            $('#guestinfo .book input:eq(1)').live('click', function () {
                var gName = $('#guestinfo .txtGuestName').val();
                var email = $('#guestinfo .txtEmail').val();
                var repEmail = $('#guestinfo .txtVerify').val();
                //check email valid
                if (gName == null || gName == '') {
                    alert("Please fill in your full name.");
                    gName.focus;
                    return false;
                }
                if (email == null || email == '') {
                    alert("Please fill in your e-mail address.");
                    return false;
                } else {
                    if (!filter.test(email)) {
                        alert('Please provide a valid email address.');
                        email.focus
                        return false;
                    }
                }
                if (repEmail != email) {
                    alert("Please check your verified email address!");
                    repEmail.focus;
                    return false;
                }
                var ret = true;
                $('select').each(function () {
                    if ($(this).attr('name') == "ddlHowKnown") {
                        if ($(this).get(0).selectedIndex == 0) {
                            alert("Please fill how did you first know about Bhaya Cruises");
                            ret = false;
                        }
                    }
                });
                $('.litxtDateofbirth').each(function () {
                    var dateinput = $(this).find('input').val();
                    if (dateinput.length > 0 && !isDate(dateinput)) {
                        alert("Please fill valid date of birth of guest detail.");
                        dateinput.focus;
                        ret = false;
                    }
                });
                $('.litxtVisa').each(function () {
                    var dateinput = $(this).find('input').val();
                    if (dateinput.length > 0 && !isDate(dateinput)) {
                        alert("Please fill valid visa expire date of guest detail.");
                        dateinput.focus;
                        ret = false;
                    }
                });
                if (ret) {
                    pos = $('#wrapContent #payment').index();
                    displayBooking('payment', pos);
                    tabBooking('payment', 3);
                } else {
                    return false;
                }
            });
            //payment
            $('#payment .book input:eq(0)').live('click', function () {
                pos = $('#wrapContent #guestinfo').index();
                hideBooking('guestinfo', pos);
                displayBooking('guestinfo', pos);
                return false;
            });
            //Online Booking Condition Click
            if ($('#relatetour2').length > 0) {
                $('a.bookdingContition').live('mousedown', function () {
                    $(this).colorbox({
                        transition: 'fade',
                        speed: 500,
                        inline: true,
                        href: "#relatetour2",
                        width: wid,
                        height: hei,
                        onLoad: function () {
                            $('#relatetour2').prepend("<div id='bookingcondition' style='font-weight:bold;font-size:14px;'>Booking Conditions</div>");
                            $('#relatetour2').css({ "width": 600 });
                            $('#cboxContent').css({ "background-color": "#00795F", "padding": 0, "margin": 0, "right": 25 }).parent().attr('id', "Content");
                        },
                        onComplete: function () {
                            $('#cboxLoadedContent').css({ "background": "none" });
                            $('.specialFuture').css({ "width": 570 });
                            $('#cboxClose').css({ 'background': 'none', 'text-indent': 0, 'font-size': 23, 'text-align': 'center' }).text("X");
                        },
                        onClosed: function () {
                            $('#bookingcondition').remove();
                        }
                    });
                });
            }
        }
        if ($('#wrapContent').length > 0) {
            clickBooking();
        }
        if ($('#productgallery').length > 0) {
            $('#productgallery ul:eq(0)').children().last().addClass('last');
        }
        //        if ($('.documentlist').length > 0) {
        //            $('.documentlist ul li:odd').addClass('right');
        //            if ($('.documentlist ul li').length > 0) {
        //                for (var a = 0; a < ($('.documentlist ul li').length + 1) % 2; a++) {
        //                    var hl = $(a * $('.documentlist ul li').length / 2).height();
        //                    var hr = $((a * ($('.documentlist ul li').length / 2)) + 1).height();
        //                    alert(hl);
        //                    alert(hr);
        //                }
        //            }
        //        }
    }
    function checkThank() {
        var email = getQuerystring('email');
        if (email != '') {
            $('#mailto').attr('href', "mailto:" + email).text(email);
        }
    };
    if ($('#thankyou').length > 0) {
        checkThank();
    }
});
function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
function yesCheck() {
    $('#checkrate2 .rdlb input:eq(0):checked');
    $('#container_Trans').show();
    $('.picknote:eq(0)').hide();
    $('.picknote:eq(1)').show();
    if ($('.servicecar') && $('.servicecar').is(':checked')) {
        $('.picknote:eq(0)').show();
        $('.picknote:eq(1)').hide();
    }
    if ($('.servicebus') && $('.servicebus').is(':checked')) {
        $('.picknote:eq(0)').hide();
        $('.picknote:eq(1)').show();
    }
}
function noCheck() {
    $('#checkrate2 .rdlb input:eq(1):checked');
    $('#container_Trans').hide();
    $('.picknote:eq(0)').hide();
    $('.picknote:eq(1)').hide();
}
