﻿var bookingEnginePath = '';
var productCodeId = '';
var arrDateLabel = '';
var depDateLabel = '';
var showUnits;
var showAdults;
var showChildren;
var showOther;
var currentOptGrp = '';
var currentOpt = '';

function addFormField(form, fieldName, fieldValue) {
    var inputField = document.createElement("input");
    inputField.setAttribute("name", fieldName);
    inputField.setAttribute("id", fieldName);
    inputField.setAttribute("type", "hidden");
    inputField.setAttribute("value", fieldValue);
    form.appendChild(inputField);
}

$(document).ready(function() {
    //    $.ajax({
    //        url: configurationFileUrl,
    //        success: function(data) {
    //            buildForm(data);
    //        }
    //    });
    $('#btnGo').attr('src', btnGoSrc);
    $("#arrivalDate1").datepicker({
        minDate: '0',
        showOn: 'focus',
        onSelect: function(dateText, inst) { setMinDepartureDate(dateText, inst); }
    });
    $("#arrivalDate1").change(function() { setMinDepartureDate($("#arrivalDate1").val(), $("#arrivalDate1")); });
    $("#arrivalDate2").datepicker({
        minDate: '0',
        showOn: 'focus',
        onSelect: function(dateText, inst) { setMinDepartureDate(dateText, inst); }
    });
    $("#arrivalDate2").change(function() { alert('change'); setMinDepartureDate($("#arrivalDate2").val(), $("#arrivalDate2")); });

    $("#departureDate1").datepicker({
        minDate: '0',
        showOn: 'focus',
        onSelect: function(dateText, inst) { setMaxArrivalDate(dateText, inst); }
    });
    $("#departureDate1").change(function() { setMaxArrivalDate($("#departureDate1").val(), $("#departureDate1")); });
    $("#departureDate2").datepicker({
        minDate: '0',
        showOn: 'focus',
        onSelect: function(dateText, inst) { setMaxArrivalDate(dateText, inst); }
    });
    $("#departureDate2").change(function() { setMaxArrivalDate($("#departureDate2").val(), $("#departureDate2")); });


    $('#arrivalDate1').focus(function() {
        if (this.value == arrDateLabel) {
            this.value = '';
        }
    }).blur(function() {
        if (this.value == '') {
            this.value = arrDateLabel;
        }
    });
    $('#arrivalDate2').focus(function() {
        if (this.value == arrDateLabel) {
            this.value = '';
        }
    }).blur(function() {
        if (this.value == '') {
            this.value = arrDateLabel;
        }
    });

    $('#departureDate1').focus(function() {
        if (this.value == depDateLabel) {
            this.value = '';
        }
    }).blur(function() {
        if (this.value == '') {
            this.value = depDateLabel;
        }
    });
    $('#departureDate2').focus(function() {
        if (this.value == depDateLabel) {
            this.value = '';
        }
    }).blur(function() {
        if (this.value == '') {
            this.value = depDateLabel;
        }
    });

    $('#submitBook1').click(function() {
        return goBook("1");
    });
    $('#submitBook2').click(function() {
        return goBook("2");
    });


    /* selectmenu custome dropdowns */
    $("[id$='ddlHotelsInterior']").selectmenu({
        style: 'dropdown',
        width: 259
    });
    $("[id$='ddlHotelsTop']").selectmenu({
        style: 'dropdown',
        width: 259
    });
    /*
    $('#bookingRooms').selectmenu({
    style: 'dropdown',
    width: 59
    });
    $('#bookingAdults').selectmenu({
    style: 'dropdown',
    width: 59
    });
    $('#bookingRoomsHead').selectmenu({
    style: 'dropdown',
    width: 59
    });
    $('#bookingAdultsHead').selectmenu({
    style: 'dropdown',
    width: 59
    });
    */

    /*$("#arrivalDate1").click(function() {
        alert("zach");
    });
    $("#arrivalDate1").focusout(function() {
        alert("focusout");
        
    });
    $("#arrivalDate1").change(function() {
        alert("solomon");
    });*/
});



function setHotelURL() {

}

