/**
 * Calendar Utility Functions
 * @author Gourav Budhia
 * @version $Header: @(#) /ccviews/genesis_ua_fd/genesis_ua/Source/Docroot/js/calendar_utils.js@@/main/1  2004-05-20 12:43:56 GMT  BudhiaG $
 * @created 05/20/2004
 */

function openCalendarCheckIn(theForm, brand, region, locale, secure)
{
	var sitename = "http://www.ichotelsgroup.com/"
	var cDate = theForm.arrivalDay[theForm.arrivalDay.selectedIndex].value;
	if(cDate == null || cDate == ""){
	    cDate = "-1";
	}
	var cMonthYear = theForm.arrivalMonthYear[theForm.arrivalMonthYear.selectedIndex].value;
	var url = sitename + '/h/d/' + brand + '/' + region + '/' + locale + '/calendar?test=grb&openerForm=' + theForm.name + '&monthYear=' + cMonthYear + '&date=' + cDate + '&checkOut=false&secure=' + secure;
//	alert(url);
	if(navigator.userAgent.indexOf("AOL 6.0") != -1 || navigator.userAgent.indexOf("AOL 5.0") != -1) {
		newWin = window.open(url,'HI','DEPENDANT=YES,WIDTH=225,HEIGHT=230,TITLEBAR=YES,MENUBAR=NO,SCROLLBARS=NO,TOP=0,LEFT=0');
	} else {
		var topPos = (screen.availHeight/2)-115;
		var leftPos = (screen.availWidth/2)-105;
		newWin = window.open(url,'cal','top=' + topPos + ',left=' + leftPos + ',dependent=yes,width=225,height=230,screenX=' + leftPos + ',screenY=' + topPos + ',titlebar=yes');
	}
	newWin.focus();
	return false;
}

function openCalendarCheckOut(theForm, brand, region, locale, secure)
{
	var cDate = theForm.departureDay[theForm.departureDay.selectedIndex].value;
	if(cDate == null || cDate == ""){
	    cDate = "-1";
	}
	var cMonthYear = theForm.departureMonthYear[theForm.departureMonthYear.selectedIndex].value;
	var url = '/h/d/' + brand + '/' + region + '/' + locale + '/calendar?test=grb&openerForm=' + theForm.name + '&monthYear=' + cMonthYear + '&date=' + cDate + '&checkOut=true&secure=' + secure;
	if(navigator.userAgent.indexOf("AOL 6.0") != -1 || navigator.userAgent.indexOf("AOL 5.0") != -1) {
		newWin = window.open(url,'HI','DEPENDANT=YES,WIDTH=225,HEIGHT=230,TITLEBAR=YES,MENUBAR=NO,SCROLLBARS=NO,TOP=0,LEFT=0');
	} else {
		var topPos = (screen.availHeight/2)-115;
		var leftPos = (screen.availWidth/2)-105;
		newWin = window.open(url,'cal','top=' + topPos + ',left=' + leftPos + ',dependent=yes,width=225,height=230,screenX=' + leftPos + ',screenY=' + topPos + ',titlebar=yes');
	}
	newWin.focus();
	return false;
}


function populateCheckOut(theForm)
{
	if(theForm.arrivalDay.selectedIndex != 0 && theForm.arrivalMonthYear.selectedIndex != 0) {
		if ((theForm.departureDay.selectedIndex == 0) ||
			(theForm.departureMonthYear.selectedIndex == 0) ||
			(theForm.departureMonthYear.selectedIndex < theForm.arrivalMonthYear.selectedIndex) ||
			((theForm.departureMonthYear.selectedIndex == theForm.arrivalMonthYear.selectedIndex) &&
			 (theForm.departureDay.selectedIndex <= theForm.arrivalDay.selectedIndex))) {
			var monthYear= theForm.arrivalMonthYear.value;
			var month = monthYear.substring(0,monthYear.length-4);
			var year = monthYear.substring(monthYear.length-4);
			var date = theForm.arrivalDay.selectedIndex;
			var outDateObj = new Date(year, month, date+1);


			var year = outDateObj.getYear();
			if(year.toString().length == 3)
				year = 2000 + (outDateObj.getYear() % 100);
			theForm.departureMonthYear.value = "" + outDateObj.getMonth() + year;

			//We have set the departureMonthYear, now see how to set the date field
			if((theForm.departureMonthYear.selectedIndex != theForm.arrivalMonthYear.selectedIndex) ||
				(outDateObj.getDate() > theForm.departureDay.selectedIndex)) {
				theForm.departureDay.selectedIndex = outDateObj.getDate();
			}
		}
	}
}
// new calendar model below

