﻿//constant
var baseURl = '';
if (document.URL.indexOf('vdevappserver') != -1) {
    baseURl = '/H4U';
}

$(document).ready(function ($) {
    $(".tooltip").tooltip();
    $("#search-tabs").tabs();
    $("#offers-tabs").tabs();

    $('#destinations_vertival_menu').dcAccordion({
        eventType: 'click',
        autoClose: false,
        saveState: true,
        disableLink: true,
        showCount: true,
        speed: 'fast'
    });
    $('.dcjq-count').html('<img src="/Content/css/Home/media/arrow-right.png">');
    $('.dcjq-parent').bind("click", function (event) { moveArrow(event) });

    $('#hotel_checkin').datepicker({ dateFormat: 'dd-mm-yy' });
    $('#flight_checkin').datepicker({ dateFormat: 'dd-mm-yy' });
    $('#sunholiday_checkin').datepicker({ dateFormat: 'dd-mm-yy' });

    $(".tooltip").tooltip({
        bodyHandler: function () {
            var content = '';
            var url = baseURl + '/Pages/AjaxElements/Tooltip.aspx';
            var data = $(this).attr('id');
            data = data.split('-');
            var XMLSource = data[0];
            var sourceID = data[1];

            $.ajax({
                type: 'GET',
                url: url,
                data: 'XMLSource=' + XMLSource + '&sourceID=' + sourceID,
                async: false,
                cache: true,
                success: function (msg) {
                    content = msg;
                },
                error: function (msg) {
                    content = "Sorry, there was an error when loading the offer information.";
                }
            });
            return content;
        },
        showURL: false
    });
});

function moveArrow(event) {
    var imgDown = '<img src="/Content/css/Home/media/arrow-down.png">';
    var imgRight = '<img src="/Content/css/Home/media/arrow-right.png">'
    var arrow = $(event.currentTarget).children('.dcjq-count');
    var currenthtml = $(arrow).html()
    if (currenthtml == imgDown) {
        $(arrow).html(imgRight);
    }
    else {
        $(arrow).html(imgDown);
    }
    return true;
}

function addRoomInfoRow() {
    var rows = $('#hotel_norooms').val();
    for (var i = 2; i <= 4; i++) {
        $('#room_info_' + i).hide();
    }
    for (var i = 2; i <= rows; i++) {
        $('#room_info_' + i).show();
    }
}

function updateHotelcheckout(){
    if (($('#hotel_checkin').val() != '') && (isDate($('#hotel_checkin').val()))) {
        var checkin = $('#hotel_checkin').val().toString();
        checkin = checkin.split("-");
        checkin = new Date(checkin[2], checkin[1] - 1, checkin[0]);
        var checkoutUTC = checkin.setDate(checkin.getDate() + parseInt($('#hotel_nonights').val()));
        var checkout = new Date(checkoutUTC).toDateString();
        $('#hotel_checkout').html(checkout);
    }
}

function updateFlightcheckout() {
    if (($('#flight_checkin').val() != '') && (isDate($('#flight_checkin').val()))) {
        var checkin = $('#flight_checkin').val().toString();
        checkin = checkin.split("-");
        checkin = new Date(checkin[2], checkin[1] - 1, checkin[0]);
        var checkoutUTC = checkin.setDate(checkin.getDate() + parseInt($('#flight_nonights').val()));
        var checkout = new Date(checkoutUTC).toDateString();
        $('#flight_checkout').html(checkout);
        $('#ReturnDate').val(checkout);
    }
}

function updateSunHolidaycheckout() {
    if (($('#sunholiday_checkin').val() != '') && (isDate($('#sunholiday_checkin').val()))) {
        var checkin = $('#sunholiday_checkin').val().toString();
        checkin = checkin.split("-");
        checkin = new Date(checkin[2], checkin[1] - 1, checkin[0]);
        var checkoutUTC = checkin.setDate(checkin.getDate() + parseInt($('#sunholiday_nonights').val()));
        var checkout = new Date(checkoutUTC).toDateString();
        $('#sunholiday_checkout').html(checkout);
    }
}

