/***********************************************
 Fool-Proof Date Input Script with DHTML Calendar
 by Jason Moon - calendar@moonscript.com
 ************************************************/

// Customizable variables
var DefaultDateFormat = 'MM/DD/YYYY'; // If no date format is supplied, this will be used instead
var HideWait = 7; // Number of seconds before the calendar will disappear
var Y2kPivotPoint = 76; // 2-digit years before this point will be created in the 21st century
var UnselectedMonthText = ''; // Text to display in the 1st month list item when the date isn't required
var FontSize = 9; // In pixels
var FontFamily = 'Verdana';
var CellWidth = 25;
var CellHeight = 19;
var ImageURL = 'http://www.galwayhotelguide.com/js/calendar.jpg';
var NextURL = 'http://www.galwayhotelguide.com/js/next.gif';
var PrevURL = 'http://www.galwayhotelguide.com/js/prev.gif';
var CalBGColor = '#ffffff';
var TopRowBGColor = '#58022d';
var DayBGColor = '#b7879f';



// Global variables
var ZCounter = 100;
var Today = new Date();
var WeekDays = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var MonthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

// Write out the stylesheet definition for the calendar
with (document) {
   writeln('<style>');
   writeln('td.calendarDateInput {letter-spacing:normal;line-height:normal;font-family:' + FontFamily + ',Arial;font-size:' + FontSize + 'px ;}');
   writeln('select.calendarDateInput {letter-spacing:.06em;font-family:Verdana,Arial;font-size:9px;}');
   writeln('input.calendarDateInput {letter-spacing:.06em;font-family:Verdana,Arial;font-size:9px;}');
   writeln('</style>');
}

// Only allows certain keys to be used in the date field
function YearDigitsOnly(e) {
   var KeyCode = (e.keyCode) ? e.keyCode : e.which;
   return ((KeyCode == 8) // backspace
        || (KeyCode == 9) // tab
        || (KeyCode == 37) // left arrow
        || (KeyCode == 39) // right arrow
        || (KeyCode == 46) // delete
        || ((KeyCode > 47) && (KeyCode < 58)) // 0 - 9
   );
}

