function ajaxButton() {

var ajaxRequest;
	try {
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Your browser broke!");
				return false;
			}
		}
	}

function stateChanged()
{
if (ajaxRequest.readyState == 4 || ajaxRequest.readyState=="complete")
	{
var yep = "after";
document.getElementById(yep).innerHTML=ajaxRequest.responseText;
document.forms['availForm'].submit();
 	}
}

var str = "str=a";
var url = "/inc/resload.php";
ajaxRequest.onreadystatechange=stateChanged;
ajaxRequest.open("POST", url, true);
ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", str.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(str);

}


function submitRes() {

document.getElementById("before").style.display = "none";
document.getElementById("after").style.display = "inline";

var ajaxRequest;
	try {
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Your browser broke!");
				return false;
			}
		}
	}

function stateChanged()
{
if (ajaxRequest.readyState == 4 || ajaxRequest.readyState=="complete")
	{
var yep = "after";
document.getElementById(yep).innerHTML=ajaxRequest.responseText;

   var iframe = document.createElement("IFRAME");
   iframe.setAttribute("src", "https://reservations.chateauelan.com/smsworld/wc.dll?smsworld~availbox~WSI=cewr");
   iframe.name = "fakeres";
   iframe.style.display = "none";
   iframe.style.width = 1 + "px";
   iframe.style.height = 1 + "px";
   document.body.appendChild(iframe);
   setTimeout('ajaxButton()', 6000);
  
 	}
}

var str = "str=a";
var url = "/inc/resload.php";
ajaxRequest.onreadystatechange=stateChanged;
ajaxRequest.open("POST", url, true);
ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", str.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(str);

}

function setFormDate() {

	var d = new Date();
	var t = new Date();
	
	t.setDate(t.getDate() + 2);
	
	// var x = document.getElementById("inday");
	// x.options[x.selectedIndex].text = d.getDate();
	// document.getElementById("outday").options[document.getElementById("outday").selectedIndex].text = t.getDate();

	var month=new Array(12);
	month[0]="January";
	month[1]="February";
	month[2]="March";
	month[3]="April";
	month[4]="May";
	month[5]="June";
	month[6]="July";
	month[7]="August";
	month[8]="September";
	month[9]="October";
	month[10]="November";
	month[11]="December";
	
	document.availForm.inday.options[d.getDate() - 1].selected = true;
	document.availForm.outday.options[t.getDate() - 1].selected = true;

	document.availForm.inmonth.options[d.getMonth()].selected = true;
	document.availForm.outmonth.options[t.getMonth()].selected = true;

	
	// document.getElementById("inmonth").options[document.getElementById("inmonth").selectedIndex].text = month[d.getMonth()];
	// document.getElementById("outmonth").options[document.getElementById("outmonth").selectedIndex].text = month[t.getMonth()];
}


//function which handles checking the entire res form for errors before submitting
function validateResrv(formData) 
{
var validity=true;

var arrMonthField = formData.inmonth;
var arrDayField   = formData.inday;
var arrYearField  = formData.inyear;
var depMonthField = formData.outmonth;
var depDayField   = formData.outday;
var depYearField  = formData.outyear;

if (!isBrowserSupp()) 
	{	
	return validity;
	}
var inMIdx   = arrMonthField.options.selectedIndex;
var outMIdx  = depMonthField.options.selectedIndex;
var inDIdx   = arrDayField.options.selectedIndex;
var outDIdx  = depDayField.options.selectedIndex;
var inYrIdx  = arrYearField.options.selectedIndex;
var outYrIdx = depYearField.options.selectedIndex;

inDate = new Date (parseInt(arrYearField.options[inYrIdx].text), inMIdx, inDIdx + 1);
outDate = new Date (parseInt(depYearField.options[outYrIdx].text), outMIdx, outDIdx + 1);

var inDtVal = inDate.getTime();
var outDtVal = outDate.getTime();

var currentTime = new Date();
var curDate = ( new Date( currentTime.getYear(), currentTime.getMonth(), currentTime.getDate() ) ).getTime();

// Validate the arrival and departure dates 
if (inDtVal > outDtVal) {

	alert("The Departure date selected is invalid." );
	validity= false;

} else if ( ( inDtVal < curDate ) && ( outDtVal < curDate ) ) { 

	alert( "The arrival and departure dates are invalid." );
	validity= false;

} else if ( inDtVal < curDate ) { 

	alert( "The Arrival date selected is invalid." );
	validity= false;

} else if ( outDtVal < curDate ) { 

	alert( "The Departure date selected is invalid." );
	validity= false;

} else {
	// address milliseconds returned from getTime
	var numDays = Math.round((outDtVal-inDtVal)/(1000*60*60*24));
	if (numDays > 21)
		{
		alert("We are sorry.  You cannot make reservations for more than 21 days.\nPlease adjust your arrival and departure dates and resubmit your request.");
		validity= false;
		}
	}
return validity;
}