function validateHotelSearchForm() {
    updateHotelcheckout();
    var error = '';
    var success = false;
    if ($('#hotel_destination').val() == '')
    {
        error += "-You must fill the destination field.\n\r";
    }
    if ($('#hotel_checkin').val() == '') {
        error += "-You must fill the check-in field.\n\r";
    }
    else {
        if (!isDate($('#hotel_checkin').val())) {
            error += "-Check-in must be a valid date.\n\r";
        }
    }
    if (error.length > 0) {
        success = false;
        alert(error);
    }
    else {
        success = true;
    }
    return success;
}

function validateFlightSearchForm() {
    updateFlightcheckout();
    var error = '';
    var success = false;
    if ($('#flight_checkin').val() == '') {
        error += "-You must fill the check-in field.\n\r";
    }
    else {
        if (!isDate($('#flight_checkin').val())) {
            error += "-Check-in must be a valid date.\n\r";
        }
    }
    if (error.length > 0) {
        success = false;
        alert(error);
    }
    else {
        success = true;
    }
    return success;
    return false;
}

function validateSunHolidaySearchForm() {
    var error = '';
    var success = false;
    if (($('#sunholiday_departure_city').val() == 'any') || ($('#sunholiday_departure_city').val() == '---')) {
        error += "-You must fill the airport filed.\n\r";
    }
    if (($('#sunholiday_arrival_country').val() == 'any') || ($('#sunholiday_arrival_country').val() == '---')) {
        error += "-You must fill the arrival country field.\n\r";
    }
    if (($('#sunholiday_arrival_region').val() == 'any') || ($('#sunholiday_arrival_country').val() == '---')) {
        error += "-You must fill the region field.\n\r";
    }
    if (($('#sunholiday_arrival_resort').val() == 'any') || ($('#sunholiday_arrival_country').val() == '---')) {
        error += "-You must fill the resort field.\n\r";
    }
    if ($('#sunholiday_checkin').val() == '') {
        error += "-You must fill the check-in field.\n\r";
    }
    else {
        if (!isDate($('#sunholiday_checkin').val())) {
            error += "-Check-in must be a valid date.\n\r";
        }
    }
    if (error.length > 0) {
        success = false;
        alert(error);
    }
    else {
        success = true;
    }
    return success;
    return false;
    return false;
}

function isDate(dateStr) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat);
    var returndate = '01/05/2003';
    if (dateStr.length != 10) {
        return false;
    }
    if (matchArray == null) {
        return false;
    }
    return true; // date is valid
}

function loadSunHolidayDestinationList(id,parentListSelectedElement) {
    var url = baseURl + '/Pages/AjaxElements/DestinationList.aspx';
    switch(id)
    {
        case 'sunholiday_departure_city':
            var SelectedValue = $('#sunholiday_departure_city').val();
            var selectedElement = '';
            $('#sunholiday_arrival_country').html('<option value="any" selected="selected">Select an airport</option>');
            $('#sunholiday_arrival_region').html('<option value="---" selected="selected">Select a country</option>');
            $('#sunholiday_arrival_resort').html('<option value="---" selected="selected">Select a region</option>');
            break;
        case 'sunholiday_arrival_country':
            var SelectedValue = $('#sunholiday_departure_city').val();
            var selectedElement = "&selectedElement=" + $('#sunholiday_arrival_country').val();
            $('#sunholiday_arrival_region').html('<option value="---" selected="selected">Select a country</option>');
            $('#sunholiday_arrival_resort').html('<option value="---" selected="selected">Select a region</option>');
            break;
        case 'sunholiday_arrival_region':
            var SelectedValue = $('#sunholiday_departure_city').val();
            var selectedElement = "&selectedElement=" + $('#sunholiday_arrival_region').val();
            $('#sunholiday_arrival_resort').html('<option value="---" selected="selected">Select a country</option>');
            break;
    }
    if ((SelectedValue != 'any') && (SelectedValue != '---')) {
        $.ajax({
            type: 'GET',
            url: url,
            data: 'SelectedValue=' + SelectedValue + "&selectedList=" + id + selectedElement,
            async: false,
            cache: true,
            success: function (msg) {
                switch (id) {
                    case 'sunholiday_departure_city':
                        $('#sunholiday_arrival_country').html(msg);
                        break;
                    case 'sunholiday_arrival_country':
                        $('#sunholiday_arrival_region').html(msg);
                        break;
                    case 'sunholiday_arrival_region':
                        $('#sunholiday_arrival_resort').html(msg);
                        break;
                }
            },
            error: function (msg) {
                content = "Sorry, there was an error when loading the offer information.";
            }
        });
    }
}

