/* ****************************************************
 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.  
 
****************************************************  */

/* ****************************************************

	Application: YHA
	
	File: calendar_popup.js
	
	Type: Javascript Source
	
	Desc: functions for the calendar popup
	
	Comments: 
	
	Author: Lindsay Evans <lindsay@redsquare.com.au>
	Date:  15/07/01
	Version: 1.0.0
	
	***** Modification Details *****
	
	DATE  	NAME  	MODIFICATION
	9/9/04	Pat		updated date formatting to multi languages
	7/10/04	pat		changes due to BE bug fix for IE mac.
**************************************************** */


/* pop-up calendar functions */
var currFormElement = null;
var currFormElementMonthYear = null;
var currDepartureElement = null;
var bCalendarLoaded;
var bCalendarInLayer = false;
var bFloated = 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.nights != 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';
		if (bFloated) {
			oDateChooser.style.left = '54%';
			oDateChooser.style.top = parseInt(calLyrTop-100) + '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 + '&langCode=' + langCode;
}

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);

		//changed for japanese	dd d
		sOptionText = i + ' ' + tDate.getDayOfWeekAsString();
		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();
*/

		//yyyy mmmm dd dddd japanese format
		sOptionText = tDate.getFullYear() + ' ' + tDate.getMonthName() + ' ' + tDate.getDate() + ' ' + tDate.getDayOfWeekAsString().substr(0, 3);
		//sOptionText = tDate.getDayOfWeekAsString().substr(0, 3) + ' ' + tDate.getDate() + ' ' + tDate.getShortMonthName() + ', ' + tDate.getFullYear()
		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;
	}
}
