/* ****************************************************
 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

**************************************************** */


/* pop-up calendar functions */
var currFormElement = null;
var currFormElementMonthYear = null;
var currDepartureElement = null;
var bCalendarLoaded;
var bCalendarInLayer = false;

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 hostelSelected(oFormElement){
	//look up value currently in hostel drop down
	//if it is not -1, get value (which is id)
	//look for id in hostel id array
	//find matching index in num nights array
	//populate departure date / num nights 

	if (oFormElement != null){
		
		var hostelId = oFormElement[oFormElement.selectedIndex].value;
		if (hostelId != -1){
		
			var departureDateSelect = document.forms[0].departure_date;
			var numNightsSelect = document.forms[0].nights;			
			
			var i = 0;
			
//			for (var i = departureDateSelect.length - 1; i >= 0; i--){
//	   			departureDateSelect.options.remove(i);
// 			}
//			for (var i = numNightsSelect.length - 1; i >= 0; i--){
//	   			numNightsSelect.options.remove(i);
// 			}

			for(i = numNightsSelect.options.length - 1; i >= 0; i--){
				numNightsSelect.options[i] = null;
			}			
			
			if (departureDateSelect.length != null){
				for(i = departureDateSelect.options.length - 1; i >= 0; i--){
					departureDateSelect.options[i] = null;
				}
			}
 			
 			//look for id in hostel id array
			//find matching index in num nights array
			var hostelIdArray = window["hostelIdArray"];
			var numNightsArray = window["numNightsArray"];
			
			var hostelArrayIndex = -1;
			
			for (i = 0; i < hostelIdArray.length; i++){
				if (hostelId == hostelIdArray[i]){
					hostelArrayIndex = i;	
				}
			}
			
			if (hostelArrayIndex != -1){
				var numNights = numNightsArray[hostelArrayIndex];
				
				//find departure date and number of nights, remove entries and populate up until numNights
				
				for (i = 1; i <= numNights; i++){
					oOption = new Option(i, i);
					//numNightsSelect.options.add(oOption);
					numNightsSelect.options[numNightsSelect.length] = oOption;
					if (oOption.index == 0) oOption.selected = true;
				}
				
				var sSelectedMonthYear = 
					document.forms[0].arrival_month_year[document.forms[0].arrival_month_year.selectedIndex].value;
				var vMonth = sSelectedMonthYear.split('/')[0];
				var vYear = sSelectedMonthYear.split('/')[1];
				var vDay = document.forms[0].arrival_day[document.forms[0].arrival_day.selectedIndex].value;
				
				var tDate = new Date();
				tDate.setDate(vDay);
				tDate.setMonth(vMonth);
				tDate.setYear(vYear);
				
				if (departureDateSelect.length != null){
					for(i = 1; i <= numNights; i++){
						
						tDate.setDate(tDate.getDate() + 1);

						sOptionText = tDate.getDayOfWeekAsString().substr(0, 3) + ' ' + tDate.getDate() + ' ' + tDate.getShortMonthName() + ', ' + tDate.getFullYear()
						sOptionValue = tDate.getDate() + '/' + (tDate.getMonth() + 1) + '/' + tDate.getFullYear();
						
						oOption = new Option(sOptionText, sOptionValue);
						
						if (departureDateSelect != null){
							//departureDateSelect.options.add(oOption);
							departureDateSelect.options[departureDateSelect.length] = oOption;
							if (oOption.index == 0) oOption.selected = true;
						}
					}
				}							
			}
			document.getElementById("nights").removeAttribute("disabled");			
		} else {
		    var numNightsSelect = document.forms[0].nights;
		    for(i = numNightsSelect.options.length - 1; i >= 0; i--){
				numNightsSelect.options[i] = null;
			}	
			document.getElementById("nights").setAttribute("disabled","true");
									
		}
	}
}

function checkPage(oFormElement) {
    if(oFormElement != null)
	{
		var hostelId = oFormElement[oFormElement.selectedIndex].value;
		if(hostelId != -1) {
			document.getElementById("nights").removeAttribute("disabled");	
		}
	}
}

//selectDate(document.forms[0].arrival_month_year, document.forms[0].arrival_day, document.forms[0].departure_date);
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;
	var i = 0;

	for(i = oTargetElement.options.length - 1; i >= 0; i--){
		oTargetElement.options[i] = null;
	}

	for(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);
	}

}

//populateDepartureDate(document.forms[0].departure_date, document.forms[0].arrival_day, document.forms[0].arrival_month_year);
function populateDepartureDate(oFormElement, oDateElement, oMonthYearElement){
	//
	if (oFormElement.length == null)
	{
		return;
	}

	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;
	
	var i;

	for(i = oFormElement.options.length - 1; i >= 0; i--){
		oFormElement.options[i] = null;
	}
	for(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.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;
	}
}

function datesOfStaySubmit(oForm){
	oForm.submitButton.value = 'Loading...';
	oForm.submitButton.disabled = true;
}


