$(document).ready(function() {
    $('#bookNowSubmit').click(function() {
        goBook();
    });
    $('#location').change(function() {
        $("#resort").children().remove();
        var sLocation = $('#location').val();
        fillResortSelect(sLocation);
        showHideDateSelection(sLocation);
    });
    $('#location').trigger('change');
    $('#resort').change(function() {
        showMore();
    });
    $('#step').val(1);

});

function setMinDepartureDate(dateText, datepicker) {
    var dt = new Date(dateText);
    dt.setDate(dt.getDate() + 1);
    $('#departureDate').datepicker('option', 'minDate', dt);
}
function setMaxArrivalDate(dateText, datepicker) {
    var dt = new Date(dateText);
    dt.setDate(dt.getDate() - 1);
    $('#arrivalDate').datepicker('option', 'maxDate', dt);
}


function goBook() {

    var sLocation = $('#location').val();

    var oResort = document.getElementById("resort");
    if (oResort.value == '') {
        alert("Please select a Lodging Property");
        oResort.focus();
        return false;
    }

    if (sLocation != "tahoe") {
        var dtNow = new Date();
        dtNow.setHours(0, 0, 0, 0);

        var oArrive = document.getElementById("arrivalDate");
        var dtArrive = new Date(oArrive.value);
        if (oArrive.value == '' || dtArrive < dtNow) {
            alert("Please select a future arrival date.");
            oArrive.focus();
            return false;
        }

        var oDepart = document.getElementById("departureDate");
        var dtDepart = new Date(oDepart.value);
        if (oDepart.value == '' || 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 oAdults = document.getElementById("adults");
        if (oAdults.value == '') {
            alert("Please enter number of adults");
            oAdults.focus();
            return false;
        }

        var oChildren = document.getElementById("children");
        var iChildren = oChildren.value;
        if (iChildren == '') iChildren = 0;
    }

    var sHotelID = $('#resort').val();

    if (sHotelID.indexOf('##') >= 0) {
        if ($('#step').val() == 1)
            getVCUIChildren();
        else
            bookVCUI();
    }


    if (sHotelID == 'Marriott') {
        bookMarriott(dtArrive,
			dtDepart,
			oAdults.value,
			iChildren
			);
    }

    if (sHotelID == 'Bimini Bay Resort and Marina') {
        bookSynXis(dtArrive,
			dtDepart,
			oAdults.value,
			iChildren,
			'20797',
			'8240'
			);
    }
    
    /*if (sHotelID == 'Austria Haus') {
        bookSynXis(dtArrive,
			dtDepart,
			oAdults.value,
			iChildren,
			'15745',
			'6158'
			);
    }*/

    if (sHotelID == 'Jackson Lake Lodge') {
        bookGtlc(dtArrive,
			dtDepart,
			oAdults.value,
			iChildren,
			'15216',
			'6521');
    }

    if (sHotelID == 'South Lake Tahoe') {
        window.open('http://tahoelodging.escapia.com/site/PropertyList/1889/DesktopDefault.aspx');
    }
    if (sHotelID == 'Northstar-at-Tahoe') {
        window.open('http://reservations.northstarattahoe.com/4807_hotel-list.html');
    }
    
    return false;
}

function hideGetChildren() {
    $('#step').val(1);
    $('#divChildAges').hide();
}

function getVCUIChildren() {
    if ($('#children').val() == 0) $('#step').val(2);
    if ($('#step').val() == 2) bookVCUI();
    else {
        $('#step').val(2);
        $('#divChildAges div.divChildSelect').hide();
        $('#divChildAges').show();
        for (i = 0; i < $('#children').val(); i++) {
            $('#divChildAges div.divChildSelect:eq(' + i + ')').show();
        }
    }
}

function bookVCUI() {

    hideGetChildren();

    var dtArrive = new Date($('#arrivalDate').val());
    var dtDepart = new Date($('#departureDate').val());
    var adults = $('#adults').val();
    var children = $('#children').val();
    var propertyName = $('#resort').val();
    var location = $('#location').val();

    var postForm = document.createElement("form");
    postForm.method = "post";
    postForm.target = "_blank";
    postForm.setAttribute("id", 'quickbook');

    addFormField(postForm, "propertyName", propertyName);
    addFormField(postForm, "promoMainCatIDResort", propertyName);
    addFormField(postForm, "arrival", (dtArrive.getMonth() + 1) + "/" + dtArrive.getDate() + "/" + dtArrive.getFullYear());
    addFormField(postForm, "departure", (dtDepart.getMonth() + 1) + "/" + dtDepart.getDate() + "/" + dtDepart.getFullYear());
    addFormField(postForm, "txt_outBound", (dtArrive.getMonth() + 1) + "/" + dtArrive.getDate() + "/" + dtArrive.getFullYear());
    addFormField(postForm, "txt_inBound", (dtDepart.getMonth() + 1) + "/" + dtDepart.getDate() + "/" + dtDepart.getFullYear());
    addFormField(postForm, "adults", adults);
    for (i = 1; i < 20; i++) {
        addFormField(postForm, "adultid", i); addFormField(postForm, "adultfname", ""); addFormField(postForm, "adultlname", "");
    }
    addFormField(postForm, "children", children);
    addFormField(postForm, "childid", "1001"); addFormField(postForm, "childfname", ""); addFormField(postForm, "childlname", "");
    addFormField(postForm, "childid", "1002"); addFormField(postForm, "childfname", ""); addFormField(postForm, "childlname", "");
    addFormField(postForm, "childid", "1003"); addFormField(postForm, "childfname", ""); addFormField(postForm, "childlname", "");
    addFormField(postForm, "childid", "1004"); addFormField(postForm, "childfname", ""); addFormField(postForm, "childlname", "");
    addFormField(postForm, "childid", "1005"); addFormField(postForm, "childfname", ""); addFormField(postForm, "childlname", "");
    addFormField(postForm, "childid", "1006"); addFormField(postForm, "childfname", ""); addFormField(postForm, "childlname", "");
    addFormField(postForm, "childid", "1007"); addFormField(postForm, "childfname", ""); addFormField(postForm, "childlname", "");
    for (i = 0; i < $('#children').val(); i++) {
        addFormField(postForm, "childages", $('#divChildAges div.divChildSelect select:eq(' + i + ')').val());
    }
    for (i = (parseInt($('#children').val()) + 1); i < 6; i++) {
        addFormField(postForm, "childages", "");
    }

    if (propertyName.indexOf('##') != propertyName.lastIndexOf('##') && location != 'costarica') {
        postForm.action = 'http://reservations.snow.com/switch.board.asp?siteid=10';
        //		postForm.action = 'http://vcui-v2.dev.resorttech.com/switch.board.asp?siteid=10';
        addFormField(postForm, "pageName", "rocksite");
    }
    else if (location == 'beavercreek') {
        postForm.action = 'http://reservations.snow.com/switch.board.asp?siteid=6';
        //		postForm.action = 'http://vcui-v2.dev.resorttech.com/switch.board.asp?siteid=2';
        addFormField(postForm, "pageName", "brand.quickbook");
    }
    else if (location == 'vail') {
        postForm.action = 'http://reservations.snow.com/switch.board.asp?siteid=8';
        //postForm.action = 'http://vcui-v2.dev.resorttech.com/switch.board.asp?siteid=8';		
        addFormField(postForm, "pageName", "brand.quickbook");
    }
    else if (location == 'keystone') {
        //postForm.action = 'http://reservations.snow.com/switch.board.asp?siteid=3';
        //		postForm.action = 'http://vcui-v2.dev.resorttech.com/switch.board.asp?siteid=3';
        //postForm.action = 'http://www.keystoneresort.com/Plan-A-Vacation/Lodging/Lodging-Explorer.aspx?Search=01/30/2010,01/31/2010,2,2,0|0|';
        //postForm.action = 'http://www.keystoneresort.com/Plan-A-Vacation/Lodging/Lodging-Detail.aspx?ProductId=1&Search=1/30/2010,1/31/2010,2,2,0|0|&Code=RIVER-RUN-CONDOMINIUMS';
        //addFormField(postForm, "pageName", "brand.quickbook");

        var arrivalDate = eval(dtArrive.getMonth() + 1) + "/" + dtArrive.getDate() + "/" + dtArrive.getFullYear();
        var departureDate = eval(dtDepart.getMonth() + 1) + "/" + dtDepart.getDate() + "/" + dtDepart.getFullYear();
        var keystoneFreerideUrl = "http://www.keystoneresort.com/Plan-A-Vacation/Lodging/Lodging-Detail.aspx?ProductId=1&Search=";
        keystoneFreerideUrl += arrivalDate + "," + departureDate + ",";
        keystoneFreerideUrl += adults + ",";
        keystoneFreerideUrl += children + ",";
        var childAges = "";
        for (i = 0; i < $('#children').val(); i++) {
            childAges += $('#divChildAges div.divChildSelect select:eq(' + i + ')').val() + "|";
        }
        keystoneFreerideUrl += childAges;
        keystoneFreerideUrl += "&Code=" + propertyName.split("##")[0];
        window.open(keystoneFreerideUrl);
        return;
    }
    else if (location == 'breckenridge') {
        postForm.action = 'http://reservations.snow.com/switch.board.asp?siteid=7';
        //		postForm.action = 'http://vcui-v2.dev.resorttech.com/switch.board.asp?siteid=7';
        addFormField(postForm, "pageName", "brand.quickbook");
    }
    else if (location == 'costarica') {
        postForm.action = 'http://reservations.snow.com/switch.board.asp?siteid=12';
        addFormField(postForm, "pageName", "rr.promo.datestravelers.asp");
        addFormField(postForm, "Lodging", "y");
    }
    else if (location == 'jamaica') {
        postForm.action = 'http://reservations.snow.com/switch.board.asp?siteid=10';
        //postForm.action = 'http://vcui-v2.dev.resorttech.com/switch.board.asp?siteid=8';		
        addFormField(postForm, "pageName", "brand.quickbook");
    }


    document.body.appendChild(postForm);
    postForm.submit();

    //	$('#mainContent').text(postForm.innerHTML) ;

    document.body.removeChild(postForm);

}

function bookMarriott(dtArrive, dtDepart, adults, children) {
    var postForm = document.createElement("form");
    postForm.method = "get";
    postForm.action = 'http://www.marriott.com/reservation/availabilitySearch.mi';
    postForm.target = "_blank";

    addFormField(postForm, "fromDate", (dtArrive.getMonth() + 1) + "/" + dtArrive.getDate() + "/" + dtArrive.getFullYear());
    addFormField(postForm, "toDate", (dtDepart.getMonth() + 1) + "/" + dtDepart.getDate() + "/" + dtDepart.getFullYear());
    addFormField(postForm, "numberOfGuests", (parseInt(adults) + parseInt(children)));
    addFormField(postForm, "numberOfRooms", 1);
    addFormField(postForm, "propertyCode", "whrco");
    addFormField(postForm, "isGlobalSite", "false");
    addFormField(postForm, "isPostSearchState", "false");
    addFormField(postForm, "isSearch", "false");

    document.body.appendChild(postForm);
    postForm.submit();
}

function bookSynXis(dtArrive, dtDepart, adults, children, hotelid, chainid) {

    var postForm = document.createElement("form");
    postForm.method = "get";
    postForm.action = 'https://gc.synxis.com/rez.aspx';
    postForm.target = "_blank";

    addFormField(postForm, "arrive", (dtArrive.getMonth() + 1) + "/" + dtArrive.getDate() + "/" + dtArrive.getFullYear());
    addFormField(postForm, "depart", (dtDepart.getMonth() + 1) + "/" + dtDepart.getDate() + "/" + dtDepart.getFullYear());
    addFormField(postForm, "adult", adults);
    addFormField(postForm, "child", children);
    addFormField(postForm, "Hotel", hotelid);
    addFormField(postForm, "Chain", chainid);
    addFormField(postForm, "step", 2);
    addFormField(postForm, "roomGroup", "");

    document.body.appendChild(postForm);
    postForm.submit();

    return false;

}

function bookGtlc(dtArrive, dtDepart, adults, children, hotelid, chainid) {

    var postForm = document.createElement("form");
    postForm.method = "post";
    postForm.action = 'https://gtlcreservations.com/V1WebControls/ResvMain.aspx';
    postForm.target = "_blank";
    postForm.name = "resForm";
    var oneDay = 1000 * 60 * 60 * 24;
    addFormField(postForm, "ArvDate", (dtArrive.getMonth() + 1) + "/" + dtArrive.getDate() + "/" + dtArrive.getFullYear());
    addFormField(postForm, "iNgts", (Math.ceil((dtDepart.getTime() - dtArrive.getTime()) / (oneDay))));
    addFormField(postForm, "adult", adults);
    addFormField(postForm, "child", children);
    addFormField(postForm, "Hotel", hotelid);
    addFormField(postForm, "Chain", chainid);
    addFormField(postForm, "step", 2);
    addFormField(postForm, "roomGroup", "");

    document.body.appendChild(postForm);
    postForm.submit();

    return false;

}

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);
}