function openFPCalendarCheckOutAdvanced(theForm, brand, region, locale, secure)
{
	var cFullDate = theForm.checkOutFullDate.value;
	var dateArray = cFullDate.split("../index.html");
	var cDate = dateArray[1];
	if(cDate == null || cDate == "dd" || cDate == ""){
	    cDate = "-1";
	}
	var cMonthYear = dateArray[0] + dateArray[2];
	if(cMonthYear == null || cMonthYear == "mmyyyy" || cMonthYear == ""){
	    cMonthYear = "-1";
	    }
	else {
	   cMonthYear = (dateArray[0] - 1) + dateArray[2];   	
	}
	document.getElementById('AdvancedCheckInCalendar').style.visibility = "hidden";
	document.getElementById('AdvancedCheckOutCalendar').style.visibility = "visible";
	document.getElementById('age_room_set').style.visibility = "hidden";
	document.getElementById('roomprefselect').style.visibility = "hidden";
	document.getElementById('smokingprefselect').style.visibility = "hidden";
	document.getElementById('wheelCheck').style.visibility = "hidden";
	document.getElementById('wheelCheckImg').style.visibility = "hidden";
	if (document.getElementById('roomrateselect')) {
	document.getElementById('roomrateselect').style.visibility = "hidden";
		}
	var url = ("/h/d/" + brand + "/" + region + "/" + locale + "/fpcal?test=grb&openerForm=" + theForm.name + '&monthYear=' + cMonthYear + '&date=' + cDate + '&checkOut=true&secure=' + secure);
	document.getElementById('AdvancedCheckOutCalendar').innerHTML = "<iframe SRC=" + url + " WIDTH=\"266\" HEIGHT=\"133\" scrolling=\"no\" FRAMEBORDER=0 style=\"border: '1px solid'\">Your browser does not support the iframe tag.  Please consider upgrading your browser.</iframe>";
	return false;
}

function openFPCalendarCheckInAdvanced(theForm, brand, region, locale, secure)
{
	var cFullDate = theForm.checkInFullDate.value;
	if (!cFullDate) cFullDate = "mm/dd/yyyy";
	var dateArray = cFullDate.split("../index.html");
	var cDate = dateArray[1];
	if(cDate == null || cDate == "dd" || cDate == ""){
	    cDate = "-1";
	}
	var cMonthYear = dateArray[0] + dateArray[2];
	if(cMonthYear == null || cMonthYear == "mmyyyy" || cMonthYear == ""){
	    cMonthYear = "-1";
	}
	else {
	   cMonthYear = (dateArray[0] - 1) + dateArray[2];   	
	}
	document.getElementById('AdvancedCheckOutCalendar').style.visibility = "hidden";
	document.getElementById('AdvancedCheckInCalendar').style.visibility = "visible";
	document.getElementById('checkOutTextBox').style.visibility = "hidden";
	document.getElementById('age_room_set').style.visibility = "hidden";
	document.getElementById('roomprefselect').style.visibility = "hidden";
	document.getElementById('smokingprefselect').style.visibility = "hidden";
	document.getElementById('wheelCheck').style.visibility = "hidden";
	document.getElementById('wheelCheckImg').style.visibility = "hidden";
	var url = ("/h/d/" + brand + "/" + region + "/" + locale + "/fpcal?test=grb&openerForm=" + theForm.name + '&monthYear=' + cMonthYear + '&date=' + cDate + '&checkOut=false&secure=' + secure);
	document.getElementById('AdvancedCheckInCalendar').innerHTML = "<iframe SRC=" + url + " WIDTH=\"266\" HEIGHT=\"133\" scrolling=\"no\" FRAMEBORDER=0 style=\"border: '1px solid'\">Your browser does not support the iframe tag.  Please consider upgrading your browser.</iframe>";
	return false;
}
function openFPCalendarCheckIn(theForm, brand, region, locale, secure)
{
	var cFullDate = theForm.checkInFullDate.value;
	if (!cFullDate) cFullDate = "mm/dd/yyyy";
	var dateArray = cFullDate.split("../index.html");
	var cDate = dateArray[1];
	if(cDate == null || cDate == "dd" || cDate == ""){
	    cDate = "-1";
	}
	var cMonthYear = dateArray[0] + dateArray[2];
	if(cMonthYear == null || cMonthYear == "mmyyyy" || cMonthYear == ""){
	    cMonthYear = "-1";
	}
	else {
	   cMonthYear = (dateArray[0] - 1) + dateArray[2];   	
	}
	document.getElementById('quickResCheckOutCalendar').style.visibility = "hidden";
	document.getElementById('quickResCheckInCalendar').style.visibility = "visible";
	document.getElementById('quickResCheckInCalendar').zIndex = 5;
	var url = ("/h/d/" + brand + "/" + region + "/" + locale + "/fpcal?test=grb&openerForm=" + theForm.name + "&monthYear=" + cMonthYear + "&date=" + cDate + "&checkOut=false&secure=" + secure);
	document.getElementById('quickResCheckInCalendar').innerHTML = "<iframe SRC=" + url + " WIDTH=\"266\" HEIGHT=\"133\" scrolling=\"no\" FRAMEBORDER=0 style=\"border: '1px solid'\">Your browser does not support the iframe tag.  Please consider upgrading your browser.</iframe>";
	//document.getElementById('checkInTextBox').focus();
	return false;
}
    