// Gets the absolute pixel position of the supplied element
function GetTagPixels(StartTag, Direction) {
   var PixelAmt = (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
   while ((StartTag.tagName != 'BODY') && (StartTag.tagName != 'HTML')) {
      StartTag = StartTag.offsetParent;
      PixelAmt += (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
   }
   return PixelAmt;
}

// Is the specified select-list behind the calendar?
function BehindCal(SelectList, CalLeftX, CalRightX, CalTopY, CalBottomY, ListTopY) {
   var ListLeftX = GetTagPixels(SelectList, 'LEFT');
   var ListRightX = ListLeftX + SelectList.offsetWidth;
   var ListBottomY = ListTopY + SelectList.offsetHeight;
   return (((ListTopY < CalBottomY) && (ListBottomY > CalTopY)) && ((ListLeftX < CalRightX) && (ListRightX > CalLeftX)));
}

// For IE, hides any select-lists that are behind the calendar
function FixSelectLists(Over) {
   if (navigator.appName == 'Microsoft Internet Explorer') {
      var CalDiv = this.getCalendar();
      var CalLeftX = CalDiv.offsetLeft;
      var CalRightX = CalLeftX + CalDiv.offsetWidth;
      var CalTopY = CalDiv.offsetTop;
      var CalBottomY = CalTopY + (CellHeight * 9);
      var FoundCalInput = false;
      formLoop :
      for (var j=this.formNumber;j<document.forms.length;j++) {
         for (var i=0;i<document.forms[j].elements.length;i++) {
            if (typeof document.forms[j].elements[i].type == 'string') {
               if ((document.forms[j].elements[i].type == 'hidden') && (document.forms[j].elements[i].name == this.hiddenFieldName)) {
                  FoundCalInput = true;
                  i += 3; // 3 elements between the 1st hidden field and the last year input field
               }
               if (FoundCalInput) {
                  if (document.forms[j].elements[i].type.substr(0,6) == 'select') {
                     ListTopY = GetTagPixels(document.forms[j].elements[i], 'TOP');
                     if (ListTopY < CalBottomY) {
                        if (BehindCal(document.forms[j].elements[i], CalLeftX, CalRightX, CalTopY, CalBottomY, ListTopY)) {
                           document.forms[j].elements[i].style.visibility = (Over) ? 'hidden' : 'visible';
                        }
                     }
                     else break formLoop;
                  }
               }
            }
         }
      }
   }
}

// Displays a message in the status bar when hovering over the calendar days
function DayCellHover(Cell, Over, Color, HoveredDay) {
   Cell.style.backgroundColor = (Over) ? DayBGColor : Color;
   if (Over) {
      if ((this.yearValue == Today.getFullYear()) && (this.monthIndex == Today.getMonth()) && (HoveredDay == Today.getDate())) self.status = 'Click to select today';
      else {
         var Suffix = HoveredDay.toString();
         switch (Suffix.substr(Suffix.length - 1, 1)) {
            case '1' : Suffix += (HoveredDay == 11) ? 'th' : 'st'; break;
            case '2' : Suffix += (HoveredDay == 12) ? 'th' : 'nd'; break;
            case '3' : Suffix += (HoveredDay == 13) ? 'th' : 'rd'; break;
            default : Suffix += 'th'; break;
         }
         self.status = 'Click to select ' + this.monthName + ' ' + Suffix;
      }
   }
   else self.status = '';
   return true;
}

// Sets the form elements after a day has been picked from the calendar
function PickDisplayDay(ClickedDay) {
   //this.show(); //changed 18 Sept, 2008 - Jonny (must be removed otherwise calendar will hide after date chosen)
   var MonthList = this.getMonthList();
   var DayList = this.getDayList();
   var YearField = this.getYearField();
   FixDayList(DayList, GetDayCount(this.displayed.yearValue, this.displayed.monthIndex));
   // Select the month and day in the lists
   for (var i=0;i<MonthList.length;i++) {
      if (MonthList.options[i].value == this.displayed.monthIndex) MonthList.options[i].selected = true;
   }
   for (var j=1;j<=DayList.length;j++) {
      if (j == ClickedDay) DayList.options[j-1].selected = true;
   }
   this.setPicked(this.displayed.yearValue, this.displayed.monthIndex, ClickedDay);
   // Change the year, if necessary
   YearField.value = this.picked.yearPad;
   YearField.defaultValue = YearField.value;
}

// Builds the HTML for the calendar days EDIT FONT COLOR FOR MONTHS HERE
function BuildCalendarDays() {
  var Live;
  var TextStyle, BackColor;
  var Rows = 5;
   if (((this.displayed.dayCount == 31) && (this.displayed.firstDay > 4)) || ((this.displayed.dayCount == 30) && (this.displayed.firstDay == 6))) Rows = 6;
   else if ((this.displayed.dayCount == 28) && (this.displayed.firstDay == 0)) Rows = 4;
   var HTML = '<table width="' + (CellWidth * 7) + '" cellspacing="0" cellpadding="0" height="110px" style="cursor:default">';
   for (var j=0;j<Rows;j++) {
      HTML += '<tr>';
      for (var i=1;i<=7;i++) {
         Day = (j * 7) + (i - this.displayed.firstDay);
         if ((Day >= 1) && (Day <= this.displayed.dayCount)) {
            if ((this.displayed.yearValue == this.picked.yearValue) && (this.displayed.monthIndex == this.picked.monthIndex) && (Day == this.picked.day)) 
			{
               TextStyle = 'color:#000000;font-weight:bold;'
               BackColor = DayBGColor;
			}
			else 
			{
               	if( (this.displayed.yearValue == Today.getFullYear())  )
				{
					if( (this.displayed.monthIndex == Today.getMonth()) )
					{
						if ( Day >= Today.getDate() )
						{
							   TextStyle = 'color:#000000;font-weight: bold;'
              				   BackColor = CalBGColor;
						}
						else
						{
							   TextStyle = 'color:#bababa; font-style: italic; font-size: 8px;'
              				   BackColor = CalBGColor;
						}
					}
					else if( (this.displayed.monthIndex < Today.getMonth()) )
					{
						 TextStyle = 'color:#C0C0C0'
              			 BackColor = CalBGColor;
					}
					else
					{
						 TextStyle = 'color:#000000;'
              			 BackColor = CalBGColor;
					}
				}
				else if( (this.displayed.yearValue < Today.getFullYear())  )
				{
					 TextStyle = 'color:#C0C0C0'
            		 BackColor = CalBGColor;
				}
            
			}
			
            if ((this.displayed.yearValue == Today.getFullYear()) && (this.displayed.monthIndex == Today.getMonth()) && (Day == Today.getDate()))
			
				
				TextStyle += 'padding:0px;';
    	      	
				if( (this.displayed.yearValue == Today.getFullYear())  )
				{
					if( (this.displayed.monthIndex == Today.getMonth()) )
					{
						if ( Day >= Today.getDate() )
						{
							 Live = 'onClick="' + this.objName + '.pickDay(' + Day + '),AddDays()"'
						}
					}
					else if((this.displayed.monthIndex > Today.getMonth()))
					{
						 Live = 'onClick="' + this.objName + '.pickDay(' + Day + '),AddDays()"'
					}
				}
				else if( (this.displayed.yearValue > Today.getFullYear()) )
				{
					Live = 'onClick="' + this.objName + '.pickDay(' + Day + '),AddDays()"'
				}
				
				HTML += '<td align="center" class="calendarDateInput" style="cursor:default;height:' + CellHeight + ';width:' + CellWidth + ';' + TextStyle + ';background-color:' + BackColor + '" onMouseOver="return ' + this.objName + '.displayed.dayHover(this,true,\'' + BackColor + '\',' + Day + ')" onMouseOut="return ' + this.objName + '.displayed.dayHover(this,false,\'' + BackColor + '\')"'+Live+'>' + Day + '</td>';
			    TextStyle = '';
                BackColor = '';
		 }
         else HTML += '<td class="calendarDateInput" style="height:' + CellHeight + '">&nbsp;</td>';
      }
      HTML += '</tr>';
   }
   return HTML += '</table>';
}

// Determines which century to use (20th or 21st) when dealing with 2-digit years
function GetGoodYear(YearDigits) {
   if (YearDigits.length == 4) return YearDigits;
   else {
      var Millennium = (YearDigits < Y2kPivotPoint) ? 2000 : 1900;
      return Millennium + parseInt(YearDigits,10);
   }
}

// Returns the number of days in a month (handles leap-years)
function GetDayCount(SomeYear, SomeMonth) {
   return ((SomeMonth == 1) && ((SomeYear % 400 == 0) || ((SomeYear % 4 == 0) && (SomeYear % 100 != 0)))) ? 29 : MonthDays[SomeMonth];
}

// Highlights the buttons
function VirtualButton(Cell, ButtonDown) {
   if (ButtonDown) {
      Cell.style.borderLeft = 'none';
      Cell.style.borderTop = 'none';
      Cell.style.borderBottom = 'none';
      Cell.style.borderRight = 'none';
	  Cell.style.backgroundColor = '#58022d';
   }
   else {
      Cell.style.borderLeft = 'none';
      Cell.style.borderTop = 'none';
      Cell.style.borderBottom = 'none';
      Cell.style.borderRight = 'none';
	  Cell.style.backgroundColor = '#58022d';
   }
}

// Mouse-over for the previous/next month buttons
function NeighborHover(Cell, Over, DateObj) {
   if (Over) {
      VirtualButton(Cell, false);
      self.status = 'Click to view ' + DateObj.fullName;
   }
   else {
      Cell.style.border = 'none';
      self.status = '';
   }
   return true;
}

// Adds/removes days from the day list, depending on the month/year
function FixDayList(DayList, NewDays) {
   var DayPick = DayList.selectedIndex + 1;
   if (NewDays != DayList.length) {
      var OldSize = DayList.length;
      for (var k=Math.min(NewDays,OldSize);k<Math.max(NewDays,OldSize);k++) {
         (k >= NewDays) ? DayList.options[NewDays] = null : DayList.options[k] = new Option(k+1, k+1);
      }
      DayPick = Math.min(DayPick, NewDays);
      DayList.options[DayPick-1].selected = true;
   }
   return DayPick;
}

// Resets the year to its previous valid value when something invalid is entered
function FixYearInput(YearField) {
   var YearRE = new RegExp('\\d{' + YearField.defaultValue.length + '}');
   if (!YearRE.test(YearField.value)) YearField.value = YearField.defaultValue;
}

// Displays a message in the status bar when hovering over the calendar icon
function CalIconHover(Over) {
   var Message = (this.isShowing()) ? 'hide' : 'show';
   self.status = (Over) ? 'Click to ' + Message + ' the calendar' : '';
   return true;
}

// Starts the timer over from scratch
function CalTimerReset() {
   eval('clearTimeout(' + this.timerID + ')');
   //eval(this.timerID + '=setTimeout(\'' + this.objName + '.show()\',' + (HideWait * 1000) + ')'); //changed 18 Sept, 2008 - Jonny (turn off calendar hide timer)
}

// The timer for the calendar
function DoTimer(CancelTimer) {
   if (CancelTimer) eval('clearTimeout(' + this.timerID + ')');
   else {
      eval(this.timerID + '=null');
      this.resetTimer();
   }
}

// Show or hide the calendar
function ShowCalendar() {
   if (this.isShowing()) {
      var StopTimer = true;
      this.getCalendar().style.zIndex = --ZCounter;
      this.getCalendar().style.visibility = 'hidden';
      this.fixSelects(false);
   }
   else {
      var StopTimer = false;
      this.fixSelects(true);
      this.getCalendar().style.zIndex = ++ZCounter;
      this.getCalendar().style.visibility = 'visible';
   }
   this.handleTimer(StopTimer);
   self.status = '';
}

// Hides the input elements when the "blank" month is selected
function SetElementStatus(Hide) {
   this.getDayList().style.visibility = (Hide) ? 'hidden' : 'visible';
   this.getYearField().style.visibility = (Hide) ? 'hidden' : 'visible';
   this.getCalendarLink().style.visibility = (Hide) ? 'hidden' : 'visible';
}

// Sets the date, based on the month selected
function CheckMonthChange(MonthList) {
   var DayList = this.getDayList();
   if (MonthList.options[MonthList.selectedIndex].value == '') {
      DayList.selectedIndex = 0;
      this.hideElements(true);
      this.setHidden('');
   }
   else {
      this.hideElements(false);
      if (this.isShowing()) {
         this.resetTimer(); // Gives the user more time to view the calendar with the newly-selected month
         this.getCalendar().style.zIndex = ++ZCounter; // Make sure this calendar is on top of any other calendars
      }
      var DayPick = FixDayList(DayList, GetDayCount(this.picked.yearValue, MonthList.options[MonthList.selectedIndex].value));
      this.setPicked(this.picked.yearValue, MonthList.options[MonthList.selectedIndex].value, DayPick);
   }
}

// Sets the date, based on the day selected
function CheckDayChange(DayList) {
   if (this.isShowing()) this.show();
   this.setPicked(this.picked.yearValue, this.picked.monthIndex, DayList.selectedIndex+1);
}

// Changes the date when a valid year has been entered
function CheckYearInput(YearField) {

      var NewYear = GetGoodYear(YearField.value);
      var MonthList = this.getMonthList();
      var NewDay = FixDayList(this.getDayList(), GetDayCount(NewYear, this.picked.monthIndex));
      this.setPicked(NewYear, this.picked.monthIndex, NewDay);
      YearField.defaultValue = YearField.value;
}

// Holds characteristics about a date
function dateObject() {
   if (Function.call) { // Used when 'call' method of the Function object is supported
      var ParentObject = this;
      var ArgumentStart = 0;
   }
   else { // Used with 'call' method of the Function object is NOT supported
      var ParentObject = arguments[0];
      var ArgumentStart = 1;
   }
   ParentObject.date = (arguments.length == (ArgumentStart+1)) ? new Date(arguments[ArgumentStart+0]) : new Date(arguments[ArgumentStart+0], arguments[ArgumentStart+1], arguments[ArgumentStart+2]);
   ParentObject.yearValue = ParentObject.date.getFullYear();
   ParentObject.monthIndex = ParentObject.date.getMonth();
   ParentObject.monthName = MonthNames[ParentObject.monthIndex];
   ParentObject.fullName = ParentObject.monthName + ' ' + ParentObject.yearValue;
   ParentObject.day = ParentObject.date.getDate();
   ParentObject.dayCount = GetDayCount(ParentObject.yearValue, ParentObject.monthIndex);
   var FirstDate = new Date(ParentObject.yearValue, ParentObject.monthIndex, 1);
   ParentObject.firstDay = FirstDate.getDay();
}

// Keeps track of the date that goes into the hidden field
function storedMonthObject(DateFormat, DateYear, DateMonth, DateDay) {
   (Function.call) ? dateObject.call(this, DateYear, DateMonth, DateDay) : dateObject(this, DateYear, DateMonth, DateDay);
   this.yearPad = this.yearValue.toString();
   this.monthPad = (this.monthIndex < 9) ? '0' + String(this.monthIndex + 1) : this.monthIndex + 1;
   this.dayPad = (this.day < 10) ? '0' + this.day.toString() : this.day;
   this.monthShort = this.monthName.substr(0,3).toUpperCase();
   // Formats the year with 2 digits instead of 4
   if (DateFormat.indexOf('YYYY') == -1) this.yearPad = this.yearPad.substr(2);
   // Define the date-part delimiter
   if (DateFormat.indexOf('/') >= 0) var Delimiter = '/';
   else if (DateFormat.indexOf('-') >= 0) var Delimiter = '-';
   else var Delimiter = '';
   // Determine the order of the months and days
   if (/DD?.?((MON)|(MM?M?))/.test(DateFormat)) {
      this.formatted = this.dayPad + Delimiter;
      this.formatted += (RegExp.$1.length == 3) ? this.monthShort : this.monthPad;
   }
   else if (/((MON)|(MM?M?))?.?DD?/.test(DateFormat)) {
      this.formatted = (RegExp.$1.length == 3) ? this.monthShort : this.monthPad;
      this.formatted += Delimiter + this.dayPad;
   }
   // Either prepend or append the year to the formatted date
   this.formatted = (DateFormat.substr(0,2) == 'YY') ? this.yearPad + Delimiter + this.formatted : this.formatted + Delimiter + this.yearPad;
}

// Object for the current displayed month
function displayMonthObject(ParentObject, DateYear, DateMonth, DateDay) {
   (Function.call) ? dateObject.call(this, DateYear, DateMonth, DateDay) : dateObject(this, DateYear, DateMonth, DateDay);
   this.displayID = ParentObject.hiddenFieldName + '_Current_ID';
   this.getDisplay = new Function('return document.getElementById(this.displayID)');
   this.dayHover = DayCellHover;
   this.goCurrent = new Function(ParentObject.objName + '.getCalendar().style.zIndex=++ZCounter;' + ParentObject.objName + '.setDisplayed(Today.getFullYear(),Today.getMonth());');
   if (ParentObject.formNumber >= 0) this.getDisplay().innerHTML = this.fullName;
}

// Object for the previous/next buttons
function neighborMonthObject(ParentObject, IDText, DateMS) {
   (Function.call) ? dateObject.call(this, DateMS) : dateObject(this, DateMS);
   this.buttonID = ParentObject.hiddenFieldName + '_' + IDText + '_ID';
   this.hover = new Function('C','O','NeighborHover(C,O,this)');
   this.getButton = new Function('return document.getElementById(this.buttonID)');
   this.go = new Function(ParentObject.objName + '.getCalendar().style.zIndex=++ZCounter;' + ParentObject.objName + '.setDisplayed(this.yearValue,this.monthIndex);');
   if (ParentObject.formNumber >= 0) this.getButton().title = this.monthName;
}

// Sets the currently-displayed month object
function SetDisplayedMonth(DispYear, DispMonth) {
   this.displayed = new displayMonthObject(this, DispYear, DispMonth, 1);
   // Creates the previous and next month objects
   this.previous = new neighborMonthObject(this, 'Previous', this.displayed.date.getTime() - 86400000);
   this.next = new neighborMonthObject(this, 'Next', this.displayed.date.getTime() + (86400000 * (this.displayed.dayCount + 1)));
   // Creates the HTML for the calendar
   if (this.formNumber >= 0) this.getDayTable().innerHTML = this.buildCalendar();
}

// Sets the current selected date
function SetPickedMonth(PickedYear, PickedMonth, PickedDay) {
   this.picked = new storedMonthObject(this.format, PickedYear, PickedMonth, PickedDay);
   this.setHidden(this.picked.formatted);
   this.setDisplayed(PickedYear, PickedMonth);
}

// The calendar object
function calendarObject(DateName, DateFormat, DefaultDate) {

   /* Properties */
   this.hiddenFieldName = DateName;
   this.monthListID = DateName + '_Month_ID';
   this.dayListID = DateName + '_Day_ID';
   this.yearFieldID = DateName + '_Year_ID';
   this.monthDisplayID = DateName + '_Current_ID';
   this.calendarID = DateName + '_ID';
   this.dayTableID = DateName + '_DayTable_ID';
   this.calendarLinkID = this.calendarID + '_Link';
   this.timerID = this.calendarID + '_Timer';
   this.objName = DateName + '_Object';
   this.format = DateFormat;
   this.formNumber = -1;
   this.picked = null;
   this.displayed = null;
   this.previous = null;
   this.next = null;

   /* Methods */
   this.setPicked = SetPickedMonth;
   this.setDisplayed = SetDisplayedMonth;
   this.checkYear = CheckYearInput;
   this.fixYear = FixYearInput;
   this.changeMonth = CheckMonthChange;
   this.changeDay = CheckDayChange;
   this.resetTimer = CalTimerReset;
   this.hideElements = SetElementStatus;
   this.show = ShowCalendar;
   this.handleTimer = DoTimer;
   this.iconHover = CalIconHover;
   this.buildCalendar = BuildCalendarDays;
   this.pickDay = PickDisplayDay;
   this.fixSelects = FixSelectLists;
   this.setHidden = new Function('D','if (this.formNumber >= 0) this.getHiddenField().value=D');
   // Returns a reference to these elements
   this.getHiddenField = new Function('return document.forms[this.formNumber].elements[this.hiddenFieldName]');
   this.getMonthList = new Function('return document.getElementById(this.monthListID)');
   this.getDayList = new Function('return document.getElementById(this.dayListID)');
   this.getYearField = new Function('return document.getElementById(this.yearFieldID)');
   this.getCalendar = new Function('return document.getElementById(this.calendarID)');
   this.getDayTable = new Function('return document.getElementById(this.dayTableID)');
   this.getCalendarLink = new Function('return document.getElementById(this.calendarLinkID)');
   this.getMonthDisplay = new Function('return document.getElementById(this.monthDisplayID)');
   this.isShowing = new Function('return !(this.getCalendar().style.visibility != \'visible\')');

   /* Constructor */
   // Functions used only by the constructor
   function getMonthIndex(MonthAbbr) { // Returns the index (0-11) of the supplied month abbreviation
      for (var MonPos=0;MonPos<MonthNames.length;MonPos++) {
         if (MonthNames[MonPos].substr(0,3).toUpperCase() == MonthAbbr.toUpperCase()) break;
      }
      return MonPos;
   }
   function SetGoodDate(CalObj, Notify) { // Notifies the user about their bad default date, and sets the current system date
      CalObj.setPicked(Today.getFullYear(), Today.getMonth(), Today.getDate());
      if (Notify) alert('WARNING: The supplied date is not in valid \'' + DateFormat + '\' format: ' + DefaultDate + '.\nTherefore, the current system date will be used instead: ' + CalObj.picked.formatted);
   }
   // Main part of the constructor
   if (DefaultDate != '') {
      if ((this.format == 'YYYYMMDD') && (/^(\d{4})(\d{2})(\d{2})$/.test(DefaultDate))) this.setPicked(RegExp.$1, parseInt(RegExp.$2,10)-1, RegExp.$3);
      else {
         // Get the year
         if ((this.format.substr(0,2) == 'YY') && (/^(\d{2,4})(-|\/)/.test(DefaultDate))) { // Year is at the beginning
            var YearPart = GetGoodYear(RegExp.$1);
            // Determine the order of the months and days
            if (/(-|\/)(\w{1,3})(-|\/)(\w{1,3})$/.test(DefaultDate)) {
               var MidPart = RegExp.$2;
               var EndPart = RegExp.$4;
               if (/D$/.test(this.format)) { // Ends with days
                  var DayPart = EndPart;
                  var MonthPart = MidPart;
               }
               else {
                  var DayPart = MidPart;
                  var MonthPart = EndPart;
               }
               MonthPart = (/\d{1,2}/i.test(MonthPart)) ? parseInt(MonthPart,10)-1 : getMonthIndex(MonthPart);
               this.setPicked(YearPart, MonthPart, DayPart);
            }
            else SetGoodDate(this, true);
         }
         else if (/(-|\/)(\d{2,4})$/.test(DefaultDate)) { // Year is at the end
            var YearPart = GetGoodYear(RegExp.$2);
            // Determine the order of the months and days
            if (/^(\w{1,3})(-|\/)(\w{1,3})(-|\/)/.test(DefaultDate)) {
               if (this.format.substr(0,1) == 'D') { // Starts with days
                  var DayPart = RegExp.$1;
                  var MonthPart = RegExp.$3;
               }
               else { // Starts with months
                  var MonthPart = RegExp.$1;
                  var DayPart = RegExp.$3;
               }
               MonthPart = (/\d{1,2}/i.test(MonthPart)) ? parseInt(MonthPart,10)-1 : getMonthIndex(MonthPart);
               this.setPicked(YearPart, MonthPart, DayPart);
            }
            else SetGoodDate(this, true);
         }
         else SetGoodDate(this, true);
      }
   }
}

// Main function that creates the form elements
function DateInput(DateName, Required, DateFormat, DefaultDate) {
   if (arguments.length == 0) document.writeln('<span style="color:red;font-size:' + FontSize + 'px;font-family:' + FontFamily + ';">ERROR: Missing required parameter in call to \'DateInput\': [name of hidden date field].</span>');
   else {
      // Handle DateFormat
      if (arguments.length < 3) { // The format wasn't passed in, so use default
         DateFormat = DefaultDateFormat;
         if (arguments.length < 2) Required = false;
      }
      else if (/^(Y{2,4}(-|\/)?)?((MON)|(MM?M?)|(DD?))(-|\/)?((MON)|(MM?M?)|(DD?))((-|\/)Y{2,4})?$/i.test(DateFormat)) DateFormat = DateFormat.toUpperCase();
      else { // Passed-in DateFormat was invalid, use default format instead
         var AlertMessage = 'WARNING: The supplied date format for the \'' + DateName + '\' field is not valid: ' + DateFormat + '\nTherefore, the default date format will be used instead: ' + DefaultDateFormat;
         DateFormat = DefaultDateFormat;
         if (arguments.length == 4) { // DefaultDate was passed in with an invalid date format
            var CurrentDate = new storedMonthObject(DateFormat, Today.getFullYear(), Today.getMonth(), Today.getDate());
            AlertMessage += '\n\nThe supplied date (' + DefaultDate + ') cannot be interpreted with the invalid format.\nTherefore, the current system date will be used instead: ' + CurrentDate.formatted;
            DefaultDate = CurrentDate.formatted;
         }
         alert(AlertMessage);
      }
      // Define the current date if it wasn't set already
      if (!CurrentDate) var CurrentDate = new storedMonthObject(DateFormat, Today.getFullYear(), Today.getMonth(), Today.getDate());
      // Handle DefaultDate
      if (arguments.length < 4) { // The date wasn't passed in
         DefaultDate = (Required) ? CurrentDate.formatted : ''; // If required, use today's date
      }
      // Creates the calendar object!
      eval(DateName + '_Object=new calendarObject(\'' + DateName + '\',\'' + DateFormat + '\',\'' + DefaultDate + '\')');
      // Determine initial viewable state of day, year, and calendar icon
      if ((Required) || (arguments.length == 4)) {
         var InitialStatus = '';
         var InitialDate = eval(DateName + '_Object.picked.formatted');
      }
      else {
         var InitialStatus = ' style="visibility:hidden"';
         var InitialDate = '';
         eval(DateName + '_Object.setPicked(' + Today.getFullYear() + ',' + Today.getMonth() + ',' + Today.getDate() + ')');
      }
      // Create the form elements
      with (document) {
         writeln('<input type="hidden" name="' + DateName + '" value="' + InitialDate + '">');
         // Find this form number
         for (var f=0;f<forms.length;f++) {
            for (var e=0;e<forms[f].elements.length;e++) {
               if (typeof forms[f].elements[e].type == 'string') {
                  if ((forms[f].elements[e].type == 'hidden') && (forms[f].elements[e].name == DateName)) {
                     eval(DateName + '_Object.formNumber='+f);
                     break;
                  }
               }
            }
         }
         writeln('<table cellpadding="0" cellspacing="0" align="right"><tr align="right">' + String.fromCharCode(13) + '<td valign="top">');

		 writeln('<select class="calendarDateInput" id="' + DateName + '_Month_ID" onChange="' + DateName + '_Object.changeMonth(this),AddDays()">');
         if (!Required) {
            var NoneSelected = (DefaultDate == '') ? ' selected' : '';
            writeln('<option value=""' + NoneSelected + '>' + UnselectedMonthText + '</option>');
         }
         for (var i=0;i<12;i++) {
            
			MonthSelected = ((DefaultDate != '') && (eval(DateName + '_Object.picked.monthIndex') == i)) ? ' selected' : '';
   			writeln('<option value="' + i + '"' + MonthSelected+ '>' + MonthNames[i].substr(0,3) + '</option>');
         }
	 
		 writeln('</select>' + String.fromCharCode(13) + '</td>' + String.fromCharCode(13) + '<td valign="top">');
         
		 writeln('<select' + InitialStatus + ' class="calendarDateInput" id="' + DateName + '_Day_ID" onChange="' + DateName + '_Object.changeDay(this),AddDays()">');
         for (var j=1;j<=eval(DateName + '_Object.picked.dayCount');j++) {
            DaySelected = ((DefaultDate != '') && (eval(DateName + '_Object.picked.day') == j)) ? ' selected' : '';
            writeln('<option' + DaySelected + '>' + j + '</option>');
         }
         writeln('</select>' + String.fromCharCode(13) + '</td>' + String.fromCharCode(13) + '<td valign="top">');
         
		 
		 var yr;
		 Tday = new Date();
		 yr = Tday.getFullYear();
 		 writeln('<select class="calendarDateInput" id="' + DateName + '_Year_ID" onChange="' + DateName + '_Object.checkYear(this),AddDays()">');
         for(var x=yr;x<=yr+1;x++)  
		 {
            YearSelected = ((DefaultDate != '') && (eval(DateName + '_Object.picked.yearPad') == x)) ? ' selected' : '';
            writeln('<option value="' + x + '"' + YearSelected + '>' + x + '</option>');
         }
		//changed 18 Sept, 2008 - Jonny
		//calendar spans changed to divs, calendar button removed (button anchor must remain), calendar put into separate table row
		//callendar position changed absolute to relative, visibility property removed
		 writeln('</select>' + String.fromCharCode(13) + '</td>' + String.fromCharCode(13) + '</tr>' + String.fromCharCode(13));
		 writeln('<tr>' + String.fromCharCode(13) + '<td valign="top" colspan="3">' + String.fromCharCode(13) + '<a' + InitialStatus + ' id="' + DateName + '_ID_Link" href="javascript:' + DateName + '_Object.show()" onMouseOver="return ' + DateName + '_Object.iconHover(true)" onMouseOut="return ' + DateName + '_Object.iconHover(false)"></a>&nbsp;');
			writeln('<div id="' + DateName + '_ID" style="position:relative;width:' + (CellWidth * 7) + 'px;background-color:' + CalBGColor + ';border:1px solid dimgray;" onMouseOver="' + DateName + '_Object.handleTimer(true)" onMouseOut="' + DateName + '_Object.handleTimer(false)">');
         writeln('<table width="' + (CellWidth * 7) + '" cellspacing="0" cellpadding="1">' + String.fromCharCode(13) + '<tr style="background-color:' + TopRowBGColor + ';">');
         writeln('<td id="' + DateName + '_Previous_ID" style="cursor:default" align="center" class="calendarDateInput" style="height:' + CellHeight + '" onClick="' + DateName + '_Object.previous.go()" onMouseDown="VirtualButton(this,true)" onMouseUp="VirtualButton(this,false)" onMouseOver="return ' + DateName + '_Object.previous.hover(this,true)" onMouseOut="return ' + DateName + '_Object.previous.hover(this,false)" title="' + eval(DateName + '_Object.previous.monthName') + '"><img src="' + PrevURL + '"></td>');
         writeln('<td height="19" id="' + DateName + '_Current_ID" style="cursor:pointer; color: #ffffff;" align="center" class="calendarDateInput" style="height:' + CellHeight + '" colspan="5" onClick="' + DateName + '_Object.displayed.goCurrent()" onMouseOver="self.status=\'Click to view ' + CurrentDate.fullName + '\';return true;" onMouseOut="self.status=\'\';return true;" title="Show Current Month">' + eval(DateName + '_Object.displayed.fullName') + '</td>');
         writeln('<td id="' + DateName + '_Next_ID" style="cursor:default" align="center" class="calendarDateInput" style="height:' + CellHeight + '" onClick="' + DateName + '_Object.next.go()" onMouseDown="VirtualButton(this,true)" onMouseUp="VirtualButton(this,false)" onMouseOver="return ' + DateName + '_Object.next.hover(this,true)" onMouseOut="return ' + DateName + '_Object.next.hover(this,false)" title="' + eval(DateName + '_Object.next.monthName') + '"><img src="' + NextURL + '"></td></tr>' + String.fromCharCode(13) + '<tr>');
         for (var w=0;w<7;w++) 
		 writeln('<td width="' + CellWidth + '" align="center" class="calendarDateInput" style="height:' + CellHeight + ';width:' + CellWidth + ';font-weight:bold" bgcolor="#ffffff">' + WeekDays[w] + '</td>');
         writeln('</tr>' + String.fromCharCode(13) + '</table>' + String.fromCharCode(13) + '<span id="' + DateName + '_DayTable_ID">' + eval(DateName + '_Object.buildCalendar()') + '</span>' + String.fromCharCode(13) + '</div>' + String.fromCharCode(13) + '</td>' + String.fromCharCode(13) + '</tr>' + String.fromCharCode(13) + '</table>');
	 }
   }
}

function AddDays()
{
	var NNights = document.findhotel.Nights.value;
	var Arr = document.findhotel.Arrival.value;
	var SptArr = Arr.split("/");
	var ETA = new Date();	
	ETA = new Date(SptArr[0], (SptArr[1] - 1), SptArr[2], 12, 0, 0, 0);
			
	ETA = Date.parse(ETA);
	ETA = parseInt(ETA, 10);
	ETA = ETA + NNights*(24*60*60*1000);
	Depart = new Date(ETA);
		 
	var Mn = eval(parseInt(Depart.getMonth())+1);
	var month;
	switch (Mn)
    {
	 case 1:
         month = 'Jan';
         break;
      case 2:
         month = 'Feb';
         break;
      case 3:
         month = 'Mar';
         break;
      case 4:
         month = 'Apr';
         break;
      case 5:
         month = 'May';
         break;
      case 6:
         month = 'Jun';
         break;
      case 7:
         month = 'Jul';
         break;
      case 8:
         month = 'Aug';
         break;
      case 9:
         month = 'Sep';
         break;
      case 10:
         month = 'Oct';
         break;
      case 11:
         month = 'Nov';
         break;
      case 12:
         month = 'Dec';
         break;
	 } 
	
	var PadM = (Mn < 10) ? '0' + Mn : Mn;	
	var PadD = (Depart.getDate() < 10) ? '0' + Depart.getDate() : Depart.getDate();
	document.findhotel.Departure.value = Depart.getFullYear()+"/"+PadM+"/"+PadD;
	
	var DepTxt = document.getElementById("Dep");
   	DepTxt.firstChild.nodeValue= Depart.getDate()+" "+month+" "+Depart.getFullYear();
	
	document.findhotel.SearchBooking.disabled = false;
}

function CheckBooking()
{	
	//
	var RoomCapacity = new Array();
	RoomCapacity["Apartment"] = 12;
	RoomCapacity["Double"] = 2; 
	RoomCapacity["Large_Family"] = 5; 
	RoomCapacity["Single"] = 1; 
	RoomCapacity["Small_Family"] = 3; 
	RoomCapacity["Suite"] = 2; 
	RoomCapacity["Triple"] = 3; 
	RoomCapacity["Twin"] = 2;
	
	var today = new Date();
	var Arr = document.findhotel.Arrival.value;
	var SptArr = Arr.split("/");
	var refDate = new Date(SptArr[0], (SptArr[1] - 1), SptArr[2], 0, 0, 0, 0);
	if (refDate.setHours(0,0,0,0) < today.setHours(0,0,0,0))
	{
		alert("Invalid date selected");
		return false;
	}
	//
}

function LoadDef() {
	AddDays();
	
	//reset the County drop down
	if(document.getElementById('County')) {
		document.findhotel.County.selectedIndex = 0;
	}
	if(document.getElementById('location')) {
		document.findhotel.location.options[0]=new Option("All Regions","");
	}
	
	
	if(typeof(CounT) != 'undefined'){
		for(var c=0;c<document.findhotel.County.length;c++){
			if(document.findhotel.County.options[c].value === CounT){
				document.findhotel.County.selectedIndex = c;
				break; 
			}
		}
		
		//CounT = capitalizeMe(CounT);
		Regions(CounT);
	} 
	
	if(typeof(Town) != 'undefined'){
		if(Town != ""){
			for(var t=0;t<document.findhotel.location.length;t++){
				if(document.findhotel.location.options[t].value === Town){
					document.findhotel.location.selectedIndex = t;
					break; 
				}
			}
		}
	}
	
	
}

function capitalizeMe(obj) {
	var val = obj;
	newVal = '';
	var val = val.split(' ');
	for(var c=0; c < val.length; c++) {
		newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
	}
	return trim11 (newVal);
}

function trim11 (str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

function Regions(County){
var Counties = new Array();
//Counties["Please select"] = new Array(" ");
Counties["Antrim"] = new Array(" Belfast City"," Belfast City"," Belfast International Airport","Aldergrove","Antrim Town","Ballymena","Carrickfergus","Castlereagh","Dunadry","Mallusk","Newtownabbey","Portballintrae","Portrush","Templepatrick");
Counties["Armagh"] = new Array(" ", "Armagh","Bessbrook","Camlough","Crossmaglen","Forkhill","Jonesborough","Keady","Lurgan","Markethill");
Counties["Carlow"] = new Array(" ", " Carlow","Ballon","Carlow Guest Houses","Killerig","Leighlinbridge","Tullow");
Counties["Cavan"] = new Array(" ", "Arvagh","Bailieborough","Ballyconnell","Belturbet","Cavan","Cootehill","Kingscourt","Mountnugent","Virginia");
Counties["Clare"] = new Array(" ", "Ballyvaughan","Bunratty","Clare Guest Houses","Doolin","Ennis","Ennistymon","Kilkee","Lehinch","Liscannor","Lisdoonvarna","Miltown Malby","Newmarket-on-fergus","Shannon","Shannon airport");
Counties["Cork"] = new Array("All Cork city","All County Cork","Ballincollig","Ballycotton","Baltimore","Bandon","Bantry","Blarney","Carrigaline","Castlemartyr","Clonakilty","Cobh","Cork airport","Dunmanway","Fota Island","Garryvoe","Glanmire","Glengarriff","Kinsale","Little Island","Macroom","Mallow","Midleton","Rosscarbery","Skibbereen","Youghal");
Counties["Derry"] = new Array("Coleraine","Derry","Dungiven","Eglinton");
Counties["Donegal"] = new Array("Ballybofey","Bunbeg","Bundoran","Donegal","Donegal Guesthouses","Dungloe","Glen","Gweedore","Inishowen","Letterkenny","Milford","Rathmullan","Stranorlar");
Counties["Down"] = new Array("Ballynahinch","Banbridge","Bangor","Carryduff","Comber","Donaghadee","Downpatrick","Dromore","Dundonald","Holywood","Kilkeel","Lisburn","Newcastle","Newry","Newtownards","Warrenpoint");
Counties["Dublin"] = new Array("All County Dublin"," All Dublin City"," All Dublin Towns"," Dublin Airport"," Dublin City Centre"," Dublin City North"," Dublin City Pheonix Park"," Dublin City Port"," Dublin City South"," Dublin Croke Park Area"," Dublin Guest Houses"," Merrrion Square Area"," North County Dublin"," South County Dublin"," Stephens Green Area"," Temple Bar Area","Balbriggan","Ballsbridge","Ballyfermot","Ballymun","Blackrock","Castlenock","Chapelizod","Christchurch","Clondalkin","Clontarf","Dalkey","Deansgrange","Dollymount","Donabate","Drumcondra","Dublin 1","Dublin 2","Dublin 3","Dublin 4","Dublin 5","Dublin 6","Dublin 7","Dublin 8","Dublin 9","Dublin 10","Dublin 11","Dublin 12","Dublin 13","Dublin 14","Dublin 15","Dublin 16","Dublin 17","Dublin 18","Dublin 20","Dublin 22","Dublin 24","Dublin Mountains","Dun Laoghaire","Dunlaoire","Foxrock","Howth","Killiney","Killiney village","Leixlip","Leopardstown","Loughlinstown","Lucan","Malahide","Monkstown","Mount merrion","Mt merrion","Naas Road","Newlands Cross","Rathmines","Sandycove","Sandyford","Sandymount","Santry","Shankill","Stillorgan","Swords","Tallaght");
Counties["Fermanagh"] = new Array("Belleek","Enniskillen","Irvinestown","Kesh");
Counties["Galway"] = new Array("Aran Islands","Athenry","Ballinasloe","Barna","Carna","Claddaghduff","Claregalway","Clarinbridge","Cleggan","Clifden","Clonbur","Connemara","Dunmore","Galway City","Glenamaddy","Gort","Headford","Inis Boffin","Inverin","Kinvara","Leenane","Letterfrack","Loughrea","Milltown","Moycullen","Moylough","Mt Bellew","Oranmore","Oughterard","Portumna","Recess","Renvyle","Rossaveel","Roundstone","Salthill","Spiddal","Tuam","Woodford");
Counties["Kerry"] = new Array("Kerry Hotels","Ballybunion","Cahirciveen","Castleisland","Dingle","Dingle peninsula","Kenmare","Kerry airport","Kerry Guest Houses","Killarney","Listowel","Ring of kerry","Sneem","Tarbert","Tralee","Valentia island");
Counties["Kildare"] = new Array("Athy","Celbridge","Clane","Curragh","Kildare Town","Leixlip","Maynooth","Monasterevin","Moyvally","Naas","Naas Rd, Dublin","Newbridge","Punchestown Racecourse Hotels");
Counties["Kilkenny"] = new Array("Ballyfoyle","Ballyragget","Bennettsbridge","Callan","Castlecomer","Castlewarren","Clifden","Dungarvan","Freshford","Glenmore","Goresbridge","Gowran","Graiguenamanagh","Inistioge","Jenkinstown","Kells","Kilkenny City","Kilmanagh","Knocktopher","Mullinavat","Piltown","Slieverue","Thomastown","Tullaroan","Urlingford");
Counties["Laois"] = new Array("Abbeyleix","Ballacolla","Ballaghmore","Clogh","Donaghmore","Durrow","Killenard","Mountmellick","Mountrath","Portalington","Portlaoise");
Counties["Leitrim"] = new Array("Ballinamore","Carrick-on-shannon","Carrigallen","Dromahair","Drumshanbo","Mohill","Rooskey");
Counties["Limerick"] = new Array("Abbeyfeale","Adare","Castleconnell","Castletroy","Glin"," Limerick City","Newcastle West","Templeglantine");
Counties["Longford"] = new Array("Ballymahon","Edgeworthstown","Longford");
Counties["Louth"] = new Array("All Louth Guest Houses","All Louth Hotels","Ardee","Carlingford","Carlingford village","Drogheda","Dundalk","Dunleer","Omeath","Ravensdale","Termonfeckin");
Counties["Mayo"] = new Array("Achill","Ballina","Ballinrobe","Ballycastle","Belmullet","Castlebar","Charlestown","Clare Island","Claremorris","Cong","Crossmolina","Doohoma","Foxford","Killala","Kiltimagh","Knock","Louisburgh","Mayo Guest Houses","Mulranny","Mulrany","Newport","Pontoon","Swinford","Westport");
Counties["Meath"] = new Array("Ashbourne","Bettystown","Dunboyne","Enfield","Gormanstown","Kilmessan","Navan","Trim");
Counties["Monaghan"] = new Array("Carrickmacross","Castleblayney","Clones","Glaslough","Inniskeen","Monaghan");
Counties["Offaly"] = new Array("Banagher","Birr","Clara","Clonmacnoise","Ferbane","Kinnitty","Shannonbridge","Tullamore");
Counties["Roscommon"] = new Array("Athlone Town, Roscommon","Ballaghaderreen","Ballinlough","Ballyfarnon","Boyle","Castlerea","Cloonfad","Four Roads","Frenchpark","Knockcroghery","Roosky","Roscommon B&B's","Roscommon Guest Houses","Roscommon Town","Strokestown","Thomastown","Tulsk");
Counties["Sligo"] = new Array("Sligo Town","Ballincar","Ballygawley","Curry","Enniscrone","Rosses Point","Tubbercurry");
Counties["Tipperary"] = new Array("Ballina","Bansha","Borrisokane","Cahir","Carrick-on-suir","Cashel","Clonmel","Dundrum","Fethard","Holycross","Horse and Jockey","Nenagh","Newport","Roscrea","Templemore","Thurles","Tipperary Town");
Counties["Tyrone"] = new Array("Cookstown","Dungannon","Fivemiletown","Gortaclare","Omagh","Strabane");
Counties["Waterford"] = new Array("All County Waterford","All Waterford City","Ardmore","Butlerstown","Cappoquin","Dungarvan","Dunmore east","Furraleigh","Kilmeaden","Little Island","Slieverue","Tramore","Waterford","Woodstown");
Counties["Westmeath"] = new Array("Athlone","Glasson","Kilbeggan","Kinnegad","Mullingar","Rochfortbridge","Tyrrellspass","Westmeath Guest Houses/B&Bs","Westmeath Self Catering");
Counties["Wexford"] = new Array("Adamstown","Blackwater","Campile","Castlebridge","Churchtown","Clogh","Drinagh","Enniscorthy","Ferns","Gorey","New ross","Rosslare","Rosslare Harbour","Wexford");
Counties["Wicklow"] = new Array("Arklow","Aughrim","Bray","Delgany","Enniskerry","Greystones","Newtownmountkennedy","Wicklow","Woodenbridge");
	//clear the location drop down
	document.findhotel.location.options.length = 0;
	
	document.findhotel.location.options[0]=new Option("All Regions","");

	for(var g=0; g <= Counties[County].length-1; g++) {
		document.findhotel.location.options[g+1]=new Option(Counties[County][g+1], Counties[County][g+1])
	}

}