$(function () {
    $.ajax({
        url: baseURl + "/Pages/AjaxElements/DestinationAutocomplete.xml",
        dataType: "xml",
        success: function (xmlResponse) {
            var data = $("destination", xmlResponse).map(function () {
                return {
                    value: $(this).text()
                };
            }).get();
            $("#hotel_destination").autocomplete({
                source: data,
                minLength: 0,
                appendTo: '#hotel-destintion-autocomplete-container'
            });
        }
    });

});

function validateUnSubscriptForm() {
    var success = false;
    var errorMsg = "";
    if (($('#email').val() != "") || ($('#mobile').val() != "")) {
        if ($('#email').val() != "") {
            if (isNotValidEmail($('#email').val())) {
                errorMsg += "-You must insert a valid email.\n\r";
            }
        }
        if ($('#mobile').val() != "") {
            if (isNotValidMobile($('#mobile').val())) {
                errorMsg += "-You must insert a valid mobile number.\n\r";
            }
        }
    }
    else {
        errorMsg += "-You must fill the phone number or the email field.\n\r";
    }
    if (errorMsg.length > 0) {
        alert(errorMsg);
    }
    else {
        success = true;
    }
    return success;
}

function validateSubscriptForm() {
    var success = false;
    var errorMsg = "";
    if ($('#fname').val() == "") {
        errorMsg += "-You must fill the name field.\n\r";
    }
    if ($('#lname').val() == "") {
        errorMsg += "-You must fill the last name field.\n\r";
    }
    if ($('#chkBoth:checked').val() == '0') {
        if ($('#email').val() == "") {
            errorMsg += "-You must fill the email field.\n\r";
        }
        else {
            if (isNotValidEmail($('#email').val())) {
                errorMsg += "-You must insert a valid email.\n\r";
            }
        }
        if ($('#mobile').val() == "") {
            errorMsg += "-You must fill the mobile field.\n\r";
        }
        else {
            if (isNotValidMobile($('#mobile').val())) {
                errorMsg += "-You must insert a valid mobile number.\n\r";
            }
        }
    }
    else {
        if ($('#chkEmail:checked').val() == '0') {
            if ($('#email').val() == "") {
                errorMsg += "-You must fill the email field.\n\r";
            }
            else {
                if (isNotValidEmail($('#email').val())) {
                    errorMsg += "-You must insert a valid email.\n\r";
                }
            }
        }
        else {
            if ($('#chkMobile:checked').val() == '0') {
                if ($('#mobile').val() == "") {
                    errorMsg += "-You must fill the mobile field.\n\r";
                }
                else {
                    if (isNotValidMobile($('#mobile').val())) {
                        errorMsg += "-You must insert a valid mobile number.\n\r";
                    }
                }
            }
        }
        if (($('#chkMobile:checked').val() != '0') && ($('#chkEmail:checked').val() != '0')) {
            errorMsg += "-You must select email, mobile or both.\n\r";
        }
    }
    if (errorMsg.length > 0) {
        alert(errorMsg);
    }
    else {
        success = true;
    }
    return success;
}

function isNotValidEmail(email) {
    var success = false;
    var supported = 0;
    if (window.RegExp) {
        var tempStr = "a";
        var tempReg = new RegExp(tempStr);
        if (tempReg.test(tempStr)) supported = 1;
    }
    if (!supported)
        return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
    var validation = (!r1.test(email) && r2.test(email));
    if (validation == false) {
        success = true;
    }
    return success;
}

function isNotValidMobile(email) {
    var success = false;
    if ((isNaN(email)) || (email.length < 9)) {
        success = true;
    }
    return success;
}

