﻿function ShowFlash(strFileName, strXml, StrWidth, strHeight) {
//    document.write('<object width="' + StrWidth + '" height="' + strHeight + '" title="Ha Long bay">');
//    document.write('  <param name="movie" value="banner.swf" />');
//    document.write('  <param name="quality" value="high" />');
//    document.write('  <param name="wmode" value="transparent" />');
//    document.write('  <param name="flashvars" value="xmlpath=' + strXml + '" />');
//    document.write('  <embed src="' + strFileName + '" flashvars="xmlpath=' + strXml + '" wmode="transparent" quality="high" type="application/x-shockwave-flash" width="' + StrWidth + '" height="' + strHeight + '" ></embed></object>');
    document.write('<embed src="' + strFileName + '" flashvars="xmlpath=' + strXml + '" wmode="transparent" quality="high" type="application/x-shockwave-flash" width="' + StrWidth + '" height="' + strHeight + '" ></embed>');
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

function SubmitBooking() {
    $("#BookingFormContent").mask("Waiting...");
    var errView = 'Required';
    var flag = true;

    var ddlGender = $("#ddlGender :selected").text();
    var txtYourName = $('#txtYourName').val();
    var txtTelephone = $('#txtTelephone').val();
    var txtBEmail = $('#txtBEmail').val();
    var txtBConfirmEmail = $('#txtBConfirmEmail').val();    
    var txtCruises = $('#txtCruises').val();
    var txtCruisesLink = $('#txtCruisesLink').val();
    var txtPrograms = $("#ddlProgram :selected").text();
    var txtDepartureDate = $("#ddlDepartureMonth :selected").text() + ' - ' + $("#ddlDepartureDay :selected").text() + ' - ' + $("#ddlDepartureYear :selected").text();
    var ddlAdults = $("#ddlAdults :selected").text();
    var ddlChildren = $('#ddlChildren').val();
    var txtDetailed = $('#txtDetailed').val();

    var errYourName = $('#errYourName');
    var errTelephone = $('#errTelephone');
    var errBEmail = $('#errBEmail');

    errYourName.html('');
    errTelephone.html('');
    errBEmail.html('');

    if (txtYourName == '') {
        flag = false;
        errYourName.html(errView);
    }

    if (txtBEmail == '') {
        flag = false;
        errBEmail.html(errView);
    }

    if (isValidEmailAddress(txtBEmail) == false) {
        flag = false;
        errBEmail.html('Error');
    }

    if (flag == true) {
        var list = [ddlGender, txtYourName, txtTelephone, txtBEmail, txtBConfirmEmail, txtCruises, txtCruisesLink, txtPrograms, txtDepartureDate, ddlAdults, ddlChildren, txtDetailed];
        var jsonText = JSON.stringify({ list: list });
        $.ajax({
            type: "POST",
            url: "/Default.aspx/SaveBooking",
            data: jsonText,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                $("#BookingFormContent").unmask(); // Bỏ loading
                $("#BookingFormContent").hide(); // Ẩn form
                if (response.d == "error") {
                    $('#BookingFormContent').load('/Page/ErrorPage.htm', function () { $('#BookingFormContent').fadeIn('slow'); });
                } else {
                    $('#BookingFormContent').load('/Page/BookingComplete.htm', function () { $('#BookingFormContent').fadeIn('slow'); });
                }
            },
            failure: function (response) {
                alert(response);
            }
        });
    }
    else {
        $("#BookingFormContent").unmask(); // Bỏ loading
    }
    return;
}
function SubmitContact() {
    $("#BodyContactForm").mask("Waiting...");

    var errView = 'Required';
    var flag = true;

    var txtFullName = $('#txtFullName');
    var txtEmail = $('#txtEmail');
    var txtContent = $('#txtContent');

    var errFullName = $('#errFullName');
    var errEmail = $('#errEmail');
    var errContent = $('#errContent');

    errFullName.html('');
    errEmail.html('');
    errContent.html('');

    if (txtFullName.val() == '') {
        flag = false;
        errFullName.html(errView);
    }

    if (txtEmail.val() == '') {
        flag = false;
        errEmail.html(errView);
    }

    if (isValidEmailAddress(txtEmail.val()) == false) {
        flag = false;
        $('#errEmail').html('Error');
    }

    if (txtContent.val() == '') {
        flag = false;
        errContent.html(errView);
    }

    if (flag == true) {
        //$('#BodyContactForm').load('/Page/Loading.htm');
        $.ajax({
            type: "POST",
            url: "/Default.aspx/SaveContact",
            data: '{txtFullName: "' + txtFullName.val() + '", "txtEmail": "' + txtEmail.val() + '", "txtContent": "' + txtContent.val() + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            //cache: false,
            //async: false,
            success: function (response) {
                $("#BodyContactForm").unmask(); // Bỏ loading
                $("#BodyContactDetailForm").hide(); // Ẩn form
                if (response.d == "error") {
                    $('#BodyContactForm').load('/Page/ErrorPage.htm', function () { $('#BodyContactForm').fadeIn('slow'); });
                } else {
                    $('#BodyContactForm').load('/Page/ContactComplete.htm', function () { $('#BodyContactForm').fadeIn('slow'); });
                }
            },
            failure: function (response) {
                alert(response);
            }
        });


    }
    else {
        $("#BodyContactForm").unmask(); // Bỏ loading
    }
    return;
}

