﻿//Updated on 2009-09-08
var gUrl = {
    noArrival: "http://www.accorhotels.com/accorhotels/lien_externe.svlt?RA1={promoCode}&nom_ville={keyword}&nb_nuit={night}&code_chaine=ALL&ragp=1&libelle_tarif=1&sub=ISP&goto=spec_rate"
    , withArrival: "http://www.accorhotels.com/accorhotels/lien_externe.svlt?RA1={promoCode}&nom_ville={keyword}&jour_arrivee={arrivalday}&mois_arrivee={arrivalmonth}&annee_arrivee={arrivalyear}&jour_depart={departureday}&mois_depart={departuremonth}&annee_depart={departureyear}&nb_nuit={night}&code_chaine=ALL&ragp=1&libelle_tarif=1&sub=ISP&goto=spec_rate"
	, promotion: "ANZBONUS"
};

var settings = {
	optional: false
	, startDate: Date.parseExact("1/10/2009", "d/M/yyyy")
	, maxDate: Date.parseExact("30/12/2009", "d/M/yyyy")
	, startNight: 1
	, nights: 30
};

$(document).ready(init);

function init() {
	updateArrivalDate(settings);
	updateNight(settings);
	updateCityList();
	$("#btnSearch").click(validate);
	
	//window.setTimeout(function() { $("#cmbCountry").change();return false; }, 500);
	window.setTimeout(function() { $("#cmbCountry").trigger("change");return false; }, 500);
}

function updateArrivalDate(setttings) {	
	$("#cmbDay").empty();
	$("#cmbMonthYear").empty();
	//Day
	var d = Date.parse("1/1/2009");
	for(var i = 1; i <= 31; i++) {		
		$("<option/>").val(i).text(d.toString("dd")).appendTo("#cmbDay");
		d.addDays(1);
	}
	//Month/Year
	d = (typeof(settings.startDate) == "undefined") ? Date.today().moveToFirstDayOfMonth() : settings.startDate.clone().moveToFirstDayOfMonth();
	while (d.compareTo(settings.maxDate) <= 0) {
		$("<option/>").val(d.toString("MM/yyyy")).text(d.toString("MMM yyyy")).appendTo("#cmbMonthYear");
		d.addMonths(1);
	}
	
	if (settings.optional) {
		$("<option value=''>-</option>").insertBefore("#cmbDay option:first");	
		$("<option value=''>-</option>").insertBefore("#cmbMonthYear option:first");	
		//IE Bugs
		$("#cmbDay option:first").attr({selected: "selected"});
		$("#cmbMonthYear option:first").attr({selected: "selected"});
	} else {
		window.setTimeout(function() {
		    if (Date.today().compareTo(settings.startDate) > 0)
			    $("#cmbDay option:eq(" + (Date.today().getDate() - 1)+ ")").attr("selected", "selected");			
		}, 250);
	}
}

function updateNight(setttings) {
	$("#cmbNight").empty();
	//Nights
	for(var i = settings.startNight; i <= settings.nights; i++) 
		$("<option/>").val(i).text(i).appendTo("#cmbNight");
	
	if (settings.optional) {
		$("<option value=''>-</option>").insertBefore("#cmbNight option:first");	
		//IE Bugs
		$("#cmbNight option:first").attr({selected: "selected"});		
	} 
}

function updateCityList() {
	$("#cmbCountry").change(function() {		
		//$("#cmbCity option:not(:first)").remove();
		var countryCode = $(this).val();
		if (countryCode != "") {			
			if (countries) {
				$.each(countries, function() {
					if (countryCode == this.countryCode) $("<option/>").val(this.id).text(this.name).appendTo("#cmbCity");
				});
			}
		}
	});
}

