function year(y) { // some browsers return year - 1900, others don't 
    return (y<1000) ? y + 1900 : y;
}


kMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
function month(m) { // returns human readable month based on int input, 0==january, etc.
    return(kMonths[m]);
}


function daysInMonth(m,y) {
    if(m==2) {
        // February has 29 days in any year evenly divisible by four,
        // EXCEPT for centurial years which are not also divisible by 400.
        return (((y % 4 == 0) && ( (!(y % 100 == 0)) || (y % 400 == 0))) ? 29 : 28);	   
    }
    else if(m==4 || m==6 || m==9 || m==11) return 30;
    else return 31;
}


// populate year and month and date selection
function populateDateSelection() {	                    
	today = new Date();            

    // year and date
    d = new Date(year(today.getYear()), today.getMonth(), 1);    
    for(i=0; i<24; i++) {	            
        // human readable options
        sm = month(d.getMonth());
        sy = year(d.getYear());
                        
        // values for the option
        vm = d.getMonth()+1;
        vy = year(d.getYear());
                
        // set option -- option syntax will be mm,yy
        document.bookingForm.arrivalMonthYear.options[i] = new Option(sm + " " + sy,vm+","+vy);
        
        d.setMonth(d.getMonth()+1);            
    }
    
    // days
	tm = today.getMonth()+1;
	ty = today.getYear();
	ty = year(ty);
    maxDays = daysInMonth(tm,ty);
     
    for(i=1; i<=maxDays; i++) {
        document.bookingForm.arrivalDate.options[i-1] = new Option(i,i);
    }
    theDate = today.getDate();
    document.bookingForm.arrivalDate.selectedIndex = theDate-1;
}


// redo Date
function checkDate() {

	// in dates
    my = document.bookingForm.arrivalMonthYear.value.split(","); // month, year            
    im = my[0]; // in month
    iy = my[1]; // in year
    id = document.bookingForm.arrivalDate.value; // in date

	// current dates
	today = new Date();
	tm = today.getMonth()+1;
	ty = today.getYear();
	ty = year(ty);
	td = today.getDate()-1;
	
	// same month, year - is date less?
	if (iy==ty && im==tm && id<=td) {
		document.bookingForm.arrivalDate.selectedIndex = td;
	}
}

// redo Month & Year
function checkMonthYear() {
	
	// in dates
    my = document.bookingForm.arrivalMonthYear.value.split(","); // month, year            
    im = my[0]; // in month
    iy = my[1]; // in year
    id = document.bookingForm.arrivalDate.value; // in date
    
    // redo dates
    maxDays = daysInMonth(im,iy);
    document.bookingForm.arrivalDate.length=0;
    for(i=1; i<=maxDays; i++) {
        document.bookingForm.arrivalDate.options[i-1] = new Option(i,i);
    }
    
    // is date no longer available?
    if (id > maxDays) {
    	document.bookingForm.arrivalDate.selectedIndex = maxDays-1;
    }
    else document.bookingForm.arrivalDate.selectedIndex = id-1;
    
    checkDate();
}


// send form
function sendForm() {

	// in dates
    my = document.bookingForm.arrivalMonthYear.value.split(","); // month, year            
    im = my[0]; // in month
    iy = my[1]; // in year
    id = document.bookingForm.arrivalDate.value; // in date
	
	ni = parseInt(document.bookingForm.nights.value);
	ad = parseInt(document.bookingForm.adults.value);
	ch = parseInt(document.bookingForm.children.value);
	
	// check rooms
	if (ad==1 || ad==2) ro=1;
	if (ad==3 || ad==4) ro=2;
	if (ad==5) ro=3;
	
	hotelid = parseInt(document.bookingForm.hotel.value);
	

myURL = "https://reservations.ihotelier.com/istay.cfm?hotelid=" + hotelid + "&languageid=1";

myURL += "&DateIn=" + im + "/" + id + "/" + iy ;

myURL += "&Length=" + ni + "&Rooms=" + ro + "&Adults=" + ad + "&Children=" + ch ;

extern=window.open(myURL,'_blank','dependent=no,locationbar=no,menubar=no,resizable=yes,width=1024,height=610,scrollbars=yes,status=no');

extern.focus();

}


// cancel or modify
function cancelIt() {
myURL = "https://reservations.ihotelier.com/istay.cfm?hotelid=" + hotelid + "&languageid=1";
extern=window.open(myURL,'_blank','dependent=no,locationbar=no,menubar=no,resizable=yes,width=1024,height=610,scrollbars=yes,status=no');
extern.focus();
}