// Checks if browser is Netscape 2.0x since the options array properties don't work with Netscape 2.0x
function isBrowserSupp() {

    // Get the version of the browser
    version =  parseFloat( navigator.appVersion );

    if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    }
    else {
	return true;
    }
}


function isLeapYear(yrStr)
{
var leapYear=false;
var year = parseInt(yrStr, 10);
// every fourth year is a leap year
if (year%4 == 0)
	{
	leapYear=true;
	// unless it's a multiple of 100
	if (year%100 == 0)
		{
		leapYear=false;
		// unless it's a multiple of 400
		if (year%400 == 0)
			{
			leapYear=true;
			}
		}
	}
return leapYear;
}


function getDaysInMonth(mthIdx, YrStr)
{
// all the rest have 31
var maxDays=31
// expect Feb. (of course)
if (mthIdx==1) 
	{
	if (isLeapYear(YrStr))
		{
		maxDays=29;
		}
	else 
		{
		maxDays=28;
		}
	}
// thirty days hath...
if (mthIdx==3 || mthIdx==5 || mthIdx==8 || mthIdx==10)
	{
	maxDays=30;
	}
return maxDays;
}


//the function which does some magic to the date fields
// return non-zero if it is the last day of the month
function adjustDate(mthIdx, Dt, Yr) 
{
var value=0; 		
var numDays=getDaysInMonth(mthIdx, Yr.options[Yr.options.selectedIndex].text);

if (mthIdx==1) 
	{
	if (Dt.options.selectedIndex + 1 < numDays)
		{
		return 0;
		}
	else 
		{
		Dt.options.selectedIndex=numDays - 1;
		//check for leap year
		if (numDays==29)
			{
			return 99;
			}
		else 
			{
			return 1;
			}
		}
	}
if (Dt.options.selectedIndex + 1 < numDays)
	{
	value=0;
	}
else 
	{
	if (Dt.options.selectedIndex + 1 > numDays)
		{
		Dt.options.selectedIndex--;
		value=3;
		}
	else 
		{
		//index is 31 or 30
		value=2;
		}
	}
return value;
}

// Get the index of the corresponding option in the field's list
function getIndex (val, field) {
	var i;
	for (i = 0; i < field.length; i++) {
		if (val == field.options[i].text) {
			return i;
		}
	}
	return -1;
}


//changes departure month when arrival month is changed
function inMonthChange( inM, outM, inD, outD, inY, outY ) {

	if ( !isBrowserSupp() ) {

		return;
	}

	var res = adjustDate(inM.options.selectedIndex, inD,inY);

	if ( res != 0 ) {
		outD.options.selectedIndex=0;
		outM.options.selectedIndex = inM.options.selectedIndex + 1;
	} else {
		outM.options.selectedIndex = inM.options.selectedIndex;
		outD.options.selectedIndex = inD.options.selectedIndex + 1;
	}

	// Set the out year to the same as the in year
	outY.options.selectedIndex = inY.options.selectedIndex;

	var currentMonth = ( new Date() ).getMonth();
    
	// If the in date is 12/31/?? set the out date to 01/01/??
	if ( ( inM.options.selectedIndex == 11 ) && ( inD.options.selectedIndex == 30 ) ) {
		outM.options.selectedIndex=0;
		outY.options.selectedIndex++;
	}

	// If the selected month is before the current month, increment the in year.
	if ( ( inM.options.selectedIndex < currentMonth ) && (inY.options.selectedIndex == 0 ) ) {

		// First make sure it is a valid selection
		if ( inY.selectedIndex < ( inY.options.length - 1 ) )
			inY.options.selectedIndex++;

		// Now set the out year to the same as the in year
		outY.options.selectedIndex = inY.options.selectedIndex;		
	}

	return;
}
	

//changes departure day when arrival day is changed
function inDayChange(inD, outD, inM, outM, inY, outY) 
{
if (!isBrowserSupp())
	{
 	return;
	}			
var Inmth = inM.options.selectedIndex;

var res =adjustDate(Inmth, inD, inY)
if (res != 0)
	{
	outD.options.selectedIndex=0;
	outM.options.selectedIndex=inM.options.selectedIndex + 1;
	}
else
	{
	outM.options.selectedIndex = inM.options.selectedIndex;
	outD.options.selectedIndex = inD.options.selectedIndex+1;
	}
outY.options.selectedIndex = inY.options.selectedIndex;
if ((inM.options.selectedIndex == 11) && (inD.options.selectedIndex == 30))
	{
	outM.options.selectedIndex=0;
	outY.options.selectedIndex++;
	}
return;
}
	

