/*
* Date Format 1.2.2
* (c) 2007-2008 Steven Levithan <stevenlevithan.com>
* MIT license
* Includes enhancements by Scott Trenda <scott.trenda.net> and Kris Kowal <cixar.com/~kris.kowal/>
*
* Accepts a date, a mask, or a date and a mask.
* Returns a formatted version of the given date.
* The date defaults to the current date/time.
* The mask defaults to dateFormat.masks.default.
*/
var dateFormat = function() {
    var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function(val, len) {
		    val = String(val);
		    len = len || 2;
		    while (val.length < len) val = "0" + val;
		    return val;
		};

    // Regexes and supporting functions are cached through closure
    return function(date, mask, utc) {
        var dF = dateFormat;

        // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
        if (arguments.length == 1 && (typeof date == "string" || date instanceof String) && !/\d/.test(date)) {
            mask = date;
            date = undefined;
        }

        // Passing date through Date applies Date.parse, if necessary
        date = date ? new Date(date) : new Date();
        if (isNaN(date)) throw new SyntaxError("invalid date");

        mask = String(dF.masks[mask] || mask || dF.masks["default"]);

        // Allow setting the utc argument via the mask
        if (mask.slice(0, 4) == "UTC:") {
            mask = mask.slice(4);
            utc = true;
        }

        var _ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
			    d: d,
			    dd: pad(d),
			    ddd: dF.i18n.dayNames[D],
			    dddd: dF.i18n.dayNames[D + 7],
			    m: m + 1,
			    mm: pad(m + 1),
			    mmm: dF.i18n.monthNames[m],
			    mmmm: dF.i18n.monthNames[m + 12],
			    yy: String(y).slice(2),
			    yyyy: y,
			    h: H % 12 || 12,
			    hh: pad(H % 12 || 12),
			    H: H,
			    HH: pad(H),
			    M: M,
			    MM: pad(M),
			    s: s,
			    ss: pad(s),
			    l: pad(L, 3),
			    L: pad(L > 99 ? Math.round(L / 10) : L),
			    t: H < 12 ? "a" : "p",
			    tt: H < 12 ? "am" : "pm",
			    T: H < 12 ? "A" : "P",
			    TT: H < 12 ? "AM" : "PM",
			    Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
			    o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
			    S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

        return mask.replace(token, function($0) {
            return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
        });
    };
} ();