function setMinDepartureDate(dateText, datepicker) {
	var depDate = $('#departureDate1');
	if (datepicker.id == 'arrivalDate2') {
		depDate = $('#departureDate2');
	}
    var dt = new Date(dateText);
    dt.setDate(dt.getDate() + 1);
    $(depDate).datepicker('option', 'minDate', dt);
    if ($(depDate).val() == '') {
		$(depDate).datepicker('setDate', dt);
	}

}
function setMaxArrivalDate(dateText, datepicker) {
    var dt = new Date(dateText);
    dt.setDate(dt.getDate() - 1);
    $('#arrivalDate').datepicker('option', 'maxDate', dt);
}
function goBook(type) {

    var dtNow = new Date();
    dtNow.setHours(0, 0, 0, 0);
    

    var oArrive = document.getElementById("arrivalDate" + type);
    var dtArrive = new Date(oArrive.value);
    var oDepart = document.getElementById("departureDate" + type);
    var dtDepart = new Date(oDepart.value);

        if (oArrive.value == '' || oArrive.value == arrDateLabel || dtArrive < dtNow) {
            alert("Please select a future arrival date.");
            oArrive.focus();
            return false;
        }

        if (oDepart.value == '' || oDepart.value == depDateLabel || dtDepart < dtNow) {
            alert("Please select a future departure date.");
            oDepart.focus();
            return false;
        }

        if (dtArrive > dtDepart) {
            alert("Please select an arrival date that is before the departure date");
            oArrive.focus();
            return false;
        }

        var bookingDDL;
        var bookingURL;
        if (type == 1) {
            bookingDDL = $("[id$='ddlHotelsTop']");
        }
        else {
            bookingDDL = $("[id$='ddlHotelsInterior']");
        }

        if (bookingDDL.val() == "0") {
            bookingURL = "https://www.res99.com/nexres/search/search_results.cgi";
        }
        else {
            bookingURL = "https://www.res99.com/nexres/reservations/availability.cgi";
        }

    var postForm = document.createElement("form");
    postForm.setAttribute("action", bookingURL);
    postForm.setAttribute("method", "post");
    postForm.setAttribute("name", "frmGoBook2");
    postForm.setAttribute("id", "frmGoBook2");
    postForm.setAttribute("style", "display:none;");
    postForm.setAttribute("target", "obe");
    postForm.setAttribute("onSubmit", "window.open('about:blank', 'obe', 'width=1024,height=680,scrollbars=yes,resizable=yes')");
    //postForm.setAttribute("onSubmit", "pageTracker._linkByPost(this); window.open('about:blank', 'obe', 'width=1024,height=680,scrollbars=yes,resizable=yes')");

    //Common pre-set fields
    addFormField(postForm, "avail", "Y");
    addFormField(postForm, "smoking_pref", "NP");
    addFormField(postForm, "bed_type", "NP");
    addFormField(postForm, "src", "10028168");
    addFormField(postForm, "currency_id", "USD");
    addFormField(postForm, "tab", "tab0");
    addFormField(postForm, "city", "Beverly Hills");
    addFormField(postForm, "state", "CA");
    addFormField(postForm, "num_rooms", "1");
    addFormField(postForm, "num_adults", "2");
    addFormField(postForm, "num_children", "0");
    
    //input fields
    if (!isNaN(dtArrive.getMonth())) {
        addFormField(postForm, "chk_in", (dtArrive.getMonth() + 1) + "/" + dtArrive.getDate() + "/" + dtArrive.getFullYear());
        addFormField(postForm, "doa_dd", dtArrive.getDate());
        addFormField(postForm, "doa_mm", dtArrive.getMonth() + 1);
        addFormField(postForm, "doa_yy", dtArrive.getFullYear());
    }
    if (!isNaN(dtDepart.getMonth())) {
        addFormField(postForm, "chk_out", (dtDepart.getMonth() + 1) + "/" + dtDepart.getDate() + "/" + dtDepart.getFullYear());
        addFormField(postForm, "dod_dd", dtDepart.getDate());
        addFormField(postForm, "dod_mm", dtDepart.getMonth() + 1);
        addFormField(postForm, "dod_yy", dtDepart.getFullYear());
    }

    
    //test
    addFormField(postForm, "hotels_id", bookingDDL.val());
    //addFormField(postForm, "name", $("[id$='ddlHotels']").val());
    //addFormField(postForm, "hotel_id", $("[id$='ddlHotels']").val());
    
    document.body.appendChild(postForm);

    var newSubmitButton = document.createElement("input");
    newSubmitButton.setAttribute("type", "submit");
    newSubmitButton.setAttribute("name", "btnSubmit");
    newSubmitButton.setAttribute("value", "Go");
    postForm.appendChild(newSubmitButton);
    newSubmitButton.click();

    return false;
}
