/* ****************************************************
 Web Application: YHA Australia Web Site
 Type: JavaScript library                      
 
 Date: 16/08/2004
         
 ***** Legal Notice *****
 The code contained within this page is the Intellectual property
 of Red Square Productions Pty Ltd and licensed to YHA Australia Inc for 
 use only within the specified Web Application.
 
 
 This notice cannot be removed or altered.  
 8/7/2005	Pat		merged IE mac changes from live.
 30/08/07	PG		added dynamic number of nights management
****************************************************  */

/* pop-up calendar functions */
var currFormElement = null;
var currFormElementMonthYear = null;
var currDepartureElement = null;
var currNightsElement = null;
var bCalendarLoaded;
var bCalendarInLayer = false;

function openDateChooser(buttonImage, formElementDate, formElementMonthYear, oDepartureDateElement){
	if(!bCalendarLoaded){return false;}
	var oDateChooser = document.getElementById('dateChooser');

	if((buttonImage != null && formElementDate != null)){
		currFormElementDate = formElementDate;
		currFormElementMonthYear = formElementMonthYear;
		currDepartureElement = oDepartureDateElement;
		if (formElementDate.form.night != null)
		{
			currNightsElement = formElementDate.form.nights;
		}	
		buttonImage = eval('document.images.' + buttonImage);

		calLyrTop = buttonImage.offsetTop;
		calLyrLeft = buttonImage.offsetLeft - document.getElementById('dateChooser').width;

		if(bCalendarInLayer){
			var oParentLayer = document.getElementById(sCalendarParentLayer);
			calLyrTop = parseInt(calLyrTop) + parseInt(oParentLayer.style.top) + 'px';
			calLyrLeft = parseInt(calLyrLeft) + parseInt(oParentLayer.style.left)  +'px';
		}
		oDateChooser.style.top = parseInt(calLyrTop) + 'px';
		oDateChooser.style.left = parseInt(calLyrLeft) + 'px';
		oDateChooser.style.visibility = 'visible';
	}else{
		oDateChooser.style.visibility = (oDateChooser.style.visibility == 'visible')?'hidden':'visible';
	}

}
function openDateChooserExt(buttonImage, formElementDate, formElementMonthYear, oDepartureDateElement,oNightsElement){
	if(!bCalendarLoaded){return false;}
	var oDateChooser = document.getElementById('dateChooser');

	if((buttonImage != null && formElementDate != null)){
		currFormElementDate = formElementDate;
		currFormElementMonthYear = formElementMonthYear;
		currDepartureElement = oDepartureDateElement;
		currNightsElement = oNightsElement;
		buttonImage = eval('document.images.' + buttonImage);

		calLyrTop = buttonImage.offsetTop;
		calLyrLeft = buttonImage.offsetLeft - document.getElementById('dateChooser').width;

		if(bCalendarInLayer){
			var oParentLayer = document.getElementById(sCalendarParentLayer);
			calLyrTop = parseInt(calLyrTop) + parseInt(oParentLayer.style.top) + 'px';
			calLyrLeft = parseInt(calLyrLeft) + parseInt(oParentLayer.style.left)  +'px';
		}
		oDateChooser.style.top = parseInt(calLyrTop) + 'px';
		oDateChooser.style.left = parseInt(calLyrLeft) + 'px';
		oDateChooser.style.visibility = 'visible';
	}else{
		oDateChooser.style.visibility = (oDateChooser.style.visibility == 'visible')?'hidden':'visible';
	}

}


function setDateRange(start, end){
//alert('setDateRange('+start+', '+end+')');
	var oDateChooser = document.getElementById('dateChooser');
	var sURL = calendarURL + '?start=' + start + '&end=' + end;
	if(start != ''){
		sURL += '&year=' + start.split('/')[2] + '&month=' + start.split('/')[1];
	}
//alert(sURL + '&colourScheme=' + sColourScheme)
	oDateChooser.contentWindow.location = sURL + '&colourScheme=' + sColourScheme + '&calendarBaseURL=' + sCalendarBaseURL;
}

function selectDate(oFormElement, oTargetElement, oDepartureDateElement){
	var sSelectedMonth = oFormElement[oFormElement.selectedIndex].value;
	var nMonth = sSelectedMonth.split('/')[0];
	var nYear = sSelectedMonth.split('/')[1];
	var dtArrival = new Date(nYear, nMonth - 1, 1);
	var nSelectedDay = oTargetElement.selectedIndex;
	var nDaysInMonth = dtArrival.getDaysInMonth();
	var tDate;
	var oOption;
	var sOptionText;
	var sOptionValue;

	for(var i = oTargetElement.options.length - 1; i >= 0; i--){
		oTargetElement.options[i] = null;
	}

	for(var i = 1; i <= nDaysInMonth; i++){
		tDate = new Date(nYear, nMonth - 1, i);

		sOptionText = tDate.getDayOfWeekAsString().substr(0, 3) + ' ' + i;
		sOptionValue = i;

		if(document.createElement && (navigator.userAgent.toLowerCase().indexOf('mac') == -1)){
			oOption = document.createElement('option');
			oTargetElement.options.add(oOption);
			oOption.text = sOptionText;
			oOption.value = sOptionValue;
		}else{
			oOption = new Option(sOptionText, sOptionValue);
			oTargetElement.options[oTargetElement.length] = oOption;
		}

		if(i - 1 == nSelectedDay || nSelectedDay > nDaysInMonth){
//			oOption.selected = true;
			oTargetElement.selectedIndex = i - 1;
		}
	}

	if(oDepartureDateElement != null){
		populateDepartureDate(oDepartureDateElement, oTargetElement, oFormElement);
	}

}