function showMore() {

}

function showHideDateSelection(sLocation) {
    if (sLocation == "tahoe") {
        $('#dateSelections').hide();
    } else {
        $('#dateSelections').show();
    }
}

function fillResortSelect(sLocation) {
    var arrLocation = window["options_" + sLocation];
    var iLength = 0;

    if (arrLocation) {
        $.each(arrLocation, function(key, value) { iLength++; });
        if (iLength > 1 && sLocation != "tahoe")
            $("#resort").append("<option value=''>Select a Lodging Property</option>");
        $.each(arrLocation, function(key, value) {
            $("#resort").append("<option value='" + key + "'>" + value + "</option>");
        });
    }
}

var options_vail = {
    "707##ARRABELLE-AT-VAIL-SQUARE##The Arrabelle at Vail Square, A RockResort": "The Arrabelle at Vail Square",
    "Austria Haus Hotel##Austria Haus Hotel": "Austria Haus Hotel",
    "708##LODGE-AT-VAIL##The Lodge at Vail, A RockResort": "The Lodge at Vail",
    "Marriott": "Vail Mountain Marriott Resort &amp; Spa",
    "The Ritz-Carlton Residences##The Ritz-Carlton Residences": "The Ritz-Carlton Residences"
};
var options_beavercreek = {
    "Arrowhead Village##Arrowhead Village": "Arrowhead Village",
    "Arrowhead Private Homes##Arrowhead Private Homes": "Private Residences of Arrowhead Village",
    "Bachelor Gulch##Bachelor Gulch": "Bachelor Gulch Village",
    "Canyon Run##Canyon Run": "Canyon Run",
    "Chapel Square##Chapel Square": "Chapel Square",
    "Elkhorn Lodge##Elkhorn Lodge": "Elkhorn Lodge",
    "Market Square##Market Square Lodge": "Market Square",
    "Oxford Court##Oxford Court": "Oxford Court",
    "709##THE_OSPREY_AT_BC_VBCRP##The Osprey at Beaver Creek, A RockResort": "The Osprey at Beaver Creek",
    "710##PINES-LODGE_VBCRP##The Pines Lodge, A RockResort": "The Pines Lodge, A RockResort",
    "Saddleridge##Saddleridge": "Saddleridge at Beaver Creek",
    "St. James##St. James Place": "St. James Place",
    "Seasons at Avon##Seasons at Avon": "The Seasons at Avon",
    "Brookside Lodge##Brookside Lodge": "The Residences at Brookside Park",
    "Townsend Place##Townsend Place": "Townsend Place"

};