//changes departure year when arrival year is changed
function inYearChange(inY, outY, inM, outM, inD, outD) 
{
if (!isBrowserSupp()) 
	{
	return;			
	}

outY.options.selectedIndex = inY.options.selectedIndex;
adjustDate(inM.options.selectedIndex, inD,inY);
return;
}	

function outMonthChange(outM, outD, outY) {

	if ( !isBrowserSupp() ) {
		return;			
	}

	adjustDate( outM.options.selectedIndex, outD, outY );	

	return;	
}


function outDayChange(outD, outM, outY) 
{
if (!isBrowserSupp())
	{
	return;			
	}

adjustDate(outM.options.selectedIndex,outD,outY);		
return;	
}


function outYearChange(outY, outM, outD) 
{
if (!isBrowserSupp())
	{
	return;			
	}

adjustDate(outM.options.selectedIndex,outD,outY);		
return;	
}


//Calendar Section

//calculation functions
function nextMonth(month) 
{
if (month==11)
	{
	return 0;
	}
else
	{
	return (month+1);
	}
}


function prevMonth(month) 
{
var prevMonth = (month-1)
if (month==0)
	{
	prevMonth = 11;
	}
return prevMonth
}

//increments or decrements month when it goes past Jan or Dec
function changeYear(direction,month,year)
{
var theYear = year
if (direction=="next")
	{
	if (month == 11)
		{
		theYear = (year+1)
		}
	}
if (direction=="prev")
	{
	if (month == 0)
		{
		theYear = (year-1)
		}
	}
return theYear
}


//opens a new window for the calendar
function createCalendar(month,year,io) {

if (!isBrowserSupp()) {

	alert("Your browser appears to be outdated and does not support this feature.")
 	return;
	}
if (navigator.appVersion.indexOf("Mac",0) != -1)  {calendarWindow=window.open("","Calendar","width=230,height=265,resizable=yes,scrollbars=no"); 
	}
	else  {calendarWindow=window.open("","Calendar","width=230,height=345,resizable=yes,scrollbars=no");
  	}
var mthIdx = month.options.selectedIndex
// var mthVal = getMonthNumber(month.options[mthIdx].text)
var yearVal = year.options[year.options.selectedIndex].text
//call the function to populate the window
generateCalendar(calendarWindow,mthIdx,yearVal,io)
}