function openFPCalendarCheckOut(theForm, brand, region, locale, secure)
{
	var cFullDate = theForm.checkOutFullDate.value;
	var dateArray = cFullDate.split("../index.html");
	var cDate = dateArray[1];
	if(cDate == null || cDate == "dd" || cDate == ""){
	    cDate = "-1";
	}
	var cMonthYear = dateArray[0] + dateArray[2];
	if(cMonthYear == null || cMonthYear == "mmyyyy" || cMonthYear == ""){
	    cMonthYear = "-1";
	    }
	else {
	   cMonthYear = (dateArray[0] - 1) + dateArray[2];   	
	}
	document.getElementById('quickResCheckInCalendar').style.visibility = "hidden";
	document.getElementById('quickResCheckOutCalendar').style.visibility = "visible";
	var url = ("/h/d/" + brand + "/" + region + "/" + locale + "/fpcal?test=grb&openerForm=" + theForm.name + "&monthYear=" + cMonthYear + "&date=" + cDate + "&checkOut=true&secure=" + secure);
	document.getElementById('quickResCheckOutCalendar').innerHTML = "<iframe SRC=" + url + " WIDTH=\"266\" HEIGHT=\"133\" scrolling=\"no\" FRAMEBORDER=0 style=\"border: '1px solid'\">Your browser does not support the iframe tag.  Please consider upgrading your browser.</iframe>";
	return false;
}

function populateCheckIn(fulldate)
{
	document.getElementById('checkInTextBox').value = fulldate;
	fieldAgreement(fulldate, 'in');
	calendarExit();

}

function populateCheckOutAdv(fulldate)
{
	document.getElementById('checkOutTextBox').value = fulldate;
	fieldAgreement(fulldate,'out');
	calendarExit();
}