function Url(city, country, arrival, night, extra) {
   keyword = (city == "") ? country : city + "," + country;
   extra = (typeof(extra) == "undefined") ? "" : extra;
    
    if (arrival) {
        var departure = arrival.clone().addDays(night);        
        return gUrl.withArrival.replace(/{keyword}/, $.URLEncode(keyword))
			.replace(/{promoCode}/, $.URLEncode(gUrl.promotion))
            .replace(/{arrivalday}/, $.URLEncode(arrival.toString("dd")))
            .replace(/{arrivalmonth}/, $.URLEncode(arrival.toString("MM")))
            .replace(/{arrivalyear}/, $.URLEncode(arrival.toString("yyyy")))
            .replace(/{departureday}/, $.URLEncode(departure.toString("dd")))
            .replace(/{departuremonth}/, $.URLEncode(departure.toString("MM")))
            .replace(/{departureyear}/, $.URLEncode(departure.toString("yyyy")))
            .replace(/{night}/, $.URLEncode(night));                
    } else {
        return gUrl.noArrival.replace(/{keyword}/, $.URLEncode(keyword))
			.replace(/{promoCode}/, $.URLEncode(gUrl.promotion))
            .replace(/{night}/, $.URLEncode(night));                
    }
};

function validate() {
    var intError = 0;
    var err = "";
    var country, countryText;
    var city, cityText;
    
    if ($("#cmbCountry")[0].selectedIndex == 0) {
        err += 'Please select a country to search\n';
        intError = 1;
    }
    
    var isArrivalSelected = (($("#cmbDay").val() + $("#cmbMonthYear").val() + $("#cmbNight").val()) != "");    
    if (isArrivalSelected && 
        (
            (Date.parseExact($("#cmbDay").val() + "/" + $("#cmbMonthYear").val(), "d/M/yyyy") == null)
            || (Number($("#cmbNight").val()) <= 0)
        )) 
    {
        err += 'Please select a valid arrival date and night(s)\n';
        intError = 1;
    }
                
    if (intError) {
        alert(err);
        return false;
    } else {        
        country = $("#cmbCountry").val();
		countryText = $("#cmbCountry option:selected").text();
        if ($("#cmbCity").val() != "") {
            city = $("#cmbCity").val();
            cityText = $("#cmbCity option:selected").text();
        } else {
            city = "";
            cityText = "";
        }

        if (country == "OTHER") {
            window.location.href = "http://www.accorhotels.com";
        } else {
            var arrival = Date.parseExact($("#cmbDay").val() + "/" + $("#cmbMonthYear").val(), "d/M/yyyy");
            var nights = 1;
            if (arrival) {
                nights = Number($("#cmbNight").val());
            }
            //alert(Url(cityText, countryText, arrival, nights, ""));
            window.location.href = Url(cityText, countryText, arrival, nights, "");
        }
        return false;
    }
};

//Countries object
var countries = [
{countryCode:'AU',id:1,name:'Adelaide'},
{countryCode:'AU',id:2,name:'Ballarat'},
{countryCode:'AU',id:3,name:'Barossa Valley'},
{countryCode:'AU',id:4,name:'Brisbane'},
{countryCode:'AU',id:5,name:'Broome'},
{countryCode:'AU',id:6,name:'Cairns'},
{countryCode:'AU',id:7,name:'Canberra'},
{countryCode:'AU',id:8,name:'Coffs Harbour'},
{countryCode:'AU',id:9,name:'Creswick'},
{countryCode:'AU',id:10,name:'Darwin'},
{countryCode:'AU',id:11,name:'Exmouth'},
{countryCode:'AU',id:12,name:'Geelong'},
{countryCode:'AU',id:13,name:'Hobart'},
{countryCode:'AU',id:14,name:'Hunter Valley'},
{countryCode:'AU',id:15,name:'Lake Macquarie'},
{countryCode:'AU',id:16,name:'Launceston'},
{countryCode:'AU',id:17,name:'Melbourne'},
{countryCode:'AU',id:18,name:'Mildura'},
{countryCode:'AU',id:19,name:'Perth'},
{countryCode:'AU',id:20,name:'Port Douglas'},
{countryCode:'AU',id:21,name:'Port Macquarie'},
{countryCode:'AU',id:22,name:'Sunshine Coast'},
{countryCode:'AU',id:23,name:'Sydney'},
{countryCode:'AU',id:24,name:'Thredbo Valley'},
{countryCode:'AU',id:25,name:'Townsville'},
{countryCode:'AU',id:26,name:'Wollongong'}
];