//generates the meat of the calendar
function generateCalendar(target,mthIdx,year,io) 
{
if (!isBrowserSupp())
	{
 	return;
	}	
var monthName = new Array ("January","February","March","April","May","June","July","August","September","October","November","December")

//begin table for calendar
target.document.open()
calendar = "<html><head><title>calendar</title></head><body bgcolor=ffffff>"
calendar +="<table border=0 cellspacing=0 cellpadding=4 width=200>"
calendar +="<tr valign=top>"

// var mthIdx = parseInt(month);
var endday = getDaysInMonth(mthIdx, year)

//month header
calendar +="<td colspan=7 align=center bgcolor=#C0C0C0>"
calendar +="<b><font face='Helvetica,Arial,Futura'>" + monthName[mthIdx] + " " + year + "</font></b></td></tr>"

//writes in the day of the week labels
calendar +="</tr><tr align=center>"
calendar +="<td width=10><font face='Helvetica,Arial,Futura' color='#FF0000'>&nbsp;<b>S</b></font></td>"
calendar +="<td width=10><font face='Helvetica,Arial,Futura'>&nbsp;<b>M</b></font></td>"
calendar +="<td width=10><font face='Helvetica,Arial,Futura'>&nbsp;<b>T</b></font></td>"
calendar +="<td width=10><font face='Helvetica,Arial,Futura'>&nbsp;<b>W</b></font></td>"
calendar +="<td width=10><font face='Helvetica,Arial,Futura'>&nbsp;<b>T</b></font></td>"
calendar +="<td width=10><font face='Helvetica,Arial,Futura'>&nbsp;<b>F</b></font></td>"
calendar +="<td width=10><font face='Helvetica,Arial,Futura' color='#FF0000'>&nbsp;<b>S</b></font></td>"
calendar +="</tr>"

// month = mthIdx + 1;
// wholeDate = month + "/01/" + year
// thedate = new Date(wholeDate)
thedate = new Date (year, mthIdx, 1);
firstDay = thedate.getDay()

selectedmonth = mthIdx;
var today = new Date();
var thisyear = today.getYear() + 1900;
//adb selectedyear = document.resrvForm.inyear.options.selectedIndex
selectedyear = year

var lastDay = (endday + firstDay+1)

calendar +="<tr>"
for (var i = 1; i < lastDay; i++)
	{
	if (i <= firstDay)
		{
		// 'empty' boxes prior to first day
		calendar +="<td>&nbsp;</td>"
		}
	else 
		{
		// enter date number
		calendar +="<td align=center><a href='JavaScript:self.close();opener.closeCalendar"+io+"("+(i-firstDay) + ")'><font color='#FF0000'> "+(i-firstDay)+"</font></a></td>"
		}
	//must start new row after each week
	if (i % 7 == 0 &&  i != lastDay)
		{
		calendar +="</tr><tr>"
		}
	}
calendar +="</tr>"

//separator line
calendar +="<tr><td colspan=7 align=center width=200><hr noshade></td></tr>"

//next month and previous month buttons
var goPrevMonth = prevMonth(mthIdx)
var goNextMonth = nextMonth(mthIdx)
var nextYear = changeYear("next",mthIdx,parseInt(year))
var prevYear = changeYear("prev",mthIdx,parseInt(year))

if(navigator.userAgent.indexOf('MSIE',0) != -1)
	{
	calendar +="<tr><td align=left colspan=3 bgcolor=#A7CCDC><a href='javascript:opener.generateCalendar(self,"+goPrevMonth+","+prevYear+",&quot;"+io+"&quot;)'>prev</a></td>"
	calendar +="<td align=center colspan=1 bgcolor=#A7CCDC>&nbsp;</td>"
	calendar +="<td align=right colspan=3 bgcolor=#A7CCDC><a href='javascript:opener.generateCalendar(self,"+goNextMonth+","+nextYear+",&quot;"+io+"&quot;)'>next</a></td></tr>"
	calendar +="</table></body></html>"
	target.document.close()
	}
else
	{
	calendar +="<form><tr><td align=left colspan=3 bgcolor=#A7CCDC><input type=button value=' < '"+"onClick='document.clear();opener.generateCalendar(opener.calendarWindow,\"+goPrevMonth+\",\"+prevYear+\",&quot;\"+io+\"&quot;)'></td>"
	calendar +="<td align=center colspan=1 bgcolor=#A7CCDC>&nbsp;</td>"
	calendar +="<td align=right colspan=3 bgcolor=#A7CCDC><input type=button value=' > '"+"onClick='document.clear();opener.generateCalendar(opener.calendarWindow,\"+goNextMonth+\",\"+nextYear+\",&quot;\"+io+\"&quot;)'></td></tr></form>"
	calendar +="</table></body></html>"
	}
target.document.write(calendar);
target.document.close()	
}

//changes date when a date is clicked
function closeCalendarIn(day)
{
var arrMonthField = document.resrvForm.inmonth;
var arrDayField   = document.resrvForm.inday;
var arrYearField  = document.resrvForm.inyear;
var depMonthField = document.resrvForm.outmonth;
var depDayField   = document.resrvForm.outday;
var depYearField  = document.resrvForm.outyear;

var yrIdx = getIndex (selectedyear,arrYearField );

arrMonthField.options.selectedIndex=selectedmonth;
arrYearField.options.selectedIndex= yrIdx;
arrDayField.options.selectedIndex=parseInt(day) - 1;
var res = adjustDate(selectedmonth, arrDayField, arrYearField); 
if (res != 0)
	{
	depDayField.options.selectedIndex=0;
	depMonthField.options.selectedIndex = arrMonthField.options.selectedIndex+1;
	}
else 
	{
	depMonthField.options.selectedIndex = arrMonthField.options.selectedIndex;
	depDayField.options.selectedIndex = arrDayField.options.selectedIndex+1;
	}
depYearField.options.selectedIndex = arrYearField.options.selectedIndex;
if ((arrMonthField.options.selectedIndex == 11) && (arrDayField.options.selectedIndex == 30))
	{
	depMonthField.options.selectedIndex=0;
	depYearField.options.selectedIndex++;
	}
return;
}

function closeCalendarOut(day)
{
var depMonthField = document.resrvForm.outmonth;
var depDayField   = document.resrvForm.outday;
var depYearField  = document.resrvForm.outyear;

var yrIdx = getIndex (selectedyear,depYearField );
depMonthField.options.selectedIndex=selectedmonth;
depYearField.options.selectedIndex=yrIdx;
depDayField.options.selectedIndex=parseInt(day) - 1;
var res = adjustDate(selectedmonth, depDayField, depYearField); 
}