/**
 * Calendar Utility Functions
 * @author Neeraj Vij
 * @version $Header: @(#) /ccviews/genesis_ua_fd/genesis_ua/Source/Docroot/js/gdCalender_utils.js
 * @created 06/06/2006
 */

function openFPCalendarCheckInGD(theForm, brand, region, locale, secure)
{
	var cFullDate = theForm.checkInFullDate.value;
	if (!cFullDate) cFullDate = "dd/mm/yyyy";
	var dateArray = cFullDate.split("/");
	var cDate = dateArray[0];
	if(cDate == null || cDate == "dd" || cDate == ""){
	    cDate = "-1";
	}
	var cMonthYear = dateArray[1] + dateArray[2];
	if(cMonthYear == null || cMonthYear == "mmyyyy" || cMonthYear == ""){
	    cMonthYear = "-1";
	}
	else {
	   cMonthYear = (dateArray[1] - 1) + dateArray[2];
	}
	document.getElementById('quickResCheckOutCalendar').style.visibility = "hidden";
	document.getElementById('quickResCheckInCalendar').style.visibility = "visible";
	 if(window.location.protocol.indexOf('https:')==0)
     {
		secure=true;
	 }
 	var url = ("/h/d/" + brand + "/" + region + "/" + locale + "/gdcal?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 openFPCalendarCheckOutGD(theForm, brand, region, locale, secure)
{
	var cFullDate = theForm.checkOutFullDate.value;
	var dateArray = cFullDate.split("/");
	var cDate = dateArray[0];
	if(cDate == null || cDate == "dd" || cDate == ""){
	    cDate = "-1";
	}
	var cMonthYear = dateArray[1] + dateArray[2];
	if(cMonthYear == null || cMonthYear == "mmyyyy" || cMonthYear == ""){
	    cMonthYear = "-1";
	    }
	else {
	   cMonthYear = (dateArray[1] - 1) + dateArray[2];
	}
	document.getElementById('quickResCheckInCalendar').style.visibility = "hidden";
	document.getElementById('quickResCheckOutCalendar').style.visibility = "visible";
	if(window.location.protocol.indexOf('https:')==0)
     {
		secure=true;
	 }
	var url = ("/h/d/" + brand + "/" + region + "/" + locale + "/gdcal?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 populateCheckInGD(fulldate)
{
	document.getElementById('checkInTextBox').value = fulldate;
	//document.getElementById('quickResCheckOut').style.visibility = "visible"
	//document.getElementById('quickResCheckOutCalendar').style.visibility = "hidden";
	//document.getElementById('quickResCheckInCalendar').style.visibility = "hidden";
	fieldAgreementGD(fulldate, 'in');
	calendarExitGD();

}

function populateCheckOutAdvGD(fulldate)
{
	document.getElementById('checkOutTextBox').value = fulldate;
	//document.getElementById('quickResCheckOut').style.visibility = "visible"
	//document.getElementById('quickResCheckOutCalendar').style.visibility = "hidden";
	//document.getElementById('quickResCheckInCalendar').style.visibility = "hidden";
	fieldAgreementGD(fulldate,'out');
	calendarExitGD();
}

function fieldAgreementGD(fulldate,alpha) {
	var date1 = new Date();
	var date2 = new Date();
	diff  = new Date();

	checkInValue = document.getElementById('checkInTextBox').value;
	checkOutValue = document.getElementById('checkOutTextBox').value;


if (checkInValue == "dd/mm/yyyy") { checkInValue = ""; }
if (checkOutValue == "dd/mm/yyyy") { checkOutValue = ""; }

if (checkInValue != '' && checkOutValue != '') { // to set the nights field if both checkIn & checkOut dates have been selected

		var dateArray = checkInValue.split("/");
		date1 = new Date();
		date1.setDate(dateArray[0]);
		date1.setMonth(dateArray[1]);
		date1.setFullYear(dateArray[2]);

		dateArray = checkOutValue.split("/");
		date2 = new Date();
		date2.setDate(dateArray[0]);
		date2.setMonth(dateArray[1]);
		date2.setFullYear(dateArray[2]);

	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,populate the checkOut date
	var dateArray = checkInValue.split("/");
	date1 = new Date(dateArray[1]+"/"+dateArray[0]+"/"+dateArray[2]);
	date1.setDate(parseInt(date1.getDate())+1); 
	var yr = date1.getFullYear(); 
	var dt = date1.getDate();
	var mo = date1.getMonth()+1;
	if (mo <= 9) { mo = '0' + mo; }
	if (dt <= 9) { dt = '0' + dt; }
	document.getElementById('checkOutTextBox').value = (dt + "/" + mo + "/" + yr);
//	else return false; // otherwise exits

	}
else if (checkInValue == '' && checkOutValue != '') { // if only the checkOut date has been chosen,  populate the checkIn date
	var dateArray = checkOutValue.split("/");
	date2 = new Date(dateArray[1]+"/"+dateArray[0]+"/"+dateArray[2]);
	date2.setDate(parseInt(date2.getDate())-1);
	var yr = date2.getFullYear();
	var dt = date2.getDate();
	var mo = date2.getMonth()+1;

	if (mo <= 9) { mo = '0' + mo; }
	if (dt <= 9) { dt = '0' + dt; }
	document.getElementById('checkInTextBox').value = (dt + "/" + mo + "/" + yr);
//	else return false; // otherwise exits

	}
	}
function fieldAgreementNightsGD(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 == "dd/mm/yyyy") { checkInValue = ""; }
	if (checkOutValue == "dd/mm/yyyy") { checkOutValue = ""; }

	if (checkInValue != '') {
		var dateArray = checkInValue.split("/");
		date1.setDate(dateArray[0]);
		date1.setMonth(dateArray[1]);
		date1.setFullYear(dateArray[2]);
		
		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();
		if (mo <= 9) { mo = '0' + mo; }
		if (dt <= 9) { dt = '0' + dt; }
		document.getElementById('checkOutTextBox').value = (dt + "/" + mo + "/" + yr);

		return false;
		}

	}

function calendarExitGD() {
	document.getElementById('quickResCheckOutCalendar').style.visibility = "hidden";
	document.getElementById('quickResCheckInCalendar').style.visibility = "hidden";
	//document.getElementById('quickResCheckOut').style.visibility = "visible";
	}