function ArrivalToCalendar(CalendarClientID)
{
	//alert("Arrival");

	if (CSC_GetDate)
	{
		//alert("Arrival");
	
		var sSelectedMonthYear = 
			document.forms[0].arrival_month_year[document.forms[0].arrival_month_year.selectedIndex].value;

		var vMonth = sSelectedMonthYear.split('/')[0];
		var vYear = sSelectedMonthYear.split('/')[1];
		var vDay = document.forms[0].arrival_day[document.forms[0].arrival_day.selectedIndex].value;
	
		CSC_SetDate(CalendarClientID, vYear, vMonth-1, vDay, true, false);
	}
}

function CalendarToArrival(CalendarClientID)
{
	var i = 0;

	var vDateObject = CSC_GetDate(CalendarClientID);
	if (vDateObject != null)
	{
	
		var vMonth = vDateObject.getMonth() + 1;
		var vYear = vDateObject.getFullYear();
		var vDay = vDateObject.getDate();
		
		var vMonthYear = vMonth + "/" + vYear;
		
		var arrival_month_year = document.forms[0].arrival_month_year;
		for (i = 0; i < arrival_month_year.length; i++){
		
			if (arrival_month_year.options[i].value == vMonthYear){
				arrival_month_year.selectedIndex = i;
				selectDate(document.forms[0].arrival_month_year, document.forms[0].arrival_day, document.forms[0].departure_date);
			}
		}
				
		var arrival_day = document.forms[0].arrival_day;
		for (i = 0; i < arrival_day.length; i++){
			if (arrival_day.options[i].value == vDay){
				arrival_day.selectedIndex = i;
				selectDate(document.forms[0].arrival_month_year, document.forms[0].arrival_day, document.forms[0].departure_date);
			}
		}
		
		//populate departure calendar popup accordingly
		//var test = PDP_GetById("HostelPage1_DateDropDown_DeparturePopupCalendar");
		//test.xMinDate
	}
	else
	{
		//no selection. make your control contain no date
		alert("was null");
	}
}

function DepartureToCalendar(CalendarClientID){

	var selectedDate = 
		document.forms[0].departure_date[document.forms[0].departure_date.selectedIndex].value;
	
	var day = selectedDate.split('/')[0];
	var month = selectedDate.split('/')[1];
	var year = selectedDate.split('/')[2];

	CSC_SetDate(CalendarClientID, year, month-1, day, true, false);	
}

function CalendarToDeparture(CalendarClientID){
	var i = 0;

	//alert("CalendarToDeparture");

	var vDateObject = CSC_GetDate(CalendarClientID);
	if (vDateObject != null)
	{
		var vDate1 = CSC_GetDateString(CalendarClientID, "d/MM/yyyy", 0);
		var vDate2 = CSC_GetDateString(CalendarClientID, "d/M/yyyy", 0);
		
		var departure_date = document.forms[0].departure_date;
		for (i = 0; i < departure_date.length; i++){
			
			if ((departure_date.options[i].value == vDate1) || 
				(departure_date.options[i].value == vDate2))
			{
				departure_date.selectedIndex = i;
				setNumberNights(document.forms[0].departure_date);
			}
		}
	}
	else
	{
		//no selection. make your control contain no date
		alert("was null");
	}
}




function DateDropDownToCalendar(CalendarClientID, monthYearSelectClientID, daySelectClientID)
{	
	var monthYearSelect = document.getElementById(monthYearSelectClientID);
	var daySelect = document.getElementById(daySelectClientID);

	if (CSC_GetDate)
	{
		var sSelectedMonthYear = monthYearSelect[monthYearSelect.selectedIndex].value;
			
		var vMonth = sSelectedMonthYear.split('/')[0];
		var vYear = sSelectedMonthYear.split('/')[1];
		var vDay = daySelect[daySelect.selectedIndex].value;
	
		CSC_SetDate(CalendarClientID, vYear, vMonth-1, vDay, true, false);
	}
}

function CalendarToDateDropDown(CalendarClientID, monthYearSelectClientID, daySelectClientID)
{
	var monthYearSelect = document.getElementById(monthYearSelectClientID);
	var daySelect = document.getElementById(daySelectClientID);

	var i = 0;

	var vDateObject = CSC_GetDate(CalendarClientID);
	if (vDateObject != null)
	{
		var vMonth = vDateObject.getMonth() + 1;
		var vYear = vDateObject.getFullYear();
		var vDay = vDateObject.getDate();
		
		var vMonthYear = vMonth + "/" + vYear;
		
		for (i = 0; i < monthYearSelect.length; i++){
		
			if (monthYearSelect.options[i].value == vMonthYear){
				monthYearSelect.selectedIndex = i;
				selectDate(monthYearSelect, daySelect, null);
			}
		}
		
		for (i = 0; i < daySelect.length; i++){
			if (daySelect.options[i].value == vDay){
				daySelect.selectedIndex = i;
				selectDate(monthYearSelect, daySelect, null);
			}
		}
	}
}