function populateDepartureDate(oFormElement, oDateElement, oMonthYearElement){
	var sMonthYear = oMonthYearElement[oMonthYearElement.selectedIndex].value;
	var sDate = oDateElement[oDateElement.selectedIndex].value;
	var nSelectedDay = (oFormElement.form.nights != null) ? parseInt(oFormElement.form.nights[oFormElement.form.nights.selectedIndex].value) - 1 : 0;
	var dtArrival = new Date(parseInt(sMonthYear.split('/')[1]), parseInt(sMonthYear.split('/')[0]) - 1, parseInt(sDate));
	var nDays = oFormElement.length;
	var tDate = dtArrival;
	var sOptionText;
	var sOptionValue;

	for(var i = oFormElement.options.length - 1; i >= 0; i--){
		oFormElement.options[i] = null;
	}
	for(var i = 0; i < nDays; i++){
		tDate.setDate(tDate.getDate() + 1);
/*
		oOption = document.createElement('option');
		oFormElement.options.add(oOption);
		oOption.text = tDate.getDayOfWeekAsString().substr(0, 3) + ' ' + tDate.getDate() + ' ' + tDate.getShortMonthName() + ', ' + tDate.getFullYear();
		oOption.value = tDate.getDate() + '/' + (tDate.getMonth() + 1) + '/' + tDate.getFullYear();
*/

		sOptionText = tDate.getShortMonthName() + ' ' + tDate.getFullYear() + ', '+ tDate.getDayOfWeekAsString().substr(0, 3) + ' ' + tDate.getDate() ; 
		sOptionValue = tDate.getDate() + '/' + (tDate.getMonth() + 1) + '/' + tDate.getFullYear();

		if(document.createElement && (navigator.userAgent.toLowerCase().indexOf('mac') == -1)){
			oOption = document.createElement('option');
			oFormElement.options.add(oOption);
			oOption.text = sOptionText;
			oOption.value = sOptionValue;
		}else{
			oOption = new Option(sOptionText, sOptionValue);
			oFormElement.options[oFormElement.length] = oOption;
		}


		if(oOption.index == nSelectedDay){
			oOption.selected = true;
		}
	}
}

function setNumberNights(oFormElement){
	if(oFormElement.form.nights != null){
		oFormElement.form.nights.selectedIndex = oFormElement.selectedIndex;
	}
}

function populateDepartureDateExt(oFormElement, oDateElement, oMonthYearElement,oNights){
	var sMonthYear = oMonthYearElement[oMonthYearElement.selectedIndex].value;
	var sDate = oDateElement[oDateElement.selectedIndex].value;
	var nSelectedDay = (oNights != null) ? parseInt(oNights[oNights.selectedIndex].value) - 1 : 0;
	var dtArrival = new Date(parseInt(sMonthYear.split('/')[1]), parseInt(sMonthYear.split('/')[0]) - 1, parseInt(sDate));
	var nDays = oFormElement.length;
	var tDate = dtArrival;
	var sOptionText;
	var sOptionValue;

	for(var i = oFormElement.options.length - 1; i >= 0; i--){
		oFormElement.options[i] = null;
	}
	for(var i = 0; i < nDays; i++){
		tDate.setDate(tDate.getDate() + 1);
/*
		oOption = document.createElement('option');
		oFormElement.options.add(oOption);
		oOption.text = tDate.getDayOfWeekAsString().substr(0, 3) + ' ' + tDate.getDate() + ' ' + tDate.getShortMonthName() + ', ' + tDate.getFullYear();
		oOption.value = tDate.getDate() + '/' + (tDate.getMonth() + 1) + '/' + tDate.getFullYear();
*/

		sOptionText = tDate.getShortMonthName() + ' ' + tDate.getFullYear() + ', '+ tDate.getDayOfWeekAsString().substr(0, 3) + ' ' + tDate.getDate() ; 
		sOptionValue = tDate.getDate() + '/' + (tDate.getMonth() + 1) + '/' + tDate.getFullYear();

		if(document.createElement && (navigator.userAgent.toLowerCase().indexOf('mac') == -1)){
			oOption = document.createElement('option');
			oFormElement.options.add(oOption);
			oOption.text = sOptionText;
			oOption.value = sOptionValue;
		}else{
			oOption = new Option(sOptionText, sOptionValue);
			oFormElement.options[oFormElement.length] = oOption;
		}


		if(oOption.index == nSelectedDay){
			oOption.selected = true;
		}
	}
}

function setNumberNightsExt(oFormElement,oNights){
	if(oNights != null){
		oNights.selectedIndex = oFormElement.selectedIndex;
	}
}


function datesOfStaySubmit(oForm){
	oForm.submitButton.value = 'Loading...';
	oForm.submitButton.disabled = true;
}