var options_keystone = {
    "aspen-ridge##aspen-ridge": "Aspen Ridge Condominiums",
    "cabin-in-the-pines##cabin-in-the-pines": "Cabin in the Pines",
    "chateaux-dumont##chateaux-dumont": "Chateaux DuMont",
    "evergreen-condominiums##evergreen-condominiums": "Evergreen Condominiums",
    "forest-condominiums##forest-condominiums": "Forest Condominiums",
    "gateway-mountain-lodge##gateway-mountain-lodge": "Gateway Mountain Lodge",
    "hidden-river-lodge##hidden-river-lodge": "Hidden River Lodge",
    "inn-at_keystone##inn-at_keystone": "The Inn at Keystone",
    "ironwood-townhomes##ironwood-townhomes": "Ironwood Townhomes",
    "key-condo-condominiums##key-condo-condominiums": "Key Condo",
    "keystone-lodge##keystone-lodge": "Keystone Lodge",
    "keystone-lakeside-village-condos##keystone-lakeside-village-condos": "Lakeside Village Condominiums",
    "lone-eagle-condominiums##lone-eagle-condominiums": "Lone Eagle Condominiums",
    "northstar-townhomes##northstar-townhomes": "Northstar Townhomes",
    "red-hawk-townhomes##red-hawk-townhomes": "Red Hawk Townhomes",
    "river-run-condominiums##river-run-condominiums": "River Run Condominiums",
    "riverbank-condominiums##riverbank-condominiums": "Riverbank Condominiums",
    "settlers-creek-townhomes##settlers-creek-townhomes": "Settlers Creek Townhomes",
    "ski-tip-bed-and-breakfast##ski-tip-bed-and-breakfast": "Ski Tip Lodge, Bed and Breakfast",
    "ski-tip-townhomes##ski-tip-townhomes": "Ski Tip Ranch Townhomes",
    "slopeside-condominiums##slopeside-condominiums": "Slopeside Condominiums",
    "snake-river-village##snake-river-village": "Snake River Village",
    "soda-springs-townhomes##soda-springs-townhomes": "Soda Springs Townhomes",
    "starfire-townhomes##starfire-townhomes": "Starfire Townhomes",
    "seasons-townhomes##seasons-townhomes": "The Seasons Townhomes",
    "tenderfoot-lodge##tenderfoot-lodge": "Tenderfoot Lodge",
    "the-sanctuary-and-private-homes##the-sanctuary-and-private-homes": "The Sanctuary and Private Homes",
    "the-springs##the-springs": "The Springs",
    "the-timbers##the-timbers": "The Timbers",
    "trappers-crossing-condos##trappers-crossing-condos": "Trappers Crossing Condominiums",
    "keystone-plaza-condominiums##keystone-plaza-condominiums": "Village Plaza"
};

