function validateDate(string) {
    var m = /^(\d\d) (\w{3}) (\d{4})$/.exec(string);
    if (!m) return false;
    var month = validateDate.months[m[2].toLowerCase()];
    if (typeof month != "number") return false;
    var date = +m[1];
    var year = +m[3];
    var d = new Date(year, month, date);
    if (d.getDate() != date || d.getMonth() != month) return false;
    return d;
}
validateDate.months = {
    jan: 0, feb: 1, mar: 2, apr: 3, may: 4, jun: 5,
    jul: 6, aug: 7, sep: 8, oct: 9, nov: 10, dec: 11
};



function checkDates() {
    // regular expression to match required date format

    var arrival = document.getElementById('cphFullPage_txtArrivalDate').value;
    var departure = document.getElementById('cphFullPage_txtDepDate').value;



    if (validateDate(arrival) == false) {
        ShowPopup('Your Arrival date is not in the correct format. An example of a correctly formatted date should read - 1 Aug 2011');
        document.getElementById('ArrContCel').className += " ErrorTextCal";
        return false;
    }
    if (validateDate(departure) == false) {
        ShowPopup('Your Departure date is not in the correct format. An example of a correctly formatted date should read - 1 Aug 2011');
        document.getElementById('DepContCel').className += " ErrorTextCal";
        return false;
    }
    RedirectPopup();
    return false;
}

function RedirectPopup() {
    var v = "https://secure.ospreymail.com/secure/newdolmen2/rooms.asp?date1x=" + document.getElementById('cphFullPage_txtArrivalDate').value.replace(/ /g, "-") + "&date2x=" + document.getElementById('cphFullPage_txtDepDate').value.replace(/ /g, "-");
    open_win(v);
}
function open_win(url_add)
   {
   window.open(url_add);
   }



function ShowPopup(ErrorMessage) {

    //Fade in the Popup and add close button
    var popID = "popup1";
    var popURL = "#?w=520";


    //Pull Query & Variables from href URL
    var query = popURL.split('?');
    var dim = query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value


    $('#' + popID).fadeIn().css({ 'width': Number(popWidth) }).prepend('<a href="#" class="close"><img src="/images/btClosePopUp.png" class="btn_close" title="Close Window" alt="Close" /></a>');
    $('#PopupMessage').text(ErrorMessage);
    //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top': -popMargTop,
        'margin-left': -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn(); //Fade in the fade layer 

}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22732505-1']);
_gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