// Some common format strings
dateFormat.masks = {
    "default": "ddd mmm dd yyyy HH:MM:ss",
    shortDate: "m/d/yy",
    mediumDate: "mmm d, yyyy",
    longDate: "mmmm d, yyyy",
    fullDate: "dddd, mmmm d, yyyy",
    shortTime: "h:MM TT",
    mediumTime: "h:MM:ss TT",
    longTime: "h:MM:ss TT Z",
    isoDate: "yyyy-mm-dd",
    isoTime: "HH:MM:ss",
    isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
    isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
    dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
    monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function(mask, utc) {
    return dateFormat(this, mask, utc);
};








/* Client-side access to querystring name=value pairs
Version 1.3
28 May 2008

License (Simplified BSD):
http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}







function writeAccuBookSearchForm() {

    if (!isAccuBookSettingsOK()) {
        alert("Online Booking is not installed correctly. Please check your business name, site name and filename settings.");
        document.write('Online Booking is currently not available.');
    }
    else {

        document.write('<div id="accubookSearchForm">');
        document.write('<div id="accubookCheckInRow">');
        document.write('  <div id="checkinLbl">Arrive</div>');
        document.write('  <div id="checkinDay"><select name="DdCheckinDay" id="DdCheckinDay"></select></div>');
        document.write('  <div id="checkinMonth"><select name="DdCheckinMonth" id="DdCheckinMonth"></select></div>');
        document.write('</div>');
        document.write('<div id="accubookCheckOutRow">');
        document.write('  <div id="checkoutLbl">Depart</div>');
        document.write('  <div id="checkoutDay"><select name="DdCheckOutDay" id="DdCheckOutDay"></select></div>');
        document.write('  <div id="checkoutMonth"><select name="DdCheckoutMonth" id="DdCheckoutMonth"></select></div>');
        document.write('</div>');
        document.write('<div id="accubookSearchRow">');
        document.write('  <div id="accubookSearchButton"><input type="button" value="Search" onclick="searchAccuBook();" /></div>');
        document.write('</div>');
        document.write('</div>');



        for (var i = 1; i < 32; i = i + 1) {
            var optn = document.createElement("OPTION");
            optn.text = i;
            optn.value = i;
            document.getElementById("DdCheckinDay").options.add(optn);
            var optn2 = document.createElement("OPTION");
            optn2.text = i;
            optn2.value = i;
            document.getElementById("DdCheckOutDay").options.add(optn2);

        }


        var accubook_two_days_away = new Date().setDate(new Date().getDate() + 1);
        var accubook_four_days_away = new Date().setDate(new Date().getDate() + 2);
        var accubook_today = new Date().setDate(new Date().getDate());
        var accubook_tomorrow = new Date().setDate(new Date().getDate() + 1);
        var accubook_startMonth = new Date().setDate(1);


        for (var i = 0; i < 12; i = i + 1) {

            var tempDate = new Date().setFullYear(new Date().getFullYear(), new Date().getMonth() + i, 1);

            var optn = document.createElement("OPTION");
            optn.text = dateFormat(tempDate, "mmm yy"); ;
            optn.value = dateFormat(tempDate, "yyyy/mm/");
            document.getElementById("DdCheckinMonth").options.add(optn);
            var optn2 = document.createElement("OPTION");
            optn2.text = dateFormat(tempDate, "mmm yy"); ;
            optn2.value = dateFormat(tempDate, "yyyy/mm/");
            document.getElementById("DdCheckoutMonth").options.add(optn2);


        }






        for (var i = 0; i <= document.getElementById("DdCheckinDay").length - 1; i = i + 1) {
            var ddlText = document.getElementById("DdCheckinDay").options[i].text;
            txtText = dateFormat(accubook_today, "d");
            if (ddlText == txtText) {
                document.getElementById("DdCheckinDay").selectedIndex = i;
                break;
            }
        }
        for (var i = 0; i <= document.getElementById("DdCheckinMonth").length - 1; i = i + 1) {
            var ddlText = document.getElementById("DdCheckinMonth").options[i].value;
            txtText = dateFormat(accubook_today, "yyyy/mm/");
            if (ddlText == txtText) {
                document.getElementById("DdCheckinMonth").selectedIndex = i;
                break;
            }
        }
        for (var i = 0; i <= document.getElementById("DdCheckOutDay").length - 1; i = i + 1) {
            var ddlText = document.getElementById("DdCheckOutDay").options[i].value;
            txtText = dateFormat(accubook_tomorrow, "d");
            if (ddlText == txtText) {
                document.getElementById("DdCheckOutDay").selectedIndex = i;
                break;
            }
        }
        for (var i = 0; i <= document.getElementById("DdCheckoutMonth").length - 1; i = i + 1) {
            var ddlText = document.getElementById("DdCheckoutMonth").options[i].value;
            txtText = dateFormat(accubook_tomorrow, "yyyy/mm/");
            if (ddlText == txtText) {
                document.getElementById("DdCheckoutMonth").selectedIndex = i;
                break;
            }
        }

    }
}

function writeAccuBookIFrame() {


    if (!isAccuBookSettingsOK()) {
        alert("Online Booking is not installed correctly. Please check your business name, site name and filename settings.");
        document.write('Online Booking is currently not available.');
    }
    else {
        var accubook_accubookURL;
        var accubook_framecode;
        var accubook_fromDate;
        var accubook_toDate;

        var accubook_qs = new Querystring();

        var accubook_two_days_away = new Date().setDate(new Date().getDate() + 2);
        var accubook_four_days_away = new Date().setDate(new Date().getDate() + 4);

        var accubook_today = new Date().setDate(new Date().getDate());
        var accubook_tomorrow = new Date().setDate(new Date().getDate() + 1);

        var accubook_qs_from = accubook_qs.get("fromdate", accubook_today);
        var accubook_qs_to = accubook_qs.get("todate", accubook_tomorrow);



        accubook_fromDate = dateFormat(new Date(accubook_qs_from), "dd") + "/" + dateFormat(new Date(accubook_qs_from), "mmm") + "/" + dateFormat(new Date(accubook_qs_from), "yyyy");
        accubook_toDate = dateFormat(new Date(accubook_qs_to), "dd") + "/" + dateFormat(new Date(accubook_qs_to), "mmm") + "/" + dateFormat(new Date(accubook_qs_to), "yyyy");

        accubook_accubookURL = "http://reservations.accubook.net/Default.aspx?view=room&amp;business_name=" + accubook_Business + "&amp;site_name=" + accubook_Site + "&amp;fromdate=" + accubook_fromDate + "&amp;todate=" + accubook_toDate;


        accubook_framecode = "<iframe name='availFrame' marginwidth='0' marginheight='0' frameborder='0' width='510' scrolling='no' style='height: 1620px;' src='" + accubook_accubookURL + "'>Your browser does not support Frames - <a href='" + accubook_accubookURL + "'>click here</a> to view availability.</iframe>";


        document.write(accubook_framecode);

    }

}




function searchAccuBook() {

    var accubook_searchFrom;
    var accubook_searchTo;
    accubook_searchFrom = new Date(document.getElementById('DdCheckinMonth').value + document.getElementById('DdCheckinDay').value);
    accubook_searchTo = new Date(document.getElementById('DdCheckoutMonth').value + document.getElementById('DdCheckOutDay').value);

    if (accubook_searchFrom > accubook_searchTo) {
        alert("The departure date should be after the arrival date.");
    }
    else {
        var today = new Date();
        today.setHours(0);
        today.setMilliseconds(0);
        today.setMinutes(0);
        today.setSeconds(0);

        if (accubook_searchFrom < today || accubook_searchTo < today) {
            alert("The arrival date and departure date should not be before today's date.");
        }
        else {
            window.location.href = accubook_SearchPage + "?fromdate=" + document.getElementById('DdCheckinMonth').value + document.getElementById('DdCheckinDay').value + "&todate=" + document.getElementById('DdCheckoutMonth').value + document.getElementById('DdCheckOutDay').value
        }

    }

}




function isAccuBookSettingsOK() {
    if (typeof accubook_Business == 'undefined' || typeof accubook_Site == 'undefined' || typeof accubook_SearchPage == 'undefined')
        return false;
    else
        return true;
}