var options_breckenridge = {
    "Crystal Peak Lodge##Crystal Peak Lodge": "Crystal Peak Lodge",
    "Mountain Thunder##Mountain Thunder": "Mountain Thunder Lodge",
    "2320##ONE-SKI-HILL-PLACE##One Ski Hill Place, A RockResort": "One Ski Hill Place",
    "Village at Breckenridge##Village at Breckenridge": "Village at Breckenridge"
};
var options_wyoming = {
    "Jackson Lake Lodge": "Jackson Lake Lodge",
    "703##SNAKE-RIVER-LODGE_RR##Snake River Lodge &amp; Spa, A RockResort": "Snake River Lodge &amp; Spa"
};
var options_tahoe = {
    "South Lake Tahoe": "South Lake Tahoe",
    "Northstar-at-Tahoe": "Northstar-at-Tahoe &#153;"
};
var options_balconesdelatlantico = {
    "2319##BALCONES_DEL_ATLANTICO_RR##Balcones Del Atlantico, A RockResort": "Balcones Del Atlantico, A RockResort"
};
var options_stlucia = {
    "706##LANDINGS-ST-LUCIA_RR##The Landings St. Lucia, A RockResort": "The Landings St. Lucia"
};
var options_costarica = {
    "2508##HOTEL-ALMA_RR##Alma del Pacifico": "Alma del Pacifico Hotel"
};
var options_jamaica = {
    "2505##HALF-MOON_RR##Half Moon, A RockResort": "Half Moon"
};
var options_bahamas = {
    "Bimini Bay Resort and Marina": "Bimini Bay Resort and Marina"
};


