//Copyright (C) 2003, MetroGuide.com, Inc. All Rights Reserved.
var disableEvents = false;
var today = new Date(new Date().valueOf());
var i_m,i_d,i_y, o_m, o_d, o_y,dateForm,currentCalendar, isNightsPicker = false;  

function y_2k(number){return (number < 1000) ? number + 1900 : number;}



function createYearOptions()
{
var currentYear = new Date().getYear(); 
document.write ('<option selected value="' + currentYear + '" >' + currentYear + '</option>\n');
document.write ('<option value="' + (currentYear + 1) + '">' + (currentYear + 1) + '</option>\n');
return;
}


function changeYearBase()
{
 var val = i_y.options[i_y.options.selectedIndex].value;
i_y.options[i_y.options.selectedIndex].value  = '0' + (parseInt(val)  - 2000).toString(); 
 
return true;
}




function set_Dates(daysInAdvance, im, id, iy, om, od, oy, formName)
{
 
  // This will populate the date dropdowns with today and tomorrow's values.
 
   
isNightsPicker = ((om==null) || (od==null) || (oy==null));

dateForm = eval('document.' + formName);

i_m = eval('dateForm.' + im);
i_d = eval('dateForm.' + id);
i_y = eval('dateForm.' + iy);

if (!isNightsPicker)
{
o_m = eval('dateForm.' + om);
o_d = eval('dateForm.' + od);
o_y = eval('dateForm.' + oy);
}

 


  var yearOffset = parseInt(i_y.options[0].value, 10);
  // getDate
 var tomorrow = new Date((new Date().valueOf() + (24*60*60*1000) * daysInAdvance));
  var inMonth=tomorrow.getMonth();
  var inDay=tomorrow.getDate();
  var inYear=y_2k(tomorrow.getYear());

  if(is_Leap_Year(inYear)) { var days = new Array(31,29,31,30,31,30,31,31,30,31,30,31); }
  else { var days = new Array(31,28,31,30,31,30,31,31,30,31,30,31); }
if(!isNightsPicker)
	{
 var  outMonth=inMonth;
 var outDay = inDay%days[inMonth];
  var outYear=inYear;
  if(outDay == 0) { outMonth = (inMonth + 1) % 12; }
  if(outDay == 0 && inMonth == 11) { outYear++; }
	}
  // Now set the select boxes to the appropriate dates:
 
  i_m.options[inMonth].selected=true; 
  i_d.options[(inDay-1)].selected=true; 
  i_y.options[(inYear-yearOffset)].selected=true; 
if(!isNightsPicker)
	{
  o_m.options[outMonth].selected=true; 
  o_d.options[outDay].selected=true; 
  o_y.options[(outYear-yearOffset)].selected=true; 
	}
}
 
 


 

function checkDateRules ()

{

///return true;  /////// TEST


if (!isBrowserSupp())
	{
 	return true;
	}
    var frm = frm_Search
	var inMth = frm.inmonth.options.selectedIndex + 1; 
	var inDay =  frm.inday.options.selectedIndex + 1;
	var inYear =  frm.inyear.options(frm.inyear.options.selectedIndex).value; 
	var outMth = frm.outmonth.options.selectedIndex +1 ;
	var outDay = frm.outday.options.selectedIndex +1 ;
	var outYear = frm.outyear.options(frm.outyear.options.selectedIndex).value;
 
var today = new Date();
var inDate = new Date  (inMth + "/" + inDay + "/" + inYear);
var outDate = new Date (outMth + "/" + outDay + "/" + outYear);

if (outDate <= inDate)
{
	alert('Your Check-out Date must be at least one day after your Check-in Date.');
	return false;
}
if (DateDiff( today, inDate, 'd') > 320)
{
	alert('Reservations cannot be made more than 320 days in advance.');
	return false;
}
if (DateDiff( inDate, outDate, 'd') > 30)
{
	alert('Reservations cannot be made for stays of more than 30 nights.');
	return false;
}


}


 
 
 
 
 
 
 

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;
		}

	return true;
	}


function OpenInfo(file,input) 
	{
  	window.open(file,'PropInfo','scrollbars=1,width=536,height=480,resizable=1,toolbar=1');
	}


function is_Leap_Year(yr)
{
  if (((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0)) { return true; }
  else { return false; } 
}



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);
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;
}

 
 