function ResetContact() {
    var errFullName = $('#errFullName');
    var errEmail = $('#errEmail');
    var errContent = $('#errContent');

    errFullName.html('');
    errEmail.html('');
    errContent.html('');
}

function LoadAjaxBookingForm(idCruise, urlCruise) {
    if ($("#content_Booking")) {
        $.get("/Page/BookingForm.aspx", {
            id: idCruise,
            url: urlCruise
        }, function (data) {
            $("#content_Booking").html(CutString(data));
        });
    }
}

function CutString(str) {
    if (str.length > 0) {
        var i = str.indexOf('<div id="BookingForm">');
        var t = str.indexOf("</form>");
        if (i > 0 && t > i)
            str = str.substring(i, t);
    }
    return str;
}

function LoadIndexTabs() {
    if (document.getElementById('tabbed_box_1') != null) {
        var url = window.location.href;
        var c_url = url.split('#');
        if (c_url.length > 1) {
            var id = c_url[1];
            if (document.getElementById(id) != null) {
                $(".content").hide();
                $("#" + id).fadeIn("slow");
            }

            $('.tabs li').each(function (idx) {
                var aItem = $(this).find("a").first();
                aItem.removeClass("active");
                var content_show = aItem.attr("title");
                if (id == content_show)
                    aItem.addClass("active");
            });
        }
    }
}

function btnBookingOnClick(idItem) {
    $("#ddlProgram").val(idItem);
    $(".content").hide();
    $("#content_Booking").fadeIn("slow");

    $('.tabs li').each(function (idx) {
        var aItem = $(this).find("a").first();
        aItem.removeClass("active");
        var content_show = aItem.attr("title");
        if ("content_Booking" == content_show)
            aItem.addClass("active");
    });
}

function LoadWindowMode() {
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

        //transition effect		
        $('#mask').fadeIn(1000);
        $('#mask').fadeTo("slow", 0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
        var yScroll = 0;
        if (self.pageYOffset) {
            yScroll = self.pageYOffset;
        }
        //Set the popup window to center
        $(id).css('top', winH / 2 - $(id).height() / 2 + yScroll);
        $(id).css('left', winW / 2 - $(id).width() / 2);

        //transition effect
        $(id).fadeIn(2000);
    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask').hide();
        $('.window').hide();
    });

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });
}
$(document).ready(function () {
    $(".lightbox").lightbox();

    if ($("#hTestimonialsBox")) {
        $("#hTestimonialsBox").vTicker({
            speed: 1000,
            pause: 4000,
            animation: 'fade',
            mousePause: false,
            showItems: 3
        });
    }

    LoadWindowMode();
    //LoadAjaxBookingForm();
    LoadIndexTabs();
    if ($("a.tab")) {
        $("a.tab").click(function () {
            $(".active").removeClass("active");
            $(this).addClass("active");
            $(".content").hide();
            var content_show = $(this).attr("title");
            $("#" + content_show).fadeIn("slow");
            return false;
        });
        return false;
    }
});