function fieldAgreement(fulldate,alpha) {
	var date1 = new Date();
	var date2 = new Date();
	diff  = new Date();

	checkInValue = document.getElementById('checkInTextBox').value;
	checkOutValue = document.getElementById('checkOutTextBox').value;
	nightsValue = document.getElementById('number_Nights').selectedIndex + 1;
	
if (checkInValue == "mm/dd/yyyy") { checkInValue = ""; }
if (checkOutValue == "mm/dd/yyyy") { checkOutValue = ""; }

if (checkInValue != '' && checkOutValue != '') { // to set the nights field if both checkIn & checkOut dates have been selected	

		date1temp = new Date(checkInValue);
		date1.setTime(date1temp.getTime());

		date2temp = new Date(checkOutValue);
		date2.setTime(date2temp.getTime());

	diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
	timediff = diff.getTime();

	if (date1.getTime() < date2.getTime()) {
	days = Math.floor(timediff / ((1000 * 60 * 60 * 24) + (1000 * 60 * 60 * 2))); 
	//the above is (# of milliseconds in a second * # of seconds in a minute * # of minutes in an hour * # of hours in a day) 
	//the adding of (1000 * 60 * 60 * 2), or 2 hours, means that the system time would count from 2am instead of midnight... thereby accounting for DST
	timediff -= days * (1000 * 60 * 60 * 24);
	document.getElementById('number_Nights').selectedIndex= days;
		}
		else {
		document.getElementById('number_Nights').selectedIndex = -1;
		}
	return false; 
} 
else if (checkInValue != '' && checkOutValue == '') { // if only the checkIn date has been chosen, add the # of nights & populate the checkOut date
		date1temp = new Date(checkInValue);
		date1.setTime(date1temp.getTime());
	diff.setTime(Math.floor(date1.getTime() + ((1000 * 60 * 60 * 24) + (1000 * 60 * 60 * 2)) * nightsValue));
	var yr = diff.getFullYear();
	var dt = diff.getDate();
	var mo = diff.getMonth() + 1;
	if (mo <= 9) { mo = '0' + mo; }
	if (dt <= 9) { dt = '0' + dt; }
	document.getElementById('checkOutTextBox').value = (mo + "/" + dt + "/" + yr);
//	else return false; // otherwise exits
	
	}
else if (checkInValue == '' && checkOutValue != '') { // if only the checkOut date has been chosen, subtract the # of nights & populate the checkIn date
		date2temp = new Date(checkOutValue);
		date2.setTime(date2temp.getTime());
	diff.setTime(Math.floor(date2.getTime() - ((1000 * 60 * 60 * 24) + (1000 * 60 * 60 * 2)) * nightsValue));
	var yr = diff.getFullYear();
	var dt = diff.getDate();
	var mo = diff.getMonth() + 1;
	if (mo <= 9) { mo = '0' + mo; }
	if (dt <= 9) { dt = '0' + dt; }
	document.getElementById('checkInTextBox').value = (mo + "/" + dt + "/" + yr);
//	else return false; // otherwise exits
	
	}
	}
function fieldAgreementNights(value) {
	var nights = value;
	var date1 = new Date();
	var date2 = new Date();
	diff  = new Date();

	checkInValue = document.getElementById('checkInTextBox').value;
	checkOutValue = document.getElementById('checkOutTextBox').value;
	
	if (checkInValue == "mm/dd/yyyy") { checkInValue = ""; }
	if (checkOutValue == "mm/dd/yyyy") { checkOutValue = ""; }
	
	if (checkInValue != '') { 	
			date1temp = new Date(checkInValue);
			date1.setTime(date1temp.getTime());
		nights = nights * ((1000 * 60 * 60 * 24) + (1000 * 60 * 60 * 2)); //convert to milliseconds by multiplying value x 1000 x seconds x minutes x hours
		diff.setTime(Math.abs(date1.getTime() + nights ));
		var yr = diff.getFullYear();
		var dt = diff.getDate();
		var mo = diff.getMonth() + 1;
		if (mo <= 9) { mo = '0' + mo; }
		if (dt <= 9) { dt = '0' + dt; }
		document.getElementById('checkOutTextBox').value = (mo + "/" + dt + "/" + yr);

		return false; 
		} 

	}

function calendarExit () {
   if(document.getElementById('AdvancedCheckOutCalendar')) 
        {
	document.getElementById('AdvancedCheckOutCalendar').style.visibility = "hidden";
	document.getElementById('AdvancedCheckInCalendar').style.visibility = "hidden";
	document.getElementById('nights').style.visibility = "visible";
	document.getElementById('checkOutTextBox').style.visibility = "visible";
	document.getElementById('age_room_set').style.visibility = "visible";
	document.getElementById('roomprefselect').style.visibility = "visible";
	document.getElementById('smokingprefselect').style.visibility = "visible";
	document.getElementById('wheelCheck').style.visibility = "visible";
	document.getElementById('wheelCheckImg').style.visibility = "visible";
	if (document.getElementById('roomrateselect')) {
		document.getElementById('roomrateselect').style.visibility = "visible";
		}
	} 
    else 
	{
	document.getElementById('quickResCheckOutCalendar').style.visibility = "hidden";
	document.getElementById('quickResCheckInCalendar').style.visibility = "hidden";
	document.getElementById('quickResCheckOut').style.visibility = "visible";
	}
    }