//changes departure month when arrival month is changed
function amadChange() 
{
if (!isBrowserSupp())
	{
 	return;
	}
if (disableEvents) return;
var d  = new Date(); 
today = new Date ((d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getYear());
//if the date would be in the past, make it next year
if (Date.parse((i_m.options.selectedIndex+1) + "/" + (i_d.options.selectedIndex+1) + "/" + i_y.options[i_y.options.selectedIndex].value) < today)
	{
	i_y.options.selectedIndex=i_y.options.selectedIndex + 1;
	o_y.options.selectedIndex=i_y.options.selectedIndex + 1;
	}

var res = adjustDate(i_m.options.selectedIndex, i_d, i_y);
if (res != 0)
	{
	       o_d.options.selectedIndex=0;
	       if (i_m.options.selectedIndex==11){
			o_m.options.selectedIndex=0;
			o_y.options.selectedIndex=i_y.options.selectedIndex + 1;
	       }
	       else{
			o_m.options.selectedIndex=i_m.options.selectedIndex + 1;
			o_y.options.selectedIndex=i_y.options.selectedIndex;
	       }
	}
else 
	{
	o_m.options.selectedIndex = i_m.options.selectedIndex;
	o_d.options.selectedIndex = i_d.options.selectedIndex+1;
	o_y.options.selectedIndex= i_y.options.selectedIndex;
	}

//adjustNights(dayCount,inM,inD,inY,outM,outD,outY); 
return;
}
 
function adjustNights(dayCount, inM,inD,inY,outM,outD,outY) 
{
if (!isBrowserSupp())
	{
 	return;
	}

if (dayCount == null) return;

var nights; 
var inMth = i_m.options.selectedIndex + 1;
var inYear = i_y.options(inY.options.selectedIndex).value;
var outMth = o_m.options.selectedIndex + 1; 
var outYear = o_y.options(outY.options.selectedIndex).value;
var outDay = o_d.options.selectedIndex + 1; 
var inDay = i_d.options.selectedIndex  +  1;
var inDate = new Date (inMth + '/' + inDay + '/' +  inYear);
var outDate = new Date (outMth + '/' + outDay + '/' +  outYear);
nights = parseInt(DateDiff( inDate, outDate, 'D'));

if (nights  > 30) 
	{
	disableSubmit = true;
    dayCount.options.selectedIndex = 0;
	}
else
	{
	disableSubmit = false;	
    dayCount.options.selectedIndex = nights - 1;
	}
return;
}


function dmddChange() 
{
if (!isBrowserSupp())
	{
	return;			
	}
if (disableEvents) return;
 
var today = new Date();
//if the date would be in the past, make it next year
if (Date.parse((o_m.options.selectedIndex+1) + "/" + (o_d.options.selectedIndex+1) + "/" + o_y.options[o_y.options.selectedIndex].value) < today)
	{
	o_y.options.selectedIndex=o_y.options.selectedIndex + 1;
	}

 adjustDate(o_m.options.selectedIndex,o_d,o_y);	
//adjustNights(dayCount,inM,inD,inY,outM,outD,outY); 
return;	
}

 


function OpenCalendar(c)
	{
 
    var filename ="calendar.html";
	var formname = dateForm.name; 
    var d,m,y;
    
    currentCalendar = c;
    
    
	switch (parseInt(c))
	{
	case 0:
 
     d = i_d.options[i_d.selectedIndex].value;
     m = i_m.options[i_m.selectedIndex].value - 1;
	 y = i_y.options[i_y.selectedIndex].value;
	 
	 break;

	case 1:

     d = o_d.options[o_d.selectedIndex].value;
	 m = o_m.options[o_m.selectedIndex].value - 1;
	 y = o_y.options[o_y.selectedIndex].value;
 
	}

	var ad = 'i_d';
	var am = 'i_m';
	var ay = 'i_y';
	var dd = 'o_d';
    var dm = 'o_m';
	var dy = 'o_y';
 
	filename += '?formName=' + formname + '&Cal=' + c;
	if (( ad != null ) || ( dd != null ))
		filename += '&ad=' + ad + '&dd=' + dd + '&d=' + d;
	if (( am != null ) || ( dm != null ))
		filename += '&am=' + am + '&dm=' + dm + '&m=' + m;
	if (( ay != null ) || ( dy != null ))
		filename += '&ay=' + ay + '&dy=' + dy + '&y=' + y;

	window.name='lydia';

// window.showModalDialog(filename, '','dialogWidth=350,dialogHeight=350,resizable=0')

 window.open(filename, "dates", 'width=350,height=350,resizable=0,status=no,location=no');

	}

 

function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	
	return returnDate;

}



function  DateDiff( start, end, interval)
	 
	 {
    var iOut = 0;
    var bufferA = Date.parse( start ) ;
    var bufferB = Date.parse( end ) ;
    var number = bufferB-bufferA ;
    
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
         
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
   
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
      
            break ;
        default:
      
        return null ;
    }
    
    return iOut ;
}

 
